Author: tchemit Date: 2013-07-30 19:22:49 +0200 (Tue, 30 Jul 2013) New Revision: 1161 Url: http://forge.codelutin.com/projects/tutti/repository/revisions/1161 Log: refactor multiposte import/export + add tests for export (need to add some for import) Added: trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/multipost/TuttiMultiPostExportService.java trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/multipost/TuttiMultiPostImportService.java trunk/tutti-service/src/test/java/fr/ifremer/tutti/service/catches/multipost/ trunk/tutti-service/src/test/java/fr/ifremer/tutti/service/catches/multipost/TuttiMultiPostExportServiceTest.java Modified: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/TuttiIOUtil.java trunk/tutti-persistence/src/main/resources/i18n/tutti-persistence_en_GB.properties trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/TuttiServiceContext.java trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/multipost/CatchRow.java trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/multipost/CatchRowModel.java trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/multipost/TuttiMultiPostImportExportService.java trunk/tutti-service/src/main/resources/i18n/tutti-service_en_GB.properties trunk/tutti-service/src/main/resources/i18n/tutti-service_fr_FR.properties trunk/tutti-service/src/test/java/fr/ifremer/tutti/service/ServiceDbResource.java trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/TuttiUIContext.java trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/AbstractExportMultiPostAction.java trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/AbstractImportMultiPostAction.java trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/util/attachment/SaveAttachmentAction.java Modified: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/TuttiIOUtil.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/TuttiIOUtil.java 2013-07-27 21:21:41 UTC (rev 1160) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/TuttiIOUtil.java 2013-07-30 17:22:49 UTC (rev 1161) @@ -24,6 +24,8 @@ * #L% */ +import com.google.common.base.Charsets; +import com.google.common.io.Files; import org.apache.commons.io.FileUtils; import org.apache.commons.io.FilenameUtils; import org.apache.commons.vfs2.FileObject; @@ -34,8 +36,15 @@ import java.io.Closeable; import java.io.File; +import java.io.FileNotFoundException; import java.io.IOException; +import java.io.Reader; +import java.io.Writer; +import java.util.List; +import java.util.UUID; +import static org.nuiton.i18n.I18n._; + /** * Useful methods around IO, all method errors are boxes a * {@link TuttiTechnicalException}. @@ -66,6 +75,14 @@ } } + public static void zip(File source, File archiveFile, List<File> files, String errorMessage) { + try { + ZipUtil.compressFiles(archiveFile, source, files); + } catch (IOException e) { + throw new TuttiTechnicalException(_(errorMessage, archiveFile), e); + } + } + public static FileObject resolveFile(String file, String errorMessage) { try { FileSystemManager manager = VFS.getManager(); @@ -168,13 +185,6 @@ } } -// public static File addExtensionIfMissing(File file, String extension) { -// if (!file.getName().endsWith(extension)) { -// file = new File(file.getParentFile(), file.getName() + extension); -// } -// return file; -// } - public static String getBaseName(String file) { return FilenameUtils.getBaseName(file); } @@ -182,4 +192,35 @@ public static String getExtension(String file) { return FilenameUtils.getExtension(file); } + + public static Reader newReader(File file, String errorMessage) { + try { + return Files.newReader(file, Charsets.UTF_8); + + } catch (FileNotFoundException e) { + throw new TuttiTechnicalException(_(errorMessage, file), e); + } + } + + public static Writer newWriter(File file, String errorMessage + ) { + try { + return Files.newWriter(file, Charsets.UTF_8); + + } catch (FileNotFoundException e) { + throw new TuttiTechnicalException(_(errorMessage, file), e); + } + } + + public static File explodeZip(File rootDirectory, + File file, + String errorMessage) { + File tempDir = new File(rootDirectory, UUID.randomUUID().toString()); + try { + ZipUtil.uncompress(file, tempDir); + } catch (IOException e) { + throw new TuttiTechnicalException(_(errorMessage, file)); + } + return tempDir; + } } Modified: trunk/tutti-persistence/src/main/resources/i18n/tutti-persistence_en_GB.properties =================================================================== --- trunk/tutti-persistence/src/main/resources/i18n/tutti-persistence_en_GB.properties 2013-07-27 21:21:41 UTC (rev 1160) +++ trunk/tutti-persistence/src/main/resources/i18n/tutti-persistence_en_GB.properties 2013-07-30 17:22:49 UTC (rev 1161) @@ -1,4 +1,6 @@ nuitonutil.error.no.convertor= +tutti.common.caracteristic.notFound= +tutti.common.protocol.categories.not.compatible= tutti.config.option.persistence.SampleCategoryModel.description= tutti.config.option.persistence.db.attachment.directory.description= tutti.config.option.persistence.db.cache.directory.description= Modified: trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/TuttiServiceContext.java =================================================================== --- trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/TuttiServiceContext.java 2013-07-27 21:21:41 UTC (rev 1160) +++ trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/TuttiServiceContext.java 2013-07-30 17:22:49 UTC (rev 1161) @@ -42,6 +42,7 @@ import java.io.IOException; import java.lang.reflect.Constructor; import java.util.Date; +import java.util.UUID; import java.util.concurrent.ExecutionException; import static org.nuiton.i18n.I18n._; @@ -156,4 +157,8 @@ Preconditions.checkNotNull(result, "Need a not null sample category model"); return result; } + + public String generateId(Class<?> type) { + return UUID.randomUUID().toString(); + } } Modified: trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/multipost/CatchRow.java =================================================================== --- trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/multipost/CatchRow.java 2013-07-27 21:21:41 UTC (rev 1160) +++ trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/multipost/CatchRow.java 2013-07-30 17:22:49 UTC (rev 1161) @@ -44,16 +44,6 @@ public static final String SPECIES = "species"; -// public static final String SORTED_UNSORTED_SAMPLE_CATEGORY = "sortedUnsortedSampleCategory"; -// -// public static final String SIZE_SAMPLE_CATEGORY = "sizeSampleCategory"; -// -// public static final String SEX_SAMPLE_CATEGORY = "sexSampleCategory"; -// -// public static final String MATURITY_SAMPLE_CATEGORY = "maturitySampleCategory"; -// -// public static final String AGE_SAMPLE_CATEGORY = "ageSampleCategory"; - public static final String CATEGORY_ID = "categoryId"; public static final String CATEGORY_VALUE = "categoryValue"; @@ -78,16 +68,6 @@ protected Serializable categoryValue; -// protected Serializable sortedUnsortedSampleCategory; -// -// protected Serializable sizeSampleCategory; -// -// protected Serializable sexSampleCategory; -// -// protected Serializable maturitySampleCategory; -// -// protected Serializable ageSampleCategory; - protected Float categoryWeight; protected Float weight; @@ -138,46 +118,6 @@ this.categoryValue = categoryValue; } -// public Serializable getSortedUnsortedSampleCategory() { -// return sortedUnsortedSampleCategory; -// } -// -// public void setSortedUnsortedSampleCategory(Serializable sortedUnsortedSampleCategory) { -// this.sortedUnsortedSampleCategory = sortedUnsortedSampleCategory; -// } -// -// public Serializable getSizeSampleCategory() { -// return sizeSampleCategory; -// } -// -// public void setSizeSampleCategory(Serializable sizeSampleCategory) { -// this.sizeSampleCategory = sizeSampleCategory; -// } -// -// public Serializable getSexSampleCategory() { -// return sexSampleCategory; -// } -// -// public void setSexSampleCategory(Serializable sexSampleCategory) { -// this.sexSampleCategory = sexSampleCategory; -// } -// -// public Serializable getMaturitySampleCategory() { -// return maturitySampleCategory; -// } -// -// public void setMaturitySampleCategory(Serializable maturitySampleCategory) { -// this.maturitySampleCategory = maturitySampleCategory; -// } -// -// public Serializable getAgeSampleCategory() { -// return ageSampleCategory; -// } -// -// public void setAgeSampleCategory(Serializable ageSampleCategory) { -// this.ageSampleCategory = ageSampleCategory; -// } - public Float getCategoryWeight() { return categoryWeight; } Modified: trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/multipost/CatchRowModel.java =================================================================== --- trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/multipost/CatchRowModel.java 2013-07-27 21:21:41 UTC (rev 1160) +++ trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/multipost/CatchRowModel.java 2013-07-30 17:22:49 UTC (rev 1161) @@ -89,37 +89,16 @@ Species.PROPERTY_REFERENCE_TAXON_ID, speciesMap); - //FIXME: deal with it! -// newColumnForExport(CatchRow.SORTED_UNSORTED_SAMPLE_CATEGORY, -// caracteristicValueFormatter); -// -// newMandatoryColumn(CatchRow.SORTED_UNSORTED_SAMPLE_CATEGORY, -// TuttiCsvUtil.STRING); -// -// newColumnForExport(CatchRow.SIZE_SAMPLE_CATEGORY, -// caracteristicValueFormatter); -// -// newMandatoryColumn(CatchRow.SIZE_SAMPLE_CATEGORY, -// TuttiCsvUtil.STRING); -// -// newColumnForExport(CatchRow.SEX_SAMPLE_CATEGORY, -// caracteristicValueFormatter); -// -// newMandatoryColumn(CatchRow.SEX_SAMPLE_CATEGORY, -// TuttiCsvUtil.STRING); -// -// newColumnForExport(CatchRow.MATURITY_SAMPLE_CATEGORY, -// caracteristicValueFormatter); -// -// newMandatoryColumn(CatchRow.MATURITY_SAMPLE_CATEGORY, -// TuttiCsvUtil.STRING); -// -// newColumnForExport(CatchRow.AGE_SAMPLE_CATEGORY, -// caracteristicValueFormatter); -// -// newMandatoryColumn(CatchRow.AGE_SAMPLE_CATEGORY, -// TuttiCsvUtil.STRING); + newColumnForImportExport(CatchRow.CATEGORY_ID, + TuttiCsvUtil.INTEGER); + + newColumnForExport(CatchRow.CATEGORY_VALUE, + caracteristicValueFormatter); + + newMandatoryColumn(CatchRow.CATEGORY_VALUE, + TuttiCsvUtil.STRING); + newColumnForImportExport(CatchRow.CATEGORY_WEIGHT, TuttiCsvUtil.FLOAT); Copied: trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/multipost/TuttiMultiPostExportService.java (from rev 1156, trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/multipost/TuttiMultiPostImportExportService.java) =================================================================== --- trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/multipost/TuttiMultiPostExportService.java (rev 0) +++ trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/multipost/TuttiMultiPostExportService.java 2013-07-30 17:22:49 UTC (rev 1161) @@ -0,0 +1,683 @@ +package fr.ifremer.tutti.service.catches.multipost; + +/* + * #%L + * Tutti :: Service + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2012 - 2013 Ifremer + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/gpl-3.0.html>. + * #L% + */ + +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.google.common.io.Files; +import fr.ifremer.tutti.TuttiIOUtil; +import fr.ifremer.tutti.TuttiTechnicalException; +import fr.ifremer.tutti.persistence.entities.CaracteristicMap; +import fr.ifremer.tutti.persistence.entities.data.AccidentalBatch; +import fr.ifremer.tutti.persistence.entities.data.AttachementObjectTypeEnum; +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.CatchBatch; +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.SampleCategoryModel; +import fr.ifremer.tutti.persistence.entities.data.SampleCategoryModelEntry; +import fr.ifremer.tutti.persistence.entities.data.SpeciesBatch; +import fr.ifremer.tutti.persistence.entities.data.SpeciesBatchFrequency; +import fr.ifremer.tutti.persistence.entities.referential.Caracteristic; +import fr.ifremer.tutti.persistence.entities.referential.CaracteristicQualitativeValue; +import fr.ifremer.tutti.persistence.entities.referential.CaracteristicType; +import fr.ifremer.tutti.service.AbstractTuttiService; +import fr.ifremer.tutti.service.DecoratorService; +import fr.ifremer.tutti.service.PersistenceService; +import fr.ifremer.tutti.service.TuttiServiceContext; +import org.apache.commons.io.IOUtils; +import org.nuiton.csv.Export; +import org.nuiton.csv.ExportModel; + +import java.io.File; +import java.io.Writer; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +import static org.nuiton.i18n.I18n._; +import static org.nuiton.i18n.I18n.n_; + +/** + * Service to export batches from a satellite post into a master post. + * + * @author kmorin <kmorin@codelutin.com> + * @author tchemit <chemit@codelutin.com> + * @since 2.2 + */ +public class TuttiMultiPostExportService extends AbstractTuttiService { + +// private static final Log log = +// LogFactory.getLog(TuttiMultiPostImportExportService.class); + + public static final String BATCHES_KEY = "batchesKey"; + + protected static final String ATTACHMENTS_DIRECTORY = "attachments"; + + protected static final String ATTACHMENTS_FILE = "attachments.csv"; + + protected static final String SPECIES_FILE = "species.csv"; + + protected static final String BENTHOS_FILE = "benthos.csv"; + + protected static final String MARINE_LITTER_FILE = "marineLitter.csv"; + + protected static final String INDIVIDUAL_OBSERVATION_FILE = "individualObservation.csv"; + + protected static final String ACCIDENTAL_CATCHES_FILE = "accidentalCatches.csv"; + + protected static final String FREQUENCIES_FILE = "frequencies.csv"; + + protected static final String CARACTERISTIC_FILE = "caracteristics.csv"; + + protected static final String WEIGHTS_FILE = "weights.csv"; + + protected PersistenceService persistenceService; + + protected DecoratorService decoratorService; + + protected char csvSeparator; + + protected SampleCategoryModel sampleCategoryModel; + + protected Map<String, CaracteristicQualitativeValue> sampleCategoryValueMap; + + @Override + public void setServiceContext(TuttiServiceContext context) { + super.setServiceContext(context); + persistenceService = getService(PersistenceService.class); + decoratorService = getService(DecoratorService.class); + + csvSeparator = context.getConfig().getCsvSeparator(); + + sampleCategoryModel = context.getSampleCategoryModel(); + + sampleCategoryValueMap = Maps.newTreeMap(); + + for (SampleCategoryModelEntry sampleCategoryModelEntry : sampleCategoryModel.getCategory()) { + Caracteristic caracteristic = sampleCategoryModelEntry.getCaracteristic(); + if (caracteristic.getCaracteristicType() == CaracteristicType.QUALITATIVE) { + List<CaracteristicQualitativeValue> qualitativeValue = caracteristic.getQualitativeValue(); + for (CaracteristicQualitativeValue value : qualitativeValue) { + sampleCategoryValueMap.put(value.getId(), value); + } + } + } + + } + + /** + * Export species batches. + * + * @param file the file to export the batches into + * @param operation the operation to export + */ + public void exportSpecies(File file, FishingOperation operation) { + + String operationId = operation.getId(); + if (persistenceService.isFishingOperationWithCatchBatch(operationId)) { + + // create rows + + BatchContainer<SpeciesBatch> speciesBatchContainer = + persistenceService.getRootSpeciesBatch(operationId, null); + + List<CatchRow> rows = Lists.newArrayList(); + List<CatchFrequencyRow> frequencyRows = Lists.newArrayList(); + List<AttachmentRow> attachmentRows = Lists.newArrayList(); + + 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); + } + } + + /** + * Export benthos batches. + * + * @param file the file to export the batches into + * @param operation the operation to export + */ + public void exportBenthos(File file, FishingOperation operation) { + + String operationId = operation.getId(); + if (persistenceService.isFishingOperationWithCatchBatch(operationId)) { + + BatchContainer<BenthosBatch> benthosBatchContainer = + persistenceService.getRootBenthosBatch(operationId, null); + + // create rows + + List<CatchRow> rows = Lists.newArrayList(); + List<CatchFrequencyRow> frequencyRows = Lists.newArrayList(); + List<AttachmentRow> attachmentRows = Lists.newArrayList(); + + 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); + } + } + + /** + * Export marine litter batches. + * + * @param file the file to export the batches into + * @param operation the operation to export + */ + public void exportMarineLitter(File file, FishingOperation operation) { + + String operationId = operation.getId(); + if (persistenceService.isFishingOperationWithCatchBatch(operationId)) { + + // create rows + + BatchContainer<MarineLitterBatch> marineLitterBatchContainer = + persistenceService.getRootMarineLitterBatch(operationId); + + List<MarineLitterRow> rows = Lists.newArrayList(); + List<AttachmentRow> attachmentRows = Lists.newArrayList(); + + for (MarineLitterBatch batch : marineLitterBatchContainer.getChildren()) { + MarineLitterRow row = new MarineLitterRow(); + + String id = context.generateId(MarineLitterRow.class); + row.setBatchId(id); + + 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(), + AttachementObjectTypeEnum.BATCH, + attachmentRows); + } + + + File directory = Files.createTempDir(); + List<File> file2zip = Lists.newArrayList(); + + // export marine litters + + MarineLitterRowModel marineLitterRowModel = new MarineLitterRowModel(csvSeparator); + File marineLitterFile = new File(directory, MARINE_LITTER_FILE); + file2zip.add(marineLitterFile); + export(marineLitterFile, + marineLitterRowModel, + rows, + n_("tutti.service.multipost.export.batches.error")); + + // export weights + + CatchBatch catchBatch = persistenceService.getCatchBatchFromFishingOperation(operationId); + MarineLitterWeightRow weight = new MarineLitterWeightRow(); + weight.setTotalWeight(catchBatch.getMarineLitterTotalWeight()); + exportOperation(weight, operation); + + MarineLitterWeightRowModel weightModel = new MarineLitterWeightRowModel(csvSeparator); + + File weightFile = new File(directory, WEIGHTS_FILE); + file2zip.add(weightFile); + export(weightFile, + weightModel, + Lists.newArrayList(weight), + n_("tutti.service.multipost.export.weight.error")); + + // export attachments + create final zip + + exportAttachmentsAndCreateZip(file, + directory, + file2zip, + attachmentRows); + } + } + + /** + * Export individual observation batches. + * + * @param file the file to export the batches into + * @param operation the operation to export + */ + public void exportIndividualObservation(File file, FishingOperation operation) { + List<IndividualObservationBatch> individualObservations = + persistenceService.getAllIndividualObservationBatch(operation.getId()); + + // 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(), AttachementObjectTypeEnum.SAMPLE, attachmentRows); + } + + File directory = Files.createTempDir(); + List<File> file2zip = Lists.newArrayList(); + + // export individual observations + + IndividualObservationRowModel csvModel = new IndividualObservationRowModel(csvSeparator); + File individualObservationFile = new File(directory, INDIVIDUAL_OBSERVATION_FILE); + file2zip.add(individualObservationFile); + export(individualObservationFile, + csvModel, + rows, + n_("tutti.service.multipost.export.batches.error")); + + // export caracteristics + + CaracteristicRowModel caracteristicCsvModel = new CaracteristicRowModel(csvSeparator); + 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(csvSeparator); + + 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); + } + + /** + * Export accidental catch batches. + * + * @param file the file to export the batches into + * @param operation the operation to export + */ + public void exportAccidentalCatch(File file, FishingOperation operation) { + List<AccidentalBatch> accidentalCatches = + persistenceService.getAllAccidentalBatch(operation.getId()); + + 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(), AttachementObjectTypeEnum.SAMPLE, attachmentRows); + } + + // export accidental catches + + AccidentalCatchRowModel csvModel = new AccidentalCatchRowModel(csvSeparator); + + File directory = Files.createTempDir(); + List<File> file2zip = Lists.newArrayList(); + + File accidentalCatchesFile = new File(directory, ACCIDENTAL_CATCHES_FILE); + file2zip.add(accidentalCatchesFile); + export(accidentalCatchesFile, + csvModel, + rows, + n_("tutti.service.multipost.export.batches.error")); + + // export caracteristics + + CaracteristicRowModel caracteristicCsvModel = new CaracteristicRowModel(csvSeparator); + 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(csvSeparator); + + 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 exportCatches(File file, + String batchFile, + CatchWeightsRow weights, + List<CatchRow> rows, + List<CatchFrequencyRow> frequencyRows, + List<AttachmentRow> attachmentRows) { + CatchRowModel csvModel = new CatchRowModel(csvSeparator); + CatchFrequencyRowModel csvFrequencyModel = new CatchFrequencyRowModel(csvSeparator); + CatchWeightsRowModel catchWeightsModel = new CatchWeightsRowModel(csvSeparator); + + 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); + } + + protected void createSpeciesRow(SpeciesBatch batch, + String parentId, + List<CatchRow> rows, + List<CatchFrequencyRow> frequencyRows, + List<AttachmentRow> attachmentRows) { + CatchRow row = new CatchRow(); + + 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); + + addFrequencies(id, batch.getId(), frequencyRows); + addAttachments(id, batch.getIdAsInt(), AttachementObjectTypeEnum.BATCH, attachmentRows); + + for (SpeciesBatch child : batch.getChildBatchs()) { + createSpeciesRow(child, id, rows, frequencyRows, attachmentRows); + } + } + + protected void createBenthosRow(BenthosBatch batch, + String parentId, + List<CatchRow> rows, + List<CatchFrequencyRow> frequencyRows, + List<AttachmentRow> attachmentRows) { + CatchRow row = new CatchRow(); + + 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); + + addFrequencies(id, batch.getId(), frequencyRows); + addAttachments(id, batch.getIdAsInt(), AttachementObjectTypeEnum.BATCH, attachmentRows); + + for (BenthosBatch child : batch.getChildBatchs()) { + createBenthosRow(child, id, rows, frequencyRows, attachmentRows); + } + } + + protected void exportOperation(AbstractFishingOperationRow afoRow, FishingOperation operation) { + afoRow.setStationNumber(operation.getStationNumber()); + afoRow.setOperationNumber(operation.getFishingOperationNumber()); + afoRow.setMultirigAggregation(operation.getMultirigAggregation()); + afoRow.setDate(operation.getGearShootingStartDate()); + } + + protected void addFrequencies(String rowId, + String 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, + AttachementObjectTypeEnum 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, + String errorMessage) { + Writer writer = TuttiIOUtil.newWriter( + file, + n_("tutti.service.multipost.export.file.writer.error")); + try { + Export export = Export.newExport(exportModel, rows); + export.write(writer); + writer.close(); + + } catch (Exception e) { + throw new TuttiTechnicalException(_(errorMessage, file), e); + } finally { + IOUtils.closeQuietly(writer); + } + } + + protected void exportAttachments(File directory, + List<File> file2zip, + List<AttachmentRow> attachmentRows) { + + AttachmentRowModel csvAttachmentModel = new AttachmentRowModel(csvSeparator); + + File attachmentDirectory = new File(directory, ATTACHMENTS_DIRECTORY); + TuttiIOUtil.forceMkdir(attachmentDirectory, _("tutti.service.multipost.attachment.mkdirDir.error", attachmentDirectory)); + file2zip.add(attachmentDirectory); + for (AttachmentRow attachmentRow : attachmentRows) { + File attachmentFile = attachmentRow.getFile(); + File destFile = new File(attachmentDirectory, attachmentFile.getName()); + TuttiIOUtil.copyFile(attachmentFile, + destFile, + _("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 { + TuttiIOUtil.zip(directory, + file, + file2zip, + n_("tutti.service.multipost.export.error")); + } finally { + TuttiIOUtil.deleteDirectory(directory, _("tutti.service.multipost.export.deleteTempDirectory.error", file)); + } + } +} Modified: trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/multipost/TuttiMultiPostImportExportService.java =================================================================== --- trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/multipost/TuttiMultiPostImportExportService.java 2013-07-27 21:21:41 UTC (rev 1160) +++ trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/multipost/TuttiMultiPostImportExportService.java 2013-07-30 17:22:49 UTC (rev 1161) @@ -84,7 +84,9 @@ * * @author kmorin <kmorin@codelutin.com> * @since 2.2 + * @deprecated since 2.5, too long, too bad :( */ +@Deprecated public class TuttiMultiPostImportExportService extends AbstractTuttiService { // private static final Log log = Copied: trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/multipost/TuttiMultiPostImportService.java (from rev 1156, trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/multipost/TuttiMultiPostImportExportService.java) =================================================================== --- trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/multipost/TuttiMultiPostImportService.java (rev 0) +++ trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/catches/multipost/TuttiMultiPostImportService.java 2013-07-30 17:22:49 UTC (rev 1161) @@ -0,0 +1,1269 @@ +package fr.ifremer.tutti.service.catches.multipost; + +/* + * #%L + * Tutti :: Service + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2012 - 2013 Ifremer + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/gpl-3.0.html>. + * #L% + */ + +import com.google.common.base.Function; +import com.google.common.base.Preconditions; +import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.ListMultimap; +import com.google.common.collect.Maps; +import fr.ifremer.tutti.TuttiBusinessException; +import fr.ifremer.tutti.TuttiIOUtil; +import fr.ifremer.tutti.persistence.entities.CaracteristicMap; +import fr.ifremer.tutti.persistence.entities.TuttiBeanFactory; +import fr.ifremer.tutti.persistence.entities.TuttiEntities; +import fr.ifremer.tutti.persistence.entities.TuttiEntity; +import fr.ifremer.tutti.persistence.entities.data.AccidentalBatch; +import fr.ifremer.tutti.persistence.entities.data.AttachementObjectTypeEnum; +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; +import fr.ifremer.tutti.persistence.entities.data.MarineLitterBatch; +import fr.ifremer.tutti.persistence.entities.data.SampleCategoryModel; +import fr.ifremer.tutti.persistence.entities.data.SampleCategoryModelEntry; +import fr.ifremer.tutti.persistence.entities.data.SpeciesBatch; +import fr.ifremer.tutti.persistence.entities.data.SpeciesBatchFrequency; +import fr.ifremer.tutti.persistence.entities.referential.Caracteristic; +import fr.ifremer.tutti.persistence.entities.referential.CaracteristicQualitativeValue; +import fr.ifremer.tutti.persistence.entities.referential.CaracteristicType; +import fr.ifremer.tutti.persistence.entities.referential.Species; +import fr.ifremer.tutti.service.AbstractTuttiService; +import fr.ifremer.tutti.service.DecoratorService; +import fr.ifremer.tutti.service.PersistenceService; +import fr.ifremer.tutti.service.TuttiDataContext; +import fr.ifremer.tutti.service.TuttiServiceContext; +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.ObjectUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.tuple.Pair; +import org.nuiton.csv.Import; + +import java.io.File; +import java.io.Reader; +import java.io.Serializable; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import static org.nuiton.i18n.I18n._; +import static org.nuiton.i18n.I18n.n_; + +/** + * Service to export batches from a satellite post or import batches into a master post. + * + * @author kmorin <kmorin@codelutin.com> + * @since 2.2 + */ +public class TuttiMultiPostImportService extends AbstractTuttiService { + +// private static final Log log = +// LogFactory.getLog(TuttiMultiPostImportExportService.class); + + public static final String BATCHES_KEY = "batchesKey"; + + protected static final String ATTACHMENTS_DIRECTORY = "attachments"; + + protected static final String ATTACHMENTS_FILE = "attachments.csv"; + + protected static final String SPECIES_FILE = "species.csv"; + + protected static final String BENTHOS_FILE = "benthos.csv"; + + protected static final String MARINE_LITTER_FILE = "marineLitter.csv"; + + protected static final String INDIVIDUAL_OBSERVATION_FILE = "individualObservation.csv"; + + protected static final String ACCIDENTAL_CATCHES_FILE = "accidentalCatches.csv"; + + protected static final String FREQUENCIES_FILE = "frequencies.csv"; + + protected static final String CARACTERISTIC_FILE = "caracteristics.csv"; + + protected static final String WEIGHTS_FILE = "weights.csv"; + + protected PersistenceService persistenceService; + + protected DecoratorService decoratorService; + + protected char csvSeparator; + + protected SampleCategoryModel sampleCategoryModel; + + protected Map<String, CaracteristicQualitativeValue> sampleCategoryValueMap; + + @Override + public void setServiceContext(TuttiServiceContext context) { + super.setServiceContext(context); + persistenceService = getService(PersistenceService.class); + decoratorService = getService(DecoratorService.class); + + csvSeparator = context.getConfig().getCsvSeparator(); + + sampleCategoryModel = context.getSampleCategoryModel(); + + sampleCategoryValueMap = Maps.newTreeMap(); + + for (SampleCategoryModelEntry sampleCategoryModelEntry : sampleCategoryModel.getCategory()) { + Caracteristic caracteristic = sampleCategoryModelEntry.getCaracteristic(); + if (caracteristic.getCaracteristicType() == CaracteristicType.QUALITATIVE) { + List<CaracteristicQualitativeValue> qualitativeValue = caracteristic.getQualitativeValue(); + for (CaracteristicQualitativeValue value : qualitativeValue) { + sampleCategoryValueMap.put(value.getId(), value); + } + } + } + } + + //------------------------------------------------------------------------// + //-- Import Species --// + //------------------------------------------------------------------------// + + /** + * Import species batches from a satellite post + * + * @param file the file to import the batches from + * @param operation the operation in which to add the batches + * @return the list of the species which have not been imported, because there were concurrent batches + */ + public Map<String, Object> importSpecies(File file, FishingOperation operation) { + + File tempDir = TuttiIOUtil.explodeZip( + context.getConfig().getTmpDirectory(), + file, + n_("tutti.service.multipost.uncompress.error")); + + //check operation + CatchWeightsRowModel weightsModel = new CatchWeightsRowModel(csvSeparator); + checkSameOperation(tempDir, weightsModel, operation); + + Map<String, Object> notImportedData = Maps.newHashMap(); + final Map<String, SpeciesBatch> notImportedBatches = Maps.newLinkedHashMap(); + + BatchContainer<SpeciesBatch> speciesBatches = + persistenceService.getRootSpeciesBatch(operation.getId(), null); + + TuttiDataContext dataContext = context.getDataContext(); + List<Species> speciesList = dataContext.getReferentSpecies(); + + // Import batches + // map containing the batches by their persistence id + + final Map<String, SpeciesBatch> batches = importSpeciesBatches( + tempDir, + operation, + speciesBatches, + notImportedBatches, + speciesList); + + List<Caracteristic> caracteristics = dataContext.getCaracteristics(); + + // Import frequencies + + ListMultimap<String, SpeciesBatchFrequency> frequencyMap = importSpeciesFrequencies( + file, + batches, + notImportedBatches, + caracteristics); + + // Persist frequencies + + for (String batchId : frequencyMap.keySet()) { + List<SpeciesBatchFrequency> frequencies = frequencyMap.get(batchId); + persistenceService.saveSpeciesBatchFrequency(batchId, frequencies); + } + + // Import attachments + + importAttachments(tempDir, + batches, + notImportedBatches, + AttachementObjectTypeEnum.BATCH); + + // Import weights + + importSpeciesWeights(tempDir, + operation, + weightsModel, + notImportedData); + + notImportedData.put(BATCHES_KEY, notImportedBatches.values()); + return notImportedData; + } + + protected Map<String, SpeciesBatch> importSpeciesBatches(File file, + FishingOperation operation, + BatchContainer<SpeciesBatch> speciesBatches, + Map<String, SpeciesBatch> notImportedBatches, + List<Species> speciesList) { + final Map<String, SpeciesBatch> batches = Maps.newHashMap(); + + Reader reader = TuttiIOUtil.newReader( + new File(file, SPECIES_FILE), + n_("tutti.service.multipost.import.batches.error")); + + try { + CatchRowModel csvModel = new CatchRowModel(csvSeparator, + speciesList); + + Import<CatchRow> importer = Import.newImport(csvModel, reader); + + try { + for (CatchRow row : importer) { + + // create batch + SpeciesBatch batch = TuttiBeanFactory.newSpeciesBatch(); + batch.setFishingOperation(operation); + + Species species = row.getSpecies(); + batch.setSpecies(row.getSpecies()); + batch.setWeight(row.getWeight()); + batch.setNumber(row.getNumber()); + batch.setComment(row.getComment()); + batch.setSpeciesToConfirm(row.isToConfirm()); + + Pair<Integer, Serializable> valueAndCategoryType = getValueAndCategoryType(row); + Integer categoryId = valueAndCategoryType.getKey(); + Serializable value = valueAndCategoryType.getValue(); + + batch.setSampleCategoryId(categoryId); + batch.setSampleCategoryValue(value); + batch.setSampleCategoryWeight(row.getCategoryWeight()); + + // get parent batch + String parentId = row.getParentId(); + String parentPersistedId = null; + SpeciesBatch parent = null; + if (StringUtils.isNotEmpty(parentId)) { + parent = batches.get(parentId); + parentPersistedId = parent.getId(); + } + + SpeciesBatch existingBatch = null; + List<SpeciesBatch> batchesToBrowse; + if (parent == null) { + batchesToBrowse = speciesBatches.getChildren(); + } else { + batchesToBrowse = parent.getChildBatchs(); + } + + // check if the parent has already a batch with the caracteristic value + // and if the children batch are categorized with the same caracterstic + if (batchesToBrowse != null) { + for (SpeciesBatch sb : batchesToBrowse) { + if (sb.getSpecies().equals(species) + && (!sb.getSampleCategoryId().equals(batch.getSampleCategoryId()) + || sb.getSampleCategoryValue().equals(value))) { + + existingBatch = sb; + batches.put(row.getId(), sb); + notImportedBatches.put(row.getId(), batch); + break; + } + } + } + + if (existingBatch == null) { + batch = persistenceService.createSpeciesBatch(batch, parentPersistedId); + batches.put(row.getId(), batch); + } + } + TuttiIOUtil.close(importer, "Could not close importer"); + } finally { + IOUtils.closeQuietly(importer); + } + TuttiIOUtil.close(reader, "Could not close reader"); + } finally { + IOUtils.closeQuietly(reader); + } + return batches; + } + + protected ListMultimap<String, SpeciesBatchFrequency> importSpeciesFrequencies( + File file, + Map<String, SpeciesBatch> batches, + Map<String, SpeciesBatch> notImportedBatches, + List<Caracteristic> caracteristics) { + ListMultimap<String, SpeciesBatchFrequency> frequencyMap = + ArrayListMultimap.create(); + + Reader reader = TuttiIOUtil.newReader( + new File(file, FREQUENCIES_FILE), + n_("tutti.service.multipost.import.frequencies.error")); + + try { + + CatchFrequencyRowModel frequencyModel = new CatchFrequencyRowModel(csvSeparator, + caracteristics); + + Import<CatchFrequencyRow> importer = Import.newImport(frequencyModel, reader); + + + try { + for (CatchFrequencyRow frequencyRow : importer) { + if (notImportedBatches.get(frequencyRow.getBatchId()) == null) { + SpeciesBatch batch = batches.get(frequencyRow.getBatchId()); + if (batch != null) { + SpeciesBatchFrequency frequency = TuttiBeanFactory.newSpeciesBatchFrequency(); + frequency.setLengthStepCaracteristic(frequencyRow.getLengthStepCaracteristic()); + frequency.setLengthStep(frequencyRow.getLengthStep()); + frequency.setNumber(frequencyRow.getNumber()); + frequency.setWeight(frequencyRow.getWeight()); + frequency.setBatch(batch); + frequencyMap.put(batch.getId(), frequency); + } + } + } + TuttiIOUtil.close(importer, "Could not close importer"); + } finally { + IOUtils.closeQuietly(importer); + } + TuttiIOUtil.close(reader, "Could not close reader"); + } finally { + IOUtils.closeQuietly(reader); + } + return frequencyMap; + } + + protected void importSpeciesWeights(File file, + FishingOperation operation, + CatchWeightsRowModel weightsModel, + Map<String, Object> notImportedData) { + Reader reader = TuttiIOUtil.newReader( + new File(file, WEIGHTS_FILE), + n_("tutti.service.multipost.import.weights.error")); + + try { + Import<CatchWeightsRow> importer = Import.newImport(weightsModel, reader); + + try { + Iterator<CatchWeightsRow> iterator = importer.iterator(); + if (iterator.hasNext()) { + CatchWeightsRow row = iterator.next(); + CatchBatch catchBatch = persistenceService.getCatchBatchFromFishingOperation(operation.getId()); + catchBatch.setFishingOperation(operation); + + if (catchBatch.getSpeciesTotalSortedWeight() == null) { + catchBatch.setSpeciesTotalSortedWeight(row.getTotalSortedWeight()); + + } else if (row.getTotalSortedWeight() != null) { + notImportedData.put(CatchBatch.PROPERTY_SPECIES_TOTAL_SORTED_WEIGHT, row.getTotalSortedWeight()); + } + + if (catchBatch.getSpeciesTotalInertWeight() == null) { + catchBatch.setSpeciesTotalInertWeight(row.getInertWeight()); + + } else if (row.getInertWeight() != null) { + notImportedData.put(CatchBatch.PROPERTY_SPECIES_TOTAL_INERT_WEIGHT, row.getInertWeight()); + } + + if (catchBatch.getSpeciesTotalLivingNotItemizedWeight() == null) { + catchBatch.setSpeciesTotalLivingNotItemizedWeight(row.getLivingNotItemizedWeight()); + + } else if (row.getLivingNotItemizedWeight() != null) { + notImportedData.put(CatchBatch.PROPERTY_SPECIES_TOTAL_LIVING_NOT_ITEMIZED_WEIGHT, + row.getLivingNotItemizedWeight()); + } + persistenceService.saveCatchBatch(catchBatch); + } + TuttiIOUtil.close(importer, "Could not close importer"); + } finally { + IOUtils.closeQuietly(importer); + } + TuttiIOUtil.close(reader, "Could not close reader"); + } finally { + IOUtils.closeQuietly(reader); + } + } + + //------------------------------------------------------------------------// + //-- Import Benthos --// + //------------------------------------------------------------------------// + + /** + * Import benthos batches from a satellite post + * + * @param file the file to import the batches from + * @param operation the operation in which to add the batches + * @return the list of the benthos which have not been imported, because there were concurrent batches + */ + public Map<String, Object> importBenthos(File file, FishingOperation operation) { + + File tempDir = TuttiIOUtil.explodeZip( + context.getConfig().getTmpDirectory(), + file, + n_("tutti.service.multipost.uncompress.error")); + + //check operation + CatchWeightsRowModel weightsModel = new CatchWeightsRowModel(csvSeparator); + checkSameOperation(tempDir, weightsModel, operation); + + Map<String, Object> notImportedData = Maps.newHashMap(); + final Map<String, BenthosBatch> notImportedBatches = Maps.newLinkedHashMap(); + + BatchContainer<BenthosBatch> benthosBatches = + persistenceService.getRootBenthosBatch(operation.getId(), null); + + TuttiDataContext dataContext = context.getDataContext(); + List<Species> speciesList = dataContext.getReferentSpecies(); + + // Import batches + // map containing the batches by their persistence id + final Map<String, BenthosBatch> batches = importBenthosBatches( + tempDir, + operation, + benthosBatches, + notImportedBatches, + speciesList); + + // Import frequencies + + ListMultimap<String, BenthosBatchFrequency> frequencyMap = importBenthosFrequencies( + tempDir, + batches, + notImportedBatches, + dataContext.getCaracteristics()); + + // Persist frequencies + + for (String batchId : frequencyMap.keySet()) { + List<BenthosBatchFrequency> frequencies = frequencyMap.get(batchId); + persistenceService.saveBenthosBatchFrequency(batchId, frequencies); + } + + // Import attachments + + importAttachments(tempDir, batches, notImportedBatches, AttachementObjectTypeEnum.BATCH); + + // Import weights + + importBenthosWeights(tempDir, operation, weightsModel, notImportedData); + + notImportedData.put(BATCHES_KEY, notImportedBatches.values()); + return notImportedData; + } + + protected Map<String, BenthosBatch> importBenthosBatches(File file, + FishingOperation operation, + BatchContainer<BenthosBatch> benthosBatches, + Map<String, BenthosBatch> notImportedBatches, + List<Species> speciesList) { + final Map<String, BenthosBatch> batches = Maps.newHashMap(); + Reader reader = TuttiIOUtil.newReader( + new File(file, BENTHOS_FILE), + n_("tutti.service.multipost.import.batches.error")); + + try { + CatchRowModel csvModel = new CatchRowModel(csvSeparator, + speciesList); + + Import<CatchRow> importer = Import.newImport(csvModel, reader); + + try { + for (CatchRow row : importer) { + + // create batch + BenthosBatch batch = TuttiBeanFactory.newBenthosBatch(); + batch.setFishingOperation(operation); + + Species species = row.getSpecies(); + batch.setSpecies(row.getSpecies()); + batch.setWeight(row.getWeight()); + batch.setNumber(row.getNumber()); + batch.setComment(row.getComment()); + batch.setSpeciesToConfirm(row.isToConfirm()); + + Pair<Integer, Serializable> valueAndCategoryType = getValueAndCategoryType(row); + Integer categoryId = valueAndCategoryType.getKey(); + Serializable value = valueAndCategoryType.getValue(); + + batch.setSampleCategoryId(categoryId); + batch.setSampleCategoryValue(value); + batch.setSampleCategoryWeight(row.getCategoryWeight()); + + // get parent batch + String parentId = row.getParentId(); + String parentPersistedId = null; + BenthosBatch parent = null; + if (StringUtils.isNotEmpty(parentId)) { + parent = batches.get(parentId); + parentPersistedId = parent.getId(); + } + + BenthosBatch existingBatch = null; + List<BenthosBatch> batchesToBrowse; + if (parent == null) { + batchesToBrowse = benthosBatches.getChildren(); + } else { + batchesToBrowse = parent.getChildBatchs(); + } + + // check if the parent has already a batch with the caracteristic value + // and if the children batch are categorized with the same caracterstic + if (batchesToBrowse != null) { + for (BenthosBatch bb : batchesToBrowse) { + if (bb.getSpecies().equals(species) + && (!bb.getSampleCategoryId().equals(batch.getSampleCategoryId()) + || bb.getSampleCategoryValue().equals(value))) { + + existingBatch = bb; + batches.put(row.getId(), bb); + notImportedBatches.put(row.getId(), batch); + break; + } + } + } + + if (existingBatch == null) { + batch = persistenceService.createBenthosBatch(batch, parentPersistedId); + batches.put(row.getId(), batch); + } + } + + TuttiIOUtil.close(importer, "Could not close importer"); + } finally { + IOUtils.closeQuietly(importer); + } + TuttiIOUtil.close(reader, "Could not close reader"); + } finally { + IOUtils.closeQuietly(reader); + } + return batches; + } + + protected ListMultimap<String, BenthosBatchFrequency> importBenthosFrequencies(File file, + Map<String, BenthosBatch> batches, + Map<String, BenthosBatch> notImportedBatches, + List<Caracteristic> caracteristics) { + ListMultimap<String, BenthosBatchFrequency> frequencyMap = + ArrayListMultimap.create(); + + Reader reader = TuttiIOUtil.newReader( + new File(file, FREQUENCIES_FILE), + n_("tutti.service.multipost.import.frequencies.error")); + try { + CatchFrequencyRowModel frequencyModel = new CatchFrequencyRowModel(csvSeparator, + caracteristics); + + Import<CatchFrequencyRow> importer = Import.newImport(frequencyModel, reader); + + try { + + for (CatchFrequencyRow frequencyRow : importer) { + if (notImportedBatches.get(frequencyRow.getBatchId()) == null) { + BenthosBatch batch = batches.get(frequencyRow.getBatchId()); + if (batch != null) { + BenthosBatchFrequency frequency = TuttiBeanFactory.newBenthosBatchFrequency(); + frequency.setLengthStepCaracteristic(frequencyRow.getLengthStepCaracteristic()); + frequency.setLengthStep(frequencyRow.getLengthStep()); + frequency.setNumber(frequencyRow.getNumber()); + frequency.setWeight(frequencyRow.getWeight()); + frequency.setBatch(batch); + frequencyMap.put(batch.getId(), frequency); + } + } + } + TuttiIOUtil.close(importer, "Could not close importer"); + } finally { + IOUtils.closeQuietly(importer); + } + TuttiIOUtil.close(reader, "Could not close reader"); + } finally { + IOUtils.closeQuietly(reader); + } + return frequencyMap; + } + + protected void importBenthosWeights(File file, + FishingOperation operation, + CatchWeightsRowModel weightsModel, + Map<String, Object> notImportedData) { + Reader reader = TuttiIOUtil.newReader( + new File(file, WEIGHTS_FILE), + n_("tutti.service.multipost.import.weights.error")); + + try { + Import<CatchWeightsRow> importer = Import.newImport(weightsModel, reader); + + try { + Iterator<CatchWeightsRow> iterator = importer.iterator(); + if (iterator.hasNext()) { + CatchWeightsRow row = iterator.next(); + CatchBatch catchBatch = persistenceService.getCatchBatchFromFishingOperation(operation.getId()); + catchBatch.setFishingOperation(operation); + + if (catchBatch.getBenthosTotalSortedWeight() == null) { + catchBatch.setBenthosTotalSortedWeight(row.getTotalSortedWeight()); + + } else if (row.getTotalSortedWeight() != null) { + notImportedData.put(CatchBatch.PROPERTY_BENTHOS_TOTAL_SORTED_WEIGHT, row.getTotalSortedWeight()); + } + + if (catchBatch.getBenthosTotalInertWeight() == null) { + catchBatch.setBenthosTotalInertWeight(row.getInertWeight()); + + } else if (row.getInertWeight() != null) { + notImportedData.put(CatchBatch.PROPERTY_BENTHOS_TOTAL_INERT_WEIGHT, row.getInertWeight()); + } + + if (catchBatch.getBenthosTotalLivingNotItemizedWeight() == null) { + catchBatch.setBenthosTotalLivingNotItemizedWeight(row.getLivingNotItemizedWeight()); + + } else if (row.getLivingNotItemizedWeight() != null) { + notImportedData.put(CatchBatch.PROPERTY_BENTHOS_TOTAL_LIVING_NOT_ITEMIZED_WEIGHT, + row.getLivingNotItemizedWeight()); + } + + persistenceService.saveCatchBatch(catchBatch); + } + TuttiIOUtil.close(importer, "Could not close importer"); + } finally { + IOUtils.closeQuietly(importer); + } + TuttiIOUtil.close(reader, "Could not close reader"); + } finally { + IOUtils.closeQuietly(reader); + } + } + + //------------------------------------------------------------------------// + //-- Import Marine litter --// + //------------------------------------------------------------------------// + + /** + * Import marine litter batches from a satellite post + * + * @param file the file to import the batches from + * @param operation the operation in which to add the batches + * @return the list of the marine litter which have not been imported, because there were concurrent batches + */ + public Map<String, Object> importMarineLitter(File file, FishingOperation operation) { + + File tempDir = TuttiIOUtil.explodeZip( + context.getConfig().getTmpDirectory(), + file, + n_("tutti.service.multipost.uncompress.error")); + + //check operation + MarineLitterWeightRowModel weightModel = new MarineLitterWeightRowModel(csvSeparator); + checkSameOperation(tempDir, weightModel, operation); + + String operationId = operation.getId(); + + Map<String, Object> notImportedData = Maps.newHashMap(); + final Map<String, MarineLitterBatch> notImportedBatches = Maps.newLinkedHashMap(); + + BatchContainer<MarineLitterBatch> marineLitterBatches = + persistenceService.getRootMarineLitterBatch(operationId); + List<CaracteristicQualitativeValue> categoryValues = + persistenceService.getMarineLitterCategoryCaracteristic().getQualitativeValue(); + List<CaracteristicQualitativeValue> sizeCategoryValues = + persistenceService.getMarineLitterSizeCategoryCaracteristic().getQualitativeValue(); + + // Import batches + // map containing the batches by their persistence id + final Map<String, MarineLitterBatch> batches = importMarineLitterBatches( + tempDir, + operation, + categoryValues, + sizeCategoryValues, + marineLitterBatches, + notImportedBatches); + + // Import attachments + + importAttachments(tempDir, + batches, + notImportedBatches, + AttachementObjectTypeEnum.BATCH); + + // Import weights + + importMarineLitterCatchWeights(tempDir, + operation, + weightModel, + notImportedData); + + notImportedData.put(BATCHES_KEY, notImportedBatches.values()); + return notImportedData; + } + + protected Map<String, MarineLitterBatch> importMarineLitterBatches(File file, FishingOperation operation, + List<CaracteristicQualitativeValue> categoryValues, + List<CaracteristicQualitativeValue> sizeCategoryValues, + BatchContainer<MarineLitterBatch> marineLitterBatches, + Map<String, MarineLitterBatch> notImportedBatches) { + + Map<String, MarineLitterBatch> batches = Maps.newLinkedHashMap(); + + Reader reader = TuttiIOUtil.newReader( + new File(file, MARINE_LITTER_FILE), + n_("tutti.service.multipost.import.batches.error")); + + try { + + MarineLitterRowModel csvModel = new MarineLitterRowModel(csvSeparator, + categoryValues, + sizeCategoryValues); + + Import<MarineLitterRow> importer = Import.newImport(csvModel, reader); + + try { + for (MarineLitterRow row : importer) { + + // create batch + MarineLitterBatch batch = TuttiBeanFactory.newMarineLitterBatch(); + batch.setFishingOperation(operation); + + batch.setWeight(row.getWeight()); + batch.setNumber(row.getNumber()); + batch.setComment(row.getComment()); + batch.setMarineLitterCategory(row.getCategory()); + batch.setMarineLitterSizeCategory(row.getSizeCategory()); + + MarineLitterBatch existingBatch = null; + List<MarineLitterBatch> batchesToBrowse = marineLitterBatches.getChildren(); + + // check if the parent has already a batch with the caracteristic value + // and if the children batch are categorized with the same caracterstic + if (batchesToBrowse != null) { + for (MarineLitterBatch mlb : batchesToBrowse) { + if (mlb.getMarineLitterCategory().equals(batch.getMarineLitterCategory()) + && mlb.getMarineLitterSizeCategory().equals(batch.getMarineLitterSizeCategory())) { + + existingBatch = mlb; + batches.put(row.getBatchId(), mlb); + notImportedBatches.put(row.getBatchId(), batch); + break; + } + } + } + + if (existingBatch == null) { + batch = persistenceService.createMarineLitterBatch(batch); + batches.put(row.getBatchId(), batch); + } + + } + TuttiIOUtil.close(importer, "Could not close importer"); + } finally { + IOUtils.closeQuietly(importer); + } + + TuttiIOUtil.close(reader, "Could not close reader"); + } finally { + IOUtils.closeQuietly(reader); + } + return batches; + } + + protected void importMarineLitterCatchWeights(File file, + FishingOperation operation, + MarineLitterWeightRowModel weightModel, + Map<String, Object> notImportedData) { + + Reader reader = TuttiIOUtil.newReader( + new File(file, WEIGHTS_FILE), + n_("tutti.service.multipost.import.weights.error")); + + Import<MarineLitterWeightRow> weight = Import.newImport(weightModel, reader); + + Iterator<MarineLitterWeightRow> iterator = weight.iterator(); + if (iterator.hasNext()) { + MarineLitterWeightRow row = iterator.next(); + CatchBatch catchBatch = persistenceService.getCatchBatchFromFishingOperation(operation.getId()); + catchBatch.setFishingOperation(operation); + + if (catchBatch.getMarineLitterTotalWeight() == null) { + catchBatch.setMarineLitterTotalWeight(row.getTotalWeight()); + + } else if (row.getTotalWeight() != null) { + notImportedData.put(CatchBatch.PROPERTY_MARINE_LITTER_TOTAL_WEIGHT, row.getTotalWeight()); + } + + persistenceService.saveCatchBatch(catchBatch); + } + } + + //------------------------------------------------------------------------// + //-- Import individual observations --// + //------------------------------------------------------------------------// + + /** + * Import individual observation batches from a satellite post + * + * @param file the file to import the batches from + * @param operation the operation in which to add the batches + */ + public void importIndividualObservation(File file, FishingOperation operation) { + + File tempDir = TuttiIOUtil.explodeZip( + context.getConfig().getTmpDirectory(), + file, + n_("tutti.service.multipost.uncompress.error")); + + //check operation + FishingOperationRowModel operationModel = + new FishingOperationRowModel(csvSeparator); + checkSameOperation(tempDir, operationModel, operation); + + TuttiDataContext dataContext = context.getDataContext(); + List<Species> speciesList = dataContext.getReferentSpecies(); + List<Caracteristic> caracteristics = dataContext.getCaracteristicWithProtected(); + + // Import batches + // map containing the batches by their persistence id + final Map<String, IndividualObservationBatch> batches = importIndividualObservationBatches( + tempDir, + operation, + speciesList, + caracteristics); + + // Import caracteristics + + importIndividualObservationCaracteristics(file, batches, caracteristics); + + // Persist batches + + for (IndividualObservationBatch batch : batches.values()) { + persistenceService.createIndividualObservationBatch(batch); + } + + // Import attachments + + importAttachments(tempDir, batches, AttachementObjectTypeEnum.SAMPLE); + } + + protected Map<String, IndividualObservationBatch> importIndividualObservationBatches(File file, FishingOperation operation, + List<Species> speciesList, + List<Caracteristic> caracteristics) { + + Map<String, IndividualObservationBatch> batches = Maps.newLinkedHashMap(); + + + IndividualObservationRowModel csvModel = new IndividualObservationRowModel(csvSeparator, + speciesList, + caracteristics); + Reader reader = TuttiIOUtil.newReader( + new File(file, INDIVIDUAL_OBSERVATION_FILE), + n_("tutti.service.multipost.import.batches.error")); + + try { + Import<IndividualObservationRow> importer = Import.newImport(csvModel, reader); + + try { + for (IndividualObservationRow row : importer) { + + // create batch + IndividualObservationBatch batch = + TuttiBeanFactory.newIndividualObservationBatch(); + batch.setFishingOperation(operation); + + batch.setSpecies(row.getSpecies()); + batch.setWeight(row.getWeight()); + batch.setSize(row.getSize()); + batch.setLengthStepCaracteristic(row.getLengthStepCaracteristic()); + batch.setSamplingCode(row.getSamplingCode()); + batch.setCalcifiedPieceSamplingCode(row.getCalcifiedPieceSamplingCode()); + batch.setComment(row.getComment()); + batch.setCaracteristics(new CaracteristicMap()); + + batches.put(row.getBatchId(), batch); + } + + TuttiIOUtil.close(importer, "Could not close importer"); + } finally { + IOUtils.closeQuietly(importer); + } + + TuttiIOUtil.close(reader, "Could not close reader"); + } finally { + IOUtils.closeQuietly(reader); + } + return batches; + } + + protected void importIndividualObservationCaracteristics(File file, Map<String, IndividualObservationBatch> batches, + List<Caracteristic> caracteristics) { + + CaracteristicRowModel caracteristicModel = new CaracteristicRowModel(csvSeparator, + caracteristics); + + Reader reader = TuttiIOUtil.newReader( + new File(file, CARACTERISTIC_FILE), + n_("tutti.service.multipost.import.caracteristics.error")); + + try { + Import<CaracteristicRow> importer = Import.newImport(caracteristicModel, reader); + + try { + for (CaracteristicRow caracteristicRow : importer) { + IndividualObservationBatch batch = batches.get(caracteristicRow.getBatchId()); + if (batch != null) { + Caracteristic caracteristic = caracteristicRow.getCaracteristic(); + Serializable value = caracteristicRow.getValue(); + + switch (caracteristic.getCaracteristicType()) { + case QUALITATIVE: + value = TuttiEntities.getQualitativeValue(caracteristic, Integer.parseInt(value.toString())); + break; + + case NUMBER: + value = Float.parseFloat(value.toString()); + break; + } + CaracteristicMap map = batch.getCaracteristics(); + map.put(caracteristic, value); + } + } + + TuttiIOUtil.close(importer, "Could not close importer"); + + } finally { + IOUtils.closeQuietly(importer); + } + + TuttiIOUtil.close(reader, "Could not close reader"); + } finally { + IOUtils.closeQuietly(reader); + } + } + + //------------------------------------------------------------------------// + //-- Import Accidental catches --// + //------------------------------------------------------------------------// + + /** + * Import accidental catches batches from a satellite post + * + * @param file the file to import the batches from + * @param operation the operation in which to add the batches + */ + public void importAccidentalCatches(File file, FishingOperation operation) { + + File tempDir = TuttiIOUtil.explodeZip( + context.getConfig().getTmpDirectory(), + file, + n_("tutti.service.multipost.uncompress.error")); + + //check operation + FishingOperationRowModel operationModel = + new FishingOperationRowModel(csvSeparator); + checkSameOperation(tempDir, operationModel, operation); + + TuttiDataContext dataContext = context.getDataContext(); + List<Species> speciesList = dataContext.getReferentSpecies(); + List<CaracteristicQualitativeValue> genderValues = dataContext.getGenderValues(); + List<Caracteristic> caracteristics = dataContext.getCaracteristicWithProtected(); + List<CaracteristicQualitativeValue> dedOrAliveValues = dataContext.getDeadOrAliveValues(); + + // Import batches + // map containing the batches by their persistence id + final Map<String, AccidentalBatch> batches = importAccidentalCatchesBatches( + tempDir, + operation, + speciesList, + genderValues, + caracteristics, + dedOrAliveValues + ); + + // Import caracteristics + + importAccidentalCatchesCaracteristics(file, batches, caracteristics); + + // Persist batches + + for (AccidentalBatch batch : batches.values()) { + persistenceService.createAccidentalBatch(batch); + } + + // Import attachments + + importAttachments(tempDir, batches, AttachementObjectTypeEnum.SAMPLE); + } + + protected Map<String, AccidentalBatch> importAccidentalCatchesBatches(File file, FishingOperation operation, + List<Species> speciesList, + List<CaracteristicQualitativeValue> genderValues, + List<Caracteristic> caracteristics, + List<CaracteristicQualitativeValue> dedOrAliveValues) { + + Map<String, AccidentalBatch> batches = Maps.newLinkedHashMap(); + + Reader reader = TuttiIOUtil.newReader( + new File(file, INDIVIDUAL_OBSERVATION_FILE), + n_("tutti.service.multipost.import.batches.error")); + + try { + + AccidentalCatchRowModel csvModel = new AccidentalCatchRowModel(csvSeparator, + speciesList, + genderValues, + caracteristics, + dedOrAliveValues); + + Import<AccidentalCatchRow> importer = Import.newImport(csvModel, reader); + + try { + for (AccidentalCatchRow row : importer) { + + // create batch + AccidentalBatch batch = TuttiBeanFactory.newAccidentalBatch(); + batch.setFishingOperation(operation); + + batch.setSpecies(row.getSpecies()); + batch.setGender(row.getGender()); + batch.setWeight(row.getWeight()); + batch.setSize(row.getSize()); + batch.setLengthStepCaracteristic(row.getLengthStepCaracteristic()); + batch.setDeadOrAlive(row.getDeadOrAlive()); + batch.setComment(row.getComment()); + batch.setCaracteristics(new CaracteristicMap()); + + batches.put(row.getBatchId(), batch); + } + + TuttiIOUtil.close(importer, "Could not close importer"); + } finally { + IOUtils.closeQuietly(importer); + } + + TuttiIOUtil.close(reader, "Could not close reader"); + } finally { + IOUtils.closeQuietly(reader); + } + return batches; + } + + protected void importAccidentalCatchesCaracteristics(File file, Map<String, AccidentalBatch> batches, + List<Caracteristic> caracteristics) { + + CaracteristicRowModel caracteristicModel = new CaracteristicRowModel(csvSeparator, + caracteristics); + + Reader reader = TuttiIOUtil.newReader( + new File(file, CARACTERISTIC_FILE), + n_("tutti.service.multipost.import.caracteristics.error")); + + try { + Import<CaracteristicRow> importer = Import.newImport(caracteristicModel, reader); + + try { + for (CaracteristicRow caracteristicRow : importer) { + AccidentalBatch batch = batches.get(caracteristicRow.getBatchId()); + if (batch != null) { + Caracteristic caracteristic = caracteristicRow.getCaracteristic(); + Serializable value = caracteristicRow.getValue(); + + switch (caracteristic.getCaracteristicType()) { + case QUALITATIVE: + value = TuttiEntities.getQualitativeValue(caracteristic, Integer.parseInt(value.toString())); + break; + + case NUMBER: + value = Float.parseFloat(value.toString()); + break; + } + CaracteristicMap map = batch.getCaracteristics(); + map.put(caracteristic, value); + } + } + + TuttiIOUtil.close(importer, "Could not close importer"); + + } finally { + IOUtils.closeQuietly(importer); + } + + TuttiIOUtil.close(reader, "Could not close reader"); + } finally { + IOUtils.closeQuietly(reader); + } + + } + + //------------------------------------------------------------------------// + //-- Internal methods --// + //------------------------------------------------------------------------// + + protected Pair<Integer, Serializable> getValueAndCategoryType(CatchRow row) { + + Serializable value = row.getCategoryValue(); + Integer categoryId = row.getCategoryId(); + + SampleCategoryModelEntry categoryModelEntry = + sampleCategoryModel.getCategoryById(categoryId); + + Caracteristic caracteristic = categoryModelEntry.getCaracteristic(); + if (caracteristic.getCaracteristicType() == CaracteristicType.QUALITATIVE) { + CaracteristicQualitativeValue caracteristicQualitativeValue = sampleCategoryValueMap.get(String.valueOf(value)); + Preconditions.checkNotNull(caracteristicQualitativeValue, "Can't find caracteristic qualitative value with id: " + value + " for caracteristic of id: " + categoryId); + value = caracteristicQualitativeValue; + } + return Pair.of(categoryId, value); + } + + protected void checkSameOperation(File directory, + AbstractFishingOperationRowModel fishingOperationRowModel, + FishingOperation operation) { + Reader reader = TuttiIOUtil.newReader( + new File(directory, WEIGHTS_FILE), + n_("tutti.service.multipost.import.operation.error")); + + try { + Import<AbstractFishingOperationRow> importer = Import.<AbstractFishingOperationRow>newImport(fishingOperationRowModel, reader); + + Iterator<AbstractFishingOperationRow> iterator = importer.iterator(); + try { + if (iterator.hasNext()) { + AbstractFishingOperationRow row = iterator.next(); + if (ObjectUtils.notEqual(row.getStationNumber(), operation.getStationNumber()) + || ObjectUtils.notEqual(row.getOperationNumber(), operation.getFishingOperationNumber()) + || ObjectUtils.notEqual(row.getMultirigAggregation(), operation.getMultirigAggregation()) + || ObjectUtils.notEqual(row.getDate(), operation.getGearShootingStartDate())) { + + throw new TuttiBusinessException(_("tutti.service.multipost.import.wrongOperation.error", + decoratorService.getDecoratorByType(FishingOperation.class) + .toString(operation))); + } + } + } finally { + IOUtils.closeQuietly(importer); + } + } finally { + IOUtils.closeQuietly(reader); + } + } + + protected <R extends TuttiEntity> void importAttachments(File directory, + final Map<String, R> data, + final Map<String, R> notImportedData, + AttachementObjectTypeEnum objectType) { + + Function<AttachmentRow, Integer> getObjetcIdFunction = new Function<AttachmentRow, Integer>() { + @Override + public Integer apply(AttachmentRow input) { + Integer result = null; + if (notImportedData.get(input.getBatchId()) == null) { + R batch = data.get(input.getBatchId()); + if (batch != null) { + result = batch.getIdAsInt(); + } + } + return result; + } + }; + + Reader reader = TuttiIOUtil.newReader( + new File(directory, ATTACHMENTS_FILE), + n_("tutti.service.multipost.import.attachments.error")); + + try { + AttachmentRowModel attachmentModel = + new AttachmentRowModel(csvSeparator, new File(directory, ATTACHMENTS_DIRECTORY)); + Import<AttachmentRow> importer = Import.newImport(attachmentModel, reader); + + try { + for (AttachmentRow row : importer) { + Integer objectId = getObjetcIdFunction.apply(row); + if (objectId != null) { + Attachment attachment = TuttiBeanFactory.newAttachment(); + attachment.setName(row.getName()); + attachment.setComment(row.getComment()); + attachment.setObjectType(objectType); + + attachment.setObjectId(objectId); + + persistenceService.createAttachment(attachment, row.getFile()); + } + } + + TuttiIOUtil.close(importer, "Could not close importer"); + } finally { + IOUtils.closeQuietly(importer); + } + + TuttiIOUtil.close(reader, "Could not close reader"); + } finally { + IOUtils.closeQuietly(reader); + } + } + + protected <R extends TuttiEntity> void importAttachments(File directory, + final Map<String, R> data, + AttachementObjectTypeEnum objectType) { + + Function<AttachmentRow, Integer> getObjetcIdFunction = new Function<AttachmentRow, Integer>() { + @Override + public Integer apply(AttachmentRow input) { + Integer result = null; + R batch = data.get(input.getBatchId()); + if (batch != null) { + result = batch.getIdAsInt(); + } + return result; + } + }; + + Reader reader = TuttiIOUtil.newReader( + new File(directory, ATTACHMENTS_FILE), + n_("tutti.service.multipost.import.attachments.error")); + + try { + AttachmentRowModel attachmentModel = + new AttachmentRowModel(csvSeparator, new File(directory, ATTACHMENTS_DIRECTORY)); + Import<AttachmentRow> importer = Import.newImport(attachmentModel, reader); + + try { + for (AttachmentRow row : importer) { + Integer objectId = getObjetcIdFunction.apply(row); + if (objectId != null) { + Attachment attachment = TuttiBeanFactory.newAttachment(); + attachment.setName(row.getName()); + attachment.setComment(row.getComment()); + attachment.setObjectType(objectType); + + attachment.setObjectId(objectId); + + persistenceService.createAttachment(attachment, row.getFile()); + } + } + + TuttiIOUtil.close(importer, "Could not close importer"); + } finally { + IOUtils.closeQuietly(importer); + } + + TuttiIOUtil.close(reader, "Could not close reader"); + } finally { + IOUtils.closeQuietly(reader); + } + } + +} Modified: trunk/tutti-service/src/main/resources/i18n/tutti-service_en_GB.properties =================================================================== --- trunk/tutti-service/src/main/resources/i18n/tutti-service_en_GB.properties 2013-07-27 21:21:41 UTC (rev 1160) +++ trunk/tutti-service/src/main/resources/i18n/tutti-service_en_GB.properties 2013-07-30 17:22:49 UTC (rev 1161) @@ -81,10 +81,12 @@ tutti.service.exportSumatra.header.weight= tutti.service.mkDir.error= tutti.service.multipost.attachment.copy.error= +tutti.service.multipost.attachment.mkdirDir.error= tutti.service.multipost.export.attachments.error= tutti.service.multipost.export.batches.error= tutti.service.multipost.export.deleteTempDirectory.error= tutti.service.multipost.export.error= +tutti.service.multipost.export.file.writer.error= tutti.service.multipost.export.frequencies.error= tutti.service.multipost.export.operation.error= tutti.service.multipost.export.weight.error= Modified: trunk/tutti-service/src/main/resources/i18n/tutti-service_fr_FR.properties =================================================================== --- trunk/tutti-service/src/main/resources/i18n/tutti-service_fr_FR.properties 2013-07-27 21:21:41 UTC (rev 1160) +++ trunk/tutti-service/src/main/resources/i18n/tutti-service_fr_FR.properties 2013-07-30 17:22:49 UTC (rev 1161) @@ -81,10 +81,13 @@ tutti.service.exportSumatra.header.weight=Total tutti.service.mkDir.error=Erreur à la création du dossier %s tutti.service.multipost.attachment.copy.error=Erreur lors de l'export de la pièce-jointe %s +tutti.service.multipost.attachment.mkdir.error=Impossible de créer le répertoire %s +tutti.service.multipost.attachment.mkdirDir.error= tutti.service.multipost.export.attachments.error=Erreur lors de l'export des pièces-jointes tutti.service.multipost.export.batches.error=Erreur lors de l'export des lots tutti.service.multipost.export.deleteTempDirectory.error=Erreur lors de la suppression du dossier temporaire tutti.service.multipost.export.error=Erreur lors de la création du fichier d'export +tutti.service.multipost.export.file.writer.error=Erreur à la création du fichier %s tutti.service.multipost.export.frequencies.error=Erreur lors de l'export des mensurations tutti.service.multipost.export.operation.error=Erreur lors de l'export des données du trait tutti.service.multipost.export.weight.error=Erreur lors de l'export des poids Modified: trunk/tutti-service/src/test/java/fr/ifremer/tutti/service/ServiceDbResource.java =================================================================== --- trunk/tutti-service/src/test/java/fr/ifremer/tutti/service/ServiceDbResource.java 2013-07-27 21:21:41 UTC (rev 1160) +++ trunk/tutti-service/src/test/java/fr/ifremer/tutti/service/ServiceDbResource.java 2013-07-30 17:22:49 UTC (rev 1161) @@ -25,7 +25,9 @@ */ import com.google.common.collect.Lists; +import com.google.common.collect.Maps; import com.google.common.io.Files; +import fr.ifremer.tutti.TuttiIOUtil; import fr.ifremer.tutti.persistence.RessourceClassLoader; import fr.ifremer.tutti.persistence.config.TuttiPersistenceConfigOption; import fr.ifremer.tutti.persistence.entities.TuttiEntities; @@ -40,6 +42,7 @@ import fr.ifremer.tutti.service.protocol.TuttiProtocolImportExportService; import org.apache.commons.io.Charsets; import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.tuple.Pair; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.junit.Assert; @@ -51,6 +54,7 @@ import java.io.IOException; import java.util.List; import java.util.Locale; +import java.util.Map; /** * TODO @@ -63,6 +67,26 @@ /** Logger. */ private static final Log log = LogFactory.getLog(ServiceDbResource.class); + public void assertZipContent(String message, + File zipFile, + Pair<String, String>... entryAndExpectedContent) throws IOException { + Assert.assertTrue(zipFile.exists()); + + File explodeDir = TuttiIOUtil.explodeZip( + getServiceConfig().getTmpDirectory(), + zipFile, + "could not explode archive zip"); + for (Pair<String, String> stringStringPair : entryAndExpectedContent) { + String entry = stringStringPair.getKey(); + File explodeFile = new File(explodeDir, entry); + Assert.assertTrue(zipFile.exists()); + + String expectedContent = stringStringPair.getValue(); + + assertFileContent(message + " - " + entry + "\n", explodeFile, expectedContent); + } + } + public static void assertFileContent(String message, File actualFile, String expectedContent) throws IOException { @@ -78,7 +102,21 @@ protected TuttiServiceContext createServiceContext(RessourceClassLoader loader, TuttiServiceConfig config) { - return new TuttiServiceContext(loader, config); + return new TuttiServiceContext(loader, config) { + + Map<Class<?>, Integer> counts = Maps.newHashMap(); + + @Override + public String generateId(Class<?> type) { + Integer count = counts.get(type); + if (count == null) { + count = 0; + } + count++; + counts.put(type, count); + return type.getSimpleName() + "_" + count; + } + }; } @Override Added: trunk/tutti-service/src/test/java/fr/ifremer/tutti/service/catches/multipost/TuttiMultiPostExportServiceTest.java =================================================================== --- trunk/tutti-service/src/test/java/fr/ifremer/tutti/service/catches/multipost/TuttiMultiPostExportServiceTest.java (rev 0) +++ trunk/tutti-service/src/test/java/fr/ifremer/tutti/service/catches/multipost/TuttiMultiPostExportServiceTest.java 2013-07-30 17:22:49 UTC (rev 1161) @@ -0,0 +1,259 @@ +package fr.ifremer.tutti.service.catches.multipost; + +/* + * #%L + * Tutti :: Service + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2012 - 2013 Ifremer + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/gpl-3.0.html>. + * #L% + */ + +import com.google.common.io.Files; +import fr.ifremer.tutti.service.ServiceDbResource; +import fr.ifremer.tutti.service.TuttiServiceContext; +import org.apache.commons.lang3.tuple.Pair; +import org.junit.Assert; +import org.junit.Before; +import org.junit.ClassRule; +import org.junit.Test; + +import java.io.File; + +/** + * @author tchemit <chemit@codelutin.com> + * @since 2.5 + */ +public class TuttiMultiPostExportServiceTest { + + @ClassRule + public static final ServiceDbResource dbResource = + ServiceDbResource.readDb("dbCGFS"); + + public static final String PROGRAM_ID = "CAM-CGFS"; + + public static final String CRUISE_ID = "100001"; + + public static final String CRUISE_CGFS_ID = "100000"; + + public static final String OPERATION_1_ID = "100106"; + + public static final String OPERATION_2_ID = "100107"; + + public static final String SPECIES_CONTENT = "id;parentId;species;categoryId;categoryValue;categoryWeight;weight;number;comment;toConfirm\n" + + "CatchRow_1;;11242;1428;311;100.0;;;;N;\n" + + "CatchRow_2;CatchRow_1;11242;198;305;80.0;;;;N;\n" + + "CatchRow_3;CatchRow_2;11242;196;300;30.0;;;;N;\n" + + "CatchRow_4;CatchRow_3;11242;174;272;10.0;5.0;;;N;\n" + + "CatchRow_5;CatchRow_3;11242;174;274;10.0;;;;N;\n" + + "CatchRow_6;CatchRow_2;11242;196;301;50.0;30.0;;;N;\n" + + "CatchRow_7;CatchRow_1;11242;198;306;20.0;;;;N;\n" + + "CatchRow_8;;11242;1428;310;20.0;;2;;N;"; + + public static final String SPECIES_FREQUENCIES_CONTENT = "batchId;lengthStepCaracteristic;lengthStep;number;weight\n" + + "CatchRow_4;307;10.0;5;;\n" + + "CatchRow_4;307;10.5;2;;\n" + + "CatchRow_4;307;11.0;1;;\n" + + "CatchRow_5;307;11.0;5;;\n" + + "CatchRow_6;1425;10.0;5;;\n" + + "CatchRow_6;1425;11.0;6;;\n" + + "CatchRow_6;1425;12.0;7;;"; + + public static final String SPECIES_WEIGHTS_CONTENT = "stationNumber;operationNumber;multirigAggregation;date;totalSortedWeight;inertWeight;livingNotItemizedWeight\n" + + "A;1;1;01/05/2013 00:00;;;;"; + + public static final String SPECIES_ATTACHMENT_CONTENT = "batchId;name;comment;file"; + + + public static final String BENTHOS_CONTENT = "id;parentId;species;categoryId;categoryValue;categoryWeight;weight;number;comment;toConfirm"; + + public static final String BENTHOS_FREQUENCIES_CONTENT = "batchId;lengthStepCaracteristic;lengthStep;number;weight"; + + public static final String BENTHOS_WEIGHTS_CONTENT = "stationNumber;operationNumber;multirigAggregation;date;totalSortedWeight;inertWeight;livingNotItemizedWeight\n" + + "A;1;1;01/05/2013 00:00;;;;"; + + public static final String BENTHOS_ATTACHMENT_CONTENT = "batchId;name;comment;file"; + + public static final String MARINE_LITTER_CONTENT = "batchId;category;sizeCategory;number;weight;comment\n" + + "MarineLitterRow_1;2119;2120;2;5.0;S1;\n" + + "MarineLitterRow_2;2126;2121;3;1.0;S2;"; + + public static final String MARINE_LITTER_WEIGHTS_CONTENT = "stationNumber;operationNumber;multirigAggregation;date;totalWeight\n" + + "A;1;1;01/05/2013 00:00;;"; + + public static final String ACCIDENTAL_CATCH_CONTENT = "batchId;species;gender;weight;size;lengthStepCaracteristic;deadOrAlive;comment\n" + + "AccidentalCatchRow_1;3835;301;10.0;4.0;1425;1769;;"; + + public static final String ACCIDENTAL_CATCH_WEIGHTS_CONTENT = "stationNumber;operationNumber;multirigAggregation;date\n" + + "A;1;1;01/05/2013 00:00;"; + + public static final String ACCIDENTAL_CATCH_CARACTERISTIC_CONTENT = "batchId;caracteristic;value"; + + public static final String ACCIDENTAL_CATCH_ATTACHMENT_CONTENT = "batchId;name;comment;file"; + + public static final String INDIVIDUAL_OBSERVATION_CONTENT = "batchId;species;weight;size;lengthStepCaracteristic;samplingCode;calcifiedPieceSamplingCode;comment\n" + + "IndividualObservationRow_1;11242;0.1;10.0;307;A20;10;P1;"; + + public static final String INDIVIDUAL_OBSERVATION_CARACTERISTIC_CONTENT = "batchId;caracteristic;value\n" + + "IndividualObservationRow_1;101;10.0;\n" + + "IndividualObservationRow_1;46;168;\n" + + "IndividualObservationRow_1;1388;5.0;"; + + public static final String INDIVIDUAL_OBSERVATION_WEIGHTS_CONTENT = "stationNumber;operationNumber;multirigAggregation;date\n" + + "A;1;1;01/05/2013 00:00;"; + + public static final String MARINE_LITTER_ATTACHMENT_CONTENT = "batchId;name;comment;file"; + + public static final String INDIVIDUAL_OBSERVATION_ATTACHMENT_CONTENT = "batchId;name;comment;file"; + + protected TuttiMultiPostExportService service; + + protected ServiceDbResource.DataContext dataContext; + + protected File dataDirectory; + + @Before + public void setUp() throws Exception { + + dataDirectory = dbResource.getServiceConfig().getDataDirectory(); + + TuttiServiceContext serviceContext = dbResource.getServiceContext(); + + dbResource.openDataContext(); + + service = serviceContext.getService(TuttiMultiPostExportService.class); + + dataContext = dbResource.loadContext(PROGRAM_ID, + CRUISE_ID, + 2, + OPERATION_1_ID, + OPERATION_2_ID); + } + + @Test + public void testExportSpecies() throws Exception { + + File exportFile = new File(dataDirectory, + "exportSpecies.zip"); + + Files.createParentDirs(exportFile); + + Assert.assertFalse(exportFile.exists()); + + service.exportSpecies(exportFile, dataContext.operations.get(0)); + Assert.assertTrue(exportFile.exists()); + + dbResource.assertZipContent( + "Species export", + exportFile, + Pair.of(TuttiMultiPostExportService.FREQUENCIES_FILE, SPECIES_FREQUENCIES_CONTENT), + Pair.of(TuttiMultiPostExportService.SPECIES_FILE, SPECIES_CONTENT), + Pair.of(TuttiMultiPostExportService.WEIGHTS_FILE, SPECIES_WEIGHTS_CONTENT), + Pair.of(TuttiMultiPostExportService.ATTACHMENTS_FILE, SPECIES_ATTACHMENT_CONTENT) + ); + + } + + @Test + public void testExportBenthos() throws Exception { + File exportFile = new File(dataDirectory, + "exportBenthos.zip"); + + Files.createParentDirs(exportFile); + + Assert.assertFalse(exportFile.exists()); + + service.exportBenthos(exportFile, dataContext.operations.get(0)); + Assert.assertTrue(exportFile.exists()); + + dbResource.assertZipContent( + "Benthos export", + exportFile, + Pair.of(TuttiMultiPostExportService.FREQUENCIES_FILE, BENTHOS_FREQUENCIES_CONTENT), + Pair.of(TuttiMultiPostExportService.BENTHOS_FILE, BENTHOS_CONTENT), + Pair.of(TuttiMultiPostExportService.WEIGHTS_FILE, BENTHOS_WEIGHTS_CONTENT), + Pair.of(TuttiMultiPostExportService.ATTACHMENTS_FILE, BENTHOS_ATTACHMENT_CONTENT) + + ); + } + + @Test + public void testExportMarineLitter() throws Exception { + File exportFile = new File(dataDirectory, + "exportMarineLitter.zip"); + + Files.createParentDirs(exportFile); + + Assert.assertFalse(exportFile.exists()); + + service.exportMarineLitter(exportFile, dataContext.operations.get(0)); + Assert.assertTrue(exportFile.exists()); + + dbResource.assertZipContent( + "MarineLitter export", + exportFile, + Pair.of(TuttiMultiPostExportService.MARINE_LITTER_FILE, MARINE_LITTER_CONTENT), + Pair.of(TuttiMultiPostExportService.WEIGHTS_FILE, MARINE_LITTER_WEIGHTS_CONTENT), + Pair.of(TuttiMultiPostExportService.ATTACHMENTS_FILE, MARINE_LITTER_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(TuttiMultiPostExportService.INDIVIDUAL_OBSERVATION_FILE, INDIVIDUAL_OBSERVATION_CONTENT), + Pair.of(TuttiMultiPostExportService.CARACTERISTIC_FILE, INDIVIDUAL_OBSERVATION_CARACTERISTIC_CONTENT), + Pair.of(TuttiMultiPostExportService.WEIGHTS_FILE, INDIVIDUAL_OBSERVATION_WEIGHTS_CONTENT), + Pair.of(TuttiMultiPostExportService.ATTACHMENTS_FILE, INDIVIDUAL_OBSERVATION_ATTACHMENT_CONTENT) + ); + } + + @Test + public void testExportAccidentalCatch() throws Exception { + File exportFile = new File(dataDirectory, + "exportAccidentalCatch.zip"); + + Files.createParentDirs(exportFile); + + Assert.assertFalse(exportFile.exists()); + + service.exportAccidentalCatch(exportFile, dataContext.operations.get(0)); + Assert.assertTrue(exportFile.exists()); + + dbResource.assertZipContent( + "AccidentalCatch export", + exportFile, + Pair.of(TuttiMultiPostExportService.ACCIDENTAL_CATCHES_FILE, ACCIDENTAL_CATCH_CONTENT), + Pair.of(TuttiMultiPostExportService.WEIGHTS_FILE, ACCIDENTAL_CATCH_WEIGHTS_CONTENT), + Pair.of(TuttiMultiPostExportService.CARACTERISTIC_FILE, ACCIDENTAL_CATCH_CARACTERISTIC_CONTENT), + Pair.of(TuttiMultiPostExportService.ATTACHMENTS_FILE, ACCIDENTAL_CATCH_ATTACHMENT_CONTENT) + ); + } +} Property changes on: trunk/tutti-service/src/test/java/fr/ifremer/tutti/service/catches/multipost/TuttiMultiPostExportServiceTest.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/TuttiUIContext.java =================================================================== --- trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/TuttiUIContext.java 2013-07-27 21:21:41 UTC (rev 1160) +++ trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/TuttiUIContext.java 2013-07-30 17:22:49 UTC (rev 1161) @@ -40,7 +40,8 @@ import fr.ifremer.tutti.service.catches.ExportCatchesReportService; import fr.ifremer.tutti.service.catches.TuttiWeightComputingService; import fr.ifremer.tutti.service.catches.ValidateCruiseOperationsService; -import fr.ifremer.tutti.service.catches.multipost.TuttiMultiPostImportExportService; +import fr.ifremer.tutti.service.catches.multipost.TuttiMultiPostExportService; +import fr.ifremer.tutti.service.catches.multipost.TuttiMultiPostImportService; import fr.ifremer.tutti.service.export.TuttiExportService; import fr.ifremer.tutti.service.protocol.TuttiProtocolImportExportService; import fr.ifremer.tutti.service.pupitri.TuttiPupitriImportExportService; @@ -566,10 +567,14 @@ return serviceContext.getService(TuttiCatchesSumatraExportService.class); } - public TuttiMultiPostImportExportService getMultiPostImportExportService() { - return serviceContext.getService(TuttiMultiPostImportExportService.class); + public TuttiMultiPostImportService getMultiPostImportService() { + return serviceContext.getService(TuttiMultiPostImportService.class); } + public TuttiMultiPostExportService getMultiPostExportService() { + return serviceContext.getService(TuttiMultiPostExportService.class); + } + public boolean useRealPersistenceService() { return isDbExist() && isDbLoaded(); } Modified: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/AbstractExportMultiPostAction.java =================================================================== --- trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/AbstractExportMultiPostAction.java 2013-07-27 21:21:41 UTC (rev 1160) +++ trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/AbstractExportMultiPostAction.java 2013-07-30 17:22:49 UTC (rev 1161) @@ -26,7 +26,7 @@ import fr.ifremer.tutti.persistence.entities.data.FishingOperation; import fr.ifremer.tutti.service.DecoratorService; -import fr.ifremer.tutti.service.catches.multipost.TuttiMultiPostImportExportService; +import fr.ifremer.tutti.service.catches.multipost.TuttiMultiPostExportService; import fr.ifremer.tutti.ui.swing.util.AbstractTuttiUIHandler; import fr.ifremer.tutti.ui.swing.util.TuttiUI; import fr.ifremer.tutti.ui.swing.util.action.AbstractTuttiAction; @@ -45,11 +45,11 @@ protected FishingOperation currentOperation; - protected TuttiMultiPostImportExportService multiPostImportExportService; + protected TuttiMultiPostExportService multiPostImportExportService; public AbstractExportMultiPostAction(H handler) { super(handler, false); - multiPostImportExportService = getContext().getMultiPostImportExportService(); + multiPostImportExportService = getContext().getMultiPostExportService(); } @Override Modified: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/AbstractImportMultiPostAction.java =================================================================== --- trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/AbstractImportMultiPostAction.java 2013-07-27 21:21:41 UTC (rev 1160) +++ trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/AbstractImportMultiPostAction.java 2013-07-30 17:22:49 UTC (rev 1161) @@ -25,7 +25,7 @@ */ import fr.ifremer.tutti.persistence.entities.data.FishingOperation; -import fr.ifremer.tutti.service.catches.multipost.TuttiMultiPostImportExportService; +import fr.ifremer.tutti.service.catches.multipost.TuttiMultiPostImportService; import fr.ifremer.tutti.ui.swing.content.operation.EditFishingOperationAction; import fr.ifremer.tutti.ui.swing.content.operation.FishingOperationsUI; import fr.ifremer.tutti.ui.swing.util.AbstractTuttiUIHandler; @@ -47,11 +47,11 @@ protected EditFishingOperationAction editAction; - protected TuttiMultiPostImportExportService multiPostImportExportService; + protected TuttiMultiPostImportService multiPostImportExportService; public AbstractImportMultiPostAction(H handler) { super(handler, false); - multiPostImportExportService = getContext().getMultiPostImportExportService(); + multiPostImportExportService = getContext().getMultiPostImportService(); } public EditFishingOperationAction getEditAction() { Modified: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/util/attachment/SaveAttachmentAction.java =================================================================== --- trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/util/attachment/SaveAttachmentAction.java 2013-07-27 21:21:41 UTC (rev 1160) +++ trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/util/attachment/SaveAttachmentAction.java 2013-07-30 17:22:49 UTC (rev 1161) @@ -1,5 +1,29 @@ package fr.ifremer.tutti.ui.swing.util.attachment; +/* + * #%L + * Tutti :: UI + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2012 - 2013 Ifremer + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/gpl-3.0.html>. + * #L% + */ + import fr.ifremer.tutti.TuttiIOUtil; import fr.ifremer.tutti.persistence.entities.data.Attachment; import fr.ifremer.tutti.ui.swing.TuttiUIContext; Property changes on: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/util/attachment/SaveAttachmentAction.java ___________________________________________________________________ Modified: svn:keywords - Author Date Id Revision + Author Date Id Revision HeadURL