This is an automated email from the git hooks/post-receive script. New commit to branch feature/8061 in repository tutti. See http://git.codelutin.com/tutti.git commit fe5868c44cfab05622615c0fd609deb983d18400 Author: Tony CHEMIT <chemit@codelutin.com> Date: Wed Mar 9 17:09:34 2016 +0100 Revue de l'export multi-poste --- .../catches/multipost/MultiPostConstants.java | 2 + .../catches/multipost/MultiPostExportContext.java | 300 ++++++++++ .../catches/multipost/MultiPostExportService.java | 608 ++++++--------------- .../multipost/MultiPostExportServiceTest.java | 46 +- 4 files changed, 488 insertions(+), 468 deletions(-) diff --git a/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/multipost/MultiPostConstants.java b/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/multipost/MultiPostConstants.java index b50b5fd..31d8c6a 100644 --- a/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/multipost/MultiPostConstants.java +++ b/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/multipost/MultiPostConstants.java @@ -55,4 +55,6 @@ public interface MultiPostConstants { String CARACTERISTIC_FILE = "caracteristics.csv"; String WEIGHTS_FILE = "weights.csv"; + + char CSV_SEPARATOR = ';'; } diff --git a/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/multipost/MultiPostExportContext.java b/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/multipost/MultiPostExportContext.java new file mode 100644 index 0000000..118a242 --- /dev/null +++ b/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/multipost/MultiPostExportContext.java @@ -0,0 +1,300 @@ +package fr.ifremer.tutti.service.catches.multipost; + +import com.google.common.io.Files; +import fr.ifremer.adagio.core.dao.referential.ObjectTypeCode; +import fr.ifremer.tutti.persistence.entities.CaracteristicMap; +import fr.ifremer.tutti.persistence.entities.data.AccidentalBatch; +import fr.ifremer.tutti.persistence.entities.data.Attachment; +import fr.ifremer.tutti.persistence.entities.data.FishingOperation; +import fr.ifremer.tutti.persistence.entities.data.IndividualObservationBatch; +import fr.ifremer.tutti.persistence.entities.data.MarineLitterBatch; +import fr.ifremer.tutti.persistence.entities.data.SpeciesAbleBatch; +import fr.ifremer.tutti.persistence.entities.data.SpeciesAbleBatchFrequency; +import fr.ifremer.tutti.persistence.entities.referential.Caracteristic; +import fr.ifremer.tutti.service.PersistenceService; +import org.nuiton.csv.Export; +import org.nuiton.csv.ExportModel; +import org.nuiton.jaxx.application.ApplicationIOUtil; +import org.nuiton.jaxx.application.ApplicationTechnicalException; + +import java.io.Closeable; +import java.io.File; +import java.io.Writer; +import java.util.ArrayList; +import java.util.List; + +import static org.nuiton.i18n.I18n.n; +import static org.nuiton.i18n.I18n.t; + +/** + * Created on 09/03/16. + * + * @author Tony Chemit - chemit@codelutin.com + */ +public class MultiPostExportContext implements MultiPostConstants, Closeable { + + private final File targetFile; + private final File temporaryDirectory; + private final FishingOperation operation; + + private final PersistenceService persistenceService; + + private final List<AttachmentRow> attachmentRows = new ArrayList<>(); + private final List<CatchRow> speciesOrBenthosRows = new ArrayList<>(); + private final List<CatchFrequencyRow> frequencyRows = new ArrayList<>(); + + private final List<AccidentalCatchRow> accidentialCatchRows = new ArrayList<>(); + private final List<MarineLitterRow> marineLitterRows = new ArrayList<>(); + private final List<IndividualObservationRow> individualObservationRows = new ArrayList<>(); + private final List<CaracteristicRow> caracteristicRows = new ArrayList<>(); + + private final List<File> file2zip = new ArrayList<>(); + + public MultiPostExportContext(File targetFile, FishingOperation operation, PersistenceService persistenceService) { + this.targetFile = targetFile; + this.temporaryDirectory = Files.createTempDir(); + this.operation = operation; + this.persistenceService = persistenceService; + } + + public void addAttachments(String batchId, int objectId, ObjectTypeCode objectType) { + List<Attachment> attachments = + persistenceService.getAllAttachments(objectType, objectId); + for (Attachment attachment : attachments) { + AttachmentRow attachmentRow = new AttachmentRow(); + attachmentRow.setBatchId(batchId); + attachmentRow.setName(attachment.getName()); + attachmentRow.setComment(attachment.getComment()); + attachmentRow.setFile(persistenceService.getAttachmentFile(attachment.getId())); + attachmentRows.add(attachmentRow); + } + } + + public void storeSpeciesOrBenthosBatches(String fileName, boolean exportFrequencies, boolean exportIndividualObservations) { + + export(fileName, + new CatchRowModel(CSV_SEPARATOR), + speciesOrBenthosRows, + n("tutti.service.multipost.export.batches.error")); + + if (exportFrequencies) { + + export(FREQUENCIES_FILE, + new CatchFrequencyRowModel(CSV_SEPARATOR), + frequencyRows, + n("tutti.service.multipost.export.frequencies.error")); + + } + + if (exportIndividualObservations) { + + export(INDIVIDUAL_OBSERVATION_FILE, + new IndividualObservationRowModel(CSV_SEPARATOR), + individualObservationRows, + n("tutti.service.multipost.export.batches.error")); + + export(CARACTERISTIC_FILE, + new CaracteristicRowModel(CSV_SEPARATOR), + caracteristicRows, + n("tutti.service.multipost.export.batches.error")); + + } + + } + + public void storeAttachments() { + + AttachmentRowModel csvAttachmentModel = new AttachmentRowModel(CSV_SEPARATOR); + + File attachmentDirectory = new File(temporaryDirectory, ATTACHMENTS_DIRECTORY); + ApplicationIOUtil.forceMkdir(attachmentDirectory, t("tutti.service.multipost.attachment.mkdir.error", attachmentDirectory)); + addFile(attachmentDirectory); + for (AttachmentRow attachmentRow : attachmentRows) { + File attachmentFile = attachmentRow.getFile(); + File destFile = new File(attachmentDirectory, attachmentFile.getName()); + ApplicationIOUtil.copyFile(attachmentFile, + destFile, + t("tutti.service.multipost.attachment.copy.error", attachmentFile)); + addFile(destFile); + } + + export(ATTACHMENTS_FILE, + csvAttachmentModel, + attachmentRows, + n("tutti.service.multipost.export.attachments.error")); + + } + + + @Override + public void close() { + + storeAttachments(); + + try { + ApplicationIOUtil.zip(temporaryDirectory, + targetFile, + file2zip, + n("tutti.service.multipost.export.error")); + } finally { + ApplicationIOUtil.deleteDirectory(temporaryDirectory, t("tutti.service.multipost.export.deleteTempDirectory.error", targetFile)); + } + } + + public <B extends SpeciesAbleBatch> void addSpeciesOrBenthosBatch(String batchId, String parentId, B batch) { + + CatchRow row = new CatchRow(); + row.setId(batchId); + row.setParentId(parentId); + row.setSpecies(batch.getSpecies()); + + row.setCategoryId(batch.getSampleCategoryId()); + row.setCategoryValue(batch.getSampleCategoryValue()); + row.setCategoryWeight(batch.getSampleCategoryWeight()); + row.setWeight(batch.getWeight()); + row.setNumber(batch.getNumber()); + row.setComment(batch.getComment()); + row.setToConfirm(batch.isSpeciesToConfirm()); + speciesOrBenthosRows.add(row); + + addAttachments(batchId, batch.getIdAsInt(), ObjectTypeCode.BATCH); + + } + + public <F extends SpeciesAbleBatchFrequency> void addFrequencies(String rowId, List<F> frequencies) { + + for (F frequency : frequencies) { + CatchFrequencyRow frequencyRow = new CatchFrequencyRow(); + frequencyRow.setBatchId(rowId); + frequencyRow.setLengthStepCaracteristic(frequency.getLengthStepCaracteristic()); + frequencyRow.setLengthStep(frequency.getLengthStep()); + frequencyRow.setNumber(frequency.getNumber()); + frequencyRow.setWeight(frequency.getWeight()); + frequencyRows.add(frequencyRow); + } + + } + + public void addIndividualObservations(String id, IndividualObservationBatch batch) { + + IndividualObservationRow row = new IndividualObservationRow(); + row.setBatchId(id); + + row.setSpecies(batch.getSpecies()); + row.setWeight(batch.getWeight()); + row.setSize(batch.getSize()); + row.setLengthStepCaracteristic(batch.getLengthStepCaracteristic()); +// row.setSamplingCode(batch.getSamplingCode()); +// row.setCalcifiedPieceSamplingCode(batch.getCalcifiedPieceSamplingCode()); + row.setComment(batch.getComment()); + + individualObservationRows.add(row); + + CaracteristicMap caracteristicMap = batch.getCaracteristics(); + for (Caracteristic caracteristic : caracteristicMap.keySet()) { + CaracteristicRow caracteristicRow = new CaracteristicRow(); + caracteristicRow.setBatchId(id); + caracteristicRow.setCaracteristic(caracteristic); + caracteristicRow.setValue(caracteristicMap.get(caracteristic)); + caracteristicRows.add(caracteristicRow); + } + + addAttachments(id, batch.getIdAsInt(), ObjectTypeCode.SAMPLE); + + } + + public void addMarineLitterBatch(String batchId, MarineLitterBatch batch) { + + MarineLitterRow row = new MarineLitterRow(); + row.setBatchId(batchId); + + row.setCategory(batch.getMarineLitterCategory()); + row.setSizeCategory(batch.getMarineLitterSizeCategory()); + row.setNumber(batch.getNumber()); + row.setWeight(batch.getWeight()); + row.setComment(batch.getComment()); + + marineLitterRows.add(row); + + addAttachments(batchId, batch.getIdAsInt(), ObjectTypeCode.BATCH); + + } + + public void addAccidentalCatch(String batchId, AccidentalBatch batch) { + + AccidentalCatchRow row = new AccidentalCatchRow(); + + row.setBatchId(batchId); + + row.setSpecies(batch.getSpecies()); + row.setGender(batch.getGender()); + row.setWeight(batch.getWeight()); + row.setSize(batch.getSize()); + row.setLengthStepCaracteristic(batch.getLengthStepCaracteristic()); + row.setDeadOrAlive(batch.getDeadOrAlive()); + row.setComment(batch.getComment()); + + accidentialCatchRows.add(row); + + CaracteristicMap caracteristicMap = batch.getCaracteristics(); + for (Caracteristic caracteristic : caracteristicMap.keySet()) { + CaracteristicRow caracteristicRow = new CaracteristicRow(); + caracteristicRow.setBatchId(batchId); + caracteristicRow.setCaracteristic(caracteristic); + caracteristicRow.setValue(caracteristicMap.get(caracteristic)); + caracteristicRows.add(caracteristicRow); + } + + addAttachments(batchId, batch.getIdAsInt(), ObjectTypeCode.SAMPLE); + + } + + public <R> void export(String fileName, + ExportModel<R> exportModel, + List<R> rows, + String errorMessage) { + + File file = new File(temporaryDirectory, fileName); + + addFile(file); + + try (Writer writer = ApplicationIOUtil.newWriter(file, n("tutti.service.multipost.export.file.writer.error"))) { + + Export export = Export.newExport(exportModel, rows); + export.write(writer); + + } catch (Exception e) { + throw new ApplicationTechnicalException(t(errorMessage, file), e); + } + + } + + public void storeAccidentalCatches() { + + export(ACCIDENTAL_CATCHES_FILE, + new AccidentalCatchRowModel(CSV_SEPARATOR), + accidentialCatchRows, + n("tutti.service.multipost.export.batches.error")); + + // export caracteristics + + export(CARACTERISTIC_FILE, + new CaracteristicRowModel(CSV_SEPARATOR), + caracteristicRows, + n("tutti.service.multipost.export.batches.error")); + + } + + public void storeMarineLitterBatches() { + + export(MARINE_LITTER_FILE, + new MarineLitterRowModel(CSV_SEPARATOR), + marineLitterRows, + n("tutti.service.multipost.export.batches.error")); + + } + + protected void addFile(File file) { + file2zip.add(file); + } +} diff --git a/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/multipost/MultiPostExportService.java b/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/multipost/MultiPostExportService.java index 32db8b3..e752f33 100644 --- a/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/multipost/MultiPostExportService.java +++ b/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/multipost/MultiPostExportService.java @@ -24,13 +24,11 @@ package fr.ifremer.tutti.service.catches.multipost; import com.google.common.collect.Lists; import com.google.common.collect.Maps; -import com.google.common.io.Files; import fr.ifremer.adagio.core.dao.referential.ObjectTypeCode; -import fr.ifremer.tutti.persistence.entities.CaracteristicMap; import fr.ifremer.tutti.persistence.entities.data.AccidentalBatch; -import fr.ifremer.tutti.persistence.entities.data.Attachment; import fr.ifremer.tutti.persistence.entities.data.BatchContainer; import fr.ifremer.tutti.persistence.entities.data.BenthosBatch; +import fr.ifremer.tutti.persistence.entities.data.BenthosBatchFrequency; import fr.ifremer.tutti.persistence.entities.data.CatchBatch; import fr.ifremer.tutti.persistence.entities.data.FishingOperation; import fr.ifremer.tutti.persistence.entities.data.IndividualObservationBatch; @@ -73,8 +71,6 @@ public class MultiPostExportService extends AbstractTuttiService implements Mult protected DecoratorService decoratorService; - public static final char CSV_SEPARATOR = ';'; - protected Map<String, CaracteristicQualitativeValue> sampleCategoryValueMap; @Override @@ -109,26 +105,36 @@ public class MultiPostExportService extends AbstractTuttiService implements Mult Integer operationId = operation.getIdAsInt(); if (persistenceService.isFishingOperationWithCatchBatch(operationId)) { - // export catch batch weights + try (MultiPostExportContext exportContext = new MultiPostExportContext(file, operation, persistenceService)) { + + // Load data to export + + CatchBatch catchBatch = persistenceService.getCatchBatchFromFishingOperation(operationId); + + CatchBatchRow weights = new CatchBatchRow(); + weights.setCatchTotalWeight(catchBatch.getCatchTotalWeight()); + weights.setCatchTotalRejectedWeight(catchBatch.getCatchTotalRejectedWeight()); + weights.setSpeciesTotalSortedWeight(catchBatch.getSpeciesTotalSortedWeight()); + weights.setBenthosTotalSortedWeight(catchBatch.getBenthosTotalSortedWeight()); + weights.setMarineLitterTotalWeight(catchBatch.getMarineLitterTotalWeight()); - CatchBatch catchBatch = persistenceService.getCatchBatchFromFishingOperation(operationId); + // export catch batch weights - CatchBatchRow weights = new CatchBatchRow(); - weights.setCatchTotalWeight(catchBatch.getCatchTotalWeight()); - weights.setCatchTotalRejectedWeight(catchBatch.getCatchTotalRejectedWeight()); - weights.setSpeciesTotalSortedWeight(catchBatch.getSpeciesTotalSortedWeight()); - weights.setBenthosTotalSortedWeight(catchBatch.getBenthosTotalSortedWeight()); - weights.setMarineLitterTotalWeight(catchBatch.getMarineLitterTotalWeight()); + exportOperation(weights, operation); - List<AttachmentRow> attachmentRows = Lists.newArrayList(); - addAttachments(catchBatch.getId(), catchBatch.getIdAsInt(), ObjectTypeCode.CATCH_BATCH, attachmentRows); + exportContext.addAttachments(catchBatch.getId(), catchBatch.getIdAsInt(), ObjectTypeCode.CATCH_BATCH); - exportOperation(weights, operation); + CatchBatchRowModel csvModel = new CatchBatchRowModel(CSV_SEPARATOR); + + exportContext.export(CATCH_BATCH_FILE, + csvModel, + Lists.newArrayList(weights), + n("tutti.service.multipost.export.weights.error")); + + } - exportCatchBatch(file, - weights, - attachmentRows); } + } /** @@ -137,40 +143,50 @@ public class MultiPostExportService extends AbstractTuttiService implements Mult * @param file the file to export the batches into * @param operation the operation to export */ - public void exportSpecies(File file, FishingOperation operation) { + public void exportSpecies(File file, + FishingOperation operation, + boolean exportFrequencies, + boolean exportIndividualObservations) { Integer operationId = operation.getIdAsInt(); if (persistenceService.isFishingOperationWithCatchBatch(operationId)) { - // create rows + try (MultiPostExportContext exportContext = new MultiPostExportContext(file, operation, persistenceService)) { + + // Export catch weights + attachments + + CatchBatch catchBatch = persistenceService.getCatchBatchFromFishingOperation(operationId); + CatchWeightsRow weights = new CatchWeightsRow(); + weights.setTotalSortedWeight(catchBatch.getSpeciesTotalSortedWeight()); + weights.setInertWeight(catchBatch.getSpeciesTotalInertWeight()); + weights.setLivingNotItemizedWeight(catchBatch.getSpeciesTotalLivingNotItemizedWeight()); + exportOperation(weights, operation); - BatchContainer<SpeciesBatch> speciesBatchContainer = - persistenceService.getRootSpeciesBatch(operationId, false); + exportContext.addAttachments(catchBatch.getId(), catchBatch.getIdAsInt(), ObjectTypeCode.CATCH_BATCH); - List<CatchRow> rows = Lists.newArrayList(); - List<CatchFrequencyRow> frequencyRows = Lists.newArrayList(); - List<AttachmentRow> attachmentRows = Lists.newArrayList(); + exportContext.export(WEIGHTS_FILE, + new CatchWeightsRowModel(CSV_SEPARATOR), + Lists.newArrayList(weights), + n("tutti.service.multipost.export.weights.error")); + + // Export species catch, frequencies and individual observations + + BatchContainer<SpeciesBatch> speciesBatchContainer = persistenceService.getRootSpeciesBatch(operationId, false); + + for (SpeciesBatch batch : speciesBatchContainer.getChildren()) { + createSpeciesRow(exportContext, batch, null, exportFrequencies); + } + + if (exportIndividualObservations) { + addIndividualObservations(exportContext, operation); + } + + exportContext.storeSpeciesOrBenthosBatches(SPECIES_FILE, exportFrequencies, exportIndividualObservations); - for (SpeciesBatch batch : speciesBatchContainer.getChildren()) { - createSpeciesRow(batch, null, rows, frequencyRows, attachmentRows); } - // export catches + weights + attachments - - CatchBatch catchBatch = persistenceService.getCatchBatchFromFishingOperation(operationId); - CatchWeightsRow weights = new CatchWeightsRow(); - weights.setTotalSortedWeight(catchBatch.getSpeciesTotalSortedWeight()); - weights.setInertWeight(catchBatch.getSpeciesTotalInertWeight()); - weights.setLivingNotItemizedWeight(catchBatch.getSpeciesTotalLivingNotItemizedWeight()); - exportOperation(weights, operation); - - exportCatches(file, - SPECIES_FILE, - weights, - rows, - frequencyRows, - attachmentRows); } + } /** @@ -179,39 +195,48 @@ public class MultiPostExportService extends AbstractTuttiService implements Mult * @param file the file to export the batches into * @param operation the operation to export */ - public void exportBenthos(File file, FishingOperation operation) { + public void exportBenthos(File file, + FishingOperation operation, + boolean exportFrequencies, + boolean exportIndividualObservations) { Integer operationId = operation.getIdAsInt(); if (persistenceService.isFishingOperationWithCatchBatch(operationId)) { - BatchContainer<BenthosBatch> benthosBatchContainer = - persistenceService.getRootBenthosBatch(operationId, false); + try (MultiPostExportContext exportContext = new MultiPostExportContext(file, operation, persistenceService)) { + + // Export catch weights + + CatchBatch catchBatch = persistenceService.getCatchBatchFromFishingOperation(operationId); + CatchWeightsRow weights = new CatchWeightsRow(); + weights.setTotalSortedWeight(catchBatch.getBenthosTotalSortedWeight()); + weights.setInertWeight(catchBatch.getBenthosTotalInertWeight()); + weights.setLivingNotItemizedWeight(catchBatch.getBenthosTotalLivingNotItemizedWeight()); + exportOperation(weights, operation); + + exportContext.addAttachments(catchBatch.getId(), catchBatch.getIdAsInt(), ObjectTypeCode.CATCH_BATCH); + + exportContext.export(WEIGHTS_FILE, + new CatchWeightsRowModel(CSV_SEPARATOR), + Lists.newArrayList(weights), + n("tutti.service.multipost.export.weights.error")); + + // Export benthos catch, frequencies and individual observations + + BatchContainer<BenthosBatch> benthosBatchContainer = persistenceService.getRootBenthosBatch(operationId, false); + + for (BenthosBatch batch : benthosBatchContainer.getChildren()) { + createBenthosRow(exportContext, batch, null, exportFrequencies); + } - // create rows + if (exportIndividualObservations) { + addIndividualObservations(exportContext, operation); + } - List<CatchRow> rows = Lists.newArrayList(); - List<CatchFrequencyRow> frequencyRows = Lists.newArrayList(); - List<AttachmentRow> attachmentRows = Lists.newArrayList(); + exportContext.storeSpeciesOrBenthosBatches(BENTHOS_FILE, exportFrequencies, exportIndividualObservations); - for (BenthosBatch batch : benthosBatchContainer.getChildren()) { - createBenthosRow(batch, null, rows, frequencyRows, attachmentRows); } - // export catches + weights + attachments - - CatchBatch catchBatch = persistenceService.getCatchBatchFromFishingOperation(operationId); - CatchWeightsRow weights = new CatchWeightsRow(); - weights.setTotalSortedWeight(catchBatch.getBenthosTotalSortedWeight()); - weights.setInertWeight(catchBatch.getBenthosTotalInertWeight()); - weights.setLivingNotItemizedWeight(catchBatch.getBenthosTotalLivingNotItemizedWeight()); - exportOperation(weights, operation); - - exportCatches(file, - BENTHOS_FILE, - weights, - rows, - frequencyRows, - attachmentRows); } } @@ -226,70 +251,36 @@ public class MultiPostExportService extends AbstractTuttiService implements Mult Integer operationId = operation.getIdAsInt(); if (persistenceService.isFishingOperationWithCatchBatch(operationId)) { - // create rows - - BatchContainer<MarineLitterBatch> marineLitterBatchContainer = - persistenceService.getRootMarineLitterBatch(operationId); - - List<MarineLitterRow> rows = Lists.newArrayList(); - List<AttachmentRow> attachmentRows = Lists.newArrayList(); + try (MultiPostExportContext exportContext = new MultiPostExportContext(file, operation, persistenceService)) { - for (MarineLitterBatch batch : marineLitterBatchContainer.getChildren()) { - MarineLitterRow row = new MarineLitterRow(); + // export weights - String id = context.generateId(MarineLitterRow.class); - row.setBatchId(id); + CatchBatch catchBatch = persistenceService.getCatchBatchFromFishingOperation(operationId); + MarineLitterWeightRow weight = new MarineLitterWeightRow(); + weight.setTotalWeight(catchBatch.getMarineLitterTotalWeight()); + exportOperation(weight, operation); - row.setCategory(batch.getMarineLitterCategory()); - row.setSizeCategory(batch.getMarineLitterSizeCategory()); - row.setNumber(batch.getNumber()); - row.setWeight(batch.getWeight()); - row.setComment(batch.getComment()); - - rows.add(row); - - addAttachments(id, - batch.getIdAsInt(), - ObjectTypeCode.BATCH, - attachmentRows); - } + exportContext.export(WEIGHTS_FILE, + new MarineLitterWeightRowModel(CSV_SEPARATOR), + Lists.newArrayList(weight), + n("tutti.service.multipost.export.weight.error")); + // export marine litters - File directory = Files.createTempDir(); - List<File> file2zip = Lists.newArrayList(); + BatchContainer<MarineLitterBatch> marineLitterBatchContainer = + persistenceService.getRootMarineLitterBatch(operationId); - // export marine litters + for (MarineLitterBatch batch : marineLitterBatchContainer.getChildren()) { - MarineLitterRowModel marineLitterRowModel = new MarineLitterRowModel(CSV_SEPARATOR); - File marineLitterFile = new File(directory, MARINE_LITTER_FILE); - file2zip.add(marineLitterFile); - export(marineLitterFile, - marineLitterRowModel, - rows, - n("tutti.service.multipost.export.batches.error")); + String id = context.generateId(MarineLitterRow.class); + exportContext.addMarineLitterBatch(id, batch); - // export weights - - CatchBatch catchBatch = persistenceService.getCatchBatchFromFishingOperation(operationId); - MarineLitterWeightRow weight = new MarineLitterWeightRow(); - weight.setTotalWeight(catchBatch.getMarineLitterTotalWeight()); - exportOperation(weight, operation); - - MarineLitterWeightRowModel weightModel = new MarineLitterWeightRowModel(CSV_SEPARATOR); + } - File weightFile = new File(directory, WEIGHTS_FILE); - file2zip.add(weightFile); - export(weightFile, - weightModel, - Lists.newArrayList(weight), - n("tutti.service.multipost.export.weight.error")); + exportContext.storeMarineLitterBatches(); - // export attachments + create final zip + } - exportAttachmentsAndCreateZip(file, - directory, - file2zip, - attachmentRows); } } @@ -300,87 +291,9 @@ public class MultiPostExportService extends AbstractTuttiService implements Mult * @param operation the operation to export */ public void exportIndividualObservation(File file, FishingOperation operation) { - List<IndividualObservationBatch> individualObservations = - persistenceService.getAllIndividualObservationBatchsForFishingOperation(operation.getIdAsInt()); - - // create rows - - List<IndividualObservationRow> rows = Lists.newArrayList(); - List<CaracteristicRow> caracteristicRows = Lists.newArrayList(); - List<AttachmentRow> attachmentRows = Lists.newArrayList(); - - - for (IndividualObservationBatch batch : individualObservations) { - IndividualObservationRow row = new IndividualObservationRow(); - - String id = context.generateId(IndividualObservationRow.class); - row.setBatchId(id); - - row.setSpecies(batch.getSpecies()); - row.setWeight(batch.getWeight()); - row.setSize(batch.getSize()); - row.setLengthStepCaracteristic(batch.getLengthStepCaracteristic()); -// row.setSamplingCode(batch.getSamplingCode()); -// row.setCalcifiedPieceSamplingCode(batch.getCalcifiedPieceSamplingCode()); - row.setComment(batch.getComment()); - - rows.add(row); - - CaracteristicMap caracteristicMap = batch.getCaracteristics(); - for (Caracteristic caracteristic : caracteristicMap.keySet()) { - CaracteristicRow caracteristicRow = new CaracteristicRow(); - caracteristicRow.setBatchId(id); - caracteristicRow.setCaracteristic(caracteristic); - caracteristicRow.setValue(caracteristicMap.get(caracteristic)); - caracteristicRows.add(caracteristicRow); - } - - addAttachments(id, batch.getIdAsInt(), ObjectTypeCode.SAMPLE, attachmentRows); - } - - File directory = Files.createTempDir(); - List<File> file2zip = Lists.newArrayList(); - - // export individual observations - IndividualObservationRowModel csvModel = new IndividualObservationRowModel(CSV_SEPARATOR); - File individualObservationFile = new File(directory, INDIVIDUAL_OBSERVATION_FILE); - file2zip.add(individualObservationFile); - export(individualObservationFile, - csvModel, - rows, - n("tutti.service.multipost.export.batches.error")); + //FIXME Remove this method - // export caracteristics - - CaracteristicRowModel caracteristicCsvModel = new CaracteristicRowModel(CSV_SEPARATOR); - File caracteristicFile = new File(directory, CARACTERISTIC_FILE); - file2zip.add(caracteristicFile); - export(caracteristicFile, - caracteristicCsvModel, - caracteristicRows, - n("tutti.service.multipost.export.batches.error")); - - // export operation - - FishingOperationRow foRow = new FishingOperationRow(); - exportOperation(foRow, operation); - - FishingOperationRowModel foRowModel = new FishingOperationRowModel(CSV_SEPARATOR); - - File weightFile = new File(directory, WEIGHTS_FILE); - file2zip.add(weightFile); - export(weightFile, - foRowModel, - Lists.newArrayList(foRow), - n("tutti.service.multipost.export.operation.error")); - - // export attachments + create final zip - - exportAttachmentsAndCreateZip(file, - directory, - file2zip, - attachmentRows); } /** @@ -390,217 +303,97 @@ public class MultiPostExportService extends AbstractTuttiService implements Mult * @param operation the operation to export */ public void exportAccidentalCatch(File file, FishingOperation operation) { - List<AccidentalBatch> accidentalCatches = - persistenceService.getAllAccidentalBatch(operation.getIdAsInt()); - - List<AccidentalCatchRow> rows = Lists.newArrayList(); - List<CaracteristicRow> caracteristicRows = Lists.newArrayList(); - List<AttachmentRow> attachmentRows = Lists.newArrayList(); - - // create rows - - for (AccidentalBatch batch : accidentalCatches) { - AccidentalCatchRow row = new AccidentalCatchRow(); - - String id = context.generateId(AccidentalCatchRow.class); - row.setBatchId(id); - - row.setSpecies(batch.getSpecies()); - row.setGender(batch.getGender()); - row.setWeight(batch.getWeight()); - row.setSize(batch.getSize()); - row.setLengthStepCaracteristic(batch.getLengthStepCaracteristic()); - row.setDeadOrAlive(batch.getDeadOrAlive()); - row.setComment(batch.getComment()); - - rows.add(row); - - CaracteristicMap caracteristicMap = batch.getCaracteristics(); - for (Caracteristic caracteristic : caracteristicMap.keySet()) { - CaracteristicRow caracteristicRow = new CaracteristicRow(); - caracteristicRow.setBatchId(id); - caracteristicRow.setCaracteristic(caracteristic); - caracteristicRow.setValue(caracteristicMap.get(caracteristic)); - caracteristicRows.add(caracteristicRow); - } - addAttachments(id, batch.getIdAsInt(), ObjectTypeCode.SAMPLE, attachmentRows); - } + try (MultiPostExportContext exportContext = new MultiPostExportContext(file, operation, persistenceService)) { + + // export operation - // export accidental catches + FishingOperationRow foRow = new FishingOperationRow(); + exportOperation(foRow, operation); - AccidentalCatchRowModel csvModel = new AccidentalCatchRowModel(CSV_SEPARATOR); + FishingOperationRowModel foRowModel = new FishingOperationRowModel(CSV_SEPARATOR); - File directory = Files.createTempDir(); - List<File> file2zip = Lists.newArrayList(); + exportContext.export(WEIGHTS_FILE, + foRowModel, + Lists.newArrayList(foRow), + n("tutti.service.multipost.export.operation.error")); - File accidentalCatchesFile = new File(directory, ACCIDENTAL_CATCHES_FILE); - file2zip.add(accidentalCatchesFile); - export(accidentalCatchesFile, - csvModel, - rows, - n("tutti.service.multipost.export.batches.error")); + // export accidental catches - // export caracteristics + List<AccidentalBatch> accidentalCatches = + persistenceService.getAllAccidentalBatch(operation.getIdAsInt()); - CaracteristicRowModel caracteristicCsvModel = new CaracteristicRowModel(CSV_SEPARATOR); - File caracteristicFile = new File(directory, CARACTERISTIC_FILE); - file2zip.add(caracteristicFile); - export(caracteristicFile, - caracteristicCsvModel, - caracteristicRows, - n("tutti.service.multipost.export.batches.error")); + for (AccidentalBatch batch : accidentalCatches) { - // export operation + String id = context.generateId(AccidentalCatchRow.class); + exportContext.addAccidentalCatch(id, batch); - FishingOperationRow foRow = new FishingOperationRow(); - exportOperation(foRow, operation); + } - FishingOperationRowModel foRowModel = new FishingOperationRowModel(CSV_SEPARATOR); + exportContext.storeAccidentalCatches(); - File weightFile = new File(directory, WEIGHTS_FILE); - file2zip.add(weightFile); - export(weightFile, - foRowModel, - Lists.newArrayList(foRow), - n("tutti.service.multipost.export.operation.error")); - // export attachments + create final zip + } - exportAttachmentsAndCreateZip(file, - directory, - file2zip, - attachmentRows); } //------------------------------------------------------------------------// //-- Internal methods --// //------------------------------------------------------------------------// - protected void exportCatchBatch(File file, - CatchBatchRow weights, - List<AttachmentRow> attachmentRows) { - - CatchBatchRowModel csvModel = new CatchBatchRowModel(CSV_SEPARATOR); - - File directory = Files.createTempDir(); - List<File> file2zip = Lists.newArrayList(); + protected void createSpeciesRow(MultiPostExportContext exportContext, + SpeciesBatch batch, + String parentId, + boolean addFrequencies) { - File weightsFile = new File(directory, CATCH_BATCH_FILE); - file2zip.add(weightsFile); - export(weightsFile, - csvModel, - Lists.newArrayList(weights), - n("tutti.service.multipost.export.weights.error")); + String id = context.generateId(CatchRow.class); - // export attachments + create final zip + exportContext.addSpeciesOrBenthosBatch(id, parentId, batch); - exportAttachmentsAndCreateZip(file, - directory, - file2zip, - attachmentRows); - } + if (addFrequencies) { + List<SpeciesBatchFrequency> frequencies = persistenceService.getAllSpeciesBatchFrequency(batch.getIdAsInt()); + exportContext.addFrequencies(id, frequencies); + } - protected void exportCatches(File file, - String batchFile, - CatchWeightsRow weights, - List<CatchRow> rows, - List<CatchFrequencyRow> frequencyRows, - List<AttachmentRow> attachmentRows) { - CatchRowModel csvModel = new CatchRowModel(CSV_SEPARATOR); - CatchFrequencyRowModel csvFrequencyModel = new CatchFrequencyRowModel(CSV_SEPARATOR); - CatchWeightsRowModel catchWeightsModel = new CatchWeightsRowModel(CSV_SEPARATOR); - - File directory = Files.createTempDir(); - List<File> file2zip = Lists.newArrayList(); - - File weightsFile = new File(directory, WEIGHTS_FILE); - file2zip.add(weightsFile); - export(weightsFile, - catchWeightsModel, - Lists.newArrayList(weights), - n("tutti.service.multipost.export.weights.error")); - - File speciesFile = new File(directory, batchFile); - file2zip.add(speciesFile); - export(speciesFile, - csvModel, - rows, - n("tutti.service.multipost.export.batches.error")); - - File frequencyFile = new File(directory, FREQUENCIES_FILE); - file2zip.add(frequencyFile); - export(frequencyFile, - csvFrequencyModel, - frequencyRows, - n("tutti.service.multipost.export.frequencies.error")); - - // export attachments + create final zip - - exportAttachmentsAndCreateZip(file, - directory, - file2zip, - attachmentRows); + for (SpeciesBatch child : batch.getChildBatchs()) { + createSpeciesRow(exportContext, child, id, addFrequencies); + } } - protected void createSpeciesRow(SpeciesBatch batch, + protected void createBenthosRow(MultiPostExportContext exportContext, + BenthosBatch batch, String parentId, - List<CatchRow> rows, - List<CatchFrequencyRow> frequencyRows, - List<AttachmentRow> attachmentRows) { - CatchRow row = new CatchRow(); + boolean addFrequencies) { String id = context.generateId(CatchRow.class); - row.setId(id); - row.setParentId(parentId); - row.setSpecies(batch.getSpecies()); - - row.setCategoryId(batch.getSampleCategoryId()); - row.setCategoryValue(batch.getSampleCategoryValue()); - row.setCategoryWeight(batch.getSampleCategoryWeight()); - row.setWeight(batch.getWeight()); - row.setNumber(batch.getNumber()); - row.setComment(batch.getComment()); - row.setToConfirm(batch.isSpeciesToConfirm()); - rows.add(row); + exportContext.addSpeciesOrBenthosBatch(id, parentId, batch); - addFrequencies(id, batch.getIdAsInt(), frequencyRows); - addAttachments(id, batch.getIdAsInt(), ObjectTypeCode.BATCH, attachmentRows); + if (addFrequencies) { + List<BenthosBatchFrequency> frequencies = + persistenceService.getAllBenthosBatchFrequency(batch.getIdAsInt()); + exportContext.addFrequencies(id, frequencies); + } - for (SpeciesBatch child : batch.getChildBatchs()) { - createSpeciesRow(child, id, rows, frequencyRows, attachmentRows); + for (BenthosBatch child : batch.getChildBatchs()) { + createBenthosRow(exportContext, child, id, addFrequencies); } } - protected void createBenthosRow(BenthosBatch batch, - String parentId, - List<CatchRow> rows, - List<CatchFrequencyRow> frequencyRows, - List<AttachmentRow> attachmentRows) { - CatchRow row = new CatchRow(); + protected void addIndividualObservations(MultiPostExportContext exportContext, FishingOperation operation) { - String id = context.generateId(CatchRow.class); - row.setId(id); - row.setParentId(parentId); - row.setSpecies(batch.getSpecies()); + List<IndividualObservationBatch> individualObservations = + persistenceService.getAllIndividualObservationBatchsForFishingOperation(operation.getIdAsInt()); - row.setCategoryId(batch.getSampleCategoryId()); - row.setCategoryValue(batch.getSampleCategoryValue()); - row.setCategoryWeight(batch.getSampleCategoryWeight()); - row.setWeight(batch.getWeight()); - row.setNumber(batch.getNumber()); - row.setComment(batch.getComment()); - row.setToConfirm(batch.isSpeciesToConfirm()); + for (IndividualObservationBatch batch : individualObservations) { - rows.add(row); - addFrequencies(id, batch.getIdAsInt(), frequencyRows); - addAttachments(id, batch.getIdAsInt(), ObjectTypeCode.BATCH, attachmentRows); + String id = context.generateId(IndividualObservationRow.class); + + exportContext.addIndividualObservations(id, batch); - for (BenthosBatch child : batch.getChildBatchs()) { - createBenthosRow(child, id, rows, frequencyRows, attachmentRows); } + } protected void exportOperation(AbstractFishingOperationRow afoRow, FishingOperation operation) { @@ -610,39 +403,6 @@ public class MultiPostExportService extends AbstractTuttiService implements Mult afoRow.setDate(operation.getGearShootingStartDate()); } - protected void addFrequencies(String rowId, - Integer batchId, - List<CatchFrequencyRow> frequencyRows) { - List<SpeciesBatchFrequency> frequencies = - persistenceService.getAllSpeciesBatchFrequency(batchId); - for (SpeciesBatchFrequency frequency : frequencies) { - CatchFrequencyRow frequencyRow = new CatchFrequencyRow(); - frequencyRow.setBatchId(rowId); - frequencyRow.setLengthStepCaracteristic(frequency.getLengthStepCaracteristic()); - frequencyRow.setLengthStep(frequency.getLengthStep()); - frequencyRow.setNumber(frequency.getNumber()); - frequencyRow.setWeight(frequency.getWeight()); - frequencyRows.add(frequencyRow); - } - } - - protected void addAttachments(String batchId, - int objectId, - ObjectTypeCode objectType, - List<AttachmentRow> attachmentRows) { - List<Attachment> attachments = - persistenceService.getAllAttachments(objectType, objectId); - for (Attachment attachment : attachments) { - AttachmentRow attachmentRow = new AttachmentRow(); - attachmentRow.setBatchId(batchId); - attachmentRow.setName(attachment.getName()); - attachmentRow.setComment(attachment.getComment()); - attachmentRow.setFile(persistenceService.getAttachmentFile(attachment.getId())); - attachmentRows.add(attachmentRow); - } - } - - protected <R> void export(File file, ExportModel<R> exportModel, List<R> rows, @@ -662,46 +422,4 @@ public class MultiPostExportService extends AbstractTuttiService implements Mult } } - protected void exportAttachments(File directory, - List<File> file2zip, - List<AttachmentRow> attachmentRows) { - - AttachmentRowModel csvAttachmentModel = new AttachmentRowModel(CSV_SEPARATOR); - - File attachmentDirectory = new File(directory, ATTACHMENTS_DIRECTORY); - ApplicationIOUtil.forceMkdir(attachmentDirectory, t("tutti.service.multipost.attachment.mkdir.error", attachmentDirectory)); - file2zip.add(attachmentDirectory); - for (AttachmentRow attachmentRow : attachmentRows) { - File attachmentFile = attachmentRow.getFile(); - File destFile = new File(attachmentDirectory, attachmentFile.getName()); - ApplicationIOUtil.copyFile(attachmentFile, - destFile, - t("tutti.service.multipost.attachment.copy.error", attachmentFile)); - file2zip.add(destFile); - } - - File attachmentFile = new File(directory, ATTACHMENTS_FILE); - file2zip.add(attachmentFile); - export(attachmentFile, - csvAttachmentModel, - attachmentRows, - n("tutti.service.multipost.export.attachments.error")); - - } - - protected void exportAttachmentsAndCreateZip(File file, - File directory, - List<File> file2zip, - List<AttachmentRow> attachmentRows) { - exportAttachments(directory, file2zip, attachmentRows); - - try { - ApplicationIOUtil.zip(directory, - file, - file2zip, - n("tutti.service.multipost.export.error")); - } finally { - ApplicationIOUtil.deleteDirectory(directory, t("tutti.service.multipost.export.deleteTempDirectory.error", file)); - } - } } diff --git a/tutti-service/src/test/java/fr/ifremer/tutti/service/catches/multipost/MultiPostExportServiceTest.java b/tutti-service/src/test/java/fr/ifremer/tutti/service/catches/multipost/MultiPostExportServiceTest.java index 7550f97..6f3b062 100644 --- a/tutti-service/src/test/java/fr/ifremer/tutti/service/catches/multipost/MultiPostExportServiceTest.java +++ b/tutti-service/src/test/java/fr/ifremer/tutti/service/catches/multipost/MultiPostExportServiceTest.java @@ -154,7 +154,7 @@ public class MultiPostExportServiceTest { Assert.assertFalse(exportFile.exists()); - service.exportSpecies(exportFile, dataContext.operations.get(0)); + service.exportSpecies(exportFile, dataContext.operations.get(0), true, false); Assert.assertTrue(exportFile.exists()); dbResource.assertZipContent( @@ -177,7 +177,7 @@ public class MultiPostExportServiceTest { Assert.assertFalse(exportFile.exists()); - service.exportBenthos(exportFile, dataContext.operations.get(0)); + service.exportBenthos(exportFile, dataContext.operations.get(0), true, true); Assert.assertTrue(exportFile.exists()); dbResource.assertZipContent( @@ -212,27 +212,27 @@ public class MultiPostExportServiceTest { ); } - @Test - public void testExportIndividualObservation() throws Exception { - File exportFile = new File(dataDirectory, - "exportIndividualObservation.zip"); - - Files.createParentDirs(exportFile); - - Assert.assertFalse(exportFile.exists()); - - service.exportIndividualObservation(exportFile, dataContext.operations.get(0)); - Assert.assertTrue(exportFile.exists()); - - dbResource.assertZipContent( - "IndividualObservation export", - exportFile, - Pair.of(MultiPostExportService.INDIVIDUAL_OBSERVATION_FILE, INDIVIDUAL_OBSERVATION_CONTENT), - Pair.of(MultiPostExportService.CARACTERISTIC_FILE, INDIVIDUAL_OBSERVATION_CARACTERISTIC_CONTENT), - Pair.of(MultiPostExportService.WEIGHTS_FILE, INDIVIDUAL_OBSERVATION_WEIGHTS_CONTENT), - Pair.of(MultiPostExportService.ATTACHMENTS_FILE, INDIVIDUAL_OBSERVATION_ATTACHMENT_CONTENT) - ); - } +// @Test +// public void testExportIndividualObservation() throws Exception { +// File exportFile = new File(dataDirectory, +// "exportIndividualObservation.zip"); +// +// Files.createParentDirs(exportFile); +// +// Assert.assertFalse(exportFile.exists()); +// +// service.exportIndividualObservation(exportFile, dataContext.operations.get(0)); +// Assert.assertTrue(exportFile.exists()); +// +// dbResource.assertZipContent( +// "IndividualObservation export", +// exportFile, +// Pair.of(MultiPostExportService.INDIVIDUAL_OBSERVATION_FILE, INDIVIDUAL_OBSERVATION_CONTENT), +// Pair.of(MultiPostExportService.CARACTERISTIC_FILE, INDIVIDUAL_OBSERVATION_CARACTERISTIC_CONTENT), +// Pair.of(MultiPostExportService.WEIGHTS_FILE, INDIVIDUAL_OBSERVATION_WEIGHTS_CONTENT), +// Pair.of(MultiPostExportService.ATTACHMENTS_FILE, INDIVIDUAL_OBSERVATION_ATTACHMENT_CONTENT) +// ); +// } @Test public void testExportAccidentalCatch() throws Exception { -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@codelutin.com>.