/** * Method saving a poi and all the associated foreign collections without transaction management. * * <p>Do not call the DAO directly to save a poi, use this method. * * @param poi The poi to save * @return The saved poi * @see #savePoi(Poi) */ private Poi savePoiNoTransaction(Poi poi) { List<PoiTag> poiTagsToRemove = poiTagDao.queryByPoiId(poi.getId()); poiTagsToRemove.removeAll(poi.getTags()); for (PoiTag poiTag : poiTagsToRemove) { poiTagDao.delete(poiTag); } List<PoiNodeRef> poiNodeRefsToRemove = poiNodeRefDao.queryByPoiId(poi.getId()); poiNodeRefsToRemove.removeAll(poi.getNodeRefs()); for (PoiNodeRef poiNodeRef : poiNodeRefsToRemove) { poiNodeRefDao.delete(poiNodeRef); } poiDao.createOrUpdate(poi); if (poi.getTags() != null) { for (PoiTag poiTag : poi.getTags()) { poiTag.setPoi(poi); poiTagDao.createOrUpdate(poiTag); } } if (poi.getNodeRefs() != null) { for (PoiNodeRef poiNodeRef : poi.getNodeRefs()) { poiNodeRef.setPoi(poi); poiNodeRefDao.createOrUpdate(poiNodeRef); } } return poi; }
/** * Get the backup data and put it back in the active Poi, at the end the backup Poi is deleted. * * @param poiId The Id of the Poi to revert. */ private void revertPoi(Long poiId) { Poi poi = poiDao.queryForId(poiId); Poi backup; Long oldPoiId = poi.getOldPoiId(); if (oldPoiId != null) { backup = poiDao.queryForId(oldPoiId); // we retrieve the backup data and put it back backup.setOld(false); backup.setId(poi.getId()); savePoi(backup); // we prepare to delete the modifications poi.setId(oldPoiId); } deletePoi(poi); }
/** * Merge POIs in parameters to those already in the database. * * @param remotePois The POIs to merge. */ public void mergeFromOsmPois(List<Poi> remotePois) { List<Poi> toMergePois = new ArrayList<>(); Map<String, Poi> remotePoisMap = new HashMap<>(); // Map remote Poi backend Ids for (Poi poi : remotePois) { remotePoisMap.put(poi.getBackendId(), poi); } // List matching Pois List<Poi> localPois = poiDao.queryForBackendIds(remotePoisMap.keySet()); Map<String, Poi> localPoisMap = new HashMap<>(); // Map matching local Pois for (Poi localPoi : localPois) { localPoisMap.put(localPoi.getBackendId(), localPoi); } // Browse remote pois for (Poi remotePoi : remotePois) { Poi localPoi = localPoisMap.get(remotePoi.getBackendId()); Long localVersion = -1L; // If localPoi is versioned if (localPoi != null && localPoi.getVersion() != null) { localVersion = Long.valueOf(localPoi.getVersion()); } // Compute version delta if (Long.valueOf(remotePoi.getVersion()) > localVersion) { // Remote version is newer, override existing one if (localPoi != null) { remotePoi.setId(localPoi.getId()); } // This Poi should be updated toMergePois.add(remotePoi); } } // savePois of either new or existing Pois savePois(toMergePois); }