/** * Updates the local database after the location was updated on the server * * @param request {@link Request} performed * @param response {@link Response} received */ private void updateLocationPerformed(Request<Location> request, Response<Void> response) { Location l = request.getData(); if (l == null) { return; } Location dbLoc = locHome.get(l); if (!l.equals(dbLoc)) { boolean success = locHome.update(l); if (!success) { Log.i(TAG, "update of location " + l + " failed"); } } }
/** * Synchronizes the local {@link Location} database * * @param request {@link Request} performed * @param response {@link Response} received */ private void getLocationListPerformed(Request<Void> request, Response<List<Location>> response) { List<Location> dbList = locHome.getAll(); HashMap<Integer, Location> loc = new HashMap<Integer, Location>(); for (Location l : dbList) { loc.put(l.getRemoteId(), l); } List<Location> remote = response.getData(); for (Location l : remote) { if (loc.containsKey(l.getRemoteId())) { Location dbLoc = loc.get(l.getRemoteId()); l.setLocalId(dbLoc.getLocalId()); if (dbLoc.getMap() != null && l.getMap() != null) { ((Map) l.getMap()).setLocalId(((Map) dbLoc.getMap()).getLocalId()); } if (!l.equals(dbLoc)) { if (!locHome.update(l)) { Log.w(TAG, "update of location " + l + " failed"); } } } else { if (l.getMap() == null) { continue; } Map m = mapHome.getByRemoteId(((Map) l.getMap()).getRemoteId()); if (m != null) { l.setMap(m); } locHome.add(l); } } loc.clear(); for (Location l : remote) { loc.put(l.getRemoteId(), l); } for (Location l : dbList) { if (!loc.containsKey(l.getRemoteId())) { locHome.remove(l); } } }
/** * Adds {@link Location} if not yet in local database * * @param request {@link Request} performed * @param response {@link Response} received */ private void getLocationPerformed(Request<Measurement> request, Response<Location> response) { Location l = response.getData(); if (l == null) { return; } Location dbLoc = locHome.getByRemoteId(l.getRemoteId()); if (dbLoc != null) { l.setLocalId(dbLoc.getLocalId()); if (!l.equals(dbLoc)) { if (!locHome.update(l)) { Log.w(TAG, "update of location " + l + " failed"); } } } else { locHome.add(l); } }