Author: tchemit Date: 2013-05-01 15:30:06 +0200 (Wed, 01 May 2013) New Revision: 906 Url: http://forge.codelutin.com/projects/tutti/repository/revisions/906 Log: refs #2221: Rendre les donn?\195?\169es CGFS compatible avec Tutti (am?\195?\169lioration validation arbre + api) Modified: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/TuttiPersistenceImpl.java trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/AccidentalBatchPersistenceServiceImpl.java trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/AttachmentPersistenceServiceImpl.java trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/BenthosBatchPersistenceServiceImpl.java trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/CatchBatchPersistenceService.java trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/CatchBatchPersistenceServiceImpl.java trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/IndividualObservationBatchPersistenceServiceImpl.java trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/MarineLitterBatchPersistenceServiceImpl.java trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/SpeciesBatchPersistenceServiceImpl.java trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/batch/BatchPersistenceHelper.java trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/batch/ScientificCruiseCatchBatchValidator.java trunk/tutti-persistence/src/main/resources/i18n/tutti-persistence_en_GB.properties trunk/tutti-persistence/src/main/resources/i18n/tutti-persistence_fr_FR.properties trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/CatchBatchPersistenceServiceReadTest.java trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/CatchBatchPersistenceServiceWriteTest.java Modified: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/TuttiPersistenceImpl.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/TuttiPersistenceImpl.java 2013-05-01 13:21:10 UTC (rev 905) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/TuttiPersistenceImpl.java 2013-05-01 13:30:06 UTC (rev 906) @@ -25,7 +25,6 @@ */ import com.google.common.collect.Maps; -import fr.ifremer.adagio.core.dao.data.batch.validator.CatchBatchValidationException; import fr.ifremer.adagio.core.service.technical.CacheService; import fr.ifremer.tutti.persistence.entities.data.AccidentalBatch; import fr.ifremer.tutti.persistence.entities.data.AttachementObjectTypeEnum; @@ -579,12 +578,7 @@ @Override public CatchBatch getCatchBatchFromFishingOperation(String id) throws InvalidBatchModelException { - try { - return catchBatchService.getCatchBatchFromFishingOperation(id); - } catch (CatchBatchValidationException e) { - throw new InvalidBatchModelException( - "L'arbre d'échantillonage n'est pas compatible avec celui de Tutti.", e); - } + return catchBatchService.getCatchBatchFromFishingOperation(id); } @Override Modified: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/AccidentalBatchPersistenceServiceImpl.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/AccidentalBatchPersistenceServiceImpl.java 2013-05-01 13:21:10 UTC (rev 905) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/AccidentalBatchPersistenceServiceImpl.java 2013-05-01 13:30:06 UTC (rev 906) @@ -29,7 +29,6 @@ import fr.ifremer.adagio.core.dao.administration.programStrategy.Program; import fr.ifremer.adagio.core.dao.administration.user.DepartmentImpl; import fr.ifremer.adagio.core.dao.administration.user.PersonImpl; -import fr.ifremer.adagio.core.dao.data.operation.FishingOperationDao; import fr.ifremer.adagio.core.dao.data.operation.FishingOperationImpl; import fr.ifremer.adagio.core.dao.data.sample.Sample; import fr.ifremer.adagio.core.dao.data.sample.SampleDao; @@ -79,9 +78,6 @@ @Autowired protected SampleMeasurementPersistenceHelper sampleMeasurementPersistenceHelper; - @Resource(name = "fishingOperationDao") - protected FishingOperationDao fishingOperationDao; - @Resource(name = "sampleDao") protected SampleDao sampleDao; @@ -326,5 +322,4 @@ } } - } Modified: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/AttachmentPersistenceServiceImpl.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/AttachmentPersistenceServiceImpl.java 2013-05-01 13:21:10 UTC (rev 905) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/AttachmentPersistenceServiceImpl.java 2013-05-01 13:30:06 UTC (rev 906) @@ -97,7 +97,7 @@ "allAttachment", "objectId", IntegerType.INSTANCE, objectId, "objectTypeCode", StringType.INSTANCE, objectType.getFieldValue() - ); + ); List<Attachment> result = Lists.newArrayList(); while (list.hasNext()) { Modified: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/BenthosBatchPersistenceServiceImpl.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/BenthosBatchPersistenceServiceImpl.java 2013-05-01 13:21:10 UTC (rev 905) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/BenthosBatchPersistenceServiceImpl.java 2013-05-01 13:30:06 UTC (rev 906) @@ -29,7 +29,6 @@ import com.google.common.collect.Sets; import fr.ifremer.adagio.core.dao.data.batch.Batch; import fr.ifremer.adagio.core.dao.data.batch.CatchBatch; -import fr.ifremer.adagio.core.dao.data.batch.CatchBatchExtendDao; import fr.ifremer.adagio.core.dao.data.batch.SortingBatch; import fr.ifremer.adagio.core.dao.data.measure.QuantificationMeasurement; import fr.ifremer.adagio.core.dao.data.measure.SortingMeasurement; @@ -43,14 +42,12 @@ import fr.ifremer.tutti.persistence.entities.data.BenthosBatch; import fr.ifremer.tutti.persistence.entities.data.BenthosBatchFrequency; import fr.ifremer.tutti.persistence.entities.data.SampleCategoryEnum; -import fr.ifremer.tutti.persistence.entities.data.SpeciesBatch; import fr.ifremer.tutti.persistence.entities.referential.Caracteristic; import fr.ifremer.tutti.persistence.entities.referential.Species; import fr.ifremer.tutti.persistence.service.batch.BatchPersistenceHelper; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.hibernate.FlushMode; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataIntegrityViolationException; import org.springframework.stereotype.Service; @@ -82,9 +79,6 @@ @Autowired protected AttachmentPersistenceService attachmentPersistenceService; - @Resource(name = "catchBatchDao") - protected CatchBatchExtendDao catchBatchDao; - @Resource(name = "batchPersistenceHelper") protected BatchPersistenceHelper batchHelper; @@ -96,46 +90,43 @@ public BatchContainer<BenthosBatch> getRootBenthosBatch(String fishingOperationId) { Preconditions.checkNotNull(fishingOperationId); - // Load batch tree - Integer catchBatchId = catchBatchDao.getIdByFishingOperationId(Integer.valueOf(fishingOperationId)); - Preconditions.checkNotNull(catchBatchId); + CatchBatch catchBatch = batchHelper.getRootCatchBatchByFishingOperationId(fishingOperationId, false); - CatchBatch catchBatch = catchBatchDao.loadFullTree(catchBatchId); - Preconditions.checkNotNull(catchBatch); - - Collection<Batch> catchBatchChils = catchBatch.getChildBatchs(); - // Vrac / Benthos - SortingBatch vracBenthosBatch = catchBatchDao.getSortingBatch( - catchBatchChils, - BatchPersistenceHelper.BATCH_PMFM_ID, enumeration.PMFM_ID_SORTED_UNSORTED, enumeration.QUALITATIVE_VRAC_ID, - BatchPersistenceHelper.BATCH_PMFM_ID, enumeration.PMFM_ID_SORTING_TYPE, enumeration.QUALITATIVE_ID_SORTING_TYPE_BENTHOS, - BatchPersistenceHelper.BATCH_PMFM_ID, enumeration.PMFM_ID_SORTING_TYPE_2, enumeration.QUALITATIVE_ID_SORTING_TYPE_2_ALIVE_ITEMIZED + SortingBatch vracBenthosBatch = batchHelper.getSortingBatch( + catchBatch, + "Vrac > Benthos > Alive Itemized", + enumeration.PMFM_ID_SORTED_UNSORTED, enumeration.QUALITATIVE_VRAC_ID, + enumeration.PMFM_ID_SORTING_TYPE, enumeration.QUALITATIVE_ID_SORTING_TYPE_BENTHOS, + enumeration.PMFM_ID_SORTING_TYPE_2, enumeration.QUALITATIVE_ID_SORTING_TYPE_2_ALIVE_ITEMIZED ); BatchContainer<BenthosBatch> result = new BatchContainer<BenthosBatch>(); - result.setId(vracBenthosBatch.getId()); + if (vracBenthosBatch != null) { + result.setId(vracBenthosBatch.getId()); - for (Batch batch : vracBenthosBatch.getChildBatchs()) { - SortingBatch source = (SortingBatch) batch; - ReferenceTaxon referenceTaxon = source.getReferenceTaxon(); - Preconditions.checkNotNull(referenceTaxon, "Can't have a rootBenthosBatch with a null taxon, but was for " + batch.getId()); - Species species = referentialService.getSpeciesByReferenceTaxonId(referenceTaxon.getId()); + for (Batch batch : vracBenthosBatch.getChildBatchs()) { + SortingBatch source = (SortingBatch) batch; + ReferenceTaxon referenceTaxon = source.getReferenceTaxon(); + Preconditions.checkNotNull(referenceTaxon, "Can't have a rootBenthosBatch with a null taxon, but was for " + batch.getId()); + Species species = referentialService.getSpeciesByReferenceTaxonId(referenceTaxon.getId()); - BenthosBatch target = TuttiBeanFactory.newBenthosBatch(); - target.setSpecies(species); - entityToBenthosBatch(source, target); - result.addChildren(target); - if (log.isInfoEnabled()) { - log.info("Loaded CatchBatch Vrac > Benthos > " + target.getSpecies().getReferenceTaxonId() + " : " + target.getId()); + BenthosBatch target = TuttiBeanFactory.newBenthosBatch(); + target.setSpecies(species); + entityToBenthosBatch(source, target); + result.addChildren(target); + if (log.isInfoEnabled()) { + log.info("Loaded CatchBatch Vrac > Benthos > Alive Itemized > " + target.getSpecies().getReferenceTaxonId() + ": " + target.getId()); + } } } // Hors-Vrac / Benthos - SortingBatch horsVracBenthosBatch = catchBatchDao.getSortingBatch( - catchBatchChils, - BatchPersistenceHelper.BATCH_PMFM_ID, enumeration.PMFM_ID_SORTED_UNSORTED, enumeration.QUALITATIVE_HORS_VRAC_ID, - BatchPersistenceHelper.BATCH_PMFM_ID, enumeration.PMFM_ID_SORTING_TYPE, enumeration.QUALITATIVE_ID_SORTING_TYPE_BENTHOS + SortingBatch horsVracBenthosBatch = batchHelper.getSortingBatch( + catchBatch, + "Hors Vrac > Benthos", + enumeration.PMFM_ID_SORTED_UNSORTED, enumeration.QUALITATIVE_HORS_VRAC_ID, + enumeration.PMFM_ID_SORTING_TYPE, enumeration.QUALITATIVE_ID_SORTING_TYPE_BENTHOS ); if (horsVracBenthosBatch != null) { for (Batch batch : horsVracBenthosBatch.getChildBatchs()) { @@ -150,7 +141,7 @@ entityToBenthosBatch(source, target); result.addChildren(target); if (log.isInfoEnabled()) { - log.info("Loaded CatchBatch Hors Vrac > Benthos > " + target.getSpecies().getReferenceTaxonId() + " : " + target.getId()); + log.info("Loaded CatchBatch Hors Vrac > Benthos > " + target.getSpecies().getReferenceTaxonId() + ": " + target.getId()); } } } @@ -167,20 +158,11 @@ Preconditions.checkNotNull(bean.getFishingOperation()); Preconditions.checkNotNull(bean.getFishingOperation().getId()); - getCurrentSession().setFlushMode(FlushMode.COMMIT); - - // Load full batch tree - Integer catchBatchId = catchBatchDao.getIdByFishingOperationId(bean.getFishingOperation().getIdAsInt()); - CatchBatch catchBatch = catchBatchDao.loadFullTree(catchBatchId); - + CatchBatch catchBatch = batchHelper.getRootCatchBatchByFishingOperationId(bean.getFishingOperation().getId(), false); SortingBatch batch = SortingBatch.Factory.newInstance(); benthosBatchToEntity(bean, batch, parentBatchId, catchBatch); - batch = catchBatchDao.createSortingBatch(batch, catchBatch); + bean = batchHelper.createSortingBatch(bean, catchBatch, batch); - bean.setId(batch.getId()); - - getCurrentSession().flush(); - return bean; } @@ -189,23 +171,16 @@ Preconditions.checkNotNull(bean); Preconditions.checkNotNull(bean.getId()); - // Load batch tree - Integer catchBatchId = catchBatchDao.getIdBySortingBatchId(bean.getIdAsInt()); - Preconditions.checkNotNull(catchBatchId); + Integer batchId = bean.getIdAsInt(); + CatchBatch catchBatch = batchHelper.getRootCatchBatchByBatchId(batchId); + SortingBatch batch = batchHelper.getSortingBatchById(catchBatch, batchId); - CatchBatch catchBatch = catchBatchDao.loadFullTree(catchBatchId); - Preconditions.checkNotNull(catchBatch); - - getCurrentSession().setFlushMode(FlushMode.COMMIT); - - SortingBatch batch = catchBatchDao.getSortingBatchById(catchBatch, bean.getIdAsInt()); String parentBatchId = null; if (bean.getParentBatch() != null) { parentBatchId = bean.getParentBatch().getId(); } benthosBatchToEntity(bean, batch, parentBatchId, catchBatch); - catchBatchDao.updateSortingBatch(batch, catchBatch); - getCurrentSession().flush(); + batchHelper.updateSortingBatch(batch, catchBatch); return bean; } @@ -214,37 +189,22 @@ public void deleteBenthosBatch(String id) { Preconditions.checkNotNull(id); - getCurrentSession().setFlushMode(FlushMode.COMMIT); - Integer batchId = Integer.valueOf(id); - catchBatchDao.removeWithChildren(batchId); + batchHelper.removeWithChildren(batchId); attachmentPersistenceService.deleteAllAttachment( AttachementObjectTypeEnum.BATCH, batchId); - - getCurrentSession().flush(); } @Override public void deleteBenthosSubBatch(String id) { Preconditions.checkNotNull(id); - getCurrentSession().setFlushMode(FlushMode.COMMIT); Integer benthosBatchId = Integer.valueOf(id); - // get catch batch - Integer catchBatchId = catchBatchDao.getIdBySortingBatchId(benthosBatchId); - - Preconditions.checkNotNull( - catchBatchId, - "Could not find catchBatch for benthosBatch: " + benthosBatchId); - - CatchBatch catchBatch = - catchBatchDao.loadFullTree(catchBatchId); - - // get species batch - SortingBatch sortingBatch = catchBatchDao.getSortingBatchById( + CatchBatch catchBatch = batchHelper.getRootCatchBatchByBatchId(benthosBatchId); + SortingBatch sortingBatch = batchHelper.getSortingBatchById( catchBatch, benthosBatchId); // get his children @@ -260,7 +220,7 @@ if (log.isInfoEnabled()) { log.info("Delete child [" + childBatchId + "] of species batch: " + id); } - catchBatchDao.removeWithChildren(childBatchId); + batchHelper.removeWithChildren(childBatchId); // delete his attachment (if any) attachmentPersistenceService.deleteAllAttachment( @@ -268,8 +228,6 @@ childBatchId); } } - - getCurrentSession().flush(); } @Override @@ -279,7 +237,7 @@ Preconditions.checkNotNull(species); Preconditions.checkNotNull(species.getReferenceTaxonId()); - catchBatchDao.setSortingBatchReferenceTaxon(batchId, species.getReferenceTaxonId()); + batchHelper.setSortingBatchReferenceTaxon(batchId, species.getReferenceTaxonId()); } //------------------------------------------------------------------------// @@ -318,18 +276,15 @@ } } - getCurrentSession().setFlushMode(FlushMode.COMMIT); - Integer sortingBatchId = Integer.valueOf(benthosBatchId); - Integer catchBatchId = catchBatchDao.getIdBySortingBatchId(sortingBatchId); - CatchBatch catchBatch = catchBatchDao.loadFullTree(catchBatchId); + CatchBatch catchBatch = batchHelper.getRootCatchBatchByBatchId(sortingBatchId); if (catchBatch == null) { return frequencies; } // Retrieve parent - SortingBatch parentBatch = catchBatchDao.getSortingBatchById(catchBatch, sortingBatchId); + SortingBatch parentBatch = batchHelper.getSortingBatchById(catchBatch, sortingBatchId); // Remember child ids, to remove unchanged item (see at bottom in this method) List<Integer> notUpdatedChildIds = Lists.newArrayList(); @@ -352,13 +307,12 @@ benthosBatchFrequencyToEntity(source, target, parentBatch, rankOrder); // Create the targeted batch, then update the source id - catchBatchDao.createSortingBatch(target, catchBatch); - source.setId(target.getId()); + batchHelper.createSortingBatch(source, catchBatch, target); } // Existing batch else { - target = catchBatchDao.getSortingBatchById(catchBatch, source.getIdAsInt()); + target = batchHelper.getSortingBatchById(catchBatch, source.getIdAsInt()); // Fill the sorting batch from the source benthosBatchFrequencyToEntity(source, target, parentBatch, rankOrder); @@ -372,17 +326,15 @@ // If some batchs need to be update, do it if (batchsToUpdate.size() > 0) { - catchBatchDao.updateSortingBatch(batchsToUpdate, catchBatch); + batchHelper.updateSortingBatch(batchsToUpdate, catchBatch); } if (notUpdatedChildIds.size() > 0) { for (Integer batchId : notUpdatedChildIds) { - catchBatchDao.removeWithChildren(batchId, catchBatch); + batchHelper.removeWithChildren(batchId, catchBatch); } } - getCurrentSession().flush(); - return Collections.unmodifiableList(frequencies); } @@ -416,7 +368,7 @@ if (source.getSortingMeasurements().size() == 1) { sm = source.getSortingMeasurements().iterator().next(); } else if (source.getReferenceTaxon() != null && source.getReferenceTaxon().getId() != null) { - sm = catchBatchDao.getInheritedSortingMeasurement(source, enumeration.PMFM_ID_SORTED_UNSORTED); + sm = batchHelper.getInheritedSortingMeasurement(source); } if (sm != null) { SampleCategoryEnum sampleCategory = enumeration.getSampleCategoryByPmfmId(sm.getPmfm().getId()); @@ -488,8 +440,6 @@ Preconditions.checkNotNull(source.getFishingOperation()); Preconditions.checkNotNull(source.getFishingOperation().getId()); - // Retrieve recorder department - Integer recorderDepartmentId = batchHelper.getRecorderDepartmentId(); Collection<QuantificationMeasurement> quantificationMeasurements = target.getQuantificationMeasurements(); Collection<SortingMeasurement> sortingMeasurements = target.getSortingMeasurements(); @@ -537,12 +487,14 @@ if (source.getWeight() == null && source.getSampleCategoryWeight() == null) { // Nothing to do : will be removed later, using notChangedSortingMeasurements } else if (source.getSampleCategoryWeight() != null && source.getWeight() == null) { - QuantificationMeasurement quantificationMeasurement = catchBatchDao.setQuantificationMeasurement(target, - enumeration.PMFM_ID_WEIGHT_MEASURED, recorderDepartmentId, source.getSampleCategoryWeight(), true); + QuantificationMeasurement quantificationMeasurement = batchHelper.setWeightMeasurementQuantificationMeasurement( + target, + source.getSampleCategoryWeight()); notChangedQuantificationMeasurements.remove(quantificationMeasurement); } else if (source.getWeight() != null && source.getSampleCategoryWeight() == null) { - QuantificationMeasurement quantificationMeasurement = catchBatchDao.setQuantificationMeasurement(target, - enumeration.PMFM_ID_WEIGHT_MEASURED, recorderDepartmentId, source.getWeight(), true); + QuantificationMeasurement quantificationMeasurement = batchHelper.setWeightMeasurementQuantificationMeasurement( + target, + source.getWeight()); notChangedQuantificationMeasurements.remove(quantificationMeasurement); } @@ -555,20 +507,21 @@ samplingRatioText = samplingRatioText.replaceAll(",", "."); target.setSamplingRatioText(samplingRatioText); target.setSamplingRatio(source.getWeight() / source.getSampleCategoryWeight()); - QuantificationMeasurement quantificationMeasurement = catchBatchDao.setQuantificationMeasurement(target, - enumeration.PMFM_ID_WEIGHT_MEASURED, recorderDepartmentId, source.getWeight(), true); + QuantificationMeasurement quantificationMeasurement = batchHelper.setWeightMeasurementQuantificationMeasurement( + target, + source.getWeight()); notChangedQuantificationMeasurements.remove(quantificationMeasurement); } // Sorting measurement - if ((source.getSampleCategoryType() == null || source.getSampleCategoryValue() == null)) { - // Nothing to do : will be removed later, using notChangedSortingMeasurements - } else { + if (source.getSampleCategoryType() != null && source.getSampleCategoryValue() != null) { Integer pmfmId = source.getSampleCategoryType().getFieldValue(); // Do not store sorting measurement if pmfm = SORTED (already store in an ancestor batch) if (!pmfmId.equals(enumeration.PMFM_ID_SORTED_UNSORTED)) { - SortingMeasurement sortingMeasurement = batchHelper.setSortingMeasurement(target, recorderDepartmentId, - source.getSampleCategoryType(), source.getSampleCategoryValue()); + SortingMeasurement sortingMeasurement = batchHelper.setSortingMeasurement( + target, + source.getSampleCategoryType(), + source.getSampleCategoryValue()); notChangedSortingMeasurements.remove(sortingMeasurement); } } @@ -616,9 +569,6 @@ Preconditions.checkNotNull(source.getBatch()); Preconditions.checkNotNull(source.getBatch().getId()); - // Retrieve recorder department - Integer recorderDepartmentId = batchHelper.getRecorderDepartmentId(); - Collection<QuantificationMeasurement> quantificationMeasurements = target.getQuantificationMeasurements(); Collection<SortingMeasurement> sortingMeasurements = target.getSortingMeasurements(); @@ -645,21 +595,17 @@ target.setRankOrder(rankOrder); // Weight or SampleCategoryWeight - if (source.getWeight() == null) { - // Nothing to do : will be removed later, using notChangedSortingMeasurements - } else { - QuantificationMeasurement quantificationMeasurement = catchBatchDao.setQuantificationMeasurement( + if (source.getWeight() != null) { + QuantificationMeasurement quantificationMeasurement = batchHelper.setWeightMeasurementQuantificationMeasurement( target, - enumeration.PMFM_ID_WEIGHT_MEASURED, recorderDepartmentId, source.getWeight(), true); + source.getWeight()); notChangedQuantificationMeasurements.remove(quantificationMeasurement); } // Sorting measurement - if ((source.getLengthStepCaracteristic() == null || source.getLengthStep() == null)) { - // Nothing to do : will be removed later, using notChangedSortingMeasurements - } else { + if ((source.getLengthStepCaracteristic() != null && source.getLengthStep() != null)) { Integer pmfmId = source.getLengthStepCaracteristic().getIdAsInt(); - SortingMeasurement sortingMeasurement = batchHelper.setSortingMeasurement(target, recorderDepartmentId, pmfmId, + SortingMeasurement sortingMeasurement = batchHelper.setSortingMeasurement(target, pmfmId, source.getLengthStep()); notChangedSortingMeasurements.remove(sortingMeasurement); } @@ -705,26 +651,5 @@ alphanumericalValue, qualitativeValueId); target.setSampleCategoryValue(categoryValue); -// if (numericalvalue != null) { -// target.setSampleCategoryValue(numericalvalue); -// return; -// } -// if (alphanumericalValue != null) { -// target.setSampleCategoryValue(alphanumericalValue); -// return; -// } -// -// Caracteristic caracteristic = referentialService.getCaracteristic(pmfmId); -// if (caracteristic == null || caracteristic.getCaracteristicType() != CaracteristicType.QUALITATIVE) { -// return; -// } -// CaracteristicQualitativeValue value = null; -// for (CaracteristicQualitativeValue qv : caracteristic.getQualitativeValue()) { -// if (qualitativeValueId.equals(qv.getIdAsInt())) { -// value = qv; -// break; -// } -// } -// target.setSampleCategoryValue(value); } } Modified: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/CatchBatchPersistenceService.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/CatchBatchPersistenceService.java 2013-05-01 13:21:10 UTC (rev 905) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/CatchBatchPersistenceService.java 2013-05-01 13:30:06 UTC (rev 906) @@ -24,8 +24,6 @@ * #L% */ - -import fr.ifremer.adagio.core.dao.data.batch.validator.CatchBatchValidationException; import fr.ifremer.tutti.persistence.TuttiPersistenceServiceImplementor; import fr.ifremer.tutti.persistence.entities.data.CatchBatch; import org.springframework.transaction.annotation.Transactional; @@ -42,9 +40,8 @@ * * @param fishingOperationId id of the fihsing operation * @return found catchBatch - * @throws CatchBatchValidationException */ - CatchBatch getCatchBatchFromFishingOperation(String fishingOperationId) throws CatchBatchValidationException; + CatchBatch getCatchBatchFromFishingOperation(String fishingOperationId); /** * Create the given CatchBatch and return it. Modified: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/CatchBatchPersistenceServiceImpl.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/CatchBatchPersistenceServiceImpl.java 2013-05-01 13:21:10 UTC (rev 905) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/CatchBatchPersistenceServiceImpl.java 2013-05-01 13:30:06 UTC (rev 906) @@ -26,15 +26,10 @@ import com.google.common.base.Preconditions; -import com.google.common.collect.Maps; import com.google.common.collect.Sets; -import fr.ifremer.adagio.core.dao.data.batch.Batch; -import fr.ifremer.adagio.core.dao.data.batch.CatchBatchExtendDao; +import fr.ifremer.adagio.core.dao.data.batch.CatchBatchImpl; import fr.ifremer.adagio.core.dao.data.batch.SortingBatch; -import fr.ifremer.adagio.core.dao.data.batch.validator.CatchBatchValidationException; -import fr.ifremer.adagio.core.dao.data.batch.validator.CatchBatchValidator; import fr.ifremer.adagio.core.dao.data.measure.QuantificationMeasurement; -import fr.ifremer.adagio.core.dao.data.measure.SortingMeasurement; import fr.ifremer.adagio.core.dao.data.operation.FishingOperationImpl; import fr.ifremer.adagio.core.dao.referential.QualityFlagImpl; import fr.ifremer.adagio.core.dao.technical.synchronization.SynchronizationStatus; @@ -52,7 +47,6 @@ import javax.annotation.Resource; import java.util.Collection; -import java.util.Map; import java.util.Set; @Service("batchPersistenceService") @@ -69,40 +63,20 @@ @Autowired protected AttachmentPersistenceService attachmentPersistenceService; - @Resource(name = "catchBatchDao") - protected CatchBatchExtendDao catchBatchDao; - - @Resource(name = "scientificCruiseCatchBatchValidator") - protected CatchBatchValidator catchBatchValidator; - @Resource(name = "batchPersistenceHelper") protected BatchPersistenceHelper batchHelper; - @Override - public void init() { - super.init(); - catchBatchDao.registerCatchBatchValidator(catchBatchValidator); - } - - @Override - public void close() { - catchBatchDao.unregisterCatchBatchValidator(catchBatchValidator); - super.close(); - } - //------------------------------------------------------------------------// //-- CatchBatch methods --// //------------------------------------------------------------------------// @Override - public CatchBatch getCatchBatchFromFishingOperation(String fishingOperationId) throws CatchBatchValidationException { + public CatchBatch getCatchBatchFromFishingOperation(String fishingOperationId) { Preconditions.checkNotNull(fishingOperationId); - Integer catchBatchId = catchBatchDao.getIdByFishingOperationId(Integer.valueOf(fishingOperationId)); - Preconditions.checkNotNull(catchBatchId); - // whenever want to repair anything from Tutti - fr.ifremer.adagio.core.dao.data.batch.CatchBatch source = catchBatchDao.loadFullTree(catchBatchId, true, false); + fr.ifremer.adagio.core.dao.data.batch.CatchBatch source = + batchHelper.getRootCatchBatchByFishingOperationId(fishingOperationId, true); CatchBatch result = new CatchBatchBean(); result.setId(source.getId()); @@ -113,176 +87,126 @@ } // Vrac - SortingBatch vracBatch = catchBatchDao.getSortingBatch( - source.getChildBatchs(), - BatchPersistenceHelper.BATCH_PMFM_ID, + SortingBatch vracBatch = batchHelper.getSortingBatch( + source, + "Vrac", enumeration.PMFM_ID_SORTED_UNSORTED, enumeration.QUALITATIVE_VRAC_ID); - if (log.isInfoEnabled()) { - log.info("Loaded CatchBatch Vrac: " + vracBatch.getId()); - } + result.setCatchTotalSortedCarousselWeight(vracBatch.getWeight()); result.setCatchTotalSortedTremisWeight(vracBatch.getWeightBeforeSampling()); { // Vrac > Species - SortingBatch speciesBatch = catchBatchDao.getSortingBatch( - vracBatch.getChildBatchs(), - BatchPersistenceHelper.BATCH_PMFM_ID, + SortingBatch vracSpeciesBatch = batchHelper.getSortingBatch( + vracBatch, + "Vrac > Species", enumeration.PMFM_ID_SORTING_TYPE, enumeration.QUALITATIVE_ID_SORTING_TYPE_SPECIES); - if (log.isInfoEnabled()) { - log.info("Loaded CatchBatch Vrac > Species: " + speciesBatch.getId()); - } + if (vracSpeciesBatch != null) { - result.setSpeciesTotalSortedWeight(speciesBatch.getWeight()); + result.setSpeciesTotalSortedWeight(vracSpeciesBatch.getWeight()); - { // Vrac > Species > Inert - SortingBatch inertBatch = catchBatchDao.getSortingBatch( - speciesBatch.getChildBatchs(), - BatchPersistenceHelper.BATCH_PMFM_ID, + SortingBatch inertBatch = batchHelper.getSortingBatch( + vracSpeciesBatch, + "Vrac > Species > Inert", enumeration.PMFM_ID_SORTING_TYPE_2, enumeration.QUALITATIVE_ID_SORTING_TYPE_2_INERT); - if (inertBatch != null) { - if (log.isInfoEnabled()) { - log.info("Loaded CatchBatch Vrac > Species > Inert: " + inertBatch.getId()); - } result.setSpeciesTotalInertWeight(inertBatch.getWeight()); } // Vrac > Species > Alive not itemized - SortingBatch livingNotItemizedBatch = catchBatchDao.getSortingBatch( - speciesBatch.getChildBatchs(), - BatchPersistenceHelper.BATCH_PMFM_ID, + SortingBatch livingNotItemizedBatch = batchHelper.getSortingBatch( + vracSpeciesBatch, + "Vrac > Species > Alive not itemized", enumeration.PMFM_ID_SORTING_TYPE_2, enumeration.QUALITATIVE_ID_SORTING_TYPE_2_ALIVE_NOT_ITEMIZED); if (livingNotItemizedBatch != null) { - if (log.isInfoEnabled()) { - log.info("Loaded CatchBatch Vrac > Species > Alive not itemized: " + livingNotItemizedBatch.getId()); - } result.setSpeciesTotalLivingNotItemizedWeight(livingNotItemizedBatch.getWeight()); } } // Vrac > Benthos - SortingBatch benthosBatch = catchBatchDao.getSortingBatch( - vracBatch.getChildBatchs(), - BatchPersistenceHelper.BATCH_PMFM_ID, + SortingBatch vracBenthosBatch = batchHelper.getSortingBatch( + vracBatch, + "Vrac > Benthos", enumeration.PMFM_ID_SORTING_TYPE, enumeration.QUALITATIVE_ID_SORTING_TYPE_BENTHOS); - if (log.isInfoEnabled()) { - log.info("Loaded CatchBatch Vrac > Benthos: " + benthosBatch.getId()); - } + if (vracBenthosBatch != null) { + result.setBenthosTotalSortedWeight(vracBenthosBatch.getWeight()); - result.setBenthosTotalSortedWeight(benthosBatch.getWeight()); - - { // Vrac > Benthos > Inert - SortingBatch inertBatch = catchBatchDao.getSortingBatch( - benthosBatch.getChildBatchs(), - BatchPersistenceHelper.BATCH_PMFM_ID, + SortingBatch inertBatch = batchHelper.getSortingBatch( + vracBenthosBatch, + "Vrac > Benthos > Inert", enumeration.PMFM_ID_SORTING_TYPE_2, enumeration.QUALITATIVE_ID_SORTING_TYPE_2_INERT); if (inertBatch != null) { - if (log.isInfoEnabled()) { - log.info("Loaded CatchBatch Vrac > Benthos > Inert: " + inertBatch.getId()); - } result.setBenthosTotalInertWeight(inertBatch.getWeight()); } // Vrac > Benthos > Alive no itemized - SortingBatch livingNotItemizedBatch = catchBatchDao.getSortingBatch( - benthosBatch.getChildBatchs(), - BatchPersistenceHelper.BATCH_PMFM_ID, + SortingBatch livingNotItemizedBatch = batchHelper.getSortingBatch( + vracBenthosBatch, + "Vrac > Benthos > Alive not itemized", enumeration.PMFM_ID_SORTING_TYPE_2, enumeration.QUALITATIVE_ID_SORTING_TYPE_2_ALIVE_NOT_ITEMIZED); if (livingNotItemizedBatch != null) { - if (log.isInfoEnabled()) { - log.info("Loaded CatchBatch Vrac > Benthos > Alive not itemized: " + livingNotItemizedBatch.getId()); - } result.setBenthosTotalLivingNotItemizedWeight(livingNotItemizedBatch.getWeight()); } } - - // TODO : Plancton... } // Hors Vrac - SortingBatch horsVracBatch = catchBatchDao.getSortingBatch( - source.getChildBatchs(), - BatchPersistenceHelper.BATCH_PMFM_ID, + SortingBatch horsVracBatch = batchHelper.getSortingBatch( + source, + "Hors Vrac", enumeration.PMFM_ID_SORTED_UNSORTED, enumeration.QUALITATIVE_HORS_VRAC_ID); + if (horsVracBatch != null) { - if (log.isInfoEnabled()) { - log.info("Loaded CatchBatch Hors Vrac: " + horsVracBatch.getId()); - } - { - // Hors Vrac > Species - SortingBatch speciesBatch = catchBatchDao.getSortingBatch( - horsVracBatch.getChildBatchs(), - BatchPersistenceHelper.BATCH_PMFM_ID, - enumeration.PMFM_ID_SORTING_TYPE, - enumeration.QUALITATIVE_ID_SORTING_TYPE_SPECIES); - if (speciesBatch != null) { - if (log.isInfoEnabled()) { - log.info("Loaded CatchBatch Hors Vrac > Species: " + speciesBatch.getId()); - } - } + // Hors Vrac > Species + batchHelper.getSortingBatch( + horsVracBatch, + "Hors Vrac > Species", + enumeration.PMFM_ID_SORTING_TYPE, + enumeration.QUALITATIVE_ID_SORTING_TYPE_SPECIES); - // Hors Vrac > Benthos - SortingBatch benthosBatch = catchBatchDao.getSortingBatch( - horsVracBatch.getChildBatchs(), - BatchPersistenceHelper.BATCH_PMFM_ID, - enumeration.PMFM_ID_SORTING_TYPE, - enumeration.QUALITATIVE_ID_SORTING_TYPE_BENTHOS); + // Hors Vrac > Benthos + batchHelper.getSortingBatch( + horsVracBatch, + "Hors Vrac > Benthos", + enumeration.PMFM_ID_SORTING_TYPE, + enumeration.QUALITATIVE_ID_SORTING_TYPE_BENTHOS); + // Hors Vrac > MarineLitter + SortingBatch marineLitterBatch = batchHelper.getSortingBatch( + horsVracBatch, + "Hors Vrac > MarineLitter", + enumeration.PMFM_ID_SORTING_TYPE, + enumeration.QUALITATIVE_ID_SORTING_TYPE_MARINE_LITTER); - if (benthosBatch != null) { - if (log.isInfoEnabled()) { - log.info("Loaded CatchBatch Hors Vrac > Benthos: " + benthosBatch.getId()); - } - } - - // Hors Vrac > MarineLitter - SortingBatch marineLitterBatch = catchBatchDao.getSortingBatch( - horsVracBatch.getChildBatchs(), - BatchPersistenceHelper.BATCH_PMFM_ID, - enumeration.PMFM_ID_SORTING_TYPE, - enumeration.QUALITATIVE_ID_SORTING_TYPE_MARINE_LITTER); - - - if (marineLitterBatch != null) { - if (log.isInfoEnabled()) { - log.info("Loaded CatchBatch Hors Vrac > MarineLitter: " + marineLitterBatch.getId()); - } - - result.setMarineLitterTotalWeight(marineLitterBatch.getWeight()); - } - - // TODO : Plancton... + if (marineLitterBatch != null) { + result.setMarineLitterTotalWeight(marineLitterBatch.getWeight()); } } // Non trié - SortingBatch unsortedBatch = catchBatchDao.getSortingBatch( - source.getChildBatchs(), - BatchPersistenceHelper.BATCH_PMFM_ID, + SortingBatch unsortedBatch = batchHelper.getSortingBatch( + source, + "Unsorted", enumeration.PMFM_ID_SORTED_UNSORTED, enumeration.QUALITATIVE_UNSORTED_ID); if (unsortedBatch != null) { - if (log.isInfoEnabled()) { - log.info("Loaded CatchBatch Unsorted: " + unsortedBatch.getId()); - } result.setCatchTotalRejectedWeight(unsortedBatch.getWeight()); } @@ -298,8 +222,7 @@ fr.ifremer.adagio.core.dao.data.batch.CatchBatch catchBatch = fr.ifremer.adagio.core.dao.data.batch.CatchBatch.Factory.newInstance(); catchBatchToEntity(bean, catchBatch); - catchBatch = catchBatchDao.create(catchBatch); - bean.setId(catchBatch.getId()); + bean = batchHelper.createCatchBatch(bean, catchBatch); // Link to fishing operation getCurrentSession().flush(); @@ -324,13 +247,13 @@ getCurrentSession().enableFetchProfile("batch-with-childs"); getCurrentSession().setFlushMode(FlushMode.COMMIT); - fr.ifremer.adagio.core.dao.data.batch.CatchBatch catchBatch = catchBatchDao.load(bean.getIdAsInt()); + fr.ifremer.adagio.core.dao.data.batch.CatchBatch catchBatch = load(CatchBatchImpl.class, bean.getIdAsInt()); if (catchBatch == null) { throw new DataRetrievalFailureException("Could not retrieve catch batch with id=" + bean.getId()); } catchBatchToEntity(bean, catchBatch); - catchBatchDao.update(catchBatch); + batchHelper.update(catchBatch); getCurrentSession().flush(); return bean; @@ -346,7 +269,6 @@ Preconditions.checkNotNull(source.getFishingOperation().getId()); // Retrieve recorder department - Integer recorderDepartmentId = getRecorderDepartmentId(); // First initialization (when created) Integer fishingOperationId = source.getFishingOperation().getIdAsInt(); @@ -364,11 +286,11 @@ } // Total Weight - if (source.getCatchTotalWeight() == null) { - // Nothing to do : will be removed later, using notChangedQuantificationMeasurements - } else { - QuantificationMeasurement quantificationMeasurement = catchBatchDao.setQuantificationMeasurement(target, - enumeration.PMFM_ID_WEIGHT_MEASURED, recorderDepartmentId, source.getCatchTotalWeight(), true); + if (source.getCatchTotalWeight() != null) { + + QuantificationMeasurement quantificationMeasurement = batchHelper.setWeightMeasurementQuantificationMeasurement( + target, + source.getCatchTotalWeight()); notChangedQuantificationMeasurements.remove(quantificationMeasurement); } @@ -377,487 +299,110 @@ quantificationMeasurements.removeAll(notChangedQuantificationMeasurements); } - Map<Integer, SortingBatch> catchBatchChilds = getChildsMap( - target, enumeration.PMFM_ID_SORTED_UNSORTED); - - // ----------------------------------------------------------------------------- + // --------------------------------------------------------------------- // Vrac - // ----------------------------------------------------------------------------- - { - SortingBatch batch = catchBatchChilds.get(enumeration.QUALITATIVE_VRAC_ID); - if (batch == null) { - batch = SortingBatch.Factory.newInstance(); - target.getChildBatchs().add(batch); - } + // --------------------------------------------------------------------- + SortingBatch vracBatch = batchHelper.getOrCreateVracBatch( + target, + source.getCatchTotalSortedCarousselWeight(), + source.getCatchTotalSortedTremisWeight()); - beanToEntitySortingBatch(target, - target, - batch, - recorderDepartmentId, - enumeration.PMFM_ID_SORTED_UNSORTED, - enumeration.QUALITATIVE_VRAC_ID, - source.getCatchTotalSortedCarousselWeight(), - source.getCatchTotalSortedTremisWeight()); - batch.setRankOrder((short) 1); + // --------------------------------------------------------------------- + // Vrac / Species + // --------------------------------------------------------------------- - { - if (batch.getChildBatchs() == null) { - batch.setChildBatchs(Sets.<Batch>newHashSet()); - } + SortingBatch speciesBatch = batchHelper.getOrCreateSpeciesVracRootBatch( + target, + vracBatch, + source.getSpeciesTotalSortedWeight()); - Map<Integer, SortingBatch> batchChilds = getChildsMap(batch, enumeration.PMFM_ID_SORTING_TYPE); + // --------------------------------------------------------------------- + // Vrac / Species / Alive not itemized + // --------------------------------------------------------------------- + batchHelper.getOrCreateSpeciesVracAliveNotItemizeRootBatch( + target, + speciesBatch, + source.getSpeciesTotalLivingNotItemizedWeight()); - // ----------------------------------------------------------------------------- - // Vrac / Species - // ----------------------------------------------------------------------------- + // --------------------------------------------------------------------- + // Vrac / Species / Inert (not alive) + // --------------------------------------------------------------------- + batchHelper.getOrCreateSpeciesVracInertRootBatch( + target, + speciesBatch, + source.getSpeciesTotalInertWeight()); - SortingBatch speciesBatch = batchChilds.get(enumeration.QUALITATIVE_ID_SORTING_TYPE_SPECIES); + // --------------------------------------------------------------------- + // Vrac / Species / Alive itemized + // --------------------------------------------------------------------- + batchHelper.getOrCreateSpeciesVracAliveItemizeRootBatch( + target, + speciesBatch); - if (speciesBatch == null) { - speciesBatch = SortingBatch.Factory.newInstance(); - batch.getChildBatchs().add(speciesBatch); - } - beanToEntitySortingBatch(target, - batch, - speciesBatch, - recorderDepartmentId, - enumeration.PMFM_ID_SORTING_TYPE, - enumeration.QUALITATIVE_ID_SORTING_TYPE_SPECIES, - source.getSpeciesTotalSortedWeight(), - null); - speciesBatch.setRankOrder((short) 1); + // --------------------------------------------------------------------- + // Vrac > Benthos + // --------------------------------------------------------------------- + SortingBatch benthosBatch = batchHelper.getOrCreateBenthosVracRootBatch( + target, + vracBatch, + source.getBenthosTotalSortedWeight()); - { - if (speciesBatch.getChildBatchs() == null) { - speciesBatch.setChildBatchs(Sets.<Batch>newHashSet()); - } + // --------------------------------------------------------------------- + // Vrac / Benthos / Alive not itemized + // --------------------------------------------------------------------- + batchHelper.getOrCreateBenthosVracAliveNotItemizeRootBatch( + target, + benthosBatch, + source.getBenthosTotalLivingNotItemizedWeight()); - Map<Integer, SortingBatch> speciesBatchChilds = getChildsMap(speciesBatch, enumeration.PMFM_ID_SORTING_TYPE_2); + // --------------------------------------------------------------------- + // Vrac / Benthos / Inert (not alive) + // --------------------------------------------------------------------- + batchHelper.getOrCreateBenthosVracInertRootBatch( + target, + benthosBatch, + source.getBenthosTotalInertWeight()); - // ----------------------------------------------------------------------------- - // Vrac / Species / Alive not itemized - // ----------------------------------------------------------------------------- + // --------------------------------------------------------------------- + // Vrac / Benthos / Alive itemized + // --------------------------------------------------------------------- + batchHelper.getOrCreateBenthosVracAliveItemizeRootBatch( + target, + benthosBatch); - SortingBatch aliveNotItemizedBatch = speciesBatchChilds.get(enumeration.QUALITATIVE_ID_SORTING_TYPE_2_ALIVE_NOT_ITEMIZED); - if (aliveNotItemizedBatch == null) { - aliveNotItemizedBatch = SortingBatch.Factory.newInstance(); - speciesBatch.getChildBatchs().add(aliveNotItemizedBatch); - } - - beanToEntitySortingBatch(target, - speciesBatch, - aliveNotItemizedBatch, - recorderDepartmentId, - enumeration.PMFM_ID_SORTING_TYPE_2, - enumeration.QUALITATIVE_ID_SORTING_TYPE_2_ALIVE_NOT_ITEMIZED, - source.getSpeciesTotalLivingNotItemizedWeight(), - null); - aliveNotItemizedBatch.setRankOrder((short) 1); - - // ----------------------------------------------------------------------------- - // Vrac / Species / Inert (not alive) - // ----------------------------------------------------------------------------- - - SortingBatch inertBatch = speciesBatchChilds.get(enumeration.QUALITATIVE_ID_SORTING_TYPE_2_INERT); - if (inertBatch == null) { - inertBatch = SortingBatch.Factory.newInstance(); - speciesBatch.getChildBatchs().add(inertBatch); - } - - beanToEntitySortingBatch(target, - speciesBatch, - inertBatch, - recorderDepartmentId, - enumeration.PMFM_ID_SORTING_TYPE_2, - enumeration.QUALITATIVE_ID_SORTING_TYPE_2_INERT, - source.getSpeciesTotalInertWeight(), - null); - inertBatch.setRankOrder((short) 2); - - // ----------------------------------------------------------------------------- - // Vrac / Species / Alive itemized - // ----------------------------------------------------------------------------- - - SortingBatch aliveItemizedBatch = speciesBatchChilds.get(enumeration.QUALITATIVE_ID_SORTING_TYPE_2_ALIVE_ITEMIZED); - if (aliveItemizedBatch == null) { - aliveItemizedBatch = SortingBatch.Factory.newInstance(); - speciesBatch.getChildBatchs().add(aliveItemizedBatch); - } - - beanToEntitySortingBatch(target, - speciesBatch, - aliveItemizedBatch, - recorderDepartmentId, - enumeration.PMFM_ID_SORTING_TYPE_2, - enumeration.QUALITATIVE_ID_SORTING_TYPE_2_ALIVE_ITEMIZED, - null, - null); - aliveItemizedBatch.setRankOrder((short) 3); - } - - // ----------------------------------------------------------------------------- - // Vrac > Benthos - // ----------------------------------------------------------------------------- - - SortingBatch benthosBatch = batchChilds.get(enumeration.QUALITATIVE_ID_SORTING_TYPE_BENTHOS); - - if (benthosBatch == null) { - benthosBatch = SortingBatch.Factory.newInstance(); - batch.getChildBatchs().add(benthosBatch); - } - beanToEntitySortingBatch(target, - batch, - benthosBatch, - recorderDepartmentId, - enumeration.PMFM_ID_SORTING_TYPE, - enumeration.QUALITATIVE_ID_SORTING_TYPE_BENTHOS, - source.getBenthosTotalSortedWeight(), - null); - benthosBatch.setRankOrder((short) 2); - - { - - if (benthosBatch.getChildBatchs() == null) { - benthosBatch.setChildBatchs(Sets.<Batch>newHashSet()); - } - - Map<Integer, SortingBatch> benthosBatchChilds = getChildsMap(benthosBatch, enumeration.PMFM_ID_SORTING_TYPE_2); - - // ----------------------------------------------------------------------------- - // Vrac / Benthos / Alive not itemized - // ----------------------------------------------------------------------------- - - SortingBatch aliveNotItemizedBatch = benthosBatchChilds.get(enumeration.QUALITATIVE_ID_SORTING_TYPE_2_ALIVE_NOT_ITEMIZED); - if (aliveNotItemizedBatch == null) { - aliveNotItemizedBatch = SortingBatch.Factory.newInstance(); - benthosBatch.getChildBatchs().add(aliveNotItemizedBatch); - } - - beanToEntitySortingBatch(target, - benthosBatch, - aliveNotItemizedBatch, - recorderDepartmentId, - enumeration.PMFM_ID_SORTING_TYPE_2, - enumeration.QUALITATIVE_ID_SORTING_TYPE_2_ALIVE_NOT_ITEMIZED, - source.getBenthosTotalLivingNotItemizedWeight(), - null); - aliveNotItemizedBatch.setRankOrder((short) 1); - - // ----------------------------------------------------------------------------- - // Vrac / Benthos / Inert (not alive) - // ----------------------------------------------------------------------------- - - SortingBatch inertBatch = benthosBatchChilds.get(enumeration.QUALITATIVE_ID_SORTING_TYPE_2_INERT); - if (inertBatch == null) { - inertBatch = SortingBatch.Factory.newInstance(); - benthosBatch.getChildBatchs().add(inertBatch); - } - - beanToEntitySortingBatch(target, - benthosBatch, - inertBatch, - recorderDepartmentId, - enumeration.PMFM_ID_SORTING_TYPE_2, - enumeration.QUALITATIVE_ID_SORTING_TYPE_2_INERT, - source.getBenthosTotalInertWeight(), - null); - inertBatch.setRankOrder((short) 2); - - // ----------------------------------------------------------------------------- - // Vrac / Benthos / Alive itemized - // ----------------------------------------------------------------------------- - - SortingBatch aliveItemizedBatch = benthosBatchChilds.get(enumeration.QUALITATIVE_ID_SORTING_TYPE_2_ALIVE_ITEMIZED); - if (aliveItemizedBatch == null) { - aliveItemizedBatch = SortingBatch.Factory.newInstance(); - benthosBatch.getChildBatchs().add(aliveItemizedBatch); - } - - beanToEntitySortingBatch(target, - benthosBatch, - aliveItemizedBatch, - recorderDepartmentId, - enumeration.PMFM_ID_SORTING_TYPE_2, - enumeration.QUALITATIVE_ID_SORTING_TYPE_2_ALIVE_ITEMIZED, - null, - null); - aliveItemizedBatch.setRankOrder((short) 3); - } - - // TODO plancton - } - } - - // ----------------------------------------------------------------------------- + // --------------------------------------------------------------------- // Hors Vrac - // ----------------------------------------------------------------------------- - { - SortingBatch batch = catchBatchChilds.get(enumeration.QUALITATIVE_HORS_VRAC_ID); - if (batch == null) { - batch = SortingBatch.Factory.newInstance(); - target.getChildBatchs().add(batch); - } - beanToEntitySortingBatch(target, - target, - batch, - recorderDepartmentId, - enumeration.PMFM_ID_SORTED_UNSORTED, - enumeration.QUALITATIVE_HORS_VRAC_ID, - null, - null); - batch.setRankOrder((short) 2); + // --------------------------------------------------------------------- + SortingBatch horsVracBatch = batchHelper.getOrCreateHorsVracBatch(target); - { - Map<Integer, SortingBatch> batchChilds = getChildsMap(batch, enumeration.PMFM_ID_SORTING_TYPE); - if (batch.getChildBatchs() == null) { - batch.setChildBatchs(Sets.<Batch>newHashSet()); - } - // ----------------------------------------------------------------------------- - // Hors Vrac > Species - // ----------------------------------------------------------------------------- - { - SortingBatch speciesBatch = batchChilds.get(enumeration.QUALITATIVE_ID_SORTING_TYPE_SPECIES); - if (speciesBatch == null) { - speciesBatch = SortingBatch.Factory.newInstance(); - batch.getChildBatchs().add(speciesBatch); - } - beanToEntitySortingBatch(target, - batch, - speciesBatch, - recorderDepartmentId, - enumeration.PMFM_ID_SORTING_TYPE, - enumeration.QUALITATIVE_ID_SORTING_TYPE_SPECIES, - null, - null); - speciesBatch.setRankOrder((short) 1); - } + // --------------------------------------------------------------------- + // Hors Vrac > Species + // --------------------------------------------------------------------- + batchHelper.getOrCreateSpeciesHorsVracRootBatch( + target, + horsVracBatch); - // ----------------------------------------------------------------------------- - // Hors Vrac > Benthos - // ----------------------------------------------------------------------------- - { - SortingBatch benthosBatch = batchChilds.get(enumeration.QUALITATIVE_ID_SORTING_TYPE_BENTHOS); - if (benthosBatch == null) { - benthosBatch = SortingBatch.Factory.newInstance(); - batch.getChildBatchs().add(benthosBatch); - } - beanToEntitySortingBatch(target, - batch, - benthosBatch, - recorderDepartmentId, - enumeration.PMFM_ID_SORTING_TYPE, - enumeration.QUALITATIVE_ID_SORTING_TYPE_BENTHOS, - null, - null); - benthosBatch.setRankOrder((short) 2); - } + // --------------------------------------------------------------------- + // Hors Vrac > Benthos + // --------------------------------------------------------------------- + batchHelper.getOrCreateBenthosHorsVracRootBatch( + target, + horsVracBatch); - // ----------------------------------------------------------------------------- - // Hors Vrac > MarineLitter - // ----------------------------------------------------------------------------- - { - SortingBatch marineLitterBatch = batchChilds.get(enumeration.QUALITATIVE_ID_SORTING_TYPE_MARINE_LITTER); - if (marineLitterBatch == null) { - marineLitterBatch = SortingBatch.Factory.newInstance(); - batch.getChildBatchs().add(marineLitterBatch); - } - beanToEntitySortingBatch(target, - batch, - marineLitterBatch, - recorderDepartmentId, - enumeration.PMFM_ID_SORTING_TYPE, - enumeration.QUALITATIVE_ID_SORTING_TYPE_MARINE_LITTER, - source.getMarineLitterTotalWeight(), - null); - marineLitterBatch.setRankOrder((short) 3); - } + // --------------------------------------------------------------------- + // Hors Vrac > MarineLitter + // --------------------------------------------------------------------- + batchHelper.getOrCreateMarineLitterRootBatch( + target, + horsVracBatch, + source.getMarineLitterTotalWeight()); - // TODO : plancton - } - - } - - // ----------------------------------------------------------------------------- + // --------------------------------------------------------------------- // Unsorted (=rejected) - // ----------------------------------------------------------------------------- - // Unsorted : - SortingBatch unsortedBatch = catchBatchChilds.get(enumeration.QUALITATIVE_UNSORTED_ID); - if (unsortedBatch == null) { - unsortedBatch = SortingBatch.Factory.newInstance(); - target.getChildBatchs().add(unsortedBatch); - } - beanToEntitySortingBatch(target, - target, - unsortedBatch, - recorderDepartmentId, - enumeration.PMFM_ID_SORTED_UNSORTED, - enumeration.QUALITATIVE_UNSORTED_ID, - source.getCatchTotalRejectedWeight(), - null); - unsortedBatch.setRankOrder((short) 3); - } + // --------------------------------------------------------------------- + batchHelper.getOrCreateRejectedBatch( + target, source.getCatchTotalRejectedWeight()); - protected Map<Integer, SortingBatch> getChildsMap(Batch parentBatch, Integer pmfmId) { - Map<Integer, SortingBatch> batchByQualitativeValueId = Maps.newHashMap(); - if (parentBatch.getChildBatchs() == null) { - return batchByQualitativeValueId; - } - for (Batch childBatch : parentBatch.getChildBatchs()) { - SortingBatch childSortingBatch = (SortingBatch) childBatch; - SortingMeasurement sm = catchBatchDao.getSortingMeasurement((SortingBatch) childBatch, pmfmId, null, false); - if (sm != null && sm.getQualitativeValue() != null && sm.getQualitativeValue().getId() != null) { - batchByQualitativeValueId.put(sm.getQualitativeValue().getId(), childSortingBatch); - } - } - return batchByQualitativeValueId; } - protected void beanToEntitySortingBatch( - fr.ifremer.adagio.core.dao.data.batch.CatchBatch rootBatch, - Batch parentBatch, - SortingBatch target, - Integer recorderDepartmentId, - Integer sortingPmfmId, - Integer sortingQualitativeValueId, - Float weight, - Float weightBeforeSampling) { - - Collection<QuantificationMeasurement> quantificationMeasurements = target.getQuantificationMeasurements(); - Collection<SortingMeasurement> sortingMeasurements = target.getSortingMeasurements(); - - // Create lists to store all updates, then remove not updated items - Set<QuantificationMeasurement> notChangedQuantificationMeasurements = Sets.newHashSet(); - if (quantificationMeasurements != null) { - notChangedQuantificationMeasurements.addAll(quantificationMeasurements); - } - Set<SortingMeasurement> notChangedSortingMeasurements = Sets.newHashSet(); - if (sortingMeasurements != null) { - notChangedSortingMeasurements.addAll(sortingMeasurements); - } - - // Some mandatory properties : - QualityFlagImpl qualityFlag = load(QualityFlagImpl.class, enumeration.QUALITY_FLAG_CODE_NOT_QUALIFIED); - target.setQualityFlag(qualityFlag); - target.setRootBatch(rootBatch); - target.setParentBatch(parentBatch); - target.setExhaustiveInventory(true); - - // No taxon or taxon group - target.setReferenceTaxon(null); - target.setTaxonGroup(null); - - // Sorting measurement - if (sortingPmfmId == null || sortingQualitativeValueId == null) { - // Nothing to do : will be removed later, using notChangedSortingMeasurements - } else { - SortingMeasurement sm = batchHelper.setSortingMeasurement(target, recorderDepartmentId, sortingPmfmId, sortingQualitativeValueId); - notChangedSortingMeasurements.remove(sm); - } - - // Sampling Ratio - if (weightBeforeSampling == null || weight == null) { - target.setSamplingRatio(null); - target.setSamplingRatioText(null); - } else { - String samplingRatioText = weight + "/" + weightBeforeSampling; - samplingRatioText = samplingRatioText.replaceAll(",", "."); - target.setSamplingRatioText(samplingRatioText); - target.setSamplingRatio(weight / weightBeforeSampling); - } - - // Weight - if (weightBeforeSampling == null && weight == null) { - // Nothing to do : will be removed later, using notChangedQuantificationMeasurements - } else { - Float batchReferenceWeight = weight; - if (batchReferenceWeight == null) { - batchReferenceWeight = weightBeforeSampling; - } - QuantificationMeasurement quantificationMeasurement = catchBatchDao.setQuantificationMeasurement( - target, - enumeration.PMFM_ID_WEIGHT_MEASURED, - recorderDepartmentId, - batchReferenceWeight, - true); - notChangedQuantificationMeasurements.remove(quantificationMeasurement); - } - - // Removed not changed measurements (in sorting and quantification measurement lists) - if (quantificationMeasurements != null) { - quantificationMeasurements.removeAll(notChangedQuantificationMeasurements); - } - if (sortingMeasurements != null) { - sortingMeasurements.removeAll(notChangedSortingMeasurements); - } - } - -// protected void beanToEntityReferenceTaxonBatch( -// fr.ifremer.adagio.core.dao.data.batch.CatchBatch rootBatch, -// Batch parentBatch, -// SortingBatch target, -// Integer recorderDepartmentId, -// Integer referenceTaxonId, -// Float weight, -// Float sampleWeight) { -// -// Collection<QuantificationMeasurement> quantificationMeasurements = target.getQuantificationMeasurements(); -// Collection<SortingMeasurement> sortingMeasurements = target.getSortingMeasurements(); -// -// // Create lists to store all updates, then remove not updated items -// Set<QuantificationMeasurement> notChangedQuantificationMeasurements = Sets.newHashSet(); -// if (quantificationMeasurements != null) { -// notChangedQuantificationMeasurements.addAll(quantificationMeasurements); -// } -// Set<SortingMeasurement> notChangedSortingMeasurements = Sets.newHashSet(); -// if (sortingMeasurements != null) { -// notChangedSortingMeasurements.addAll(sortingMeasurements); -// } -// -// // Some mandatory properties : -// target.setQualityFlag(load(QualityFlagImpl.class, enumeration.QUALITY_FLAG_CODE_NOT_QUALIFIED)); -// target.setRootBatch(rootBatch); -// target.setParentBatch(parentBatch); -// target.setExhaustiveInventory(true); -// -// // Reference taxon -// target.setReferenceTaxon(load(ReferenceTaxonImpl.class, referenceTaxonId)); -// target.setTaxonGroup(null); -// -// // Sampling Ratio -// if ((sampleWeight == null || weight == null)) { -// target.setSamplingRatio(null); -// target.setSamplingRatioText(null); -// } else { -// String samplingRatioText = weight + "/" + sampleWeight; -// samplingRatioText = samplingRatioText.replaceAll(",", "."); -// target.setSamplingRatioText(samplingRatioText); -// target.setSamplingRatio(weight / sampleWeight); -// } -// -// // Weight -// if ((sampleWeight == null && weight == null)) { -// // Nothing to do : will be removed later, using notChangedQuantificationMeasurements -// } else { -// Float batchReferenceWeight = weight; -// if (batchReferenceWeight == null) { -// batchReferenceWeight = sampleWeight; -// } -// QuantificationMeasurement quantificationMeasurement = catchBatchDao.setQuantificationMeasurement(target, -// enumeration.PMFM_ID_WEIGHT_MEASURED, recorderDepartmentId, batchReferenceWeight, true); -// notChangedQuantificationMeasurements.remove(quantificationMeasurement); -// } -// -// // Removed not changed measurements (in sorting and quantification measurement lists) -// if (quantificationMeasurements != null) { -// quantificationMeasurements.removeAll(notChangedQuantificationMeasurements); -// } -// if (sortingMeasurements != null) { -// sortingMeasurements.removeAll(notChangedSortingMeasurements); -// } -// } - - protected Integer getRecorderDepartmentId() { - // TODO BL : voir si on peut récupérer le departement (du 1er saisisseur ?) - return enumeration.DEPARTMENT_ID_UNKNOWN_RECORDER_DEPARTMENT; - } } Modified: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/IndividualObservationBatchPersistenceServiceImpl.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/IndividualObservationBatchPersistenceServiceImpl.java 2013-05-01 13:21:10 UTC (rev 905) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/IndividualObservationBatchPersistenceServiceImpl.java 2013-05-01 13:30:06 UTC (rev 906) @@ -30,9 +30,7 @@ import fr.ifremer.adagio.core.dao.administration.user.DepartmentImpl; import fr.ifremer.adagio.core.dao.administration.user.PersonImpl; import fr.ifremer.adagio.core.dao.data.batch.CatchBatch; -import fr.ifremer.adagio.core.dao.data.batch.CatchBatchExtendDao; import fr.ifremer.adagio.core.dao.data.batch.SortingBatch; -import fr.ifremer.adagio.core.dao.data.operation.FishingOperationDao; import fr.ifremer.adagio.core.dao.data.operation.FishingOperationImpl; import fr.ifremer.adagio.core.dao.data.sample.Sample; import fr.ifremer.adagio.core.dao.data.sample.SampleDao; @@ -83,12 +81,9 @@ @Autowired protected SampleMeasurementPersistenceHelper sampleMeasurementPersistenceHelper; - @Resource(name = "fishingOperationDao") - protected FishingOperationDao fishingOperationDao; + @Resource(name = "batchPersistenceHelper") + protected BatchPersistenceHelper batchHelper; - @Resource(name = "catchBatchDao") - protected CatchBatchExtendDao catchBatchDao; - @Resource(name = "sampleDao") protected SampleDao sampleDao; @@ -323,20 +318,17 @@ } protected SortingBatch getBatch(Integer operationId) { + Preconditions.checkNotNull(operationId); - // Load batch tree - Integer catchBatchId = catchBatchDao.getIdByFishingOperationId(operationId); - Preconditions.checkNotNull(catchBatchId); + CatchBatch catchBatch = batchHelper.getRootCatchBatchByFishingOperationId(operationId.toString(), false); - CatchBatch catchBatch = catchBatchDao.loadFullTree(catchBatchId); - Preconditions.checkNotNull(catchBatch); - // Vrac / Species - SortingBatch vracSpeciesBatch = catchBatchDao.getSortingBatch( - catchBatch.getChildBatchs(), - BatchPersistenceHelper.BATCH_PMFM_ID, enumeration.PMFM_ID_SORTED_UNSORTED, enumeration.QUALITATIVE_VRAC_ID, - BatchPersistenceHelper.BATCH_PMFM_ID, enumeration.PMFM_ID_SORTING_TYPE, enumeration.QUALITATIVE_ID_SORTING_TYPE_SPECIES, - BatchPersistenceHelper.BATCH_PMFM_ID, enumeration.PMFM_ID_SORTING_TYPE_2, enumeration.QUALITATIVE_ID_SORTING_TYPE_2_ALIVE_ITEMIZED + SortingBatch vracSpeciesBatch = batchHelper.getSortingBatch( + catchBatch, + "Vrac > Species > Alive Itemized", + enumeration.PMFM_ID_SORTED_UNSORTED, enumeration.QUALITATIVE_VRAC_ID, + enumeration.PMFM_ID_SORTING_TYPE, enumeration.QUALITATIVE_ID_SORTING_TYPE_SPECIES, + enumeration.PMFM_ID_SORTING_TYPE_2, enumeration.QUALITATIVE_ID_SORTING_TYPE_2_ALIVE_ITEMIZED ); Preconditions.checkNotNull(vracSpeciesBatch); return vracSpeciesBatch; Modified: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/MarineLitterBatchPersistenceServiceImpl.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/MarineLitterBatchPersistenceServiceImpl.java 2013-05-01 13:21:10 UTC (rev 905) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/MarineLitterBatchPersistenceServiceImpl.java 2013-05-01 13:30:06 UTC (rev 906) @@ -28,13 +28,11 @@ import com.google.common.collect.Sets; import fr.ifremer.adagio.core.dao.data.batch.Batch; import fr.ifremer.adagio.core.dao.data.batch.CatchBatch; -import fr.ifremer.adagio.core.dao.data.batch.CatchBatchExtendDao; import fr.ifremer.adagio.core.dao.data.batch.SortingBatch; import fr.ifremer.adagio.core.dao.data.measure.QuantificationMeasurement; import fr.ifremer.adagio.core.dao.data.measure.SortingMeasurement; import fr.ifremer.adagio.core.dao.referential.QualityFlagImpl; import fr.ifremer.adagio.core.dao.referential.pmfm.QualitativeValue; -import fr.ifremer.adagio.core.dao.referential.pmfm.QualitativeValueImpl; import fr.ifremer.tutti.persistence.entities.TuttiBeanFactory; import fr.ifremer.tutti.persistence.entities.TuttiEntities; import fr.ifremer.tutti.persistence.entities.data.AttachementObjectTypeEnum; @@ -45,9 +43,7 @@ import fr.ifremer.tutti.persistence.service.batch.BatchPersistenceHelper; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.hibernate.FlushMode; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.dao.DataIntegrityViolationException; import org.springframework.stereotype.Service; import javax.annotation.Resource; @@ -72,9 +68,6 @@ @Autowired protected AttachmentPersistenceService attachmentPersistenceService; - @Resource(name = "catchBatchDao") - protected CatchBatchExtendDao catchBatchDao; - @Resource(name = "batchPersistenceHelper") protected BatchPersistenceHelper batchHelper; @@ -103,32 +96,29 @@ public BatchContainer<MarineLitterBatch> getRootMarineLitterBatch(String fishingOperationId) { Preconditions.checkNotNull(fishingOperationId); - // Load batch tree - Integer catchBatchId = catchBatchDao.getIdByFishingOperationId(Integer.valueOf(fishingOperationId)); - Preconditions.checkNotNull(catchBatchId); + CatchBatch catchBatch = batchHelper.getRootCatchBatchByFishingOperationId(fishingOperationId, false); - CatchBatch catchBatch = catchBatchDao.loadFullTree(catchBatchId); - Preconditions.checkNotNull(catchBatch); - // Get marine litter root batch - SortingBatch vracMarineLitterBatch = getMarineLitterParentBatch(catchBatch); + SortingBatch sortingBatch = getMarineLitterParentBatch(catchBatch); BatchContainer<MarineLitterBatch> result = new BatchContainer<MarineLitterBatch>(); - result.setId(vracMarineLitterBatch.getId()); + if (sortingBatch != null) { + result.setId(sortingBatch.getId()); - for (Batch batch1 : vracMarineLitterBatch.getChildBatchs()) { - SortingBatch source = (SortingBatch) batch1; + for (Batch batch1 : sortingBatch.getChildBatchs()) { + SortingBatch source = (SortingBatch) batch1; - MarineLitterBatch target = TuttiBeanFactory.newMarineLitterBatch(); - entityToMarineLitterBatch(source, target); - result.addChildren(target); + MarineLitterBatch target = TuttiBeanFactory.newMarineLitterBatch(); + entityToMarineLitterBatch(source, target); + result.addChildren(target); - if (log.isInfoEnabled()) { - log.info("Loaded CatchBatch MarineLetter > " + - target.getMarineLitterCategory().getName() + " / " + - target.getMarineLitterSizeCategory().getName() + " : " + - target.getId()); + if (log.isInfoEnabled()) { + log.info("Loaded CatchBatch Hors Vrac > MarineLetter > " + + target.getMarineLitterCategory().getName() + " / " + + target.getMarineLitterSizeCategory().getName() + ": " + + target.getId()); + } } } @@ -144,22 +134,12 @@ Preconditions.checkNotNull(bean.getMarineLitterCategory()); Preconditions.checkNotNull(bean.getMarineLitterSizeCategory()); Preconditions.checkNotNull(bean.getNumber()); -// Preconditions.checkNotNull(bean.getWeight()); - getCurrentSession().setFlushMode(FlushMode.COMMIT); - - // Load full batch tree - Integer catchBatchId = catchBatchDao.getIdByFishingOperationId(bean.getFishingOperation().getIdAsInt()); - CatchBatch catchBatch = catchBatchDao.loadFullTree(catchBatchId); - + CatchBatch catchBatch = batchHelper.getRootCatchBatchByFishingOperationId(bean.getFishingOperation().getId(), false); SortingBatch batch = SortingBatch.Factory.newInstance(); marineLitterBatchToEntity(bean, batch, catchBatch); - batch = catchBatchDao.createSortingBatch(batch, catchBatch); + bean = batchHelper.createSortingBatch(bean, catchBatch, batch); - bean.setId(batch.getId()); - - getCurrentSession().flush(); - return bean; } @@ -167,20 +147,11 @@ public MarineLitterBatch saveMarineLitterBatch(MarineLitterBatch bean) { Preconditions.checkNotNull(bean); Preconditions.checkNotNull(bean.getId()); - - // Load batch tree - Integer catchBatchId = catchBatchDao.getIdBySortingBatchId(bean.getIdAsInt()); - Preconditions.checkNotNull(catchBatchId); - - CatchBatch catchBatch = catchBatchDao.loadFullTree(catchBatchId); - Preconditions.checkNotNull(catchBatch); - - getCurrentSession().setFlushMode(FlushMode.COMMIT); - - SortingBatch batch = catchBatchDao.getSortingBatchById(catchBatch, bean.getIdAsInt()); + Integer batchId = bean.getIdAsInt(); + CatchBatch catchBatch = batchHelper.getRootCatchBatchByBatchId(batchId); + SortingBatch batch = batchHelper.getSortingBatchById(catchBatch, batchId); marineLitterBatchToEntity(bean, batch, catchBatch); - catchBatchDao.updateSortingBatch(batch, catchBatch); - getCurrentSession().flush(); + batchHelper.updateSortingBatch(batch, catchBatch); return bean; } @@ -189,16 +160,11 @@ public void deleteMarineLitterBatch(String id) { Preconditions.checkNotNull(id); - getCurrentSession().setFlushMode(FlushMode.COMMIT); - Integer batchId = Integer.valueOf(id); - catchBatchDao.removeWithChildren(batchId); + batchHelper.removeWithChildren(batchId); attachmentPersistenceService.deleteAllAttachment( - AttachementObjectTypeEnum.BATCH, - batchId); - - getCurrentSession().flush(); + AttachementObjectTypeEnum.BATCH, batchId); } public MarineLitterBatch entityToMarineLitterBatch(SortingBatch source, @@ -241,9 +207,6 @@ Preconditions.checkNotNull(source.getFishingOperation()); Preconditions.checkNotNull(source.getFishingOperation().getId()); - // Retrieve recorder department - Integer recorderDepartmentId = batchHelper.getRecorderDepartmentId(); - Collection<QuantificationMeasurement> quantificationMeasurements = target.getQuantificationMeasurements(); Collection<SortingMeasurement> sortingMeasurements = target.getSortingMeasurements(); @@ -277,41 +240,33 @@ target.setSubgroupCount(1f); // Weight - if (source.getWeight() == null) { - // Nothing to do : will be removed later, using notChangedSortingMeasurements - } else { - QuantificationMeasurement quantificationMeasurement = catchBatchDao.setQuantificationMeasurement( + if (source.getWeight() != null) { + + QuantificationMeasurement quantificationMeasurement = batchHelper.setWeightMeasurementQuantificationMeasurement( target, - enumeration.PMFM_ID_WEIGHT_MEASURED, - recorderDepartmentId, - source.getWeight(), - true); + source.getWeight()); notChangedQuantificationMeasurements.remove(quantificationMeasurement); } // MarineLitterCategory - if (source.getMarineLitterCategory() == null) { - // Nothing to do : will be removed later, using notChangedQuantificationMeasurements - } else { - SortingMeasurement measurement = catchBatchDao.getSortingMeasurement( + if (source.getMarineLitterCategory() != null) { + + SortingMeasurement measurement = batchHelper.setSortingMeasurement( target, marineLitterCategory, - recorderDepartmentId, - true); - measurement.setQualitativeValue(load(QualitativeValueImpl.class, source.getMarineLitterCategory().getIdAsInt())); + source.getMarineLitterCategory().getIdAsInt() + ); notChangedSortingMeasurements.remove(measurement); } // MarineLitterSizeCategory - if (source.getMarineLitterSizeCategory() == null) { - // Nothing to do : will be removed later, using notChangedQuantificationMeasurements - } else { - SortingMeasurement measurement = catchBatchDao.getSortingMeasurement( + if (source.getMarineLitterSizeCategory() != null) { + + SortingMeasurement measurement = batchHelper.setSortingMeasurement( target, marineLitterSizeCategory, - recorderDepartmentId, - true); - measurement.setQualitativeValue(load(QualitativeValueImpl.class, source.getMarineLitterSizeCategory().getIdAsInt())); + source.getMarineLitterSizeCategory().getIdAsInt() + ); notChangedSortingMeasurements.remove(measurement); } @@ -345,28 +300,31 @@ Preconditions.checkNotNull(target); - // Load existing parent and root SortingBatch parentBatch = getMarineLitterParentBatch(catchBatch); - // Parent Batch - if (parentBatch == null) { - throw new DataIntegrityViolationException( - "Could not retrieve parent batch, for a given marineLitterBatch : invalid batch tree structure. Please make sure CatchBatch has been saved before to create a MarineLitterBatch."); + + SortingBatch horsVracBatch = batchHelper.getOrCreateHorsVracBatch( + catchBatch + ); + + parentBatch = batchHelper.getOrCreateMarineLitterRootBatch( + catchBatch, + horsVracBatch, + null + ); } - // Parent Batch target.setParentBatch(parentBatch); target.setRootBatch(catchBatch); } protected SortingBatch getMarineLitterParentBatch(CatchBatch catchBatch) { - SortingBatch parentBatch = catchBatchDao.getSortingBatch( - catchBatch.getChildBatchs(), - BatchPersistenceHelper.BATCH_PMFM_ID, // hors vrac + SortingBatch parentBatch = batchHelper.getSortingBatch( + catchBatch, + "Hors Vrac > Marine Litter", enumeration.PMFM_ID_SORTED_UNSORTED, enumeration.QUALITATIVE_HORS_VRAC_ID, - BatchPersistenceHelper.BATCH_PMFM_ID, // marine litter enumeration.PMFM_ID_SORTING_TYPE, enumeration.QUALITATIVE_ID_SORTING_TYPE_MARINE_LITTER); return parentBatch; Modified: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/SpeciesBatchPersistenceServiceImpl.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/SpeciesBatchPersistenceServiceImpl.java 2013-05-01 13:21:10 UTC (rev 905) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/SpeciesBatchPersistenceServiceImpl.java 2013-05-01 13:30:06 UTC (rev 906) @@ -29,7 +29,6 @@ import com.google.common.collect.Sets; import fr.ifremer.adagio.core.dao.data.batch.Batch; import fr.ifremer.adagio.core.dao.data.batch.CatchBatch; -import fr.ifremer.adagio.core.dao.data.batch.CatchBatchExtendDao; import fr.ifremer.adagio.core.dao.data.batch.SortingBatch; import fr.ifremer.adagio.core.dao.data.measure.QuantificationMeasurement; import fr.ifremer.adagio.core.dao.data.measure.SortingMeasurement; @@ -49,7 +48,6 @@ import org.apache.commons.collections.CollectionUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.hibernate.FlushMode; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataIntegrityViolationException; import org.springframework.stereotype.Service; @@ -81,9 +79,6 @@ @Autowired protected ReferentialPersistenceService referentialService; - @Resource(name = "catchBatchDao") - protected CatchBatchExtendDao catchBatchDao; - @Resource(name = "batchPersistenceHelper") protected BatchPersistenceHelper batchHelper; @@ -95,20 +90,15 @@ public BatchContainer<SpeciesBatch> getRootSpeciesBatch(String fishingOperationId) { Preconditions.checkNotNull(fishingOperationId); - // Load batch tree - Integer catchBatchId = catchBatchDao.getIdByFishingOperationId(Integer.valueOf(fishingOperationId)); - Preconditions.checkNotNull(catchBatchId); + CatchBatch catchBatch = batchHelper.getRootCatchBatchByFishingOperationId(fishingOperationId, false); - CatchBatch catchBatch = catchBatchDao.loadFullTree(catchBatchId); - Preconditions.checkNotNull(catchBatch); - - Collection<Batch> catchBatchChilds = catchBatch.getChildBatchs(); // Vrac / Species - SortingBatch vracSpeciesBatch = catchBatchDao.getSortingBatch( - catchBatchChilds, - BatchPersistenceHelper.BATCH_PMFM_ID, enumeration.PMFM_ID_SORTED_UNSORTED, enumeration.QUALITATIVE_VRAC_ID, - BatchPersistenceHelper.BATCH_PMFM_ID, enumeration.PMFM_ID_SORTING_TYPE, enumeration.QUALITATIVE_ID_SORTING_TYPE_SPECIES, - BatchPersistenceHelper.BATCH_PMFM_ID, enumeration.PMFM_ID_SORTING_TYPE_2, enumeration.QUALITATIVE_ID_SORTING_TYPE_2_ALIVE_ITEMIZED + SortingBatch vracSpeciesBatch = batchHelper.getSortingBatch( + catchBatch, + "Vrac > Species > Alive Itemized", + enumeration.PMFM_ID_SORTED_UNSORTED, enumeration.QUALITATIVE_VRAC_ID, + enumeration.PMFM_ID_SORTING_TYPE, enumeration.QUALITATIVE_ID_SORTING_TYPE_SPECIES, + enumeration.PMFM_ID_SORTING_TYPE_2, enumeration.QUALITATIVE_ID_SORTING_TYPE_2_ALIVE_ITEMIZED ); // container of speciesBatch is arbitraty put on vrac type (there is @@ -128,15 +118,16 @@ result.addChildren(target); if (log.isInfoEnabled()) { - log.info("Loaded CatchBatch Vrac > Species > " + target.getSpecies().getReferenceTaxonId() + " : " + target.getId()); + log.info("Loaded CatchBatch Vrac > Species > Alive Itemized > " + target.getSpecies().getReferenceTaxonId() + ": " + target.getId()); } } // Hors-Vrac / Species - SortingBatch horsVracSpeciesBatch = catchBatchDao.getSortingBatch( - catchBatchChilds, - BatchPersistenceHelper.BATCH_PMFM_ID, enumeration.PMFM_ID_SORTED_UNSORTED, enumeration.QUALITATIVE_HORS_VRAC_ID, - BatchPersistenceHelper.BATCH_PMFM_ID, enumeration.PMFM_ID_SORTING_TYPE, enumeration.QUALITATIVE_ID_SORTING_TYPE_SPECIES); + SortingBatch horsVracSpeciesBatch = batchHelper.getSortingBatch( + catchBatch, + "Hors Vrac > Species", + enumeration.PMFM_ID_SORTED_UNSORTED, enumeration.QUALITATIVE_HORS_VRAC_ID, + enumeration.PMFM_ID_SORTING_TYPE, enumeration.QUALITATIVE_ID_SORTING_TYPE_SPECIES); if (horsVracSpeciesBatch != null) { for (Batch batch : horsVracSpeciesBatch.getChildBatchs()) { SortingBatch source = (SortingBatch) batch; @@ -149,7 +140,7 @@ entityToSpeciesBatch(source, target); result.addChildren(target); if (log.isInfoEnabled()) { - log.info("Loaded CatchBatch Hors Vrac > Species > " + target.getSpecies().getReferenceTaxonId() + " : " + target.getId()); + log.info("Loaded CatchBatch Hors Vrac > Species > " + target.getSpecies().getReferenceTaxonId() + ": " + target.getId()); } } } @@ -167,20 +158,11 @@ Preconditions.checkNotNull(bean.getFishingOperation()); Preconditions.checkNotNull(bean.getFishingOperation().getId()); - getCurrentSession().setFlushMode(FlushMode.COMMIT); - - // Load full batch tree - Integer catchBatchId = catchBatchDao.getIdByFishingOperationId(bean.getFishingOperation().getIdAsInt()); - CatchBatch catchBatch = catchBatchDao.loadFullTree(catchBatchId); - + CatchBatch catchBatch = batchHelper.getRootCatchBatchByFishingOperationId(bean.getFishingOperation().getId(), false); SortingBatch batch = SortingBatch.Factory.newInstance(); speciesBatchToEntity(bean, batch, parentBatchId, catchBatch); - batch = catchBatchDao.createSortingBatch(batch, catchBatch); + bean = batchHelper.createSortingBatch(bean, catchBatch, batch); - bean.setId(batch.getId()); - - getCurrentSession().flush(); - return bean; } @@ -189,23 +171,16 @@ Preconditions.checkNotNull(bean); Preconditions.checkNotNull(bean.getId()); - // Load batch tree - Integer catchBatchId = catchBatchDao.getIdBySortingBatchId(bean.getIdAsInt()); - Preconditions.checkNotNull(catchBatchId); + Integer batchId = bean.getIdAsInt(); - CatchBatch catchBatch = catchBatchDao.loadFullTree(catchBatchId); - Preconditions.checkNotNull(catchBatch); - - getCurrentSession().setFlushMode(FlushMode.COMMIT); - - SortingBatch batch = catchBatchDao.getSortingBatchById(catchBatch, bean.getIdAsInt()); + CatchBatch catchBatch = batchHelper.getRootCatchBatchByBatchId(batchId); + SortingBatch batch = batchHelper.getSortingBatchById(catchBatch, batchId); String parentBatchId = null; if (bean.getParentBatch() != null) { parentBatchId = bean.getParentBatch().getId(); } speciesBatchToEntity(bean, batch, parentBatchId, catchBatch); - catchBatchDao.updateSortingBatch(batch, catchBatch); - getCurrentSession().flush(); + batchHelper.updateSortingBatch(batch, catchBatch); return bean; } @@ -214,38 +189,22 @@ public void deleteSpeciesBatch(String id) { Preconditions.checkNotNull(id); - getCurrentSession().setFlushMode(FlushMode.COMMIT); - Integer batchId = Integer.valueOf(id); - catchBatchDao.removeWithChildren(batchId); + batchHelper.removeWithChildren(batchId); attachmentPersistenceService.deleteAllAttachment( AttachementObjectTypeEnum.BATCH, batchId); - - getCurrentSession().flush(); } @Override public void deleteSpeciesSubBatch(String id) { Preconditions.checkNotNull(id); - getCurrentSession().setFlushMode(FlushMode.COMMIT); - Integer speciesBatchId = Integer.valueOf(id); - // get catch batch - Integer catchBatchId = catchBatchDao.getIdBySortingBatchId(speciesBatchId); - - Preconditions.checkNotNull( - catchBatchId, - "Could not find catchBatch for speciesBatch: " + speciesBatchId); - - CatchBatch catchBatch = - catchBatchDao.loadFullTree(catchBatchId); - - // get species batch - SortingBatch sortingBatch = catchBatchDao.getSortingBatchById( + CatchBatch catchBatch = batchHelper.getRootCatchBatchByBatchId(speciesBatchId); + SortingBatch sortingBatch = batchHelper.getSortingBatchById( catchBatch, speciesBatchId); // get his children @@ -261,7 +220,7 @@ if (log.isInfoEnabled()) { log.info("Delete child [" + childBatchId + "] of species batch: " + id); } - catchBatchDao.removeWithChildren(childBatchId); + batchHelper.removeWithChildren(childBatchId); // delete his attachment (if any) attachmentPersistenceService.deleteAllAttachment( @@ -269,9 +228,6 @@ childBatchId); } } - - - getCurrentSession().flush(); } @Override @@ -281,7 +237,7 @@ Preconditions.checkNotNull(species); Preconditions.checkNotNull(species.getReferenceTaxonId()); - catchBatchDao.setSortingBatchReferenceTaxon( + batchHelper.setSortingBatchReferenceTaxon( batchId, species.getReferenceTaxonId()); } @@ -295,7 +251,8 @@ String speciesBatchId) { Preconditions.checkNotNull(speciesBatchId); - List<SortingBatch> frequencyChilds = batchHelper.getFrequencies(speciesBatchId); + List<SortingBatch> frequencyChilds = + batchHelper.getFrequencies(speciesBatchId); List<SpeciesBatchFrequency> results = Lists.newArrayList(); for (SortingBatch child : frequencyChilds) { SpeciesBatchFrequency target = @@ -331,18 +288,15 @@ } } - getCurrentSession().setFlushMode(FlushMode.COMMIT); - Integer sortingBatchId = Integer.valueOf(speciesBatchId); - Integer catchBatchId = catchBatchDao.getIdBySortingBatchId(sortingBatchId); - CatchBatch catchBatch = catchBatchDao.loadFullTree(catchBatchId); + CatchBatch catchBatch = batchHelper.getRootCatchBatchByBatchId(sortingBatchId); if (catchBatch == null) { return notNullFrequencies; } // Retrieve parent - SortingBatch parentBatch = catchBatchDao.getSortingBatchById(catchBatch, sortingBatchId); + SortingBatch parentBatch = batchHelper.getSortingBatchById(catchBatch, sortingBatchId); // Remember child ids, to remove unchanged item (see at bottom in this method) List<Integer> notUpdatedChildIds = Lists.newArrayList(); @@ -366,7 +320,7 @@ speciesBatchFrequencyToEntity(source, target, parentBatch, rankOrder); // Create the targeted batch, then update the source id - catchBatchDao.createSortingBatch(target, catchBatch); + batchHelper.createSortingBatch(source, catchBatch, target); // push back id of created sortingBatch source.setId(target.getId()); @@ -377,7 +331,7 @@ } else { // Existing batch - target = catchBatchDao.loadSortingBatch(source.getIdAsInt(), catchBatch); + target = batchHelper.loadSortingBatch(source.getIdAsInt(), catchBatch); // Fill the sorting batch from the source speciesBatchFrequencyToEntity(source, target, parentBatch, rankOrder); @@ -397,7 +351,7 @@ if (batchsToUpdate.size() > 0) { // update some batchs - catchBatchDao.updateSortingBatch(batchsToUpdate, catchBatch); + batchHelper.updateSortingBatch(batchsToUpdate, catchBatch); } if (notUpdatedChildIds.size() > 0) { @@ -408,12 +362,10 @@ if (log.isInfoEnabled()) { log.info("Remove obsolete frequency sortingBatch: " + batchId); } - catchBatchDao.removeWithChildren(batchId, catchBatch); + batchHelper.removeWithChildren(batchId, catchBatch); } } - getCurrentSession().flush(); - return Collections.unmodifiableList(notNullFrequencies); } @@ -447,7 +399,7 @@ if (source.getSortingMeasurements().size() == 1) { sm = source.getSortingMeasurements().iterator().next(); } else if (source.getReferenceTaxon() != null && source.getReferenceTaxon().getId() != null) { - sm = catchBatchDao.getInheritedSortingMeasurement(source, enumeration.PMFM_ID_SORTED_UNSORTED); + sm = batchHelper.getInheritedSortingMeasurement(source); } if (sm != null) { SampleCategoryEnum sampleCategory = enumeration.getSampleCategoryByPmfmId(sm.getPmfm().getId()); @@ -514,15 +466,11 @@ protected void speciesBatchToEntity(SpeciesBatch source, SortingBatch target, String parentBatchId, - fr.ifremer.adagio.core.dao.data.batch.CatchBatch catchBatch - ) { + fr.ifremer.adagio.core.dao.data.batch.CatchBatch catchBatch ) { Preconditions.checkNotNull(source.getFishingOperation()); Preconditions.checkNotNull(source.getFishingOperation().getId()); - // Retrieve recorder department - Integer recorderDepartmentId = batchHelper.getRecorderDepartmentId(); - Collection<QuantificationMeasurement> quantificationMeasurements = target.getQuantificationMeasurements(); Collection<SortingMeasurement> sortingMeasurements = target.getSortingMeasurements(); @@ -570,12 +518,14 @@ if (source.getWeight() == null && source.getSampleCategoryWeight() == null) { // Nothing to do : will be removed later, using notChangedSortingMeasurements } else if (source.getSampleCategoryWeight() != null && source.getWeight() == null) { - QuantificationMeasurement quantificationMeasurement = catchBatchDao.setQuantificationMeasurement(target, - enumeration.PMFM_ID_WEIGHT_MEASURED, recorderDepartmentId, source.getSampleCategoryWeight(), true); + QuantificationMeasurement quantificationMeasurement = batchHelper.setWeightMeasurementQuantificationMeasurement( + target, + source.getSampleCategoryWeight()); notChangedQuantificationMeasurements.remove(quantificationMeasurement); } else if (source.getWeight() != null && source.getSampleCategoryWeight() == null) { - QuantificationMeasurement quantificationMeasurement = catchBatchDao.setQuantificationMeasurement(target, - enumeration.PMFM_ID_WEIGHT_MEASURED, recorderDepartmentId, source.getWeight(), true); + QuantificationMeasurement quantificationMeasurement = batchHelper.setWeightMeasurementQuantificationMeasurement( + target, + source.getWeight()); notChangedQuantificationMeasurements.remove(quantificationMeasurement); } @@ -588,20 +538,21 @@ samplingRatioText = samplingRatioText.replaceAll(",", "."); target.setSamplingRatioText(samplingRatioText); target.setSamplingRatio(source.getWeight() / source.getSampleCategoryWeight()); - QuantificationMeasurement quantificationMeasurement = catchBatchDao.setQuantificationMeasurement(target, - enumeration.PMFM_ID_WEIGHT_MEASURED, recorderDepartmentId, source.getWeight(), true); + QuantificationMeasurement quantificationMeasurement = batchHelper.setWeightMeasurementQuantificationMeasurement( + target, + source.getWeight()); notChangedQuantificationMeasurements.remove(quantificationMeasurement); } // Sorting measurement - if ((source.getSampleCategoryType() == null || source.getSampleCategoryValue() == null)) { - // Nothing to do : will be removed later, using notChangedSortingMeasurements - } else { + if (source.getSampleCategoryType() != null && source.getSampleCategoryValue() != null) { Integer pmfmId = source.getSampleCategoryType().getFieldValue(); // Do not store sorting measurement if pmfm = SORTED (already store in an ancestor batch) if (!pmfmId.equals(enumeration.PMFM_ID_SORTED_UNSORTED)) { - SortingMeasurement sortingMeasurement = batchHelper.setSortingMeasurement(target, recorderDepartmentId, - source.getSampleCategoryType(), source.getSampleCategoryValue()); + SortingMeasurement sortingMeasurement = batchHelper.setSortingMeasurement( + target, + source.getSampleCategoryType(), + source.getSampleCategoryValue()); notChangedSortingMeasurements.remove(sortingMeasurement); } } @@ -649,9 +600,6 @@ Preconditions.checkNotNull(source.getBatch()); Preconditions.checkNotNull(source.getBatch().getId()); - // Retrieve recorder department - Integer recorderDepartmentId = batchHelper.getRecorderDepartmentId(); - Collection<QuantificationMeasurement> quantificationMeasurements = target.getQuantificationMeasurements(); Collection<SortingMeasurement> sortingMeasurements = target.getSortingMeasurements(); @@ -678,21 +626,17 @@ target.setRankOrder(rankOrder); // Weight or SampleCategoryWeight - if (source.getWeight() == null) { - // Nothing to do : will be removed later, using notChangedSortingMeasurements - } else { - QuantificationMeasurement quantificationMeasurement = catchBatchDao.setQuantificationMeasurement( + if (source.getWeight() != null) { + QuantificationMeasurement quantificationMeasurement = batchHelper.setWeightMeasurementQuantificationMeasurement( target, - enumeration.PMFM_ID_WEIGHT_MEASURED, recorderDepartmentId, source.getWeight(), true); + source.getWeight()); notChangedQuantificationMeasurements.remove(quantificationMeasurement); } // Sorting measurement - if ((source.getLengthStepCaracteristic() == null || source.getLengthStep() == null)) { - // Nothing to do : will be removed later, using notChangedSortingMeasurements - } else { + if ((source.getLengthStepCaracteristic() != null && source.getLengthStep() != null)) { Integer pmfmId = source.getLengthStepCaracteristic().getIdAsInt(); - SortingMeasurement sortingMeasurement = batchHelper.setSortingMeasurement(target, recorderDepartmentId, pmfmId, + SortingMeasurement sortingMeasurement = batchHelper.setSortingMeasurement(target, pmfmId, source.getLengthStep()); notChangedSortingMeasurements.remove(sortingMeasurement); } Modified: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/batch/BatchPersistenceHelper.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/batch/BatchPersistenceHelper.java 2013-05-01 13:21:10 UTC (rev 905) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/batch/BatchPersistenceHelper.java 2013-05-01 13:30:06 UTC (rev 906) @@ -26,12 +26,19 @@ import com.google.common.base.Preconditions; import com.google.common.collect.Lists; +import com.google.common.collect.Sets; import fr.ifremer.adagio.core.dao.data.batch.Batch; import fr.ifremer.adagio.core.dao.data.batch.CatchBatch; import fr.ifremer.adagio.core.dao.data.batch.CatchBatchExtendDao; import fr.ifremer.adagio.core.dao.data.batch.SortingBatch; +import fr.ifremer.adagio.core.dao.data.batch.validator.CatchBatchValidationException; +import fr.ifremer.adagio.core.dao.data.batch.validator.CatchBatchValidator; +import fr.ifremer.adagio.core.dao.data.measure.QuantificationMeasurement; import fr.ifremer.adagio.core.dao.data.measure.SortingMeasurement; +import fr.ifremer.adagio.core.dao.referential.QualityFlagImpl; import fr.ifremer.adagio.core.dao.referential.pmfm.Pmfm; +import fr.ifremer.tutti.persistence.InvalidBatchModelException; +import fr.ifremer.tutti.persistence.entities.TuttiEntity; import fr.ifremer.tutti.persistence.entities.data.SampleCategoryEnum; import fr.ifremer.tutti.persistence.entities.referential.Caracteristic; import fr.ifremer.tutti.persistence.entities.referential.CaracteristicQualitativeValue; @@ -39,6 +46,8 @@ import fr.ifremer.tutti.persistence.service.AbstractPersistenceService; import fr.ifremer.tutti.persistence.service.MeasurementPersistenceHelper; import fr.ifremer.tutti.persistence.service.ReferentialPersistenceService; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataIntegrityViolationException; import org.springframework.stereotype.Component; @@ -46,7 +55,9 @@ import javax.annotation.Resource; import java.io.Serializable; import java.text.MessageFormat; +import java.util.Collection; import java.util.List; +import java.util.Set; /** * Helper around batches. @@ -57,9 +68,9 @@ @Component("batchPersistenceHelper") public class BatchPersistenceHelper extends AbstractPersistenceService { -// /** Logger. */ -// private static final Log log = -// LogFactory.getLog(BatchPersistenceHelper.class); + /** Logger. */ + private static final Log log = + LogFactory.getLog(BatchPersistenceHelper.class); public static final String BATCH_PMFM_ID = "pmfmId"; @@ -69,9 +80,76 @@ @Resource(name = "catchBatchDao") protected CatchBatchExtendDao catchBatchDao; + @Resource(name = "scientificCruiseCatchBatchValidator") + protected CatchBatchValidator catchBatchValidator; + @Autowired protected MeasurementPersistenceHelper measurementPersistenceHelper; + @Override + public void init() { + super.init(); + catchBatchDao.registerCatchBatchValidator(catchBatchValidator); + } + + @Override + public void close() { + catchBatchDao.unregisterCatchBatchValidator(catchBatchValidator); + super.close(); + } + + public fr.ifremer.tutti.persistence.entities.data.CatchBatch createCatchBatch(fr.ifremer.tutti.persistence.entities.data.CatchBatch bean, CatchBatch catchBatch) { + catchBatch = catchBatchDao.create(catchBatch); + bean.setId(catchBatch.getId()); + return bean; + } + + public <D extends TuttiEntity> D createSortingBatch(D bean, CatchBatch catchBatch, SortingBatch batch) { + batch = catchBatchDao.createSortingBatch(batch, catchBatch); + bean.setId(batch.getId()); + return bean; + } + + public void removeWithChildren(Integer batchId) { + catchBatchDao.removeWithChildren(batchId); + } + + public void removeWithChildren(Integer batchId, CatchBatch parentCatchBatch) { + catchBatchDao.removeWithChildren(batchId, parentCatchBatch); + } + + public void updateSortingBatch(List<SortingBatch> sortingBatchs, CatchBatch parentCatchBatch) { + catchBatchDao.updateSortingBatch(sortingBatchs, parentCatchBatch); + } + + public SortingBatch loadSortingBatch(Integer sortingBatchId, CatchBatch parentCatchBatch) { + return catchBatchDao.loadSortingBatch(sortingBatchId, parentCatchBatch); + } + + public void update(CatchBatch catchBatch) { + catchBatchDao.update(catchBatch); + } + + public void setSortingBatchReferenceTaxon(String batchId, Integer referenceTaxonId) { + catchBatchDao.setSortingBatchReferenceTaxon(batchId, referenceTaxonId); + } + + public SortingBatch getSortingBatchById(CatchBatch catchBatch, Integer sortingBatchId) { + return catchBatchDao.getSortingBatchById(catchBatch, sortingBatchId); + } + + public void updateSortingBatch(SortingBatch sortingBatch, CatchBatch parentCatchBatch) { + catchBatchDao.updateSortingBatch(sortingBatch, parentCatchBatch); + } + + public SortingMeasurement getInheritedSortingMeasurement(SortingBatch sortingBatch) { + return catchBatchDao.getInheritedSortingMeasurement(sortingBatch, enumeration.PMFM_ID_SORTED_UNSORTED); + } + + public QuantificationMeasurement setWeightMeasurementQuantificationMeasurement(Batch batch, Float weightValue) { + return catchBatchDao.setQuantificationMeasurement(batch, enumeration.PMFM_ID_WEIGHT_MEASURED, getRecorderDepartmentId(), weightValue, true); + } + public List<SortingBatch> getFrequencyChilds(SortingBatch sortingBatch) { List<SortingBatch> result = Lists.newArrayList(); @@ -87,13 +165,7 @@ public List<SortingBatch> getFrequencies(String batchId) { Preconditions.checkNotNull(batchId); Integer sortingBatchId = Integer.valueOf(batchId); - Integer catchBatchId = catchBatchDao.getIdBySortingBatchId(sortingBatchId); - Preconditions.checkNotNull( - catchBatchId, - "Could not find catchBatch for batch: " + batchId); - - CatchBatch catchBatch = catchBatchDao.loadFullTree(catchBatchId); - + CatchBatch catchBatch = getRootCatchBatchByBatchId(sortingBatchId); SortingBatch sortingBatch = catchBatchDao.getSortingBatchById( catchBatch, sortingBatchId); @@ -101,33 +173,45 @@ return frequencyChilds; } - /** - * Check if the given {@code sortingBatch} is a frequency one. - * <p/> - * We test that: - * <ul> - * <li>batch has exactly one measurement</li> - * <li>the measurement pmfm is not a sample category</li> - * </ul> - * - * @param sortingBatch batch to check - * @return {@code true} if given batch is a frequency batch, - * {@code false} otherwise. - */ - protected boolean isFrequencyBatch(SortingBatch sortingBatch) { - boolean result = false; - if (sortingBatch.getSortingMeasurements().size() == 1) { - SortingMeasurement sm - = sortingBatch.getSortingMeasurements().iterator().next(); - Pmfm pmfm = sm.getPmfm(); - SampleCategoryEnum sampleCategoryByPmfmId = - enumeration.getSampleCategoryByPmfmId(pmfm.getId()); - result = sampleCategoryByPmfmId == null; + public fr.ifremer.adagio.core.dao.data.batch.CatchBatch getRootCatchBatchByFishingOperationId(String fishingOperationId, boolean validate) { + Preconditions.checkNotNull(fishingOperationId); + Integer catchBatchId = catchBatchDao.getIdByFishingOperationId(Integer.valueOf(fishingOperationId)); + Preconditions.checkNotNull(catchBatchId); + + // whenever want to repair anything from Tutti + fr.ifremer.adagio.core.dao.data.batch.CatchBatch result; + + if (validate) { + + try { + result = catchBatchDao.loadFullTree(catchBatchId, true, false); + } catch (CatchBatchValidationException e) { + throw new InvalidBatchModelException( + "L'arbre d'échantillonage n'est pas compatible avec celui de Tutti.", e); + } + } else { + result = catchBatchDao.loadFullTree(catchBatchId); } + Preconditions.checkNotNull(result); return result; } + public fr.ifremer.adagio.core.dao.data.batch.CatchBatch getRootCatchBatchByBatchId(Integer batchId) { + Preconditions.checkNotNull(batchId); + + Integer catchBatchId = catchBatchDao.getIdBySortingBatchId(batchId); + Preconditions.checkNotNull(catchBatchId); + + // whenever want to repair anything from Tutti + fr.ifremer.adagio.core.dao.data.batch.CatchBatch result; + + result = catchBatchDao.loadFullTree(catchBatchId); + + Preconditions.checkNotNull(result); + return result; + } + public void setBatchParents(SampleCategoryEnum sampleCategoryType, Serializable sampleCategoryValue, SortingBatch target, @@ -158,17 +242,21 @@ if (enumeration.QUALITATIVE_VRAC_ID.equals(qualitativeValueId)) { // vrac - parentBatch = catchBatchDao.getSortingBatch(catchBatch.getChildBatchs(), - BatchPersistenceHelper.BATCH_PMFM_ID, enumeration.PMFM_ID_SORTED_UNSORTED, enumeration.QUALITATIVE_VRAC_ID, // vrac - BatchPersistenceHelper.BATCH_PMFM_ID, enumeration.PMFM_ID_SORTING_TYPE, batchPmfmId, // Species | Benthos - BatchPersistenceHelper.BATCH_PMFM_ID, enumeration.PMFM_ID_SORTING_TYPE_2, enumeration.QUALITATIVE_ID_SORTING_TYPE_2_ALIVE_ITEMIZED // vivant trié + parentBatch = getSortingBatch( + catchBatch, + "Vrac > (Species | Benthos) > Alive Itemized", + enumeration.PMFM_ID_SORTED_UNSORTED, enumeration.QUALITATIVE_VRAC_ID, // vrac + enumeration.PMFM_ID_SORTING_TYPE, batchPmfmId, // Species | Benthos + enumeration.PMFM_ID_SORTING_TYPE_2, enumeration.QUALITATIVE_ID_SORTING_TYPE_2_ALIVE_ITEMIZED // vivant trié ); } else if (enumeration.QUALITATIVE_HORS_VRAC_ID.equals(qualitativeValueId)) { // hors-vrac - parentBatch = catchBatchDao.getSortingBatch(catchBatch.getChildBatchs(), - BatchPersistenceHelper.BATCH_PMFM_ID, enumeration.PMFM_ID_SORTED_UNSORTED, enumeration.QUALITATIVE_HORS_VRAC_ID, // hors vrac - BatchPersistenceHelper.BATCH_PMFM_ID, enumeration.PMFM_ID_SORTING_TYPE, batchPmfmId // Species | Benthos + parentBatch = getSortingBatch( + catchBatch, + "Hors vrac > (Species|Benthos)", + enumeration.PMFM_ID_SORTED_UNSORTED, enumeration.QUALITATIVE_HORS_VRAC_ID, // hors vrac + enumeration.PMFM_ID_SORTING_TYPE, batchPmfmId // Species | Benthos ); } else { @@ -201,37 +289,213 @@ return qualitativeValueId; } - public Integer getRecorderDepartmentId() { - // TODO BL : voir si on peut récupérer le departement (du 1er saisisseur ?) - return enumeration.DEPARTMENT_ID_UNKNOWN_RECORDER_DEPARTMENT; + public SortingBatch getSortingBatch(Batch source, + String debugMessage, + Integer... ids) { + + return getSortingBatch(source.getChildBatchs(), debugMessage, ids); } -// public 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) { -// Integer qvId; -// if (value instanceof CaracteristicQualitativeValue) { -// qvId = ((CaracteristicQualitativeValue) value).getIdAsInt(); -// } else if (value instanceof Integer) { -// qvId = (Integer) value; -// } -// // TODO BL : not used ? => to remove -// else { -// qvId = Integer.valueOf(value.toString()); -// } -// QualitativeValue qv = load(QualitativeValueImpl.class, qvId); -// measurement.setQualitativeValue(qv); -// } -// } + public SortingBatch getSortingBatch(Collection<Batch> childs, + String debugMessage, + Integer... ids) { + int nbParams = ids.length / 2; + + Object[] params = new Object[nbParams * 3]; + for (int i = 0; i < nbParams; i++) { + Integer sortingPmfmId = ids[2 * i]; + Integer sortingQualitativeValueId = ids[2 * i + 1]; + params[3 * i] = BatchPersistenceHelper.BATCH_PMFM_ID; + params[3 * i + 1] = sortingPmfmId; + params[3 * i + 2] = sortingQualitativeValueId; + } + SortingBatch result = catchBatchDao.getSortingBatch(childs, params); + if (result != null && debugMessage != null && log.isInfoEnabled()) { + log.info("Loaded " + debugMessage + ": " + result.getId()); + } + return result; + } + + public SortingBatch getOrCreateVracBatch(CatchBatch batch, + Float weight, + Float weightBeforeSampling) { + return getOrCreate( + batch, + batch, + enumeration.PMFM_ID_SORTED_UNSORTED, + enumeration.QUALITATIVE_VRAC_ID, + weight, + weightBeforeSampling, + (short) 1 + ); + } + + public SortingBatch getOrCreateSpeciesVracRootBatch(CatchBatch target, + SortingBatch batch, + Float totalWeight) { + return getOrCreate( + target, + batch, + enumeration.PMFM_ID_SORTING_TYPE, + enumeration.QUALITATIVE_ID_SORTING_TYPE_SPECIES, + totalWeight, + (short) 1 + ); + } + + public SortingBatch getOrCreateSpeciesVracAliveNotItemizeRootBatch(CatchBatch target, + SortingBatch batch, + Float totalWeight) { + return getOrCreate( + target, + batch, + enumeration.PMFM_ID_SORTING_TYPE_2, + enumeration.QUALITATIVE_ID_SORTING_TYPE_2_ALIVE_NOT_ITEMIZED, + totalWeight, + (short) 1 + ); + } + + public SortingBatch getOrCreateSpeciesVracInertRootBatch(CatchBatch target, + SortingBatch batch, + Float totalWeight) { + return getOrCreate( + target, + batch, + enumeration.PMFM_ID_SORTING_TYPE_2, + enumeration.QUALITATIVE_ID_SORTING_TYPE_2_INERT, + totalWeight, + (short) 2 + ); + } + + public SortingBatch getOrCreateSpeciesVracAliveItemizeRootBatch(CatchBatch target, + SortingBatch batch) { + return getOrCreate( + target, + batch, + enumeration.PMFM_ID_SORTING_TYPE_2, + enumeration.QUALITATIVE_ID_SORTING_TYPE_2_ALIVE_ITEMIZED, + null, + (short) 3 + ); + } + + public SortingBatch getOrCreateBenthosVracRootBatch(CatchBatch target, + SortingBatch batch, + Float totalWeight) { + return getOrCreate( + target, + batch, + enumeration.PMFM_ID_SORTING_TYPE, + enumeration.QUALITATIVE_ID_SORTING_TYPE_BENTHOS, + totalWeight, + (short) 2 + ); + } + + public SortingBatch getOrCreateBenthosVracAliveNotItemizeRootBatch(CatchBatch target, + SortingBatch batch, + Float totalWeight) { + return getOrCreate( + target, + batch, + enumeration.PMFM_ID_SORTING_TYPE_2, + enumeration.QUALITATIVE_ID_SORTING_TYPE_2_ALIVE_NOT_ITEMIZED, + totalWeight, + (short) 1 + ); + } + + public SortingBatch getOrCreateBenthosVracInertRootBatch(fr.ifremer.adagio.core.dao.data.batch.CatchBatch target, + SortingBatch batch, + Float totalWeight) { + return getOrCreate( + target, + batch, + enumeration.PMFM_ID_SORTING_TYPE_2, + enumeration.QUALITATIVE_ID_SORTING_TYPE_2_INERT, + totalWeight, + (short) 2 + ); + } + + public SortingBatch getOrCreateBenthosVracAliveItemizeRootBatch(CatchBatch target, + SortingBatch batch) { + return getOrCreate( + target, + batch, + enumeration.PMFM_ID_SORTING_TYPE_2, + enumeration.QUALITATIVE_ID_SORTING_TYPE_2_ALIVE_ITEMIZED, + null, + (short) 3 + ); + } + + public SortingBatch getOrCreateHorsVracBatch(CatchBatch batch) { + return getOrCreate( + batch, + batch, + enumeration.PMFM_ID_SORTED_UNSORTED, + enumeration.QUALITATIVE_HORS_VRAC_ID, + null, + (short) 2 + ); + } + + public SortingBatch getOrCreateSpeciesHorsVracRootBatch(CatchBatch target, + SortingBatch batch) { + return getOrCreate( + target, + batch, + enumeration.PMFM_ID_SORTING_TYPE, + enumeration.QUALITATIVE_ID_SORTING_TYPE_SPECIES, + null, + (short) 1 + ); + } + + public SortingBatch getOrCreateBenthosHorsVracRootBatch(CatchBatch target, + SortingBatch batch) { + return getOrCreate( + target, + batch, + enumeration.PMFM_ID_SORTING_TYPE, + enumeration.QUALITATIVE_ID_SORTING_TYPE_BENTHOS, + null, + (short) 2 + ); + } + + public SortingBatch getOrCreateMarineLitterRootBatch(CatchBatch target, + SortingBatch batch, + Float totalWeight) { + return getOrCreate( + target, + batch, + enumeration.PMFM_ID_SORTING_TYPE, + enumeration.QUALITATIVE_ID_SORTING_TYPE_MARINE_LITTER, + totalWeight, + (short) 3 + ); + } + + public SortingBatch getOrCreateRejectedBatch(CatchBatch batch, + Float weight) { + + return getOrCreate( + batch, + batch, + enumeration.PMFM_ID_SORTED_UNSORTED, + enumeration.QUALITATIVE_UNSORTED_ID, + weight, + (short) 3 + ); + } + public SortingMeasurement setSortingMeasurement( SortingBatch sortingBatch, - Integer recorderDepartmentId, SampleCategoryEnum sampleCategory, Serializable value) { Preconditions.checkNotNull(sampleCategory); @@ -241,14 +505,13 @@ Caracteristic caracteristic = referentialService.getCaracteristic(pmfmId); SortingMeasurement sortingMeasurement = catchBatchDao.getSortingMeasurement( - sortingBatch, pmfmId, recorderDepartmentId, true); + sortingBatch, pmfmId, getRecorderDepartmentId(), true); measurementPersistenceHelper.setMeasurement(sortingMeasurement, caracteristic, value); return sortingMeasurement; } public SortingMeasurement setSortingMeasurement( SortingBatch sortingBatch, - Integer recorderDepartmentId, Integer pmfmId, Serializable value) { Preconditions.checkNotNull(pmfmId); @@ -256,7 +519,7 @@ Caracteristic caracteristic = referentialService.getCaracteristic(pmfmId); SortingMeasurement sortingMeasurement = catchBatchDao.getSortingMeasurement( - sortingBatch, pmfmId, recorderDepartmentId, true); + sortingBatch, pmfmId, getRecorderDepartmentId(), true); measurementPersistenceHelper.setMeasurement(sortingMeasurement, caracteristic, value); @@ -289,4 +552,155 @@ return value; } + + //------------------------------------------------------------------------// + //-- Internal methods --// + //------------------------------------------------------------------------// + + protected Integer getRecorderDepartmentId() { + // TODO BL : voir si on peut récupérer le departement (du 1er saisisseur ?) + return enumeration.DEPARTMENT_ID_UNKNOWN_RECORDER_DEPARTMENT; + } + + /** + * Check if the given {@code sortingBatch} is a frequency one. + * <p/> + * We test that: + * <ul> + * <li>batch has exactly one measurement</li> + * <li>the measurement pmfm is not a sample category</li> + * </ul> + * + * @param sortingBatch batch to check + * @return {@code true} if given batch is a frequency batch, + * {@code false} otherwise. + */ + protected boolean isFrequencyBatch(SortingBatch sortingBatch) { + boolean result = false; + if (sortingBatch.getSortingMeasurements().size() == 1) { + SortingMeasurement sm + = sortingBatch.getSortingMeasurements().iterator().next(); + Pmfm pmfm = sm.getPmfm(); + SampleCategoryEnum sampleCategoryByPmfmId = + enumeration.getSampleCategoryByPmfmId(pmfm.getId()); + result = sampleCategoryByPmfmId == null; + + } + return result; + } + + protected SortingBatch getOrCreate(fr.ifremer.adagio.core.dao.data.batch.CatchBatch target, + Batch batch, + Integer pmfmId, + Integer pmfmValue, + Float totalWeight, + short rankOrder) { + return getOrCreate( + target, + batch, + pmfmId, + pmfmValue, + totalWeight, + null, + rankOrder + ); + } + + protected SortingBatch getOrCreate(fr.ifremer.adagio.core.dao.data.batch.CatchBatch rootBatch, + Batch parentBatch, + Integer sortingPmfmId, + Integer sortingQualitativeValueId, + Float weight, + Float weightBeforeSampling, + short rankOrder) { + SortingBatch result = getSortingBatch( + parentBatch, + null, + sortingPmfmId, + sortingQualitativeValueId); + if (result == null) { + + result = SortingBatch.Factory.newInstance(); + if (parentBatch.getChildBatchs() == null) { + parentBatch.setChildBatchs(Sets.<Batch>newHashSet()); + } + parentBatch.getChildBatchs().add(result); + +// Collection<QuantificationMeasurement> quantificationMeasurements = result.getQuantificationMeasurements(); + Collection<SortingMeasurement> sortingMeasurements = result.getSortingMeasurements(); + + // Create lists to store all updates, then remove not updated items +// Set<QuantificationMeasurement> notChangedQuantificationMeasurements = Sets.newHashSet(); +// if (quantificationMeasurements != null) { +// notChangedQuantificationMeasurements.addAll(quantificationMeasurements); +// } + Set<SortingMeasurement> notChangedSortingMeasurements = Sets.newHashSet(); + if (sortingMeasurements != null) { + notChangedSortingMeasurements.addAll(sortingMeasurements); + } + + // Some mandatory properties : + QualityFlagImpl qualityFlag = load(QualityFlagImpl.class, enumeration.QUALITY_FLAG_CODE_NOT_QUALIFIED); + result.setQualityFlag(qualityFlag); + result.setRootBatch(rootBatch); + result.setParentBatch(parentBatch); + result.setExhaustiveInventory(true); + + // No taxon or taxon group + result.setReferenceTaxon(null); + result.setTaxonGroup(null); + + result.setRankOrder(rankOrder); + + // Sorting measurement + if (sortingPmfmId != null && sortingQualitativeValueId != null) { + SortingMeasurement sm = setSortingMeasurement( + result, + sortingPmfmId, + sortingQualitativeValueId); + notChangedSortingMeasurements.remove(sm); + } + // Removed not changed sorting measurements + if (sortingMeasurements != null) { + sortingMeasurements.removeAll(notChangedSortingMeasurements); + } + + catchBatchDao.createSortingBatch(result, rootBatch); + } + + Collection<QuantificationMeasurement> quantificationMeasurements = result.getQuantificationMeasurements(); + Set<QuantificationMeasurement> notChangedQuantificationMeasurements = Sets.newHashSet(); + if (quantificationMeasurements != null) { + notChangedQuantificationMeasurements.addAll(quantificationMeasurements); + } + + // Sampling Ratio + if (weightBeforeSampling == null || weight == null) { + result.setSamplingRatio(null); + result.setSamplingRatioText(null); + } else { + String samplingRatioText = weight + "/" + weightBeforeSampling; + samplingRatioText = samplingRatioText.replaceAll(",", "."); + result.setSamplingRatioText(samplingRatioText); + result.setSamplingRatio(weight / weightBeforeSampling); + } + + // Weight + if (weightBeforeSampling != null || weight != null) { + Float batchReferenceWeight = weight; + if (batchReferenceWeight == null) { + batchReferenceWeight = weightBeforeSampling; + } + QuantificationMeasurement quantificationMeasurement = setWeightMeasurementQuantificationMeasurement( + result, + batchReferenceWeight); + notChangedQuantificationMeasurements.remove(quantificationMeasurement); + } + // Removed not changed quantification measurements + if (quantificationMeasurements != null) { + quantificationMeasurements.removeAll(notChangedQuantificationMeasurements); + } + + return result; + } } Modified: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/batch/ScientificCruiseCatchBatchValidator.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/batch/ScientificCruiseCatchBatchValidator.java 2013-05-01 13:21:10 UTC (rev 905) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/batch/ScientificCruiseCatchBatchValidator.java 2013-05-01 13:30:06 UTC (rev 906) @@ -60,6 +60,9 @@ @Autowired protected TuttiEnumerationFile enumeration; + @Autowired + protected BatchPersistenceHelper batchHelper; + @Override public boolean isEnable(CatchBatch catchBatch) { // Apply validation only on catch batch for fishingOperation @@ -81,9 +84,9 @@ List<CatchBatchValidationError> errors) { // Vrac - SortingBatch vracBatch = catchBatchDao.getSortingBatch( + SortingBatch vracBatch = batchHelper.getSortingBatch( batchs, - BatchPersistenceHelper.BATCH_PMFM_ID, + "Vrac", enumeration.PMFM_ID_SORTED_UNSORTED, enumeration.QUALITATIVE_VRAC_ID); @@ -95,18 +98,31 @@ addError(errors, n_("tutti.persistence.batch.validation.vracNotFound")); } else { // Vrac > Species - SortingBatch speciesBatch = catchBatchDao.getSortingBatch( - vracBatch.getChildBatchs(), - BatchPersistenceHelper.BATCH_PMFM_ID, + SortingBatch speciesBatch = batchHelper.getSortingBatch( + vracBatch, + "Vrac > Species", enumeration.PMFM_ID_SORTING_TYPE, enumeration.QUALITATIVE_ID_SORTING_TYPE_SPECIES); + + // Vrac > Benthos + SortingBatch benthosBatch = batchHelper.getSortingBatch( + vracBatch, + "Vrac > Benthos", + enumeration.PMFM_ID_SORTING_TYPE, + enumeration.QUALITATIVE_ID_SORTING_TYPE_BENTHOS); + if (speciesBatch == null) { - addError(errors, n_("tutti.persistence.batch.validation.vracSpeciesNotFound")); + + if (benthosBatch == null) { + addError(errors, n_("tutti.persistence.batch.validation.vracSpeciesNotFound")); + } else { + addWarning(errors, n_("tutti.persistence.batch.validation.vracSpeciesNotFound")); + } } else { // Vrac > Species > Alive not itemized - SortingBatch livingNotItemizedBatch = catchBatchDao.getSortingBatch( - speciesBatch.getChildBatchs(), - BatchPersistenceHelper.BATCH_PMFM_ID, + SortingBatch livingNotItemizedBatch = batchHelper.getSortingBatch( + speciesBatch, + "Vrac > Species > Alive not itemized", enumeration.PMFM_ID_SORTING_TYPE_2, enumeration.QUALITATIVE_ID_SORTING_TYPE_2_ALIVE_NOT_ITEMIZED); if (livingNotItemizedBatch == null) { @@ -114,9 +130,9 @@ } // Vrac > Species > Inert - SortingBatch inertBatch = catchBatchDao.getSortingBatch( - speciesBatch.getChildBatchs(), - BatchPersistenceHelper.BATCH_PMFM_ID, + SortingBatch inertBatch = batchHelper.getSortingBatch( + speciesBatch, + "Vrac > Species > Inert", enumeration.PMFM_ID_SORTING_TYPE_2, enumeration.QUALITATIVE_ID_SORTING_TYPE_2_INERT); if (inertBatch == null) { @@ -124,9 +140,9 @@ } // Vrac > Species > Alive itemized - SortingBatch aliveItemizedBatch = catchBatchDao.getSortingBatch( - speciesBatch.getChildBatchs(), - BatchPersistenceHelper.BATCH_PMFM_ID, + SortingBatch aliveItemizedBatch = batchHelper.getSortingBatch( + speciesBatch, + "Vrac > Species > Alive itemized", enumeration.PMFM_ID_SORTING_TYPE_2, enumeration.QUALITATIVE_ID_SORTING_TYPE_2_ALIVE_ITEMIZED); if (aliveItemizedBatch == null) { @@ -136,20 +152,18 @@ // TODO verifier que les espèces en haut de grappe ont bien la catégorie voulue (sorted-unsorted) } - // Vrac > Benthos - SortingBatch benthosBatch = catchBatchDao.getSortingBatch( - vracBatch.getChildBatchs(), - BatchPersistenceHelper.BATCH_PMFM_ID, - enumeration.PMFM_ID_SORTING_TYPE, - enumeration.QUALITATIVE_ID_SORTING_TYPE_BENTHOS); if (benthosBatch == null) { - addError(errors, n_("tutti.persistence.batch.validation.vracBenthosNotFound")); + if (speciesBatch == null) { + addError(errors, n_("tutti.persistence.batch.validation.vracBenthosNotFound")); + } else { + addWarning(errors, n_("tutti.persistence.batch.validation.vracBenthosNotFound")); + } } else { // Vrac > Benthos > Alive not itemized - SortingBatch livingNotItemizedBatch = catchBatchDao.getSortingBatch( - benthosBatch.getChildBatchs(), - BatchPersistenceHelper.BATCH_PMFM_ID, + SortingBatch livingNotItemizedBatch = batchHelper.getSortingBatch( + benthosBatch, + "Vrac > Benthos > Alive not itemized", enumeration.PMFM_ID_SORTING_TYPE_2, enumeration.QUALITATIVE_ID_SORTING_TYPE_2_ALIVE_NOT_ITEMIZED); if (livingNotItemizedBatch == null) { @@ -157,9 +171,9 @@ } // Vrac > Benthos > Inert - SortingBatch inertBatch = catchBatchDao.getSortingBatch( - benthosBatch.getChildBatchs(), - BatchPersistenceHelper.BATCH_PMFM_ID, + SortingBatch inertBatch = batchHelper.getSortingBatch( + benthosBatch, + "Vrac > Benthos > Inert", enumeration.PMFM_ID_SORTING_TYPE_2, enumeration.QUALITATIVE_ID_SORTING_TYPE_2_INERT); if (inertBatch == null) { @@ -167,31 +181,30 @@ } // Vrac > Benthos > Alive itermized - SortingBatch aliveItemizedBatch = catchBatchDao.getSortingBatch( - benthosBatch.getChildBatchs(), - BatchPersistenceHelper.BATCH_PMFM_ID, + SortingBatch aliveItemizedBatch = batchHelper.getSortingBatch( + benthosBatch, + "Vrac > Benthos > Alive itermized", enumeration.PMFM_ID_SORTING_TYPE_2, enumeration.QUALITATIVE_ID_SORTING_TYPE_2_ALIVE_ITEMIZED); if (aliveItemizedBatch == null) { addWarning(errors, n_("tutti.persistence.batch.validation.vracBenthosAliveItemizedNotFound")); } } - // TODO : Plankton... } // Hors Vrac - SortingBatch horsVracBatch = catchBatchDao.getSortingBatch( + SortingBatch horsVracBatch = batchHelper.getSortingBatch( batchs, - BatchPersistenceHelper.BATCH_PMFM_ID, + "Hors Vrac", enumeration.PMFM_ID_SORTED_UNSORTED, enumeration.QUALITATIVE_HORS_VRAC_ID); if (horsVracBatch == null) { - addWarning(errors, n_("tutti.persistence.batch.validation.horsVracSpeciesNotFound")); + addWarning(errors, n_("tutti.persistence.batch.validation.horsVracNotFound")); } else { // Hors Vrac > Species - SortingBatch speciesBatch = catchBatchDao.getSortingBatch( - horsVracBatch.getChildBatchs(), - BatchPersistenceHelper.BATCH_PMFM_ID, + SortingBatch speciesBatch = batchHelper.getSortingBatch( + horsVracBatch, + "Hors Vrac > Species", enumeration.PMFM_ID_SORTING_TYPE, enumeration.QUALITATIVE_ID_SORTING_TYPE_SPECIES); if (speciesBatch == null) { @@ -199,31 +212,30 @@ } // Hors Vrac > Benthos - SortingBatch benthosBatch = catchBatchDao.getSortingBatch( - horsVracBatch.getChildBatchs(), - BatchPersistenceHelper.BATCH_PMFM_ID, + SortingBatch benthosBatch = batchHelper.getSortingBatch( + horsVracBatch, + "Hors Vrac > Benthos", enumeration.PMFM_ID_SORTING_TYPE, enumeration.QUALITATIVE_ID_SORTING_TYPE_BENTHOS); if (benthosBatch == null) { addWarning(errors, n_("tutti.persistence.batch.validation.horsVracBenthosNotFound")); } // Hors Vrac > Marine Litter - SortingBatch marineLitterBatch = catchBatchDao.getSortingBatch( - horsVracBatch.getChildBatchs(), - BatchPersistenceHelper.BATCH_PMFM_ID, + SortingBatch marineLitterBatch = batchHelper.getSortingBatch( + horsVracBatch, + "Hors Vrac > Marine Litter", enumeration.PMFM_ID_SORTING_TYPE, enumeration.QUALITATIVE_ID_SORTING_TYPE_MARINE_LITTER); if (marineLitterBatch == null) { addWarning(errors, n_("tutti.persistence.batch.validation.horsVracMarineLitterNotFound")); } - // TODO : Plankton... } // Unsorted - SortingBatch unsortedBatch = catchBatchDao.getSortingBatch( + SortingBatch unsortedBatch = batchHelper.getSortingBatch( batchs, - BatchPersistenceHelper.BATCH_PMFM_ID, + "Unsorted", enumeration.PMFM_ID_SORTED_UNSORTED, enumeration.QUALITATIVE_UNSORTED_ID); if (unsortedBatch == null) { Modified: trunk/tutti-persistence/src/main/resources/i18n/tutti-persistence_en_GB.properties =================================================================== --- trunk/tutti-persistence/src/main/resources/i18n/tutti-persistence_en_GB.properties 2013-05-01 13:21:10 UTC (rev 905) +++ trunk/tutti-persistence/src/main/resources/i18n/tutti-persistence_en_GB.properties 2013-05-01 13:30:06 UTC (rev 906) @@ -18,6 +18,7 @@ tutti.persistence.attachment.deleteFile.error= tutti.persistence.batch.validation.horsVracBenthosNotFound= tutti.persistence.batch.validation.horsVracMarineLitterNotFound= +tutti.persistence.batch.validation.horsVracNotFound= tutti.persistence.batch.validation.horsVracSpeciesNotFound= tutti.persistence.batch.validation.unsortedNotFound= tutti.persistence.batch.validation.vracBenthosAliveItemizedNotFound= Modified: trunk/tutti-persistence/src/main/resources/i18n/tutti-persistence_fr_FR.properties =================================================================== --- trunk/tutti-persistence/src/main/resources/i18n/tutti-persistence_fr_FR.properties 2013-05-01 13:21:10 UTC (rev 905) +++ trunk/tutti-persistence/src/main/resources/i18n/tutti-persistence_fr_FR.properties 2013-05-01 13:30:06 UTC (rev 906) @@ -18,6 +18,7 @@ tutti.persistence.attachment.deleteFile.error=Erreur lors de la suppression de la pièce jointe %s tutti.persistence.batch.validation.horsVracBenthosNotFound=Lot 'Hors Vrac > Benthos' non trouvé ou configuré avec d'autres critères de classement. tutti.persistence.batch.validation.horsVracMarineLitterNotFound=Lot 'Hors Vrac > Macro-déchet' non trouvé, ou configuré avec d'autres critères de classement. +tutti.persistence.batch.validation.horsVracNotFound=Lot 'Hors Vrac' non trouvé ou configuré avec d'autres critères de classement. tutti.persistence.batch.validation.horsVracSpeciesNotFound=Lot 'Hors Vrac > Espèces' non trouvé ou configuré avec d'autres critères de classement. tutti.persistence.batch.validation.unsortedNotFound=Lot 'Hors Vrac > Espèces' non trouvé ou configuré avec d'autres critères de classement. tutti.persistence.batch.validation.vracBenthosAliveItemizedNotFound=Lot 'Vrac > Benthos > Vivant Trié' non trouvé, ou configuré avec d'autres critères de classement. Modified: trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/CatchBatchPersistenceServiceReadTest.java =================================================================== --- trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/CatchBatchPersistenceServiceReadTest.java 2013-05-01 13:21:10 UTC (rev 905) +++ trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/CatchBatchPersistenceServiceReadTest.java 2013-05-01 13:30:06 UTC (rev 906) @@ -24,8 +24,8 @@ * #L% */ -import fr.ifremer.adagio.core.dao.data.batch.validator.CatchBatchValidationException; import fr.ifremer.tutti.persistence.DatabaseResource; +import fr.ifremer.tutti.persistence.InvalidBatchModelException; import fr.ifremer.tutti.persistence.entities.data.FishingOperation; import org.junit.Before; import org.junit.ClassRule; @@ -68,7 +68,7 @@ assertNotNull(fishingOperation.getId()); } - @Test(expected = CatchBatchValidationException.class) + @Test(expected = InvalidBatchModelException.class) public void getCatchBatchFromFishingOperation() throws Exception { //TODO-TC Change test when data will be Tutti-aware Modified: trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/CatchBatchPersistenceServiceWriteTest.java =================================================================== --- trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/CatchBatchPersistenceServiceWriteTest.java 2013-05-01 13:21:10 UTC (rev 905) +++ trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/CatchBatchPersistenceServiceWriteTest.java 2013-05-01 13:30:06 UTC (rev 906) @@ -24,8 +24,8 @@ * #L% */ -import fr.ifremer.adagio.core.dao.data.batch.validator.CatchBatchValidationException; import fr.ifremer.tutti.persistence.DatabaseResource; +import fr.ifremer.tutti.persistence.InvalidBatchModelException; import fr.ifremer.tutti.persistence.entities.TuttiBeanFactory; import fr.ifremer.tutti.persistence.entities.data.CatchBatch; import fr.ifremer.tutti.persistence.entities.data.Cruise; @@ -188,7 +188,7 @@ CatchBatch reloadedCatchBatch = null; try { reloadedCatchBatch = service.getCatchBatchFromFishingOperation(fishingOperationId); - } catch (CatchBatchValidationException e) { + } catch (InvalidBatchModelException e) { Assert.fail(e.getMessage()); } assertCatchBatch(createdCatchBatch, reloadedCatchBatch, true); @@ -205,7 +205,7 @@ CatchBatch reloadedCatchBatch = null; try { reloadedCatchBatch = service.getCatchBatchFromFishingOperation(fishingOperationId); - } catch (CatchBatchValidationException e) { + } catch (InvalidBatchModelException e) { Assert.fail(e.getMessage()); } assertCatchBatch(savedCatchBatch, reloadedCatchBatch, true);
participants (1)
-
tchemit@users.forge.codelutin.com