This is an automated email from the git hooks/post-receive script. New commit to branch feature/6688 in repository tutti. See http://git.codelutin.com/tutti.git commit 923d204b4821174e288b257eb51a4874bd11e082 Author: Tony CHEMIT <chemit@codelutin.com> Date: Sun Feb 22 08:37:31 2015 +0100 generic import is ok \o/ --- .../GenericFormatImportOperationContext.java | 30 ++---- .../genericformat/GenericFormatImportService.java | 5 +- .../GenericformatImportPersitenceHelper.java | 106 +++++++++++-------- .../consumer/CsvConsumerForCatch.java | 112 ++++++++++++--------- 4 files changed, 140 insertions(+), 113 deletions(-) diff --git a/tutti-service/src/main/java/fr/ifremer/tutti/service/genericformat/GenericFormatImportOperationContext.java b/tutti-service/src/main/java/fr/ifremer/tutti/service/genericformat/GenericFormatImportOperationContext.java index 5a1d60f..e614a49 100644 --- a/tutti-service/src/main/java/fr/ifremer/tutti/service/genericformat/GenericFormatImportOperationContext.java +++ b/tutti-service/src/main/java/fr/ifremer/tutti/service/genericformat/GenericFormatImportOperationContext.java @@ -2,7 +2,6 @@ package fr.ifremer.tutti.service.genericformat; import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableSet; import fr.ifremer.tutti.persistence.entities.CaracteristicMap; import fr.ifremer.tutti.persistence.entities.data.AccidentalBatch; import fr.ifremer.tutti.persistence.entities.data.BenthosBatch; @@ -19,6 +18,7 @@ import org.apache.commons.collections4.MapUtils; import java.io.Serializable; import java.util.ArrayList; import java.util.Collection; +import java.util.List; import java.util.Map; import java.util.TreeMap; @@ -44,13 +44,13 @@ public class GenericFormatImportOperationContext { private final Map<Integer, SpeciesBatch> horsVracSpeciesBatches; - private final ArrayListMultimap<SpeciesBatch, SpeciesBatchFrequency> speciesFrequencies; + private final ArrayListMultimap<Integer, SpeciesBatchFrequency> speciesFrequencies; private final Map<Integer, BenthosBatch> vracBenthosBatches; private final Map<Integer, BenthosBatch> horsVracBenthosBatches; - private final ArrayListMultimap<BenthosBatch, BenthosBatchFrequency> benthosFrequencies; + private final ArrayListMultimap<Integer, BenthosBatchFrequency> benthosFrequencies; private final CaracteristicMap fishinOperationGearUseFeatures; @@ -110,14 +110,6 @@ public class GenericFormatImportOperationContext { return MapUtils.isNotEmpty(getBenthosBatchMap(vrac)); } - public boolean withSpeciesFrequencies() { - return !speciesFrequencies.isEmpty(); - } - - public boolean withBenthosFrequencies() { - return !benthosFrequencies.isEmpty(); - } - public AccidentalBatch getAccidentalBatchById(Integer accidentalBatchId) { return accidentalBatchesById.get(accidentalBatchId); } @@ -163,11 +155,11 @@ public class GenericFormatImportOperationContext { } public void addSpeciesFrequency(SpeciesBatch batch, SpeciesBatchFrequency frequency) { - speciesFrequencies.put(batch, frequency); + speciesFrequencies.put(batch.getIdAsInt(), frequency); } public void addBenthosFrequency(BenthosBatch batch, BenthosBatchFrequency frequency) { - benthosFrequencies.put(batch, frequency); + benthosFrequencies.put(batch.getIdAsInt(), frequency); } public Collection<MarineLitterBatch> getMarineLitterBatches() { @@ -183,19 +175,19 @@ public class GenericFormatImportOperationContext { } public Collection<SpeciesBatch> getSpeciesBatches(boolean vrac) { - return ImmutableSet.copyOf(getSpeciesBatchMap(vrac).values()); + return ImmutableList.copyOf(getSpeciesBatchMap(vrac).values()); } public Collection<BenthosBatch> getBenthosBatches(boolean vrac) { - return ImmutableSet.copyOf(getBenthosBatchMap(vrac).values()); + return ImmutableList.copyOf(getBenthosBatchMap(vrac).values()); } - public ArrayListMultimap<BenthosBatch, BenthosBatchFrequency> getBenthosFrequencies() { - return benthosFrequencies; + public List<BenthosBatchFrequency> getBenthosFrequencies(BenthosBatch benthosBatch) { + return benthosFrequencies.get(benthosBatch.getIdAsInt()); } - public ArrayListMultimap<SpeciesBatch, SpeciesBatchFrequency> getSpeciesFrequencies() { - return speciesFrequencies; + public List<SpeciesBatchFrequency> getSpeciesFrequencies(SpeciesBatch speciesBatch) { + return speciesFrequencies.get(speciesBatch.getIdAsInt()); } public CaracteristicMap getFishinOperationGearUseFeatures() { diff --git a/tutti-service/src/main/java/fr/ifremer/tutti/service/genericformat/GenericFormatImportService.java b/tutti-service/src/main/java/fr/ifremer/tutti/service/genericformat/GenericFormatImportService.java index bd579e2..eb1bbea 100644 --- a/tutti-service/src/main/java/fr/ifremer/tutti/service/genericformat/GenericFormatImportService.java +++ b/tutti-service/src/main/java/fr/ifremer/tutti/service/genericformat/GenericFormatImportService.java @@ -519,9 +519,8 @@ public class GenericFormatImportService extends AbstractTuttiService { throw new ApplicationTechnicalException("Could not close catch.csv file", e); } - //TODO -// importContext.getPersitenceHelper().persistSpeciesBatches(); -// importContext.getPersitenceHelper().persistBenthosBatches(); + importContext.getPersitenceHelper().persistSpeciesBatches(); + importContext.getPersitenceHelper().persistBenthosBatches(); } diff --git a/tutti-service/src/main/java/fr/ifremer/tutti/service/genericformat/GenericformatImportPersitenceHelper.java b/tutti-service/src/main/java/fr/ifremer/tutti/service/genericformat/GenericformatImportPersitenceHelper.java index 409b147..92efdc0 100644 --- a/tutti-service/src/main/java/fr/ifremer/tutti/service/genericformat/GenericformatImportPersitenceHelper.java +++ b/tutti-service/src/main/java/fr/ifremer/tutti/service/genericformat/GenericformatImportPersitenceHelper.java @@ -1,7 +1,6 @@ package fr.ifremer.tutti.service.genericformat; import com.google.common.base.Predicate; -import com.google.common.collect.ArrayListMultimap; import fr.ifremer.tutti.persistence.entities.CaracteristicMap; import fr.ifremer.tutti.persistence.entities.data.AccidentalBatch; import fr.ifremer.tutti.persistence.entities.data.BenthosBatch; @@ -15,6 +14,7 @@ import fr.ifremer.tutti.persistence.entities.data.SpeciesBatch; import fr.ifremer.tutti.persistence.entities.data.SpeciesBatchFrequency; import fr.ifremer.tutti.persistence.entities.referential.Gear; import fr.ifremer.tutti.service.PersistenceService; +import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.MapUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -230,42 +230,24 @@ public class GenericformatImportPersitenceHelper { for (GenericFormatImportOperationContext fishingOperationContext : fishingOperationContexts) { FishingOperation fishingOperation = fishingOperationContext.getFishingOperation(); - String fishingOperationId = fishingOperation.getId(); if (fishingOperationContext.withSpeciesBatches(true)) { - Collection<SpeciesBatch> speciesBatches = fishingOperationContext.getSpeciesBatches(true); + Collection<SpeciesBatch> batches = fishingOperationContext.getSpeciesBatches(true); if (log.isInfoEnabled()) { - log.info("Persist " + speciesBatches.size() + " VRAC species batch(es) of " + fishingOperationContext.getFishingOperationLabel() + " for cruise: " + cruiseDecorator.toString(fishingOperation.getCruise())); + log.info("Persist " + batches.size() + " VRAC root species batch(es) of " + fishingOperationContext.getFishingOperationLabel() + " for cruise: " + cruiseDecorator.toString(fishingOperation.getCruise())); } - persistenceService.createSpeciesBatches(fishingOperationId, speciesBatches); + persistSpeciesBatches(fishingOperationContext, batches, null); } if (fishingOperationContext.withSpeciesBatches(false)) { - Collection<SpeciesBatch> speciesBatches = fishingOperationContext.getSpeciesBatches(false); + Collection<SpeciesBatch> batches = fishingOperationContext.getSpeciesBatches(false); if (log.isInfoEnabled()) { - log.info("Persist " + speciesBatches.size() + " HORS VRAC species batch(es) of " + fishingOperationContext.getFishingOperationLabel() + " for cruise: " + cruiseDecorator.toString(fishingOperation.getCruise())); - } - persistenceService.createSpeciesBatches(fishingOperationId, speciesBatches); - - } - - if (fishingOperationContext.withSpeciesFrequencies()) { - - ArrayListMultimap<SpeciesBatch, SpeciesBatchFrequency> allFrequencies = fishingOperationContext.getSpeciesFrequencies(); - for (SpeciesBatch benthosBatch : allFrequencies.keySet()) { - - List<SpeciesBatchFrequency> frequencies = allFrequencies.get(benthosBatch); - - String batchId = benthosBatch.getId(); - if (log.isInfoEnabled()) { - log.info("Persist " + frequencies.size() + " frequency(ies) (species batch: " + batchId + ") of " + fishingOperationContext.getFishingOperationLabel() + " for cruise: " + cruiseDecorator.toString(fishingOperation.getCruise())); - } - persistenceService.saveSpeciesBatchFrequency(batchId, frequencies); - + log.info("Persist " + batches.size() + " HORS VRAC root species batch(es) of " + fishingOperationContext.getFishingOperationLabel() + " for cruise: " + cruiseDecorator.toString(fishingOperation.getCruise())); } + persistSpeciesBatches(fishingOperationContext, batches, null); } @@ -285,45 +267,87 @@ public class GenericformatImportPersitenceHelper { for (GenericFormatImportOperationContext fishingOperationContext : fishingOperationContexts) { FishingOperation fishingOperation = fishingOperationContext.getFishingOperation(); - String fishingOperationId = fishingOperation.getId(); if (fishingOperationContext.withBenthosBatches(true)) { - Collection<BenthosBatch> benthosBatches = fishingOperationContext.getBenthosBatches(true); + Collection<BenthosBatch> batches = fishingOperationContext.getBenthosBatches(true); if (log.isInfoEnabled()) { - log.info("Persist " + benthosBatches.size() + " VRAC benthos batch(es) of " + fishingOperationContext.getFishingOperationLabel() + " for cruise: " + cruiseDecorator.toString(fishingOperation.getCruise())); + log.info("Persist " + batches.size() + " VRAC benthos batch(es) of " + fishingOperationContext.getFishingOperationLabel() + " for cruise: " + cruiseDecorator.toString(fishingOperation.getCruise())); } - persistenceService.createBenthosBatches(fishingOperationId, benthosBatches); + persistBenthosBatches(fishingOperationContext, batches, null); } if (fishingOperationContext.withBenthosBatches(false)) { - Collection<BenthosBatch> benthosBatches = fishingOperationContext.getBenthosBatches(false); + Collection<BenthosBatch> batches = fishingOperationContext.getBenthosBatches(false); if (log.isInfoEnabled()) { - log.info("Persist " + benthosBatches.size() + " HORS VRAC benthos batch(es) of " + fishingOperationContext.getFishingOperationLabel() + " for cruise: " + cruiseDecorator.toString(fishingOperation.getCruise())); + log.info("Persist " + batches.size() + " HORS VRAC benthos batch(es) of " + fishingOperationContext.getFishingOperationLabel() + " for cruise: " + cruiseDecorator.toString(fishingOperation.getCruise())); } - persistenceService.createBenthosBatches(fishingOperationId, benthosBatches); + persistBenthosBatches(fishingOperationContext, batches, null); } - if (fishingOperationContext.withBenthosFrequencies()) { + } - ArrayListMultimap<BenthosBatch, BenthosBatchFrequency> allFrequencies = fishingOperationContext.getBenthosFrequencies(); - for (BenthosBatch benthosBatch : allFrequencies.keySet()) { + } - List<BenthosBatchFrequency> frequencies = allFrequencies.get(benthosBatch); + protected void persistSpeciesBatches(GenericFormatImportOperationContext fishingOperationContext, Collection<SpeciesBatch> batches, String parentId) { - String batchId = benthosBatch.getId(); - if (log.isInfoEnabled()) { - log.info("Persist " + frequencies.size() + " frequency(ies) (benthos batch: " + batchId + ") of " + fishingOperationContext.getFishingOperationLabel() + " for cruise: " + cruiseDecorator.toString(fishingOperation.getCruise())); - } - persistenceService.saveBenthosBatchFrequency(batchId, frequencies); + for (SpeciesBatch batch : batches) { + + List<SpeciesBatchFrequency> frequencies = fishingOperationContext.getSpeciesFrequencies(batch); + + // reset temporary id to persist batch + batch.setId((Integer) null); + SpeciesBatch createdBatch = persistenceService.createSpeciesBatch(batch, parentId); + + if (CollectionUtils.isNotEmpty(frequencies)) { + + String batchId = batch.getId(); + if (log.isInfoEnabled()) { + log.info("Persist " + frequencies.size() + " frequency(ies) (species batch: " + batchId + ") of " + fishingOperationContext.getFishingOperationLabel() + " for cruise: " + cruiseDecorator.toString(fishingOperationContext.getFishingOperation().getCruise())); + } + persistenceService.saveSpeciesBatchFrequency(batchId, frequencies); + + } + + if (!batch.isChildBatchsEmpty()) { + + persistSpeciesBatches(fishingOperationContext, batch.getChildBatchs(), createdBatch.getId()); + + } + + } + + } + protected void persistBenthosBatches(GenericFormatImportOperationContext fishingOperationContext, Collection<BenthosBatch> batches, String parentId) { + + for (BenthosBatch batch : batches) { + + List<BenthosBatchFrequency> frequencies = fishingOperationContext.getBenthosFrequencies(batch); + + // reset temporary id to persist batch + batch.setId((Integer) null); + + BenthosBatch createdBatch = persistenceService.createBenthosBatch(batch, parentId); + + if (CollectionUtils.isNotEmpty(frequencies)) { + + String batchId = batch.getId(); + if (log.isInfoEnabled()) { + log.info("Persist " + frequencies.size() + " frequency(ies) (benthos batch: " + batchId + ") of " + fishingOperationContext.getFishingOperationLabel() + " for cruise: " + cruiseDecorator.toString(fishingOperationContext.getFishingOperation().getCruise())); } + persistenceService.saveBenthosBatchFrequency(batchId, frequencies); } + if (!batch.isChildBatchsEmpty()) { + + persistBenthosBatches(fishingOperationContext, batch.getChildBatchs(), createdBatch.getId()); + + } } } diff --git a/tutti-service/src/main/java/fr/ifremer/tutti/service/genericformat/consumer/CsvConsumerForCatch.java b/tutti-service/src/main/java/fr/ifremer/tutti/service/genericformat/consumer/CsvConsumerForCatch.java index 34cfd0a..5efda23 100644 --- a/tutti-service/src/main/java/fr/ifremer/tutti/service/genericformat/consumer/CsvConsumerForCatch.java +++ b/tutti-service/src/main/java/fr/ifremer/tutti/service/genericformat/consumer/CsvConsumerForCatch.java @@ -43,6 +43,7 @@ public class CsvConsumerForCatch extends CsvComsumer<CatchRow, CatchModel> { private final Predicate<CatchRow> catchRowVracPredicate; public CsvConsumerForCatch(Path file, char separator, SampleCategoryModel sampleCategoryModel, GenericFormatImportEntityParserFactory parserFactory) { + super(file, CatchModel.forImport(separator, sampleCategoryModel, parserFactory)); this.catchRowVracPredicate = new Predicate<CatchRow>() { @@ -103,56 +104,65 @@ public class CsvConsumerForCatch extends CsvComsumer<CatchRow, CatchModel> { if (bean.isBenthos()) { - BenthosBatch batch = getOrCreateBenthosBatch(fishingOperationContext, bean); + prepareBenthosRowForPersist(fishingOperationContext, bean, withFrequency); - if (withFrequency) { + } else { - BenthosBatchFrequency frequency = BenthosBatchFrequencys.newBenthosBatchFrequency(); - frequency.setLengthStepCaracteristic(bean.getFrequencyLengthStepCaracteristic()); - frequency.setLengthStep(bean.getFrequencyLengthStep()); - frequency.setWeight(bean.getFrequencyWeight()); - frequency.setNumber(bean.getBatchNumber()); - if (log.isInfoEnabled()) { - log.info("Create benthos frequency " + frequency.getNumber() + " for batch: " + batch.getSpecies().getName() + " - " + batch.getSampleCategoryId() + " - " + batch.getSampleCategoryValue()); - } + prepareSpeciesRowForPersist(fishingOperationContext, bean, withFrequency); - fishingOperationContext.addBenthosFrequency(batch, frequency); + } - } else { + } - batch.setNumber(bean.getBatchNumber()); + protected void prepareSpeciesRowForPersist(GenericFormatImportOperationContext fishingOperationContext, CatchRow bean, boolean withFrequency) { - } + Species species = bean.getSpecies(); + boolean vrac = bean.isVrac(); - } else { + SpeciesBatch batch = fishingOperationContext.getSpeciesBatch(vrac, species.getReferenceTaxonId()); - SpeciesBatch batch = getOrCreateSpeciesBatch(fishingOperationContext, bean); + if (batch == null) { - if (withFrequency) { + // create root batch - SpeciesBatchFrequency frequency = SpeciesBatchFrequencys.newSpeciesBatchFrequency(); - frequency.setLengthStepCaracteristic(bean.getFrequencyLengthStepCaracteristic()); - frequency.setLengthStep(bean.getFrequencyLengthStep()); - frequency.setWeight(bean.getFrequencyWeight()); - frequency.setNumber(bean.getBatchNumber()); + batch = SpeciesBatchs.newSpeciesBatch(); + batch.setId(getNextBatchId()); + batch.setSpecies(species); + batch.setFishingOperation(fishingOperationContext.getFishingOperation()); - if (log.isInfoEnabled()) { - log.info("Create species frequency " + frequency.getNumber() + " for batch: " + batch.getSpecies().getName() + " - " + batch.getSampleCategoryId() + " - " + batch.getSampleCategoryValue()); - } + if (log.isInfoEnabled()) { + log.info("Create species root batch (" + (vrac ? "VRAC" : "HORS VRAC") + ") batch species: " + species.getName() + " for " + fishingOperationContext.getFishingOperationLabel()); + } + + fishingOperationContext.addSpeciesBatch(vrac, batch); + + } - fishingOperationContext.addSpeciesFrequency(batch, frequency); + batch = fillBatchCategories(batch, bean); - } else { + if (withFrequency) { - batch.setNumber(bean.getBatchNumber()); + SpeciesBatchFrequency frequency = SpeciesBatchFrequencys.newSpeciesBatchFrequency(); + frequency.setBatch(batch); + frequency.setLengthStepCaracteristic(bean.getFrequencyLengthStepCaracteristic()); + frequency.setLengthStep(bean.getFrequencyLengthStep()); + frequency.setWeight(bean.getFrequencyWeight()); + frequency.setNumber(bean.getBatchNumber()); + if (log.isInfoEnabled()) { + log.info("Create species frequency " + frequency.getNumber() + " for batch: " + batch.getSpecies().getName() + " - " + batch.getSampleCategoryId() + " - " + batch.getSampleCategoryValue()); } - } + fishingOperationContext.addSpeciesFrequency(batch, frequency); + + } else { + + batch.setNumber(bean.getBatchNumber()); + } } - protected BenthosBatch getOrCreateBenthosBatch(GenericFormatImportOperationContext fishingOperationContext, CatchRow bean) { + protected void prepareBenthosRowForPersist(GenericFormatImportOperationContext fishingOperationContext, CatchRow bean, boolean withFrequency) { Species species = bean.getSpecies(); boolean vrac = bean.isVrac(); @@ -164,8 +174,10 @@ public class CsvConsumerForCatch extends CsvComsumer<CatchRow, CatchModel> { // create root batch batch = BenthosBatchs.newBenthosBatch(); + batch.setId(getNextBatchId()); batch.setSpecies(species); batch.setFishingOperation(fishingOperationContext.getFishingOperation()); + if (log.isInfoEnabled()) { log.info("Create benthos root batch (" + (vrac ? "VRAC" : "HORS VRAC") + ") batch species: " + species.getName() + " for " + fishingOperationContext.getFishingOperationLabel()); } @@ -175,34 +187,27 @@ public class CsvConsumerForCatch extends CsvComsumer<CatchRow, CatchModel> { } batch = fillBatchCategories(batch, bean); - return batch; - - } - protected SpeciesBatch getOrCreateSpeciesBatch(GenericFormatImportOperationContext fishingOperationContext, CatchRow bean) { + if (withFrequency) { - Species species = bean.getSpecies(); - boolean vrac = bean.isVrac(); - - SpeciesBatch batch = fishingOperationContext.getSpeciesBatch(vrac, species.getReferenceTaxonId()); - - if (batch == null) { - - // create root batch + BenthosBatchFrequency frequency = BenthosBatchFrequencys.newBenthosBatchFrequency(); + frequency.setBatch(batch); + frequency.setLengthStepCaracteristic(bean.getFrequencyLengthStepCaracteristic()); + frequency.setLengthStep(bean.getFrequencyLengthStep()); + frequency.setWeight(bean.getFrequencyWeight()); + frequency.setNumber(bean.getBatchNumber()); - batch = SpeciesBatchs.newSpeciesBatch(); - batch.setSpecies(species); - batch.setFishingOperation(fishingOperationContext.getFishingOperation()); if (log.isInfoEnabled()) { - log.info("Create species root batch (" + (vrac ? "VRAC" : "HORS VRAC") + ") batch species: " + species.getName() + " for " + fishingOperationContext.getFishingOperationLabel()); + log.info("Create benthos frequency " + frequency.getNumber() + " for batch: " + batch.getSpecies().getName() + " - " + batch.getSampleCategoryId() + " - " + batch.getSampleCategoryValue()); } - fishingOperationContext.addSpeciesBatch(vrac, batch); + fishingOperationContext.addBenthosFrequency(batch, frequency); - } + } else { - batch = fillBatchCategories(batch, bean); - return batch; + batch.setNumber(bean.getBatchNumber()); + + } } @@ -255,6 +260,7 @@ public class CsvConsumerForCatch extends CsvComsumer<CatchRow, CatchModel> { // add a child result = SpeciesAbleBatchs.createNewChild(batch); + result.setId(getNextBatchId()); result.setSampleCategoryId(sampleCategory.getCategoryId()); result.setSampleCategoryValue(sampleCategory.getCategoryValue()); result.setSampleCategoryWeight(sampleCategory.getCategoryWeight()); @@ -270,4 +276,10 @@ public class CsvConsumerForCatch extends CsvComsumer<CatchRow, CatchModel> { } + private int batchId; + + private int getNextBatchId() { + return batchId++; + } + } \ No newline at end of file -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@codelutin.com>.