Author: kmorin Date: 2013-04-03 19:29:52 +0200 (Wed, 03 Apr 2013) New Revision: 722 Url: http://forge.codelutin.com/projects/tutti/repository/revisions/722 Log: create service to compute the weights Added: trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/ trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/TuttiWeightComputingService.java Modified: trunk/tutti-service/src/main/resources/i18n/tutti-service_en_GB.properties trunk/tutti-service/src/main/resources/i18n/tutti-service_fr_FR.properties Added: trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/TuttiWeightComputingService.java =================================================================== --- trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/TuttiWeightComputingService.java (rev 0) +++ trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/TuttiWeightComputingService.java 2013-04-03 17:29:52 UTC (rev 722) @@ -0,0 +1,330 @@ +package fr.ifremer.tutti.service.catches; + +import fr.ifremer.tutti.TuttiBusinessException; +import fr.ifremer.tutti.persistence.entities.data.*; +import fr.ifremer.tutti.persistence.entities.referential.CaracteristicQualitativeValue; +import fr.ifremer.tutti.service.AbstractTuttiService; +import fr.ifremer.tutti.service.PersistenceService; +import fr.ifremer.tutti.service.TuttiServiceContext; +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import javax.swing.*; +import java.util.List; + +import static org.nuiton.i18n.I18n._; + +/** + * @author kmorin <kmorin@codelutin.com> + * @since 1.3 + */ +public class TuttiWeightComputingService extends AbstractTuttiService { + + private static final Log log = + LogFactory.getLog(TuttiWeightComputingService.class); + + protected PersistenceService persistenceService; + + @Override + public void setServiceContext(TuttiServiceContext context) { + super.setServiceContext(context); + persistenceService = getService(PersistenceService.class); + } + + public void computeCatchBatchWeights(CatchBatch catchBatch, + BatchContainer<SpeciesBatch> rootSpeciesBatch, + BatchContainer<SpeciesBatch> rootBenthosBatch) { + + // Species + Float speciesTotalComputedSortedWeight = 0f; + Float speciesTotalComputedUnsortedWeight = 0f; + + for (int i = 0; i < rootSpeciesBatch.getChildren().size(); i++) { + SpeciesBatch row = rootSpeciesBatch.getChildren().get(i); + Float weight = row.getSampleCategoryWeight(); + if (weight == null) { + weight = row.getSampleCategoryComputedWeight(); + } + if (weight == null) { + return; + } + + CaracteristicQualitativeValue value = (CaracteristicQualitativeValue) row.getSampleCategoryValue(); + if (SortedUnsortedEnum.SORTED.matchValue(value)) { + speciesTotalComputedSortedWeight += weight; + } else { + speciesTotalComputedUnsortedWeight += weight; + } + } + + Number inertWeight = catchBatch.getSpeciesTotalInertWeight(); + if (inertWeight != null) { + speciesTotalComputedSortedWeight += inertWeight.floatValue(); + } else { + catchBatch.setSpeciesTotalInertComputedWeight(0f); + } + + Number livingNotItemizedWeight = catchBatch.getSpeciesTotalLivingNotItemizedWeight(); + if (livingNotItemizedWeight != null) { + speciesTotalComputedSortedWeight += livingNotItemizedWeight.floatValue(); + } else { + catchBatch.setSpeciesTotalLivingNotItemizedComputedWeight(0f); + } + + catchBatch.setSpeciesTotalSampleSortedComputedWeight(speciesTotalComputedSortedWeight); + + Float speciesTotalSortedWeight = catchBatch.getSpeciesTotalSortedWeight(); + if (speciesTotalSortedWeight == null) { + speciesTotalSortedWeight = speciesTotalComputedSortedWeight; + catchBatch.setSpeciesTotalSortedComputedWeight(speciesTotalSortedWeight); + + } else if (speciesTotalSortedWeight < speciesTotalComputedSortedWeight) { + throw new TuttiBusinessException(_("tutti.service.catches.computeWeights.error.incoherentSpeciesTotalSorted")); + } + catchBatch.setSpeciesTotalUnsortedComputedWeight(speciesTotalComputedUnsortedWeight); + + if (speciesTotalSortedWeight == null) { + speciesTotalSortedWeight = catchBatch.getSpeciesTotalSortedComputedWeight(); + } + Float speciesTotalWeight = speciesTotalComputedUnsortedWeight + speciesTotalSortedWeight; + catchBatch.setSpeciesTotalComputedWeight(speciesTotalWeight); + + // Benthos + Float benthosTotalComputedSortedWeight = 0f; + Float benthosTotalComputedUnsortedWeight = 0f; + + for (int i = 0; i < rootBenthosBatch.getChildren().size(); i++) { + SpeciesBatch row = rootBenthosBatch.getChildren().get(i); + Float weight = row.getSampleCategoryWeight(); + if (weight == null) { + weight = row.getSampleCategoryComputedWeight(); + } + if (weight == null) { + return; + } + + CaracteristicQualitativeValue value = (CaracteristicQualitativeValue) row.getSampleCategoryValue(); + if (SortedUnsortedEnum.SORTED.matchValue(value)) { + benthosTotalComputedSortedWeight += weight; + } else { + benthosTotalComputedUnsortedWeight += weight; + } + } + + inertWeight = catchBatch.getBenthosTotalInertWeight(); + if (inertWeight != null) { + benthosTotalComputedSortedWeight += inertWeight.floatValue(); + } else { + catchBatch.setBenthosTotalInertComputedWeight(0f); + } + + livingNotItemizedWeight = catchBatch.getBenthosTotalLivingNotItemizedWeight(); + if (livingNotItemizedWeight != null) { + benthosTotalComputedSortedWeight += livingNotItemizedWeight.floatValue(); + } else { + catchBatch.setBenthosTotalLivingNotItemizedComputedWeight(0f); + } + + catchBatch.setBenthosTotalSampleSortedComputedWeight(benthosTotalComputedSortedWeight); + + Float benthosTotalSortedWeight = catchBatch.getBenthosTotalSortedWeight(); + if (benthosTotalSortedWeight == null) { + benthosTotalSortedWeight = benthosTotalComputedSortedWeight; + catchBatch.setBenthosTotalSortedComputedWeight(benthosTotalSortedWeight); + + } else if (benthosTotalSortedWeight < benthosTotalComputedSortedWeight) { + throw new TuttiBusinessException(_("tutti.service.catches.computeWeights.error.incoherentBenthosTotalSorted")); + } + catchBatch.setBenthosTotalUnsortedComputedWeight(benthosTotalComputedUnsortedWeight); + + if (benthosTotalSortedWeight == null) { + benthosTotalSortedWeight = catchBatch.getBenthosTotalSortedComputedWeight(); + } + Float benthosTotalWeight = benthosTotalComputedUnsortedWeight + benthosTotalSortedWeight; + catchBatch.setBenthosTotalComputedWeight(benthosTotalWeight); + + // Catch + Float carrouselWeight = catchBatch.getCatchTotalSortedCarousselWeight(); + Float totalUnsortedWeight = catchBatch.getSpeciesTotalUnsortedComputedWeight() + + catchBatch.getBenthosTotalUnsortedComputedWeight(); + Float totalSortedWeight; + if (carrouselWeight != null) { + totalSortedWeight = carrouselWeight; + } else { + totalSortedWeight = speciesTotalSortedWeight + benthosTotalSortedWeight; + } + catchBatch.setCatchTotalSortedComputedWeight(totalSortedWeight); + catchBatch.setCatchTotalUnsortedComputedWeight(totalUnsortedWeight); + + Float totalWeight = catchBatch.getCatchTotalWeight(); + Float rejectedWeight = catchBatch.getCatchTotalRejectedWeight(); + + if (rejectedWeight == null && totalWeight != null) { + if (!totalWeight.equals(totalUnsortedWeight + + totalSortedWeight)) { + throw new TuttiBusinessException(_("tutti.service.catches.computeWeights.error.incoherentTotal")); + + } else { + catchBatch.setCatchTotalRejectedComputedWeight(totalWeight + - totalUnsortedWeight + - totalSortedWeight); + } + + } else if (totalWeight == null) { + if (rejectedWeight == null) { + rejectedWeight = 0f; + catchBatch.setCatchTotalRejectedComputedWeight(0f); + } + catchBatch.setCatchTotalComputedWeight(totalUnsortedWeight + + totalSortedWeight + + rejectedWeight); + + } else if (!totalWeight.equals(totalUnsortedWeight + totalSortedWeight + rejectedWeight)) { + throw new TuttiBusinessException(_("tutti.service.catches.computeWeights.error.incoherentTotal")); + } + } + + public BatchContainer<SpeciesBatch> getComputedSpeciesBatches(FishingOperation operation) { + + BatchContainer<SpeciesBatch> rootSpeciesBatch = + persistenceService.getRootSpeciesBatch(operation.getId()); + + if (rootSpeciesBatch != null) { + List<SpeciesBatch> roots = rootSpeciesBatch.getChildren(); + + for (int i = 0; i < roots.size(); i++) { + SpeciesBatch batch = roots.get(i); + computeSpeciesBatch(batch); + } + } + + return rootSpeciesBatch; + } + + public BatchContainer<SpeciesBatch> getComputedBenthosBatches(FishingOperation operation) { + + BatchContainer<SpeciesBatch> rootBenthosBatch = + persistenceService.getRootBenthosBatch(operation.getId()); + + List<SpeciesBatch> roots = rootBenthosBatch.getChildren(); + + for (int i = 0; i < roots.size(); i++) { + SpeciesBatch batch = roots.get(i); + computeSpeciesBatch(batch); + } + + return rootBenthosBatch; + } + + protected Float computeSpeciesBatch(SpeciesBatch batch) { + Float result = null; + Float categoryWeight = batch.getSampleCategoryWeight(); + Float rowWeight = batch.getWeight(); + + List<SpeciesBatch> children = batch.getChildBatchs(); + // if the row is not a leaf + if (batch.sizeChildBatchs() > 0) { + Float sum = 0f; + // make the sum of the children weights + for (SpeciesBatch child : children) { + Float weight = computeSpeciesBatch(child); + if (weight == null) { + sum = null; + break; + } + sum += weight; + } + + if (sum != null) { +// if (categoryWeight == null) { +// finestCategory.setComputedWeight(sum); +// for (SpeciesBatchRowModel child : children) { +// child.getFinestCategory().setSubSample(false); +// } +// +// } else if (categoryWeight < sum) { +// throw new TuttiBusinessException(_("tutti.editCatchBatch.action.computeWeights.error.incoherentParentCategoryWeight")); +// +// } else { +// boolean subSample = categoryWeight > sum; +// for (SpeciesBatchRowModel child : children) { +// child.getFinestCategory().setSubSample(subSample); +// } +// sum = categoryWeight; +// } +// result = sum; + if (categoryWeight == null) { + batch.setSampleCategoryComputedWeight(sum); + + } else if (categoryWeight < sum) { + throw new TuttiBusinessException(_("tutti.service.catches.computeWeights.error.incoherentParentCategoryWeight")); + + } else { + sum = categoryWeight; + } + result = sum; + } + + } else {// the row is a leaf + + batch.setComputedWeight(null); + + List<SpeciesBatchFrequency> frequencies = persistenceService.getAllSpeciesBatchFrequency(batch.getId()); + + if (CollectionUtils.isNotEmpty(frequencies)) { + // if there are frequencies, then compute their weight + Float frequencyWeight = 0f; + for (SpeciesBatchFrequency frequency : frequencies) { + Float w = frequency.getWeight(); + if (w == null) { + + // can't sum when a null value appears + frequencyWeight = null; + break; + + } else if (frequencyWeight != null) { + + // still can sum weights + frequencyWeight += w; + } + } + + if (categoryWeight == null && rowWeight != null) { + throw new TuttiBusinessException(_("tutti.service.catches.computeWeights.error.incoherentRowWeightCategory")); + + } else if (categoryWeight == null && frequencyWeight != null) { + // if the category weight is null and the frequencies have a weight, + // then this weight is the result + batch.setSampleCategoryComputedWeight(frequencyWeight); + result = frequencyWeight; + + } else if (frequencyWeight != null + && !frequencyWeight.equals(categoryWeight)) { + + // if the weight of the frequencies is different from the category + // weight, then set the weight of the sample + if (frequencyWeight > categoryWeight) { + throw new TuttiBusinessException(_("tutti.service.catches.computeWeights.error.incoherentCategoryWeight")); + + } else if (rowWeight == null) { + batch.setComputedWeight(frequencyWeight); + + } else if (!rowWeight.equals(frequencyWeight)) { + throw new TuttiBusinessException(_("tutti.service.catches.computeWeights.error.incoherentRowWeightFrequency")); + } + result = categoryWeight; + + } else { + result = categoryWeight; + } + + } else { + result = categoryWeight; + } + } + + return result; + } + +} Modified: trunk/tutti-service/src/main/resources/i18n/tutti-service_en_GB.properties =================================================================== --- trunk/tutti-service/src/main/resources/i18n/tutti-service_en_GB.properties 2013-04-03 15:15:57 UTC (rev 721) +++ trunk/tutti-service/src/main/resources/i18n/tutti-service_en_GB.properties 2013-04-03 17:29:52 UTC (rev 722) @@ -7,6 +7,10 @@ tutti.config.option.tmp.directory.description= tutti.config.option.version.description= tutti.config.service= +tutti.editCatchBatch.action.computeWeights.error.incoherentCategoryWeight= +tutti.editCatchBatch.action.computeWeights.error.incoherentParentCategoryWeight= +tutti.editCatchBatch.action.computeWeights.error.incoherentRowWeightCategory= +tutti.editCatchBatch.action.computeWeights.error.incoherentRowWeightFrequency= tutti.property.attachment= tutti.property.caracteristic= tutti.property.country= @@ -43,6 +47,12 @@ tutti.propety.no.vessel.name= tutti.propety.no.zone= tutti.propety.vessel.nation.registrationCode= +tutti.service.catches.computeWeights.error.incoherentCategoryWeight= +tutti.service.catches.computeWeights.error.incoherentParentCategoryWeight= +tutti.service.catches.computeWeights.error.incoherentRowWeightCategory= +tutti.service.catches.computeWeights.error.incoherentRowWeightFrequency= +tutti.service.catches.computeWeights.error.incoherentSpeciesTotalSorted= +tutti.service.catches.computeWeights.error.incoherentTotal= tutti.validator.error.comment.too.long= tutti.validator.error.cruise.beginDate.required= tutti.validator.error.cruise.dates.endBeforeStart= Modified: trunk/tutti-service/src/main/resources/i18n/tutti-service_fr_FR.properties =================================================================== --- trunk/tutti-service/src/main/resources/i18n/tutti-service_fr_FR.properties 2013-04-03 15:15:57 UTC (rev 721) +++ trunk/tutti-service/src/main/resources/i18n/tutti-service_fr_FR.properties 2013-04-03 17:29:52 UTC (rev 722) @@ -7,6 +7,7 @@ tutti.config.option.tmp.directory.description=Répertoire temporaire utilisée par l'application (est nettoyé à chaque démarrage de l'application). tutti.config.option.version.description=Version courante de l'application tutti.config.service=Configuration des services de Tutti +tutti.editCatchBatch.action.computeWeights.error.incoherentParentCategoryWeight= tutti.property.attachment=Pièce jointe tutti.property.caracteristic=Caractéristique tutti.property.country=Pays @@ -43,6 +44,12 @@ tutti.propety.no.vessel.name=Nom inconnu tutti.propety.no.zone=Pas de zone tutti.propety.vessel.nation.registrationCode=%s (nat.) +tutti.service.catches.computeWeights.error.incoherentCategoryWeight=Le poids total des mensurations est supérieur au poids de la catégorie +tutti.service.catches.computeWeights.error.incoherentParentCategoryWeight=Le poids de la catégorie est différent de la somme des poids de ses sous-catégories +tutti.service.catches.computeWeights.error.incoherentRowWeightCategory=Le poids de la catégorie ne peut pas être nul si le poids de sous-échantillonage est renseigné +tutti.service.catches.computeWeights.error.incoherentRowWeightFrequency=Le poids total des mensurations est différent du poids du sous-échantillon +tutti.service.catches.computeWeights.error.incoherentSpeciesTotalSorted=Le poids total Vrac des espèces est inférieur à la somme des poids Vrac triés, inerte trié et vivant non détaillé trié +tutti.service.catches.computeWeights.error.incoherentTotal=Le poids total de la capture ne correspond pas à la somme des poids totaux Vrac, Hors Vrac et non triés tutti.validator.error.comment.too.long=Taille de commentaire trop longue (limitée à %s caractères) tutti.validator.error.cruise.beginDate.required=La date de début est obligatoire tutti.validator.error.cruise.dates.endBeforeStart=La date de fin doit être après la date de début