r240 - in trunk: tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service tutti-persistence-adagio/src/main/java/fr/ifremer/tutti/persistence/service tutti-persistence-adagio/src/main/resources tutti-persistence-adagio/src/test/java/fr/ifremer/tutti/persistence tutti-persistence-adagio/src/test/java/fr/ifremer/tutti/persistence/service
Author: blavenier Date: 2013-01-23 11:00:00 +0100 (Wed, 23 Jan 2013) New Revision: 240 Url: http://forge.codelutin.com/projects/tutti/repository/revisions/240 Log: ref refs #1920: [Persistence] Adagio Donnees thematiques - Add storage of EnvironmentCaracteristics and GearShootingCaracteristics in createFishingOperation() - Add loading of EnvironmentCaracteristics and GearShootingCaracteristics in getFishingOperation() - Add of FishingOperation.save(), and manage cas when caracteristics have been deleted Modified: trunk/tutti-persistence-adagio/src/main/java/fr/ifremer/tutti/persistence/service/AbstractPersistenceService.java trunk/tutti-persistence-adagio/src/main/java/fr/ifremer/tutti/persistence/service/CruisePersistenceServiceImpl.java trunk/tutti-persistence-adagio/src/main/java/fr/ifremer/tutti/persistence/service/FishingOperationPersistenceServiceImpl.java trunk/tutti-persistence-adagio/src/main/java/fr/ifremer/tutti/persistence/service/ReferentialPersistenceServiceImpl.java trunk/tutti-persistence-adagio/src/main/java/fr/ifremer/tutti/persistence/service/TuttiEnumerationFile.java trunk/tutti-persistence-adagio/src/main/resources/queries-override.hbm.xml trunk/tutti-persistence-adagio/src/main/resources/tutti-db-enumerations.properties trunk/tutti-persistence-adagio/src/test/java/fr/ifremer/tutti/persistence/DatabaseFixtures.java trunk/tutti-persistence-adagio/src/test/java/fr/ifremer/tutti/persistence/service/CruisePersistenceServiceTest.java trunk/tutti-persistence-adagio/src/test/java/fr/ifremer/tutti/persistence/service/FishingOperationPersistenceServiceTest.java trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/ReferentialPersistenceService.java Modified: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/ReferentialPersistenceService.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/ReferentialPersistenceService.java 2013-01-21 15:51:02 UTC (rev 239) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/ReferentialPersistenceService.java 2013-01-23 10:00:00 UTC (rev 240) @@ -129,6 +129,8 @@ Person getPerson(Integer personId); Gear getGear(Integer gearId); + + Caracteristic getCaracteristic(Integer pmfmId); boolean isSortedQualitativeValue(CaracteristicQualitativeValue value); } Modified: trunk/tutti-persistence-adagio/src/main/java/fr/ifremer/tutti/persistence/service/AbstractPersistenceService.java =================================================================== --- trunk/tutti-persistence-adagio/src/main/java/fr/ifremer/tutti/persistence/service/AbstractPersistenceService.java 2013-01-21 15:51:02 UTC (rev 239) +++ trunk/tutti-persistence-adagio/src/main/java/fr/ifremer/tutti/persistence/service/AbstractPersistenceService.java 2013-01-23 10:00:00 UTC (rev 240) @@ -35,6 +35,7 @@ import org.springframework.beans.factory.annotation.Autowired; import javax.annotation.Resource; +import java.io.Serializable; import java.util.Iterator; /** @@ -118,4 +119,9 @@ } return query; } + + protected <T extends Serializable> T load(Class<? extends T> clazz, Serializable id) { + T load = (T) getCurrentSession().load(clazz, id); + return load; + } } Modified: trunk/tutti-persistence-adagio/src/main/java/fr/ifremer/tutti/persistence/service/CruisePersistenceServiceImpl.java =================================================================== --- trunk/tutti-persistence-adagio/src/main/java/fr/ifremer/tutti/persistence/service/CruisePersistenceServiceImpl.java 2013-01-21 15:51:02 UTC (rev 239) +++ trunk/tutti-persistence-adagio/src/main/java/fr/ifremer/tutti/persistence/service/CruisePersistenceServiceImpl.java 2013-01-23 10:00:00 UTC (rev 240) @@ -54,10 +54,16 @@ import fr.ifremer.adagio.core.dao.referential.QualityFlag; import fr.ifremer.adagio.core.dao.referential.QualityFlagDao; import fr.ifremer.adagio.core.dao.referential.gear.GearDao; +import fr.ifremer.adagio.core.dao.referential.gear.GearImpl; import fr.ifremer.adagio.core.dao.referential.location.Location; import fr.ifremer.adagio.core.dao.referential.location.LocationDao; +import fr.ifremer.adagio.core.dao.referential.pmfm.Pmfm; import fr.ifremer.adagio.core.dao.referential.pmfm.PmfmDao; +import fr.ifremer.adagio.core.dao.referential.pmfm.PmfmImpl; +import fr.ifremer.adagio.core.dao.referential.pmfm.QualitativeValue; import fr.ifremer.adagio.core.dao.referential.pmfm.QualitativeValueDao; +import fr.ifremer.adagio.core.dao.referential.pmfm.QualitativeValueDaoImpl; +import fr.ifremer.adagio.core.dao.referential.pmfm.QualitativeValueImpl; import fr.ifremer.adagio.core.dao.referential.vessel.VesselDao; import fr.ifremer.adagio.core.dao.technical.synchronization.SynchronizationStatus; import fr.ifremer.tutti.persistence.entities.data.Cruise; @@ -247,15 +253,24 @@ if (result.getGear() == null) { Iterator<Object[]> list = queryList( "allCruiseGears", - "cruiseId", IntegerType.INSTANCE, Integer.valueOf(id)); + "cruiseId", IntegerType.INSTANCE, Integer.valueOf(id), + "pmfmIdTrawlNet", IntegerType.INSTANCE, enumeration.PMFM_ID_TRAWL_NET); List<Gear> gears = Lists.newArrayList(); + int maxTrawlNetFound = 0; while (list.hasNext()) { Object[] gearRow = list.next(); Gear target = referentielService.getGear((Integer) gearRow[0]); + Float trawlNet = (Float) gearRow[1]; + if (trawlNet != null && trawlNet.intValue() > maxTrawlNetFound) { + maxTrawlNetFound = trawlNet.intValue(); + } gears.add(target); } result.setGear(gears); + if (maxTrawlNetFound > 0) { + result.setTrawlNet(maxTrawlNetFound); + } } return result; } @@ -502,10 +517,12 @@ GearPhysicalFeatures gearPhysicalFeature = GearPhysicalFeatures.Factory.newInstance(); gearPhysicalFeature.setFishingTrip(fishingTrip); - fr.ifremer.adagio.core.dao.referential.gear.Gear gear = gearDao.load(gearId); - if (gear == null) { - throw new DataRetrievalFailureException("Gear not found with id=" + gearId); - } + + fr.ifremer.adagio.core.dao.referential.gear.Gear gear = (fr.ifremer.adagio.core.dao.referential.gear.Gear)getCurrentSession().load(GearImpl.class, gearId); + //fr.ifremer.adagio.core.dao.referential.gear.Gear gear = gearDao.load(gearId); + //if (gear == null) { + // throw new DataRetrievalFailureException("Gear not found with id=" + gearId); + //} gearPhysicalFeature.setGear(gear); if (fishingTrip.getGearPhysicalFeatures() == null) { fishingTrip.setGearPhysicalFeatures(Lists.newArrayList(gearPhysicalFeature)); @@ -547,7 +564,9 @@ } gearPhysicalMeasurement.setQualityFlag(qualityFlagDao.load(enumeration.QUALITY_FLAG_CODE_NOT_QUALIFIED)); gearPhysicalMeasurement.setDepartment(scientificCruise.getRecorderDepartment()); - gearPhysicalMeasurement.setPmfm(pmfmDao.load(pmfmId)); + Pmfm pmfm = (Pmfm)getCurrentSession().load(PmfmImpl.class, pmfmId); + gearPhysicalMeasurement.setPmfm(pmfm); + //gearPhysicalMeasurement.setPmfm(pmfmDao.load(pmfmId)); } return gearPhysicalMeasurement; @@ -577,7 +596,9 @@ gearPhysicalMeasurement.setNumericalValue(numericalValue); } else if (qualitativevalueId != null) { - gearPhysicalMeasurement.setQualitativeValue(qualitativeValueDao.load(qualitativevalueId)); + QualitativeValue qv = (QualitativeValue)getCurrentSession().load(QualitativeValueImpl.class, qualitativevalueId); + gearPhysicalMeasurement.setQualitativeValue(qv); + //gearPhysicalMeasurement.setQualitativeValue(qualitativeValueDao.load(qualitativevalueId)); } return gearPhysicalMeasurement; Modified: trunk/tutti-persistence-adagio/src/main/java/fr/ifremer/tutti/persistence/service/FishingOperationPersistenceServiceImpl.java =================================================================== --- trunk/tutti-persistence-adagio/src/main/java/fr/ifremer/tutti/persistence/service/FishingOperationPersistenceServiceImpl.java 2013-01-21 15:51:02 UTC (rev 239) +++ trunk/tutti-persistence-adagio/src/main/java/fr/ifremer/tutti/persistence/service/FishingOperationPersistenceServiceImpl.java 2013-01-23 10:00:00 UTC (rev 240) @@ -24,17 +24,22 @@ * #L% */ +import java.io.Serializable; import java.sql.Timestamp; +import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; +import java.util.HashSet; import java.util.Iterator; import java.util.List; +import java.util.Set; import javax.annotation.Resource; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.hibernate.FlushMode; import org.hibernate.type.IntegerType; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataIntegrityViolationException; @@ -42,9 +47,14 @@ import org.springframework.stereotype.Service; import com.google.common.collect.Lists; +import com.google.common.collect.Sets; +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.FishingArea2RegulationLocationPK; import fr.ifremer.adagio.core.dao.data.measure.GearPhysicalMeasurement; import fr.ifremer.adagio.core.dao.data.measure.GearUseMeasurement; +import fr.ifremer.adagio.core.dao.data.measure.Measurement; import fr.ifremer.adagio.core.dao.data.measure.VesselUseMeasurement; import fr.ifremer.adagio.core.dao.data.operation.FishingOperationDao; import fr.ifremer.adagio.core.dao.data.operation.Operation; @@ -56,14 +66,23 @@ import fr.ifremer.adagio.core.dao.data.vessel.feature.use.VesselUseFeatures; import fr.ifremer.adagio.core.dao.data.vessel.feature.use.isActive; import fr.ifremer.adagio.core.dao.data.vessel.position.VesselPosition; -import fr.ifremer.adagio.core.dao.referential.QualityFlagDao; -import fr.ifremer.adagio.core.dao.referential.gear.Gear; +import fr.ifremer.adagio.core.dao.referential.QualityFlagImpl; import fr.ifremer.adagio.core.dao.referential.gear.GearDao; +import fr.ifremer.adagio.core.dao.referential.gear.GearImpl; import fr.ifremer.adagio.core.dao.referential.location.LocationDao; -import fr.ifremer.adagio.core.dao.referential.pmfm.PmfmDao; -import fr.ifremer.adagio.core.dao.referential.pmfm.PmfmId; -import fr.ifremer.adagio.core.dao.referential.pmfm.QualitativeValueDao; +import fr.ifremer.adagio.core.dao.referential.location.LocationExtendDao; +import fr.ifremer.adagio.core.dao.referential.location.LocationImpl; +import fr.ifremer.adagio.core.dao.referential.location.LocationLevelId; +import fr.ifremer.adagio.core.dao.referential.pmfm.Pmfm; +import fr.ifremer.adagio.core.dao.referential.pmfm.PmfmImpl; +import fr.ifremer.adagio.core.dao.referential.pmfm.QualitativeValue; +import fr.ifremer.adagio.core.dao.referential.pmfm.QualitativeValueImpl; +import fr.ifremer.adagio.core.service.referential.location.LocationService; +import fr.ifremer.tutti.persistence.entities.CaracteristicMap; import fr.ifremer.tutti.persistence.entities.data.FishingOperation; +import fr.ifremer.tutti.persistence.entities.referential.Caracteristic; +import fr.ifremer.tutti.persistence.entities.referential.CaracteristicType; +import fr.ifremer.tutti.persistence.entities.referential.FishingOperationLocation; /** * @author tchemit <chemit@codelutin.com> @@ -80,29 +99,20 @@ protected MeasurementService measurementService; @Autowired(required = true) - protected ReferentialPersistenceService referentielService; + protected ReferentialPersistenceService referentialService; + @Resource(name = "scientificCruiseDao") + protected ScientificCruiseDao scientificCruiseDao; + @Resource(name = "fishingOperationDao") protected FishingOperationDao fishingOperationDao; + + @Resource(name = "locationService") + protected LocationService locationService; - @Resource(name = "gearDao") - protected GearDao gearDao; - @Resource(name = "locationDao") - protected LocationDao locationDao; - - @Resource(name = "scientificCruiseDao") - protected ScientificCruiseDao scientificCruiseDao; + protected LocationExtendDao locationDao; - @Resource(name = "qualityFlagDao") - protected QualityFlagDao qualityFlagDao; - - @Resource(name = "pmfmDao") - protected PmfmDao pmfmDao; - - @Resource(name = "qualitativeValueDao") - protected QualitativeValueDao qualitativeValueDao; - protected static Float DEFAULT_EMPTY_LATITUDE = 0.0001f; protected static Float DEFAULT_EMPTY_LONGITUDE = 0.0001f; @@ -124,7 +134,10 @@ "pmfmIdTrawlDistance", IntegerType.INSTANCE, enumeration.PMFM_ID_TRAWL_DISTANCE, "pmfmIdRectilinearOperation", IntegerType.INSTANCE, enumeration.PMFM_ID_RECTILINEAR_OPERATION, "pmfmIdHaulValid", IntegerType.INSTANCE, enumeration.PMFM_ID_HAUL_VALID, - "pmfmTrawlNetNumber", IntegerType.INSTANCE, enumeration.PMFM_ID_TRAWL_NET_NUMBER + "pmfmIdTrawlNetNumber", IntegerType.INSTANCE, enumeration.PMFM_ID_TRAWL_NET_NUMBER, + "locationLevelIdStrata", IntegerType.INSTANCE, enumeration.LOCATION_LEVEL_ID_STRATA, + "locationLevelIdSubStrata", IntegerType.INSTANCE, enumeration.LOCATION_LEVEL_ID_SUB_STRATA, + "locationLevelIdLocalite", IntegerType.INSTANCE, enumeration.LOCATION_LEVEL_ID_LOCALITE ); if (source == null) { @@ -176,7 +189,7 @@ Integer gearId = (Integer)source[colIndex++]; if (gearId != null) { // TODO TC : activer un cache sur getGear() - fr.ifremer.tutti.persistence.entities.referential.Gear gear = referentielService.getGear(gearId); + fr.ifremer.tutti.persistence.entities.referential.Gear gear = referentialService.getGear(gearId); result.setGear(gear); } @@ -210,18 +223,50 @@ // Haul valid ? Integer haulValidQualitativeId = (Integer)source[colIndex++]; - if (haulValidQualitativeId != null) { - result.setFishingOperationValid((enumeration.QUALITATIVE_HAUL_VALID_YES.equals(haulValidQualitativeId))); - } + //if (haulValidQualitativeId != null) { + // result.setFishingOperationValid((enumeration.QUALITATIVE_HAUL_VALID_YES.equals(haulValidQualitativeId))); + //} // Trawl net number (override the value found in the OP name) Float trawlNetNumber = (Float)source[colIndex++]; if (trawlNetNumber != null) { result.setTrawlNetNumber(trawlNetNumber.intValue()); } + + // Strata : + Integer strataId = (Integer)source[colIndex++]; + if (strataId != null) { + FishingOperationLocation strata = new FishingOperationLocation(); + strata.setId(strataId.toString()); + result.setStrata(strata); + } + // Sub Strata : + Integer subStrataId = (Integer)source[colIndex++]; + if (subStrataId != null) { + FishingOperationLocation subStrata = new FishingOperationLocation(); + subStrata.setId(subStrataId.toString()); + result.setSubStrata(subStrata); + } + + // Localite : + Integer localiteId = (Integer)source[colIndex++]; + if (localiteId != null) { + FishingOperationLocation localite = new FishingOperationLocation(); + localite.setId(localiteId.toString()); + result.setLocation(localite); + } + + // Retrieve environment caracteristics : + getEnvironmentCaracteristics(id, result); + + // Retrieve gear shooting caracteristics : + getGearShootingCaracteristics(id, result); + return result; } + + @Override public FishingOperation createFishingOperation(FishingOperation bean) { @@ -231,21 +276,145 @@ if (bean.getCruise() == null || bean.getCruise().getId() == null) { throw new IllegalArgumentException("fishingOperation.getCruise() could not be null"); } + getCurrentSession().setFlushMode(FlushMode.COMMIT); fr.ifremer.adagio.core.dao.data.operation.FishingOperation fishingOperation = fr.ifremer.adagio.core.dao.data.operation.FishingOperation.Factory.newInstance(); beanToEntity(bean, fishingOperation, true); fishingOperationDao.create(fishingOperation); - bean.setId(String.valueOf(fishingOperation.getId())); + getCurrentSession().flush(); return bean; + } + + @Override + public FishingOperation saveFishingOperation(FishingOperation bean) { + if (bean == null) { + throw new IllegalArgumentException("fishingOperation could not be null"); + } + if (bean.getId() == null || bean.getId().isEmpty()) { + throw new IllegalArgumentException("fishingOperation.id could not be null"); + } + if (bean.getCruise() == null || bean.getCruise().getId() == null) { + throw new IllegalArgumentException("fishingOperation.getCruise() could not be null"); + } + getCurrentSession().clear(); + getCurrentSession().setFlushMode(FlushMode.COMMIT); + fr.ifremer.adagio.core.dao.data.operation.FishingOperation fishingOperation = fishingOperationDao.load(Integer.valueOf(bean.getId())); + beanToEntity(bean, fishingOperation, true); + fishingOperationDao.update(fishingOperation); + getCurrentSession().flush(); + return bean; } + //------------------------------------------------------------------------// + //-- Internal methods --// + //------------------------------------------------------------------------// + + protected void getEnvironmentCaracteristics(String fishingOperationId, FishingOperation result) { + // retrieve fishing operation caracteristics + Iterator<Object[]> list = queryList( + "fishingOperationEnvironment", + "fishingOperationId", IntegerType.INSTANCE, Integer.valueOf(fishingOperationId) + ); + + CaracteristicMap environmentCaracteristics = new CaracteristicMap(); + while (list.hasNext()) { + int colIndex=0; + Object[] source = list.next(); + Integer pmfmId = (Integer)source[colIndex++]; + Float numericalValue = (Float)source[colIndex++]; + String alphanumericalValue = (String)source[colIndex++]; + Integer qualitativeValueId = (Integer)source[colIndex++]; + + // Trawl distance + if (enumeration.PMFM_ID_TRAWL_DISTANCE.equals(pmfmId)) { + result.setTrawlDistance(numericalValue); + } + + // Rectilinear operation ? + else if (enumeration.PMFM_ID_RECTILINEAR_OPERATION.equals(pmfmId)) { + result.setFishingOperationRectiligne(enumeration.QUALITATIVE_RECTILINEAR_OPERATION_YES.equals(qualitativeValueId)); + } + + // Haul valid ? + else if (enumeration.PMFM_ID_HAUL_VALID.equals(pmfmId)) { + result.setFishingOperationValid(enumeration.QUALITATIVE_HAUL_VALID_YES.equals(qualitativeValueId)); + } + + // Station Number : + else if (enumeration.PMFM_ID_STATION_NUMBER.equals(pmfmId)) { + result.setStationNumber(alphanumericalValue); + } + + // Environment caracteristic + else { + Caracteristic environmentCaracteristic = referentialService.getCaracteristic(pmfmId); + Serializable value = null; + if (environmentCaracteristic.getCaracteristicType() == CaracteristicType.NUMBER) { + value = numericalValue; + } + else if (environmentCaracteristic.getCaracteristicType() == CaracteristicType.TEXT) { + value = alphanumericalValue; + } + else if (environmentCaracteristic.getCaracteristicType() == CaracteristicType.QUALITATIVE) { + value = qualitativeValueId; + } + environmentCaracteristics.put(environmentCaracteristic, value); + } + } + + if (environmentCaracteristics.size() > 0) { + result.setEnvironmentCaracteristics(environmentCaracteristics); + } + } + + protected void getGearShootingCaracteristics(String fishingOperationId, FishingOperation result) { + // retrieve fishing operation caracteristics + Iterator<Object[]> list = queryList( + "fishingOperationGearShooting", + "fishingOperationId", IntegerType.INSTANCE, Integer.valueOf(fishingOperationId) + ); + + CaracteristicMap gearShootingCaracteristics = new CaracteristicMap(); + while (list.hasNext()) { + int colIndex=0; + Object[] source = list.next(); + Integer pmfmId = (Integer)source[colIndex++]; + Float numericalValue = (Float)source[colIndex++]; + String alphanumericalValue = (String)source[colIndex++]; + Integer qualitativeValueId = (Integer)source[colIndex++]; + + // Trawl distance + if (enumeration.PMFM_ID_TRAWL_NET_NUMBER.equals(pmfmId)) { + result.setTrawlNetNumber(numericalValue.intValue()); + } + + // Gear Shooting Caracteristics + else { + Caracteristic gearShootingCaracteristic = referentialService.getCaracteristic(pmfmId); + Serializable value = null; + if (gearShootingCaracteristic.getCaracteristicType() == CaracteristicType.NUMBER) { + value = numericalValue; + } + else if (gearShootingCaracteristic.getCaracteristicType() == CaracteristicType.TEXT) { + value = alphanumericalValue; + } + else if (gearShootingCaracteristic.getCaracteristicType() == CaracteristicType.QUALITATIVE) { + value = qualitativeValueId; + } + gearShootingCaracteristics.put(gearShootingCaracteristic, value); + } + } + + if (gearShootingCaracteristics.size() > 0) { + result.setGearShootingCaracteristics(gearShootingCaracteristics); + } + } + protected void beanToEntity(FishingOperation source, fr.ifremer.adagio.core.dao.data.operation.FishingOperation target, boolean copyIfNull) { - // Retrieve entities : FishingTrip and ScientificCruise ScientificCruise scientificCruise = null; FishingTrip fishingTrip = target.getFishingTrip(); if (fishingTrip == null) { - // TODO BLA éviter ici un rechargement de fishingTrip : scientificCruise = scientificCruiseDao.load(Integer.valueOf(source.getCruise().getId())); fishingTrip = scientificCruise.getFishingTrips().iterator().next(); fishingTrip.getOperations().add(target); // Inverse link @@ -262,10 +431,10 @@ if (target.getVesselPositions() != null) { for (Iterator iterator = target.getVesselPositions().iterator(); iterator.hasNext();) { VesselPosition position = (VesselPosition) iterator.next(); - if (position.getDateTime() != null && position.getDateTime().equals(target.getStartDateTime())) { + if (position.getDateTime() != null && position.getDateTime().getTime() == target.getStartDateTime().getTime()) { startPosition = position; } - else if (position.getDateTime() == null || position.getDateTime().equals(target.getEndDateTime())) { + else if (position.getDateTime() == null || (target.getEndDateTime() != null && position.getDateTime().getTime() == target.getEndDateTime().getTime())) { endPosition = position; } } @@ -277,20 +446,26 @@ gearUseFeatures = GearUseFeatures.Factory.newInstance(); gearUseFeatures.setOperation(target); if (target.getGearUseFeatures() == null) { - target.setGearUseFeatures(Lists.newArrayList(gearUseFeatures)); + target.setGearUseFeatures(Sets.newHashSet(gearUseFeatures)); } else { target.getGearUseFeatures().add(gearUseFeatures); } } else { gearUseFeatures = target.getGearUseFeatures().iterator().next(); } + + // Create a list to store all updates, then remove not updated items + Set<GearUseMeasurement> notChangedGearUseMeasurements = new HashSet<GearUseMeasurement>(); + if (gearUseFeatures.getGearUseMeasurements() != null) { + notChangedGearUseMeasurements.addAll(gearUseFeatures.getGearUseMeasurements()); + } // Retrieve entities : Vessel Use Features VesselUseFeatures vesselUseFeatures = null; if (target.getVesselUseFeatures() == null || target.getVesselUseFeatures().size() == 0) { vesselUseFeatures = VesselUseFeatures.Factory.newInstance(); if (target.getVesselUseFeatures() == null) { - target.setVesselUseFeatures(Lists.newArrayList(vesselUseFeatures)); + target.setVesselUseFeatures(Sets.newHashSet(vesselUseFeatures)); vesselUseFeatures.setOperation(target); } else { target.getVesselUseFeatures().add(vesselUseFeatures); @@ -300,6 +475,12 @@ vesselUseFeatures = target.getVesselUseFeatures().iterator().next(); } + // Create a list to store all updates, then remove not updated items + Set<VesselUseMeasurement> notChangedVesselUseMeasurements = new HashSet<VesselUseMeasurement>(); + if (vesselUseFeatures.getVesselUseMeasurements() != null) { + notChangedVesselUseMeasurements.addAll(vesselUseFeatures.getVesselUseMeasurements()); + } + // Retrieve entities : Gear Physical Features GearPhysicalFeatures gearPhysicalFeatures = target.getGearPhysicalFeatures(); if (gearPhysicalFeatures == null && source.getGear() != null && source.getGear().getId() != null) { @@ -309,26 +490,44 @@ } target.setGearPhysicalFeatures(gearPhysicalFeatures); if (gearPhysicalFeatures.getOperations() == null) { - gearPhysicalFeatures.setOperations(Lists.newArrayList((Operation)target)); + gearPhysicalFeatures.setOperations(Sets.newHashSet((Operation)target)); } else { gearPhysicalFeatures.getOperations().add(target); } } - // Retrieve tarwl net, from Gear physical features + // Retrieve entities : Fishing Area + FishingArea fishingArea = null; + if (gearUseFeatures.getFishingAreas() == null || gearUseFeatures.getFishingAreas().size() == 0) { + fishingArea = FishingArea.Factory.newInstance(); + if (gearUseFeatures.getFishingAreas() == null) { + gearUseFeatures.setFishingAreas(Sets.newHashSet(fishingArea)); + fishingArea.setGearUseFeatures(gearUseFeatures); + } else { + gearUseFeatures.getFishingAreas().add(fishingArea); + fishingArea.setGearUseFeatures(gearUseFeatures); + } + } else { + fishingArea = gearUseFeatures.getFishingAreas().iterator().next(); + // Reset all other fishing areas + } + + // Retrieve trawl net, from Gear physical features int trawlNet = 1; // default value GearPhysicalMeasurement gpmTrawlNet = measurementService.getGearPhysicalMeasurement(gearPhysicalFeatures, enumeration.PMFM_ID_TRAWL_NET); if (gpmTrawlNet != null && gpmTrawlNet.getNumericalValue() != null) { trawlNet = gpmTrawlNet.getNumericalValue().intValue(); - } - + } + StringBuffer nameBuffer = new StringBuffer(); // StationNumber if (copyIfNull && source.getStationNumber() == null) { - setVesselUseMeasurement(scientificCruise, vesselUseFeatures, enumeration.PMFM_ID_STATION_NUMBER, null, "", null); + //setVesselUseMeasurement(scientificCruise, vesselUseFeatures, enumeration.PMFM_ID_STATION_NUMBER, null, "", null); } else if (source.getStationNumber() != null) { - setVesselUseMeasurement(scientificCruise, vesselUseFeatures, enumeration.PMFM_ID_STATION_NUMBER, null, source.getStationNumber(), null); + VesselUseMeasurement vum = setVesselUseMeasurement(scientificCruise, vesselUseFeatures, enumeration.PMFM_ID_STATION_NUMBER, null, source.getStationNumber(), null); + notChangedVesselUseMeasurements.remove(vum); + nameBuffer.append(source.getStationNumber()); } @@ -342,14 +541,12 @@ if (nameBuffer.length() > 0) { nameBuffer.append(FISHING_OPERATION_NAME_SEPARATOR); } - // TODO BL : store OP N° ? or compute it ? nameBuffer.append(source.getFishingOperationNumber()); } // Trawl net number if (copyIfNull && source.getTrawlNetNumber() == null) { - // Remove from Gear Use Features - removeGearUseMeasurement(gearUseFeatures, enumeration.PMFM_ID_TRAWL_NET_NUMBER); + // Nothing to do : will be removed later, using notChangedGearUseMeasurements if (nameBuffer.length() > 0) { nameBuffer.append(FISHING_OPERATION_NAME_SEPARATOR); } @@ -358,7 +555,9 @@ throw new DataIntegrityViolationException("An operation could not have a 'trawl net number' greater than 'trawl net' defined in the cruise."); } // Store into Gear Use Features - setGearUseMeasurement(scientificCruise, gearUseFeatures, enumeration.PMFM_ID_TRAWL_NET_NUMBER, Float.valueOf(source.getTrawlNetNumber()), null, null); + GearUseMeasurement gum = setGearUseMeasurement(scientificCruise, gearUseFeatures, enumeration.PMFM_ID_TRAWL_NET_NUMBER, Float.valueOf(source.getTrawlNetNumber()), null, null); + notChangedGearUseMeasurements.remove(gum); + // Store into the name attribute : if (nameBuffer.length() > 0) { nameBuffer.append(FISHING_OPERATION_NAME_SEPARATOR); @@ -394,19 +593,24 @@ // Trawl distance if (copyIfNull && source.getTrawlDistance() == null) { - removeVesselUseMeasurement(vesselUseFeatures, enumeration.PMFM_ID_TRAWL_DISTANCE); + // Nothing to do : will be removed later, using notChangedVesselUseMeasurements } else if (source.getTrawlDistance() != null) { - setVesselUseMeasurement(scientificCruise, vesselUseFeatures, enumeration.PMFM_ID_TRAWL_DISTANCE, source.getTrawlDistance(), null, null); + VesselUseMeasurement vum = setVesselUseMeasurement(scientificCruise, vesselUseFeatures, enumeration.PMFM_ID_TRAWL_DISTANCE, source.getTrawlDistance(), null, null); + notChangedVesselUseMeasurements.remove(vum); } // Rectilinear operation - setVesselUseMeasurement(scientificCruise, vesselUseFeatures, enumeration.PMFM_ID_RECTILINEAR_OPERATION, null, null, source.isFishingOperationRectiligne()?enumeration.QUALITATIVE_RECTILINEAR_OPERATION_YES:enumeration.QUALITATIVE_RECTILINEAR_OPERATION_NO); - + { + VesselUseMeasurement vum = setVesselUseMeasurement(scientificCruise, vesselUseFeatures, enumeration.PMFM_ID_RECTILINEAR_OPERATION, null, null, source.isFishingOperationRectiligne()?enumeration.QUALITATIVE_RECTILINEAR_OPERATION_YES:enumeration.QUALITATIVE_RECTILINEAR_OPERATION_NO); + notChangedVesselUseMeasurements.remove(vum); + } + // Operation is valid ? if (copyIfNull && source.getFishingOperationValid() == null) { removeVesselUseMeasurement(vesselUseFeatures, enumeration.PMFM_ID_HAUL_VALID); } else if (source.getFishingOperationValid() != null) { - setVesselUseMeasurement(scientificCruise, vesselUseFeatures, enumeration.PMFM_ID_HAUL_VALID, null, null, source.getFishingOperationValid().booleanValue()?enumeration.QUALITATIVE_HAUL_VALID_YES:enumeration.QUALITATIVE_HAUL_VALID_NO); + VesselUseMeasurement vum = setVesselUseMeasurement(scientificCruise, vesselUseFeatures, enumeration.PMFM_ID_HAUL_VALID, null, null, source.getFishingOperationValid().booleanValue()?enumeration.QUALITATIVE_HAUL_VALID_YES:enumeration.QUALITATIVE_HAUL_VALID_NO); + notChangedVesselUseMeasurements.remove(vum); } // Comment @@ -421,7 +625,7 @@ // Quality Flag : if (target.getQualityFlag() == null) { - target.setQualityFlag(qualityFlagDao.load(enumeration.QUALITY_FLAG_CODE_NOT_QUALIFIED)); + target.setQualityFlag(load(QualityFlagImpl.class, enumeration.QUALITY_FLAG_CODE_NOT_QUALIFIED)); } // Settings not null properties : @@ -452,7 +656,7 @@ vesselUseFeatures.setCreationDate(calendar.getTime()); } if (vesselUseFeatures.getQualityFlag() == null) { - vesselUseFeatures.setQualityFlag(qualityFlagDao.load(enumeration.QUALITY_FLAG_CODE_NOT_QUALIFIED)); + vesselUseFeatures.setQualityFlag(load(QualityFlagImpl.class, enumeration.QUALITY_FLAG_CODE_NOT_QUALIFIED)); } // GearUseFeatures : @@ -472,15 +676,14 @@ gearUseFeatures.setCreationDate(calendar.getTime()); } if (gearUseFeatures.getQualityFlag() == null) { - gearUseFeatures.setQualityFlag(qualityFlagDao.load(enumeration.QUALITY_FLAG_CODE_NOT_QUALIFIED)); + gearUseFeatures.setQualityFlag(load(QualityFlagImpl.class, enumeration.QUALITY_FLAG_CODE_NOT_QUALIFIED)); } // GearUseFeatures.Gear if (copyIfNull && source.getGear() == null) { gearUseFeatures.setGear(null); } else if (source.getGear() != null && source.getGear().getId() != null) { - Gear gear = gearDao.load(Integer.valueOf(source.getGear().getId())); - gearUseFeatures.setGear(gear); + gearUseFeatures.setGear(load(GearImpl.class, Integer.valueOf(source.getGear().getId()))); } // Start position : @@ -493,7 +696,7 @@ startPosition = VesselPosition.Factory.newInstance(); startPosition.setOperation(target); if (target.getVesselPositions() == null) { - target.setVesselPositions(Lists.newArrayList(startPosition)); + target.setVesselPositions(Sets.newHashSet(startPosition)); } target.getVesselPositions().add(startPosition); } @@ -542,12 +745,130 @@ target.getVesselPositions().remove(endPosition); } + // Environment Caracteristics + CaracteristicMap environmentCaracteristics = source.getEnvironmentCaracteristics(); + if (copyIfNull && environmentCaracteristics == null || environmentCaracteristics.size() == 0) { + // Nothing to do : will be removed later, using notChangedVesselUseMeasurements + } + else if (environmentCaracteristics != null && environmentCaracteristics.size() > 0) { + for (Caracteristic caracteristic : environmentCaracteristics.keySet()) { + VesselUseMeasurement vum = setVesselUseMeasurement(scientificCruise, vesselUseFeatures, caracteristic, environmentCaracteristics.get(caracteristic)); + notChangedVesselUseMeasurements.remove(vum); + } + } + + // Removed unecessary Vessel Use Measurement + + // Gear shooting Caracteristics + CaracteristicMap gearShootingCaracteristics = source.getGearShootingCaracteristics(); + if (copyIfNull && gearShootingCaracteristics == null || gearShootingCaracteristics.size() == 0) { + // Nothing to do : will be removed later, using notChangedGearUseMeasurements + } + else if (gearShootingCaracteristics != null && gearShootingCaracteristics.size() > 0) { + for (Caracteristic caracteristic : gearShootingCaracteristics.keySet()) { + GearUseMeasurement gum = setGearUseMeasurement(scientificCruise, gearUseFeatures, caracteristic, environmentCaracteristics.get(caracteristic)); + notChangedGearUseMeasurements.remove(gum); + } + } + + // Removed not changed measurements (in Vessel & Gear Use Measurement lists) + if (vesselUseFeatures.getVesselUseMeasurements() != null && notChangedVesselUseMeasurements.size() > 0) { + for (VesselUseMeasurement vum : notChangedVesselUseMeasurements) { + vesselUseFeatures.getVesselUseMeasurements().remove(vum); + } + } + if (gearUseFeatures.getGearUseMeasurements() != null && notChangedGearUseMeasurements.size() > 0) { + for (GearUseMeasurement gum : notChangedGearUseMeasurements) { + gearUseFeatures.getGearUseMeasurements().remove(gum); + } + } + + // Fishing Area : + Integer statisticalLocationId = null; + if (source.getGearShootingStartLatitude() != null && source.getGearShootingStartLongitude() != null) { + statisticalLocationId = locationService.getLocationIdByLatLong(source.getGearShootingStartLatitude(), source.getGearShootingStartLongitude()); + } + if (statisticalLocationId == null && source.getGearShootingEndLatitude() != null && source.getGearShootingEndLongitude() != null) { + statisticalLocationId = locationService.getLocationIdByLatLong(source.getGearShootingEndLatitude(), source.getGearShootingEndLongitude()); + } + + // Strata : + if (copyIfNull && (source.getStrata() == null || source.getStrata().getId() == null)) { + // TODO remove from fishing Area ? + } + else if (source.getStrata() != null && source.getStrata().getId() != null) { + FishingArea2RegulationLocation fa2rl = FishingArea2RegulationLocation.Factory.newInstance(); + FishingArea2RegulationLocationPK fa2rlPK = new FishingArea2RegulationLocationPK(); + fa2rl.setFishingArea2RegulationLocationPk(fa2rlPK); + fa2rlPK.setFishingArea(fishingArea); + fa2rlPK.setLocation(load(LocationImpl.class, Integer.valueOf(source.getStrata().getId()))); + if (fishingArea.getRegulationLocation() == null) { + fishingArea.setRegulationLocation(Sets.newHashSet(fa2rl)); + } + else { + fishingArea.getRegulationLocation().add(fa2rl); + } + if (statisticalLocationId == null) { + statisticalLocationId = Integer.valueOf(source.getStrata().getId()); + } + } + + // Sub-Strata : + if (copyIfNull && (source.getSubStrata() == null || source.getSubStrata().getId() == null)) { + // TODO remove from fishing Area ? + } + else if (source.getSubStrata() != null && source.getSubStrata().getId() != null) { + FishingArea2RegulationLocation fa2rl = FishingArea2RegulationLocation.Factory.newInstance(); + FishingArea2RegulationLocationPK fa2rlPK = new FishingArea2RegulationLocationPK(); + fa2rl.setFishingArea2RegulationLocationPk(fa2rlPK); + fa2rlPK.setFishingArea(fishingArea); + fa2rlPK.setLocation(load(LocationImpl.class, Integer.valueOf(source.getSubStrata().getId()))); + if (fishingArea.getRegulationLocation() == null) { + fishingArea.setRegulationLocation(Sets.newHashSet(fa2rl)); + } + else { + fishingArea.getRegulationLocation().add(fa2rl); + } + if (statisticalLocationId == null) { + statisticalLocationId = Integer.valueOf(source.getSubStrata().getId()); + } + } + + // Localite : + if (copyIfNull && (source.getLocation() == null || source.getLocation().getId() == null)) { + // TODO remove from fishing Area ? + } + else if (source.getLocation() != null && source.getLocation().getId() != null) { + FishingArea2RegulationLocation fa2rl = FishingArea2RegulationLocation.Factory.newInstance(); + FishingArea2RegulationLocationPK fa2rlPK = new FishingArea2RegulationLocationPK(); + fa2rl.setFishingArea2RegulationLocationPk(fa2rlPK); + fa2rlPK.setFishingArea(fishingArea); + fa2rlPK.setLocation(load(LocationImpl.class, Integer.valueOf(source.getLocation().getId()))); + if (fishingArea.getRegulationLocation() == null) { + fishingArea.setRegulationLocation(Sets.newHashSet(fa2rl)); + } + else { + fishingArea.getRegulationLocation().add(fa2rl); + } + if (statisticalLocationId == null) { + statisticalLocationId = Integer.valueOf(source.getLocation().getId()); + } + } + + // Fishing Area location (should be a statistical location) + if (copyIfNull && statisticalLocationId == null) { + gearUseFeatures.getFishingAreas().remove(fishingArea); + //Nothing to do : a gearUseFeatures.getFishingAreas().clear() has been done before + if (fishingArea.getRegulationLocation() != null) { + fishingArea.getRegulationLocation().clear(); + } + } + else if (statisticalLocationId != null) { + fishingArea.setLocation(load(LocationImpl.class, statisticalLocationId)); + //gearUseFeatures.getFishingAreas().add(fishingArea); + } } - @Override - public FishingOperation saveFishingOperation(FishingOperation bean) { - return null; - } /** * Test if the date has millisecond set. This yes, return null, then return the date itself. @@ -646,14 +967,14 @@ vesselUseMeasurement = VesselUseMeasurement.Factory.newInstance(); vesselUseMeasurement.setVesselUseFeatures(vesselUseFeatures); if (vesselUseFeatures.getVesselUseMeasurements() == null) { - vesselUseFeatures.setVesselUseMeasurements(Lists.newArrayList(vesselUseMeasurement)); + vesselUseFeatures.setVesselUseMeasurements(Sets.newHashSet(vesselUseMeasurement)); } else { vesselUseFeatures.getVesselUseMeasurements().add(vesselUseMeasurement); } - vesselUseMeasurement.setQualityFlag(qualityFlagDao.load(enumeration.QUALITY_FLAG_CODE_NOT_QUALIFIED)); + vesselUseMeasurement.setQualityFlag(load(QualityFlagImpl.class, enumeration.QUALITY_FLAG_CODE_NOT_QUALIFIED)); vesselUseMeasurement.setDepartment(scientificCruise.getRecorderDepartment()); - vesselUseMeasurement.setPmfm(pmfmDao.load(pmfmId)); + vesselUseMeasurement.setPmfm(load(PmfmImpl.class, pmfmId)); } return vesselUseMeasurement; @@ -683,12 +1004,50 @@ vesselUseMeasurement.setNumericalValue(numericalValue); } else if (qualitativevalueId != null) { - vesselUseMeasurement.setQualitativeValue(qualitativeValueDao.load(qualitativevalueId)); + vesselUseMeasurement.setQualitativeValue(load(QualitativeValueImpl.class, qualitativevalueId)); } return vesselUseMeasurement; } + protected VesselUseMeasurement setVesselUseMeasurement(ScientificCruise scientificCruise, VesselUseFeatures vesselUseFeatures, + Integer pmfmId, + Serializable value) { + VesselUseMeasurement vesselUseMeasurement = getVesselUseMeasurement(scientificCruise, vesselUseFeatures, pmfmId, true); + + if (value instanceof String) { + vesselUseMeasurement.setAlphanumericalValue((String)value); + } + else if (value instanceof Float) { + vesselUseMeasurement.setNumericalValue((Float)value); + } + else if (value instanceof Integer) { + vesselUseMeasurement.setQualitativeValue(load(QualitativeValueImpl.class, (Integer)value)); + } + + return vesselUseMeasurement; + } + + protected VesselUseMeasurement setVesselUseMeasurement(ScientificCruise scientificCruise, VesselUseFeatures vesselUseFeatures, + Caracteristic caracteristic, Serializable value) { + VesselUseMeasurement vesselUseMeasurement = getVesselUseMeasurement(scientificCruise, vesselUseFeatures, Integer.valueOf(caracteristic.getId()), true); + setMeasurement(vesselUseMeasurement, caracteristic, value); + return vesselUseMeasurement; + } + + protected void setMeasurement(Measurement measurement, Caracteristic caracteristic, Serializable value) { + if (caracteristic.getCaracteristicType() == CaracteristicType.TEXT) { + measurement.setAlphanumericalValue((String)value); + } + else if (caracteristic.getCaracteristicType() == CaracteristicType.NUMBER) { + measurement.setNumericalValue((Float)value); + } + else if (caracteristic.getCaracteristicType() == CaracteristicType.QUALITATIVE) { + QualitativeValue qv = (QualitativeValue)getCurrentSession().load(QualitativeValueImpl.class, Integer.valueOf((String)value)); + measurement.setQualitativeValue(qv); + } + } + protected GearUseMeasurement setGearUseMeasurement(ScientificCruise scientificCruise, GearUseFeatures gearUseFeatures, Integer pmfmId, Float numericalValue, @@ -703,12 +1062,20 @@ gearUseMeasurement.setNumericalValue(numericalValue); } else if (qualitativevalueId != null) { - gearUseMeasurement.setQualitativeValue(qualitativeValueDao.load(qualitativevalueId)); + QualitativeValue qv = (QualitativeValue)getCurrentSession().load(QualitativeValueImpl.class, qualitativevalueId); + gearUseMeasurement.setQualitativeValue(qv); } return gearUseMeasurement; } + protected GearUseMeasurement setGearUseMeasurement(ScientificCruise scientificCruise, GearUseFeatures gearUseFeatures, + Caracteristic caracteristic, Serializable value) { + GearUseMeasurement gearUseMeasurement = getGearUseMeasurement(scientificCruise, gearUseFeatures, Integer.valueOf(caracteristic.getId()), true); + setMeasurement(gearUseMeasurement, caracteristic, value); + return gearUseMeasurement; + } + protected GearUseMeasurement getGearUseMeasurement(ScientificCruise scientificCruise, GearUseFeatures gearUseFeatures, Integer pmfmId, boolean createIfNotExists) { GearUseMeasurement gearUseMeasurement = null; @@ -728,14 +1095,15 @@ gearUseMeasurement = GearUseMeasurement.Factory.newInstance(); gearUseMeasurement.setGearUseFeatures(gearUseFeatures); if (gearUseFeatures.getGearUseMeasurements() == null) { - gearUseFeatures.setGearUseMeasurements(Lists.newArrayList(gearUseMeasurement)); + gearUseFeatures.setGearUseMeasurements(Sets.newHashSet(gearUseMeasurement)); } else { gearUseFeatures.getGearUseMeasurements().add(gearUseMeasurement); } - gearUseMeasurement.setQualityFlag(qualityFlagDao.load(enumeration.QUALITY_FLAG_CODE_NOT_QUALIFIED)); + gearUseMeasurement.setQualityFlag(load(QualityFlagImpl.class, enumeration.QUALITY_FLAG_CODE_NOT_QUALIFIED)); gearUseMeasurement.setDepartment(scientificCruise.getRecorderDepartment()); - gearUseMeasurement.setPmfm(pmfmDao.load(pmfmId)); + Pmfm pmfm = (Pmfm)getCurrentSession().load(PmfmImpl.class, pmfmId); + gearUseMeasurement.setPmfm(pmfm); } return gearUseMeasurement; @@ -750,5 +1118,4 @@ gearUseFeatures.getGearUseMeasurements().remove(gearUseMeasurement); // TOBO BLa : vérifier qu'il ne faut pas dao.delete() en plus } - } Modified: trunk/tutti-persistence-adagio/src/main/java/fr/ifremer/tutti/persistence/service/ReferentialPersistenceServiceImpl.java =================================================================== --- trunk/tutti-persistence-adagio/src/main/java/fr/ifremer/tutti/persistence/service/ReferentialPersistenceServiceImpl.java 2013-01-21 15:51:02 UTC (rev 239) +++ trunk/tutti-persistence-adagio/src/main/java/fr/ifremer/tutti/persistence/service/ReferentialPersistenceServiceImpl.java 2013-01-23 10:00:00 UTC (rev 240) @@ -385,59 +385,59 @@ @Override public Caracteristic getSizeCategoryCaracteristic() { Integer pmfmId = enumeration.PMFM_ID_SIZE_CATEGORY; - Caracteristic result = loadCaracteristic(pmfmId); + Caracteristic result = getCaracteristic(pmfmId); return result; } @Override public Caracteristic getSexCaracteristic() { Integer pmfmId = enumeration.PMFM_ID_SEX; - Caracteristic result = loadCaracteristic(pmfmId); + Caracteristic result = getCaracteristic(pmfmId); return result; } @Override public Caracteristic getSortedUnsortedCaracteristic() { Integer pmfmId = enumeration.PMFM_ID_SORTED_UNSORTED; - Caracteristic result = loadCaracteristic(pmfmId); + Caracteristic result = getCaracteristic(pmfmId); return result; } @Override public Caracteristic getMaturityCaracteristic() { Integer pmfmId = enumeration.PMFM_ID_MATURITY; - Caracteristic result = loadCaracteristic(pmfmId); + Caracteristic result = getCaracteristic(pmfmId); return result; } @Override public Caracteristic getMacroWasteCategoryCaracteristic() { Integer pmfmId = enumeration.PMFM_ID_MACRO_WASTE_CATEGORY; - Caracteristic result = loadCaracteristic(pmfmId); + Caracteristic result = getCaracteristic(pmfmId); return result; } @Override public Caracteristic getMacroWasteSizeCategoryCaracteristic() { Integer pmfmId = enumeration.PMFM_ID_MACRO_WASTE_SIZE_CATEGORY; - Caracteristic result = loadCaracteristic(pmfmId); + Caracteristic result = getCaracteristic(pmfmId); return result; } - //------------------------------------------------------------------------// - //-- Internal methods --// - //------------------------------------------------------------------------// - - protected Caracteristic loadCaracteristic(Integer pmfmId) { - + @Override + public Caracteristic getCaracteristic(Integer pmfmId) { Object[] source = queryUniqueWithStatus("pmfmById", "pmfmId", IntegerType.INSTANCE, pmfmId, "unitIdNone", IntegerType.INSTANCE, enumeration.UNIT_ID_NONE); - Caracteristic target = loadCaracteristic(source); return target; } + //------------------------------------------------------------------------// + //-- Internal methods --// + //------------------------------------------------------------------------// + + protected Caracteristic loadCaracteristic(Object[] source) { Integer pmfmId = (Integer) source[0]; Modified: trunk/tutti-persistence-adagio/src/main/java/fr/ifremer/tutti/persistence/service/TuttiEnumerationFile.java =================================================================== --- trunk/tutti-persistence-adagio/src/main/java/fr/ifremer/tutti/persistence/service/TuttiEnumerationFile.java 2013-01-21 15:51:02 UTC (rev 239) +++ trunk/tutti-persistence-adagio/src/main/java/fr/ifremer/tutti/persistence/service/TuttiEnumerationFile.java 2013-01-23 10:00:00 UTC (rev 240) @@ -118,6 +118,12 @@ @Value("${LocationLevelId.RECTANGLE_STATISTIQUE_MED}") public final Integer RECTANGLE_STATISTIQUE_MED = null; + + @Value("${LocationLevelId.RECTANGLE_STATISTIQUE}") + public final Integer RECTANGLE_STATISTIQUE = null; + + @Value("${LocationLevelId.FAO_ZONE}") + public final Integer FAO_ZONE = null; @Value("${QualitativeValueId.VRAC}") public final Integer QUALITATIVE_VRAC_ID = null; Modified: trunk/tutti-persistence-adagio/src/main/resources/queries-override.hbm.xml =================================================================== --- trunk/tutti-persistence-adagio/src/main/resources/queries-override.hbm.xml 2013-01-21 15:51:02 UTC (rev 239) +++ trunk/tutti-persistence-adagio/src/main/resources/queries-override.hbm.xml 2013-01-23 10:00:00 UTC (rev 240) @@ -130,11 +130,16 @@ <![CDATA[ SELECT gpf.gear.id AS gearId, + MAX(CASE gpm.pmfm.id + WHEN :pmfmIdTrawlNet THEN gpm.numericalValue + ELSE 0 + END) as trawlNet, count(o.id) as operationCount FROM ScientificCruiseImpl sc JOIN sc.fishingTrips ft JOIN ft.gearPhysicalFeatures gpf + LEFT OUTER JOIN gpf.gearPhysicalMeasurements gpm LEFT OUTER JOIN gpf.operations o WHERE sc.id = :cruiseId @@ -143,6 +148,7 @@ ORDER BY count(o.id) DESC ]]> <query-param name="cruiseId" type="java.lang.Integer"/> + <query-param name="pmfmIdTrawlNet" type="java.lang.Integer"/> </query> <query cacheable="true" name="allCruiseSecondaryVessels"> @@ -166,30 +172,68 @@ <![CDATA[ SELECT o.name as name, - o.startDateTime as startDateTime, - o.endDateTime as endDateTime, - o.comments as comments, - guf.gear.id as gearId, + max(o.startDateTime) as startDateTime, + max(o.endDateTime) as endDateTime, + max(o.comments) as comments, + max(guf.gear.id) as gearId, (select vp_start from VesselPositionImpl vp_start where vp_start.operation.id = o.id and vp_start.dateTime = o.startDateTime) as startVesselPosition, (select vp_end from VesselPositionImpl vp_end where vp_end.operation.id = o.id and vp_end.dateTime = o.endDateTime) as endVesselPosition, (select vum.numericalValue from VesselUseMeasurementImpl vum where vum.vesselUseFeatures.id=vuf.id and vum.pmfm.id=:pmfmIdTrawlDistance) as trawlDistance, (select vum2.qualitativeValue.id from VesselUseMeasurementImpl vum2 where vum2.vesselUseFeatures.id=vuf.id and vum2.pmfm.id=:pmfmIdRectilinearOperation) as rectilinearQualitativeId, (select vum3.qualitativeValue.id from VesselUseMeasurementImpl vum3 where vum3.vesselUseFeatures.id=vuf.id and vum3.pmfm.id=:pmfmIdHaulValid) as haulValidQualitativeId, - (select gum.numericalValue from GearUseMeasurementImpl gum where gum.gearUseFeatures.id=guf.id and gum.pmfm.id=:pmfmTrawlNetNumber) as trawlNetNumber + (select gum.numericalValue from GearUseMeasurementImpl gum where gum.gearUseFeatures.id=guf.id and gum.pmfm.id=:pmfmIdTrawlNetNumber) as trawlNetNumber, + max(case when (rl.locationLevel.id = :locationLevelIdStrata) then rl.id else null end) AS strataId, + max(case when (rl.locationLevel.id = :locationLevelIdSubStrata) then rl.id else null end) AS subStrataId, + max(case when (rl.locationLevel.id = :locationLevelIdLocalite) then rl.id else null end) AS localiteId FROM FishingOperationImpl o INNER JOIN o.gearUseFeatures guf INNER JOIN o.vesselUseFeatures vuf + LEFT OUTER JOIN guf.fishingAreas fa + LEFT OUTER JOIN fa.regulationLocation fa2rl + LEFT OUTER JOIN fa2rl.id.location rl WHERE o.id=:fishingOperationId + GROUP BY o.name ]]> <query-param name="fishingOperationId" type="java.lang.Integer"/> <query-param name="pmfmIdTrawlDistance" type="java.lang.Integer"/> <query-param name="pmfmIdRectilinearOperation" type="java.lang.Integer"/> <query-param name="pmfmIdHaulValid" type="java.lang.Integer"/> - <query-param name="pmfmTrawlNetNumber" type="java.lang.Integer"/> + <query-param name="pmfmIdTrawlNetNumber" type="java.lang.Integer"/> </query> + <query cacheable="true" name="fishingOperationEnvironment"> + <![CDATA[ + SELECT + vum.pmfm.id as pmfmId, + vum.numericalValue as numericalValue, + vum.alphanumericalValue as alphanumericalValue, + vum.qualitativeValue.id as qualitativeValueId + from + VesselUseFeaturesImpl vuf + join vuf.vesselUseMeasurements vum + WHERE + vuf.operation = :fishingOperationId + ]]> + <query-param name="fishingOperationId" type="java.lang.Integer"/> + </query> + + <query cacheable="true" name="fishingOperationGearShooting"> + <![CDATA[ + SELECT + gum.pmfm.id as pmfmId, + gum.numericalValue as numericalValue, + gum.alphanumericalValue as alphanumericalValue, + gum.qualitativeValue.id as qualitativeValueId + from + GearUseFeaturesImpl guf + join guf.gearUseMeasurements gum + WHERE + guf.operation = :fishingOperationId + ]]> + <query-param name="fishingOperationId" type="java.lang.Integer"/> + </query> <!-- ===================================================================== --> <!-- === Requete techniques sur référentiels [REF-TXXX] === --> Modified: trunk/tutti-persistence-adagio/src/main/resources/tutti-db-enumerations.properties =================================================================== --- trunk/tutti-persistence-adagio/src/main/resources/tutti-db-enumerations.properties 2013-01-21 15:51:02 UTC (rev 239) +++ trunk/tutti-persistence-adagio/src/main/resources/tutti-db-enumerations.properties 2013-01-23 10:00:00 UTC (rev 240) @@ -304,9 +304,9 @@ LocationLevelId.LOCALITE=22 LocationLevelId.STRATA=23 #TODO Creer la bonne valeur -LocationLevelId.SUBSTRATA=-23 +LocationLevelId.SUBSTRATA=113 #TODO Mettre la bonne valeur -LocationLevelId.PROGRAM=21 +LocationLevelId.PROGRAM=101 MethodId.DECLARATION=1 Modified: trunk/tutti-persistence-adagio/src/test/java/fr/ifremer/tutti/persistence/DatabaseFixtures.java =================================================================== --- trunk/tutti-persistence-adagio/src/test/java/fr/ifremer/tutti/persistence/DatabaseFixtures.java 2013-01-21 15:51:02 UTC (rev 239) +++ trunk/tutti-persistence-adagio/src/test/java/fr/ifremer/tutti/persistence/DatabaseFixtures.java 2013-01-23 10:00:00 UTC (rev 240) @@ -43,4 +43,17 @@ public String fishingVesselCode() { return "851751"; } + + public String strataId() { + return "20990"; /*= Strate 24E4*/ + } + + public String subStrataId() { + // TODO TC : change ID into a real substrata ! + return "6080"; /*= Rectangle 24E4 */ + } + + public String localite() { + return "21525"; /*= Localité 4F1 */ + } } Modified: trunk/tutti-persistence-adagio/src/test/java/fr/ifremer/tutti/persistence/service/CruisePersistenceServiceTest.java =================================================================== --- trunk/tutti-persistence-adagio/src/test/java/fr/ifremer/tutti/persistence/service/CruisePersistenceServiceTest.java 2013-01-21 15:51:02 UTC (rev 239) +++ trunk/tutti-persistence-adagio/src/test/java/fr/ifremer/tutti/persistence/service/CruisePersistenceServiceTest.java 2013-01-23 10:00:00 UTC (rev 240) @@ -109,7 +109,7 @@ calendar.add(Calendar.MONTH, 1); // add one month cruise.setEndDate(calendar.getTime()); - + Country country = null; List<Country> countries = referentialService.getAllCountry(); for (Country aCountry : countries) { Modified: trunk/tutti-persistence-adagio/src/test/java/fr/ifremer/tutti/persistence/service/FishingOperationPersistenceServiceTest.java =================================================================== --- trunk/tutti-persistence-adagio/src/test/java/fr/ifremer/tutti/persistence/service/FishingOperationPersistenceServiceTest.java 2013-01-21 15:51:02 UTC (rev 239) +++ trunk/tutti-persistence-adagio/src/test/java/fr/ifremer/tutti/persistence/service/FishingOperationPersistenceServiceTest.java 2013-01-23 10:00:00 UTC (rev 240) @@ -24,15 +24,22 @@ * #L% */ +import java.io.Serializable; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; +import java.util.HashMap; import java.util.Iterator; import java.util.List; +import java.util.Map; import fr.ifremer.tutti.persistence.DatabaseResource; +import fr.ifremer.tutti.persistence.entities.CaracteristicMap; import fr.ifremer.tutti.persistence.entities.data.Cruise; import fr.ifremer.tutti.persistence.entities.data.FishingOperation; +import fr.ifremer.tutti.persistence.entities.referential.Caracteristic; +import fr.ifremer.tutti.persistence.entities.referential.CaracteristicType; +import fr.ifremer.tutti.persistence.entities.referential.FishingOperationLocation; import fr.ifremer.tutti.persistence.entities.referential.Gear; import org.junit.Before; @@ -89,6 +96,9 @@ @Test public void createFishingOperation(/*FishingOperation bean*/) { + // ----------------------------------------------------------------------------- + // Prepare data and other entities + // ----------------------------------------------------------------------------- Calendar calendar = new GregorianCalendar(); FishingOperation reloadedFishingOperation = null; FishingOperation createdFishingOperation = null; @@ -110,8 +120,52 @@ cruise = cruiseService.createCruise(cruise); assertNotNull(cruise.getId()); - + // Retrieve some environment caracteristics + List<Caracteristic> allEnvironmentCaracteristics = referentialService.getAllFishingOperationEnvironmentCaracteristic(); + CaracteristicMap environmentCaracteristics = new CaracteristicMap(); + CaracteristicMap environmentValuesOneEntry = new CaracteristicMap(); + for(Caracteristic caracteristic : allEnvironmentCaracteristics) { + Serializable value = null; + if (caracteristic.getCaracteristicType() == CaracteristicType.NUMBER) { + value = new Float(1.0f); + } + else if (caracteristic.getCaracteristicType() == CaracteristicType.TEXT) { + value = new String("some text"); + } + else if (caracteristic.getCaracteristicType() == CaracteristicType.QUALITATIVE) { + // Choose the first qualitative value + value = caracteristic.getQualitativeValue(0).getId(); + } + environmentCaracteristics.put(caracteristic, value); + if (environmentValuesOneEntry.size() == 0) { + environmentValuesOneEntry.put(caracteristic, value); + } + } + + // Retrieve some gear use caracteristics + List<Caracteristic> allGearShootingCaracteristics = referentialService.getAllFishingOperationGearCaracteristic(); + CaracteristicMap gearShootingCaracteristics = new CaracteristicMap(); + CaracteristicMap gearShootingCaracteristicsOneEntry = new CaracteristicMap(); + for(Caracteristic caracteristic : allEnvironmentCaracteristics) { + Serializable value = null; + if (caracteristic.getCaracteristicType() == CaracteristicType.NUMBER) { + value = new Float(1.0f); + } + else if (caracteristic.getCaracteristicType() == CaracteristicType.TEXT) { + value = new String("some text"); + } + else if (caracteristic.getCaracteristicType() == CaracteristicType.QUALITATIVE) { + // Choose the first qualitative value + value = caracteristic.getQualitativeValue(0).getId(); + } + gearShootingCaracteristics.put(caracteristic, value); + if (gearShootingCaracteristicsOneEntry.size() == 0) { + gearShootingCaracteristicsOneEntry.put(caracteristic, value); + } + } + + // Create new fishing operation : FishingOperation fishingOperation = new FishingOperation(); @@ -157,19 +211,28 @@ calendar.set(Calendar.HOUR_OF_DAY, 10); calendar.set(Calendar.MILLISECOND, 99); fishingOperation.setGearShootingEndDate(calendar.getTime()); - - fishingOperation.setGearShootingStartLatitude(33.2541f); - fishingOperation.setGearShootingStartLongitude(-5.514f); - fishingOperation.setGearShootingEndLatitude(33.7441f); + fishingOperation.setGearShootingStartLatitude(47.6f); + fishingOperation.setGearShootingStartLongitude(-5.05f); + fishingOperation.setGearShootingEndLatitude(47.9854f); fishingOperation.setGearShootingEndLongitude(-5.597f); fishingOperation.setTrawlDistance(100.12345f); fishingOperation.setFishingOperationRectiligne(true); fishingOperation.setFishingOperationValid(Boolean.TRUE); - fishingOperation.setComment("Unit test createFishingOperation() - Part n°2 : All properties set"); - fishingOperation.setGear(cruiseGear); + fishingOperation.setEnvironmentCaracteristics(environmentCaracteristics); + fishingOperation.setGearShootingCaracteristics(gearShootingCaracteristics); + + FishingOperationLocation strata = new FishingOperationLocation(); + strata.setId(dbResource.getFixtures().strataId()); + fishingOperation.setStrata(strata); + FishingOperationLocation subStrata = new FishingOperationLocation(); + subStrata.setId(dbResource.getFixtures().subStrataId()); + fishingOperation.setSubStrata(subStrata); + FishingOperationLocation localite = new FishingOperationLocation(); + localite.setId(dbResource.getFixtures().localite()); + fishingOperation.setLocation(localite); // Store fishing operation into database : createdFishingOperation = service.createFishingOperation(fishingOperation); @@ -197,6 +260,19 @@ assertEquals(fishingOperation.isFishingOperationRectiligne(), reloadedFishingOperation.isFishingOperationRectiligne()); assertEquals(fishingOperation.getFishingOperationValid(), reloadedFishingOperation.getFishingOperationValid()); assertEquals(fishingOperation.getComment(), reloadedFishingOperation.getComment()); + assertNotNull(reloadedFishingOperation.getEnvironmentCaracteristics()); + assertEquals(environmentCaracteristics.size(), fishingOperation.getEnvironmentCaracteristics().size()); + assertNotNull(reloadedFishingOperation.getGearShootingCaracteristics()); + assertEquals(gearShootingCaracteristics.size(), reloadedFishingOperation.getGearShootingCaracteristics().size()); + assertNotNull(reloadedFishingOperation.getStrata()); + assertNotNull(reloadedFishingOperation.getStrata().getId()); + assertEquals(fishingOperation.getStrata().getId(), reloadedFishingOperation.getStrata().getId()); + assertNotNull(reloadedFishingOperation.getSubStrata()); + assertNotNull(reloadedFishingOperation.getSubStrata().getId()); + assertEquals(fishingOperation.getSubStrata().getId(), reloadedFishingOperation.getSubStrata().getId()); + assertNotNull(reloadedFishingOperation.getLocation()); + assertNotNull(reloadedFishingOperation.getLocation().getId()); + assertEquals(fishingOperation.getLocation().getId(), reloadedFishingOperation.getLocation().getId()); // ----------------------------------------------------------------------------- // 3. Test : @@ -228,8 +304,8 @@ assertEquals(fishingOperation.getFishingOperationValid(), reloadedFishingOperation.getFishingOperationValid()); // ----------------------------------------------------------------------------- - // 4. Test : - // - try to save a operation using a gear not declared uin the cruise + // 4. Test exceptions : + // - try to save a operation using a gear not declared in the cruise // - try to save a operation using a trawl net number bigger than the cruise trawl net // ----------------------------------------------------------------------------- fishingOperation.setId(null); @@ -252,6 +328,7 @@ } catch(DataIntegrityViolationException dive) { assertNotNull(dive); + fishingOperation.setGear(cruiseGear); } fishingOperation.setTrawlNetNumber(new Integer(3)); @@ -261,11 +338,43 @@ } catch(DataIntegrityViolationException dive) { assertNotNull(dive); + fishingOperation.setTrawlNetNumber(new Integer(1)); } - //reloadedFishingOperation = service.getFishingOperation(createdFishingOperation.getId()); - //assertEquals(fishingOperation.getGearShootingStartLatitude(), reloadedFishingOperation.getGearShootingStartLatitude()); - //assertEquals(fishingOperation.getGearShootingStartLongitude(), reloadedFishingOperation.getGearShootingStartLongitude()); + // ----------------------------------------------------------------------------- + // 5. Test update (delete unecessary data) + // - remove positons + // - remove strata, substrata, localite + // - remove some environment carateristics + // - remove some gear shooting carateristics + // ----------------------------------------------------------------------------- + fishingOperation.setId(reloadedFishingOperation.getId()); + fishingOperation.setGearShootingStartLatitude(null); + fishingOperation.setGearShootingStartLongitude(null); + fishingOperation.setGearShootingEndDate(null); + fishingOperation.setGearShootingEndLatitude(null); + fishingOperation.setGearShootingEndLongitude(null); + fishingOperation.setTrawlDistance(null); + fishingOperation.setStrata(null); + fishingOperation.setSubStrata(null); + fishingOperation.setLocation(null); + fishingOperation.setEnvironmentCaracteristics(environmentValuesOneEntry); + fishingOperation.setGearShootingCaracteristics(gearShootingCaracteristicsOneEntry); + fishingOperation.setComment(fishingOperation.getComment() + "\n\nUnit test createFishingOperation() - Part n°5 : check if deleted sub items in DB"); + createdFishingOperation = service.saveFishingOperation(fishingOperation); + reloadedFishingOperation = service.getFishingOperation(createdFishingOperation.getId()); + assertNull(reloadedFishingOperation.getGearShootingStartLatitude()); + assertNull(reloadedFishingOperation.getGearShootingStartLongitude()); + assertNull(reloadedFishingOperation.getGearShootingEndDate()); + assertNull(reloadedFishingOperation.getGearShootingEndLatitude()); + assertNull(reloadedFishingOperation.getGearShootingEndLongitude()); + assertNull(reloadedFishingOperation.getStrata()); + assertNull(reloadedFishingOperation.getSubStrata()); + assertNull(reloadedFishingOperation.getLocation()); + assertNotNull(reloadedFishingOperation.getEnvironmentCaracteristics()); + assertEquals(environmentValuesOneEntry.size(), reloadedFishingOperation.getEnvironmentCaracteristics().size()); + assertNotNull(reloadedFishingOperation.getGearShootingCaracteristics()); + assertEquals(gearShootingCaracteristicsOneEntry.size(), reloadedFishingOperation.getGearShootingCaracteristics().size()); } @Test
participants (1)
-
blavenier@users.forge.codelutin.com