Author: blavenier Date: 2014-04-25 13:39:08 +0200 (Fri, 25 Apr 2014) New Revision: 1735 Url: http://forge.codelutin.com/projects/tutti/repository/revisions/1735 Log: Fix Issue #4995 : do not create a new entity when existing in the list of regulation area Modified: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/FishingOperationPersistenceServiceImpl.java trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/FishingOperationPersistenceServiceAno4995Test.java Modified: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/FishingOperationPersistenceServiceImpl.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/FishingOperationPersistenceServiceImpl.java 2014-04-25 08:56:17 UTC (rev 1734) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/FishingOperationPersistenceServiceImpl.java 2014-04-25 11:39:08 UTC (rev 1735) @@ -31,6 +31,7 @@ import fr.ifremer.adagio.core.dao.data.fishingArea.FishingArea; import fr.ifremer.adagio.core.dao.data.fishingArea.FishingArea2RegulationLocation; import fr.ifremer.adagio.core.dao.data.fishingArea.FishingArea2RegulationLocationDao; +import fr.ifremer.adagio.core.dao.data.fishingArea.FishingArea2RegulationLocationImpl; import fr.ifremer.adagio.core.dao.data.fishingArea.FishingArea2RegulationLocationPK; import fr.ifremer.adagio.core.dao.data.fishingArea.FishingAreaDao; import fr.ifremer.adagio.core.dao.data.fishingArea.FishingAreaImpl; @@ -83,6 +84,7 @@ import fr.ifremer.tutti.persistence.service.util.VesselPersonFeaturesPersistenceHelper; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.MapUtils; +import org.apache.commons.lang.ObjectUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -737,7 +739,6 @@ protected void beanToEntity(FishingOperation source, fr.ifremer.adagio.core.dao.data.operation.FishingOperation target) { - // Retrieve entities : FishingTrip and ScientificCruise ScientificCruise scientificCruise; FishingTrip fishingTrip = target.getFishingTrip(); @@ -1144,8 +1145,8 @@ // ---------------------------------------------------------------- // Fishing Area : Strata, substrata, localite --- // ---------------------------------------------------------------- - - // Fishing Area : + + // Compute a statistical rectangle, using lat/long (to be used in FishingArea.location) Integer statisticalLocationId = null; if (source.getGearShootingStartLatitude() != null && source.getGearShootingStartLongitude() != null) { statisticalLocationId = locationService.getLocationIdByLatLong(source.getGearShootingStartLatitude(), source.getGearShootingStartLongitude()); @@ -1156,18 +1157,12 @@ // Strata : if (source.getStrata() != null && source.getStrata().getId() != null) { - FishingArea2RegulationLocation fa2rl = FishingArea2RegulationLocation.Factory.newInstance(); - FishingArea2RegulationLocationPK fa2rlPK = new FishingArea2RegulationLocationPK(); - fa2rl.setFishingArea2RegulationLocationPk(fa2rlPK); - fa2rlPK.setFishingArea((FishingAreaImpl) fishingArea); - fa2rlPK.setLocation(load(LocationImpl.class, source.getStrata().getIdAsInt())); - + FishingArea2RegulationLocation fa2rl = getFishingArea2RegulationLocation(fishingArea, + source.getStrata().getIdAsInt(), + true /*create if need*/); notChangedRegulationLocation.remove(fa2rl); - if (fishingArea.getRegulationLocations() == null) { - fishingArea.setRegulationLocations(Sets.newHashSet(fa2rl)); - } else { - fishingArea.getRegulationLocations().add(fa2rl); - } + + // If no statistical location define yet, then use strata if (statisticalLocationId == null) { statisticalLocationId = source.getStrata().getIdAsInt(); } @@ -1175,19 +1170,12 @@ // Sub-Strata : if (source.getSubStrata() != null && source.getSubStrata().getId() != null) { - FishingArea2RegulationLocation fa2rl = FishingArea2RegulationLocation.Factory.newInstance(); - FishingArea2RegulationLocationPK fa2rlPK = new FishingArea2RegulationLocationPK(); - fa2rl.setFishingArea2RegulationLocationPk(fa2rlPK); - fa2rlPK.setFishingArea((FishingAreaImpl) fishingArea); - fa2rlPK.setLocation(load(LocationImpl.class, source.getSubStrata().getIdAsInt())); - + FishingArea2RegulationLocation fa2rl = getFishingArea2RegulationLocation(fishingArea, + source.getSubStrata().getIdAsInt(), + true /*create if need*/); notChangedRegulationLocation.remove(fa2rl); - - if (fishingArea.getRegulationLocations() == null) { - fishingArea.setRegulationLocations(Sets.newHashSet(fa2rl)); - } else { - fishingArea.getRegulationLocations().add(fa2rl); - } + + // If no statistical location define yet, then use sub-strata if (statisticalLocationId == null) { statisticalLocationId = source.getSubStrata().getIdAsInt(); } @@ -1195,23 +1183,18 @@ // Localite : if (source.getLocation() != null && source.getLocation().getId() != null) { - FishingArea2RegulationLocation fa2rl = FishingArea2RegulationLocation.Factory.newInstance(); - FishingArea2RegulationLocationPK fa2rlPK = new FishingArea2RegulationLocationPK(); - fa2rl.setFishingArea2RegulationLocationPk(fa2rlPK); - fa2rlPK.setFishingArea((FishingAreaImpl) fishingArea); - fa2rlPK.setLocation(load(LocationImpl.class, source.getLocation().getIdAsInt())); - + FishingArea2RegulationLocation fa2rl = getFishingArea2RegulationLocation(fishingArea, + source.getLocation().getIdAsInt(), + true /*create if need*/); notChangedRegulationLocation.remove(fa2rl); - if (fishingArea.getRegulationLocations() == null) { - fishingArea.setRegulationLocations(Sets.newHashSet(fa2rl)); - } else { - fishingArea.getRegulationLocations().add(fa2rl); - } + + // If no statistical location define yet, then use Localité if (statisticalLocationId == null) { statisticalLocationId = source.getLocation().getIdAsInt(); } } + // Remove unused regulation locations fishingArea.getRegulationLocations().removeAll(notChangedRegulationLocation); // Fishing Area location (should be a statistical location) @@ -1223,7 +1206,6 @@ } } else if (statisticalLocationId != null) { fishingArea.setLocation(load(LocationImpl.class, statisticalLocationId)); - //gearUseFeatures.getFishingAreas().add(fishingArea); } } @@ -1335,4 +1317,47 @@ target.getOperationVesselAssociations().addAll(toAdd); } } + + /** + * Issue #4995 : use this method to avoid re-create of existing regulation location + * @param fishingArea + * @param regulationLocationId + * @param createIfNotExists + * @return a entity FishingArea2RegulationLocation, or null if not found and createIfNotExists=false + */ + private FishingArea2RegulationLocation getFishingArea2RegulationLocation(FishingArea fishingArea, int regulationLocationId, boolean createIfNotExists) { + Preconditions.checkNotNull(fishingArea); + + // Create the PK + FishingArea2RegulationLocationPK pk = new FishingArea2RegulationLocationPK(); + pk.setFishingArea((FishingAreaImpl) fishingArea); + pk.setLocation(load(LocationImpl.class, regulationLocationId)); + + // Retrieve exiting regulation location + FishingArea2RegulationLocation fa2rl = null; + if (CollectionUtils.isNotEmpty(fishingArea.getRegulationLocations())) { + for(FishingArea2RegulationLocation existingFa2rl: fishingArea.getRegulationLocations()) { + FishingArea2RegulationLocationPK existingPk = existingFa2rl.getFishingArea2RegulationLocationPk(); + if (ObjectUtils.equals(existingPk, pk)) { + return fa2rl; + } + } + } + + // If not exists, create if need + if (fa2rl != null || !createIfNotExists) { + return fa2rl; + } + + fa2rl = FishingArea2RegulationLocation.Factory.newInstance(); + fa2rl.setFishingArea2RegulationLocationPk(pk); + + if (fishingArea.getRegulationLocations() == null) { + fishingArea.setRegulationLocations(Sets.newHashSet(fa2rl)); + } else { + fishingArea.getRegulationLocations().add(fa2rl); + } + + return fa2rl; + } } Modified: trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/FishingOperationPersistenceServiceAno4995Test.java =================================================================== --- trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/FishingOperationPersistenceServiceAno4995Test.java 2014-04-25 08:56:17 UTC (rev 1734) +++ trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/FishingOperationPersistenceServiceAno4995Test.java 2014-04-25 11:39:08 UTC (rev 1735) @@ -37,7 +37,7 @@ fishingOperation.setCruise(cruise); } - @Test(expected = NonUniqueObjectException.class) + @Test public void saveFishingOperation() { assertNotNull(fishingOperation);