Author: blavenier Date: 2013-02-25 15:11:54 +0100 (Mon, 25 Feb 2013) New Revision: 461 Url: http://forge.codelutin.com/projects/tutti/repository/revisions/461 Log: ref refs #1936: [CAPTURE] - Esp?\195?\168ce - pouvoir changer le nom d'une esp?\195?\168ce - Add changeSpeciesBatchSpecies() implementation Fix : - Fix save/retrieve of qualitative value on operation (refs #2028) Add : - Ajout de la conversion possible d'un arbre d'?\195?\169chantillonnage (refs #1997) Added: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/batch/ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/batch/ScientificCruiseCatchBatchValidator.java trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/batch/ trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/batch/CatchBatchValidatorReadTest.java Modified: trunk/pom.xml trunk/tutti-persistence/pom.xml trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/TuttiPersistence.java trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/TuttiPersistenceImpl.java trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/BatchPersistenceService.java trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/BatchPersistenceServiceImpl.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/CruisePersistenceServiceImpl.java trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/FishingOperationPersistenceServiceImpl.java trunk/tutti-persistence/src/main/resources/i18n/tutti-persistence_fr_FR.properties trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/BatchPersistenceServiceWriteTest.java trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/MainUIHandler.java Modified: trunk/pom.xml =================================================================== --- trunk/pom.xml 2013-02-25 13:39:39 UTC (rev 460) +++ trunk/pom.xml 2013-02-25 14:11:54 UTC (rev 461) @@ -138,7 +138,7 @@ <slf4jVersion>1.7.2</slf4jVersion> - <adagioVersion>3.3.2</adagioVersion> + <adagioVersion>3.3.3-SNAPSHOT</adagioVersion> <msaccessImporterVersion>1.4.1</msaccessImporterVersion> Modified: trunk/tutti-persistence/pom.xml =================================================================== --- trunk/tutti-persistence/pom.xml 2013-02-25 13:39:39 UTC (rev 460) +++ trunk/tutti-persistence/pom.xml 2013-02-25 14:11:54 UTC (rev 461) @@ -162,6 +162,13 @@ <artifactId>spring-test</artifactId> </dependency> + <dependency> + <groupId>org.jmock</groupId> + <artifactId>jmock-junit4</artifactId> + <version>2.5.1</version> + <scope>test</scope> + </dependency> + <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> Modified: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/TuttiPersistence.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/TuttiPersistence.java 2013-02-25 13:39:39 UTC (rev 460) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/TuttiPersistence.java 2013-02-25 14:11:54 UTC (rev 461) @@ -305,6 +305,7 @@ * @param id id of the fihsing operation * @return found catchbatch */ + @Transactional(readOnly = false) CatchBatch getCatchBatchFromFishingOperation(String id); /** 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-02-25 13:39:39 UTC (rev 460) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/TuttiPersistenceImpl.java 2013-02-25 14:11:54 UTC (rev 461) @@ -25,6 +25,8 @@ */ import com.google.common.collect.Maps; + +import fr.ifremer.adagio.core.dao.data.batch.validator.CatchBatchValidationException; import fr.ifremer.tutti.persistence.entities.data.AccidentalBatch; import fr.ifremer.tutti.persistence.entities.data.BenthosBatch; import fr.ifremer.tutti.persistence.entities.data.CatchBatch; @@ -442,7 +444,13 @@ @Override public CatchBatch getCatchBatchFromFishingOperation(String id) { - return catchBatchService.getCatchBatchFromFishingOperation(id); + try { + return catchBatchService.getCatchBatchFromFishingOperation(id, true); + } catch (CatchBatchValidationException e) { + // TODO Code lutin : throw exception and ask user : "Voulez convertir les donn�es d'�chantillonnage dans un format compatible tutti ?" + e.printStackTrace(); + return null; + } } @Override Modified: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/BatchPersistenceService.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/BatchPersistenceService.java 2013-02-25 13:39:39 UTC (rev 460) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/BatchPersistenceService.java 2013-02-25 14:11:54 UTC (rev 461) @@ -27,7 +27,13 @@ import org.springframework.transaction.annotation.Transactional; +import fr.ifremer.adagio.core.dao.data.batch.CatchBatch; +import fr.ifremer.adagio.core.dao.data.batch.CatchBatchExtendDao; + @Transactional(readOnly = true) public interface BatchPersistenceService extends CatchBatchPersistenceService, SpeciesBatchPersistenceService { + void cleanEntity(CatchBatch catchBatch, boolean keepTotalWeight); + + void setCatchBatchDao(CatchBatchExtendDao dao); } \ No newline at end of file Modified: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/BatchPersistenceServiceImpl.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/BatchPersistenceServiceImpl.java 2013-02-25 13:39:39 UTC (rev 460) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/BatchPersistenceServiceImpl.java 2013-02-25 14:11:54 UTC (rev 461) @@ -25,24 +25,49 @@ */ +import java.io.Serializable; +import java.text.MessageFormat; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import javax.annotation.Resource; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.hibernate.FlushMode; +import org.hibernate.type.IntegerType; +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.dao.DataIntegrityViolationException; +import org.springframework.dao.DataRetrievalFailureException; +import org.springframework.stereotype.Service; + import com.google.common.base.Preconditions; import com.google.common.collect.Lists; + import fr.ifremer.adagio.core.dao.data.batch.Batch; 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.SortingBatchDao; import fr.ifremer.adagio.core.dao.data.batch.denormalized.DenormalizedBatchDao; -import fr.ifremer.adagio.core.dao.data.batch.validator.CatchBatchValidationError; 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.Measurement; 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.referential.pmfm.QualitativeValue; +import fr.ifremer.adagio.core.dao.referential.pmfm.QualitativeValueImpl; import fr.ifremer.adagio.core.dao.referential.taxon.ReferenceTaxon; import fr.ifremer.adagio.core.dao.referential.taxon.ReferenceTaxonImpl; import fr.ifremer.adagio.core.dao.technical.synchronization.SynchronizationStatus; import fr.ifremer.tutti.persistence.entities.data.CatchBatch; +import fr.ifremer.tutti.persistence.entities.data.FishingOperation; import fr.ifremer.tutti.persistence.entities.data.SampleCategoryEnum; import fr.ifremer.tutti.persistence.entities.data.SpeciesBatch; import fr.ifremer.tutti.persistence.entities.data.SpeciesBatchFrequency; @@ -50,34 +75,10 @@ import fr.ifremer.tutti.persistence.entities.referential.CaracteristicQualitativeValue; import fr.ifremer.tutti.persistence.entities.referential.CaracteristicType; import fr.ifremer.tutti.persistence.entities.referential.Species; -import fr.ifremer.tutti.persistence.service.measure.MeasurementPersistenceHelper; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.hibernate.FlushMode; -import org.hibernate.type.IntegerType; -import org.springframework.cache.annotation.CacheEvict; -import org.springframework.dao.DataIntegrityViolationException; -import org.springframework.dao.DataRetrievalFailureException; -import org.springframework.stereotype.Service; -import javax.annotation.Resource; -import java.io.Serializable; -import java.text.MessageFormat; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import static org.nuiton.i18n.I18n._; - @Service("batchPersistenceService") public class BatchPersistenceServiceImpl - extends AbstractPersistenceService implements BatchPersistenceService, CatchBatchValidator { + extends AbstractPersistenceService implements BatchPersistenceService { /** Logger. */ private static final Log log = @@ -95,46 +96,34 @@ @Resource(name = "catchBatchDao") protected CatchBatchExtendDao catchBatchDao; - @Resource(name = "measurementPersistenceHelper") - protected MeasurementPersistenceHelper measurementHelper; - + @Resource(name = "scientificCruiseCatchBatchValidator") + protected CatchBatchValidator catchBatchValidator; + @Override public void init() { super.init(); - catchBatchDao.registerCatchBatchValidator(this); + catchBatchDao.registerCatchBatchValidator(catchBatchValidator); } @Override public void close() { - catchBatchDao.unregisterCatchBatchValidator(this); + catchBatchDao.unregisterCatchBatchValidator(catchBatchValidator); super.close(); } @Override - public boolean isEnable(fr.ifremer.adagio.core.dao.data.batch.CatchBatch catchBatch) { - // Apply validation only on catch batch for fishingOperation - return (catchBatch.getFishingOperation() != null); + public CatchBatch getCatchBatchFromFishingOperation(String fishingOperationId) throws CatchBatchValidationException { + return getCatchBatchFromFishingOperation(fishingOperationId, false); } @Override - public List<CatchBatchValidationError> validate(fr.ifremer.adagio.core.dao.data.batch.CatchBatch catchBatch) { - List<CatchBatchValidationError> errors = Lists.newArrayList(); - validate(catchBatch.getChildBatchs(), errors, 1); - return errors; - } - - @Override - public CatchBatch getCatchBatchFromFishingOperation(String fishingOperationId) { + public CatchBatch getCatchBatchFromFishingOperation(String fishingOperationId, boolean tryToRepair) throws CatchBatchValidationException { Preconditions.checkNotNull(fishingOperationId); Integer catchBatchId = catchBatchDao.getIdByFishingOperationId(Integer.valueOf(fishingOperationId)); fr.ifremer.adagio.core.dao.data.batch.CatchBatch source; - try { - source = catchBatchDao.loadFullTree(catchBatchId, true, true); - } catch (CatchBatchValidationException e) { - throw new DataRetrievalFailureException("Invalid batch tree structure, for fishing operation id=" + fishingOperationId, e); - } + source = catchBatchDao.loadFullTree(catchBatchId, true, tryToRepair); CatchBatch result = new CatchBatch(); result.setId(source.getId().toString()); @@ -157,12 +146,16 @@ // Inert SortingBatch inertBatch = catchBatchDao.getSortingBatch(speciesBatch.getChildBatchs(), "referenceTaxonId", enumeration.REFERENCE_TAXON_ID_INERT); - result.setSpeciesTotalInertWeight(inertBatch.getWeight()); + if (inertBatch != null) { + result.setSpeciesTotalInertWeight(inertBatch.getWeight()); + } // Alive no itemized SortingBatch livingNotItemizedBatch = catchBatchDao.getSortingBatch(speciesBatch.getChildBatchs(), "referenceTaxonId", enumeration.REFERENCE_TAXON_ID_LIFE); - result.setSpeciesTotalLivingNotItemizedWeight(livingNotItemizedBatch.getWeight()); + if (livingNotItemizedBatch != null) { + result.setSpeciesTotalLivingNotItemizedWeight(livingNotItemizedBatch.getWeight()); + } } // TODO : Benthos, Plancton... @@ -171,27 +164,59 @@ // Hors Vrac SortingBatch horsVracBatch = catchBatchDao.getSortingBatch(source.getChildBatchs(), "pmfmId", enumeration.PMFM_ID_SORTED_UNSORTED, enumeration.QUALITATIVE_HORS_VRAC_ID); - result.setCatchTotalUnsortedWeight(horsVracBatch.getWeight()); - { - // Species - SortingBatch speciesBatch = catchBatchDao.getSortingBatch(horsVracBatch.getChildBatchs(), - "pmfmId", enumeration.PMFM_ID_SORTING_TYPE, enumeration.QUALITATIVE_ID_SORTING_TYPE_SPECIES); - - result.setSpeciesTotalUnsortedWeight(speciesBatch.getWeight()); - - // TODO : Benthos, Plancton... + if (horsVracBatch != null) { + result.setCatchTotalUnsortedWeight(horsVracBatch.getWeight()); + { + // Species + SortingBatch speciesBatch = catchBatchDao.getSortingBatch(horsVracBatch.getChildBatchs(), + "pmfmId", enumeration.PMFM_ID_SORTING_TYPE, enumeration.QUALITATIVE_ID_SORTING_TYPE_SPECIES); + + result.setSpeciesTotalUnsortedWeight(speciesBatch.getWeight()); + + // TODO : Benthos, Plancton... + } } // Non trié SortingBatch unsortedBatch = catchBatchDao.getSortingBatch(source.getChildBatchs(), "pmfmId", enumeration.PMFM_ID_SORTED_UNSORTED, enumeration.QUALITATIVE_UNSORTED_ID); - result.setCatchTotalRejectedWeight(unsortedBatch.getWeight()); + if (unsortedBatch != null) { + result.setCatchTotalRejectedWeight(unsortedBatch.getWeight()); + } return result; } + + @Override + public void cleanEntity(fr.ifremer.adagio.core.dao.data.batch.CatchBatch catchBatch, + boolean keepTotalWeight) { + + // TODO BL : voir si nécessaire : + for (Iterator<Batch> iterator = catchBatch.getChildBatchs().iterator(); iterator.hasNext();) { + Batch batch = (Batch) iterator.next(); + catchBatchDao.removeWithChildren(batch.getId()); + } + catchBatch.getChildBatchs().clear(); + + // Create a new tutti bean CatchBatch, need for method beanToEntity() + fr.ifremer.tutti.persistence.entities.data.CatchBatch bean = new fr.ifremer.tutti.persistence.entities.data.CatchBatch(); + bean.setId(catchBatch.getId().toString()); + + // Link the bean to a fake fishingOperation, need in the preconditions for method beanToEntity() + FishingOperation fishingOperation = new FishingOperation(); + fishingOperation.setId(catchBatch.getFishingOperation().getId().toString()); + bean.setFishingOperation(fishingOperation); + // If need, copy the total weight, to be sure beanToEntity() will restore it + if (keepTotalWeight) { + bean.setCatchTotalWeight(catchBatch.getWeight()); + } + + beanToEntity(bean, catchBatch, true); + } + + @Override - public CatchBatch createCatchBatch(CatchBatch bean) { Preconditions.checkNotNull(bean); Preconditions.checkArgument(bean.getId() == null); @@ -215,7 +240,7 @@ return bean; } - + @Override @CacheEvict(value = "batchTree", key = "#bean.fishingOperation.id") public CatchBatch saveCatchBatch(CatchBatch bean) { @@ -268,13 +293,15 @@ SortingBatch horsVracSpeciesBatch = catchBatchDao.getSortingBatch(catchBatch.getChildBatchs(), "pmfmId", enumeration.PMFM_ID_SORTED_UNSORTED, enumeration.QUALITATIVE_HORS_VRAC_ID, "pmfmId", enumeration.PMFM_ID_SORTING_TYPE, enumeration.QUALITATIVE_ID_SORTING_TYPE_SPECIES); - for (Iterator<Batch> iterator = horsVracSpeciesBatch.getChildBatchs().iterator(); iterator.hasNext(); ) { - SortingBatch source = (SortingBatch) iterator.next(); - - SpeciesBatch target = new SpeciesBatch(); - - entityToBean(source, target); - result.add(target); + if (horsVracSpeciesBatch != null) { + for (Iterator<Batch> iterator = horsVracSpeciesBatch.getChildBatchs().iterator(); iterator.hasNext(); ) { + SortingBatch source = (SortingBatch) iterator.next(); + + SpeciesBatch target = new SpeciesBatch(); + + entityToBean(source, target); + result.add(target); + } } return result; @@ -303,7 +330,7 @@ if (source.getSortingMeasurements().size() == 1) { sm = source.getSortingMeasurements().iterator().next(); } else if (source.getReferenceTaxon() != null && source.getReferenceTaxon().getId() != null) { - sm = measurementHelper.getInheritedSortingMeasurement(source, enumeration.PMFM_ID_SORTED_UNSORTED); + sm = catchBatchDao.getInheritedSortingMeasurement(source, enumeration.PMFM_ID_SORTED_UNSORTED); } if (sm != null) { SampleCategoryEnum sampleCategory = referentialService.getSampleCategoryByPmfmId(sm.getPmfm().getId()); @@ -552,76 +579,6 @@ // -- Internal methods --// // ------------------------------------------------------------------------// - protected void validate(Collection<Batch> batchs, List<CatchBatchValidationError> errors, int treeLevel) { - - List<Integer> expectedPmfms = null; - boolean expectedReferenceTaxon = false; - // First tree level : should have pmfm SORTED (Vrac, Hors Vrac, Non trié) - if (treeLevel == 1) { - expectedPmfms = Lists.newArrayList(enumeration.PMFM_ID_SORTED_UNSORTED); - } - // Second tree level : should have pmfm SORTING_TYPE (Species, Benthos...) - else if (treeLevel == 2) { - expectedPmfms = Lists.newArrayList(enumeration.PMFM_ID_SORTING_TYPE); - } else if (treeLevel == 3) { - expectedReferenceTaxon = true; - } else if (treeLevel > 3) { - expectedPmfms = referentialService.getSampleCategoryIds(); - expectedPmfms.remove(enumeration.PMFM_ID_SORTED_UNSORTED); - } - - // First level : should be only PMFM=SORTED - for (Iterator<Batch> iterator = batchs.iterator(); iterator.hasNext(); ) { - SortingBatch batch = (SortingBatch) iterator.next(); - boolean batchHasError = false; - - // For all sorting measurements - if (batch.getSortingMeasurements() != null && batch.getSortingMeasurements().size() > 0) { - for (Iterator<SortingMeasurement> iterator2 = batch.getSortingMeasurements().iterator(); iterator2.hasNext(); ) { - SortingMeasurement sm = iterator2.next(); - Integer pmfmId = sm.getPmfm().getId(); - if (expectedReferenceTaxon) { - CatchBatchValidationError error = new CatchBatchValidationError( - "tutti.persistence.batch.validation.onlyTaxonButPmfmFound", - _("tutti.persistence.batch.validation.onlyTaxonButPmfmFound", batch.getId(), pmfmId), - CatchBatchValidationError.GRAVITY_ERROR); - errors.add(error); - } else if (expectedPmfms.contains(pmfmId) == false) { - if (sm.getNumericalValue() == null || batch.getChildBatchs().size() > 0) { - batchHasError = true; - CatchBatchValidationError error = new CatchBatchValidationError( - "tutti.persistence.batch.validation.pmfmNotFound", - _("tutti.persistence.batch.validation.pmfmNotFound", - batch.getId(), Arrays.toString(expectedPmfms.toArray()), pmfmId), - CatchBatchValidationError.GRAVITY_ERROR); - errors.add(error); - } - } - } - } else if (expectedPmfms != null && expectedPmfms.size() > 0) { - batchHasError = true; - CatchBatchValidationError error = new CatchBatchValidationError( - "tutti.persistence.batch.validation.pmfmNotFound", - _("tutti.persistence.batch.validation.pmfmNotFound", - batch.getId(), Arrays.toString(expectedPmfms.toArray()), "null"), - CatchBatchValidationError.GRAVITY_ERROR); - errors.add(error); - } - - if (!batchHasError && expectedReferenceTaxon == true && batch.getReferenceTaxon() == null) { - CatchBatchValidationError error = new CatchBatchValidationError( - "tutti.persistence.batch.validation.referenceTaxonNotFound", - _("tutti.persistence.batch.validation.referenceTaxonNotFound", batch.getId()), - CatchBatchValidationError.GRAVITY_ERROR); - errors.add(error); - } - - if (!batchHasError) { - validate(batch.getChildBatchs(), errors, treeLevel + 1); - } - } - } - protected void beanToEntity(SpeciesBatch source, SortingBatch target, String parentBatchId, @@ -668,11 +625,11 @@ if (copyIfNull && 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 = measurementHelper.setQuantificationMeasurement(target, + QuantificationMeasurement quantificationMeasurement = catchBatchDao.setQuantificationMeasurement(target, enumeration.PMFM_ID_WEIGHT_MEASURED, recorderDepartmentId, source.getSampleCategoryWeight(), true); notChangedQuantificationMeasurements.remove(quantificationMeasurement); } else if (source.getWeight() != null && source.getSampleCategoryWeight() == null) { - QuantificationMeasurement quantificationMeasurement = measurementHelper.setQuantificationMeasurement(target, + QuantificationMeasurement quantificationMeasurement = catchBatchDao.setQuantificationMeasurement(target, enumeration.PMFM_ID_WEIGHT_MEASURED, recorderDepartmentId, source.getWeight(), true); notChangedQuantificationMeasurements.remove(quantificationMeasurement); } @@ -686,7 +643,7 @@ samplingRatioText = samplingRatioText.replaceAll(",", "."); target.setSamplingRatioText(samplingRatioText); target.setSamplingRatio(source.getWeight() / source.getSampleCategoryWeight()); - QuantificationMeasurement quantificationMeasurement = measurementHelper.setQuantificationMeasurement(target, + QuantificationMeasurement quantificationMeasurement = catchBatchDao.setQuantificationMeasurement(target, enumeration.PMFM_ID_WEIGHT_MEASURED, recorderDepartmentId, source.getWeight(), true); notChangedQuantificationMeasurements.remove(quantificationMeasurement); } @@ -695,10 +652,10 @@ if (copyIfNull && (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 = measurementHelper.sampleCategory2PmfmId(source.getSampleCategoryType()); + Integer pmfmId = sampleCategory2PmfmId(source.getSampleCategoryType()); // Do not store sorting measurement if pmfm = SORTED (already store in an ancestor batch) if (!pmfmId.equals(enumeration.PMFM_ID_SORTED_UNSORTED)) { - SortingMeasurement sortingMeasurement = measurementHelper.setSortingMeasurement(target, recorderDepartmentId, + SortingMeasurement sortingMeasurement = setSortingMeasurement(target, recorderDepartmentId, source.getSampleCategoryType(), source.getSampleCategoryValue()); notChangedSortingMeasurements.remove(sortingMeasurement); } @@ -765,7 +722,7 @@ // Or retrieve parent batch, from pmfm id else { // Retrieve category type - Integer pmfmId = measurementHelper.sampleCategory2PmfmId(source.getSampleCategoryType()); + Integer pmfmId = sampleCategory2PmfmId(source.getSampleCategoryType()); if (pmfmId == null || !pmfmId.equals(enumeration.PMFM_ID_SORTED_UNSORTED)) { throw new DataIntegrityViolationException(MessageFormat.format( "A species batch with no parent should have a sampleCategoryType {0} (PMFM.ID={1})", @@ -873,7 +830,7 @@ if (copyIfNull && source.getWeight() == null) { // Nothing to do : will be removed later, using notChangedSortingMeasurements } else if (source.getWeight() != null) { - QuantificationMeasurement quantificationMeasurement = measurementHelper.setQuantificationMeasurement(target, + QuantificationMeasurement quantificationMeasurement = catchBatchDao.setQuantificationMeasurement(target, enumeration.PMFM_ID_WEIGHT_MEASURED, recorderDepartmentId, source.getWeight(), true); notChangedQuantificationMeasurements.remove(quantificationMeasurement); } @@ -883,7 +840,7 @@ // Nothing to do : will be removed later, using notChangedSortingMeasurements } else if (source.getLengthStepCaracteristic() != null && source.getLengthStep() != null) { Integer pmfmId = Integer.valueOf(source.getLengthStepCaracteristic().getId()); - SortingMeasurement sortingMeasurement = measurementHelper.setSortingMeasurement(target, recorderDepartmentId, pmfmId, + SortingMeasurement sortingMeasurement = setSortingMeasurement(target, recorderDepartmentId, pmfmId, source.getLengthStep()); notChangedSortingMeasurements.remove(sortingMeasurement); } @@ -978,7 +935,7 @@ if (copyIfNull && source.getCatchTotalWeight() == null) { // Nothing to do : will be removed later, using notChangedQuantificationMeasurements } else if (source.getCatchTotalWeight() != null) { - QuantificationMeasurement quantificationMeasurement = measurementHelper.setQuantificationMeasurement(target, + QuantificationMeasurement quantificationMeasurement = catchBatchDao.setQuantificationMeasurement(target, enumeration.PMFM_ID_WEIGHT_MEASURED, recorderDepartmentId, source.getCatchTotalWeight(), true); notChangedQuantificationMeasurements.remove(quantificationMeasurement); } @@ -1129,7 +1086,7 @@ } for (Batch childBatch : parentBatch.getChildBatchs()) { SortingBatch childSortingBatch = (SortingBatch) childBatch; - SortingMeasurement sm = measurementHelper.getSortingMeasurement((SortingBatch) childBatch, pmfmId, null, false); + 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); } @@ -1197,7 +1154,7 @@ if (copyIfNull && (sortingPmfmId == null || sortingQualitativeValueId == null)) { // Nothing to do : will be removed later, using notChangedSortingMeasurements } else if (sortingPmfmId != null && sortingQualitativeValueId != null) { - SortingMeasurement sm = measurementHelper.setSortingMeasurement(target, recorderDepartmentId, sortingPmfmId, sortingQualitativeValueId); + SortingMeasurement sm = setSortingMeasurement(target, recorderDepartmentId, sortingPmfmId, sortingQualitativeValueId); notChangedSortingMeasurements.remove(sm); } @@ -1220,7 +1177,7 @@ if (batchReferenceWeight == null) { batchReferenceWeight = sampleWeight; } - QuantificationMeasurement quantificationMeasurement = measurementHelper.setQuantificationMeasurement(target, + QuantificationMeasurement quantificationMeasurement = catchBatchDao.setQuantificationMeasurement(target, enumeration.PMFM_ID_WEIGHT_MEASURED, recorderDepartmentId, batchReferenceWeight, true); notChangedQuantificationMeasurements.remove(quantificationMeasurement); } @@ -1287,7 +1244,7 @@ if (batchReferenceWeight == null) { batchReferenceWeight = sampleWeight; } - QuantificationMeasurement quantificationMeasurement = measurementHelper.setQuantificationMeasurement(target, + QuantificationMeasurement quantificationMeasurement = catchBatchDao.setQuantificationMeasurement(target, enumeration.PMFM_ID_WEIGHT_MEASURED, recorderDepartmentId, batchReferenceWeight, true); notChangedQuantificationMeasurements.remove(quantificationMeasurement); } @@ -1309,4 +1266,80 @@ // TODO BL : voir si on peut récupérer le departement (du 1er saisisseur ?) return enumeration.DEPARTMENT_ID_UNKNOWN_RECORDER_DEPARTMENT; } + + 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 = null; + if (value instanceof CaracteristicQualitativeValue) { + qvId = Integer.valueOf(((CaracteristicQualitativeValue) value).getId()); + } 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 SortingMeasurement setSortingMeasurement( + SortingBatch sortingBatch, Integer recorderDepartmentId, + SampleCategoryEnum sampleCategory, Serializable value) { + Preconditions.checkNotNull(sampleCategory); + Preconditions.checkNotNull(value); + + Integer pmfmId = sampleCategory2PmfmId(sampleCategory); + + Caracteristic caracteristic = referentialService.getCaracteristic(pmfmId); + SortingMeasurement sortingMeasurement = catchBatchDao.getSortingMeasurement( + sortingBatch, pmfmId, recorderDepartmentId, true); + setMeasurement(sortingMeasurement, caracteristic, value); + return sortingMeasurement; + } + + public SortingMeasurement setSortingMeasurement( + SortingBatch sortingBatch, Integer recorderDepartmentId, + Integer pmfmId, Serializable value) { + Preconditions.checkNotNull(pmfmId); + Preconditions.checkNotNull(value); + + Caracteristic caracteristic = referentialService.getCaracteristic(pmfmId); + SortingMeasurement sortingMeasurement = catchBatchDao.getSortingMeasurement( + sortingBatch, pmfmId, recorderDepartmentId, true); + setMeasurement(sortingMeasurement, caracteristic, value); + return sortingMeasurement; + } + + public Integer sampleCategory2PmfmId(SampleCategoryEnum sampleCategory) { + Integer pmfmId = null; + if (sampleCategory == SampleCategoryEnum.sortedUnsorted) { + pmfmId = enumeration.PMFM_ID_SORTED_UNSORTED; + } else if (sampleCategory == SampleCategoryEnum.size) { + pmfmId = enumeration.PMFM_ID_SIZE_CATEGORY; + } else if (sampleCategory == SampleCategoryEnum.maturity) { + pmfmId = enumeration.PMFM_ID_MATURITY; + } else if (sampleCategory == SampleCategoryEnum.sex) { + pmfmId = enumeration.PMFM_ID_SEX; + } else if (sampleCategory == SampleCategoryEnum.age) { + pmfmId = enumeration.PMFM_ID_AGE; + } + if (pmfmId == null) { + throw new IllegalArgumentException("Unable to find corresponding PMFM.ID for sampleCategory : " + sampleCategory.name()); + } + return pmfmId; + } + + /** + * Need for JMock injection, in unit test + * @param catchBatchDao + */ + public void setCatchBatchDao(CatchBatchExtendDao catchBatchDao) { + this.catchBatchDao = catchBatchDao; + } } \ No newline at end of file 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-02-25 13:39:39 UTC (rev 460) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/CatchBatchPersistenceService.java 2013-02-25 14:11:54 UTC (rev 461) @@ -24,6 +24,7 @@ * #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,10 +43,21 @@ * * @param id id of the fihsing operation * @return found catchbatch + * @throws CatchBatchValidationException */ - CatchBatch getCatchBatchFromFishingOperation(String id); + CatchBatch getCatchBatchFromFishingOperation(String fishingOperationId) throws CatchBatchValidationException; /** + * Get the catchBatch from the fishing Operation id. + * + * @param id id of the fihsing operation + * @return found catchbatch + * @throws CatchBatchValidationException + */ + @Transactional(readOnly = false) + CatchBatch getCatchBatchFromFishingOperation(String fishingOperationId, boolean tryToRepair) throws CatchBatchValidationException; + + /** * Create the given CatchBatch and return it. * * @param bean catchBatch to create Modified: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/CruisePersistenceServiceImpl.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/CruisePersistenceServiceImpl.java 2013-02-25 13:39:39 UTC (rev 460) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/CruisePersistenceServiceImpl.java 2013-02-25 14:11:54 UTC (rev 461) @@ -24,8 +24,30 @@ * #L% */ +import java.sql.Timestamp; +import java.text.MessageFormat; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Set; + +import javax.annotation.Resource; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.hibernate.type.IntegerType; +import org.hibernate.type.StringType; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.dao.DataRetrievalFailureException; +import org.springframework.stereotype.Service; + import com.google.common.base.Preconditions; import com.google.common.collect.Lists; + import fr.ifremer.adagio.core.dao.administration.programStrategy.ProgramDao; import fr.ifremer.adagio.core.dao.administration.user.PersonDao; import fr.ifremer.adagio.core.dao.data.survey.fishingTrip.FishingTrip; @@ -33,6 +55,7 @@ import fr.ifremer.adagio.core.dao.data.survey.scientificCruise.ScientificCruise; import fr.ifremer.adagio.core.dao.data.survey.scientificCruise.ScientificCruiseDao; import fr.ifremer.adagio.core.dao.data.vessel.feature.physical.GearPhysicalFeatures; +import fr.ifremer.adagio.core.dao.data.vessel.feature.physical.GearPhysicalFeaturesExtendDao; import fr.ifremer.adagio.core.dao.referential.QualityFlag; import fr.ifremer.adagio.core.dao.referential.QualityFlagDao; import fr.ifremer.adagio.core.dao.referential.gear.GearDao; @@ -48,27 +71,7 @@ import fr.ifremer.tutti.persistence.entities.referential.Gear; import fr.ifremer.tutti.persistence.entities.referential.Person; import fr.ifremer.tutti.persistence.entities.referential.Vessel; -import fr.ifremer.tutti.persistence.service.measure.MeasurementPersistenceHelper; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.hibernate.type.IntegerType; -import org.hibernate.type.StringType; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.dao.DataRetrievalFailureException; -import org.springframework.stereotype.Service; -import javax.annotation.Resource; -import java.sql.Timestamp; -import java.text.MessageFormat; -import java.util.ArrayList; -import java.util.Calendar; -import java.util.Date; -import java.util.GregorianCalendar; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Set; - /** * @author tchemit <chemit@codelutin.com> * @since 0.3 @@ -119,8 +122,8 @@ @Resource(name = "gearDao") protected GearDao gearDao; - @Resource(name = "measurementPersistenceHelper") - protected MeasurementPersistenceHelper measurementHelper; + @Resource(name = "gearPhysicalFeaturesDao") + protected GearPhysicalFeaturesExtendDao gearPhysicalFeaturesDao; protected Calendar calendar = new GregorianCalendar(); @@ -500,7 +503,7 @@ // Create or update a geaPhysicalFeatures for each gears in the cruise for (int i = 0; i < source.getGear().size(); i++) { Gear gear = source.getGear().get(i); - GearPhysicalFeatures guf = measurementHelper.getGearPhysicalfeatures(fishingTrip, Integer.valueOf(gear.getId()), true); + GearPhysicalFeatures guf = gearPhysicalFeaturesDao.getGearPhysicalfeatures(fishingTrip, Integer.valueOf(gear.getId()), true); notChangedGearPhysicalFeatures.remove(guf); guf.setStartDate(fishingTrip.getDepartureDateTime()); @@ -513,9 +516,9 @@ // Trawl net (store in Gear Physical features) if (copyIfNull && source.getMultirigNumber() == null) { - measurementHelper.removeGearPhysicalMeasurement(guf, enumeration.PMFM_ID_MULTIRIG_NUMBER); + gearPhysicalFeaturesDao.removeGearPhysicalMeasurement(guf, enumeration.PMFM_ID_MULTIRIG_NUMBER); } else { - measurementHelper.setGearPhysicalMeasurement(target, guf, enumeration.PMFM_ID_MULTIRIG_NUMBER, Float.valueOf(source.getMultirigNumber()), null, null); + gearPhysicalFeaturesDao.setGearPhysicalMeasurement(target, guf, enumeration.PMFM_ID_MULTIRIG_NUMBER, Float.valueOf(source.getMultirigNumber()), null, null); } } Modified: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/FishingOperationPersistenceServiceImpl.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/FishingOperationPersistenceServiceImpl.java 2013-02-25 13:39:39 UTC (rev 460) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/FishingOperationPersistenceServiceImpl.java 2013-02-25 14:11:54 UTC (rev 461) @@ -24,9 +24,34 @@ * #L% */ +import java.io.Serializable; +import java.sql.Timestamp; +import java.text.MessageFormat; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Set; + +import javax.annotation.Resource; + +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.hibernate.FlushMode; +import org.hibernate.type.IntegerType; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.dao.DataIntegrityViolationException; +import org.springframework.dao.DataRetrievalFailureException; +import org.springframework.stereotype.Service; + 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.denormalized.DenormalizedBatch; import fr.ifremer.adagio.core.dao.data.fishingArea.FishingArea; import fr.ifremer.adagio.core.dao.data.fishingArea.FishingArea2RegulationLocation; @@ -44,6 +69,7 @@ import fr.ifremer.adagio.core.dao.data.survey.scientificCruise.ScientificCruise; import fr.ifremer.adagio.core.dao.data.survey.scientificCruise.ScientificCruiseDao; import fr.ifremer.adagio.core.dao.data.vessel.feature.physical.GearPhysicalFeatures; +import fr.ifremer.adagio.core.dao.data.vessel.feature.physical.GearPhysicalFeaturesExtendDao; import fr.ifremer.adagio.core.dao.data.vessel.feature.use.GearUseFeatures; import fr.ifremer.adagio.core.dao.data.vessel.feature.use.VesselUseFeatures; import fr.ifremer.adagio.core.dao.data.vessel.feature.use.isActive; @@ -61,34 +87,12 @@ import fr.ifremer.tutti.persistence.entities.CaracteristicMap; import fr.ifremer.tutti.persistence.entities.data.FishingOperation; import fr.ifremer.tutti.persistence.entities.referential.Caracteristic; +import fr.ifremer.tutti.persistence.entities.referential.CaracteristicQualitativeValue; import fr.ifremer.tutti.persistence.entities.referential.CaracteristicType; import fr.ifremer.tutti.persistence.entities.referential.FishingOperationLocation; import fr.ifremer.tutti.persistence.entities.referential.Person; import fr.ifremer.tutti.persistence.entities.referential.Vessel; -import fr.ifremer.tutti.persistence.service.measure.MeasurementPersistenceHelper; -import org.apache.commons.collections.CollectionUtils; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.hibernate.FlushMode; -import org.hibernate.type.IntegerType; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.dao.DataIntegrityViolationException; -import org.springframework.dao.DataRetrievalFailureException; -import org.springframework.stereotype.Service; -import javax.annotation.Resource; -import java.io.Serializable; -import java.sql.Timestamp; -import java.text.MessageFormat; -import java.util.ArrayList; -import java.util.Calendar; -import java.util.Date; -import java.util.GregorianCalendar; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Set; - /** * @author tchemit <chemit@codelutin.com> * @since 0.3 @@ -101,15 +105,15 @@ LogFactory.getLog(FishingOperationPersistenceServiceImpl.class); - @Resource(name = "measurementPersistenceHelper") - protected MeasurementPersistenceHelper measurementHelper; - @Autowired protected ReferentialPersistenceService referentialService; @Resource(name = "scientificCruiseDao") protected ScientificCruiseDao scientificCruiseDao; + @Resource(name = "gearPhysicalFeaturesDao") + protected GearPhysicalFeaturesExtendDao gearPhysicalFeaturesDao; + @Resource(name = "fishingOperationDao") protected FishingOperationDao fishingOperationDao; @@ -166,9 +170,7 @@ } if (fishingOperationNumberStr.matches("\\d+")) { fishingOperation.setFishingOperationNumber(Integer.valueOf(fishingOperationNumberStr)); - } else { - // TODO BL : invalid op.name format : log ? - } + } } // If not found, compute it using a counter (see "order by startDateTime" in HQL query) @@ -197,10 +199,7 @@ fishingOperation.setMultirigAggregation("1"); } - fishingOperations.add(fishingOperation); - // TODO BLA: code utile juste pour les tests UI - //fishingOperations.add(getFishingOperation(source[0].toString())); } return fishingOperations; } @@ -288,7 +287,6 @@ // Gear : Integer gearId = (Integer) source[colIndex++]; if (gearId != null) { - // TODO TC : activer un cache sur getGear() fr.ifremer.tutti.persistence.entities.referential.Gear gear = referentialService.getGear(gearId); result.setGear(gear); } @@ -421,7 +419,12 @@ // Haul valid ? else if (enumeration.PMFM_ID_HAUL_VALID.equals(pmfmId)) { - result.setFishingOperationValid(enumeration.QUALITATIVE_HAUL_VALID_YES.equals(qualitativeValueId)); + if (qualitativeValueId != null) { + result.setFishingOperationValid(enumeration.QUALITATIVE_HAUL_VALID_YES.equals(qualitativeValueId)); + } + else { + result.setFishingOperationValid(null); + } } // Station Number : @@ -438,7 +441,12 @@ } else if (environmentCaracteristic.getCaracteristicType() == CaracteristicType.TEXT) { value = alphanumericalValue; } else if (environmentCaracteristic.getCaracteristicType() == CaracteristicType.QUALITATIVE) { - value = qualitativeValueId; + for (CaracteristicQualitativeValue qv : environmentCaracteristic.getQualitativeValue()) { + if (qualitativeValueId == Integer.parseInt(qv.getId())) { + value = qv; + break; + } + } } environmentCaracteristics.put(environmentCaracteristic, value); } @@ -481,7 +489,12 @@ } else if (gearShootingCaracteristic.getCaracteristicType() == CaracteristicType.TEXT) { value = alphanumericalValue; } else if (gearShootingCaracteristic.getCaracteristicType() == CaracteristicType.QUALITATIVE) { - value = qualitativeValueId; + for (CaracteristicQualitativeValue qv : gearShootingCaracteristic.getQualitativeValue()) { + if (qualitativeValueId == Integer.parseInt(qv.getId())) { + value = qv; + break; + } + } } gearShootingCaracteristics.put(gearShootingCaracteristic, value); } @@ -565,7 +578,7 @@ // Retrieve entities : Gear Physical Features GearPhysicalFeatures gearPhysicalFeatures = target.getGearPhysicalFeatures(); if (gearPhysicalFeatures == null && source.getGear() != null && source.getGear().getId() != null) { - gearPhysicalFeatures = measurementHelper.getGearPhysicalfeatures(fishingTrip, Integer.valueOf(source.getGear().getId())); + gearPhysicalFeatures = gearPhysicalFeaturesDao.getGearPhysicalfeatures(fishingTrip, Integer.valueOf(source.getGear().getId())); if (gearPhysicalFeatures == null) { throw new DataIntegrityViolationException("An operation could not use a gear that is not declared in the cruise."); } @@ -607,7 +620,7 @@ // Retrieve multirig number, from Gear physical features int cruiseMultirigCount = 1; // default value if (gearPhysicalFeatures != null) { - GearPhysicalMeasurement gpmMultirigCount = measurementHelper.getGearPhysicalMeasurement(gearPhysicalFeatures, enumeration.PMFM_ID_MULTIRIG_NUMBER); + GearPhysicalMeasurement gpmMultirigCount = gearPhysicalFeaturesDao.getGearPhysicalMeasurement(gearPhysicalFeatures, enumeration.PMFM_ID_MULTIRIG_NUMBER); if (gpmMultirigCount != null && gpmMultirigCount.getNumericalValue() != null) { cruiseMultirigCount = gpmMultirigCount.getNumericalValue().intValue(); } @@ -1136,11 +1149,11 @@ } else if (caracteristic.getCaracteristicType() == CaracteristicType.NUMBER) { measurement.setNumericalValue((Float) value); } else if (caracteristic.getCaracteristicType() == CaracteristicType.QUALITATIVE) { - Integer qvId; - if (value instanceof Integer) { + Integer qvId = null; + if (value instanceof CaracteristicQualitativeValue) { + qvId = Integer.valueOf(((CaracteristicQualitativeValue) value).getId()); + } else if (value instanceof Integer) { qvId = (Integer) value; - } else { - qvId = Integer.valueOf(value.toString()); } QualitativeValue qv = load(QualitativeValueImpl.class, qvId); measurement.setQualitativeValue(qv); @@ -1159,7 +1172,7 @@ } else if (numericalValue != null) { gearUseMeasurement.setNumericalValue(numericalValue); } else if (qualitativevalueId != null) { - QualitativeValue qv = (QualitativeValue) getCurrentSession().load(QualitativeValueImpl.class, qualitativevalueId); + QualitativeValue qv = load(QualitativeValueImpl.class, qualitativevalueId); gearUseMeasurement.setQualitativeValue(qv); } Added: 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 (rev 0) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/batch/ScientificCruiseCatchBatchValidator.java 2013-02-25 14:11:54 UTC (rev 461) @@ -0,0 +1,253 @@ +package fr.ifremer.tutti.persistence.service.batch; + +import static org.nuiton.i18n.I18n._; + +import java.util.Collection; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; + +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; + +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.CatchBatchQuickFix; +import fr.ifremer.adagio.core.dao.data.batch.validator.CatchBatchValidationError; +import fr.ifremer.adagio.core.dao.data.batch.validator.CatchBatchValidator; +import fr.ifremer.adagio.core.dao.data.measure.SortingMeasurement; +import fr.ifremer.tutti.persistence.service.BatchPersistenceService; +import fr.ifremer.tutti.persistence.service.ReferentialPersistenceService; +import fr.ifremer.tutti.persistence.service.TuttiEnumerationFile; + +@Component(value = "scientificCruiseCatchBatchValidator") +public class ScientificCruiseCatchBatchValidator implements CatchBatchValidator { + + /** Logger. */ + private static final Log log = + LogFactory.getLog(ScientificCruiseCatchBatchValidator.class); + + @Autowired + protected BatchPersistenceService batchService; + + @Autowired + protected ReferentialPersistenceService referentialService; + + @Autowired + protected CatchBatchExtendDao catchBatchDao; + + @Autowired + protected TuttiEnumerationFile enumeration; + + public ScientificCruiseCatchBatchValidator() { + } + + @Override + public boolean isEnable(fr.ifremer.adagio.core.dao.data.batch.CatchBatch catchBatch) { + // Apply validation only on catch batch for fishingOperation + return (catchBatch.getFishingOperation() != null); + } + + @Override + public List<CatchBatchValidationError> validate(fr.ifremer.adagio.core.dao.data.batch.CatchBatch catchBatch) { + List<CatchBatchValidationError> errors = Lists.newArrayList(); + validate(catchBatch.getChildBatchs(), errors, 1); + return errors; + } + + // ------------------------------------------------------------------------// + // -- Internal methods --// + // ------------------------------------------------------------------------// + + protected void validate(Collection<Batch> batchs, List<CatchBatchValidationError> errors, int treeLevel) { + + // Vrac + SortingBatch vracBatch = catchBatchDao.getSortingBatch(batchs, + "pmfmId", enumeration.PMFM_ID_SORTED_UNSORTED, enumeration.QUALITATIVE_VRAC_ID); + if (vracBatch == null) { + addError(errors, "tutti.persistence.batch.validation.vracNotFound", null); + } else { + // Vrac > Species + SortingBatch speciesBatch = catchBatchDao.getSortingBatch(vracBatch.getChildBatchs(), + "pmfmId", enumeration.PMFM_ID_SORTING_TYPE, + enumeration.QUALITATIVE_ID_SORTING_TYPE_SPECIES); + if (speciesBatch == null) { + addError(errors, "tutti.persistence.batch.validation.vracSpeciesNotFound", null); + } else { + // Vrac > Species > Inert + SortingBatch inertBatch = catchBatchDao.getSortingBatch(speciesBatch.getChildBatchs(), + "referenceTaxonId", enumeration.REFERENCE_TAXON_ID_INERT); + if (inertBatch == null) { + addWarning(errors, "tutti.persistence.batch.validation.vracSpeciesInertNotFound"); + } + + // Vrac > Species > Alive no itemized + SortingBatch livingNotItemizedBatch = catchBatchDao.getSortingBatch(speciesBatch.getChildBatchs(), + "referenceTaxonId", enumeration.REFERENCE_TAXON_ID_LIFE); + if (livingNotItemizedBatch == null) { + addWarning(errors, "tutti.persistence.batch.validation.vracSpeciesLifeNotFound"); + } + } + + // TODO : Benthos, Plancton... + } + + // Hors Vrac + SortingBatch horsVracBatch = catchBatchDao.getSortingBatch(batchs, + "pmfmId", enumeration.PMFM_ID_SORTED_UNSORTED, + enumeration.QUALITATIVE_HORS_VRAC_ID); + if (horsVracBatch == null) { + addWarning(errors, "tutti.persistence.batch.validation.horsVracSpeciesNotFound"); + } else { + // Hors Vrac > Species + SortingBatch speciesBatch = catchBatchDao.getSortingBatch(horsVracBatch.getChildBatchs(), + "pmfmId", enumeration.PMFM_ID_SORTING_TYPE, + enumeration.QUALITATIVE_ID_SORTING_TYPE_SPECIES); + if (speciesBatch == null) { + addWarning(errors, "tutti.persistence.batch.validation.horsVracSpeciesNotFound"); + } + + // TODO : Benthos, Plancton... + } + + // Unsorted + SortingBatch unsortedBatch = catchBatchDao.getSortingBatch(batchs, + "pmfmId", enumeration.PMFM_ID_SORTED_UNSORTED, + enumeration.QUALITATIVE_UNSORTED_ID); + if (unsortedBatch == null) { + addWarning(errors, "tutti.persistence.batch.validation.unsortedNotFound"); + } + + // May be, the tree is an old structure + if (vracBatch == null && horsVracBatch == null && unsortedBatch == null) { + boolean allBatchHasSpecies = true; + boolean allBatchHasVrac = true; + boolean someBatchHasSortingMeasurement = false; + for (Iterator<Batch> iterator = batchs.iterator(); iterator.hasNext();) { + SortingBatch batch = (SortingBatch) iterator.next(); + if (allBatchHasSpecies && batch.getReferenceTaxon() == null) { + allBatchHasSpecies = false; + break; + } + if (allBatchHasVrac && batch.getSortingMeasurements() != null) { + if (batch.getSortingMeasurements().size() > 1) { + allBatchHasVrac = false; + someBatchHasSortingMeasurement = true; + } + else if (batch.getSortingMeasurements().size() == 1) { + someBatchHasSortingMeasurement = true; + SortingMeasurement sm = batch.getSortingMeasurements().iterator().next(); + if (enumeration.PMFM_ID_SORTED_UNSORTED.equals(sm.getPmfm().getId()) == false + || sm.getQualitativeValue() == null + || enumeration.QUALITATIVE_VRAC_ID.equals(sm.getQualitativeValue().getId()) == false) { + allBatchHasVrac = false; + } + } + } + } + if (allBatchHasSpecies && !someBatchHasSortingMeasurement) { + errors.clear(); + addError(errors, "tutti.persistence.batch.validation.onlySpeciesBatchStructure", new CatchBatchQuickFix() { + @Override + public CatchBatch repair(CatchBatch catchBatch) { + return ScientificCruiseCatchBatchValidator.this.repairHistoricalData(catchBatch, false); + } + }); + } + else if (allBatchHasSpecies && someBatchHasSortingMeasurement && allBatchHasVrac) { + errors.clear(); + addError(errors, "tutti.persistence.batch.validation.onlySpeciesBatchStructure", new CatchBatchQuickFix() { + @Override + public CatchBatch repair(CatchBatch catchBatch) { + return ScientificCruiseCatchBatchValidator.this.repairHistoricalData(catchBatch, true); + } + }); + } + } + } + + /** + * Repair tree batch, when only species have been found under the cacth batch. + * This append typically with CGFS cruise. + * @param catchBatch + * @return + */ + protected CatchBatch repairHistoricalData(CatchBatch catchBatch, boolean batchHasVracSortingMeasurement) { + Preconditions.checkNotNull(catchBatch); + Preconditions.checkNotNull(catchBatch.getId()); + Preconditions.checkNotNull(catchBatch.getFishingOperation()); + Preconditions.checkNotNull(catchBatch.getFishingOperation().getId()); + + // Copy catch batch children + Collection<Batch> speciesBatchChilds = Sets.newHashSet(); + speciesBatchChilds.addAll(catchBatch.getChildBatchs()); + catchBatch.setChildBatchs(new HashSet<Batch>()); + + // Clean catch batch + batchService.cleanEntity(catchBatch, true); + + SortingBatch speciesBatch = catchBatchDao.getSortingBatch(catchBatch.getChildBatchs(), + "pmfmId", enumeration.PMFM_ID_SORTED_UNSORTED, enumeration.QUALITATIVE_VRAC_ID, + "pmfmId", enumeration.PMFM_ID_SORTING_TYPE, enumeration.QUALITATIVE_ID_SORTING_TYPE_SPECIES); + if (speciesBatch == null) { + throw new DataIntegrityViolationException("Invalid batch tree format. Could not retrieve 'Vrac>Species' batch."); + } + + // For all species batch : set the batch 'Vrac>Species' as new parent + for (Iterator<Batch> iterator = speciesBatchChilds.iterator(); iterator.hasNext();) { + SortingBatch batch = (SortingBatch) iterator.next(); + batch.setParentBatch(speciesBatch); + + if (batchHasVracSortingMeasurement) { + // Remove redundant 'Vrac' sorting measurement + SortingMeasurement sm = catchBatchDao.getSortingMeasurement(batch, enumeration.PMFM_ID_SORTED_UNSORTED, null, false); + batch.getSortingMeasurements().remove(sm); + } + + batch.setExhaustiveInventory(Boolean.TRUE); + + // Add an offset into the rank order, because of special batchs 'Biota' (=live) and 'Inert' + short rankOrder = (short)(batch.getRankOrder().shortValue() + 2); + batch.setRankOrder(rankOrder); + + // Reset comments if set to "taxon" + if (batch.getComments() != null && "taxon".equals(batch.getComments())) { + batch.setComments(null); + } + + speciesBatch.getChildBatchs().add(batch); + } + + // Save mofidifications : + catchBatchDao.update(catchBatch); + + return catchBatch; + } + + protected void addError(List<CatchBatchValidationError> errors, String messageKey, CatchBatchQuickFix quickFix) { + CatchBatchValidationError error = new CatchBatchValidationError( + messageKey, + _(messageKey), + CatchBatchValidationError.GRAVITY_ERROR); + error.setQuickFixes(Lists.newArrayList(quickFix)); + errors.add(error); + } + + protected void addWarning(List<CatchBatchValidationError> errors, String messageKey) { + CatchBatchValidationError error = new CatchBatchValidationError( + messageKey, + _(messageKey), + CatchBatchValidationError.GRAVITY_WARNING); + errors.add(error); + } + +} 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-02-25 13:39:39 UTC (rev 460) +++ trunk/tutti-persistence/src/main/resources/i18n/tutti-persistence_fr_FR.properties 2013-02-25 14:11:54 UTC (rev 461) @@ -12,7 +12,11 @@ tutti.option.persistence.jdbc.url.description=URL de connexion à la base de données tutti.option.persistence.jdbc.username.description=Login de l'utilisateur pour se connecter à la base de données tutti.option.persistence.protocol.directory.description=Répertoire où sont conservés les protocoles -tutti.persistence.batch.validation.onlyTaxonButPmfmFound=Batch [id\=%s] should not have any sorting measurement (should only have a reference taxon), but found pmfm [id\=%s]. -tutti.persistence.batch.validation.pmfmNotFound=Batch [id\=%s] should have a sorting measurement with a pmfm id in %s or a length pmfm, but found pmfm [id\=%s]. -tutti.persistence.batch.validation.referenceTaxonNotFound=Batch [id\=%s] should have a reference taxon. +tutti.persistence.batch.validation.horsVracNotFound=Lot 'Hors Vrac' non trouvé, ou configuré avec d'autres critères de classement. +tutti.persistence.batch.validation.horsVracSpeciesNotFound=Batch 'Hors Vrac > Espèces' non trouvé ou configuré avec d'autres critères de classement. +tutti.persistence.batch.validation.unsortedNotFound=Batch 'Hors Vrac > Espèces' non trouvé ou configuré avec d'autres critères de classement. +tutti.persistence.batch.validation.vracNotFound=Lot 'Vrac' non trouvé, ou configuré avec d'autres critères de classement. +tutti.persistence.batch.validation.vracSpeciesInertNotFound=Lot 'Vrac > Espèces > Inerte' non trouvé, ou configuré avec d'autres critères de classement. +tutti.persistence.batch.validation.vracSpeciesLifeNotFound=Lot 'Vrac > Espèces > Biota' non trouvé, ou configuré avec d'autres critères de classement. +tutti.persistence.batch.validation.vracSpeciesNotFound=Lot 'Vrac > Espèces' non trouvé, ou configuré avec d'autres critères de classement. tutti.persistence.config=Configuration de la persistence de Tutti Modified: trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/BatchPersistenceServiceWriteTest.java =================================================================== --- trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/BatchPersistenceServiceWriteTest.java 2013-02-25 13:39:39 UTC (rev 460) +++ trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/BatchPersistenceServiceWriteTest.java 2013-02-25 14:11:54 UTC (rev 461) @@ -24,8 +24,28 @@ * #L% */ +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.List; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.ClassRule; +import org.junit.Test; +import org.springframework.dao.DataRetrievalFailureException; + import com.google.common.collect.Lists; import com.google.common.collect.Maps; + +import fr.ifremer.adagio.core.dao.data.batch.validator.CatchBatchValidationException; import fr.ifremer.tutti.persistence.DatabaseResource; import fr.ifremer.tutti.persistence.entities.data.CatchBatch; import fr.ifremer.tutti.persistence.entities.data.Cruise; @@ -37,586 +57,587 @@ import fr.ifremer.tutti.persistence.entities.referential.CaracteristicQualitativeValue; import fr.ifremer.tutti.persistence.entities.referential.CaracteristicType; import fr.ifremer.tutti.persistence.entities.referential.Species; -import org.junit.Before; -import org.junit.ClassRule; -import org.junit.Test; -import org.springframework.dao.DataRetrievalFailureException; -import java.util.Calendar; -import java.util.Date; -import java.util.GregorianCalendar; -import java.util.List; -import java.util.Map; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - /** * To test {@link CatchBatchPersistenceService} for write operation. - * + * * @author tchemit <chemit@codelutin.com> * @since 1.0 */ public class BatchPersistenceServiceWriteTest { - @ClassRule - public static final DatabaseResource dbResource = DatabaseResource.writeDb(); + @ClassRule + public static final DatabaseResource dbResource = DatabaseResource.writeDb(); - protected BatchPersistenceService service; + protected BatchPersistenceService service; - protected CruisePersistenceService cruiseService; + protected CruisePersistenceService cruiseService; - protected FishingOperationPersistenceService fishingOperationService; + protected FishingOperationPersistenceService fishingOperationService; - protected CatchBatchPersistenceService catchBatchService; + protected CatchBatchPersistenceService catchBatchService; - protected ReferentialPersistenceService referentialService; + protected ReferentialPersistenceService referentialService; + + /* + * Entities prepared in setUp() : + * */ + protected Cruise cruise; - protected Cruise cruise; + protected FishingOperation fishingOperationNoCatchBatch; - protected FishingOperation fishingOperationNoCatchBatch; + protected FishingOperation fishingOperationWithEmptyBatch; - protected FishingOperation fishingOperationWithEmptyBatch; + protected CatchBatch catchBacth; - protected CatchBatch catchBacth; + protected List<Species> species; - protected List<Species> species; + protected Caracteristic sortedUnsortedPMFM; - protected Caracteristic sortedUnsortedPMFM; + protected CaracteristicQualitativeValue horsVracQualitativeValue; - protected CaracteristicQualitativeValue horsVracQualitativeValue; + protected CaracteristicQualitativeValue vracQualitativeValue; - protected CaracteristicQualitativeValue vracQualitativeValue; + protected Caracteristic maturityPMFM; - protected Caracteristic maturityPMFM; + protected CaracteristicQualitativeValue firstMaturityQualitativeValue; - protected CaracteristicQualitativeValue firstMaturityQualitativeValue; + protected Caracteristic sexPMFM; - protected Caracteristic sexPMFM; + protected CaracteristicQualitativeValue maleQualitativeValue; - protected CaracteristicQualitativeValue maleQualitativeValue; + protected CaracteristicQualitativeValue femaleQualitativeValue; - protected CaracteristicQualitativeValue femaleQualitativeValue; + protected CaracteristicQualitativeValue unkQualitativeValue; - protected CaracteristicQualitativeValue unkQualitativeValue; + protected Caracteristic frequencyPMFM; - protected Caracteristic frequencyPMFM; + @Before + public void setUp() throws Exception { + service = TuttiPersistenceServiceLocator.getBatchPersistenceService(); + cruiseService = TuttiPersistenceServiceLocator.getCruisePersistenceService(); + fishingOperationService = TuttiPersistenceServiceLocator.getFishingOperationPersistenceService(); + catchBatchService = TuttiPersistenceServiceLocator.getCatchBatchPersistenceService(); + referentialService = TuttiPersistenceServiceLocator.getReferentialPersistenceService(); - @Before - public void setUp() throws Exception { - service = TuttiPersistenceServiceLocator.getBatchPersistenceService(); - cruiseService = TuttiPersistenceServiceLocator.getCruisePersistenceService(); - fishingOperationService = TuttiPersistenceServiceLocator.getFishingOperationPersistenceService(); - catchBatchService = TuttiPersistenceServiceLocator.getCatchBatchPersistenceService(); - referentialService = TuttiPersistenceServiceLocator.getReferentialPersistenceService(); + species = referentialService.getAllSpecies(); + assertNotNull(species); + assertTrue(species.size() > 2); - species = referentialService.getAllSpecies(); - assertNotNull(species); - assertTrue(species.size() > 2); + cruise = cruiseService.getCruise(dbResource.getFixtures().cruiseId()); + cruise.setId(null); + Calendar calendar = new GregorianCalendar(); + cruise.setBeginDate(calendar.getTime()); + cruise.setYear(calendar.get(Calendar.YEAR)); + calendar.add(Calendar.MONTH, 1); // add one month + cruise.setEndDate(calendar.getTime()); + cruise = cruiseService.createCruise(cruise); - cruise = cruiseService.getCruise(dbResource.getFixtures().cruiseId()); - cruise.setId(null); - Calendar calendar = new GregorianCalendar(); - cruise.setBeginDate(calendar.getTime()); - cruise.setYear(calendar.get(Calendar.YEAR)); - calendar.add(Calendar.MONTH, 1); // add one month - cruise.setEndDate(calendar.getTime()); - cruise = cruiseService.createCruise(cruise); + // Create a first operation, with no cacth batch : to test CatchBatch insert/update : + List<FishingOperation> fishingOperations = fishingOperationService.getAllFishingOperation(dbResource.getFixtures().cruiseId()); + assertNotNull(fishingOperations); + assertTrue(fishingOperations.size() > 0); + fishingOperationNoCatchBatch = fishingOperations.get(0); + fishingOperationNoCatchBatch = fishingOperationService.getFishingOperation(fishingOperationNoCatchBatch.getId()); + fishingOperationNoCatchBatch.setId(null); + fishingOperationNoCatchBatch.setCruise(cruise); + calendar.setTime(new Date()); + calendar.set(Calendar.HOUR_OF_DAY, 1); + calendar.set(Calendar.MILLISECOND, 0); + fishingOperationNoCatchBatch.setGearShootingStartDate(calendar.getTime()); + calendar.setTime(new Date()); + calendar.set(Calendar.HOUR_OF_DAY, 10); + calendar.set(Calendar.MILLISECOND, 0); + fishingOperationNoCatchBatch.setGearShootingEndDate(calendar.getTime()); + fishingOperationNoCatchBatch = fishingOperationService.createFishingOperation(fishingOperationNoCatchBatch); - // Create a first operation, with no cacth batch : to test CatchBatch insert/update : - List<FishingOperation> fishingOperations = fishingOperationService.getAllFishingOperation(dbResource.getFixtures().cruiseId()); - assertNotNull(fishingOperations); - assertTrue(fishingOperations.size() > 0); - fishingOperationNoCatchBatch = fishingOperations.get(0); - fishingOperationNoCatchBatch = fishingOperationService.getFishingOperation(fishingOperationNoCatchBatch.getId()); - fishingOperationNoCatchBatch.setId(null); - fishingOperationNoCatchBatch.setCruise(cruise); - calendar.setTime(new Date()); - calendar.set(Calendar.HOUR_OF_DAY, 1); - calendar.set(Calendar.MILLISECOND, 0); - fishingOperationNoCatchBatch.setGearShootingStartDate(calendar.getTime()); - calendar.setTime(new Date()); - calendar.set(Calendar.HOUR_OF_DAY, 10); - calendar.set(Calendar.MILLISECOND, 0); - fishingOperationNoCatchBatch.setGearShootingEndDate(calendar.getTime()); - fishingOperationNoCatchBatch = fishingOperationService.createFishingOperation(fishingOperationNoCatchBatch); + // Create a second operation, with no cacth batch : to test CatchBatch insert/update : + fishingOperationWithEmptyBatch = fishingOperations.get(1); + fishingOperationWithEmptyBatch = fishingOperationService.getFishingOperation(fishingOperationWithEmptyBatch.getId()); + fishingOperationWithEmptyBatch.setId(null); + fishingOperationWithEmptyBatch.setCruise(cruise); + calendar.setTime(new Date()); + calendar.set(Calendar.HOUR_OF_DAY, 11); + calendar.set(Calendar.MILLISECOND, 0); + fishingOperationWithEmptyBatch.setGearShootingStartDate(calendar.getTime()); + calendar.setTime(new Date()); + calendar.set(Calendar.HOUR_OF_DAY, 12); + calendar.set(Calendar.MILLISECOND, 0); + fishingOperationWithEmptyBatch.setGearShootingEndDate(calendar.getTime()); + fishingOperationWithEmptyBatch = fishingOperationService.createFishingOperation(fishingOperationWithEmptyBatch); - // Create a second operation, with no cacth batch : to test CatchBatch insert/update : - fishingOperationWithEmptyBatch = fishingOperations.get(1); - fishingOperationWithEmptyBatch = fishingOperationService.getFishingOperation(fishingOperationWithEmptyBatch.getId()); - fishingOperationWithEmptyBatch.setId(null); - fishingOperationWithEmptyBatch.setCruise(cruise); - calendar.setTime(new Date()); - calendar.set(Calendar.HOUR_OF_DAY, 11); - calendar.set(Calendar.MILLISECOND, 0); - fishingOperationWithEmptyBatch.setGearShootingStartDate(calendar.getTime()); - calendar.setTime(new Date()); - calendar.set(Calendar.HOUR_OF_DAY, 12); - calendar.set(Calendar.MILLISECOND, 0); - fishingOperationWithEmptyBatch.setGearShootingEndDate(calendar.getTime()); - fishingOperationWithEmptyBatch = fishingOperationService.createFishingOperation(fishingOperationWithEmptyBatch); + catchBacth = new CatchBatch(); + catchBacth.setFishingOperation(fishingOperationWithEmptyBatch); + catchBacth = catchBatchService.createCatchBatch(catchBacth); - catchBacth = new CatchBatch(); - catchBacth.setFishingOperation(fishingOperationWithEmptyBatch); - catchBacth = catchBatchService.createCatchBatch(catchBacth); + sortedUnsortedPMFM = referentialService.getSortedUnsortedCaracteristic(); + horsVracQualitativeValue = sortedUnsortedPMFM.getQualitativeValue(0); + vracQualitativeValue = sortedUnsortedPMFM.getQualitativeValue(1); + maturityPMFM = referentialService.getMaturityCaracteristic(); + firstMaturityQualitativeValue = maturityPMFM.getQualitativeValue(0); + sexPMFM = referentialService.getSexCaracteristic(); + maleQualitativeValue = sexPMFM.getQualitativeValue(1); + femaleQualitativeValue = sexPMFM.getQualitativeValue(2); + unkQualitativeValue = sexPMFM.getQualitativeValue(3); - sortedUnsortedPMFM = referentialService.getSortedUnsortedCaracteristic(); - horsVracQualitativeValue = sortedUnsortedPMFM.getQualitativeValue(0); - vracQualitativeValue = sortedUnsortedPMFM.getQualitativeValue(1); - maturityPMFM = referentialService.getMaturityCaracteristic(); - firstMaturityQualitativeValue = maturityPMFM.getQualitativeValue(0); - sexPMFM = referentialService.getSexCaracteristic(); - maleQualitativeValue = sexPMFM.getQualitativeValue(1); - femaleQualitativeValue = sexPMFM.getQualitativeValue(2); - unkQualitativeValue = sexPMFM.getQualitativeValue(3); + List<Caracteristic> cara = referentialService.getAllCaracteristic(); + for (Caracteristic caracteristic : cara) { + if (caracteristic.getCaracteristicType() == CaracteristicType.NUMBER + && caracteristic.getPrecision() != null + && caracteristic.getPrecision() == 0.5f) { + frequencyPMFM = caracteristic; + break; + } + } + assertNotNull("no numerical PMFM with a precision has been found. Could not define a PMFM for batch frequencies.", frequencyPMFM); - List<Caracteristic> cara = referentialService.getAllCaracteristic(); - for (Caracteristic caracteristic : cara) { - if (caracteristic.getCaracteristicType() == CaracteristicType.NUMBER - && caracteristic.getPrecision() != null - && caracteristic.getPrecision() == 0.5f) { - frequencyPMFM = caracteristic; - break; - } - } - assertNotNull("no numerical PMFM with a precision has been found. Could not define a PMFM for batch frequencies.", frequencyPMFM); + } - } + @Test + public void createAndSaveCatchBatch() throws Exception { + CatchBatch catchBatch; - @Test - public void createAndSaveCatchBatch() throws Exception { - CatchBatch catchBatch; + catchBatch = new CatchBatch(); + catchBatch.setFishingOperation(fishingOperationNoCatchBatch); - catchBatch = new CatchBatch(); - catchBatch.setFishingOperation(fishingOperationNoCatchBatch); + // ----------------------------------------------------------------------------- + // 1. Test with only mandatory properties + // ----------------------------------------------------------------------------- - // ----------------------------------------------------------------------------- - // 1. Test with only mandatory properties - // ----------------------------------------------------------------------------- + // Create and reload (test round trip) + assertCreateAndReloadCatchBatch(catchBatch, fishingOperationNoCatchBatch.getId()); - // Create and reload (test round trip) - assertCreateAndReloadCatchBatch(catchBatch, fishingOperationNoCatchBatch.getId()); + // ----------------------------------------------------------------------------- + // 2. Test with all properties + // ----------------------------------------------------------------------------- + catchBatch.setId(null); + // total weight : 100kg + catchBatch.setCatchTotalWeight(75f); + // Vrac : + { + // note : poids trie par la balance tremis (thalassa) (init par pupitri) + catchBatch.setCatchTotalSortedTremisWeight(50f); + // note : poids vrac caroussel (thalassa) (init par pupitri) (vrac trie) ou bien "poids trié fournie par la + // table de tri (Sum(Si) + catchBatch.setCatchTotalSortedCarousselWeight(45f); - // ----------------------------------------------------------------------------- - // 2. Test with all properties - // ----------------------------------------------------------------------------- - catchBatch.setId(null); - // total weight : 100kg - catchBatch.setCatchTotalWeight(75f); - // Vrac : - { - // note : poids trie par la balance tremis (thalassa) (init par pupitri) - catchBatch.setCatchTotalSortedTremisWeight(50f); - // note : poids vrac caroussel (thalassa) (init par pupitri) (vrac trie) ou bien "poids trié fournie par la table de tri (Sum(Si) - catchBatch.setCatchTotalSortedCarousselWeight(45f); + // Species + { + catchBatch.setSpeciesTotalSortedWeight(12f); + catchBatch.setSpeciesTotalSampleSortedWeight(8f); + catchBatch.setSpeciesTotalLivingNotItemizedWeight(0.2f); + catchBatch.setSpeciesTotalInertWeight(0.1f); + } + } + // Hors Vrac : 10kg + { + catchBatch.setCatchTotalUnsortedWeight(10f); - // Species - { - catchBatch.setSpeciesTotalSortedWeight(12f); - catchBatch.setSpeciesTotalSampleSortedWeight(8f); - catchBatch.setSpeciesTotalLivingNotItemizedWeight(0.2f); - catchBatch.setSpeciesTotalInertWeight(0.1f); - } - } - // Hors Vrac : 10kg - { - catchBatch.setCatchTotalUnsortedWeight(10f); + // Species + { + catchBatch.setSpeciesTotalUnsortedWeight(10f); + } + } + // Rejet : 15kg + catchBatch.setCatchTotalRejectedWeight(15f); - // Species - { - catchBatch.setSpeciesTotalUnsortedWeight(10f); - } - } - // Rejet : 15kg - catchBatch.setCatchTotalRejectedWeight(15f); + // Create and reload (test round trip) + assertCreateAndReloadCatchBatch(catchBatch, fishingOperationNoCatchBatch.getId()); - // Create and reload (test round trip) - assertCreateAndReloadCatchBatch(catchBatch, fishingOperationNoCatchBatch.getId()); + // ----------------------------------------------------------------------------- + // 2. Test save after modification + // ----------------------------------------------------------------------------- + catchBatch.setCatchTotalSortedTremisWeight(null); + catchBatch.setCatchTotalSortedCarousselWeight(null); + catchBatch.setSpeciesTotalSortedWeight(null); + catchBatch.setSpeciesTotalSampleSortedWeight(null); + catchBatch.setCatchTotalUnsortedWeight(null); + catchBatch.setSpeciesTotalUnsortedWeight(null); - // ----------------------------------------------------------------------------- - // 2. Test save after modification - // ----------------------------------------------------------------------------- - catchBatch.setCatchTotalSortedTremisWeight(null); - catchBatch.setCatchTotalSortedCarousselWeight(null); - catchBatch.setSpeciesTotalSortedWeight(null); - catchBatch.setSpeciesTotalSampleSortedWeight(null); - catchBatch.setCatchTotalUnsortedWeight(null); - catchBatch.setSpeciesTotalUnsortedWeight(null); + assertSaveAndReloadCatchBatch(catchBatch, fishingOperationNoCatchBatch.getId()); + } - assertSaveAndReloadCatchBatch(catchBatch, fishingOperationNoCatchBatch.getId()); - } + @Test + public void createAndSaveSpeciesBatchAndFrequencies() { + SpeciesBatch esp1Batch = null; + SpeciesBatch esp2Batch = null; + SpeciesBatch frequenciesParentBatch = null; + SpeciesBatch batch = null; + Species taxon1 = species.get(0); + Species taxon2 = species.get(1); - @Test - public void createAndSaveSpeciesBatchAndFrequencies() { - SpeciesBatch esp1Batch = null; - SpeciesBatch esp2Batch = null; - SpeciesBatch frequenciesParentBatch = null; - SpeciesBatch batch = null; - Species taxon1 = species.get(0); - Species taxon2 = species.get(1); + // ----------------------------------------------------------------------------- + // 1. Test with only mandatory properties + // ----------------------------------------------------------------------------- + // batch : "ESP1 - Vrac/5" + batch = new SpeciesBatch(); + batch.setParentBatch(null); + batch.setFishingOperation(fishingOperationNoCatchBatch); + batch.setSpecies(taxon1); + batch.setSampleCategoryType(SampleCategoryEnum.sortedUnsorted); + batch.setSampleCategoryValue(vracQualitativeValue); + batch.setSampleCategoryWeight(5f); - // ----------------------------------------------------------------------------- - // 1. Test with only mandatory properties - // ----------------------------------------------------------------------------- - // batch : "ESP1 - Vrac/5" - batch = new SpeciesBatch(); - batch.setParentBatch(null); - batch.setFishingOperation(fishingOperationNoCatchBatch); - batch.setSpecies(taxon1); - batch.setSampleCategoryType(SampleCategoryEnum.sortedUnsorted); - batch.setSampleCategoryValue(vracQualitativeValue); - batch.setSampleCategoryWeight(5f); + assertCreateAndReloadSpeciesBatch(batch, null); - assertCreateAndReloadSpeciesBatch(batch, null); + // Save ESP1 batch + esp1Batch = batch; - // Save ESP1 batch - esp1Batch = batch; + // ----------------------------------------------------------------------------- + // 2. Test child "Male/2" + // ----------------------------------------------------------------------------- + // Batch : ESP1 - Vrac/5 Male/2 ss-ech/1 Nombre/7 + batch = new SpeciesBatch(); + batch.setId(null); + batch.setParentBatch(esp1Batch); + batch.setSpecies(taxon1); + batch.setComment("ESP1 - Vrac/5 Male/2 ss-ech/1 Nombre/7"); + batch.setSampleCategoryType(SampleCategoryEnum.sex); + batch.setSampleCategoryValue(maleQualitativeValue); + batch.setSampleCategoryWeight(2f); + batch.setWeight(1f); + batch.setNumber(7); - // ----------------------------------------------------------------------------- - // 2. Test child "Male/2" - // ----------------------------------------------------------------------------- - // Batch : ESP1 - Vrac/5 Male/2 ss-ech/1 Nombre/7 - batch = new SpeciesBatch(); - batch.setId(null); - batch.setParentBatch(esp1Batch); - batch.setSpecies(taxon1); - batch.setComment("ESP1 - Vrac/5 Male/2 ss-ech/1 Nombre/7"); - batch.setSampleCategoryType(SampleCategoryEnum.sex); - batch.setSampleCategoryValue(maleQualitativeValue); - batch.setSampleCategoryWeight(2f); - batch.setWeight(1f); - batch.setNumber(7); + assertCreateAndReloadSpeciesBatch(batch, batch.getParentBatch().getId()); - assertCreateAndReloadSpeciesBatch(batch, batch.getParentBatch().getId()); + // ----------------------------------------------------------------------------- + // 3. Test child "Female/2" + // ----------------------------------------------------------------------------- + // Batch : ESP1 - Vrac/5 Female/3 Nombre/14 + batch = new SpeciesBatch(); + batch.setId(null); + batch.setParentBatch(esp1Batch); + batch.setSpecies(taxon1); + batch.setComment("ESP1 - Vrac/5 Female/3 Nombre/14"); + batch.setSampleCategoryType(SampleCategoryEnum.sex); + batch.setSampleCategoryValue(femaleQualitativeValue); + batch.setSampleCategoryWeight(3f); + batch.setWeight(null); + batch.setNumber(14); - // ----------------------------------------------------------------------------- - // 3. Test child "Female/2" - // ----------------------------------------------------------------------------- - // Batch : ESP1 - Vrac/5 Female/3 Nombre/14 - batch = new SpeciesBatch(); - batch.setId(null); - batch.setParentBatch(esp1Batch); - batch.setSpecies(taxon1); - batch.setComment("ESP1 - Vrac/5 Female/3 Nombre/14"); - batch.setSampleCategoryType(SampleCategoryEnum.sex); - batch.setSampleCategoryValue(femaleQualitativeValue); - batch.setSampleCategoryWeight(3f); - batch.setWeight(null); - batch.setNumber(14); + assertCreateAndReloadSpeciesBatch(batch, batch.getParentBatch().getId()); - assertCreateAndReloadSpeciesBatch(batch, batch.getParentBatch().getId()); + // ----------------------------------------------------------------------------- + // 4. Test : ESP2 - Vrac/7 + // \- ESP2 - Vrac/7 UNK/2 ss-ech/1 Nombre/11 + // ----------------------------------------------------------------------------- + // batch : "ESP2 - Vrac/7 " + batch = new SpeciesBatch(); + batch.setParentBatch(null); + batch.setSpecies(taxon2); + batch.setSampleCategoryType(SampleCategoryEnum.sortedUnsorted); + batch.setSampleCategoryValue(vracQualitativeValue); + batch.setSampleCategoryWeight(7f); - // ----------------------------------------------------------------------------- - // 4. Test : ESP2 - Vrac/7 - // \- ESP2 - Vrac/7 UNK/2 ss-ech/1 Nombre/11 - // ----------------------------------------------------------------------------- - // batch : "ESP2 - Vrac/7 " - batch = new SpeciesBatch(); - batch.setParentBatch(null); - batch.setSpecies(taxon2); - batch.setSampleCategoryType(SampleCategoryEnum.sortedUnsorted); - batch.setSampleCategoryValue(vracQualitativeValue); - batch.setSampleCategoryWeight(7f); + assertCreateAndReloadSpeciesBatch(batch, null); + esp2Batch = batch; - assertCreateAndReloadSpeciesBatch(batch, null); - esp2Batch = batch; + // Batch : ESP2 - Vrac/7 UNK/2 ss-ech/1 Nombre/11 + batch = new SpeciesBatch(); + batch.setId(null); + batch.setParentBatch(esp2Batch); + batch.setSpecies(taxon2); + batch.setComment("ESP2 - Vrac/7 UNK/2 ss-ech/1 Nombre/11"); + batch.setSampleCategoryType(SampleCategoryEnum.maturity); + batch.setSampleCategoryValue(firstMaturityQualitativeValue); + batch.setSampleCategoryWeight(2f); + batch.setWeight(1f); + batch.setNumber(11); - // Batch : ESP2 - Vrac/7 UNK/2 ss-ech/1 Nombre/11 - batch = new SpeciesBatch(); - batch.setId(null); - batch.setParentBatch(esp2Batch); - batch.setSpecies(taxon2); - batch.setComment("ESP2 - Vrac/7 UNK/2 ss-ech/1 Nombre/11"); - batch.setSampleCategoryType(SampleCategoryEnum.maturity); - batch.setSampleCategoryValue(firstMaturityQualitativeValue); - batch.setSampleCategoryWeight(2f); - batch.setWeight(1f); - batch.setNumber(11); + assertCreateAndReloadSpeciesBatch(batch, batch.getParentBatch().getId()); - assertCreateAndReloadSpeciesBatch(batch, batch.getParentBatch().getId()); + // ----------------------------------------------------------------------------- + // 5. Test save after modifications + // ----------------------------------------------------------------------------- + // Batch : ESP2 - Vrac/7 UNK/1.75 ss-ech/1.11 Nombre/99 + batch.setComment("ESP2 - Vrac/7 UNK/1.75 ss-ech/1.11 Nombre/99"); + batch.setSampleCategoryType(SampleCategoryEnum.sex); + batch.setSampleCategoryValue(unkQualitativeValue); + batch.setSampleCategoryWeight(1.75f); + batch.setWeight(1.11f); + batch.setFishingOperation(fishingOperationWithEmptyBatch); + batch.setNumber(99); - // ----------------------------------------------------------------------------- - // 5. Test save after modifications - // ----------------------------------------------------------------------------- - // Batch : ESP2 - Vrac/7 UNK/1.75 ss-ech/1.11 Nombre/99 - batch.setComment("ESP2 - Vrac/7 UNK/1.75 ss-ech/1.11 Nombre/99"); - batch.setSampleCategoryType(SampleCategoryEnum.sex); - batch.setSampleCategoryValue(unkQualitativeValue); - batch.setSampleCategoryWeight(1.75f); - batch.setWeight(1.11f); - batch.setFishingOperation(fishingOperationWithEmptyBatch); - batch.setNumber(99); + // Save and reload, then check + SpeciesBatch savedBatch = service.saveSpeciesBatch(batch); + assertSpeciesBatch(savedBatch, batch, false); + SpeciesBatch reloadedBatch = getSpeciesBatch(fishingOperationWithEmptyBatch.getId(), savedBatch.getId()); + assertSpeciesBatch(savedBatch, reloadedBatch, true); - // Save and reload, then check - SpeciesBatch savedBatch = service.saveSpeciesBatch(batch); - assertSpeciesBatch(savedBatch, batch, false); - SpeciesBatch reloadedBatch = getSpeciesBatch(fishingOperationWithEmptyBatch.getId(), savedBatch.getId()); - assertSpeciesBatch(savedBatch, reloadedBatch, true); + // Save batch for later + frequenciesParentBatch = batch; - // Save batch for later - frequenciesParentBatch = batch; + // ----------------------------------------------------------------------------- + // 6. Test change species + // ----------------------------------------------------------------------------- + esp2Batch.setSpecies(taxon1); + service.changeSpeciesBatchSpecies(esp2Batch.getId(), taxon1); + assertSpeciesBatch(savedBatch, batch, false); + reloadedBatch = getSpeciesBatch(fishingOperationWithEmptyBatch.getId(), esp2Batch.getId()); + assertSpeciesBatch(esp2Batch, reloadedBatch, true); - // ----------------------------------------------------------------------------- - // 6. Test change species - // ----------------------------------------------------------------------------- - esp2Batch.setSpecies(taxon1); - service.changeSpeciesBatchSpecies(esp2Batch.getId(), taxon1); - assertSpeciesBatch(savedBatch, batch, false); - reloadedBatch = getSpeciesBatch(fishingOperationWithEmptyBatch.getId(), esp2Batch.getId()); - assertSpeciesBatch(esp2Batch, reloadedBatch, true); + // ----------------------------------------------------------------------------- + // 7. Test get all root species + // ----------------------------------------------------------------------------- + List<SpeciesBatch> rootSpeciesBatch = service.getAllRootSpeciesBatch(fishingOperationWithEmptyBatch.getId()); + assertNotNull(rootSpeciesBatch); + assertEquals(2, rootSpeciesBatch.size()); + assertNotNull(rootSpeciesBatch.get(0).getChildBatchs()); + assertTrue(rootSpeciesBatch.get(0).getChildBatchs().size() > 0); + assertNotNull(rootSpeciesBatch.get(1).getChildBatchs()); + assertTrue(rootSpeciesBatch.get(1).getChildBatchs().size() > 0); - // ----------------------------------------------------------------------------- - // 7. Test get all root species - // ----------------------------------------------------------------------------- - List<SpeciesBatch> rootSpeciesBatch = service.getAllRootSpeciesBatch(fishingOperationWithEmptyBatch.getId()); - assertNotNull(rootSpeciesBatch); - assertEquals(2, rootSpeciesBatch.size()); - assertNotNull(rootSpeciesBatch.get(0).getChildBatchs()); - assertTrue(rootSpeciesBatch.get(0).getChildBatchs().size() > 0); - assertNotNull(rootSpeciesBatch.get(1).getChildBatchs()); - assertTrue(rootSpeciesBatch.get(1).getChildBatchs().size() > 0); + // ----------------------------------------------------------------------------- + // 8. Test batch frequency creation + // ----------------------------------------------------------------------------- - // ----------------------------------------------------------------------------- - // 8. Test batch frequency creation - // ----------------------------------------------------------------------------- + List<SpeciesBatchFrequency> frequencies = Lists.newArrayList(); + float lengthStep = 0.5f; + for (float length = lengthStep; length < lengthStep * 20; length += lengthStep) { + SpeciesBatchFrequency frequency = new SpeciesBatchFrequency(); + frequency.setLengthStep(length); + frequency.setNumber((int) (length * 2)); + frequency.setWeight(0.01f * length * 2); + frequency.setLengthStepCaracteristic(frequencyPMFM); + frequency.setBatch(frequenciesParentBatch); + frequency.setComment("comments"); + frequencies.add(frequency); + } + List<SpeciesBatchFrequency> createdFrequencies = assertCreateAndReloadSpeciesBatchFrequency(frequencies, frequenciesParentBatch.getId()); - List<SpeciesBatchFrequency> frequencies = Lists.newArrayList(); - float lengthStep = 0.5f; - for (float length = lengthStep; length < lengthStep * 20; length += lengthStep) { - SpeciesBatchFrequency frequency = new SpeciesBatchFrequency(); - frequency.setLengthStep(length); - frequency.setNumber((int) (length * 2)); - frequency.setWeight(0.01f * length * 2); - frequency.setLengthStepCaracteristic(frequencyPMFM); - frequency.setBatch(frequenciesParentBatch); - frequency.setComment("comments"); - frequencies.add(frequency); - } - List<SpeciesBatchFrequency> createdFrequencies = assertCreateAndReloadSpeciesBatchFrequency(frequencies, frequenciesParentBatch.getId()); + // ----------------------------------------------------------------------------- + // 9. Test batch frequency update + // ----------------------------------------------------------------------------- + // Update some batchs (1cm, 2cm, etc) + for (SpeciesBatchFrequency speciesBatchFrequency : createdFrequencies) { + float length = speciesBatchFrequency.getLengthStep(); + if ((float) (int) length == length) { + speciesBatchFrequency.setNumber(12); + speciesBatchFrequency.setComment(null); + } + } + // And remove the last item (should be deleted in DB) + createdFrequencies.remove(createdFrequencies.size() - 1); + List<SpeciesBatchFrequency> savedFrequencies = service.saveSpeciesBatchFrequency(frequenciesParentBatch.getId(), frequencies); + assertBatchFrequencies(createdFrequencies, savedFrequencies, true); + } - // ----------------------------------------------------------------------------- - // 9. Test batch frequency update - // ----------------------------------------------------------------------------- - // Update some batchs (1cm, 2cm, etc) - for (SpeciesBatchFrequency speciesBatchFrequency : createdFrequencies) { - float length = speciesBatchFrequency.getLengthStep(); - if ((float) (int) length == length) { - speciesBatchFrequency.setNumber(12); - speciesBatchFrequency.setComment(null); - } - } - // And remove the last item (should be deleted in DB) - createdFrequencies.remove(createdFrequencies.size() - 1); - List<SpeciesBatchFrequency> savedFrequencies = service.saveSpeciesBatchFrequency(frequenciesParentBatch.getId(), frequencies); - assertBatchFrequencies(createdFrequencies, savedFrequencies, true); - } + @Test + public void deleteSpeciesBatch(/* String id */) { + SpeciesBatch esp1Batch; + SpeciesBatch batch; + Species taxon1 = species.get(0); - @Test - public void deleteSpeciesBatch(/*String id*/) { - SpeciesBatch esp1Batch; - SpeciesBatch batch; - Species taxon1 = species.get(0); + // ----------------------------------------------------------------------------- + // 1. Create two batchs (parent + child), then remove the parent batch + // ----------------------------------------------------------------------------- + // batch : ESP1 Vrac/5 + batch = new SpeciesBatch(); + batch.setParentBatch(null); + batch.setFishingOperation(fishingOperationWithEmptyBatch); + batch.setSpecies(taxon1); + batch.setSampleCategoryType(SampleCategoryEnum.sortedUnsorted); + batch.setSampleCategoryValue(vracQualitativeValue); + batch.setSampleCategoryWeight(5f); + assertCreateAndReloadSpeciesBatch(batch, null); + esp1Batch = batch; - // ----------------------------------------------------------------------------- - // 1. Create two batchs (parent + child), then remove the parent batch - // ----------------------------------------------------------------------------- - // batch : ESP1 Vrac/5 - batch = new SpeciesBatch(); - batch.setParentBatch(null); - batch.setFishingOperation(fishingOperationWithEmptyBatch); - batch.setSpecies(taxon1); - batch.setSampleCategoryType(SampleCategoryEnum.sortedUnsorted); - batch.setSampleCategoryValue(vracQualitativeValue); - batch.setSampleCategoryWeight(5f); - assertCreateAndReloadSpeciesBatch(batch, null); - esp1Batch = batch; + // batch : ESP1 Vrac/5 Male/2 + batch = new SpeciesBatch(); + batch.setParentBatch(esp1Batch); + batch.setFishingOperation(fishingOperationWithEmptyBatch); + batch.setSpecies(taxon1); + batch.setSampleCategoryType(SampleCategoryEnum.sex); + batch.setSampleCategoryValue(maleQualitativeValue); + batch.setSampleCategoryWeight(2f); + assertCreateAndReloadSpeciesBatch(batch, esp1Batch.getId()); - // batch : ESP1 Vrac/5 Male/2 - batch = new SpeciesBatch(); - batch.setParentBatch(esp1Batch); - batch.setFishingOperation(fishingOperationWithEmptyBatch); - batch.setSpecies(taxon1); - batch.setSampleCategoryType(SampleCategoryEnum.sex); - batch.setSampleCategoryValue(maleQualitativeValue); - batch.setSampleCategoryWeight(2f); - assertCreateAndReloadSpeciesBatch(batch, esp1Batch.getId()); + // Try to remove + service.deleteSpeciesBatch(esp1Batch.getId()); - // Try to remove - service.deleteSpeciesBatch(esp1Batch.getId()); + // Check if remove + try { + batch = getSpeciesBatch(fishingOperationWithEmptyBatch.getId(), esp1Batch.getId()); + assertNull(batch); + } catch (DataRetrievalFailureException drfe) { + assertNotNull(drfe); + } + } - // Check if remove - try { - batch = getSpeciesBatch(fishingOperationWithEmptyBatch.getId(), esp1Batch.getId()); - assertNull(batch); - } catch (DataRetrievalFailureException drfe) { - assertNotNull(drfe); - } - } + protected void assertCreateAndReloadSpeciesBatch(SpeciesBatch batch, String parentBatchId) { + batch.setFishingOperation(fishingOperationWithEmptyBatch); - protected void assertCreateAndReloadSpeciesBatch(SpeciesBatch batch, String parentBatchId) { - batch.setFishingOperation(fishingOperationWithEmptyBatch); + // Create batch + SpeciesBatch createdBatch = service.createSpeciesBatch(batch, parentBatchId); + assertSpeciesBatch(batch, createdBatch, false); - // Create batch - SpeciesBatch createdBatch = service.createSpeciesBatch(batch, parentBatchId); - assertSpeciesBatch(batch, createdBatch, false); + // then reload (for round trip check) + SpeciesBatch reloadedBatch = getSpeciesBatch(fishingOperationWithEmptyBatch.getId(), createdBatch.getId()); + if (parentBatchId == null) { + assertNull(reloadedBatch.getParentBatch()); + } else { + assertNotNull(reloadedBatch.getParentBatch()); + assertEquals(parentBatchId, reloadedBatch.getParentBatch().getId()); + } + assertSpeciesBatch(createdBatch, reloadedBatch, false); - // then reload (for round trip check) - SpeciesBatch reloadedBatch = getSpeciesBatch(fishingOperationWithEmptyBatch.getId(), createdBatch.getId()); - if (parentBatchId == null) { - assertNull(reloadedBatch.getParentBatch()); - } else { - assertNotNull(reloadedBatch.getParentBatch()); - assertEquals(parentBatchId, reloadedBatch.getParentBatch().getId()); - } - assertSpeciesBatch(createdBatch, reloadedBatch, false); + batch.setId(createdBatch.getId()); + } - batch.setId(createdBatch.getId()); - } + protected void assertCreateAndReloadCatchBatch(CatchBatch catchBatch, String fishingOperationId) { + CatchBatch createdCatchBatch = service.createCatchBatch(catchBatch); + assertNotNull(createdCatchBatch); + assertNotNull(createdCatchBatch.getId()); + assertCatchBatch(catchBatch, createdCatchBatch, false); - protected void assertCreateAndReloadCatchBatch(CatchBatch catchBatch, String fishingOperationId) { - CatchBatch createdCatchBatch = service.createCatchBatch(catchBatch); - assertNotNull(createdCatchBatch); - assertNotNull(createdCatchBatch.getId()); - assertCatchBatch(catchBatch, createdCatchBatch, false); + CatchBatch reloadedCatchBatch = null; + try { + reloadedCatchBatch = service.getCatchBatchFromFishingOperation(fishingOperationId); + } catch (CatchBatchValidationException e) { + Assert.fail(e.getMessage()); + } + assertCatchBatch(createdCatchBatch, reloadedCatchBatch, true); - CatchBatch reloadedCatchBatch = service.getCatchBatchFromFishingOperation(fishingOperationId); - assertCatchBatch(createdCatchBatch, reloadedCatchBatch, true); + catchBatch.setId(createdCatchBatch.getId()); + } - catchBatch.setId(createdCatchBatch.getId()); - } + protected void assertSaveAndReloadCatchBatch(CatchBatch catchBatch, String fishingOperationId) { + CatchBatch savedCatchBatch = service.saveCatchBatch(catchBatch); + assertNotNull(savedCatchBatch); + assertNotNull(savedCatchBatch.getId()); + assertCatchBatch(catchBatch, savedCatchBatch, false); - protected void assertSaveAndReloadCatchBatch(CatchBatch catchBatch, String fishingOperationId) { - CatchBatch savedCatchBatch = service.saveCatchBatch(catchBatch); - assertNotNull(savedCatchBatch); - assertNotNull(savedCatchBatch.getId()); - assertCatchBatch(catchBatch, savedCatchBatch, false); + CatchBatch reloadedCatchBatch = null; + try { + reloadedCatchBatch = service.getCatchBatchFromFishingOperation(fishingOperationId); + } catch (CatchBatchValidationException e) { + Assert.fail(e.getMessage()); + } + assertCatchBatch(savedCatchBatch, reloadedCatchBatch, true); + } - CatchBatch reloadedCatchBatch = service.getCatchBatchFromFishingOperation(fishingOperationId); - assertCatchBatch(savedCatchBatch, reloadedCatchBatch, true); - } + protected void assertCatchBatch(CatchBatch expectedCatchBatch, CatchBatch actualCatchBatch, boolean assertIdEquals) { + if (expectedCatchBatch == null) { + assertNull(actualCatchBatch); + return; + } - protected void assertCatchBatch(CatchBatch expectedCatchBatch, CatchBatch actualCatchBatch, boolean assertIdEquals) { - if (expectedCatchBatch == null) { - assertNull(actualCatchBatch); - return; - } + assertNotNull(actualCatchBatch); + if (assertIdEquals) { + assertEquals(expectedCatchBatch.getId(), actualCatchBatch.getId()); + } + assertEquals(expectedCatchBatch.getCatchTotalWeight(), actualCatchBatch.getCatchTotalWeight()); + assertEquals(expectedCatchBatch.getCatchTotalSortedCarousselWeight(), actualCatchBatch.getCatchTotalSortedCarousselWeight()); + assertEquals(expectedCatchBatch.getCatchTotalSortedTremisWeight(), actualCatchBatch.getCatchTotalSortedTremisWeight()); + assertEquals(expectedCatchBatch.getCatchTotalUnsortedWeight(), actualCatchBatch.getCatchTotalUnsortedWeight()); - assertNotNull(actualCatchBatch); - if (assertIdEquals) { - assertEquals(expectedCatchBatch.getId(), actualCatchBatch.getId()); - } - assertEquals(expectedCatchBatch.getCatchTotalWeight(), actualCatchBatch.getCatchTotalWeight()); - assertEquals(expectedCatchBatch.getCatchTotalSortedCarousselWeight(), actualCatchBatch.getCatchTotalSortedCarousselWeight()); - assertEquals(expectedCatchBatch.getCatchTotalSortedTremisWeight(), actualCatchBatch.getCatchTotalSortedTremisWeight()); - assertEquals(expectedCatchBatch.getCatchTotalUnsortedWeight(), actualCatchBatch.getCatchTotalUnsortedWeight()); + assertEquals(expectedCatchBatch.getSpeciesTotalSampleSortedWeight(), actualCatchBatch.getSpeciesTotalSampleSortedWeight()); + assertEquals(expectedCatchBatch.getSpeciesTotalSortedWeight(), actualCatchBatch.getSpeciesTotalSortedWeight()); + assertEquals(expectedCatchBatch.getSpeciesTotalUnsortedWeight(), actualCatchBatch.getSpeciesTotalUnsortedWeight()); + assertEquals(expectedCatchBatch.getSpeciesTotalInertWeight(), actualCatchBatch.getSpeciesTotalInertWeight()); + assertEquals(expectedCatchBatch.getSpeciesTotalLivingNotItemizedWeight(), actualCatchBatch.getSpeciesTotalLivingNotItemizedWeight()); + } - assertEquals(expectedCatchBatch.getSpeciesTotalSampleSortedWeight(), actualCatchBatch.getSpeciesTotalSampleSortedWeight()); - assertEquals(expectedCatchBatch.getSpeciesTotalSortedWeight(), actualCatchBatch.getSpeciesTotalSortedWeight()); - assertEquals(expectedCatchBatch.getSpeciesTotalUnsortedWeight(), actualCatchBatch.getSpeciesTotalUnsortedWeight()); - assertEquals(expectedCatchBatch.getSpeciesTotalInertWeight(), actualCatchBatch.getSpeciesTotalInertWeight()); - assertEquals(expectedCatchBatch.getSpeciesTotalLivingNotItemizedWeight(), actualCatchBatch.getSpeciesTotalLivingNotItemizedWeight()); - } + protected void assertSpeciesBatch(SpeciesBatch expectedBatch, SpeciesBatch actualBatch, boolean assertIdEquals) { + assertNotNull(actualBatch); + assertNotNull(actualBatch.getId()); + if (assertIdEquals && expectedBatch.getId() != null) { + assertEquals(expectedBatch.getId(), actualBatch.getId()); + } + assertEquals(expectedBatch.getWeight(), actualBatch.getWeight()); + assertEquals(expectedBatch.getSampleCategoryType(), actualBatch.getSampleCategoryType()); + if (expectedBatch.getSampleCategoryValue() != null && expectedBatch.getSampleCategoryValue() instanceof CaracteristicQualitativeValue) { + assertNotNull("Bad sampleCategoryValue : expected <" + ((CaracteristicQualitativeValue) expectedBatch.getSampleCategoryValue()).getId() + + "> but was <null>", + actualBatch.getSampleCategoryValue()); + assertEquals( + ((CaracteristicQualitativeValue) expectedBatch.getSampleCategoryValue()).getId(), + ((CaracteristicQualitativeValue) actualBatch.getSampleCategoryValue()).getId()); + } else { + assertEquals(expectedBatch.getSampleCategoryValue(), actualBatch.getSampleCategoryValue()); + } + assertEquals(expectedBatch.getSampleCategoryWeight(), actualBatch.getSampleCategoryWeight()); + assertEquals(expectedBatch.getNumber(), actualBatch.getNumber()); + assertEquals(expectedBatch.getComment(), actualBatch.getComment()); - protected void assertSpeciesBatch(SpeciesBatch expectedBatch, SpeciesBatch actualBatch, boolean assertIdEquals) { - assertNotNull(actualBatch); - assertNotNull(actualBatch.getId()); - if (assertIdEquals && expectedBatch.getId() != null) { - assertEquals(expectedBatch.getId(), actualBatch.getId()); - } - assertEquals(expectedBatch.getWeight(), actualBatch.getWeight()); - assertEquals(expectedBatch.getSampleCategoryType(), actualBatch.getSampleCategoryType()); - if (expectedBatch.getSampleCategoryValue() != null && expectedBatch.getSampleCategoryValue() instanceof CaracteristicQualitativeValue) { - assertNotNull("Bad sampleCategoryValue : expected <" + ((CaracteristicQualitativeValue) expectedBatch.getSampleCategoryValue()).getId() + "> but was <null>", - actualBatch.getSampleCategoryValue()); - assertEquals( - ((CaracteristicQualitativeValue) expectedBatch.getSampleCategoryValue()).getId(), - ((CaracteristicQualitativeValue) actualBatch.getSampleCategoryValue()).getId()); - } else { - assertEquals(expectedBatch.getSampleCategoryValue(), actualBatch.getSampleCategoryValue()); - } - assertEquals(expectedBatch.getSampleCategoryWeight(), actualBatch.getSampleCategoryWeight()); - assertEquals(expectedBatch.getNumber(), actualBatch.getNumber()); - assertEquals(expectedBatch.getComment(), actualBatch.getComment()); + // Check species only if Vrac/HorsVrac or if batch has been load throw getAllxxx method + // (Because getSpeciesBatch(id) could not always retrieve the species) + if (expectedBatch.getSpecies() != null && ( + expectedBatch.getSampleCategoryType() == SampleCategoryEnum.sortedUnsorted + || actualBatch.getSpecies() != null)) { + assertNotNull(actualBatch.getSpecies()); + assertEquals(expectedBatch.getSpecies().getId(), actualBatch.getSpecies().getId()); + } + } - // Check species only if Vrac/HorsVrac or if batch has been load throw getAllxxx method - // (Because getSpeciesBatch(id) could not always retrieve the species) - if (expectedBatch.getSpecies() != null && ( - expectedBatch.getSampleCategoryType() == SampleCategoryEnum.sortedUnsorted - || actualBatch.getSpecies() != null)) { - assertNotNull(actualBatch.getSpecies()); - assertEquals(expectedBatch.getSpecies().getId(), actualBatch.getSpecies().getId()); - } - } + protected List<SpeciesBatchFrequency> assertCreateAndReloadSpeciesBatchFrequency(List<SpeciesBatchFrequency> frequencies, String parentBatchId) { - protected List<SpeciesBatchFrequency> assertCreateAndReloadSpeciesBatchFrequency(List<SpeciesBatchFrequency> frequencies, String parentBatchId) { + // Create batch + List<SpeciesBatchFrequency> createdFrequencies = service.saveSpeciesBatchFrequency(parentBatchId, frequencies); + assertBatchFrequencies(frequencies, createdFrequencies, false); - // Create batch - List<SpeciesBatchFrequency> createdFrequencies = service.saveSpeciesBatchFrequency(parentBatchId, frequencies); - assertBatchFrequencies(frequencies, createdFrequencies, false); + // then reload (for round trip check) + List<SpeciesBatchFrequency> reloadedFrequencies = service.getAllSpeciesBatchFrequency(parentBatchId); + assertBatchFrequencies(createdFrequencies, reloadedFrequencies, true); - // then reload (for round trip check) - List<SpeciesBatchFrequency> reloadedFrequencies = service.getAllSpeciesBatchFrequency(parentBatchId); - assertBatchFrequencies(createdFrequencies, reloadedFrequencies, true); + return createdFrequencies; + } - return createdFrequencies; - } + protected void assertBatchFrequencies(List<SpeciesBatchFrequency> expectedFrequencies, List<SpeciesBatchFrequency> actualFrequencies, + boolean assertIdEquals) { + assertNotNull(actualFrequencies); + assertEquals(expectedFrequencies.size(), actualFrequencies.size()); - protected void assertBatchFrequencies(List<SpeciesBatchFrequency> expectedFrequencies, List<SpeciesBatchFrequency> actualFrequencies, boolean assertIdEquals) { - assertNotNull(actualFrequencies); - assertEquals(expectedFrequencies.size(), actualFrequencies.size()); + // Store actual batches into a map, using the length as key + Map<Float, SpeciesBatchFrequency> expectedLengthMap = Maps.newHashMap(); + for (SpeciesBatchFrequency speciesBatchFrequency : expectedFrequencies) { + expectedLengthMap.put(speciesBatchFrequency.getLengthStep(), speciesBatchFrequency); + } - // Store actual batches into a map, using the length as key - Map<Float, SpeciesBatchFrequency> expectedLengthMap = Maps.newHashMap(); - for (SpeciesBatchFrequency speciesBatchFrequency : expectedFrequencies) { - expectedLengthMap.put(speciesBatchFrequency.getLengthStep(), speciesBatchFrequency); - } + // Store expected batches into a map, using the length as key + Map<Float, SpeciesBatchFrequency> actualLengthMap = Maps.newHashMap(); + for (SpeciesBatchFrequency speciesBatchFrequency : actualFrequencies) { + assertFalse("Duplicate lengthStep found in batchFrequencies, for length=" + speciesBatchFrequency.getLengthStep(), + actualLengthMap.containsKey(speciesBatchFrequency.getLengthStep())); + actualLengthMap.put(speciesBatchFrequency.getLengthStep(), speciesBatchFrequency); + assertNotNull(speciesBatchFrequency.getId()); + } - // Store expected batches into a map, using the length as key - Map<Float, SpeciesBatchFrequency> actualLengthMap = Maps.newHashMap(); - for (SpeciesBatchFrequency speciesBatchFrequency : actualFrequencies) { - assertFalse("Duplicate lengthStep found in batchFrequencies, for length=" + speciesBatchFrequency.getLengthStep(), actualLengthMap.containsKey(speciesBatchFrequency.getLengthStep())); - actualLengthMap.put(speciesBatchFrequency.getLengthStep(), speciesBatchFrequency); - assertNotNull(speciesBatchFrequency.getId()); - } + for (Float lengthStep : expectedLengthMap.keySet()) { + SpeciesBatchFrequency expectedBatchFrequency = expectedLengthMap.get(lengthStep); + SpeciesBatchFrequency actualBatchFrequency = actualLengthMap.get(lengthStep); + if (assertIdEquals) { + assertEquals(expectedBatchFrequency.getId(), actualBatchFrequency.getId()); + } + assertNotNull(expectedBatchFrequency.getLengthStepCaracteristic()); + assertEquals(expectedBatchFrequency.getLengthStepCaracteristic().getId(), actualBatchFrequency.getLengthStepCaracteristic().getId()); + assertEquals(expectedBatchFrequency.getNumber(), actualBatchFrequency.getNumber()); + assertEquals(expectedBatchFrequency.getWeight(), actualBatchFrequency.getWeight()); + assertEquals(expectedBatchFrequency.getComment(), actualBatchFrequency.getComment()); + // assertNotNull(expectedBatchFrequency.getBatch()); + // assertEquals(expectedBatchFrequency.getBatch().getId(), actualBatchFrequency.getBatch().getId()); + } + } - for (Float lengthStep : expectedLengthMap.keySet()) { - SpeciesBatchFrequency expectedBatchFrequency = expectedLengthMap.get(lengthStep); - SpeciesBatchFrequency actualBatchFrequency = actualLengthMap.get(lengthStep); - if (assertIdEquals) { - assertEquals(expectedBatchFrequency.getId(), actualBatchFrequency.getId()); - } - assertNotNull(expectedBatchFrequency.getLengthStepCaracteristic()); - assertEquals(expectedBatchFrequency.getLengthStepCaracteristic().getId(), actualBatchFrequency.getLengthStepCaracteristic().getId()); - assertEquals(expectedBatchFrequency.getNumber(), actualBatchFrequency.getNumber()); - assertEquals(expectedBatchFrequency.getWeight(), actualBatchFrequency.getWeight()); - assertEquals(expectedBatchFrequency.getComment(), actualBatchFrequency.getComment()); - //assertNotNull(expectedBatchFrequency.getBatch()); - //assertEquals(expectedBatchFrequency.getBatch().getId(), actualBatchFrequency.getBatch().getId()); - } - } + protected SpeciesBatch getSpeciesBatch(String fishingOperationId, String speciesBatchId) { + return getSpeciesBatch(speciesBatchId, service.getAllRootSpeciesBatch(fishingOperationId)); + } - protected SpeciesBatch getSpeciesBatch(String fishingOperationId, String speciesBatchId) { - return getSpeciesBatch(speciesBatchId, service.getAllRootSpeciesBatch(fishingOperationId)); - } - - protected SpeciesBatch getSpeciesBatch(String speciesBatchId, List<SpeciesBatch> speciesBatchs) { - if (speciesBatchs == null) { - return null; - } - for (SpeciesBatch speciesBatch : speciesBatchs) { - if (speciesBatchId.equals(speciesBatch.getId())) { - return speciesBatch; - } - if (speciesBatch.getChildBatchs() != null) { - speciesBatch = getSpeciesBatch(speciesBatchId, speciesBatch.getChildBatchs()); - if (speciesBatch != null) { - return speciesBatch; - } - } - } - return null; - } + protected SpeciesBatch getSpeciesBatch(String speciesBatchId, List<SpeciesBatch> speciesBatchs) { + if (speciesBatchs == null) { + return null; + } + for (SpeciesBatch speciesBatch : speciesBatchs) { + if (speciesBatchId.equals(speciesBatch.getId())) { + return speciesBatch; + } + if (speciesBatch.getChildBatchs() != null) { + speciesBatch = getSpeciesBatch(speciesBatchId, speciesBatch.getChildBatchs()); + if (speciesBatch != null) { + return speciesBatch; + } + } + } + return null; + } } Added: trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/batch/CatchBatchValidatorReadTest.java =================================================================== --- trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/batch/CatchBatchValidatorReadTest.java (rev 0) +++ trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/batch/CatchBatchValidatorReadTest.java 2013-02-25 14:11:54 UTC (rev 461) @@ -0,0 +1,118 @@ +package fr.ifremer.tutti.persistence.service.batch; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.util.Collection; +import java.util.List; + +import org.jmock.Expectations; +import org.jmock.Mockery; +import org.jmock.api.Invocation; +import org.jmock.integration.junit4.JUnit4Mockery; +import org.jmock.lib.action.CustomAction; +import org.junit.Assert; +import org.junit.Before; +import org.junit.ClassRule; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; + +import fr.ifremer.adagio.core.dao.data.batch.CatchBatchExtendDao; +import fr.ifremer.adagio.core.dao.data.batch.validator.CatchBatchValidationException; +import fr.ifremer.adagio.core.service.ServiceLocator; +import fr.ifremer.tutti.persistence.DatabaseResource; +import fr.ifremer.tutti.persistence.entities.data.CatchBatch; +import fr.ifremer.tutti.persistence.entities.data.FishingOperation; +import fr.ifremer.tutti.persistence.service.BatchPersistenceService; +import fr.ifremer.tutti.persistence.service.CatchBatchPersistenceService; +import fr.ifremer.tutti.persistence.service.CruisePersistenceService; +import fr.ifremer.tutti.persistence.service.FishingOperationPersistenceService; +import fr.ifremer.tutti.persistence.service.ReferentialPersistenceService; +import fr.ifremer.tutti.persistence.service.TuttiPersistenceServiceLocator; + +public class CatchBatchValidatorReadTest { + + @ClassRule + public static final DatabaseResource dbResource = DatabaseResource.writeDb(); + + protected BatchPersistenceService service; + + protected CruisePersistenceService cruiseService; + + protected FishingOperationPersistenceService fishingOperationService; + + protected CatchBatchPersistenceService catchBatchService; + + protected ReferentialPersistenceService referentialService; + + Mockery context ; + + CatchBatchExtendDao catchBatchExtendDaoMock; + + String fishingOperationId = null; + + CatchBatchExtendDao catchBatchDao; + + @Before + public void setUp() throws Exception { + context = new JUnit4Mockery(); + catchBatchExtendDaoMock = context.mock(CatchBatchExtendDao.class); + catchBatchDao=ServiceLocator.instance().getService("catchBatchDao", CatchBatchExtendDao.class); + + service = TuttiPersistenceServiceLocator.getBatchPersistenceService(); + cruiseService = TuttiPersistenceServiceLocator.getCruisePersistenceService(); + fishingOperationService = TuttiPersistenceServiceLocator.getFishingOperationPersistenceService(); + catchBatchService = TuttiPersistenceServiceLocator.getCatchBatchPersistenceService(); + referentialService = TuttiPersistenceServiceLocator.getReferentialPersistenceService(); + + List<FishingOperation> fishingOperations = fishingOperationService.getAllFishingOperation(dbResource.getFixtures().cruiseId()); + assertNotNull(fishingOperations); + assertTrue(fishingOperations.size() > 0); + fishingOperationId = fishingOperations.get(0).getId(); + } + + @Test + public void getInvalidCatchBatch() { + + // Full load operation + try { + CatchBatch catchBatch = service.getCatchBatchFromFishingOperation(fishingOperationId); + Assert.fail("Historical batch tree must not be valid"); + } catch (CatchBatchValidationException e) { + assertNotNull(e); + } + } + + @Test + public void getInvalidCatchBatchButTryRepair() throws CatchBatchValidationException { + + // Replace catchBatchDao with a mock : + //service.setCatchBatchDao(catchBatchExtendDaoMock); + + context.checking(new Expectations() {{ + // Assert update() is call once + //oneOf (catchBatchExtendDaoMock).update(with(any(fr.ifremer.adagio.core.dao.data.batch.CatchBatch.class))); + + // Delegate other methods to the real DAO + allowing(catchBatchExtendDaoMock); + will(new CustomAction("call delegated catchBatchDao") { + @Override + public Object invoke(Invocation invocation) throws Throwable { + return invocation.applyTo(catchBatchDao); + } + }); + }}); + + CatchBatch catchBatch = null; + try { + catchBatch = service.getCatchBatchFromFishingOperation(fishingOperationId, true/*tryToRepair*/); + assertNotNull(catchBatch); + } catch (CatchBatchValidationException e) { + Assert.fail("Historical batch tree must have been repaired"); + } + context.assertIsSatisfied(); + + // Restore original dao + //service.setCatchBatchDao(catchBatchDao); + } +} Modified: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/MainUIHandler.java =================================================================== --- trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/MainUIHandler.java 2013-02-25 13:39:39 UTC (rev 460) +++ trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/MainUIHandler.java 2013-02-25 14:11:54 UTC (rev 461) @@ -476,7 +476,9 @@ Cruise cruise; cruise = persistenceService.getCruise(cruiseId); - title += _("tutti.title.selectedCruise", cruise.getName()); + if (cruise != null) { + title += _("tutti.title.selectedCruise", cruise.getName()); + } } title += " / ";