Esempio n. 1
0
  @Override
  public void addLocation(LocationDatabaseObject locationDO) {

    if (locationDO != null) {
      new ServerAPI();
      MapData mapData =
          MapData.readMapData(
              locationDO.getLocationDataPath(
                  ServerAPI.getDataTypeIDByAlgorithmClassName(ALGORITHM_CLASSNAME)));
      //			new ServerAPI();
      //
      //	System.out.println(locationDO.getLocationDataPath(ServerAPI.getDataTypeIDByAlgorithmClassName(ALGORITHM_CLASSNAME)));
      //			System.out.println(mapData);
      if (mapData == null) {
        return;
      } else {
        if (mapData instanceof Vd2_3MapData) {
          cache.put(locationDO.getLocationId(), (Vd2_3MapData) mapData);
        }
      }
    }
  }
  public void callMapdataFunction(ObjectInputStream in, ObjectOutputStream out) {
    MapData mapData;
    try {
      mapData = (MapData) in.readObject();

      if (mapData instanceof WifiMapData || mapData instanceof Vd2_3MapData) {
        LocationDatabaseObject locationDatabaseObject =
            ServerAPI.searchLocationByLocationId(mapData.getLocationId());

        LocationDatabaseObject rootLocation =
            LocationDataManagement.getRootLocationByLocationObject(locationDatabaseObject);
        Long rootLocationMAC = rootLocation.getMacaddressIDForBuildDevice();
        Long newMapMAC;

        if (mapData instanceof WifiMapData) {
          newMapMAC = ((WifiMapData) mapData).getScannerMAC();
        } else {
          newMapMAC = ((Vd2_3MapData) mapData).getScannerMAC();
        }

        System.out.println(rootLocationMAC + " : " + newMapMAC);
        out.writeBoolean(rootLocationMAC.compareTo(newMapMAC) != 0);
        out.flush();

        boolean replaceRootMAC = in.readBoolean();

        if (!replaceRootMAC) {
          out.writeBoolean(true);
          out.flush();
          return;
        }

        if (rootLocationMAC.compareTo(newMapMAC) != 0) {
          System.out.println(rootLocationMAC + " : " + newMapMAC);
          if (mapData instanceof Vd2_3MapData) {
            Vd2_3MapData vd2_3MapData = (Vd2_3MapData) mapData;
            if (rootLocation.getMacaddressIDForBuildDevice() == 0
                || rootLocation.getMacaddressIDForBuildDevice() != vd2_3MapData.getScannerMAC()) {
              rootLocation.setMacaddressIDForBuildDevice(vd2_3MapData.getScannerMAC());
              LocationDataManagement.updateLocation(rootLocation);
            }
          } else if (mapData instanceof WifiMapData) {
            WifiMapData wifiMapData = (WifiMapData) mapData;
            if (rootLocation.getMacaddressIDForBuildDevice() == 0
                || rootLocation.getMacaddressIDForBuildDevice() != wifiMapData.getScannerMAC()) {
              rootLocation.setMacaddressIDForBuildDevice(wifiMapData.getScannerMAC());
              LocationDataManagement.updateLocation(rootLocation);
            }
          }

          // need implement delete all wifi map
          String dataTypeId =
              ServerAPI.getDataTypeIdByDataTypeClassName(Vd2_3MapData.class.getName());
          LocationDataManagement.deleleAllMapData(rootLocation, dataTypeId);
          dataTypeId = ServerAPI.getDataTypeIdByDataTypeClassName(WifiMapData.class.getName());
          LocationDataManagement.deleleAllMapData(rootLocation, dataTypeId);
        }
      }

      String datatypeId =
          ServerAPI.getDataTypeIdByDataTypeClassName(mapData.getDataTypeClassName());
      File dirUser = new File(ServerConfig.dirData + "/" + mapData.getUserId() + "/");
      if (!dirUser.exists()) {
        dirUser.mkdirs();
      }
      MapData.writeMapData(
          mapData,
          dirUser.getAbsolutePath()
              + "/"
              + mapData.getLocationId()
              + "_"
              + datatypeId
              + ServerConfig.extensionMapDataFile);

      if (mapData instanceof WifiMapData || mapData instanceof Vd2_3MapData) {
        EntityManager entityManager =
            LocalizationDataManager.getEntityManagerFactory().createEntityManager();
        LocationDatabaseObject locationDatabaseObject =
            ServerAPI.searchLocationByLocationId(mapData.getLocationId());
        ArrayList<Long> listMacAddress = new ArrayList<Long>();

        if (mapData instanceof WifiMapData) {
          WifiMapData wifiMapData = (WifiMapData) mapData;
          for (ScanningPoint scanningPoint : wifiMapData.getSpsList()) {
            for (AccessPoint accessPoint : scanningPoint.getApsList()) {
              if (!listMacAddress.contains(accessPoint.getMACAddress())) {
                listMacAddress.add(accessPoint.getMACAddress());
              }
            }
          }
        } else {
          Vd2_3MapData vd2_3MapData = (Vd2_3MapData) mapData;
          for (APStatistic apStatistic : vd2_3MapData.getStatistics().values()) {
            if (!listMacAddress.contains(apStatistic.accessPoint.getMACAddress())) {
              listMacAddress.add(apStatistic.accessPoint.getMACAddress());
            }
          }
        }

        MacAddressDatabaseObject macAddress = null;
        // Boolean checkReAdd = false;
        for (Long strMACAddress : listMacAddress) {
          macAddress = ServerAPI.searchMacAddressByID(strMACAddress);
          // entityManager.getTransaction().begin();

          if (macAddress == null) {
            System.out.println("macAddress = null");
            macAddress = new MacAddressDatabaseObject();
            macAddress.setMacAddressId(strMACAddress);
            entityManager.persist(macAddress);
            macAddress.setLocations(new ArrayList<LocationDatabaseObject>());
            System.out.println("end add");
            // checkReAdd = true;
          }

          boolean contains = false;

          for (LocationDatabaseObject locationDatabaseObject2 : macAddress.getLocations()) {
            // System.out.println(locationDatabaseObject2.getLocationId());
            if (locationDatabaseObject2
                .getLocationId()
                .equals(locationDatabaseObject.getLocationId())) {
              contains = true;
            }
          }

          if (!contains) {
            System.out.println(
                macAddress.getMacAddressId() + ":" + locationDatabaseObject.getLocationId());
            macAddress.getLocations().add(locationDatabaseObject);
            macAddress.addLocation(locationDatabaseObject.getLocationId());
            // LocationDataManagement.updateLocation(location);
            // entityManager.merge(macAddress);
          }
        }
        // entityManager.getTransaction().commit();
      }

      out.writeBoolean(true);
      out.flush();
      // System.out.println("++++++++++++++++++++++++++++++");
      return;
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (ClassNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    try {
      out.writeBoolean(false);
      out.flush();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }