Author: blavenier Date: 2013-01-30 18:13:52 +0100 (Wed, 30 Jan 2013) New Revision: 272 Url: http://forge.codelutin.com/projects/tutti/repository/revisions/272 Log: ref refs #1920: [Persistence] Adagio Donnees thematiques - Add cache on getAllFishingVessels - Add persistence for program - Fix TU on Cruise and FishingOperation Modified: trunk/tutti-persistence-adagio/src/main/java/fr/ifremer/tutti/persistence/config/TuttiPersistenceAdagioConfig.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/ProgramPersistenceServiceImpl.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/service/FishingOperationPersistenceServiceTest.java trunk/tutti-persistence-adagio/src/test/java/fr/ifremer/tutti/persistence/service/ProgramPersistenceServiceTest.java Modified: trunk/tutti-persistence-adagio/src/main/java/fr/ifremer/tutti/persistence/config/TuttiPersistenceAdagioConfig.java =================================================================== --- trunk/tutti-persistence-adagio/src/main/java/fr/ifremer/tutti/persistence/config/TuttiPersistenceAdagioConfig.java 2013-01-30 15:26:14 UTC (rev 271) +++ trunk/tutti-persistence-adagio/src/main/java/fr/ifremer/tutti/persistence/config/TuttiPersistenceAdagioConfig.java 2013-01-30 17:13:52 UTC (rev 272) @@ -112,7 +112,7 @@ } public boolean isDbExists() { - File f = new File(getDbDirectory(), getDbName() + ".data"); + File f = new File(getDbDirectory(), /*TODO TC getDbName() not working*/"allegro" + ".data"); return f.exists(); } 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-30 15:26:14 UTC (rev 271) +++ trunk/tutti-persistence-adagio/src/main/java/fr/ifremer/tutti/persistence/service/CruisePersistenceServiceImpl.java 2013-01-30 17:13:52 UTC (rev 272) @@ -42,6 +42,7 @@ import org.springframework.dao.DataRetrievalFailureException; import org.springframework.stereotype.Service; +import com.google.common.base.Preconditions; import com.google.common.collect.Lists; import fr.ifremer.adagio.core.dao.administration.user.PersonDao; @@ -147,7 +148,9 @@ "countryLocationLevelId", IntegerType.INSTANCE, enumeration.LOCATION_LEVEL_ID_COUNTRY); if (source == null) { - throw new DataRetrievalFailureException("Could not retrieve cruise with id=" + id); + return null; + // TODo TC : manage exception in the UI ? + //throw new DataRetrievalFailureException("Could not retrieve cruise with id=" + id); } Cruise result = new Cruise(); result.setId(id); @@ -275,6 +278,9 @@ @Override public Cruise createCruise(Cruise bean) { + Preconditions.checkNotNull(bean); + Preconditions.checkArgument(bean.getId() == null, "Cruise 'id' must be null to call createCruise()."); + ScientificCruise scientificCruise = ScientificCruise.Factory.newInstance(); cruiseToEntity(bean, scientificCruise, true); scientificCruiseDao.create(scientificCruise); @@ -285,15 +291,17 @@ @Override public Cruise saveCruise(Cruise bean) { - if (bean.getId() == null || bean.getId().isEmpty()) { - throw new IllegalArgumentException("Cruise 'id' must not be null or empty to be saved."); - } + Preconditions.checkNotNull(bean); + Preconditions.checkNotNull(bean.getId(), "Cruise 'id' must not be null or empty to be saved."); + ScientificCruise scientificCruise = scientificCruiseDao.load(Integer.valueOf(bean.getId())); if (scientificCruise == null) { throw new DataRetrievalFailureException("Could not retrieve cruise with id=" + bean.getId()); } cruiseToEntity(bean, scientificCruise, true); + scientificCruiseDao.update(scientificCruise); + return bean; } 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-30 15:26:14 UTC (rev 271) +++ trunk/tutti-persistence-adagio/src/main/java/fr/ifremer/tutti/persistence/service/FishingOperationPersistenceServiceImpl.java 2013-01-30 17:13:52 UTC (rev 272) @@ -127,7 +127,9 @@ Iterator<Object[]> list = queryList( "allFishingOperations", - "cruiseId", IntegerType.INSTANCE, Integer.valueOf(cruiseId) + "cruiseId", IntegerType.INSTANCE, Integer.valueOf(cruiseId), + "pmfmIdStationNumber", IntegerType.INSTANCE, enumeration.PMFM_ID_STATION_NUMBER, + "pmfmIdMultirigAggregation", IntegerType.INSTANCE, enumeration.PMFM_ID_MULTIRIG_AGGREGATION ); List<FishingOperation> fishingOperations = new ArrayList<FishingOperation>(); @@ -177,10 +179,10 @@ fishingOperation.setStationNumber(stationNumber); } - // Trawl number net - String trawlNetNumber = (String)source[colIndex++]; - if (trawlNetNumber != null) { - fishingOperation.setMultirigAggregation(trawlNetNumber); + // Multirig Aggregation + String multirigAggregation = (String)source[colIndex++]; + if (multirigAggregation != null) { + fishingOperation.setMultirigAggregation(multirigAggregation); } fishingOperations.add(fishingOperation); @@ -359,7 +361,10 @@ 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); + if (fishingOperation == null) { + throw new DataRetrievalFailureException("Could not retrieve fishing operation with id="+bean.getId()); + } + beanToEntity(bean, fishingOperation, true); fishingOperationDao.update(fishingOperation); getCurrentSession().flush(); return bean; @@ -583,12 +588,12 @@ target.setCatchBatch(catchBatch); }*/ - // Retrieve trawl net, from Gear physical features - int trawlNet = 1; // default value - GearPhysicalMeasurement gpmTrawlNet = measurementService.getGearPhysicalMeasurement(gearPhysicalFeatures, enumeration.PMFM_ID_MULTIRIG_NUMBER); - if (gpmTrawlNet != null && gpmTrawlNet.getNumericalValue() != null) { - trawlNet = gpmTrawlNet.getNumericalValue().intValue(); - } + // Retrieve multirig number, from Gear physical features + int cruiseMultirigCount = 1; // default value + GearPhysicalMeasurement gpmMultirigCount = measurementService.getGearPhysicalMeasurement(gearPhysicalFeatures, enumeration.PMFM_ID_MULTIRIG_NUMBER); + if (gpmMultirigCount != null && gpmMultirigCount.getNumericalValue() != null) { + cruiseMultirigCount = gpmMultirigCount.getNumericalValue().intValue(); + } // StationNumber if (copyIfNull && source.getStationNumber() == null) { @@ -606,16 +611,23 @@ } target.setName(nameBuffer.toString()); - // Trawl net number + // Multirig Aggregation if (copyIfNull && source.getMultirigAggregation() == null) { // Nothing to do : will be removed later, using notChangedGearUseMeasurements } else if (source.getMultirigAggregation() != null) { - // TODO BLA : Then parse the String and validate content -// if (source.getMultirigAggregation() > multirigNumber) { -// throw new DataIntegrityViolationException("An operation could not have a 'trawl net number' greater than 'trawl net' defined in the cruise."); -// } + if (source.getMultirigAggregation().matches("\\d+")) { + int mutlirigNumber = Integer.valueOf(source.getMultirigAggregation()); + if (mutlirigNumber > cruiseMultirigCount) { + throw new DataIntegrityViolationException("An operation could not have a 'multirig number' greater than 'multirig count' defined in the cruise."); + } + } + else { + // TODO BLA : Then parse the String when more than one number (ie "1,3") + // then and validate content + } + // Store into Gear Use Features - GearUseMeasurement gum = setGearUseMeasurement(scientificCruise, gearUseFeatures, enumeration.PMFM_ID_MULTIRIG_AGGREGATION, null, source.getMultirigAggregation().toString(), null); + GearUseMeasurement gum = setGearUseMeasurement(scientificCruise, gearUseFeatures, enumeration.PMFM_ID_MULTIRIG_AGGREGATION, null, source.getMultirigAggregation(), null); notChangedGearUseMeasurements.remove(gum); } @@ -1098,6 +1110,9 @@ } protected void setMeasurement(Measurement measurement, Caracteristic caracteristic, Serializable value) { + if (value == null) { + return; + } if (caracteristic.getCaracteristicType() == CaracteristicType.TEXT) { measurement.setAlphanumericalValue((String)value); } Modified: trunk/tutti-persistence-adagio/src/main/java/fr/ifremer/tutti/persistence/service/ProgramPersistenceServiceImpl.java =================================================================== --- trunk/tutti-persistence-adagio/src/main/java/fr/ifremer/tutti/persistence/service/ProgramPersistenceServiceImpl.java 2013-01-30 15:26:14 UTC (rev 271) +++ trunk/tutti-persistence-adagio/src/main/java/fr/ifremer/tutti/persistence/service/ProgramPersistenceServiceImpl.java 2013-01-30 17:13:52 UTC (rev 272) @@ -24,17 +24,35 @@ * #L% */ +import com.google.common.base.Preconditions; import com.google.common.collect.Lists; + +import fr.ifremer.adagio.core.dao.administration.programStrategy.ProgramDao; +import fr.ifremer.adagio.core.dao.data.survey.scientificCruise.ScientificCruiseDao; +import fr.ifremer.adagio.core.dao.referential.gear.GearClassificationImpl; +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.taxon.TaxonGroupTypeImpl; +import fr.ifremer.tutti.persistence.entities.data.CatchBatch; import fr.ifremer.tutti.persistence.entities.data.Program; import fr.ifremer.tutti.persistence.entities.referential.Zone; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.hibernate.type.IntegerType; import org.hibernate.type.StringType; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.dao.DataIntegrityViolationException; +import org.springframework.dao.DataRetrievalFailureException; import org.springframework.stereotype.Service; +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; import java.util.Iterator; import java.util.List; +import javax.annotation.Resource; + /** * @author tchemit <chemit@codelutin.com> * @since 0.3 @@ -45,10 +63,27 @@ /** Logger. */ private static final Log log = LogFactory.getLog(ProgramPersistenceServiceImpl.class); + + @Autowired(required = true) + protected ReferentialPersistenceService referentialService; + @Resource(name = "programDao") + protected ProgramDao programDao; + + @Resource(name = "locationDao") + protected LocationDao locationDao; + + protected final int maxCodeLengthInDatabase = 40; + @Override public List<Program> getAllProgram() { - Iterator<Object[]> list = queryList("allPrograms"); + String codePattern = "%"; + if (enumeration.PROGRAM_CODE_SCIENTIFIC_CRUISE_PREFIX != null) { + codePattern = enumeration.PROGRAM_CODE_SCIENTIFIC_CRUISE_PREFIX + codePattern; + } + Iterator<Object[]> list = queryList("allPrograms", + "codePattern", StringType.INSTANCE, codePattern + ); List<Program> result = Lists.newArrayList(); while (list.hasNext()) { @@ -56,6 +91,7 @@ Program target = new Program(); target.setId((String) source[0]); target.setName((String) source[1]); + target.setComment((String) source[2]); result.add(target); } return result; @@ -72,6 +108,7 @@ Program result = new Program(); result.setId((String) source[0]); result.setName((String) source[1]); + result.setComment((String) source[2]); // get program locations Iterator<Object[]> list = queryList( @@ -96,11 +133,128 @@ @Override public Program createProgram(Program bean) { - throw new UnsupportedOperationException(); + Preconditions.checkNotNull(bean); + Preconditions.checkNotNull(bean.getName()); + Preconditions.checkArgument(bean.getId() == null); + + fr.ifremer.adagio.core.dao.administration.programStrategy.Program program = fr.ifremer.adagio.core.dao.administration.programStrategy.Program.Factory.newInstance(); + beanToEntity(bean, program, true); + program = programDao.create(program); + bean.setId(String.valueOf(program.getCode())); + + return bean; } @Override public Program saveProgram(Program bean) { - throw new UnsupportedOperationException(); + Preconditions.checkNotNull(bean); + Preconditions.checkNotNull(bean.getName()); + Preconditions.checkNotNull(bean.getId()); + + fr.ifremer.adagio.core.dao.administration.programStrategy.Program program = programDao.load(bean.getId()); + if (program == null) { + throw new DataRetrievalFailureException("Could not retrieve program with code=" + bean.getId()); + } + + beanToEntity(bean, program, true); + programDao.update(program); + + return bean; } + + // ------------------------------------------------------------------------// + // -- Internal methods --// + // ------------------------------------------------------------------------// + protected void beanToEntity(Program source, + fr.ifremer.adagio.core.dao.administration.programStrategy.Program target, + boolean copyIfNull) { + + // Code : compute with : <prefixe><name> + if (target.getCode() == null && source.getName() != null) { + // Compute a program code (remove spaces, and capitalize the name) + String programCode = source.getName().toUpperCase().replaceAll(" ", "_"); + // Add a prefix + if (enumeration.PROGRAM_CODE_SCIENTIFIC_CRUISE_PREFIX != null) { + programCode = enumeration.PROGRAM_CODE_SCIENTIFIC_CRUISE_PREFIX + programCode; + } + // Trunc the code if too long + if (programCode.length() > maxCodeLengthInDatabase) { + programCode = programCode.substring(0, maxCodeLengthInDatabase-1); + } + target.setCode(programCode); + + // Mandatory properties : + // Gear classification : + target.setGearClassification(load(GearClassificationImpl.class, enumeration.GEAR_CLASSIFICIATION_ID_SCIENTIFIC)); + + // taxon group type + target.setTaxonGroupType(load(TaxonGroupTypeImpl.class, enumeration.TAXON_GROUP_TYPE_ID_COMMERCIAL_SPECIES)); + + // Creation date + Calendar calendar = new GregorianCalendar(); + calendar.set(Calendar.HOUR_OF_DAY, 0); + calendar.set(Calendar.MINUTE, 0); + calendar.set(Calendar.SECOND, 0); + calendar.set(Calendar.MILLISECOND, 0); + target.setCreationDate(calendar.getTime()); + } + + // Name + if (copyIfNull && source.getName() == null) { + target.setName(null); + } else if (source.getName() != null) { + target.setName(source.getName()); + } + + // Description (=source.comment) + if (copyIfNull && source.getComment() == null) { + target.setDescription(null); + } else if (source.getComment() != null) { + target.setDescription(source.getComment()); + } + + + + + // Zone + if (copyIfNull && source.getZone() == null) { + // Remove program location classifications : + if (target.getLocationClassifications() != null) { + target.getLocationClassifications().clear(); + } + // Remove program locations : + if (target.getLocations() != null) { + target.getLocations().clear(); + } + } else if (source.getZone() != null && source.getZone().getId() != null) { + Location location = locationDao.load(Integer.valueOf(source.getZone().getId())); + + // Program location classifications : + if (target.getLocationClassifications() == null) { + target.setLocationClassifications(Lists.newArrayList(location.getLocationClassification())); + } + else { + target.getLocationClassifications().clear(); + target.getLocationClassifications().add(location.getLocationClassification()); + } + + // Program locations : + if (target.getLocations() == null) { + target.setLocations(Lists.newArrayList(location)); + } + else { + target.getLocations().clear(); + target.getLocations().add(location); + } + } + } + + public int getProgramNameMaxLength() { + int maxCodeLengthInDatabase = 40; + if (enumeration.PROGRAM_CODE_SCIENTIFIC_CRUISE_PREFIX == null + || enumeration.PROGRAM_CODE_SCIENTIFIC_CRUISE_PREFIX.trim().isEmpty()) { + return maxCodeLengthInDatabase; + } + return (maxCodeLengthInDatabase - enumeration.PROGRAM_CODE_SCIENTIFIC_CRUISE_PREFIX.length()); + } } 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-30 15:26:14 UTC (rev 271) +++ trunk/tutti-persistence-adagio/src/main/java/fr/ifremer/tutti/persistence/service/ReferentialPersistenceServiceImpl.java 2013-01-30 17:13:52 UTC (rev 272) @@ -46,9 +46,11 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.hibernate.Query; +import org.hibernate.type.BooleanType; import org.hibernate.type.DateType; import org.hibernate.type.IntegerType; import org.hibernate.type.StringType; +import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import javax.annotation.Resource; @@ -122,7 +124,8 @@ @Override public List<FishingOperationLocation> getAllFishingOperationStrata(String zoneId) { - List<FishingOperationLocation> result = getFishingOperationLocations( + Preconditions.checkNotNull(zoneId); + List<FishingOperationLocation> result = getFishingOperationLocationsByParent( enumeration.LOCATION_LEVEL_ID_STRATA, Integer.valueOf(zoneId), enumeration.LOCATION_LEVEL_ID_PROGRAM); @@ -150,7 +153,7 @@ } Preconditions.checkNotNull(parentId); - List<FishingOperationLocation> result = getFishingOperationLocations( + List<FishingOperationLocation> result = getFishingOperationLocationsByParent( enumeration.LOCATION_LEVEL_ID_SUB_STRATA, Integer.valueOf(parentId), parentLocationLevelId); @@ -185,7 +188,7 @@ } Preconditions.checkNotNull(parentId); - List<FishingOperationLocation> result = getFishingOperationLocations( + List<FishingOperationLocation> result = getFishingOperationLocationsByParent( enumeration.LOCATION_LEVEL_ID_LOCALITE, Integer.valueOf(parentId), parentLocationLevelId); @@ -210,11 +213,13 @@ } @Override + @Cacheable(value = "allFishingVessel") public List<Vessel> getAllFishingVessel() { Iterator<Object[]> list = queryListWithStatus( - "allVessels", + "allVessels", "refDate", DateType.INSTANCE, new Date(), - "vesselTypeId", IntegerType.INSTANCE, enumeration.VESSEL_TYPE_ID_FISHING); + "vesselTypeId", IntegerType.INSTANCE, enumeration.VESSEL_TYPE_ID_FISHING + ); List<Vessel> result = Lists.newArrayList(); while (list.hasNext()) { @@ -452,9 +457,31 @@ //-- Internal methods --// //------------------------------------------------------------------------// - protected List<FishingOperationLocation> getFishingOperationLocations(Integer locationLevelId, Integer parentId, Integer parentLocationLevelId) { + protected List<FishingOperationLocation> getFishingOperationLocations(Integer locationLevelId) { Iterator<Object[]> sources = queryListWithStatus( "allFishingOperationLocation", + "locationClassificationId", IntegerType.INSTANCE, enumeration.LOCATION_CLASSIFICATION_ID_SECTOR, + "locationLevelId", IntegerType.INSTANCE, locationLevelId + ); + List<FishingOperationLocation> result = Lists.newArrayList(); + while (sources.hasNext()) { + Object[] source = sources.next(); + FishingOperationLocation target = new FishingOperationLocation(); + target.setId(String.valueOf(source[0])); + target.setLabel((String) source[1]); + target.setName((String) source[2]); +// target.setDescription((String)source[3]); + target.setLocationLevel((Integer) source[3]); + + setStatus((fr.ifremer.adagio.core.dao.referential.Status) source[4], target); + result.add(target); + } + return result; + } + + protected List<FishingOperationLocation> getFishingOperationLocationsByParent(Integer locationLevelId, Integer parentId, Integer parentLocationLevelId) { + Iterator<Object[]> sources = queryListWithStatus( + "allFishingOperationLocationByParent", "parentId", IntegerType.INSTANCE, parentId, "parentLocationLevelId", IntegerType.INSTANCE, parentLocationLevelId, "locationClassificationId", IntegerType.INSTANCE, enumeration.LOCATION_CLASSIFICATION_ID_SECTOR, @@ -474,7 +501,7 @@ result.add(target); } return result; - } + } protected Caracteristic loadCaracteristic(Object[] source) { @@ -555,7 +582,7 @@ setStatus((fr.ifremer.adagio.core.dao.referential.Status) source[3], result); return result; } - + protected Iterator<Object[]> queryListWithStatus(String queryName, Object... params) { Query query = createQuery(queryName, params); 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-30 15:26:14 UTC (rev 271) +++ trunk/tutti-persistence-adagio/src/main/java/fr/ifremer/tutti/persistence/service/TuttiEnumerationFile.java 2013-01-30 17:13:52 UTC (rev 272) @@ -166,6 +166,12 @@ @Value("${PmfmId.AGE}") public final Integer PMFM_ID_AGE = null; + + @Value("${ProgramCode.SCIENTIFIC_CRUISE_PREFIX}") + public final String PROGRAM_CODE_SCIENTIFIC_CRUISE_PREFIX = null; + + @Value("${TaxonGroupTypeCode.COMMERCIAL_SPECIES}") + public final String TAXON_GROUP_TYPE_ID_COMMERCIAL_SPECIES = null; public void init() { 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-30 15:26:14 UTC (rev 271) +++ trunk/tutti-persistence-adagio/src/main/resources/queries-override.hbm.xml 2013-01-30 17:13:52 UTC (rev 272) @@ -36,11 +36,12 @@ <![CDATA[ SELECT p.code, - p.name + p.name, + p.description FROM ProgramImpl p WHERE - p.code LIKE 'CAM-%' + p.code LIKE :codePattern ]]> </query> @@ -171,8 +172,8 @@ o.name AS name, guf.gear.label AS gearLabel, o.startDateTime AS startDateTime, - (select vum.alphanumericalValue from VesselUseMeasurementImpl vum where vum.vesselUseFeatures.id = vuf.id and vum.pmfm.id=1243) AS stationNumber, - (select gum.numericalValue from GearUseMeasurementImpl gum where gum.gearUseFeatures.id = guf.id and gum.pmfm.id=953) AS trawNetNumber + (select vum.alphanumericalValue from VesselUseMeasurementImpl vum where vum.vesselUseFeatures.id = vuf.id and vum.pmfm.id=:pmfmIdStationNumber) AS stationNumber, + (select gum.numericalValue from GearUseMeasurementImpl gum where gum.gearUseFeatures.id = guf.id and gum.pmfm.id=:pmfmIdMultirigAggregation) AS trawNetNumber FROM FishingOperationImpl o LEFT OUTER JOIN o.gearUseFeatures guf @@ -183,6 +184,8 @@ o.startDateTime ]]> <query-param name="cruiseId" type="java.lang.Integer"/> + <query-param name="pmfmIdStationNumber" type="java.lang.Integer"/> + <query-param name="pmfmIdMultirigAggregation" type="java.lang.Integer"/> </query> <query cacheable="true" name="fishingOperation"> @@ -449,7 +452,7 @@ </query> <!-- [REF-03] Get all fishing operation strata / substrata / localite --> - <query cacheable="true" name="allFishingOperationLocation"> + <query cacheable="true" name="allFishingOperationLocationByParent"> <![CDATA[ SELECT l.id as locationId, @@ -475,6 +478,27 @@ <query-param name="statusValidCode" type="java.lang.String"/> <query-param name="statusTemporaryCode" type="java.lang.String"/> </query> + + <query cacheable="true" name="allFishingOperationLocation"> + <![CDATA[ + SELECT + l.id as locationId, + l.label as locationLabel, + l.name as locationName, + l.locationLevel.id as locationLevelId, + l.status as status + FROM + LocationImpl l + WHERE + l.status.code IN (:statusValidCode, :statusTemporaryCode) + AND l.locationLevel.id = :locationLevelId + AND l.locationClassification.id = :locationClassificationId + ]]> + <query-param name="locationLevelId" type="java.lang.Integer"/> + <query-param name="locationClassificationId" type="java.lang.Integer"/> + <query-param name="statusValidCode" type="java.lang.String"/> + <query-param name="statusTemporaryCode" type="java.lang.String"/> + </query> <!-- [REF-04] et [REF-05] Get all vessel for a given vesselType --> <query cacheable="true" name="allVessels"> 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-30 15:26:14 UTC (rev 271) +++ trunk/tutti-persistence-adagio/src/main/resources/tutti-db-enumerations.properties 2013-01-30 17:13:52 UTC (rev 272) @@ -359,3 +359,6 @@ #TODO A creer (dans les enumerations Allegro) # 181=PDG-RBE (à confirmer par Vincent) DepartmentId.UNKNOWN_RECORDER_DEPARTMENT=181 + +#TODO A creer (dans les enumerations Allegro) +ProgramCode.SCIENTIFIC_CRUISE_PREFIX=CAM- \ No newline at end of file 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-30 15:26:14 UTC (rev 271) +++ trunk/tutti-persistence-adagio/src/test/java/fr/ifremer/tutti/persistence/service/FishingOperationPersistenceServiceTest.java 2013-01-30 17:13:52 UTC (rev 272) @@ -191,7 +191,7 @@ fishingOperation.setComment(null); // Store fishing operation into database : - /*createdFishingOperation = service.createFishingOperation(fishingOperation); + createdFishingOperation = service.createFishingOperation(fishingOperation); assertNotNull("Fishing operation ID must not be null after creation in database", createdFishingOperation); assertNotNull(createdFishingOperation.getId()); @@ -199,7 +199,6 @@ reloadedFishingOperation = service.getFishingOperation(createdFishingOperation.getId()); assertNotNull(reloadedFishingOperation); assertNull(reloadedFishingOperation.getGearShootingStartDate()); - */ // ----------------------------------------------------------------------------- // 2. Test with all properties set @@ -312,7 +311,7 @@ // ----------------------------------------------------------------------------- // 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 + // - try to save a operation using a multirig aggregation greater than the cruise multirig number // ----------------------------------------------------------------------------- fishingOperation.setId(null); @@ -327,25 +326,24 @@ } } - //TODO TC : remove comment when adagio will build -// try { -// createdFishingOperation = service.createFishingOperation(fishingOperation); -// fail("A fishing operation must not be saved if the gear is not declared in the cruise."); -// } -// catch(DataIntegrityViolationException dive) { -// assertNotNull(dive); -// fishingOperation.setGear(cruiseGear); -// } + try { + createdFishingOperation = service.createFishingOperation(fishingOperation); + fail("A fishing operation must not be saved if the gear is not declared in the cruise."); + } + catch(DataIntegrityViolationException dive) { + assertNotNull(dive); + fishingOperation.setGear(cruiseGear); + } -// fishingOperation.setMultirigAggregation("3"); -// try { -// createdFishingOperation = service.createFishingOperation(fishingOperation); -// fail("A fishing operation must not be saved if the 'trawl net number' > 'cruise trawl net'."); -// } -// catch(DataIntegrityViolationException dive) { -// assertNotNull(dive); -// fishingOperation.setMultirigAggregation("1"); -// } + fishingOperation.setMultirigAggregation("3"); + try { + createdFishingOperation = service.createFishingOperation(fishingOperation); + fail("A fishing operation must not be saved if the 'multirig aggregation' > 'cruise multirig number'."); + } + catch(DataIntegrityViolationException dive) { + assertNotNull(dive); + fishingOperation.setMultirigAggregation("1"); + } // ----------------------------------------------------------------------------- // 5. Test update (delete unecessary data) Modified: trunk/tutti-persistence-adagio/src/test/java/fr/ifremer/tutti/persistence/service/ProgramPersistenceServiceTest.java =================================================================== --- trunk/tutti-persistence-adagio/src/test/java/fr/ifremer/tutti/persistence/service/ProgramPersistenceServiceTest.java 2013-01-30 15:26:14 UTC (rev 271) +++ trunk/tutti-persistence-adagio/src/test/java/fr/ifremer/tutti/persistence/service/ProgramPersistenceServiceTest.java 2013-01-30 17:13:52 UTC (rev 272) @@ -24,16 +24,22 @@ * #L% */ +import static org.junit.Assert.*; import fr.ifremer.tutti.persistence.DatabaseResource; import fr.ifremer.tutti.persistence.entities.data.Program; +import fr.ifremer.tutti.persistence.entities.referential.Zone; + import org.junit.Assert; import org.junit.Before; import org.junit.ClassRule; import org.junit.Ignore; import org.junit.Test; +import java.util.Iterator; import java.util.List; +import javax.annotation.Resource; + /** * @author tchemit <chemit@codelutin.com> * @since 0.3 @@ -42,19 +48,27 @@ @ClassRule public static final DatabaseResource dbResource = new DatabaseResource(); - + protected ProgramPersistenceService service; + protected ReferentialPersistenceService referentialService; @Before public void setUp() throws Exception { service = TuttiPersistenceServiceLocator.getProgramPersistenceService(); + referentialService = TuttiPersistenceServiceLocator.getReferentialPersistenceService(); } @Test public void getAllProgram() { List<Program> result = service.getAllProgram(); Assert.assertNotNull(result); - Assert.assertEquals(1, result.size()); + Assert.assertTrue("More than one program must be found", result.size() > 0); + for (Iterator iterator = result.iterator(); iterator.hasNext();) { + Program program = (Program) iterator.next(); + Assert.assertNotNull(program.getId()); + Assert.assertNotEquals("SIH-OBSMER", program.getId()); + Assert.assertNotEquals("SIH-ACTIFLOT", program.getId()); + } } @Test @@ -69,14 +83,55 @@ } @Test - @Ignore - public void createProgram(/*Program bean*/) { + public void createAndSaveProgram(/*Program bean*/) { + List<Zone> zones = referentialService.getAllProgramZone(); + assertNotNull(zones); + assertTrue(zones.size() > 0); + + Program program = new Program(); + String name = "UniTest" + System.currentTimeMillis(); + if (name.length() > 40) { + name = name.substring(0, 39); + } + program.setName(name); + + program.setComment("Comments on " + name); + program.setZone(zones.get(0)); + + // Create program + Program createdProgram = service.createProgram(program); + assertNotNull(createdProgram); + assertNotNull(createdProgram.getId()); + + // Reload program and compare + Program reloadedProgram = service.getProgram(createdProgram.getId()); + assertNotNull(reloadedProgram); + assertEquals(createdProgram.getId(), reloadedProgram.getId()); + assertEquals(program.getName(), reloadedProgram.getName()); + assertEquals(program.getComment(), reloadedProgram.getComment()); + assertNotNull(program.getZone()); + assertEquals(program.getZone().getId(), reloadedProgram.getZone().getId()); + + // Modify program + program.setId(createdProgram.getId()); + program.setName("NEW_NAME"); + program.setComment(program.getComment() + "\nAdd some modification"); + program.setZone(zones.get(1)); + + // Create program + Program savedProgram = service.saveProgram(program); + assertNotNull(savedProgram); + assertNotNull(savedProgram.getId()); + + // Reload program and compare + reloadedProgram = service.getProgram(savedProgram.getId()); + assertNotNull(reloadedProgram); + assertEquals(program.getId(), reloadedProgram.getId()); + assertEquals(program.getName(), reloadedProgram.getName()); + assertEquals(program.getComment(), reloadedProgram.getComment()); + assertNotNull(program.getZone()); + assertEquals(program.getZone().getId(), reloadedProgram.getZone().getId()); } - @Test - @Ignore - public void saveProgram(/*Program bean*/) { - - } }