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 a49e2f29cf99b6070d508c0fdb927d69398a766e Author: Tony CHEMIT <chemit@codelutin.com> Date: Tue Apr 26 11:01:25 2016 +0200 Nettoyage de code et début de mise ne place de la génération des mensurations lors d'un import multi-poste espèces - observation seulement (See #8278) --- .../catches/multipost/MultiPostImportService.java | 508 +++++++++------------ 1 file changed, 206 insertions(+), 302 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 80fd69b..6f2f299 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 @@ -37,6 +37,7 @@ import fr.ifremer.tutti.persistence.entities.data.Attachment; import fr.ifremer.tutti.persistence.entities.data.Attachments; import fr.ifremer.tutti.persistence.entities.data.BatchContainer; import fr.ifremer.tutti.persistence.entities.data.CatchBatch; +import fr.ifremer.tutti.persistence.entities.data.CopyIndividualObservationMode; import fr.ifremer.tutti.persistence.entities.data.FishingOperation; import fr.ifremer.tutti.persistence.entities.data.IndividualObservationBatch; import fr.ifremer.tutti.persistence.entities.data.IndividualObservationBatchs; @@ -80,6 +81,7 @@ 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 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; @@ -97,6 +99,7 @@ import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.TreeMap; import java.util.function.Supplier; import static org.nuiton.i18n.I18n.t; @@ -289,21 +292,7 @@ public class MultiPostImportService extends AbstractTuttiService implements Mult if (importIndividualObservations) { - // Import batches - // map containing the batches by their persistence id - Map<String, IndividualObservationBatch> individualObservationsBatches = importIndividualObservationBatches(importContext, batches, notImportedBatches); - - // Import caracteristics - - importCaracteristics(importContext, individualObservationsBatches); - - // Persist batches - - persistenceService.createIndividualObservationBatches(individualObservationsBatches.values()); - - // Import attachments - - importAttachments(importContext, individualObservationsBatches, ObjectTypeCode.SAMPLE); + importIndividualObservations(importContext, importFrequencies, batches, notImportedBatches, speciesSaveFrequencyMethod); } @@ -324,6 +313,27 @@ public class MultiPostImportService extends AbstractTuttiService implements Mult } + interface SaveFrequencyMethod { + + + List<SpeciesBatchFrequency> saveSpeciesBatchFrequency(Integer speciesBatchId, List<SpeciesBatchFrequency> frequencies); + + } + + private final SaveFrequencyMethod speciesSaveFrequencyMethod = new SaveFrequencyMethod() { + @Override + public List<SpeciesBatchFrequency> saveSpeciesBatchFrequency(Integer speciesBatchId, List<SpeciesBatchFrequency> frequencies) { + return persistenceService.saveSpeciesBatchFrequency(speciesBatchId, frequencies); + } + }; + + private final SaveFrequencyMethod benthosSaveFrequencyMethod = new SaveFrequencyMethod() { + @Override + public List<SpeciesBatchFrequency> saveSpeciesBatchFrequency(Integer speciesBatchId, List<SpeciesBatchFrequency> frequencies) { + return persistenceService.saveBenthosBatchFrequency(speciesBatchId, frequencies); + } + }; + protected Map<String, SpeciesBatch> importSpeciesBatches(MultiPostImportContext importContext, BatchContainer<SpeciesBatch> speciesBatches, Map<String, SpeciesBatch> notImportedBatches) throws IOException { @@ -401,51 +411,6 @@ public class MultiPostImportService extends AbstractTuttiService implements Mult } -// protected void importSpeciesFrequencies(MultiPostImportContext importContext, -// Map<String, SpeciesBatch> batches, -// Map<String, SpeciesBatch> notImportedBatches) throws IOException { -// -// ListMultimap<Integer, SpeciesBatchFrequency> frequencyMap = ArrayListMultimap.create(); -// -// try (Reader reader = importContext.newFileReader(FREQUENCIES_FILE)) { -// -// CatchFrequencyRowModel frequencyModel = new CatchFrequencyRowModel(CSV_SEPARATOR, importContext.getSpeciesList(), importContext.getCaracteristics()); -// -// try (Import<CatchFrequencyRow> importer = Import.newImport(frequencyModel, reader)) { -// -// for (CatchFrequencyRow frequencyRow : importer) { -// String frequencyRowBatchId = frequencyRow.getBatchId(); -// if (!notImportedBatches.containsKey(frequencyRowBatchId)) { -// -// // not imported -// continue; -// } -// SpeciesBatch batch = batches.get(frequencyRowBatchId); -// if (batch == null) { -// -// // not imported (FIXME Should be an error ?) -// continue; -// } -// -// SpeciesBatchFrequency frequency = SpeciesBatchFrequencys.newSpeciesBatchFrequency(); -// frequency.setLengthStepCaracteristic(frequencyRow.getLengthStepCaracteristic()); -// frequency.setLengthStep(frequencyRow.getLengthStep()); -// frequency.setNumber(frequencyRow.getNumber()); -// frequency.setWeight(frequencyRow.getWeight()); -// frequency.setBatch(batch); -// frequencyMap.put(batch.getIdAsInt(), frequency); -// -// } -// } -// } -// -// for (Integer batchId : frequencyMap.keySet()) { -// List<SpeciesBatchFrequency> frequencies = frequencyMap.get(batchId); -// persistenceService.saveSpeciesBatchFrequency(batchId, frequencies); -// } -// -// } - protected void importSpeciesWeights(MultiPostImportContext importContext, CatchWeightsRowModel weightsModel, Map<String, Object> notImportedData) throws IOException { @@ -510,51 +475,11 @@ public class MultiPostImportService extends AbstractTuttiService implements Mult boolean importFrequencies, boolean importIndividualObservations) { - return persistenceService.invoke(() -> { - - try (MultiPostImportContext importContext = new MultiPostImportContext(context, file, operation)) { - - // Import frequencies - - List<SpeciesBatchFrequency> frequenciesToSave = new ArrayList<>(); - - if (importFrequencies) { - frequenciesToSave.addAll(importFrequencies(importContext, speciesBatch, SpeciesBatchFrequencys::newSpeciesBatchFrequency)); - frequenciesToSave.forEach(speciesBatchFrequency -> speciesBatchFrequency.setBatch(speciesBatch)); - } - - // Import indivudal observations - - List<IndividualObservationBatch> individualObservations = new ArrayList<>(); - - Multimap<Integer, File> attachmentFiles = HashMultimap.create(); - - if (importIndividualObservations) { - - // Import batches - // map containing the batches by their persistence id - Map<String, IndividualObservationBatch> individualObservationsBatches = importIndividualObservationBatches(importContext, speciesBatch); - - // Import caracteristics - - importCaracteristics(importContext, individualObservationsBatches); - - individualObservations.addAll(individualObservationsBatches.values()); - - // Persist batches - -// persistenceService.createIndividualObservationBatches(individualObservationsBatches.values()); - - // Import attachments - - attachmentFiles.putAll(getAttachmentFiles(importContext, individualObservationsBatches, ObjectTypeCode.SAMPLE)); - - } - - return new MultiPostImportResult(frequenciesToSave, individualObservations, attachmentFiles); - - } - }); + return importSpeciesOrBenthosBatch(file, + operation, + speciesBatch, + importFrequencies, + importIndividualObservations); } @@ -608,27 +533,11 @@ public class MultiPostImportService extends AbstractTuttiService implements Mult } -// importBenthosFrequencies(importContext, batches, notImportedBatches); - // Import indivudal observations if (importIndividualObservations) { - // Import batches - // map containing the batches by their persistence id - Map<String, IndividualObservationBatch> individualObservationsBatches = importIndividualObservationBatches(importContext, batches, notImportedBatches); - - // Import caracteristics - - importCaracteristics(importContext, individualObservationsBatches); - - // Persist batches - - persistenceService.createIndividualObservationBatches(individualObservationsBatches.values()); - - // Import attachments - - importAttachments(importContext, individualObservationsBatches, ObjectTypeCode.SAMPLE); + importIndividualObservations(importContext, importFrequencies, batches, notImportedBatches, speciesSaveFrequencyMethod); } @@ -728,43 +637,6 @@ public class MultiPostImportService extends AbstractTuttiService implements Mult } -// protected void importBenthosFrequencies(MultiPostImportContext importContext, -// Map<String, SpeciesBatch> batches, -// Map<String, SpeciesBatch> notImportedBatches) throws IOException { -// -// ListMultimap<Integer, SpeciesBatchFrequency> frequencyMap = ArrayListMultimap.create(); -// -// try (Reader reader = importContext.newFileReader(FREQUENCIES_FILE)) { -// -// CatchFrequencyRowModel frequencyModel = new CatchFrequencyRowModel(CSV_SEPARATOR, importContext.getSpeciesList(), importContext.getCaracteristics()); -// try (Import<CatchFrequencyRow> importer = Import.newImport(frequencyModel, reader)) { -// -// -// for (CatchFrequencyRow frequencyRow : importer) { -// if (notImportedBatches.get(frequencyRow.getBatchId()) == null) { -// SpeciesBatch batch = batches.get(frequencyRow.getBatchId()); -// if (batch != null) { -// SpeciesBatchFrequency frequency = BenthosBatchFrequencys.newBenthosBatchFrequency(); -// frequency.setLengthStepCaracteristic(frequencyRow.getLengthStepCaracteristic()); -// frequency.setLengthStep(frequencyRow.getLengthStep()); -// frequency.setNumber(frequencyRow.getNumber()); -// frequency.setWeight(frequencyRow.getWeight()); -// frequency.setBatch(batch); -// frequencyMap.put(batch.getIdAsInt(), frequency); -// } -// } -// } -// } -// -// } -// -// for (Integer batchId : frequencyMap.keySet()) { -// List<SpeciesBatchFrequency> frequencies = frequencyMap.get(batchId); -// persistenceService.saveBenthosBatchFrequency(batchId, frequencies); -// } -// -// } - protected void importBenthosWeights(MultiPostImportContext importContext, CatchWeightsRowModel weightsModel, Map<String, Object> notImportedData) throws IOException { @@ -818,9 +690,6 @@ public class MultiPostImportService extends AbstractTuttiService implements Mult //-- Import Benthos Batches data --// //------------------------------------------------------------------------// - - //FIXME exactly the same as for the species ??? - /** * Import benthos batches frequencies or individual observations from a satellite post. * @@ -834,54 +703,13 @@ public class MultiPostImportService extends AbstractTuttiService implements Mult boolean importFrequencies, boolean importIndividualObservations) { - return persistenceService.invoke(() -> { + return importSpeciesOrBenthosBatch(file, + operation, + speciesBatch, + importFrequencies, + importIndividualObservations); - try (MultiPostImportContext importContext = new MultiPostImportContext(context, file, operation)) { - - // Import frequencies - - List<SpeciesBatchFrequency> frequenciesToSave = new ArrayList<>(); - - if (importFrequencies) { - frequenciesToSave.addAll(importFrequencies(importContext, speciesBatch, SpeciesBatchFrequencys::newSpeciesBatchFrequency)); - frequenciesToSave.forEach(speciesBatchFrequency -> speciesBatchFrequency.setBatch(speciesBatch)); - } - - // Import indivudal observations - - List<IndividualObservationBatch> individualObservations = new ArrayList<>(); - - Multimap<Integer, File> attachmentFiles = HashMultimap.create(); - - if (importIndividualObservations) { - - // Import batches - // map containing the batches by their persistence id - Map<String, IndividualObservationBatch> individualObservationsBatches = importIndividualObservationBatches(importContext, speciesBatch); - - // Import caracteristics - - importCaracteristics(importContext, individualObservationsBatches); - - individualObservations.addAll(individualObservationsBatches.values()); - - // Persist batches - -// persistenceService.createIndividualObservationBatches(individualObservationsBatches.values()); - - // Import attachments - - attachmentFiles.putAll(getAttachmentFiles(importContext, individualObservationsBatches, ObjectTypeCode.SAMPLE)); - - } - - return new MultiPostImportResult(frequenciesToSave, individualObservations, attachmentFiles); - - } - - }); - } @@ -1030,52 +858,6 @@ public class MultiPostImportService extends AbstractTuttiService implements Mult } //------------------------------------------------------------------------// - //-- Import individual observations --// - //------------------------------------------------------------------------// - -// protected void importIndividualObservationCaracteristics(File file, -// Map<String, IndividualObservationBatch> batches, -// List<Caracteristic> caracteristics) throws IOException { -// -// -// try (Reader reader = importContext.newFileReaderCARACTERISTIC_FILE).toPath())) { -// -// CaracteristicRowModel caracteristicModel = new CaracteristicRowModel(CSV_SEPARATOR, caracteristics); -// -// try (Import<CaracteristicRow> importer = Import.newImport(caracteristicModel, reader)) { -// -// for (CaracteristicRow caracteristicRow : importer) { -// IndividualObservationBatch batch = batches.get(caracteristicRow.getBatchId()); -// if (batch != null) { -// Caracteristic caracteristic = caracteristicRow.getCaracteristic(); -// CaracteristicValueParserFormatter parser = CaracteristicValueParserFormatter.newParser(caracteristic); -// Serializable value = caracteristicRow.getValue(); -// try { -// parser.parse(String.valueOf(value)); -// } catch (ParseException e) { -// throw new ApplicationTechnicalException("Could not parse caracteristic value: " + value, e); -// } -// // switch (caracteristic.getCaracteristicType()) { -// // case QUALITATIVE: -// // value = CaracteristicQualitativeValues.getQualitativeValue(caracteristic, Integer.parseInt(value.toString())); -// // break; -// // -// // case NUMBER: -// // value = Float.parseFloat(value.toString()); -// // break; -// // } -// CaracteristicMap map = batch.getCaracteristics(); -// map.put(caracteristic, value); -// } -// } -// -// } -// -// } -// -// } - - //------------------------------------------------------------------------// //-- Import Accidental catches --// //------------------------------------------------------------------------// @@ -1287,8 +1069,7 @@ public class MultiPostImportService extends AbstractTuttiService implements Mult return result; } - protected <B extends SampleEntity> void importCaracteristics(MultiPostImportContext importContext, - Map<String, B> batches) throws IOException { + protected <B extends SampleEntity> void importCaracteristics(MultiPostImportContext importContext, Map<String, B> batches) throws IOException { try (Reader reader = importContext.newFileReader(CARACTERISTIC_FILE)) { @@ -1321,9 +1102,52 @@ public class MultiPostImportService extends AbstractTuttiService implements Mult } - protected Map<String, IndividualObservationBatch> importIndividualObservationBatches(MultiPostImportContext importContext, - Map<String, SpeciesBatch> speciesOrBenthosBatches, - Map<String, SpeciesBatch> notImportedBatches) throws IOException { + private ListMultimap<String, SpeciesBatchFrequency> importFrequencies(MultiPostImportContext importContext, + Map<String, SpeciesBatch> batches, + Map<String, SpeciesBatch> notImportedBatches, + Supplier<SpeciesBatchFrequency> newFrequencySupplier) throws IOException { + + ListMultimap<String, SpeciesBatchFrequency> frequencyMap = ArrayListMultimap.create(); + + try (Reader reader = importContext.newFileReader(FREQUENCIES_FILE)) { + + CatchFrequencyRowModel frequencyModel = CatchFrequencyRowModel.forImport(importContext.getSpeciesList(), importContext.getCaracteristics()); + + try (Import<CatchFrequencyRow> importer = Import.newImport(frequencyModel, reader)) { + + for (CatchFrequencyRow frequencyRow : importer) { + String frequencyRowBatchId = frequencyRow.getBatchId(); + if (notImportedBatches.containsKey(frequencyRowBatchId)) { + + // not imported + continue; + } + SpeciesBatch batch = batches.get(frequencyRowBatchId); + if (batch == null) { + + // not imported (FIXME Should be an error ?) + continue; + } + + SpeciesBatchFrequency frequency = newFrequencySupplier.get(); + frequency.setLengthStepCaracteristic(frequencyRow.getLengthStepCaracteristic()); + frequency.setLengthStep(frequencyRow.getLengthStep()); + frequency.setNumber(frequencyRow.getNumber()); + frequency.setWeight(frequencyRow.getWeight()); +// frequency.setBatch(batch); + frequencyMap.put(frequencyRowBatchId, frequency); + + } + } + } + + return frequencyMap; + + } + + private Map<String, IndividualObservationBatch> importIndividualObservationBatches(MultiPostImportContext importContext, + Map<String, SpeciesBatch> speciesOrBenthosBatches, + Map<String, SpeciesBatch> notImportedBatches) throws IOException { Map<String, IndividualObservationBatch> batches = new LinkedHashMap<>(); @@ -1378,49 +1202,6 @@ public class MultiPostImportService extends AbstractTuttiService implements Mult } - protected ListMultimap<String, SpeciesBatchFrequency> importFrequencies(MultiPostImportContext importContext, - Map<String, SpeciesBatch> batches, - Map<String, SpeciesBatch> notImportedBatches, - Supplier<SpeciesBatchFrequency> newFrequencySupplier) throws IOException { - - ListMultimap<String, SpeciesBatchFrequency> frequencyMap = ArrayListMultimap.create(); - - try (Reader reader = importContext.newFileReader(FREQUENCIES_FILE)) { - - CatchFrequencyRowModel frequencyModel = CatchFrequencyRowModel.forImport(importContext.getSpeciesList(), importContext.getCaracteristics()); - - try (Import<CatchFrequencyRow> importer = Import.newImport(frequencyModel, reader)) { - - for (CatchFrequencyRow frequencyRow : importer) { - String frequencyRowBatchId = frequencyRow.getBatchId(); - if (notImportedBatches.containsKey(frequencyRowBatchId)) { - - // not imported - continue; - } - SpeciesBatch batch = batches.get(frequencyRowBatchId); - if (batch == null) { - - // not imported (FIXME Should be an error ?) - continue; - } - - SpeciesBatchFrequency frequency = newFrequencySupplier.get(); - frequency.setLengthStepCaracteristic(frequencyRow.getLengthStepCaracteristic()); - frequency.setLengthStep(frequencyRow.getLengthStep()); - frequency.setNumber(frequencyRow.getNumber()); - frequency.setWeight(frequencyRow.getWeight()); -// frequency.setBatch(batch); - frequencyMap.put(frequencyRowBatchId, frequency); - - } - } - } - - return frequencyMap; - - } - protected List<SpeciesBatchFrequency> importFrequencies(MultiPostImportContext importContext, SpeciesBatch speciesBatch, Supplier<SpeciesBatchFrequency> newFrequencySupplier) throws IOException { List<SpeciesBatchFrequency> frequenciesToSave = new LinkedList<>(); @@ -1516,4 +1297,127 @@ 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 { + + // Import batches + // map containing the batches by their persistence id + Map<String, IndividualObservationBatch> individualObservationsBatches = importIndividualObservationBatches(importContext, batches, notImportedBatches); + + // Import caracteristics + + importCaracteristics(importContext, individualObservationsBatches); + + // Persist batches + + persistenceService.createIndividualObservationBatches(individualObservationsBatches.values()); + + // Import attachments + + importAttachments(importContext, individualObservationsBatches, ObjectTypeCode.SAMPLE); + + if (!importFrequencies) { + + // Must generate frequencies if required + batches.values().forEach(speciesBatch -> { + + Integer speciesBatchId = speciesBatch.getIdAsInt(); + + List<IndividualObservationBatch> allIndividualObservationBatchsForBatch = persistenceService.getAllIndividualObservationBatchsForBatch(speciesBatchId); + if (!allIndividualObservationBatchsForBatch.isEmpty()) { + 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: + + // nothing we said! + break; + + case ALL: + copySize = true; + break; + + case SIZE: + copySize = true; + copyWeight = true; + break; + } + if (copySize) { + + // this means first to delete any existing such frequencies + saveFrequencyMethod.saveSpeciesBatchFrequency(speciesBatchId, new ArrayList<>()); + + 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 -> { + + }); + } + + } + }); + } + + } + + private MultiPostImportResult importSpeciesOrBenthosBatch(File file, + FishingOperation operation, + SpeciesBatch speciesBatch, + boolean importFrequencies, + boolean importIndividualObservations) { + + return persistenceService.invoke(() -> { + + + try (MultiPostImportContext importContext = new MultiPostImportContext(context, file, operation)) { + + // Import frequencies + + List<SpeciesBatchFrequency> frequenciesToSave = new ArrayList<>(); + + if (importFrequencies) { + frequenciesToSave.addAll(importFrequencies(importContext, speciesBatch, SpeciesBatchFrequencys::newSpeciesBatchFrequency)); + frequenciesToSave.forEach(speciesBatchFrequency -> speciesBatchFrequency.setBatch(speciesBatch)); + } + + // Import indivudal observations + + List<IndividualObservationBatch> individualObservations = new ArrayList<>(); + + Multimap<Integer, File> attachmentFiles = HashMultimap.create(); + + if (importIndividualObservations) { + + // Import batches + // map containing the batches by their persistence id + Map<String, IndividualObservationBatch> individualObservationsBatches = importIndividualObservationBatches(importContext, speciesBatch); + + // Import caracteristics + + importCaracteristics(importContext, individualObservationsBatches); + + individualObservations.addAll(individualObservationsBatches.values()); + + // Import attachments + + attachmentFiles.putAll(getAttachmentFiles(importContext, individualObservationsBatches, ObjectTypeCode.SAMPLE)); + + } + + return new MultiPostImportResult(frequenciesToSave, individualObservations, attachmentFiles); + + } + + }); + + } } \ No newline at end of file -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@codelutin.com>.