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 f7e24929d7e010c8c0c1b8ebf2c1a1d3338a8712 Author: Kevin Morin <morin@codelutin.com> Date: Tue Apr 26 14:05:41 2016 +0200 génération des mensurations quand on est en mode de recopie taille ou tout (refs #8278) --- .../catches/multipost/MultiPostImportService.java | 104 ++++++++++++++++----- 1 file changed, 83 insertions(+), 21 deletions(-) diff --git a/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/multipost/MultiPostImportService.java b/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/multipost/MultiPostImportService.java index 6f2f299..a195229 100644 --- a/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/multipost/MultiPostImportService.java +++ b/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/multipost/MultiPostImportService.java @@ -80,8 +80,8 @@ import fr.ifremer.tutti.service.catches.multipost.csv.MarineLitterRowModel; import fr.ifremer.tutti.service.catches.multipost.csv.MarineLitterWeightRow; import fr.ifremer.tutti.service.catches.multipost.csv.MarineLitterWeightRowModel; import fr.ifremer.tutti.service.csv.CaracteristicValueParserFormatter; +import fr.ifremer.tutti.type.WeightUnit; import org.apache.commons.lang3.StringUtils; -import org.apache.commons.lang3.mutable.MutableFloat; import org.apache.commons.lang3.tuple.Pair; import org.nuiton.csv.Import; import org.nuiton.jaxx.application.ApplicationBusinessException; @@ -292,7 +292,7 @@ public class MultiPostImportService extends AbstractTuttiService implements Mult if (importIndividualObservations) { - importIndividualObservations(importContext, importFrequencies, batches, notImportedBatches, speciesSaveFrequencyMethod); + importIndividualObservations(importContext, importFrequencies, batches, notImportedBatches, speciesFrequencyHelper); } @@ -313,21 +313,33 @@ public class MultiPostImportService extends AbstractTuttiService implements Mult } - interface SaveFrequencyMethod { + interface FrequencyHelper { + SpeciesBatchFrequency newSpeciesBatchFrequency(); List<SpeciesBatchFrequency> saveSpeciesBatchFrequency(Integer speciesBatchId, List<SpeciesBatchFrequency> frequencies); } - private final SaveFrequencyMethod speciesSaveFrequencyMethod = new SaveFrequencyMethod() { + private final FrequencyHelper speciesFrequencyHelper = new FrequencyHelper() { + + @Override + public SpeciesBatchFrequency newSpeciesBatchFrequency() { + return SpeciesBatchFrequencys.newSpeciesBatchFrequency(); + } + @Override public List<SpeciesBatchFrequency> saveSpeciesBatchFrequency(Integer speciesBatchId, List<SpeciesBatchFrequency> frequencies) { return persistenceService.saveSpeciesBatchFrequency(speciesBatchId, frequencies); } }; - private final SaveFrequencyMethod benthosSaveFrequencyMethod = new SaveFrequencyMethod() { + private final FrequencyHelper benthosFrequencyHelper = new FrequencyHelper() { + + @Override + public SpeciesBatchFrequency newSpeciesBatchFrequency() { + return SpeciesBatchFrequencys.newBenthosBatchFrequency(); + } @Override public List<SpeciesBatchFrequency> saveSpeciesBatchFrequency(Integer speciesBatchId, List<SpeciesBatchFrequency> frequencies) { return persistenceService.saveBenthosBatchFrequency(speciesBatchId, frequencies); @@ -537,7 +549,7 @@ public class MultiPostImportService extends AbstractTuttiService implements Mult if (importIndividualObservations) { - importIndividualObservations(importContext, importFrequencies, batches, notImportedBatches, speciesSaveFrequencyMethod); + importIndividualObservations(importContext, importFrequencies, batches, notImportedBatches, benthosFrequencyHelper); } @@ -1297,7 +1309,7 @@ public class MultiPostImportService extends AbstractTuttiService implements Mult } - private void importIndividualObservations(MultiPostImportContext importContext, boolean importFrequencies, Map<String, SpeciesBatch> batches, Map<String, SpeciesBatch> notImportedBatches, SaveFrequencyMethod saveFrequencyMethod) throws IOException { + private void importIndividualObservations(MultiPostImportContext importContext, boolean importFrequencies, Map<String, SpeciesBatch> batches, Map<String, SpeciesBatch> notImportedBatches, FrequencyHelper frequencyHelper) throws IOException { // Import batches // map containing the batches by their persistence id @@ -1327,40 +1339,72 @@ public class MultiPostImportService extends AbstractTuttiService implements Mult IndividualObservationBatch firstIndividualObservationBatch = allIndividualObservationBatchsForBatch.get(0); CopyIndividualObservationMode copyIndividualObservationMode = firstIndividualObservationBatch.getCopyIndividualObservationMode(); Caracteristic lengthStepCaracteristic = firstIndividualObservationBatch.getLengthStepCaracteristic(); - String unit = lengthStepCaracteristic.getUnit(); - boolean copySize = true; - boolean copyWeight = true; - - switch (copyIndividualObservationMode) { - case NOTHING: + boolean copySize = false; + final boolean copyWeight; - // nothing we said! - break; + switch (copyIndividualObservationMode) { case ALL: copySize = true; + copyWeight = true; break; case SIZE: copySize = true; - copyWeight = true; + copyWeight = false; break; + + case NOTHING:// nothing we said! + default: + copyWeight = false; + } if (copySize) { // this means first to delete any existing such frequencies - saveFrequencyMethod.saveSpeciesBatchFrequency(speciesBatchId, new ArrayList<>()); +// saveFrequencyMethod.saveSpeciesBatchFrequency(speciesBatchId, new ArrayList<>()); + + float step = getStep(lengthStepCaracteristic); + Map<Float, SpeciesBatchFrequency> frequencies = new TreeMap<>(); - Map<Float, MutableFloat> sizes = new TreeMap<>(); - Map<Float, MutableFloat> weights = new TreeMap<>(); - //FIXME Generate frequencies from individual observations allIndividualObservationBatchsForBatch .stream() .filter(individualObservationBatch -> individualObservationBatch.getSize() != null) .forEach(individualObservationBatch -> { - + + // compute the lengthstep according to the step of the lengthstep caracteristic + Float lengthStep = getLengthStep(individualObservationBatch.getSize(), step); + // get the existing frequency + SpeciesBatchFrequency frequency = frequencies.get(lengthStep); + + if (frequency == null) { + // or create a new one + frequency = frequencyHelper.newSpeciesBatchFrequency(); + frequency.setLengthStep(lengthStep); + frequency.setLengthStepCaracteristic(lengthStepCaracteristic); + frequency.setNumber(0); + frequency.setBatch(speciesBatch); + + // only set a weight if we copy the weights + frequency.setWeight(copyWeight ? 0f : null); + + frequencies.put(lengthStep, frequency); + } + + // increment the number of the frequency + frequency.setNumber(frequency.getNumber() + 1); + + if (copyWeight && individualObservationBatch.getWeight() != null) { + frequency.setWeight(frequency.getWeight() + individualObservationBatch.getWeight()); + } + }); + + if (copyWeight) { + frequencies.values().forEach(frequency -> frequency.setWeight(WeightUnit.KG.round(frequency.getWeight()))); + } + frequencyHelper.saveSpeciesBatchFrequency(speciesBatchId, new ArrayList<>(frequencies.values())); } } @@ -1369,6 +1413,24 @@ public class MultiPostImportService extends AbstractTuttiService implements Mult } + private float getStep(Caracteristic caracteristic) { + Float step = null; + if (caracteristic != null) { + step = caracteristic.getPrecision(); + } + if (step == null) { + step = 1f; + } + return step; + } + + private float getLengthStep(float lengthStep, float step) { + int intValue = (int) (lengthStep * 10); + int intStep = (int) (step * 10); + int correctIntStep = intValue - (intValue % intStep); + return correctIntStep / 10f; + } + private MultiPostImportResult importSpeciesOrBenthosBatch(File file, FishingOperation operation, SpeciesBatch speciesBatch, -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@codelutin.com>.