This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository tutti. See https://gitlab.nuiton.org/codelutin/tutti.git commit 5f78949897144b289f11804f468d3d0760e5baef Author: Tony CHEMIT <chemit@codelutin.com> Date: Wed Apr 13 11:52:10 2016 +0200 Revue du service de validation en utilisant plusieurs exceptions typées --- .../catches/BenthosWeightComputingException.java | 17 + .../catches/CatchWeightComputingException.java | 14 + .../MarineLitterWeightComputingException.java | 17 + .../catches/SpeciesWeightComputingException.java | 17 + .../catches/TuttiWeightComputingException.java | 20 +- .../service/catches/WeightComputingService.java | 462 ++++++++++----------- 6 files changed, 295 insertions(+), 252 deletions(-) diff --git a/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/BenthosWeightComputingException.java b/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/BenthosWeightComputingException.java new file mode 100644 index 0000000..3d85185 --- /dev/null +++ b/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/BenthosWeightComputingException.java @@ -0,0 +1,17 @@ +package fr.ifremer.tutti.service.catches; + +/** + * Created on 13/04/16. + * + * @author Tony Chemit - chemit@codelutin.com + */ +public class BenthosWeightComputingException extends TuttiWeightComputingException { + + public BenthosWeightComputingException(String message) { + super(message); + } + + public BenthosWeightComputingException(String message, String property, int index) { + super(message, property, index); + } +} diff --git a/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/CatchWeightComputingException.java b/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/CatchWeightComputingException.java new file mode 100644 index 0000000..997281a --- /dev/null +++ b/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/CatchWeightComputingException.java @@ -0,0 +1,14 @@ +package fr.ifremer.tutti.service.catches; + +/** + * Created on 13/04/16. + * + * @author Tony Chemit - chemit@codelutin.com + */ +public class CatchWeightComputingException extends TuttiWeightComputingException { + + public CatchWeightComputingException(String message) { + super(message); + } + +} diff --git a/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/MarineLitterWeightComputingException.java b/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/MarineLitterWeightComputingException.java new file mode 100644 index 0000000..6c22618 --- /dev/null +++ b/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/MarineLitterWeightComputingException.java @@ -0,0 +1,17 @@ +package fr.ifremer.tutti.service.catches; + +/** + * Created on 13/04/16. + * + * @author Tony Chemit - chemit@codelutin.com + */ +public class MarineLitterWeightComputingException extends TuttiWeightComputingException { + + public MarineLitterWeightComputingException(String message) { + super(message); + } + + public MarineLitterWeightComputingException(String message, String property, int index) { + super(message, property, index); + } +} diff --git a/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/SpeciesWeightComputingException.java b/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/SpeciesWeightComputingException.java new file mode 100644 index 0000000..42ba734 --- /dev/null +++ b/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/SpeciesWeightComputingException.java @@ -0,0 +1,17 @@ +package fr.ifremer.tutti.service.catches; + +/** + * Created on 13/04/16. + * + * @author Tony Chemit - chemit@codelutin.com + */ +public class SpeciesWeightComputingException extends TuttiWeightComputingException { + + public SpeciesWeightComputingException(String message) { + super(message); + } + + public SpeciesWeightComputingException(String message, String property, int index) { + super(message, property, index); + } +} diff --git a/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/TuttiWeightComputingException.java b/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/TuttiWeightComputingException.java index ba21ade..4d06191 100644 --- a/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/TuttiWeightComputingException.java +++ b/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/TuttiWeightComputingException.java @@ -24,6 +24,8 @@ package fr.ifremer.tutti.service.catches; import org.nuiton.jaxx.application.ApplicationBusinessException; +import java.util.Optional; + /** * @author Kevin Morin - kmorin@codelutin.com * @since 2.0 @@ -34,21 +36,23 @@ public class TuttiWeightComputingException extends ApplicationBusinessException protected String property; - protected int index; + protected Integer index; + + protected TuttiWeightComputingException(String message) { + this(message, null, null); + } - public TuttiWeightComputingException(String message, - String property, - int index) { + protected TuttiWeightComputingException(String message, String property, Integer index) { super(message); this.property = property; this.index = index; } - public String getProperty() { - return property; + public Optional<String> getProperty() { + return Optional.ofNullable(property); } - public int getIndex() { - return index; + public Optional<Integer> getIndex() { + return Optional.ofNullable(index); } } diff --git a/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/WeightComputingService.java b/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/WeightComputingService.java index 4ef90a9..3896b7d 100644 --- a/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/WeightComputingService.java +++ b/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/WeightComputingService.java @@ -38,9 +38,11 @@ import fr.ifremer.tutti.type.WeightUnit; import fr.ifremer.tutti.util.Numbers; import fr.ifremer.tutti.util.Weights; import org.apache.commons.collections4.CollectionUtils; -import org.nuiton.jaxx.application.ApplicationBusinessException; +import org.apache.commons.lang3.tuple.Pair; +import org.nuiton.decorator.Decorator; import org.nuiton.validator.NuitonValidatorResult; +import java.io.Serializable; import java.util.List; import static org.nuiton.i18n.I18n.t; @@ -57,12 +59,17 @@ public class WeightComputingService extends AbstractTuttiService { protected DecoratorService decoratorService; + private Decorator<Species> speciesDecorator; + + private int currentSpeciesRowIndex; + private int currentBenthosRowIndex; + @Override public void setServiceContext(TuttiServiceContext context) { super.setServiceContext(context); persistenceService = getService(PersistenceService.class); validationService = getService(ValidationService.class); - decoratorService = getService(DecoratorService.class);; + decoratorService = getService(DecoratorService.class); } /** @@ -83,106 +90,78 @@ public class WeightComputingService extends AbstractTuttiService { Float speciesTotalComputedUnsortedWeight = 0f; if (rootSpeciesBatch != null) { - List<SpeciesBatch> speciesBatches = rootSpeciesBatch.getChildren(); - for (SpeciesBatch row : speciesBatches) { - Float weight = Numbers.getValueOrComputedValue( - row.getSampleCategoryWeight(), - row.getSampleCategoryComputedWeight()); - if (weight == null) { - break; - } - - if (persistenceService.isVracBatch(row)) { - speciesTotalComputedSortedWeight += weight; - } else { - speciesTotalComputedUnsortedWeight += weight; - } - } + Pair<Float, Float> pair = computeTotalComputedSortedAndUnsortedWeights(rootSpeciesBatch); + speciesTotalComputedSortedWeight = pair.getLeft(); + speciesTotalComputedUnsortedWeight = pair.getRight(); } - Number inertWeight = catchBatch.getSpeciesTotalInertWeight(); - if (inertWeight != null) { - speciesTotalComputedSortedWeight += inertWeight.floatValue(); + Float speciesInertWeight = catchBatch.getSpeciesTotalInertWeight(); + if (speciesInertWeight != null) { + speciesTotalComputedSortedWeight += speciesInertWeight; } else { catchBatch.setSpeciesTotalInertComputedWeight(0f); } - Number livingNotItemizedWeight = catchBatch.getSpeciesTotalLivingNotItemizedWeight(); - if (livingNotItemizedWeight != null) { - speciesTotalComputedSortedWeight += livingNotItemizedWeight.floatValue(); + Float speciesLivingNotItemizedWeight = catchBatch.getSpeciesTotalLivingNotItemizedWeight(); + if (speciesLivingNotItemizedWeight != null) { + speciesTotalComputedSortedWeight += speciesLivingNotItemizedWeight; } else { catchBatch.setSpeciesTotalLivingNotItemizedComputedWeight(0f); } - catchBatch.setSpeciesTotalSampleSortedComputedWeight(speciesTotalComputedSortedWeight); + catchBatch.setSpeciesTotalSampleSortedComputedWeight(WeightUnit.KG.round(speciesTotalComputedSortedWeight)); Float speciesTotalSortedWeight = catchBatch.getSpeciesTotalSortedWeight(); if (speciesTotalSortedWeight == null) { speciesTotalSortedWeight = speciesTotalComputedSortedWeight; - catchBatch.setSpeciesTotalSortedComputedWeight( - WeightUnit.KG.round(speciesTotalSortedWeight)); + catchBatch.setSpeciesTotalSortedComputedWeight(WeightUnit.KG.round(speciesTotalSortedWeight)); - } else if (speciesTotalSortedWeight < speciesTotalComputedSortedWeight) { - throw new ApplicationBusinessException(t("tutti.service.operations.computeWeights.error.species.incoherentTotalSorted")); + } else if (Weights.isSmallerWeight(speciesTotalSortedWeight, speciesTotalComputedSortedWeight)) { + throw new SpeciesWeightComputingException(t("tutti.service.operations.computeWeights.error.species.incoherentTotalSorted")); } - catchBatch.setSpeciesTotalUnsortedComputedWeight( - WeightUnit.KG.round(speciesTotalComputedUnsortedWeight)); + catchBatch.setSpeciesTotalUnsortedComputedWeight(WeightUnit.KG.round(speciesTotalComputedUnsortedWeight)); if (speciesTotalSortedWeight == null) { speciesTotalSortedWeight = catchBatch.getSpeciesTotalSortedComputedWeight(); } - // Benthos Float benthosTotalComputedSortedWeight = 0f; Float benthosTotalComputedUnsortedWeight = 0f; if (rootBenthosBatch != null) { - List<SpeciesBatch> benthosBatches = rootBenthosBatch.getChildren(); - for (SpeciesBatch row : benthosBatches) { - Float weight = Numbers.getValueOrComputedValue( - row.getSampleCategoryWeight(), - row.getSampleCategoryComputedWeight()); - if (weight == null) { - break; - } - if (persistenceService.isVracBatch(row)) { - benthosTotalComputedSortedWeight += weight; - } else { - benthosTotalComputedUnsortedWeight += weight; - } - } + Pair<Float, Float> pair = computeTotalComputedSortedAndUnsortedWeights(rootBenthosBatch); + benthosTotalComputedSortedWeight = pair.getLeft(); + benthosTotalComputedUnsortedWeight = pair.getRight(); + } - inertWeight = catchBatch.getBenthosTotalInertWeight(); - if (inertWeight != null) { - benthosTotalComputedSortedWeight += inertWeight.floatValue(); + Float benthosInertWeight = catchBatch.getBenthosTotalInertWeight(); + if (benthosInertWeight != null) { + benthosTotalComputedSortedWeight += benthosInertWeight; } else { catchBatch.setBenthosTotalInertComputedWeight(0f); } - livingNotItemizedWeight = catchBatch.getBenthosTotalLivingNotItemizedWeight(); - if (livingNotItemizedWeight != null) { - benthosTotalComputedSortedWeight += livingNotItemizedWeight.floatValue(); + Float benthosLivingNotItemizedWeight = catchBatch.getBenthosTotalLivingNotItemizedWeight(); + if (benthosLivingNotItemizedWeight != null) { + benthosTotalComputedSortedWeight += benthosLivingNotItemizedWeight; } else { catchBatch.setBenthosTotalLivingNotItemizedComputedWeight(0f); } - catchBatch.setBenthosTotalSampleSortedComputedWeight( - WeightUnit.KG.round(benthosTotalComputedSortedWeight)); + catchBatch.setBenthosTotalSampleSortedComputedWeight(WeightUnit.KG.round(benthosTotalComputedSortedWeight)); Float benthosTotalSortedWeight = catchBatch.getBenthosTotalSortedWeight(); if (benthosTotalSortedWeight == null) { benthosTotalSortedWeight = benthosTotalComputedSortedWeight; - catchBatch.setBenthosTotalSortedComputedWeight( - WeightUnit.KG.round(benthosTotalSortedWeight)); + catchBatch.setBenthosTotalSortedComputedWeight(WeightUnit.KG.round(benthosTotalSortedWeight)); - } else if (benthosTotalSortedWeight < benthosTotalComputedSortedWeight) { - throw new ApplicationBusinessException(t("tutti.service.operations.computeWeights.error.benthos.incoherentTotalSorted")); + } else if (Weights.isSmallerWeight(benthosTotalSortedWeight, benthosTotalComputedSortedWeight)) { + throw new BenthosWeightComputingException(t("tutti.service.operations.computeWeights.error.benthos.incoherentTotalSorted")); } - catchBatch.setBenthosTotalUnsortedComputedWeight( - WeightUnit.KG.round(benthosTotalComputedUnsortedWeight)); + catchBatch.setBenthosTotalUnsortedComputedWeight(WeightUnit.KG.round(benthosTotalComputedUnsortedWeight)); if (benthosTotalSortedWeight == null) { benthosTotalSortedWeight = catchBatch.getBenthosTotalSortedComputedWeight(); @@ -203,22 +182,20 @@ public class WeightComputingService extends AbstractTuttiService { } } if (marineLitterTotalComputedWeight != null) { - catchBatch.setMarineLitterTotalComputedWeight( - WeightUnit.KG.round(marineLitterTotalComputedWeight)); + catchBatch.setMarineLitterTotalComputedWeight(WeightUnit.KG.round(marineLitterTotalComputedWeight)); } Float marineLitterTotalWeight = catchBatch.getMarineLitterTotalWeight(); if (marineLitterTotalWeight == null) { marineLitterTotalWeight = marineLitterTotalComputedWeight; - } else if (marineLitterTotalComputedWeight != null && marineLitterTotalWeight < marineLitterTotalComputedWeight) { - throw new ApplicationBusinessException(t("tutti.service.operations.computeWeights.error.marineLitter.incoherentTotal")); + } else if (marineLitterTotalComputedWeight != null && Weights.isSmallerWeight(marineLitterTotalWeight, marineLitterTotalComputedWeight)) { + throw new MarineLitterWeightComputingException(t("tutti.service.operations.computeWeights.error.marineLitter.incoherentTotal")); } // nothing to do with the marine litter weight, it is an isolated weight // Catch - Float totalUnsortedWeight = catchBatch.getSpeciesTotalUnsortedComputedWeight() + - catchBatch.getBenthosTotalUnsortedComputedWeight(); + Float totalUnsortedWeight = catchBatch.getSpeciesTotalUnsortedComputedWeight() + catchBatch.getBenthosTotalUnsortedComputedWeight(); Float totalSortedSortedWeight = speciesTotalSortedWeight + benthosTotalSortedWeight; @@ -230,28 +207,22 @@ public class WeightComputingService extends AbstractTuttiService { if (rejectedWeight == null && totalWeight != null) { if (Weights.isNotEqualWeight(totalWeight, totalUnsortedWeight + totalSortedSortedWeight)) { - throw new ApplicationBusinessException(t("tutti.service.operations.computeWeights.error.incoherentTotal")); + throw new CatchWeightComputingException(t("tutti.service.operations.computeWeights.error.incoherentTotal")); } //FIXME voir avec Vincent si on calcule le poids rejeté - catchBatch.setCatchTotalRejectedComputedWeight( - WeightUnit.KG.round(totalWeight - - totalUnsortedWeight - - totalSortedSortedWeight)); + catchBatch.setCatchTotalRejectedComputedWeight(WeightUnit.KG.round(totalWeight - totalUnsortedWeight - totalSortedSortedWeight)); } else if (totalWeight == null) { if (rejectedWeight == null) { rejectedWeight = 0f; catchBatch.setCatchTotalRejectedComputedWeight(0f); } - catchBatch.setCatchTotalComputedWeight( - WeightUnit.KG.round(totalUnsortedWeight - + totalSortedSortedWeight - + rejectedWeight)); + catchBatch.setCatchTotalComputedWeight(WeightUnit.KG.round(totalUnsortedWeight + totalSortedSortedWeight + rejectedWeight)); } else if (Weights.isNotEqualWeight(totalWeight, totalUnsortedWeight + totalSortedSortedWeight + rejectedWeight)) { - throw new ApplicationBusinessException(t("tutti.service.operations.computeWeights.error.incoherentTotal")); + throw new CatchWeightComputingException(t("tutti.service.operations.computeWeights.error.incoherentTotal")); } Float totalSortedWeight = totalSortedSortedWeight; @@ -275,8 +246,6 @@ public class WeightComputingService extends AbstractTuttiService { catchBatch.setBenthosTotalComputedWeight(WeightUnit.KG.round(benthosTotalWeight)); } - private int currentSpeciesRowIndex; - public BatchContainer<SpeciesBatch> getComputedSpeciesBatches(Integer operationId) { BatchContainer<SpeciesBatch> rootSpeciesBatch = null; @@ -286,44 +255,35 @@ public class WeightComputingService extends AbstractTuttiService { currentSpeciesRowIndex = 0; if (rootSpeciesBatch != null) { - List<SpeciesBatch> roots = rootSpeciesBatch.getChildren(); - - for (SpeciesBatch batch : roots) { - computeSpeciesBatch(batch); - } + rootSpeciesBatch.getChildren().forEach(this::computeSpeciesBatch); } } return rootSpeciesBatch; - } - protected String getCategoryLabel(Integer sampleCategoryId) { - SampleCategoryModelEntry category = - context.getSampleCategoryModel().getCategoryById(sampleCategoryId); - return category.getLabel(); } public Float computeSpeciesBatch(SpeciesBatch batch) { + Float result = null; int thisIndex = currentSpeciesRowIndex++; Float categoryWeight = batch.getSampleCategoryWeight(); Float rowWeight = batch.getWeight(); - String species = decoratorService.getDecoratorByType(Species.class).toString(batch.getSpecies()); - String categoryValue = decoratorService.getDecorator(batch.getSampleCategoryValue()) - .toString(batch.getSampleCategoryValue()); + String species = decorateSpecies(batch.getSpecies()); + String categoryValue = decorateCategoryValue(batch.getSampleCategoryValue()); NuitonValidatorResult validation = validationService.validateEditSpeciesBatch(batch); if (!validation.isValid()) { List<String> messages = validation.getErrorMessages(SpeciesBatch.PROPERTY_WEIGHT); String categoryLabel = getCategoryLabel(batch.getSampleCategoryId()); - throw new TuttiWeightComputingException(t(messages.get(0), - species, - categoryLabel, - categoryValue, - batch.getWeight(), - batch.getSampleCategoryWeight()), - SpeciesBatch.PROPERTY_WEIGHT, - thisIndex); + throw new SpeciesWeightComputingException(t(messages.get(0), + species, + categoryLabel, + categoryValue, + batch.getWeight(), + batch.getSampleCategoryWeight()), + SpeciesBatch.PROPERTY_WEIGHT, + thisIndex); } List<SpeciesBatch> children = batch.getChildBatchs(); @@ -339,22 +299,22 @@ public class WeightComputingService extends AbstractTuttiService { } sum += weight; } + sum = WeightUnit.KG.round(sum); if (sum != null) { if (categoryWeight == null) { - batch.setSampleCategoryComputedWeight(WeightUnit.KG.round(sum)); + batch.setSampleCategoryComputedWeight(sum); } else if (Weights.isSmallerWeight(categoryWeight, sum)) { String categoryLabel = getCategoryLabel(batch.getSampleCategoryId()); - throw new TuttiWeightComputingException( - t("tutti.service.operations.computeWeights.error.species.incoherentParentCategoryWeight", - species, - categoryLabel, - categoryValue, - categoryWeight, - sum), - SpeciesBatch.PROPERTY_SAMPLE_CATEGORY_WEIGHT, - thisIndex); + throw new SpeciesWeightComputingException(t("tutti.service.operations.computeWeights.error.species.incoherentParentCategoryWeight", + species, + categoryLabel, + categoryValue, + categoryWeight, + sum), + SpeciesBatch.PROPERTY_SAMPLE_CATEGORY_WEIGHT, + thisIndex); } else { sum = categoryWeight; @@ -370,40 +330,23 @@ public class WeightComputingService extends AbstractTuttiService { 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; - } - } - frequencyWeight = WeightUnit.KG.round(frequencyWeight); + Float frequencyWeight = sumFrequenciesWeight(frequencies); if (categoryWeight == null && rowWeight != null) { // throw new TuttiBusinessException(t("tutti.service.operations.computeWeights.error.incoherentRowWeightCategory")); - } else if (rowWeight != null && frequencyWeight != null - && Weights.isNotEqualWeight(rowWeight, frequencyWeight)) { + } else if (rowWeight != null && frequencyWeight != null && Weights.isNotEqualWeight(rowWeight, frequencyWeight)) { String categoryLabel = getCategoryLabel(batch.getSampleCategoryId()); - throw new TuttiWeightComputingException( - t("tutti.service.operations.computeWeights.error.species.incoherentRowWeightFrequency", - species, - categoryLabel, - categoryValue, - frequencyWeight, - rowWeight), - SpeciesBatch.PROPERTY_WEIGHT, - thisIndex); + throw new SpeciesWeightComputingException(t("tutti.service.operations.computeWeights.error.species.incoherentRowWeightFrequency", + species, + categoryLabel, + categoryValue, + frequencyWeight, + rowWeight), + SpeciesBatch.PROPERTY_WEIGHT, + thisIndex); } else if (categoryWeight == null && frequencyWeight != null) { // if the category weight is null and the frequencies have a weight, @@ -411,8 +354,7 @@ public class WeightComputingService extends AbstractTuttiService { batch.setSampleCategoryComputedWeight(frequencyWeight); result = frequencyWeight; - } else if (frequencyWeight != null - && Weights.isNotEqualWeight(frequencyWeight, categoryWeight)) { + } else if (frequencyWeight != null && Weights.isNotEqualWeight(frequencyWeight, categoryWeight)) { // if the weight of the frequencies is different from the category // weight, then set the weight of the sample @@ -420,15 +362,14 @@ public class WeightComputingService extends AbstractTuttiService { String categoryLabel = getCategoryLabel(batch.getSampleCategoryId()); - throw new TuttiWeightComputingException( - t("tutti.service.operations.computeWeights.error.species.incoherentCategoryWeight", - species, - categoryLabel, - categoryValue, - frequencyWeight, - categoryWeight), - SpeciesBatch.PROPERTY_SAMPLE_CATEGORY_WEIGHT, - thisIndex); + throw new SpeciesWeightComputingException(t("tutti.service.operations.computeWeights.error.species.incoherentCategoryWeight", + species, + categoryLabel, + categoryValue, + frequencyWeight, + categoryWeight), + SpeciesBatch.PROPERTY_SAMPLE_CATEGORY_WEIGHT, + thisIndex); } else if (rowWeight == null) { batch.setComputedWeight(frequencyWeight); @@ -441,11 +382,7 @@ public class WeightComputingService extends AbstractTuttiService { } // compute number from frequencies - Integer frequencyNumber = 0; - for (SpeciesBatchFrequency frequency : frequencies) { - Integer c = frequency.getNumber(); - frequencyNumber += c; - } + Integer frequencyNumber = sumFrequenciesNumber(frequencies); batch.setComputedNumber(frequencyNumber); } else { @@ -456,38 +393,33 @@ public class WeightComputingService extends AbstractTuttiService { String categoryLabel = getCategoryLabel(batch.getSampleCategoryId()); - throw new TuttiWeightComputingException( - t("tutti.service.operations.computeWeights.error.species.noWeight", - species, - categoryLabel, - categoryValue), - SpeciesBatch.PROPERTY_SAMPLE_CATEGORY_WEIGHT, - thisIndex); + throw new SpeciesWeightComputingException(t("tutti.service.operations.computeWeights.error.species.noWeight", + species, + categoryLabel, + categoryValue), + SpeciesBatch.PROPERTY_SAMPLE_CATEGORY_WEIGHT, + thisIndex); } return WeightUnit.KG.round(result); } - private int currentBenthosRowIndex; - public BatchContainer<SpeciesBatch> getComputedBenthosBatches(Integer operationId) { BatchContainer<SpeciesBatch> rootBenthosBatch = null; if (persistenceService.isFishingOperationWithCatchBatch(operationId)) { + rootBenthosBatch = persistenceService.getRootBenthosBatch(operationId, false); currentBenthosRowIndex = 0; if (rootBenthosBatch != null) { - List<SpeciesBatch> roots = rootBenthosBatch.getChildren(); - - for (SpeciesBatch batch : roots) { - computeBenthosBatch(batch); - } + rootBenthosBatch.getChildren().forEach(this::computeBenthosBatch); } } return rootBenthosBatch; + } public Float computeBenthosBatch(SpeciesBatch batch) { @@ -495,23 +427,22 @@ public class WeightComputingService extends AbstractTuttiService { int thisIndex = currentBenthosRowIndex++; Float categoryWeight = batch.getSampleCategoryWeight(); Float rowWeight = batch.getWeight(); - String species = decoratorService.getDecoratorByType(Species.class).toString(batch.getSpecies()); - String categoryValue = decoratorService.getDecorator(batch.getSampleCategoryValue()) - .toString(batch.getSampleCategoryValue()); + String species = decorateSpecies(batch.getSpecies()); + String categoryValue = decorateCategoryValue(batch.getSampleCategoryValue()); NuitonValidatorResult validation = validationService.validateEditBenthosBatch(batch); if (!validation.isValid()) { List<String> messages = validation.getErrorMessages(SpeciesBatch.PROPERTY_WEIGHT); String categoryLabel = getCategoryLabel(batch.getSampleCategoryId()); - throw new TuttiWeightComputingException(t(messages.get(0), - species, - categoryLabel, - categoryValue, - batch.getWeight(), - batch.getSampleCategoryWeight()), - SpeciesBatch.PROPERTY_WEIGHT, - thisIndex); + throw new BenthosWeightComputingException(t(messages.get(0), + species, + categoryLabel, + categoryValue, + batch.getWeight(), + batch.getSampleCategoryWeight()), + SpeciesBatch.PROPERTY_WEIGHT, + thisIndex); } List<SpeciesBatch> children = batch.getChildBatchs(); @@ -527,24 +458,23 @@ public class WeightComputingService extends AbstractTuttiService { } sum += weight; } + sum = WeightUnit.KG.round(sum); if (sum != null) { if (categoryWeight == null) { - batch.setSampleCategoryComputedWeight(WeightUnit.KG.round(sum)); + batch.setSampleCategoryComputedWeight(sum); } else if (Weights.isSmallerWeight(categoryWeight, sum)) { - String categoryLabel = - getCategoryLabel(batch.getSampleCategoryId()); + String categoryLabel = getCategoryLabel(batch.getSampleCategoryId()); - throw new TuttiWeightComputingException( - t("tutti.service.operations.computeWeights.error.benthos.incoherentParentCategoryWeight", - species, - categoryLabel, - categoryValue, - categoryWeight, - sum), - SpeciesBatch.PROPERTY_SAMPLE_CATEGORY_WEIGHT, - thisIndex); + throw new BenthosWeightComputingException(t("tutti.service.operations.computeWeights.error.benthos.incoherentParentCategoryWeight", + species, + categoryLabel, + categoryValue, + categoryWeight, + sum), + SpeciesBatch.PROPERTY_SAMPLE_CATEGORY_WEIGHT, + thisIndex); } else { sum = categoryWeight; @@ -561,33 +491,16 @@ public class WeightComputingService extends AbstractTuttiService { 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; - } - } - frequencyWeight = WeightUnit.KG.round(frequencyWeight); + Float frequencyWeight = sumFrequenciesWeight(frequencies); if (categoryWeight == null && rowWeight != null) { // throw new TuttiBusinessException(t("tutti.service.operations.computeWeights.error.incoherentRowWeightCategory")); - } else if (rowWeight != null && frequencyWeight != null - && Weights.isNotEqualWeight(rowWeight, frequencyWeight)) { + } else if (rowWeight != null && frequencyWeight != null && Weights.isNotEqualWeight(rowWeight, frequencyWeight)) { - String categoryLabel = - getCategoryLabel(batch.getSampleCategoryId()); + String categoryLabel = getCategoryLabel(batch.getSampleCategoryId()); - throw new TuttiWeightComputingException( + throw new BenthosWeightComputingException( t("tutti.service.operations.computeWeights.error.benthos.incoherentRowWeightFrequency", species, categoryLabel, @@ -603,25 +516,22 @@ public class WeightComputingService extends AbstractTuttiService { batch.setSampleCategoryComputedWeight(frequencyWeight); result = frequencyWeight; - } else if (frequencyWeight != null - && Weights.isNotEqualWeight(frequencyWeight, categoryWeight)) { + } else if (frequencyWeight != null && Weights.isNotEqualWeight(frequencyWeight, categoryWeight)) { // if the weight of the frequencies is different from the category // weight, then set the weight of the sample if (Weights.isGreaterWeight(frequencyWeight, categoryWeight)) { - String categoryLabel = - getCategoryLabel(batch.getSampleCategoryId()); + String categoryLabel = getCategoryLabel(batch.getSampleCategoryId()); - throw new TuttiWeightComputingException( - t("tutti.service.operations.computeWeights.error.benthos.incoherentCategoryWeight", - species, - categoryLabel, - categoryValue, - frequencyWeight, - categoryWeight), - SpeciesBatch.PROPERTY_SAMPLE_CATEGORY_WEIGHT, - thisIndex); + throw new BenthosWeightComputingException(t("tutti.service.operations.computeWeights.error.benthos.incoherentCategoryWeight", + species, + categoryLabel, + categoryValue, + frequencyWeight, + categoryWeight), + SpeciesBatch.PROPERTY_SAMPLE_CATEGORY_WEIGHT, + thisIndex); } else if (rowWeight == null) { batch.setComputedWeight(frequencyWeight); @@ -634,11 +544,7 @@ public class WeightComputingService extends AbstractTuttiService { } // compute number from frequencies - Integer frequencyNumber = 0; - for (SpeciesBatchFrequency frequency : frequencies) { - Integer c = frequency.getNumber(); - frequencyNumber += c; - } + Integer frequencyNumber = sumFrequenciesNumber(frequencies); batch.setComputedNumber(frequencyNumber); } else { result = categoryWeight; @@ -646,26 +552,23 @@ public class WeightComputingService extends AbstractTuttiService { } if (Weights.isNullOrZero(result)) { - String categoryLabel = - getCategoryLabel(batch.getSampleCategoryId()); + String categoryLabel = getCategoryLabel(batch.getSampleCategoryId()); - throw new TuttiWeightComputingException( - t("tutti.service.operations.computeWeights.error.benthos.noWeight", - species, - categoryLabel, - categoryValue), - SpeciesBatch.PROPERTY_SAMPLE_CATEGORY_WEIGHT, - thisIndex); + throw new BenthosWeightComputingException(t("tutti.service.operations.computeWeights.error.benthos.noWeight", + species, + categoryLabel, + categoryValue), + SpeciesBatch.PROPERTY_SAMPLE_CATEGORY_WEIGHT, + thisIndex); } return WeightUnit.KG.round(result); + } - public BatchContainer<MarineLitterBatch> getComputedMarineLitterBatches(Integer fishingOperationId, - Float marineLitterweight) { + public BatchContainer<MarineLitterBatch> getComputedMarineLitterBatches(Integer fishingOperationId, Float marineLitterweight) { - BatchContainer<MarineLitterBatch> rootMarineLitterBatch = - persistenceService.getRootMarineLitterBatch(fishingOperationId); + BatchContainer<MarineLitterBatch> rootMarineLitterBatch = persistenceService.getRootMarineLitterBatch(fishingOperationId); if (rootMarineLitterBatch != null) { boolean checkWeight = marineLitterweight == null; @@ -675,14 +578,85 @@ public class WeightComputingService extends AbstractTuttiService { MarineLitterBatch batch = children.get(i); if (checkWeight && Weights.isNullOrZero(batch.getWeight())) { - throw new TuttiWeightComputingException( - t("tutti.validator.warning.marineLitter.weight.required"), - MarineLitterBatch.PROPERTY_WEIGHT, - i); + throw new MarineLitterWeightComputingException(t("tutti.validator.warning.marineLitter.weight.required"), + MarineLitterBatch.PROPERTY_WEIGHT, + i); } } } return rootMarineLitterBatch; + + } + + private Pair<Float, Float> computeTotalComputedSortedAndUnsortedWeights(BatchContainer<SpeciesBatch> rootSpeciesBatch) { + + Float speciesTotalComputedSortedWeight = 0f; + Float speciesTotalComputedUnsortedWeight = 0f; + + for (SpeciesBatch speciesBatch : rootSpeciesBatch.getChildren()) { + Float weight = Numbers.getValueOrComputedValue( + speciesBatch.getSampleCategoryWeight(), + speciesBatch.getSampleCategoryComputedWeight()); + if (weight == null) { + break; + } + + if (persistenceService.isVracBatch(speciesBatch)) { + speciesTotalComputedSortedWeight += weight; + } else { + speciesTotalComputedUnsortedWeight += weight; + } + + } + + return Pair.of(speciesTotalComputedSortedWeight, speciesTotalComputedUnsortedWeight); + + } + + private String getCategoryLabel(Integer sampleCategoryId) { + SampleCategoryModelEntry category = context.getSampleCategoryModel().getCategoryById(sampleCategoryId); + return category.getLabel(); + } + + private Integer sumFrequenciesNumber(List<SpeciesBatchFrequency> frequencies) { + Integer frequencyNumber = 0; + for (SpeciesBatchFrequency frequency : frequencies) { + Integer c = frequency.getNumber(); + frequencyNumber += c; + } + return frequencyNumber; + } + + private Float sumFrequenciesWeight(List<SpeciesBatchFrequency> frequencies) { + 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; + } + } + frequencyWeight = WeightUnit.KG.round(frequencyWeight); + return frequencyWeight; } + + private String decorateSpecies(Species species) { + if (speciesDecorator == null) { + speciesDecorator = decoratorService.getDecoratorByType(Species.class); + } + return speciesDecorator.toString(species); + } + + private String decorateCategoryValue(Serializable sampleCategoryValue) { + return decoratorService.getDecorator(sampleCategoryValue).toString(sampleCategoryValue); + } + } -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@codelutin.com>.