r1762 - in branches/tutti-3.4.x: tutti-service/src/main/java/fr/ifremer/tutti/service/psionimport tutti-service/src/main/resources/i18n tutti-service/src/test/java/fr/ifremer/tutti/service/psionimport tutti-service/src/test/resources/psion tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/action tutti-ui-swing/src/main/resources/i18n
Author: tchemit Date: 2014-05-13 22:52:44 +0200 (Tue, 13 May 2014) New Revision: 1762 Url: http://forge.codelutin.com/projects/tutti/repository/revisions/1762 Log: refs-90 #5059 (need new tests) Modified: branches/tutti-3.4.x/tutti-service/src/main/java/fr/ifremer/tutti/service/psionimport/PsionImportBatchModel.java branches/tutti-3.4.x/tutti-service/src/main/java/fr/ifremer/tutti/service/psionimport/PsionImportModel.java branches/tutti-3.4.x/tutti-service/src/main/java/fr/ifremer/tutti/service/psionimport/PsionImportResult.java branches/tutti-3.4.x/tutti-service/src/main/java/fr/ifremer/tutti/service/psionimport/PsionImportService.java branches/tutti-3.4.x/tutti-service/src/main/resources/i18n/tutti-service_en_GB.properties branches/tutti-3.4.x/tutti-service/src/main/resources/i18n/tutti-service_fr_FR.properties branches/tutti-3.4.x/tutti-service/src/test/java/fr/ifremer/tutti/service/psionimport/PsionImportServiceTest.java branches/tutti-3.4.x/tutti-service/src/test/resources/psion/CC053.IWA branches/tutti-3.4.x/tutti-service/src/test/resources/psion/FM001.IWA branches/tutti-3.4.x/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/action/ImportPsionAction.java branches/tutti-3.4.x/tutti-ui-swing/src/main/resources/i18n/tutti-ui-swing_fr_FR.properties Modified: branches/tutti-3.4.x/tutti-service/src/main/java/fr/ifremer/tutti/service/psionimport/PsionImportBatchModel.java =================================================================== --- branches/tutti-3.4.x/tutti-service/src/main/java/fr/ifremer/tutti/service/psionimport/PsionImportBatchModel.java 2014-05-13 14:30:26 UTC (rev 1761) +++ branches/tutti-3.4.x/tutti-service/src/main/java/fr/ifremer/tutti/service/psionimport/PsionImportBatchModel.java 2014-05-13 20:52:44 UTC (rev 1762) @@ -150,6 +150,17 @@ return frequencies.size(); } + void merge(PsionImportBatchModel batchModel) { + setWeight(getWeight() + batchModel.getWeight()); + setSampleWeight(getSampleWeight() + batchModel.getSampleWeight()); + + for (Map.Entry<Float, MutableInt> entry : batchModel.getFrequencies().entrySet()) { + Float stepClass = entry.getKey(); + int number = entry.getValue().intValue(); + addFrequency(stepClass, number); + } + } + @Override public String toString() { return new ToStringBuilder(this) Modified: branches/tutti-3.4.x/tutti-service/src/main/java/fr/ifremer/tutti/service/psionimport/PsionImportModel.java =================================================================== --- branches/tutti-3.4.x/tutti-service/src/main/java/fr/ifremer/tutti/service/psionimport/PsionImportModel.java 2014-05-13 14:30:26 UTC (rev 1761) +++ branches/tutti-3.4.x/tutti-service/src/main/java/fr/ifremer/tutti/service/psionimport/PsionImportModel.java 2014-05-13 20:52:44 UTC (rev 1762) @@ -24,19 +24,29 @@ * #L% */ +import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; import com.google.common.collect.Lists; -import com.google.common.collect.Maps; +import com.google.common.collect.Multimap; import com.google.common.collect.Sets; +import fr.ifremer.tutti.persistence.entities.TuttiEntities; import fr.ifremer.tutti.persistence.entities.referential.Species; -import org.apache.commons.lang3.mutable.MutableInt; +import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import java.io.IOException; +import java.util.Collection; +import java.util.HashMap; import java.util.Iterator; +import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Set; +import static org.nuiton.i18n.I18n.t; + /** * Created on 1/20/14. * @@ -48,40 +58,70 @@ /** Logger. */ private static final Log log = LogFactory.getLog(PsionImportModel.class); - protected final Map<String, PsionImportBatchModel> batchsByCategory; +// protected final Map<String, PsionImportBatchModel> batchsByCategory; + /** + * All registred species in their registred order. + */ + protected final LinkedHashSet<Species> speciesSet; + + /** + * All sorted batch indexed by their species. + * + * @since 3.4.2 + */ + protected final Multimap<Species, PsionImportBatchModel> sortedBatchsBySpecies; + + /** + * All unsorted batch indexed by their species. + * + * @since 3.4.2 + */ + protected final Multimap<Species, PsionImportBatchModel> unsortedBatchsBySpecies; + protected final List<String> errors; public PsionImportModel() { - batchsByCategory = Maps.newLinkedHashMap(); + speciesSet = new LinkedHashSet<>(); + sortedBatchsBySpecies = ArrayListMultimap.create(); + unsortedBatchsBySpecies = ArrayListMultimap.create(); +// batchsByCategory = Maps.newLinkedHashMap(); errors = Lists.newArrayList(); } public boolean withBatchs() { - return !batchsByCategory.isEmpty(); + return !speciesSet.isEmpty(); } public Set<Species> getSpecies() { - Set<Species> result = Sets.newLinkedHashSet(); - for (PsionImportBatchModel batch : batchsByCategory.values()) { - result.add(batch.getSpecies()); + Set<Species> result = ImmutableSet.copyOf(speciesSet); + return result; + } + + public List<PsionImportBatchModel> getUnsortedBatches(Species species) { + Collection<PsionImportBatchModel> batches = unsortedBatchsBySpecies.get(species); + List<PsionImportBatchModel> result = null; + + if (batches != null) { + result = ImmutableList.copyOf(batches); + } return result; } - public List<PsionImportBatchModel> getBatchs(Species species) { - List<PsionImportBatchModel> result = Lists.newArrayList(); - for (PsionImportBatchModel batch : batchsByCategory.values()) { - if (species.equals(batch.getSpecies())) { - result.add(batch); - } + public List<PsionImportBatchModel> getSortedBatches(Species species) { + Collection<PsionImportBatchModel> batches = sortedBatchsBySpecies.get(species); + List<PsionImportBatchModel> result = null; + + if (batches != null) { + result = ImmutableList.copyOf(batches); } return result; } public Set<Integer> getSampleCategoryIdUsed() { Set<Integer> result = Sets.newHashSet(); - for (PsionImportBatchModel batch : batchsByCategory.values()) { + for (PsionImportBatchModel batch : sortedBatchsBySpecies.values()) { Iterator<PsionImportBatchModel.SampleCategory> categoryIterator = batch.getCategoryIterator(); while (categoryIterator.hasNext()) { PsionImportBatchModel.SampleCategory next = categoryIterator.next(); @@ -89,6 +129,14 @@ result.add(next.getCategoryId()); } } + for (PsionImportBatchModel batch : unsortedBatchsBySpecies.values()) { + Iterator<PsionImportBatchModel.SampleCategory> categoryIterator = batch.getCategoryIterator(); + while (categoryIterator.hasNext()) { + PsionImportBatchModel.SampleCategory next = categoryIterator.next(); + + result.add(next.getCategoryId()); + } + } return result; } @@ -96,32 +144,65 @@ return !errors.isEmpty(); } + public List<String> getErrors() { + return errors; + } + void addBatch(PsionImportBatchModel batchModel) { - String cacheCode = batchModel.getSpecies().getSurveyCode() + "_" + batchModel.getCategoryCode(); + Species species = batchModel.getSpecies(); + speciesSet.add(species); + + String categoryCode = batchModel.getCategoryCode(); + Float weight = batchModel.getWeight(); + Float sampleWeight = batchModel.getSampleWeight(); - PsionImportBatchModel mergeBatch = batchsByCategory.get(cacheCode); + Multimap<Species, PsionImportBatchModel> store; + // --- Guess if sorted or unsorted batch --- // + if (TuttiEntities.isGreaterWeight(weight, 0) && TuttiEntities.isEqualWeight(weight, sampleWeight)) { + + store = unsortedBatchsBySpecies; + if (log.isInfoEnabled()) { + log.info(String.format("Found a unsorted batch [%s] %s - %s", species.getSurveyCode(), weight, sampleWeight)); + } + } else { + + store = sortedBatchsBySpecies; + if (log.isInfoEnabled()) { + log.info(String.format("Found a sorted batch [%s] %s - %s", species.getSurveyCode(), weight, sampleWeight)); + } + } + + // --- Get if exist the previous batch --- // + + Collection<PsionImportBatchModel> psionImportBatchModels = store.get(species); + + PsionImportBatchModel mergeBatch = null; + + if (CollectionUtils.isNotEmpty(psionImportBatchModels)) { + + for (PsionImportBatchModel importBatchModel : psionImportBatchModels) { + if (categoryCode.equals(importBatchModel.getCategoryCode())) { + mergeBatch = importBatchModel; + break; + } + } + } + if (mergeBatch == null) { // new batch - batchsByCategory.put(cacheCode, batchModel); + store.put(species, batchModel); if (log.isInfoEnabled()) { log.info("Added " + batchModel); } } else { - // merge data with this batch + // merge batch + mergeBatch.merge(batchModel); - mergeBatch.setWeight(mergeBatch.getWeight() + batchModel.getWeight()); - mergeBatch.setSampleWeight(mergeBatch.getSampleWeight() + batchModel.getSampleWeight()); - - for (Map.Entry<Float, MutableInt> entry : batchModel.getFrequencies().entrySet()) { - Float stepClass = entry.getKey(); - int number = entry.getValue().intValue(); - mergeBatch.addFrequency(stepClass, number); - } if (log.isInfoEnabled()) { log.info("Merged " + batchModel + " to " + mergeBatch); } @@ -132,7 +213,139 @@ errors.add(error); } - public List<String> getErrors() { - return errors; + void cleanSortedBatches() { + + for (Species species : sortedBatchsBySpecies.keySet()) { + + for (PsionImportBatchModel batchModel : sortedBatchsBySpecies.get(species)) { + + Float weight = batchModel.getWeight(); + Float sampleWeight = batchModel.getSampleWeight(); + + if (TuttiEntities.isEqualWeight(weight, 0) && TuttiEntities.isGreaterWeight(sampleWeight, 0)) { + + // POID = 0 et TAIL != POID : un seul poids à positionner + batchModel.setWeight(sampleWeight); + batchModel.setSampleWeight(null); + + } + + } + + } } + + void cleanUnsortedBatches() { + + for (Species species : unsortedBatchsBySpecies.keySet()) { + + for (PsionImportBatchModel batchModel : unsortedBatchsBySpecies.get(species)) { + + // POID = TAIL un seul poids à positionner + batchModel.setSampleWeight(null); + } + + } + } + + void checkSortedBatches() throws IOException { + + Set<Species> speciesSet = sortedBatchsBySpecies.keySet(); + + for (Species species : speciesSet) { + + Collection<PsionImportBatchModel> sortedBatches = sortedBatchsBySpecies.get(species); + + Map<String, Float> weightByCategory = new HashMap<>(); + + for (PsionImportBatchModel sortedBatch : sortedBatches) { + Float weight = sortedBatch.getWeight(); + if (TuttiEntities.isGreaterWeight(weight, 0)) { + + Float rootweight = weightByCategory.get(sortedBatch.getCategoryCode()); + + if (rootweight == null) { + + // first time + weightByCategory.put(sortedBatch.getCategoryCode(), weight); + } else if (!TuttiEntities.isEqualWeight(rootweight, weight)) { + + // can't have 2 batches with different vrac batch weight + throw new IOException( + t("tutti.service.psionimport.error.inconsistentVracWeight.message", species.getSurveyCode())); + } + + } + } + } + + } + +// public boolean withBatchs() { +// return !batchsByCategory.isEmpty(); +// } +// +// public Set<Species> getSpecies() { +// Set<Species> result = Sets.newLinkedHashSet(); +// for (PsionImportBatchModel batch : batchsByCategory.values()) { +// result.add(batch.getSpecies()); +// } +// return result; +// } +// +// public List<PsionImportBatchModel> getBatchs(Species species) { +// List<PsionImportBatchModel> result = Lists.newArrayList(); +// for (PsionImportBatchModel batch : batchsByCategory.values()) { +// if (species.equals(batch.getSpecies())) { +// result.add(batch); +// } +// } +// return result; +// } +// +// public Set<Integer> getSampleCategoryIdUsed() { +// Set<Integer> result = Sets.newHashSet(); +// for (PsionImportBatchModel batch : batchsByCategory.values()) { +// Iterator<PsionImportBatchModel.SampleCategory> categoryIterator = batch.getCategoryIterator(); +// while (categoryIterator.hasNext()) { +// PsionImportBatchModel.SampleCategory next = categoryIterator.next(); +// +// result.add(next.getCategoryId()); +// } +// } +// return result; +// } +// +// void addBatch(PsionImportBatchModel batchModel) { +// +// String cacheCode = batchModel.getSpecies().getSurveyCode() + "_" + batchModel.getCategoryCode(); +// +// PsionImportBatchModel mergeBatch = batchsByCategory.get(cacheCode); +// +// if (mergeBatch == null) { +// +// // new batch +// batchsByCategory.put(cacheCode, batchModel); +// +// if (log.isInfoEnabled()) { +// log.info("Added " + batchModel); +// } +// } else { +// +// // merge data with this batch +// +// mergeBatch.setWeight(mergeBatch.getWeight() + batchModel.getWeight()); +// mergeBatch.setSampleWeight(mergeBatch.getSampleWeight() + batchModel.getSampleWeight()); +// +// for (Map.Entry<Float, MutableInt> entry : batchModel.getFrequencies().entrySet()) { +// Float stepClass = entry.getKey(); +// int number = entry.getValue().intValue(); +// mergeBatch.addFrequency(stepClass, number); +// } +// if (log.isInfoEnabled()) { +// log.info("Merged " + batchModel + " to " + mergeBatch); +// } +// } +// } + } Modified: branches/tutti-3.4.x/tutti-service/src/main/java/fr/ifremer/tutti/service/psionimport/PsionImportResult.java =================================================================== --- branches/tutti-3.4.x/tutti-service/src/main/java/fr/ifremer/tutti/service/psionimport/PsionImportResult.java 2014-05-13 14:30:26 UTC (rev 1761) +++ branches/tutti-3.4.x/tutti-service/src/main/java/fr/ifremer/tutti/service/psionimport/PsionImportResult.java 2014-05-13 20:52:44 UTC (rev 1762) @@ -41,8 +41,12 @@ protected final List<String> errors; - protected int nbImported; + protected int nbSortedImported; + protected int nbUnsortedImported; + +// protected int nbImported; + public PsionImportResult(File importFile, List<String> errors) { this.importFile = importFile; this.errors = Lists.newArrayList(errors); @@ -52,18 +56,30 @@ return importFile; } - public int getNbImported() { - return nbImported; +// public int getNbImported() { +// return nbImported; +// } + + public int getNbSortedImported() { + return nbSortedImported; } + public int getNbUnsortedImported() { + return nbUnsortedImported; + } + public List<String> getErrors() { return errors; } - void incrementNbImported() { - this.nbImported++; + void incrementNbSortedImported() { + this.nbSortedImported++; } + void incrementNbUnsortedImported() { + this.nbUnsortedImported++; + } + void addError(String error) { errors.add(error); } Modified: branches/tutti-3.4.x/tutti-service/src/main/java/fr/ifremer/tutti/service/psionimport/PsionImportService.java =================================================================== --- branches/tutti-3.4.x/tutti-service/src/main/java/fr/ifremer/tutti/service/psionimport/PsionImportService.java 2014-05-13 14:30:26 UTC (rev 1761) +++ branches/tutti-3.4.x/tutti-service/src/main/java/fr/ifremer/tutti/service/psionimport/PsionImportService.java 2014-05-13 20:52:44 UTC (rev 1762) @@ -53,6 +53,7 @@ import fr.ifremer.tutti.service.PersistenceService; import fr.ifremer.tutti.service.TuttiServiceContext; import fr.ifremer.tutti.type.WeightUnit; +import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.mutable.MutableInt; @@ -232,33 +233,48 @@ if (log.isWarnEnabled()) { log.warn("Won't import psion file, errors detected."); } - } else { - // check sample categories exists + return result; + } - SampleCategoryModel sampleCategoryModel = context.getSampleCategoryModel(); - Set<Integer> sampleCategoryIdUsed = importModel.getSampleCategoryIdUsed(); - List<String> missingCategories = Lists.newArrayList(); - for (Integer categoryId : sampleCategoryIdUsed) { - if (!sampleCategoryModel.containsCategoryId(categoryId)) { - missingCategories.add("<li>" + categoryId + "</li>"); - } + // --- Check sample category id used --- // + SampleCategoryModel sampleCategoryModel = context.getSampleCategoryModel(); + Set<Integer> sampleCategoryIdUsed = importModel.getSampleCategoryIdUsed(); + List<String> missingCategories = Lists.newArrayList(); + for (Integer categoryId : sampleCategoryIdUsed) { + if (!sampleCategoryModel.containsCategoryId(categoryId)) { + missingCategories.add("<li>" + categoryId + "</li>"); } + } - if (!missingCategories.isEmpty()) { + if (!missingCategories.isEmpty()) { - result.addError( - t("tutti.service.psionimport.error.invalidSampleCategoryModel.message", - Joiner.on("").join(missingCategories)) - ); - } else { + result.addError( + t("tutti.service.psionimport.error.invalidSampleCategoryModel.message", + Joiner.on("").join(missingCategories)) + ); - // persist in db - persist(result, importModel, operation, catchBatch); - } + return result; + } + // --- Check sorted batches --- // + try { + importModel.checkSortedBatches(); + } catch (IOException e) { + result.addError(e.getMessage()); + return result; } + // --- clean sorted batches --- // + importModel.cleanSortedBatches(); + + // --- clean unsorted batches --- // + importModel.cleanUnsortedBatches(); + + // --- Ok no error, can persist --- // + + persist(result, importModel, operation, catchBatch); + return result; } @@ -285,7 +301,7 @@ throw new IOException(t("tutti.service.psionimport.error.invalid.date.format")); } - boolean correctOperation = Objects.equals(operationCode, operation.getStationNumber()) && + boolean correctOperation = Objects.equals(operationCode, String.valueOf(operation.getFishingOperationNumber())) && Objects.equals(operationDate, operation.getGearShootingStartDate()); if (!correctOperation) { @@ -492,112 +508,305 @@ // insert all imported species batches - TuttiEnumerationFile enumerationFile = persistenceService.getEnumerationFile(); +// TuttiEnumerationFile enumerationFile = persistenceService.getEnumerationFile(); Set<Species> species = importModel.getSpecies(); for (Species specy : species) { - List<PsionImportBatchModel> batchs = importModel.getBatchs(specy); + List<PsionImportBatchModel> sortedBatchs = importModel.getSortedBatches(specy); - if (batchs.size() == 1 && !batchs.get(0).withCategories()) { + if (CollectionUtils.isNotEmpty(sortedBatchs)) { + persistSortedBatches(operation, specy, sortedBatchs); - PsionImportBatchModel batchModel = batchs.get(0); + result.incrementNbSortedImported(); + } - // simple batch with no category - SpeciesBatch batch = createSpeciesBatch(operation, - batchModel.getSpecies(), - batchModel.getWeight(), - batchModel.getSampleWeight(), + List<PsionImportBatchModel> unsortedBatchs = importModel.getUnsortedBatches(specy); + + if (CollectionUtils.isNotEmpty(unsortedBatchs)) { + persistUnsortedBatches(operation, specy, unsortedBatchs); + + result.incrementNbUnsortedImported(); + } + +// List<PsionImportBatchModel> batchs = importModel.getSortedBatches(specy); +// +// if (batchs.size() == 1 && !batchs.get(0).withCategories()) { +// +// PsionImportBatchModel batchModel = batchs.get(0); +// +// // simple batch with no category +// SpeciesBatch batch = createSpeciesBatch(operation, +// batchModel.getSpecies(), +// batchModel.getWeight(), +// batchModel.getSampleWeight(), +// enumerationFile.PMFM_ID_SORTED_UNSORTED, +// sortedCaracteristic); +// +// batch = persistenceService.createSpeciesBatch(batch, null); +// +// persistFrequencies(batch, batchModel); +// +// } else { +// +// // batch with categories +// +// SpeciesBatch rootBatch = createSpeciesBatch(operation, +// specy, +// null, +// null, +// enumerationFile.PMFM_ID_SORTED_UNSORTED, +// sortedCaracteristic); +// +// rootBatch = persistenceService.createSpeciesBatch(rootBatch, null); +// +// +// for (PsionImportBatchModel batchModel : batchs) { +// +// SpeciesBatch parentBatch = rootBatch; +// +// SpeciesBatch childBatch = null; +// +// Iterator<PsionImportBatchModel.SampleCategory> categoryIterator = batchModel.getCategoryIterator(); +// +// while (categoryIterator.hasNext()) { +// PsionImportBatchModel.SampleCategory sampleCategory = categoryIterator.next(); +// +// boolean lastCategory = !categoryIterator.hasNext(); +// +// Integer categoryId = sampleCategory.getCategoryId(); +// Serializable categoryValue = sampleCategory.getCategoryValue(); +// +// if (lastCategory) { +// +// // always create the leaf +// childBatch = createSpeciesBatch(operation, +// specy, +// batchModel.getWeight(), +// batchModel.getSampleWeight(), +// categoryId, +// categoryValue); +// } else { +// +// // try to find child in parent children +// +// childBatch = null; +// for (SpeciesBatch speciesBatch : parentBatch.getChildBatchs()) { +// +// if (speciesBatch.getSampleCategoryId().equals(categoryId) && +// speciesBatch.getSampleCategoryValue().equals(categoryValue)) { +// childBatch = speciesBatch; +// break; +// } +// } +// +// if (childBatch == null) { +// +// // must create it +// childBatch = createSpeciesBatch(operation, +// specy, +// null, +// null, +// categoryId, +// categoryValue); +// } +// } +// +// if (TuttiEntities.isNew(childBatch)) { +// +// // persist it +// childBatch = persistenceService.createSpeciesBatch(childBatch, parentBatch.getId()); +// parentBatch.addChildBatchs(childBatch); +// } +// +// parentBatch = childBatch; +// } +// +// persistFrequencies(childBatch, batchModel); +// } +// } +// result.incrementNbImported(); + } + + persistenceService.saveCatchBatch(catchBatch); + } + + protected void persistSortedBatches(FishingOperation operation, + Species specy, + List<PsionImportBatchModel> batchs) { + + CaracteristicQualitativeValue caracteristicQualitativeValue = sortedCaracteristic; + + if (batchs.size() == 1 && !batchs.get(0).withCategories()) { + + PsionImportBatchModel batchModel = batchs.get(0); + + // simple batch with no category + SpeciesBatch batch = createSpeciesBatch(operation, + batchModel.getSpecies(), + batchModel.getWeight(), + batchModel.getSampleWeight(), + enumerationFile.PMFM_ID_SORTED_UNSORTED, + caracteristicQualitativeValue); + + batch = persistenceService.createSpeciesBatch(batch, null); + + persistFrequencies(batch, batchModel); + + } else { + + // batch with categories + + // Is there two weights ? If so then the weight has to be placed in the sorted batch + + Float sortedBatchWeight = null; + + Float weight = batchs.get(0).getWeight(); + Float sampleWeight = batchs.get(0).getSampleWeight(); + + if (sampleWeight != null) { + + // use the weight as sorted batch weight + sortedBatchWeight = weight; + } + + SpeciesBatch rootBatch = createSpeciesBatch(operation, + specy, + sortedBatchWeight, + null, enumerationFile.PMFM_ID_SORTED_UNSORTED, - sortedCaracteristic); + caracteristicQualitativeValue); - batch = persistenceService.createSpeciesBatch(batch, null); + rootBatch = persistenceService.createSpeciesBatch(rootBatch, null); - persistFrequencies(batch, batchModel); + createCategoryBatches(operation, specy, batchs, rootBatch, sortedBatchWeight != null); - } else { + } - // batch with categories + } - SpeciesBatch rootBatch = createSpeciesBatch(operation, - specy, - null, - null, - enumerationFile.PMFM_ID_SORTED_UNSORTED, - sortedCaracteristic); + protected void persistUnsortedBatches(FishingOperation operation, + Species specy, + List<PsionImportBatchModel> batchs) { - rootBatch = persistenceService.createSpeciesBatch(rootBatch, null); + CaracteristicQualitativeValue caracteristicQualitativeValue = unsortedCaracteristic; + if (batchs.size() == 1 && !batchs.get(0).withCategories()) { - for (PsionImportBatchModel batchModel : batchs) { + PsionImportBatchModel batchModel = batchs.get(0); - SpeciesBatch parentBatch = rootBatch; + // simple batch with no category + SpeciesBatch batch = createSpeciesBatch(operation, + batchModel.getSpecies(), + batchModel.getWeight(), + batchModel.getSampleWeight(), + enumerationFile.PMFM_ID_SORTED_UNSORTED, + caracteristicQualitativeValue); - SpeciesBatch childBatch = null; + batch = persistenceService.createSpeciesBatch(batch, null); - Iterator<PsionImportBatchModel.SampleCategory> categoryIterator = batchModel.getCategoryIterator(); + persistFrequencies(batch, batchModel); - while (categoryIterator.hasNext()) { - PsionImportBatchModel.SampleCategory sampleCategory = categoryIterator.next(); + } else { - boolean lastCategory = !categoryIterator.hasNext(); + // batch with categories - Integer categoryId = sampleCategory.getCategoryId(); - Serializable categoryValue = sampleCategory.getCategoryValue(); + SpeciesBatch rootBatch = createSpeciesBatch(operation, + specy, + null, + null, + enumerationFile.PMFM_ID_SORTED_UNSORTED, + caracteristicQualitativeValue); - if (lastCategory) { + rootBatch = persistenceService.createSpeciesBatch(rootBatch, null); - // always create the leaf - childBatch = createSpeciesBatch(operation, - specy, - batchModel.getWeight(), - batchModel.getSampleWeight(), - categoryId, - categoryValue); - } else { + createCategoryBatches(operation, specy, batchs, rootBatch, false); - // try to find child in parent children + } - childBatch = null; - for (SpeciesBatch speciesBatch : parentBatch.getChildBatchs()) { + } - if (speciesBatch.getSampleCategoryId().equals(categoryId) && - speciesBatch.getSampleCategoryValue().equals(categoryValue)) { - childBatch = speciesBatch; - break; - } - } + protected void createCategoryBatches(FishingOperation operation, + Species specy, + List<PsionImportBatchModel> batchs, + SpeciesBatch rootBatch, + boolean applyOnlySampleWeight) { - if (childBatch == null) { + for (PsionImportBatchModel batchModel : batchs) { - // must create it - childBatch = createSpeciesBatch(operation, - specy, - null, - null, - categoryId, - categoryValue); - } - } + SpeciesBatch parentBatch = rootBatch; - if (TuttiEntities.isNew(childBatch)) { + SpeciesBatch childBatch = null; - // persist it - childBatch = persistenceService.createSpeciesBatch(childBatch, parentBatch.getId()); - parentBatch.addChildBatchs(childBatch); + Iterator<PsionImportBatchModel.SampleCategory> categoryIterator = batchModel.getCategoryIterator(); + + while (categoryIterator.hasNext()) { + PsionImportBatchModel.SampleCategory sampleCategory = categoryIterator.next(); + + boolean lastCategory = !categoryIterator.hasNext(); + + Integer categoryId = sampleCategory.getCategoryId(); + Serializable categoryValue = sampleCategory.getCategoryValue(); + + if (lastCategory) { + + // always create the leaf + + Float weight; + Float sampleWeight; + + if (applyOnlySampleWeight) { + weight = batchModel.getSampleWeight(); + sampleWeight = null; + } else { + weight = batchModel.getWeight(); + sampleWeight = batchModel.getSampleWeight(); + } + childBatch = createSpeciesBatch(operation, + specy, + weight, + sampleWeight, + categoryId, + categoryValue); + } else { + + // try to find child in parent children + + childBatch = null; + for (SpeciesBatch speciesBatch : parentBatch.getChildBatchs()) { + + if (speciesBatch.getSampleCategoryId().equals(categoryId) && + speciesBatch.getSampleCategoryValue().equals(categoryValue)) { + childBatch = speciesBatch; + break; } + } - parentBatch = childBatch; + if (childBatch == null) { + + // must create it + childBatch = createSpeciesBatch(operation, + specy, + null, + null, + categoryId, + categoryValue); } + } - persistFrequencies(childBatch, batchModel); + if (TuttiEntities.isNew(childBatch)) { + + // persist it + childBatch = persistenceService.createSpeciesBatch(childBatch, parentBatch.getId()); + parentBatch.addChildBatchs(childBatch); } + + parentBatch = childBatch; } - result.incrementNbImported(); + + persistFrequencies(childBatch, batchModel); } - - persistenceService.saveCatchBatch(catchBatch); } protected PsionImportBatchModel.SampleCategory guessCategory(String categoryCode) { Modified: branches/tutti-3.4.x/tutti-service/src/main/resources/i18n/tutti-service_en_GB.properties =================================================================== --- branches/tutti-3.4.x/tutti-service/src/main/resources/i18n/tutti-service_en_GB.properties 2014-05-13 14:30:26 UTC (rev 1761) +++ branches/tutti-3.4.x/tutti-service/src/main/resources/i18n/tutti-service_en_GB.properties 2014-05-13 20:52:44 UTC (rev 1762) @@ -160,6 +160,7 @@ tutti.service.protocol.import.species.error= tutti.service.protocol.import.taxonUsed.error= tutti.service.psion.import.attachment.comment= +tutti.service.psionimport.error.inconsistentVracWeight.message= tutti.service.psionimport.error.invalid.category.syntax= tutti.service.psionimport.error.invalid.command.syntax= tutti.service.psionimport.error.invalid.date.format= Modified: branches/tutti-3.4.x/tutti-service/src/main/resources/i18n/tutti-service_fr_FR.properties =================================================================== --- branches/tutti-3.4.x/tutti-service/src/main/resources/i18n/tutti-service_fr_FR.properties 2014-05-13 14:30:26 UTC (rev 1761) +++ branches/tutti-3.4.x/tutti-service/src/main/resources/i18n/tutti-service_fr_FR.properties 2014-05-13 20:52:44 UTC (rev 1762) @@ -159,6 +159,7 @@ tutti.service.protocol.import.species.error=Erreur lors de l'import des espèces du protocole %1s du fichier %2s tutti.service.protocol.import.taxonUsed.error=Le taxon référent d'id %s est déjà utilisé tutti.service.psion.import.attachment.comment=Import Psion du %s +tutti.service.psionimport.error.inconsistentVracWeight.message=Pour l'espèce '%s', il existe deux enregistrements de lot vrac avec le champs 'POID' différent, ce qui est interdit tutti.service.psionimport.error.invalid.category.syntax=Ligne %s, catégorisation '%s' inconnue, l'espèce %s sera ignorée tutti.service.psionimport.error.invalid.command.syntax=Ligne %s, la commande '%s' n'est pas reconnue tutti.service.psionimport.error.invalid.date.format=Format de la date du trait incorrecte (mm-dd-aaaa) Modified: branches/tutti-3.4.x/tutti-service/src/test/java/fr/ifremer/tutti/service/psionimport/PsionImportServiceTest.java =================================================================== --- branches/tutti-3.4.x/tutti-service/src/test/java/fr/ifremer/tutti/service/psionimport/PsionImportServiceTest.java 2014-05-13 14:30:26 UTC (rev 1761) +++ branches/tutti-3.4.x/tutti-service/src/test/java/fr/ifremer/tutti/service/psionimport/PsionImportServiceTest.java 2014-05-13 20:52:44 UTC (rev 1762) @@ -24,12 +24,16 @@ * #L% */ +import com.google.common.base.Predicate; import fr.ifremer.tutti.TuttiConfigurationOption; import fr.ifremer.tutti.persistence.ProgressionModel; +import fr.ifremer.tutti.persistence.entities.TuttiEntities; import fr.ifremer.tutti.persistence.entities.data.BatchContainer; import fr.ifremer.tutti.persistence.entities.data.CatchBatch; import fr.ifremer.tutti.persistence.entities.data.FishingOperation; +import fr.ifremer.tutti.persistence.entities.data.SpeciesAbleBatch; import fr.ifremer.tutti.persistence.entities.data.SpeciesBatch; +import fr.ifremer.tutti.persistence.service.TuttiEnumerationFile; import fr.ifremer.tutti.service.PersistenceService; import fr.ifremer.tutti.service.ServiceDbResource; import fr.ifremer.tutti.service.TuttiServiceContext; @@ -79,6 +83,8 @@ protected File dataDirectory; + protected Predicate<SpeciesAbleBatch> vracPredicate; + @Before public void setUp() throws Exception { @@ -99,6 +105,9 @@ progressionModel.setTotal(9); dataContext = dbResource.loadContext(PROGRAM_ID, CRUISE_ID, 3, OPERATION_2_ID, OPERATION_1_ID, OPERATION_3_ID); + + TuttiEnumerationFile enumerationFile = persistenceService.getEnumerationFile(); + vracPredicate = TuttiEntities.newSpeciesAbleBatchCategoryPredicate(enumerationFile.PMFM_ID_SORTED_UNSORTED, enumerationFile.QUALITATIVE_VRAC_ID); } @Test @@ -111,24 +120,55 @@ catchBatch.setFishingOperation(operation); BatchContainer<SpeciesBatch> rootSpeciesBatch = persistenceService.getRootSpeciesBatch(operation.getId(), null); - int oldNbBatchs = rootSpeciesBatch.sizeChildren(); + int oldSortedBatchs = 0; + int oldUnsortedBatchs = 0; + for (SpeciesBatch speciesBatch : rootSpeciesBatch.getChildren()) { + boolean sorted = vracPredicate.apply(speciesBatch); + + if (sorted) { + oldSortedBatchs++; + } else { + oldUnsortedBatchs++; + } + } + PsionImportResult importResult = service.importFile(importFile, operation, catchBatch); - int nbAdded = importResult.getNbImported(); + int nbSortedAdded = importResult.getNbSortedImported(); + int nbUnsortedAdded = importResult.getNbUnsortedImported(); List<String> errors = importResult.getErrors(); if (log.isInfoEnabled()) { - log.info("Imported: " + nbAdded); + log.info("Sorted Imported: " + nbSortedAdded); + log.info("Unsorted Imported: " + nbUnsortedAdded); log.info("Errors: " + errors.size()); } - Assert.assertEquals(17, nbAdded); + int nbNewSortedBatchs = 10; + int nbNewUnsortedBatchs = 9; + Assert.assertEquals(nbNewSortedBatchs, nbSortedAdded); + Assert.assertEquals(nbNewUnsortedBatchs, nbUnsortedAdded); Assert.assertEquals(0, errors.size()); // no batch imported BatchContainer<SpeciesBatch> rootSpeciesBatchAfter = persistenceService.getRootSpeciesBatch(operation.getId(), null); - Assert.assertEquals(oldNbBatchs + 17, rootSpeciesBatchAfter.sizeChildren()); + + int totalSortedBatchs = 0; + int totalUnsortedBatchs = 0; + for (SpeciesBatch speciesBatch : rootSpeciesBatchAfter.getChildren()) { + + boolean sorted = vracPredicate.apply(speciesBatch); + + if (sorted) { + totalSortedBatchs++; + } else { + totalUnsortedBatchs++; + } + } + + Assert.assertEquals(oldSortedBatchs + nbNewSortedBatchs, totalSortedBatchs); + Assert.assertEquals(oldUnsortedBatchs + nbNewUnsortedBatchs, totalUnsortedBatchs); } @Test @@ -141,23 +181,54 @@ catchBatch.setFishingOperation(operation); BatchContainer<SpeciesBatch> rootSpeciesBatch = persistenceService.getRootSpeciesBatch(operation.getId(), null); - int oldNbBatchs = rootSpeciesBatch.sizeChildren(); + int oldSortedBatchs = 0; + int oldUnsortedBatchs = 0; + for (SpeciesBatch speciesBatch : rootSpeciesBatch.getChildren()) { + boolean sorted = vracPredicate.apply(speciesBatch); + + if (sorted) { + oldSortedBatchs++; + } else { + oldUnsortedBatchs++; + } + } + PsionImportResult importResult = service.importFile(importFile, operation, catchBatch); - int nbAdded = importResult.getNbImported(); + int nbSortedAdded = importResult.getNbSortedImported(); + int nbUnsortedAdded = importResult.getNbUnsortedImported(); List<String> errors = importResult.getErrors(); if (log.isInfoEnabled()) { - log.info("Imported: " + nbAdded); + log.info("Sorted Imported: " + nbSortedAdded); + log.info("Unsorted Imported: " + nbUnsortedAdded); log.info("Errors: " + errors.size()); } - Assert.assertEquals(10, nbAdded); + int nbNewSortedBatchs = 0; + int nbNewUnsortedBatchs = 10; + Assert.assertEquals(nbNewSortedBatchs, nbSortedAdded); + Assert.assertEquals(nbNewUnsortedBatchs, nbUnsortedAdded); Assert.assertEquals(0, errors.size()); BatchContainer<SpeciesBatch> rootSpeciesBatchAfter = persistenceService.getRootSpeciesBatch(operation.getId(), null); - Assert.assertEquals(oldNbBatchs + 10, rootSpeciesBatchAfter.sizeChildren()); + + int totalSortedBatchs = 0; + int totalUnsortedBatchs = 0; + for (SpeciesBatch speciesBatch : rootSpeciesBatchAfter.getChildren()) { + + boolean sorted = vracPredicate.apply(speciesBatch); + + if (sorted) { + totalSortedBatchs++; + } else { + totalUnsortedBatchs++; + } + } + + Assert.assertEquals(oldSortedBatchs + nbNewSortedBatchs, totalSortedBatchs); + Assert.assertEquals(oldUnsortedBatchs + nbNewUnsortedBatchs, totalUnsortedBatchs); } @Test @@ -173,15 +244,18 @@ int oldNbBatchs = rootSpeciesBatch.sizeChildren(); PsionImportResult importResult = service.importFile(importFile, operation, catchBatch); - int nbAdded = importResult.getNbImported(); + int nbSortedAdded = importResult.getNbSortedImported(); + int nbUnsortedAdded = importResult.getNbUnsortedImported(); List<String> errors = importResult.getErrors(); if (log.isInfoEnabled()) { - log.info("Imported: " + nbAdded); + log.info("Sorted Imported: " + nbSortedAdded); + log.info("Unsorted Imported: " + nbUnsortedAdded); log.info("Errors: " + errors.size()); } - Assert.assertEquals(0, nbAdded); + Assert.assertEquals(0, nbSortedAdded); + Assert.assertEquals(0, nbUnsortedAdded); Assert.assertEquals(1, errors.size()); // no batch imported Modified: branches/tutti-3.4.x/tutti-service/src/test/resources/psion/CC053.IWA =================================================================== --- branches/tutti-3.4.x/tutti-service/src/test/resources/psion/CC053.IWA 2014-05-13 14:30:26 UTC (rev 1761) +++ branches/tutti-3.4.x/tutti-service/src/test/resources/psion/CC053.IWA 2014-05-13 20:52:44 UTC (rev 1762) @@ -1,5 +1,5 @@ cc -A +1 07-01-2013 07:19:11 Modified: branches/tutti-3.4.x/tutti-service/src/test/resources/psion/FM001.IWA =================================================================== --- branches/tutti-3.4.x/tutti-service/src/test/resources/psion/FM001.IWA 2014-05-13 14:30:26 UTC (rev 1761) +++ branches/tutti-3.4.x/tutti-service/src/test/resources/psion/FM001.IWA 2014-05-13 20:52:44 UTC (rev 1762) @@ -1,5 +1,5 @@ fm -A +1 07-01-2013 18:28:13 Modified: branches/tutti-3.4.x/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/action/ImportPsionAction.java =================================================================== --- branches/tutti-3.4.x/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/action/ImportPsionAction.java 2014-05-13 14:30:26 UTC (rev 1761) +++ branches/tutti-3.4.x/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/action/ImportPsionAction.java 2014-05-13 20:52:44 UTC (rev 1762) @@ -125,7 +125,7 @@ if (importResult.isDone()) { sendMessage(t("tutti.editSpeciesBatch.action.importPsion.success", - importResult.getNbImported())); + importResult.getNbSortedImported(), importResult.getNbUnsortedImported())); } else { StringBuilder sb = new StringBuilder(); Modified: branches/tutti-3.4.x/tutti-ui-swing/src/main/resources/i18n/tutti-ui-swing_fr_FR.properties =================================================================== --- branches/tutti-3.4.x/tutti-ui-swing/src/main/resources/i18n/tutti-ui-swing_fr_FR.properties 2014-05-13 14:30:26 UTC (rev 1761) +++ branches/tutti-3.4.x/tutti-ui-swing/src/main/resources/i18n/tutti-ui-swing_fr_FR.properties 2014-05-13 20:52:44 UTC (rev 1762) @@ -982,7 +982,7 @@ tutti.editSpeciesBatch.action.importPsion.no.matching.data=Import psion non réalisé (des erreurs ont été détectées lors de la lecture du fichier) tutti.editSpeciesBatch.action.importPsion.no.matching.fishingOperation=L'import Psion n'a pas été réalisé, des erreurs ont été détectées \:<ul>%s</ul><br/>Aucun lot n'a donc été importé. tutti.editSpeciesBatch.action.importPsion.no.matching.fishingOperation.title=Import Psion -tutti.editSpeciesBatch.action.importPsion.success=Import Psion réussi \: %1s espèces importées +tutti.editSpeciesBatch.action.importPsion.success=Import Psion réussi \: %1s espèces importées (Vrac), %2s espèces importées (Hors-Vrac) tutti.editSpeciesBatch.action.importPsion.tip=Import Psion tutti.editSpeciesBatch.action.importPupitri=Import Pupitri tutti.editSpeciesBatch.action.importPupitri.existingData.help=Que voulez-vous faire ?<ul><li><strong>Annuler</strong> pour ne pas importer les données Pupitri et conserver les espèces saisies</li><li><strong>OK</strong> pour supprimer les espèces existantes et les remplacer par les données de Pupitri</li></ul>
participants (1)
-
tchemit@users.forge.codelutin.com