Author: tchemit Date: 2012-10-11 19:13:02 +0200 (Thu, 11 Oct 2012) New Revision: 723 Url: http://forge.codelutin.com/repositories/revision/echobase/723 Log: fixes #1590: Am?\195?\169liorer l'import des donn?\195?\169es de p?\195?\170che Added: trunk/echobase-domain/src/main/java/fr/ifremer/echobase/entities/references/SampleTypeImpl.java Modified: trunk/echobase-domain/src/main/java/fr/ifremer/echobase/EchoBasePredicates.java trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/importdata/CatchesImportService.java Modified: trunk/echobase-domain/src/main/java/fr/ifremer/echobase/EchoBasePredicates.java =================================================================== --- trunk/echobase-domain/src/main/java/fr/ifremer/echobase/EchoBasePredicates.java 2012-10-11 12:20:26 UTC (rev 722) +++ trunk/echobase-domain/src/main/java/fr/ifremer/echobase/EchoBasePredicates.java 2012-10-11 17:13:02 UTC (rev 723) @@ -26,10 +26,15 @@ import com.google.common.collect.Sets; import fr.ifremer.echobase.entities.data.Cell; import fr.ifremer.echobase.entities.data.Echotype; +import fr.ifremer.echobase.entities.data.Operation; +import fr.ifremer.echobase.entities.data.Sample; import fr.ifremer.echobase.entities.references.AcousticInstrument; import fr.ifremer.echobase.entities.references.CellType; +import fr.ifremer.echobase.entities.references.SampleTypeImpl; import fr.ifremer.echobase.entities.references.SizeCategory; +import org.apache.commons.collections.CollectionUtils; +import java.util.Collection; import java.util.Set; /** @@ -100,6 +105,26 @@ } }; + public static final Predicate<Operation> OPERATION_WITH_TOTAL_OR_UNSORTED_SAMPLE = new Predicate<Operation>() { + @Override + public boolean apply(Operation input) { + Collection<Sample> samples = input.getSample(); + + Set<String> acceptedTypes = Sets.newHashSet(SampleTypeImpl.TOTAL_SAMPLE_TYPE, SampleTypeImpl.UNSORTED_SAMPLE_TYPE); + boolean result = false; + if (CollectionUtils.isNotEmpty(samples)) { + for (Sample sample : samples) { + String sampleType = sample.getSampleType().getName(); + if (acceptedTypes.contains(sampleType)) { + result = true; + break; + } + } + } + return result; + } + }; + public static Predicate<Echotype> newEchotypeByNamePredicate(String name) { return new EchotypeByNamePredicate(name); } Added: trunk/echobase-domain/src/main/java/fr/ifremer/echobase/entities/references/SampleTypeImpl.java =================================================================== --- trunk/echobase-domain/src/main/java/fr/ifremer/echobase/entities/references/SampleTypeImpl.java (rev 0) +++ trunk/echobase-domain/src/main/java/fr/ifremer/echobase/entities/references/SampleTypeImpl.java 2012-10-11 17:13:02 UTC (rev 723) @@ -0,0 +1,39 @@ +package fr.ifremer.echobase.entities.references; + +/* + * #%L + * EchoBase :: Domain + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 - 2012 Ifremer, Codelutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero 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 Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ + +public class SampleTypeImpl extends SampleTypeAbstract { + + private static final long serialVersionUID = 1L; + + public static final String TOTAL_SAMPLE_TYPE = "Total"; + + public static final String UNSORTED_SAMPLE_TYPE = "Unsorted"; + + public static final String SORTED_SAMPLE_TYPE = "Sorted"; + + public static final String SUB_SAMPLE_TYPE = "Subsample"; + + public static final String INDIVIDUAL_SAMPLE_TYPE = "Individual"; +} Property changes on: trunk/echobase-domain/src/main/java/fr/ifremer/echobase/entities/references/SampleTypeImpl.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/importdata/CatchesImportService.java =================================================================== --- trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/importdata/CatchesImportService.java 2012-10-11 12:20:26 UTC (rev 722) +++ trunk/echobase-services/src/main/java/fr/ifremer/echobase/services/importdata/CatchesImportService.java 2012-10-11 17:13:02 UTC (rev 723) @@ -24,6 +24,7 @@ package fr.ifremer.echobase.services.importdata; import com.google.common.base.Preconditions; +import com.google.common.collect.Collections2; import com.google.common.collect.Maps; import fr.ifremer.echobase.EchoBaseFunctions; import fr.ifremer.echobase.EchoBasePredicates; @@ -39,6 +40,7 @@ import fr.ifremer.echobase.entities.references.SampleDataTypeDAO; import fr.ifremer.echobase.entities.references.SampleType; import fr.ifremer.echobase.entities.references.SampleTypeDAO; +import fr.ifremer.echobase.entities.references.SampleTypeImpl; import fr.ifremer.echobase.entities.references.SexCategory; import fr.ifremer.echobase.entities.references.SizeCategory; import fr.ifremer.echobase.entities.references.Species; @@ -113,6 +115,14 @@ configuration.addResult(importResult); } + // build for each operation the one with at least one sample typed + // *total* or *unsorted* + + Collection<Operation> operationWithTotalOrUnsortedSample = + Collections2.filter( + operations, + EchoBasePredicates.OPERATION_WITH_TOTAL_OR_UNSORTED_SAMPLE); + inputFile = configuration.getSubSampleFile(); if (inputFile.hasFile()) { @@ -120,6 +130,7 @@ configuration, inputFile, operationMap, + operationWithTotalOrUnsortedSample, speciesMap, sizeCategoryMap ); @@ -133,6 +144,7 @@ configuration, inputFile, operationMap, + operationWithTotalOrUnsortedSample, speciesMap); configuration.addResult(importResult); @@ -165,11 +177,11 @@ SampleDataTypeDAO sampleDataTypeDAO = getDAO(SampleDataType.class, SampleDataTypeDAO.class); SpeciesCategoryDAO speciesCategoryDAO = getDAO(SpeciesCategory.class, SpeciesCategoryDAO.class); - SampleType sampleTypeTotal = findByProperties(sampleTypeDAO, SampleType.PROPERTY_NAME, "Total"); + SampleType sampleTypeTotal = findByProperties(sampleTypeDAO, SampleType.PROPERTY_NAME, SampleTypeImpl.TOTAL_SAMPLE_TYPE); Preconditions.checkNotNull(sampleTypeTotal); - SampleType sampleTypeUnsorted = findByProperties(sampleTypeDAO, SampleType.PROPERTY_NAME, "Unsorted"); + SampleType sampleTypeUnsorted = findByProperties(sampleTypeDAO, SampleType.PROPERTY_NAME, SampleTypeImpl.UNSORTED_SAMPLE_TYPE); Preconditions.checkNotNull(sampleTypeUnsorted); - SampleType sampleTypeSorted = findByProperties(sampleTypeDAO, SampleType.PROPERTY_NAME, "Sorted"); + SampleType sampleTypeSorted = findByProperties(sampleTypeDAO, SampleType.PROPERTY_NAME, SampleTypeImpl.SORTED_SAMPLE_TYPE); Preconditions.checkNotNull(sampleTypeSorted); SampleDataType sampleDataTypeMeanLength = findByProperties(sampleDataTypeDAO, SampleDataType.PROPERTY_NAME, "MeanLengthcm"); @@ -302,6 +314,7 @@ CatchesImportConfiguration configuration, InputFile inputFile, Map<String, Operation> operationMap, + Collection<Operation> operationWithTotalOrUnsortedSample, Map<String, Species> speciesMap, Map<String, SizeCategory> sizeCategoryMap ) throws ImportException { @@ -331,7 +344,7 @@ SampleDataTypeDAO sampleDataTypeDAO = getDAO(SampleDataType.class, SampleDataTypeDAO.class); SpeciesCategoryDAO speciesCategoryDAO = getDAO(SpeciesCategory.class, SpeciesCategoryDAO.class); - SampleType sampleTypeSubsample = findByProperties(sampleTypeDAO, SampleType.PROPERTY_NAME, "Subsample"); + SampleType sampleTypeSubsample = findByProperties(sampleTypeDAO, SampleType.PROPERTY_NAME, SampleTypeImpl.SUB_SAMPLE_TYPE); Preconditions.checkNotNull(sampleTypeSubsample); SampleDataType sampleDataTypeNumberAtLength = findByProperties(sampleDataTypeDAO, SampleDataType.PROPERTY_NAME, "NumberAtLength"); @@ -355,6 +368,11 @@ doFlushTransaction(++rowNumber, inputFile, configuration); Operation operation = row.getOperation(); + checkOperationWithTotalOrUnsortedSample( + rowNumber, + operationWithTotalOrUnsortedSample, + operation); + Species species = row.getSpecies(); SizeCategory sizeCategory = row.getSizeCategory(); SexCategory sexCategory = row.getSexCategory(); @@ -425,11 +443,23 @@ } } + private void checkOperationWithTotalOrUnsortedSample( + int rowNumber, + Collection<Operation> operationWithTotalOrUnsortedSample, + Operation operation + ) throws ImportException { + if (!operationWithTotalOrUnsortedSample.contains(operation)) { + + // can not accept this import + throw new ImportException("At line " + rowNumber + ", wants to add a subsample sample but operation " + operation.getId() + " has no sample of type *Total*, nor *Unsorted*"); + } + } + private EchoBaseCsvFileImportResult importBiometrySampleFile( CatchesImportConfiguration configuration, InputFile inputFile, Map<String, Operation> operationMap, - Map<String, Species> speciesMap + Collection<Operation> operationWithTotalOrUnsortedSample, Map<String, Species> speciesMap ) throws ImportException { if (log.isInfoEnabled()) { @@ -454,7 +484,7 @@ SampleTypeDAO sampleTypeDAO = getDAO(SampleType.class, SampleTypeDAO.class); SpeciesCategoryDAO speciesCategoryDAO = getDAO(SpeciesCategory.class, SpeciesCategoryDAO.class); - SampleType sampleTypeIndividual = findByProperties(sampleTypeDAO, SampleType.PROPERTY_NAME, "Individual"); + SampleType sampleTypeIndividual = findByProperties(sampleTypeDAO, SampleType.PROPERTY_NAME, SampleTypeImpl.INDIVIDUAL_SAMPLE_TYPE); Preconditions.checkNotNull(sampleTypeIndividual); Map<String, Sample> samples = Maps.newTreeMap(); @@ -469,8 +499,14 @@ for (BiometrySampleImportRow row : importer) { doFlushTransaction(++rowNumber, inputFile, configuration); + Operation operation = row.getOperation(); + checkOperationWithTotalOrUnsortedSample( + rowNumber, + operationWithTotalOrUnsortedSample, + operation); + Species species = row.getSpecies(); int numFish = row.getNumFish();