This is an automated email from the git hooks/post-receive script. New commit to branch feature/merge_species_benthos in repository tutti. See https://gitlab.nuiton.org/codelutin/tutti.git commit 884591ea91b0c6b37b006d8035e6df58e4986026 Author: Tony CHEMIT <chemit@codelutin.com> Date: Thu Mar 17 08:46:23 2016 +0100 suppression de BenthosBatch, BenthosBatchFrequency et des contrat SpeciesBatchAble et SpeciesFrequencyBatchAble dans le module persistence --- .../tutti/persistence/TuttiPersistenceImpl.java | 27 +- .../persistence/TuttiPersistenceNoDbImpl.java | 25 +- .../entities/data/SpeciesAbleBatchs.java | 142 --- .../entities/data/SpeciesBatchFrequencys.java | 9 + .../persistence/entities/data/SpeciesBatchs.java | 73 ++ .../entities/referential/TaxonCache.java | 16 +- .../service/BenthosBatchPersistenceService.java | 39 +- .../BenthosBatchPersistenceServiceImpl.java | 658 +++++----- .../service/CatchBatchPersistenceServiceImpl.java | 2 +- .../MarineLitterBatchPersistenceServiceImpl.java | 24 +- .../SpeciesBatchPersistenceServiceImpl.java | 700 ++++++----- .../SpeciesBatchPersistenceServiceSupport.java | 933 ++++++++++++++ .../batch/ScientificCruiseCatchBatchValidator.java | 16 +- .../service/batch/TuttiCatchBatchValidator.java | 3 +- .../CaracteristicPersistenceService.java | 8 +- .../CaracteristicPersistenceServiceImpl.java | 14 +- .../service/util/BatchPersistenceHelper.java | 1281 ++++++++++---------- .../persistence/service/util/BatchTreeHelper.java | 713 ----------- .../service/util/tree/BatchTreeHelper.java | 330 +++++ .../service/util/tree/BatchTreeHelperSupport.java | 365 ++++++ .../service/util/tree/BenthosBatchTreeHelper.java | 43 + .../service/util/tree/SpeciesBatchTreeHelper.java | 43 + .../util/tree/SpeciesBatchTreeHelperSupport.java | 424 +++++++ .../src/main/xmi/tutti-persistence.properties | 1 - .../src/main/xmi/tutti-persistence.zargo | Bin 65155 -> 59307 bytes .../BenthosBatchPersistenceServiceReadTest.java | 11 +- .../BenthosBatchPersistenceServiceWriteTest.java | 94 +- 27 files changed, 3669 insertions(+), 2325 deletions(-) diff --git a/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/TuttiPersistenceImpl.java b/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/TuttiPersistenceImpl.java index 4a06981..6d9d10b 100644 --- a/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/TuttiPersistenceImpl.java +++ b/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/TuttiPersistenceImpl.java @@ -32,8 +32,6 @@ import fr.ifremer.tutti.persistence.entities.CaracteristicMap; import fr.ifremer.tutti.persistence.entities.data.AccidentalBatch; import fr.ifremer.tutti.persistence.entities.data.Attachment; import fr.ifremer.tutti.persistence.entities.data.BatchContainer; -import fr.ifremer.tutti.persistence.entities.data.BenthosBatch; -import fr.ifremer.tutti.persistence.entities.data.BenthosBatchFrequency; import fr.ifremer.tutti.persistence.entities.data.CatchBatch; import fr.ifremer.tutti.persistence.entities.data.Cruise; import fr.ifremer.tutti.persistence.entities.data.Cruises; @@ -42,7 +40,6 @@ import fr.ifremer.tutti.persistence.entities.data.FishingOperations; import fr.ifremer.tutti.persistence.entities.data.IndividualObservationBatch; import fr.ifremer.tutti.persistence.entities.data.MarineLitterBatch; import fr.ifremer.tutti.persistence.entities.data.Program; -import fr.ifremer.tutti.persistence.entities.data.SpeciesAbleBatch; import fr.ifremer.tutti.persistence.entities.data.SpeciesBatch; import fr.ifremer.tutti.persistence.entities.data.SpeciesBatchFrequency; import fr.ifremer.tutti.persistence.entities.protocol.SpeciesProtocol; @@ -461,17 +458,17 @@ public class TuttiPersistenceImpl implements TuttiPersistence { } @Override - public boolean isVracBatch(SpeciesAbleBatch speciesBatch) { + public boolean isVracBatch(SpeciesBatch speciesBatch) { return getCaracteristicService().isVracBatch(speciesBatch); } @Override - public Predicate<SpeciesAbleBatch> getVracBatchPredicate() { + public Predicate<SpeciesBatch> getVracBatchPredicate() { return getCaracteristicService().getVracBatchPredicate(); } @Override - public boolean isHorsVracBatch(SpeciesAbleBatch speciesBatch) { + public boolean isHorsVracBatch(SpeciesBatch speciesBatch) { return getCaracteristicService().isHorsVracBatch(speciesBatch); } @@ -1222,22 +1219,22 @@ public class TuttiPersistenceImpl implements TuttiPersistence { //------------------------------------------------------------------------// @Override - public BatchContainer<BenthosBatch> getRootBenthosBatch(Integer fishingOperationId, boolean validateTree) throws InvalidBatchModelException { + public BatchContainer<SpeciesBatch> getRootBenthosBatch(Integer fishingOperationId, boolean validateTree) throws InvalidBatchModelException { return getBenthosBatchService().getRootBenthosBatch(fishingOperationId, validateTree); } @Override - public BenthosBatch createBenthosBatch(BenthosBatch bean, Integer parentBatchId, boolean computeRankOrder) { + public SpeciesBatch createBenthosBatch(SpeciesBatch bean, Integer parentBatchId, boolean computeRankOrder) { return getBenthosBatchService().createBenthosBatch(bean, parentBatchId, computeRankOrder); } @Override - public Collection<BenthosBatch> createBenthosBatches(Integer fishingOperationId, Collection<BenthosBatch> beans) { + public Collection<SpeciesBatch> createBenthosBatches(Integer fishingOperationId, Collection<SpeciesBatch> beans) { return getBenthosBatchService().createBenthosBatches(fishingOperationId, beans); } @Override - public BenthosBatch saveBenthosBatch(BenthosBatch bean) { + public SpeciesBatch saveBenthosBatch(SpeciesBatch bean) { return getBenthosBatchService().saveBenthosBatch(bean); } @@ -1257,23 +1254,23 @@ public class TuttiPersistenceImpl implements TuttiPersistence { } @Override - public List<BenthosBatch> getAllBenthosBatchToConfirm(Integer fishingOperationId) throws InvalidBatchModelException { + public List<SpeciesBatch> getAllBenthosBatchToConfirm(Integer fishingOperationId) throws InvalidBatchModelException { return getBenthosBatchService().getAllBenthosBatchToConfirm(fishingOperationId); } @Override - public List<BenthosBatchFrequency> getAllBenthosBatchFrequency(Integer benthosBatchId) { + public List<SpeciesBatchFrequency> getAllBenthosBatchFrequency(Integer benthosBatchId) { return getBenthosBatchService().getAllBenthosBatchFrequency(benthosBatchId); } @Override - public Multimap<Species, BenthosBatchFrequency> getAllBenthosBatchFrequencyForBatch(BatchContainer<BenthosBatch> batchContainer) { + public Multimap<Species, SpeciesBatchFrequency> getAllBenthosBatchFrequencyForBatch(BatchContainer<SpeciesBatch> batchContainer) { return getBenthosBatchService().getAllBenthosBatchFrequencyForBatch(batchContainer); } @Override - public List<BenthosBatchFrequency> saveBenthosBatchFrequency(Integer benthosBatchId, - List<BenthosBatchFrequency> frequencies) { + public List<SpeciesBatchFrequency> saveBenthosBatchFrequency(Integer benthosBatchId, + List<SpeciesBatchFrequency> frequencies) { return getBenthosBatchService().saveBenthosBatchFrequency(benthosBatchId, frequencies); } diff --git a/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/TuttiPersistenceNoDbImpl.java b/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/TuttiPersistenceNoDbImpl.java index b703b50..c851073 100644 --- a/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/TuttiPersistenceNoDbImpl.java +++ b/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/TuttiPersistenceNoDbImpl.java @@ -29,15 +29,12 @@ import fr.ifremer.tutti.persistence.entities.CaracteristicMap; import fr.ifremer.tutti.persistence.entities.data.AccidentalBatch; import fr.ifremer.tutti.persistence.entities.data.Attachment; import fr.ifremer.tutti.persistence.entities.data.BatchContainer; -import fr.ifremer.tutti.persistence.entities.data.BenthosBatch; -import fr.ifremer.tutti.persistence.entities.data.BenthosBatchFrequency; import fr.ifremer.tutti.persistence.entities.data.CatchBatch; import fr.ifremer.tutti.persistence.entities.data.Cruise; 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.Program; -import fr.ifremer.tutti.persistence.entities.data.SpeciesAbleBatch; import fr.ifremer.tutti.persistence.entities.data.SpeciesBatch; import fr.ifremer.tutti.persistence.entities.data.SpeciesBatchFrequency; import fr.ifremer.tutti.persistence.entities.protocol.TuttiProtocol; @@ -335,17 +332,17 @@ public class TuttiPersistenceNoDbImpl implements TuttiPersistence { } @Override - public boolean isVracBatch(SpeciesAbleBatch speciesBatch) { + public boolean isVracBatch(SpeciesBatch speciesBatch) { throw notImplemented(); } @Override - public boolean isHorsVracBatch(SpeciesAbleBatch speciesBatch) { + public boolean isHorsVracBatch(SpeciesBatch speciesBatch) { throw notImplemented(); } @Override - public Predicate<SpeciesAbleBatch> getVracBatchPredicate() { + public Predicate<SpeciesBatch> getVracBatchPredicate() { throw notImplemented(); } @@ -746,22 +743,22 @@ public class TuttiPersistenceNoDbImpl implements TuttiPersistence { //------------------------------------------------------------------------// @Override - public BatchContainer<BenthosBatch> getRootBenthosBatch(Integer fishingOperationId, boolean validateTree) throws InvalidBatchModelException { + public BatchContainer<SpeciesBatch> getRootBenthosBatch(Integer fishingOperationId, boolean validateTree) throws InvalidBatchModelException { throw notImplemented(); } @Override - public BenthosBatch createBenthosBatch(BenthosBatch bean, Integer parentBatchId, boolean computeRankOrder) { + public SpeciesBatch createBenthosBatch(SpeciesBatch bean, Integer parentBatchId, boolean computeRankOrder) { throw notImplemented(); } @Override - public Collection<BenthosBatch> createBenthosBatches(Integer fishingOperationId, Collection<BenthosBatch> beans) { + public Collection<SpeciesBatch> createBenthosBatches(Integer fishingOperationId, Collection<SpeciesBatch> beans) { throw notImplemented(); } @Override - public BenthosBatch saveBenthosBatch(BenthosBatch bean) { + public SpeciesBatch saveBenthosBatch(SpeciesBatch bean) { throw notImplemented(); } @@ -781,22 +778,22 @@ public class TuttiPersistenceNoDbImpl implements TuttiPersistence { } @Override - public List<BenthosBatch> getAllBenthosBatchToConfirm(Integer fishingOperationId) throws InvalidBatchModelException { + public List<SpeciesBatch> getAllBenthosBatchToConfirm(Integer fishingOperationId) throws InvalidBatchModelException { throw notImplemented(); } @Override - public List<BenthosBatchFrequency> getAllBenthosBatchFrequency(Integer benthosBatchId) { + public List<SpeciesBatchFrequency> getAllBenthosBatchFrequency(Integer benthosBatchId) { throw notImplemented(); } @Override - public Multimap<Species, BenthosBatchFrequency> getAllBenthosBatchFrequencyForBatch(BatchContainer<BenthosBatch> batchContainer) { + public Multimap<Species, SpeciesBatchFrequency> getAllBenthosBatchFrequencyForBatch(BatchContainer<SpeciesBatch> batchContainer) { throw notImplemented(); } @Override - public List<BenthosBatchFrequency> saveBenthosBatchFrequency(Integer benthosBatchId, List<BenthosBatchFrequency> frequencies) { + public List<SpeciesBatchFrequency> saveBenthosBatchFrequency(Integer benthosBatchId, List<SpeciesBatchFrequency> frequencies) { throw notImplemented(); } diff --git a/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/entities/data/SpeciesAbleBatchs.java b/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/entities/data/SpeciesAbleBatchs.java deleted file mode 100644 index 2e3ca55..0000000 --- a/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/entities/data/SpeciesAbleBatchs.java +++ /dev/null @@ -1,142 +0,0 @@ -package fr.ifremer.tutti.persistence.entities.data; - -/* - * #%L - * Tutti :: Persistence - * $Id:$ - * $HeadURL:$ - * %% - * Copyright (C) 2012 - 2014 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.Predicate; -import fr.ifremer.tutti.persistence.entities.referential.CaracteristicQualitativeValue; -import fr.ifremer.tutti.persistence.entities.referential.Species; - -import java.util.Collection; -import java.util.List; -import java.util.Set; - -/** - * Created on 8/26/14. - * - * @author Tony Chemit - chemit@codelutin.com - * @since 3.7 - */ -public class SpeciesAbleBatchs { - - - public static Predicate<SpeciesAbleBatch> newSpeciesAbleBatchCategoryPredicate(Integer cateogryId, Integer value) { - return new SpeciesAbleBatchCategoryPredicate(cateogryId, value); - } - - public static <B extends SpeciesAbleBatch> B newInstance(B parent) { - - return (B) (parent instanceof SpeciesBatch ? SpeciesBatchs.newSpeciesBatch() : BenthosBatchs.newBenthosBatch()); - - } - - public static <B extends SpeciesAbleBatch> B createNewChild(B parent) { - - B child = newInstance(parent); - child.setFishingOperation(parent.getFishingOperation()); - child.setSpecies(parent.getSpecies()); - SpeciesAbleBatchs.setParentBatch(parent, child); - SpeciesAbleBatchs.addChildBatch(parent, child); - return child; - - } - - public static <B extends SpeciesAbleBatch> void setChildBatchs(B parent, List<B> childs) { - - if (parent instanceof SpeciesBatch) { - ((SpeciesBatch) parent).setChildBatchs((List<SpeciesBatch>) childs); - } else { - ((BenthosBatch) parent).setChildBatchs((List<BenthosBatch>) childs); - } - - } - - public static <B extends SpeciesAbleBatch> List<B> getChildBatchs(B batch) { - return (List<B>) batch.getChildBatchs(); - } - - - public static <B extends SpeciesAbleBatch> void setParentBatch(B parent, B child) { - - if (parent instanceof SpeciesBatch) { - ((SpeciesBatch) child).setParentBatch((SpeciesBatch) parent); - } else { - ((BenthosBatch) child).setParentBatch((BenthosBatch) parent); - } - - } - - public static <B extends SpeciesAbleBatch> void addChildBatch(B parent, B child) { - - if (child instanceof SpeciesBatch) { - ((SpeciesBatch) parent).addChildBatchs((SpeciesBatch) child); - } else { - ((BenthosBatch) parent).addChildBatchs((BenthosBatch) child); - } - - } - - public static <B extends SpeciesAbleBatch> void grabSpeciesChildBatchs(Collection<B> childs, Set<Species> speciesSet) { - - for (B child : childs) { - speciesSet.add(child.getSpecies()); - } - - } - - public static <B extends SpeciesAbleBatch> void grabSampleCategorieValuesChildBatchs(B batch, Set<Integer> categoryIds) { - - CaracteristicQualitativeValue sampleCategoryValue = (CaracteristicQualitativeValue) batch.getSampleCategoryValue(); - - categoryIds.add(sampleCategoryValue.getIdAsInt()); - if (!batch.isChildBatchsEmpty()) { - for (SpeciesAbleBatch child : batch.getChildBatchs()) { - grabSampleCategorieValuesChildBatchs(child, categoryIds); - } - } - - - } - - - public static class SpeciesAbleBatchCategoryPredicate implements Predicate<SpeciesAbleBatch> { - - private final Integer id; - - private final Integer qualitativeValue; - - public SpeciesAbleBatchCategoryPredicate(Integer id, Integer qualitativeValue) { - this.id = id; - this.qualitativeValue = qualitativeValue; - } - - @Override - public boolean apply(SpeciesAbleBatch input) { - return id.equals(input.getSampleCategoryId()) && - input.getSampleCategoryValue() instanceof CaracteristicQualitativeValue && - qualitativeValue.equals(((CaracteristicQualitativeValue) input.getSampleCategoryValue()).getIdAsInt()); - } - - } -} diff --git a/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/entities/data/SpeciesBatchFrequencys.java b/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/entities/data/SpeciesBatchFrequencys.java new file mode 100644 index 0000000..34ab58a --- /dev/null +++ b/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/entities/data/SpeciesBatchFrequencys.java @@ -0,0 +1,9 @@ +package fr.ifremer.tutti.persistence.entities.data; + +public class SpeciesBatchFrequencys extends AbstractSpeciesBatchFrequencys { + public static SpeciesBatchFrequency newBenthosBatchFrequency() { + SpeciesBatchFrequency speciesBatchFrequency = newSpeciesBatchFrequency(); + speciesBatchFrequency.setBenthosFrequencyBatch(true); + return speciesBatchFrequency; + } +} diff --git a/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/entities/data/SpeciesBatchs.java b/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/entities/data/SpeciesBatchs.java index f051f0d..b6e8824 100644 --- a/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/entities/data/SpeciesBatchs.java +++ b/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/entities/data/SpeciesBatchs.java @@ -25,9 +25,14 @@ package fr.ifremer.tutti.persistence.entities.data; */ import com.google.common.base.Function; +import com.google.common.base.Predicate; +import fr.ifremer.tutti.persistence.entities.referential.CaracteristicQualitativeValue; +import fr.ifremer.tutti.persistence.entities.referential.Species; import java.io.Serializable; +import java.util.Collection; import java.util.Map; +import java.util.Set; import java.util.TreeMap; /** @@ -37,6 +42,12 @@ public class SpeciesBatchs extends AbstractSpeciesBatchs { public static final Function<SpeciesBatch, Serializable> GET_SAMPLE_CATEGORY_VALUE = SpeciesBatch::getSampleCategoryValue; + public static SpeciesBatch newInstance(SpeciesBatch parent) { + SpeciesBatch speciesBatch = newSpeciesBatch(); + speciesBatch.setBenthosBatch(parent.isBenthosBatch()); + return speciesBatch; + } + public static Map<Integer, SpeciesBatch> getAllSpeciesBatchesById(BatchContainer<SpeciesBatch> rootSpeciesBatch) { Map<Integer, SpeciesBatch> result = new TreeMap<>(); @@ -55,4 +66,66 @@ public class SpeciesBatchs extends AbstractSpeciesBatchs { } } + public static Predicate<SpeciesBatch> newSpeciesAbleBatchCategoryPredicate(Integer cateogryId, Integer value) { + return new SpeciesAbleBatchCategoryPredicate(cateogryId, value); + } + + public static SpeciesBatch createNewChild(SpeciesBatch parent) { + + SpeciesBatch child = newInstance(parent); + child.setFishingOperation(parent.getFishingOperation()); + child.setSpecies(parent.getSpecies()); + child.setParentBatch(parent); + parent.addChildBatchs(child); + return child; + + } + + public static SpeciesBatch newBenthosBatch() { + SpeciesBatch speciesBatch = newSpeciesBatch(); + speciesBatch.setBenthosBatch(true); + return speciesBatch; + } + + public static void grabSpeciesChildBatchs(Collection<SpeciesBatch> childs, Set<Species> speciesSet) { + + for (SpeciesBatch child : childs) { + speciesSet.add(child.getSpecies()); + } + + } + + public static void grabSampleCategorieValuesChildBatchs(SpeciesBatch batch, Set<Integer> categoryIds) { + + CaracteristicQualitativeValue sampleCategoryValue = (CaracteristicQualitativeValue) batch.getSampleCategoryValue(); + + categoryIds.add(sampleCategoryValue.getIdAsInt()); + if (!batch.isChildBatchsEmpty()) { + for (SpeciesBatch child : batch.getChildBatchs()) { + grabSampleCategorieValuesChildBatchs(child, categoryIds); + } + } + + + } + + public static class SpeciesAbleBatchCategoryPredicate implements Predicate<SpeciesBatch> { + + private final Integer id; + + private final Integer qualitativeValue; + + public SpeciesAbleBatchCategoryPredicate(Integer id, Integer qualitativeValue) { + this.id = id; + this.qualitativeValue = qualitativeValue; + } + + @Override + public boolean apply(SpeciesBatch input) { + return id.equals(input.getSampleCategoryId()) && + input.getSampleCategoryValue() instanceof CaracteristicQualitativeValue && + qualitativeValue.equals(((CaracteristicQualitativeValue) input.getSampleCategoryValue()).getIdAsInt()); + } + + } } diff --git a/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/entities/referential/TaxonCache.java b/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/entities/referential/TaxonCache.java index 042cf1b..e109c51 100644 --- a/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/entities/referential/TaxonCache.java +++ b/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/entities/referential/TaxonCache.java @@ -25,7 +25,7 @@ package fr.ifremer.tutti.persistence.entities.referential; */ import fr.ifremer.tutti.persistence.TuttiPersistence; -import fr.ifremer.tutti.persistence.entities.data.SpeciesAbleBatch; +import fr.ifremer.tutti.persistence.entities.data.SpeciesBatch; import fr.ifremer.tutti.persistence.entities.protocol.SpeciesProtocol; import java.util.List; @@ -33,11 +33,11 @@ import java.util.Map; import java.util.TreeMap; /** -* Created on 2/17/15. -* -* @author Tony Chemit - chemit@codelutin.com -* @since 3.14 -*/ + * Created on 2/17/15. + * + * @author Tony Chemit - chemit@codelutin.com + * @since 3.14 + */ public class TaxonCache { protected final boolean loadVernacularCode; @@ -77,9 +77,9 @@ public class TaxonCache { } - public <A extends SpeciesAbleBatch> void loadInBatches(List<A> speciesAbleBatches) { + public void loadInBatches(List<SpeciesBatch> speciesAbleBatches) { - for (A speciesAbleBatch : speciesAbleBatches) { + for (SpeciesBatch speciesAbleBatch : speciesAbleBatches) { load(speciesAbleBatch.getSpecies()); } diff --git a/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/BenthosBatchPersistenceService.java b/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/BenthosBatchPersistenceService.java index 617e7b5..93003b1 100644 --- a/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/BenthosBatchPersistenceService.java +++ b/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/BenthosBatchPersistenceService.java @@ -26,8 +26,8 @@ import com.google.common.collect.Multimap; import fr.ifremer.tutti.persistence.InvalidBatchModelException; import fr.ifremer.tutti.persistence.TuttiPersistenceServiceImplementor; 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.SpeciesBatch; +import fr.ifremer.tutti.persistence.entities.data.SpeciesBatchFrequency; import fr.ifremer.tutti.persistence.entities.referential.Species; import org.springframework.transaction.annotation.Transactional; @@ -35,7 +35,9 @@ import java.util.Collection; import java.util.List; /** - * Service to persist {@link BenthosBatch}. + * Service to persist {@link SpeciesBatch}. + * + * FIXME Merge it with speciesBatch. * * @author Tony Chemit - chemit@codelutin.com * @since 1.2 @@ -48,26 +50,26 @@ public interface BenthosBatchPersistenceService extends TuttiPersistenceServiceI //------------------------------------------------------------------------// /** - * Get all root {@link BenthosBatch} for the given fishing operation. + * Get all root {@link SpeciesBatch} for the given fishing operation. * * <strong>Note:</strong> All childs of the batch should be loaded here. * * @param fishingOperationId if of the fishing operation to seek * @param validateTree flag to validate sample category model - * @return the list of root {@link BenthosBatch} + * @return the list of root {@link SpeciesBatch} * @throws InvalidBatchModelException if batch does not respect the sample category model * @since 1.0 */ - BatchContainer<BenthosBatch> getRootBenthosBatch(Integer fishingOperationId, boolean validateTree) throws InvalidBatchModelException; + BatchContainer<SpeciesBatch> getRootBenthosBatch(Integer fishingOperationId, boolean validateTree) throws InvalidBatchModelException; @Transactional(readOnly = false) - BenthosBatch createBenthosBatch(BenthosBatch bean, Integer parentBatchId, boolean computeRankOrder); + SpeciesBatch createBenthosBatch(SpeciesBatch bean, Integer parentBatchId, boolean computeRankOrder); @Transactional(readOnly = false) - Collection<BenthosBatch> createBenthosBatches(Integer fishingOperationId, Collection<BenthosBatch> beans); + Collection<SpeciesBatch> createBenthosBatches(Integer fishingOperationId, Collection<SpeciesBatch> beans); @Transactional(readOnly = false) - BenthosBatch saveBenthosBatch(BenthosBatch bean); + SpeciesBatch saveBenthosBatch(SpeciesBatch bean); @Transactional(readOnly = false) void deleteBenthosBatch(Integer id); @@ -78,7 +80,8 @@ public interface BenthosBatchPersistenceService extends TuttiPersistenceServiceI /** * Change the species in the given {@code batchId} and in all his * sub batches. - * @param batchId id of the root Benthos batch to treat + * + * @param batchId id of the root Benthos batch to treat * @param species species to affect to all batches */ @Transactional(readOnly = false) @@ -88,11 +91,11 @@ public interface BenthosBatchPersistenceService extends TuttiPersistenceServiceI * Get all the batches to confirm * * @param fishingOperationId if of the fishing operation to seek - * @return the list of {@link fr.ifremer.tutti.persistence.entities.data.BenthosBatch} to confirm + * @return the list of {@link fr.ifremer.tutti.persistence.entities.data.SpeciesBatch} to confirm * @throws InvalidBatchModelException if batch does not respect the sample category model * @since 3.13 */ - List<BenthosBatch> getAllBenthosBatchToConfirm(Integer fishingOperationId) throws InvalidBatchModelException; + List<SpeciesBatch> getAllBenthosBatchToConfirm(Integer fishingOperationId) throws InvalidBatchModelException; //------------------------------------------------------------------------// //-- BenthosBatchFrequency methods --// @@ -105,7 +108,7 @@ public interface BenthosBatchPersistenceService extends TuttiPersistenceServiceI * @return the list of frequencies for the given benthos batch id * @since 1.0 */ - List<BenthosBatchFrequency> getAllBenthosBatchFrequency(Integer benthosBatchId); + List<SpeciesBatchFrequency> getAllBenthosBatchFrequency(Integer benthosBatchId); /** * Get all frequencies for the given root benthos batch container. @@ -114,21 +117,21 @@ public interface BenthosBatchPersistenceService extends TuttiPersistenceServiceI * @return the list of benthos frequencies indexed by their species * @since 3.3 */ - Multimap<Species, BenthosBatchFrequency> getAllBenthosBatchFrequencyForBatch(BatchContainer<BenthosBatch> batchContainer); + Multimap<Species, SpeciesBatchFrequency> getAllBenthosBatchFrequencyForBatch(BatchContainer<SpeciesBatch> batchContainer); /** - * Save all given {@link BenthosBatchFrequency} into the given + * Save all given {@link SpeciesBatchFrequency} into the given * {@code benthosBatchId}. If some are not existing then creates them. * * <strong>Note:</strong> This will as a side effect remove all previous * frequency for this benthos batch. * - * @param benthosBatchId id of the {@link BenthosBatch} to use + * @param benthosBatchId id of the {@link SpeciesBatch} to use * @param frequencies list of frequencies to create or update * @return the persisted list of frequencies * @since 1.0 */ @Transactional(readOnly = false) - List<BenthosBatchFrequency> saveBenthosBatchFrequency(Integer benthosBatchId, - List<BenthosBatchFrequency> frequencies); + List<SpeciesBatchFrequency> saveBenthosBatchFrequency(Integer benthosBatchId, + List<SpeciesBatchFrequency> frequencies); } diff --git a/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/BenthosBatchPersistenceServiceImpl.java b/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/BenthosBatchPersistenceServiceImpl.java index d497be6..0f1c1ce 100644 --- a/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/BenthosBatchPersistenceServiceImpl.java +++ b/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/BenthosBatchPersistenceServiceImpl.java @@ -22,45 +22,26 @@ package fr.ifremer.tutti.persistence.service; * #L% */ -import com.google.common.base.Preconditions; -import com.google.common.collect.ArrayListMultimap; -import com.google.common.collect.Lists; import com.google.common.collect.Multimap; -import fr.ifremer.adagio.core.dao.data.batch.Batch; -import fr.ifremer.adagio.core.dao.data.batch.CatchBatch; -import fr.ifremer.adagio.core.dao.data.batch.SortingBatch; -import fr.ifremer.adagio.core.dao.referential.taxon.ReferenceTaxon; import fr.ifremer.tutti.persistence.InvalidBatchModelException; -import fr.ifremer.tutti.persistence.entities.TuttiEntities; 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.BenthosBatchFrequencys; -import fr.ifremer.tutti.persistence.entities.data.BenthosBatchs; -import fr.ifremer.tutti.persistence.entities.data.FishingOperation; import fr.ifremer.tutti.persistence.entities.data.SampleCategoryModel; +import fr.ifremer.tutti.persistence.entities.data.SpeciesBatch; +import fr.ifremer.tutti.persistence.entities.data.SpeciesBatchFrequency; +import fr.ifremer.tutti.persistence.entities.data.SpeciesBatchFrequencys; +import fr.ifremer.tutti.persistence.entities.data.SpeciesBatchs; import fr.ifremer.tutti.persistence.entities.referential.Species; -import fr.ifremer.tutti.persistence.service.referential.SpeciesPersistenceService; import fr.ifremer.tutti.persistence.service.util.BatchPersistenceHelper; -import fr.ifremer.tutti.persistence.service.util.BatchTreeHelper; -import fr.ifremer.tutti.persistence.service.util.MeasurementPersistenceHelper; -import fr.ifremer.tutti.persistence.service.util.SynchronizationStatusHelper; -import org.apache.commons.collections4.CollectionUtils; +import fr.ifremer.tutti.persistence.service.util.tree.BenthosBatchTreeHelper; +import fr.ifremer.tutti.persistence.service.util.tree.SpeciesBatchTreeHelperSupport; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.springframework.dao.DataIntegrityViolationException; import org.springframework.stereotype.Service; import javax.annotation.Resource; -import java.text.DateFormat; -import java.text.SimpleDateFormat; -import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; import java.util.List; -import static org.nuiton.i18n.I18n.t; - /** * Default implementation of {@link BenthosBatchPersistenceService}. * @@ -68,370 +49,385 @@ import static org.nuiton.i18n.I18n.t; * @since 1.2 */ @Service("benthosBatchPersistenceService") -public class BenthosBatchPersistenceServiceImpl extends AbstractPersistenceService +public class BenthosBatchPersistenceServiceImpl extends SpeciesBatchPersistenceServiceSupport implements BenthosBatchPersistenceService { /** Logger. */ - private static final Log log = - LogFactory.getLog(BenthosBatchPersistenceServiceImpl.class); - - @Resource(name = "speciesPersistenceService") - private SpeciesPersistenceService speciesService; - - @Resource(name = "fishingOperationPersistenceService") - protected FishingOperationPersistenceService fishingOperationPersistenceService; - - @Resource(name = "batchPersistenceHelper") - protected BatchPersistenceHelper batchHelper; + private static final Log log = LogFactory.getLog(BenthosBatchPersistenceServiceImpl.class); - @Resource(name = "batchTreeHelper") - protected BatchTreeHelper batchTreeHelper; + @Resource(name = "benthosBatchTreeHelper") + protected BenthosBatchTreeHelper batchTreeHelper; - @Resource(name = "measurementPersistenceHelper") - protected MeasurementPersistenceHelper measurementPersistenceHelper; - - @Resource(name = "synchronizationStatusHelper") - protected SynchronizationStatusHelper synchronizationStatusHelper; + public BenthosBatchPersistenceServiceImpl() { + super("Benthos", SpeciesBatchs::newBenthosBatch, SpeciesBatchFrequencys::newBenthosBatchFrequency); + } //------------------------------------------------------------------------// //-- Benthos Batch methods --// //------------------------------------------------------------------------// @Override - public BatchContainer<BenthosBatch> getRootBenthosBatch(Integer fishingOperationId, - boolean validateTree) { - Preconditions.checkNotNull(fishingOperationId); - - DateFormat df = new SimpleDateFormat("dd/MM/yyy"); - - CatchBatch catchBatch = batchHelper.getRootCatchBatchByFishingOperationId(fishingOperationId, false); - - // -- Vrac > Benthos > Alive Itemized - SortingBatch vracBenthosBatch = batchTreeHelper.getBenthosVracAliveItemizedRootBatch(catchBatch); - - SampleCategoryModel sampleCategoryModel = getSampleCategoryModel(); - - BatchContainer<BenthosBatch> result = new BatchContainer<>(); - if (vracBenthosBatch != null) { - result.setId(vracBenthosBatch.getId()); - - for (Batch batch : vracBenthosBatch.getChildBatchs()) { - SortingBatch source = (SortingBatch) batch; - ReferenceTaxon referenceTaxon = source.getReferenceTaxon(); - Preconditions.checkNotNull(referenceTaxon, "Can't have a rootBenthosBatch with a null taxon, but was for " + batch.getId()); - Species species = speciesService.getSpeciesByReferenceTaxonId(referenceTaxon.getId()); - if (species == null) { - FishingOperation fishingOperation = fishingOperationPersistenceService.getFishingOperation(fishingOperationId); - String fishingOperationName = fishingOperation.getStationNumber() + " - " + fishingOperation.getFishingOperationNumber() + " - " + df.format(fishingOperation.getGearShootingStartDate()); - throw new InvalidBatchModelException(t("tutti.persistence.benthosBatch.validation.unkonwn.taxon", fishingOperationName, source.getId(), referenceTaxon.getId())); - } - BenthosBatch target = BenthosBatchs.newBenthosBatch(); - target.setSpecies(species); - - batchHelper.entityToBean(sampleCategoryModel, source, target); - result.addChildren(target); - if (log.isDebugEnabled()) { - log.debug("Loaded CatchBatch Vrac > Benthos > Alive Itemized > " + target.getSpecies().getReferenceTaxonId() + ": " + target.getId()); - } - } - } - - // -- Hors Vrac > Benthos - SortingBatch horsVracBenthosBatch = batchTreeHelper.getBenthosHorsVracRootBatch(catchBatch); - - if (horsVracBenthosBatch != null) { - for (Batch batch : horsVracBenthosBatch.getChildBatchs()) { - SortingBatch source = (SortingBatch) batch; - ReferenceTaxon referenceTaxon = source.getReferenceTaxon(); - Preconditions.checkNotNull(referenceTaxon, "Can't have a rootSpeciesBatch with a null taxon, but was for " + source.getId()); - Species species = speciesService.getSpeciesByReferenceTaxonId(referenceTaxon.getId()); - if (species == null) { - FishingOperation fishingOperation = fishingOperationPersistenceService.getFishingOperation(fishingOperationId); - String fishingOperationName = fishingOperation.getStationNumber() + " - " + fishingOperation.getFishingOperationNumber() + " - " + df.format(fishingOperation.getGearShootingStartDate()); - throw new InvalidBatchModelException(t("tutti.persistence.benthosBatch.validation.unkonwn.taxon", fishingOperationName, source.getId(), referenceTaxon.getId())); - } - - BenthosBatch target = BenthosBatchs.newBenthosBatch(); - target.setSpecies(species); - - batchHelper.entityToBean(sampleCategoryModel, source, target); - result.addChildren(target); - if (log.isDebugEnabled()) { - log.debug("Loaded CatchBatch Hors Vrac > Benthos > " + target.getSpecies().getReferenceTaxonId() + ": " + target.getId()); - } - } - } - - if (validateTree) { - - // validate with given sample category model - batchHelper.validateBenthos(sampleCategoryModel, result); - } - - return result; + protected SpeciesBatchTreeHelperSupport getBatchTreeHelper() { + return batchTreeHelper; } @Override - public BenthosBatch createBenthosBatch(BenthosBatch bean, Integer parentBatchId, boolean computeRankOrder) { - Preconditions.checkNotNull(bean); - Preconditions.checkArgument(TuttiEntities.isNew(bean)); - Preconditions.checkNotNull(bean.getSpecies()); - Preconditions.checkNotNull(bean.getSpecies().getId()); - Preconditions.checkNotNull(bean.getFishingOperation()); - Preconditions.checkNotNull(bean.getFishingOperation().getId()); - - CatchBatch catchBatch = batchHelper.getRootCatchBatchByFishingOperationId(bean.getFishingOperation().getIdAsInt(), false); - - return createBenthosBatch(bean, parentBatchId, catchBatch, computeRankOrder); + protected void validate(BatchPersistenceHelper batchHelper, SampleCategoryModel sampleCategoryModel, BatchContainer<SpeciesBatch> result) { + batchHelper.validateBenthos(sampleCategoryModel, result); } @Override - public Collection<BenthosBatch> createBenthosBatches(Integer fishingOperationId, Collection<BenthosBatch> beans) { - - Preconditions.checkNotNull(beans); - Preconditions.checkNotNull(fishingOperationId); - - CatchBatch catchBatch = batchHelper.getRootCatchBatchByFishingOperationId(fishingOperationId, false); - - Collection<BenthosBatch> result = new ArrayList<>(); - for (BenthosBatch bean : beans) { - - BenthosBatch created = createBenthosBatch(bean, null, catchBatch, true); - result.add(created); - - } - return result; - + public BatchContainer<SpeciesBatch> getRootBenthosBatch(Integer fishingOperationId, boolean validateTree) { + + return getRootSpeciesBatch0(fishingOperationId, validateTree); + +// Preconditions.checkNotNull(fishingOperationId); +// +// DateFormat df = new SimpleDateFormat("dd/MM/yyy"); +// +// CatchBatch catchBatch = batchHelper.getRootCatchBatchByFishingOperationId(fishingOperationId, false); +// +// // -- Vrac > Benthos > Alive Itemized +// SortingBatch vracBenthosBatch = batchTreeHelper.getBenthosVracAliveItemizedRootBatch(catchBatch); +// +// SampleCategoryModel sampleCategoryModel = getSampleCategoryModel(); +// +// BatchContainer<SpeciesBatch> result = new BatchContainer<>(); +// if (vracBenthosBatch != null) { +// result.setId(vracBenthosBatch.getId()); +// +// for (Batch batch : vracBenthosBatch.getChildBatchs()) { +// SortingBatch source = (SortingBatch) batch; +// ReferenceTaxon referenceTaxon = source.getReferenceTaxon(); +// Preconditions.checkNotNull(referenceTaxon, "Can't have a rootBenthosBatch with a null taxon, but was for " + batch.getId()); +// Species species = speciesService.getSpeciesByReferenceTaxonId(referenceTaxon.getId()); +// if (species == null) { +// FishingOperation fishingOperation = fishingOperationPersistenceService.getFishingOperation(fishingOperationId); +// String fishingOperationName = fishingOperation.getStationNumber() + " - " + fishingOperation.getFishingOperationNumber() + " - " + df.format(fishingOperation.getGearShootingStartDate()); +// throw new InvalidBatchModelException(t("tutti.persistence.benthosBatch.validation.unkonwn.taxon", fishingOperationName, source.getId(), referenceTaxon.getId())); +// } +// SpeciesBatch target = SpeciesBatchs.newBenthosBatch(); +// target.setSpecies(species); +// +// batchHelper.entityToBean(sampleCategoryModel, source, target); +// result.addChildren(target); +// if (log.isDebugEnabled()) { +// log.debug("Loaded CatchBatch Vrac > Benthos > Alive Itemized > " + target.getSpecies().getReferenceTaxonId() + ": " + target.getId()); +// } +// } +// } +// +// // -- Hors Vrac > Benthos +// SortingBatch horsVracBenthosBatch = batchTreeHelper.getBenthosHorsVracRootBatch(catchBatch); +// +// if (horsVracBenthosBatch != null) { +// for (Batch batch : horsVracBenthosBatch.getChildBatchs()) { +// SortingBatch source = (SortingBatch) batch; +// ReferenceTaxon referenceTaxon = source.getReferenceTaxon(); +// Preconditions.checkNotNull(referenceTaxon, "Can't have a rootSpeciesBatch with a null taxon, but was for " + source.getId()); +// Species species = speciesService.getSpeciesByReferenceTaxonId(referenceTaxon.getId()); +// if (species == null) { +// FishingOperation fishingOperation = fishingOperationPersistenceService.getFishingOperation(fishingOperationId); +// String fishingOperationName = fishingOperation.getStationNumber() + " - " + fishingOperation.getFishingOperationNumber() + " - " + df.format(fishingOperation.getGearShootingStartDate()); +// throw new InvalidBatchModelException(t("tutti.persistence.benthosBatch.validation.unkonwn.taxon", fishingOperationName, source.getId(), referenceTaxon.getId())); +// } +// +// SpeciesBatch target = SpeciesBatchs.newBenthosBatch(); +// target.setSpecies(species); +// +// batchHelper.entityToBean(sampleCategoryModel, source, target); +// result.addChildren(target); +// if (log.isDebugEnabled()) { +// log.debug("Loaded CatchBatch Hors Vrac > Benthos > " + target.getSpecies().getReferenceTaxonId() + ": " + target.getId()); +// } +// } +// } +// +// if (validateTree) { +// +// // validate with given sample category model +// batchHelper.validateBenthos(sampleCategoryModel, result); +// } +// +// return result; } - protected BenthosBatch createBenthosBatch(BenthosBatch bean, Integer parentBatchId, CatchBatch catchBatch, boolean computeRankOrder) { - - Preconditions.checkNotNull(bean); - Preconditions.checkArgument(TuttiEntities.isNew(bean)); - Preconditions.checkNotNull(bean.getSpecies()); - Preconditions.checkNotNull(bean.getSpecies().getId()); - Preconditions.checkNotNull(bean.getFishingOperation()); - Preconditions.checkNotNull(bean.getFishingOperation().getId()); + @Override + public SpeciesBatch createBenthosBatch(SpeciesBatch bean, Integer parentBatchId, boolean computeRankOrder) { + + return createSpeciesBatch0(bean, parentBatchId, computeRankOrder); + +// Preconditions.checkNotNull(bean); +// Preconditions.checkArgument(TuttiEntities.isNew(bean)); +// Preconditions.checkNotNull(bean.getSpecies()); +// Preconditions.checkNotNull(bean.getSpecies().getId()); +// Preconditions.checkNotNull(bean.getFishingOperation()); +// Preconditions.checkNotNull(bean.getFishingOperation().getId()); +// +// CatchBatch catchBatch = batchHelper.getRootCatchBatchByFishingOperationId(bean.getFishingOperation().getIdAsInt(), false); +// +// return createBenthosBatch(bean, parentBatchId, catchBatch, computeRankOrder); + } - SortingBatch batch = SortingBatch.Factory.newInstance(); - beanToEntity(bean, batch, parentBatchId, catchBatch, computeRankOrder); - bean = batchHelper.createSortingBatch(bean, catchBatch, batch); + @Override + public Collection<SpeciesBatch> createBenthosBatches(Integer fishingOperationId, Collection<SpeciesBatch> beans) { + + return createSpeciesBatches0(fishingOperationId, beans); + +// Preconditions.checkNotNull(beans); +// Preconditions.checkNotNull(fishingOperationId); +// +// CatchBatch catchBatch = batchHelper.getRootCatchBatchByFishingOperationId(fishingOperationId, false); +// +// Collection<SpeciesBatch> result = new ArrayList<>(); +// for (SpeciesBatch bean : beans) { +// +// SpeciesBatch created = createBenthosBatch(bean, null, catchBatch, true); +// result.add(created); +// +// } +// return result; - return bean; } +// protected SpeciesBatch createBenthosBatch(SpeciesBatch bean, Integer parentBatchId, CatchBatch catchBatch, boolean computeRankOrder) { +// +// Preconditions.checkNotNull(bean); +// Preconditions.checkArgument(TuttiEntities.isNew(bean)); +// Preconditions.checkNotNull(bean.getSpecies()); +// Preconditions.checkNotNull(bean.getSpecies().getId()); +// Preconditions.checkNotNull(bean.getFishingOperation()); +// Preconditions.checkNotNull(bean.getFishingOperation().getId()); +// +// SortingBatch batch = SortingBatch.Factory.newInstance(); +// beanToEntity(bean, batch, parentBatchId, catchBatch, computeRankOrder); +// bean = batchHelper.createSortingBatch(bean, catchBatch, batch); +// +// return bean; +// } + @Override - public BenthosBatch saveBenthosBatch(BenthosBatch bean) { - Preconditions.checkNotNull(bean); - Preconditions.checkNotNull(bean.getId()); - - Integer batchId = bean.getIdAsInt(); - CatchBatch catchBatch = batchHelper.getRootCatchBatchByBatchId(batchId); - SortingBatch batch = batchHelper.getSortingBatchById(catchBatch, batchId); - - Integer parentBatchId = null; - if (bean.getParentBatch() != null) { - parentBatchId = bean.getParentBatch().getIdAsInt(); - } - beanToEntity(bean, batch, parentBatchId, catchBatch, true); - batchHelper.updateSortingBatch(batch, catchBatch); - - return bean; + public SpeciesBatch saveBenthosBatch(SpeciesBatch bean) { + + return saveSpeciesBatch0(bean); + +// Preconditions.checkNotNull(bean); +// Preconditions.checkNotNull(bean.getId()); +// +// Integer batchId = bean.getIdAsInt(); +// CatchBatch catchBatch = batchHelper.getRootCatchBatchByBatchId(batchId); +// SortingBatch batch = batchHelper.getSortingBatchById(catchBatch, batchId); +// +// Integer parentBatchId = null; +// if (bean.getParentBatch() != null) { +// parentBatchId = bean.getParentBatch().getIdAsInt(); +// } +// beanToEntity(bean, batch, parentBatchId, catchBatch, true); +// batchHelper.updateSortingBatch(batch, catchBatch); +// +// return bean; } @Override public void deleteBenthosBatch(Integer id) { - Preconditions.checkNotNull(id); - batchHelper.deleteBatch(id); + deleteSpeciesBatch0(id); +// Preconditions.checkNotNull(id); +// batchHelper.deleteBatch(id); } @Override public void deleteBenthosSubBatch(Integer id) { - Preconditions.checkNotNull(id); - batchHelper.deleteSpeciesSubBatch(id); + deleteSpeciesSubBatch0(id); +// Preconditions.checkNotNull(id); +// batchHelper.deleteSpeciesSubBatch(id); } @Override public void changeBenthosBatchSpecies(Integer id, Species species) { - Preconditions.checkNotNull(id); - Preconditions.checkNotNull(species); - Preconditions.checkNotNull(species.getReferenceTaxonId()); - batchHelper.changeBatchSpecies(id, species); + changeSpeciesBatchSpecies0(id, species); +// Preconditions.checkNotNull(id); +// Preconditions.checkNotNull(species); +// Preconditions.checkNotNull(species.getReferenceTaxonId()); +// batchHelper.changeBatchSpecies(id, species); } @Override - public List<BenthosBatch> getAllBenthosBatchToConfirm(Integer fishingOperationId) throws InvalidBatchModelException { - List<BenthosBatch> batchesToConfirm = new ArrayList<>(); - - BatchContainer<BenthosBatch> rootBenthosBatch = getRootBenthosBatch(fishingOperationId, false); - for (BenthosBatch benthosBatch : rootBenthosBatch.getChildren()) { - findBenthosBatchesToConfirm(benthosBatch, batchesToConfirm); - } - - return batchesToConfirm; + public List<SpeciesBatch> getAllBenthosBatchToConfirm(Integer fishingOperationId) throws InvalidBatchModelException { + return getAllSpeciesBatchToConfirm0(fishingOperationId); +// List<SpeciesBatch> batchesToConfirm = new ArrayList<>(); +// +// BatchContainer<SpeciesBatch> rootBenthosBatch = getRootBenthosBatch(fishingOperationId, false); +// for (SpeciesBatch benthosBatch : rootBenthosBatch.getChildren()) { +// findBenthosBatchesToConfirm(benthosBatch, batchesToConfirm); +// } +// +// return batchesToConfirm; } - protected void findBenthosBatchesToConfirm(BenthosBatch benthosBatch, List<BenthosBatch> batchesToConfirm) { - if (benthosBatch.isSpeciesToConfirm()) { - batchesToConfirm.add(benthosBatch); - - } else if (!benthosBatch.isChildBatchsEmpty()) { - for (BenthosBatch batch : benthosBatch.getChildBatchs()) { - findBenthosBatchesToConfirm(batch, batchesToConfirm); - } - } - } +// protected void findBenthosBatchesToConfirm(SpeciesBatch benthosBatch, List<SpeciesBatch> batchesToConfirm) { +// if (benthosBatch.isSpeciesToConfirm()) { +// batchesToConfirm.add(benthosBatch); +// +// } else if (!benthosBatch.isChildBatchsEmpty()) { +// for (SpeciesBatch batch : benthosBatch.getChildBatchs()) { +// findBenthosBatchesToConfirm(batch, batchesToConfirm); +// } +// } +// } //------------------------------------------------------------------------// - //-- BenthosBatchFrequency methods (for benthos) --// + //-- SpeciesBatchFrequency methods (for benthos) --// //------------------------------------------------------------------------// @Override - public List<BenthosBatchFrequency> getAllBenthosBatchFrequency(Integer benthosBatchId) { - Preconditions.checkNotNull(benthosBatchId); - - List<SortingBatch> frequencyChilds = batchHelper.getFrequencies( - benthosBatchId); - List<BenthosBatchFrequency> results = Lists.newArrayList(); - for (SortingBatch child : frequencyChilds) { - BenthosBatchFrequency target = BenthosBatchFrequencys.newBenthosBatchFrequency(); - - batchHelper.entityToBatchFrequency(child, target); - results.add(target); - } - return Collections.unmodifiableList(results); + public List<SpeciesBatchFrequency> getAllBenthosBatchFrequency(Integer benthosBatchId) { + return getAllSpeciesBatchFrequency0(benthosBatchId); +// Preconditions.checkNotNull(benthosBatchId); +// +// List<SortingBatch> frequencyChilds = batchHelper.getFrequencies( +// benthosBatchId); +// List<SpeciesBatchFrequency> results = Lists.newArrayList(); +// for (SortingBatch child : frequencyChilds) { +// SpeciesBatchFrequency target = SpeciesBatchFrequencys.newBenthosBatchFrequency(); +// +// batchHelper.entityToBatchFrequency(child, target); +// results.add(target); +// } +// return Collections.unmodifiableList(results); } @Override - public Multimap<Species, BenthosBatchFrequency> getAllBenthosBatchFrequencyForBatch(BatchContainer<BenthosBatch> batchContainer) { - Multimap<Species, BenthosBatchFrequency> result = ArrayListMultimap.create(); - for (BenthosBatch speciesBatch : batchContainer.getChildren()) { - getAllSpeciesBatchFrequencyForBatch(speciesBatch, result); - } - return result; + public Multimap<Species, SpeciesBatchFrequency> getAllBenthosBatchFrequencyForBatch(BatchContainer<SpeciesBatch> batchContainer) { + return getAllSpeciesBatchFrequencyForBatch0(batchContainer); +// Multimap<Species, SpeciesBatchFrequency> result = ArrayListMultimap.create(); +// for (SpeciesBatch speciesBatch : batchContainer.getChildren()) { +// getAllSpeciesBatchFrequencyForBatch(speciesBatch, result); +// } +// return result; } - protected void getAllSpeciesBatchFrequencyForBatch(BenthosBatch batch, - Multimap<Species, BenthosBatchFrequency> result) { - - List<BenthosBatchFrequency> speciesBatchFrequency = getAllBenthosBatchFrequency(batch.getIdAsInt()); - result.putAll(batch.getSpecies(), speciesBatchFrequency); - - if (!batch.isChildBatchsEmpty()) { - for (BenthosBatch child : batch.getChildBatchs()) { - getAllSpeciesBatchFrequencyForBatch(child, result); - } - } - } +// protected void getAllSpeciesBatchFrequencyForBatch(SpeciesBatch batch, +// Multimap<Species, SpeciesBatchFrequency> result) { +// +// List<SpeciesBatchFrequency> speciesBatchFrequency = getAllBenthosBatchFrequency(batch.getIdAsInt()); +// result.putAll(batch.getSpecies(), speciesBatchFrequency); +// +// if (!batch.isChildBatchsEmpty()) { +// for (SpeciesBatch child : batch.getChildBatchs()) { +// getAllSpeciesBatchFrequencyForBatch(child, result); +// } +// } +// } @Override - public List<BenthosBatchFrequency> saveBenthosBatchFrequency(Integer benthosBatchId, - List<BenthosBatchFrequency> frequencies) { - Preconditions.checkNotNull(benthosBatchId); - Preconditions.checkNotNull(frequencies); - - // Check that all frequencies have the same length PMFM (before doing any db call) - String pmfmId = null; - for (BenthosBatchFrequency source : frequencies) { - - if (pmfmId == null) { - pmfmId = source.getLengthStepCaracteristic().getId(); - } else if (!pmfmId.equals(source.getLengthStepCaracteristic().getId())) { - throw new DataIntegrityViolationException("Batch frequencies under one Speciesbatch must have all the same lengthStepCaracteristic"); - } - } - - CatchBatch catchBatch = batchHelper.getRootCatchBatchByBatchId(benthosBatchId); - - if (catchBatch == null) { - return frequencies; - } - - // Synchronization status - synchronizationStatusHelper.setDirty(catchBatch); - - // Retrieve parent - SortingBatch parentBatch = batchHelper.getSortingBatchById(catchBatch, benthosBatchId); - - // Remember child ids, to remove unchanged item (see at bottom in this method) - List<Integer> notUpdatedChildIds = Lists.newArrayList(); - List<SortingBatch> frequencyChilds = batchHelper.getFrequencyChilds( - parentBatch); - for (SortingBatch child : frequencyChilds) { - notUpdatedChildIds.add(child.getId()); - } - - short rankOrder = 0; - List<SortingBatch> batchsToUpdate = Lists.newArrayList(); - for (BenthosBatchFrequency source : frequencies) { - rankOrder++; - - // Not existing batch - SortingBatch target; - if (source.getId() == null) { - target = SortingBatch.Factory.newInstance(); - - // Fill the sorting batch from the source - batchHelper.beanToEntity(source, target, parentBatch, rankOrder); - - // Create the targeted batch, then update the source id - batchHelper.createSortingBatch(source, catchBatch, target); - } - - // Existing batch - else { - target = batchHelper.getSortingBatchById(catchBatch, source.getIdAsInt()); - - // Fill the sorting batch from the source - batchHelper.beanToEntity(source, target, parentBatch, rankOrder); - - // Add the batch into a list (will be update later, using this list) - batchsToUpdate.add(target); - - notUpdatedChildIds.remove(target.getId()); - } - } - - // If some batchs need to be update, do it - if (CollectionUtils.isNotEmpty(batchsToUpdate)) { - batchHelper.updateSortingBatch(batchsToUpdate, catchBatch); - } - - if (CollectionUtils.isNotEmpty(notUpdatedChildIds)) { - for (Integer batchId : notUpdatedChildIds) { - batchHelper.removeWithChildren(batchId, catchBatch); - } - } - - return Collections.unmodifiableList(frequencies); + public List<SpeciesBatchFrequency> saveBenthosBatchFrequency(Integer benthosBatchId, + List<SpeciesBatchFrequency> frequencies) { + return saveSpeciesBatchFrequency0(benthosBatchId, frequencies); +// Preconditions.checkNotNull(benthosBatchId); +// Preconditions.checkNotNull(frequencies); +// +// // Check that all frequencies have the same length PMFM (before doing any db call) +// String pmfmId = null; +// for (SpeciesBatchFrequency source : frequencies) { +// +// if (pmfmId == null) { +// pmfmId = source.getLengthStepCaracteristic().getId(); +// } else if (!pmfmId.equals(source.getLengthStepCaracteristic().getId())) { +// throw new DataIntegrityViolationException("Batch frequencies under one Speciesbatch must have all the same lengthStepCaracteristic"); +// } +// } +// +// CatchBatch catchBatch = batchHelper.getRootCatchBatchByBatchId(benthosBatchId); +// +// if (catchBatch == null) { +// return frequencies; +// } +// +// // Synchronization status +// synchronizationStatusHelper.setDirty(catchBatch); +// +// // Retrieve parent +// SortingBatch parentBatch = batchHelper.getSortingBatchById(catchBatch, benthosBatchId); +// +// // Remember child ids, to remove unchanged item (see at bottom in this method) +// List<Integer> notUpdatedChildIds = Lists.newArrayList(); +// List<SortingBatch> frequencyChilds = batchHelper.getFrequencyChilds( +// parentBatch); +// for (SortingBatch child : frequencyChilds) { +// notUpdatedChildIds.add(child.getId()); +// } +// +// short rankOrder = 0; +// List<SortingBatch> batchsToUpdate = Lists.newArrayList(); +// for (SpeciesBatchFrequency source : frequencies) { +// rankOrder++; +// +// // Not existing batch +// SortingBatch target; +// if (source.getId() == null) { +// target = SortingBatch.Factory.newInstance(); +// +// // Fill the sorting batch from the source +// batchHelper.beanToEntity(source, target, parentBatch, rankOrder); +// +// // Create the targeted batch, then update the source id +// batchHelper.createSortingBatch(source, catchBatch, target); +// } +// +// // Existing batch +// else { +// target = batchHelper.getSortingBatchById(catchBatch, source.getIdAsInt()); +// +// // Fill the sorting batch from the source +// batchHelper.beanToEntity(source, target, parentBatch, rankOrder); +// +// // Add the batch into a list (will be update later, using this list) +// batchsToUpdate.add(target); +// +// notUpdatedChildIds.remove(target.getId()); +// } +// } +// +// // If some batchs need to be update, do it +// if (CollectionUtils.isNotEmpty(batchsToUpdate)) { +// batchHelper.updateSortingBatch(batchsToUpdate, catchBatch); +// } +// +// if (CollectionUtils.isNotEmpty(notUpdatedChildIds)) { +// for (Integer batchId : notUpdatedChildIds) { +// batchHelper.removeWithChildren(batchId, catchBatch); +// } +// } +// +// return Collections.unmodifiableList(frequencies); } //------------------------------------------------------------------------// //-- Internal methods --// //------------------------------------------------------------------------// - protected void beanToEntity(BenthosBatch source, - SortingBatch target, - Integer parentBatchId, - CatchBatch catchBatch, - boolean computeRankOrder) { - - Preconditions.checkNotNull(source.getFishingOperation()); - Preconditions.checkNotNull(source.getFishingOperation().getId()); - - // If parent and root need to be set - if (target.getId() == null - || target.getRootBatch() == null - || (target.getParentBatch() != null && !target.getParentBatch().getId().equals(parentBatchId))) { - batchHelper.setBenthosBatchParents( - source.getSampleCategoryId(), - source.getSampleCategoryValue(), - target, - parentBatchId, - catchBatch); - } - - batchHelper.beanToEntity(parentBatchId, source, target, computeRankOrder); - } +// protected void beanToEntity(SpeciesBatch source, +// SortingBatch target, +// Integer parentBatchId, +// CatchBatch catchBatch, +// boolean computeRankOrder) { +// +// Preconditions.checkNotNull(source.getFishingOperation()); +// Preconditions.checkNotNull(source.getFishingOperation().getId()); +// +// // If parent and root need to be set +// if (target.getId() == null +// || target.getRootBatch() == null +// || (target.getParentBatch() != null && !target.getParentBatch().getId().equals(parentBatchId))) { +// batchHelper.setBenthosBatchParents( +// source.getSampleCategoryId(), +// source.getSampleCategoryValue(), +// target, +// parentBatchId, +// catchBatch); +// } +// +// batchHelper.beanToEntity(parentBatchId, source, target, computeRankOrder); +// } } diff --git a/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/CatchBatchPersistenceServiceImpl.java b/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/CatchBatchPersistenceServiceImpl.java index b50f139..b1c9682 100644 --- a/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/CatchBatchPersistenceServiceImpl.java +++ b/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/CatchBatchPersistenceServiceImpl.java @@ -36,7 +36,7 @@ import fr.ifremer.tutti.TuttiConfiguration; import fr.ifremer.tutti.persistence.entities.data.CatchBatch; import fr.ifremer.tutti.persistence.entities.data.CatchBatchBean; import fr.ifremer.tutti.persistence.service.util.BatchPersistenceHelper; -import fr.ifremer.tutti.persistence.service.util.BatchTreeHelper; +import fr.ifremer.tutti.persistence.service.util.tree.BatchTreeHelper; import fr.ifremer.tutti.persistence.service.util.MeasurementPersistenceHelper; import fr.ifremer.tutti.persistence.service.util.SynchronizationStatusHelper; import fr.ifremer.tutti.util.Numbers; diff --git a/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/MarineLitterBatchPersistenceServiceImpl.java b/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/MarineLitterBatchPersistenceServiceImpl.java index df26d89..71198f2 100644 --- a/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/MarineLitterBatchPersistenceServiceImpl.java +++ b/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/MarineLitterBatchPersistenceServiceImpl.java @@ -39,7 +39,7 @@ import fr.ifremer.tutti.persistence.entities.referential.Caracteristic; import fr.ifremer.tutti.persistence.entities.referential.CaracteristicQualitativeValue; import fr.ifremer.tutti.persistence.service.referential.CaracteristicPersistenceService; import fr.ifremer.tutti.persistence.service.util.BatchPersistenceHelper; -import fr.ifremer.tutti.persistence.service.util.BatchTreeHelper; +import fr.ifremer.tutti.persistence.service.util.tree.BatchTreeHelper; import fr.ifremer.tutti.persistence.service.util.MeasurementPersistenceHelper; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -235,7 +235,7 @@ public class MarineLitterBatchPersistenceServiceImpl extends AbstractPersistence // If parent and root need to be set if (target.getId() == null || target.getRootBatch() == null) { - batchHelper.setMarineLitterBatchParents(target, catchBatch); + setMarineLitterBatchParents(target, catchBatch); } // RankOrder (initialize once, at creation) @@ -302,4 +302,24 @@ public class MarineLitterBatchPersistenceServiceImpl extends AbstractPersistence target.setExhaustiveInventory(true); } + + private void setMarineLitterBatchParents(SortingBatch target, CatchBatch catchBatch) { + + Preconditions.checkNotNull(target); + + // -- Hors Vrac > Marine Litter + SortingBatch parentBatch = batchTreeHelper.getMarineLitterRootBatch(catchBatch); + + if (parentBatch == null) { + + // -- Hors Vrac + SortingBatch horsVracBatch = batchTreeHelper.getOrCreateHorsVracBatch(catchBatch); + + // -- Hors Vrac > Marine Litter + parentBatch = batchTreeHelper.getOrCreateMarineLitterRootBatch(catchBatch, horsVracBatch, null); + } + + target.setParentBatch(parentBatch); + target.setRootBatch(catchBatch); + } } diff --git a/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/SpeciesBatchPersistenceServiceImpl.java b/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/SpeciesBatchPersistenceServiceImpl.java index b4d763e..e84841e 100644 --- a/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/SpeciesBatchPersistenceServiceImpl.java +++ b/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/SpeciesBatchPersistenceServiceImpl.java @@ -22,45 +22,26 @@ package fr.ifremer.tutti.persistence.service; * #L% */ -import com.google.common.base.Preconditions; -import com.google.common.collect.ArrayListMultimap; -import com.google.common.collect.Lists; import com.google.common.collect.Multimap; -import fr.ifremer.adagio.core.dao.data.batch.Batch; import fr.ifremer.adagio.core.dao.data.batch.CatchBatch; -import fr.ifremer.adagio.core.dao.data.batch.SortingBatch; -import fr.ifremer.adagio.core.dao.referential.taxon.ReferenceTaxon; import fr.ifremer.tutti.persistence.InvalidBatchModelException; -import fr.ifremer.tutti.persistence.entities.TuttiEntities; import fr.ifremer.tutti.persistence.entities.data.BatchContainer; -import fr.ifremer.tutti.persistence.entities.data.FishingOperation; import fr.ifremer.tutti.persistence.entities.data.SampleCategoryModel; import fr.ifremer.tutti.persistence.entities.data.SpeciesBatch; import fr.ifremer.tutti.persistence.entities.data.SpeciesBatchFrequency; import fr.ifremer.tutti.persistence.entities.data.SpeciesBatchFrequencys; import fr.ifremer.tutti.persistence.entities.data.SpeciesBatchs; import fr.ifremer.tutti.persistence.entities.referential.Species; -import fr.ifremer.tutti.persistence.service.referential.SpeciesPersistenceService; import fr.ifremer.tutti.persistence.service.util.BatchPersistenceHelper; -import fr.ifremer.tutti.persistence.service.util.BatchTreeHelper; -import fr.ifremer.tutti.persistence.service.util.MeasurementPersistenceHelper; -import fr.ifremer.tutti.persistence.service.util.SynchronizationStatusHelper; -import org.apache.commons.collections4.CollectionUtils; +import fr.ifremer.tutti.persistence.service.util.tree.SpeciesBatchTreeHelper; +import fr.ifremer.tutti.persistence.service.util.tree.SpeciesBatchTreeHelperSupport; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.springframework.dao.DataIntegrityViolationException; import org.springframework.stereotype.Service; import javax.annotation.Resource; -import java.text.DateFormat; -import java.text.SimpleDateFormat; -import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; import java.util.List; -import java.util.stream.Collectors; - -import static org.nuiton.i18n.I18n.t; /** * Default implementation of {@link SpeciesBatchPersistenceService}. @@ -69,408 +50,419 @@ import static org.nuiton.i18n.I18n.t; * @since 1.2 */ @Service("speciesBatchPersistenceService") -public class SpeciesBatchPersistenceServiceImpl extends AbstractPersistenceService +public class SpeciesBatchPersistenceServiceImpl extends SpeciesBatchPersistenceServiceSupport implements SpeciesBatchPersistenceService { /** Logger. */ private static final Log log = LogFactory.getLog(SpeciesBatchPersistenceServiceImpl.class); - @Resource(name = "speciesPersistenceService") - private SpeciesPersistenceService speciesService; - - @Resource(name = "fishingOperationPersistenceService") - protected FishingOperationPersistenceService fishingOperationPersistenceService; - - @Resource(name = "batchPersistenceHelper") - protected BatchPersistenceHelper batchHelper; - - @Resource(name = "batchTreeHelper") - protected BatchTreeHelper batchTreeHelper; + @Resource(name = "speciesBatchTreeHelper") + protected SpeciesBatchTreeHelper batchTreeHelper; - @Resource(name = "measurementPersistenceHelper") - protected MeasurementPersistenceHelper measurementPersistenceHelper; - - @Resource(name = "synchronizationStatusHelper") - protected SynchronizationStatusHelper synchronizationStatusHelper; + public SpeciesBatchPersistenceServiceImpl() { + super("Species", SpeciesBatchs::newSpeciesBatch, SpeciesBatchFrequencys::newSpeciesBatchFrequency); + } //------------------------------------------------------------------------// //-- SpeciesBatch methods --// //------------------------------------------------------------------------// @Override - public BatchContainer<SpeciesBatch> getRootSpeciesBatch(Integer fishingOperationId, - boolean validateTree) throws InvalidBatchModelException { - Preconditions.checkNotNull(fishingOperationId); - - DateFormat df = new SimpleDateFormat("dd/MM/yyy"); - - CatchBatch catchBatch = batchHelper.getRootCatchBatchByFishingOperationId(fishingOperationId, false); - - // -- Vrac > Species > Alive Itemized - SortingBatch vracSpeciesBatch = batchTreeHelper.getSpeciesVracAliveItemizedRootBatch(catchBatch); - - // container of speciesBatch is arbitraty put on vrac type (there is - // no common ancestor for all species batch). - BatchContainer<SpeciesBatch> result = new BatchContainer<>(); - - SampleCategoryModel sampleCategoryModel = getSampleCategoryModel(); - - if (vracSpeciesBatch != null) { - - result.setId(vracSpeciesBatch.getId()); - - for (Batch batch : vracSpeciesBatch.getChildBatchs()) { - SortingBatch source = (SortingBatch) batch; - ReferenceTaxon referenceTaxon = source.getReferenceTaxon(); - Preconditions.checkNotNull(referenceTaxon, "Can't have a rootSpeciesBatch with a null taxon, but was for " + batch.getId()); - if (log.isTraceEnabled()) { - log.trace("Loading CatchBatch Vrac > Species > Alive Itemized > " + referenceTaxon.getId() + " - " + " (batch:" + source.getId() + ")"); - } - Species species = speciesService.getSpeciesByReferenceTaxonId(referenceTaxon.getId()); - if (species == null) { - FishingOperation fishingOperation = fishingOperationPersistenceService.getFishingOperation(fishingOperationId); - String fishingOperationName = fishingOperation.getStationNumber() + " - " + fishingOperation.getFishingOperationNumber() + " - " + df.format(fishingOperation.getGearShootingStartDate()); - throw new InvalidBatchModelException(t("tutti.persistence.speciesBatch.validation.unkonwn.taxon", fishingOperationName, source.getId(), referenceTaxon.getId())); - } - SpeciesBatch target = SpeciesBatchs.newSpeciesBatch(); - target.setSpecies(species); - - batchHelper.entityToBean(sampleCategoryModel, source, target); - result.addChildren(target); - - if (log.isDebugEnabled()) { - log.debug("Loaded CatchBatch Vrac > Species > Alive Itemized > " + target.getSpecies().getReferenceTaxonId() + ": " + target.getId()); - } - } - } - - // -- Hors Vrac > Species - SortingBatch horsVracSpeciesBatch = batchTreeHelper.getSpeciesHorsVracRootBatch(catchBatch); - - if (horsVracSpeciesBatch != null) { - for (Batch batch : horsVracSpeciesBatch.getChildBatchs()) { - SortingBatch source = (SortingBatch) batch; - ReferenceTaxon referenceTaxon = source.getReferenceTaxon(); - Preconditions.checkNotNull(referenceTaxon, "Can't have a rootSpeciesBatch with a null taxon, but was for " + source.getId()); - if (log.isTraceEnabled()) { - log.trace("Loading CatchBatch Hors Vrac > Species > " + referenceTaxon.getId() + " - " + " (batch:" + source.getId() + ")"); - } - Species species = speciesService.getSpeciesByReferenceTaxonId(referenceTaxon.getId()); - if (species == null) { - FishingOperation fishingOperation = fishingOperationPersistenceService.getFishingOperation(fishingOperationId); - String fishingOperationName = fishingOperation.getStationNumber() + " - " + fishingOperation.getFishingOperationNumber() + " - " + df.format(fishingOperation.getGearShootingStartDate()); - throw new InvalidBatchModelException(t("tutti.persistence.speciesBatch.validation.unkonwn.taxon", fishingOperationName, source.getId(), referenceTaxon.getId())); - } - SpeciesBatch target = SpeciesBatchs.newSpeciesBatch(); - target.setSpecies(species); - batchHelper.entityToBean(sampleCategoryModel, source, target); - result.addChildren(target); - if (log.isDebugEnabled()) { - log.debug("Loaded CatchBatch Hors Vrac > Species > " + target.getSpecies().getReferenceTaxonId() + ": " + target.getId()); - } - } - } - - if (validateTree) { - - // validate with given sample category model - batchHelper.validateSpecies(sampleCategoryModel, result); - } - - return result; + protected SpeciesBatchTreeHelperSupport getBatchTreeHelper() { + return batchTreeHelper; } @Override - public SpeciesBatch createSpeciesBatch(SpeciesBatch bean, Integer parentBatchId, boolean computeRankOrder) { - Preconditions.checkNotNull(bean); - Preconditions.checkArgument(TuttiEntities.isNew(bean)); - Preconditions.checkNotNull(bean.getSpecies()); - Preconditions.checkNotNull(bean.getSpecies().getId()); - Preconditions.checkNotNull(bean.getFishingOperation()); - Preconditions.checkNotNull(bean.getFishingOperation().getId()); - - CatchBatch catchBatch = batchHelper.getRootCatchBatchByFishingOperationId(bean.getFishingOperation().getIdAsInt(), false); - - return createSpeciesBatch(bean, parentBatchId, catchBatch, computeRankOrder); + protected void validate(BatchPersistenceHelper batchHelper, SampleCategoryModel sampleCategoryModel, BatchContainer<SpeciesBatch> result) { + batchHelper.validateSpecies(sampleCategoryModel, result); } @Override - public Collection<SpeciesBatch> createSpeciesBatches(Integer fishingOperationId, Collection<SpeciesBatch> beans) { - - Preconditions.checkNotNull(beans); - Preconditions.checkNotNull(fishingOperationId); + public BatchContainer<SpeciesBatch> getRootSpeciesBatch(Integer fishingOperationId, boolean validateTree) throws InvalidBatchModelException { + + return getRootSpeciesBatch0(fishingOperationId, validateTree); +// +// Preconditions.checkNotNull(fishingOperationId); +// +// DateFormat df = new SimpleDateFormat("dd/MM/yyy"); +// +// CatchBatch catchBatch = batchHelper.getRootCatchBatchByFishingOperationId(fishingOperationId, false); +// +// // -- Vrac > Species > Alive Itemized +// SortingBatch vracSpeciesBatch = batchTreeHelper.getSpeciesVracAliveItemizedRootBatch(catchBatch); +// +// // container of speciesBatch is arbitraty put on vrac type (there is +// // no common ancestor for all species batch). +// BatchContainer<SpeciesBatch> result = new BatchContainer<>(); +// +// SampleCategoryModel sampleCategoryModel = getSampleCategoryModel(); +// +// if (vracSpeciesBatch != null) { +// +// result.setId(vracSpeciesBatch.getId()); +// +// for (Batch batch : vracSpeciesBatch.getChildBatchs()) { +// SortingBatch source = (SortingBatch) batch; +// ReferenceTaxon referenceTaxon = source.getReferenceTaxon(); +// Preconditions.checkNotNull(referenceTaxon, "Can't have a rootSpeciesBatch with a null taxon, but was for " + batch.getId()); +// if (log.isTraceEnabled()) { +// log.trace("Loading CatchBatch Vrac > Species > Alive Itemized > " + referenceTaxon.getId() + " - " + " (batch:" + source.getId() + ")"); +// } +// Species species = speciesService.getSpeciesByReferenceTaxonId(referenceTaxon.getId()); +// if (species == null) { +// FishingOperation fishingOperation = fishingOperationPersistenceService.getFishingOperation(fishingOperationId); +// String fishingOperationName = fishingOperation.getStationNumber() + " - " + fishingOperation.getFishingOperationNumber() + " - " + df.format(fishingOperation.getGearShootingStartDate()); +// throw new InvalidBatchModelException(t("tutti.persistence.speciesBatch.validation.unkonwn.taxon", fishingOperationName, source.getId(), referenceTaxon.getId())); +// } +// SpeciesBatch target = SpeciesBatchs.newSpeciesBatch(); +// target.setSpecies(species); +// +// batchHelper.entityToBean(sampleCategoryModel, source, target); +// result.addChildren(target); +// +// if (log.isDebugEnabled()) { +// log.debug("Loaded CatchBatch Vrac > Species > Alive Itemized > " + target.getSpecies().getReferenceTaxonId() + ": " + target.getId()); +// } +// } +// } +// +// // -- Hors Vrac > Species +// SortingBatch horsVracSpeciesBatch = batchTreeHelper.getSpeciesHorsVracRootBatch(catchBatch); +// +// if (horsVracSpeciesBatch != null) { +// for (Batch batch : horsVracSpeciesBatch.getChildBatchs()) { +// SortingBatch source = (SortingBatch) batch; +// ReferenceTaxon referenceTaxon = source.getReferenceTaxon(); +// Preconditions.checkNotNull(referenceTaxon, "Can't have a rootSpeciesBatch with a null taxon, but was for " + source.getId()); +// if (log.isTraceEnabled()) { +// log.trace("Loading CatchBatch Hors Vrac > Species > " + referenceTaxon.getId() + " - " + " (batch:" + source.getId() + ")"); +// } +// Species species = speciesService.getSpeciesByReferenceTaxonId(referenceTaxon.getId()); +// if (species == null) { +// FishingOperation fishingOperation = fishingOperationPersistenceService.getFishingOperation(fishingOperationId); +// String fishingOperationName = fishingOperation.getStationNumber() + " - " + fishingOperation.getFishingOperationNumber() + " - " + df.format(fishingOperation.getGearShootingStartDate()); +// throw new InvalidBatchModelException(t("tutti.persistence.speciesBatch.validation.unkonwn.taxon", fishingOperationName, source.getId(), referenceTaxon.getId())); +// } +// SpeciesBatch target = SpeciesBatchs.newSpeciesBatch(); +// target.setSpecies(species); +// batchHelper.entityToBean(sampleCategoryModel, source, target); +// result.addChildren(target); +// if (log.isDebugEnabled()) { +// log.debug("Loaded CatchBatch Hors Vrac > Species > " + target.getSpecies().getReferenceTaxonId() + ": " + target.getId()); +// } +// } +// } +// +// if (validateTree) { +// +// // validate with given sample category model +// batchHelper.validateSpecies(sampleCategoryModel, result); +// } +// +// return result; + } - CatchBatch catchBatch = batchHelper.getRootCatchBatchByFishingOperationId(fishingOperationId, false); + @Override + public SpeciesBatch createSpeciesBatch(SpeciesBatch bean, Integer parentBatchId, boolean computeRankOrder) { - Collection<SpeciesBatch> result = new ArrayList<>(); - for (SpeciesBatch bean : beans) { + return createSpeciesBatch0(bean, parentBatchId, computeRankOrder); + +// Preconditions.checkNotNull(bean); +// Preconditions.checkArgument(TuttiEntities.isNew(bean)); +// Preconditions.checkNotNull(bean.getSpecies()); +// Preconditions.checkNotNull(bean.getSpecies().getId()); +// Preconditions.checkNotNull(bean.getFishingOperation()); +// Preconditions.checkNotNull(bean.getFishingOperation().getId()); +// +// CatchBatch catchBatch = batchHelper.getRootCatchBatchByFishingOperationId(bean.getFishingOperation().getIdAsInt(), false); +// +// return createSpeciesBatch(bean, parentBatchId, catchBatch, computeRankOrder); + } - SpeciesBatch created = createSpeciesBatch(bean, null, catchBatch, true); - result.add(created); + @Override + public Collection<SpeciesBatch> createSpeciesBatches(Integer fishingOperationId, Collection<SpeciesBatch> beans) { - } - return result; + return createSpeciesBatches0(fishingOperationId, beans); + +// Preconditions.checkNotNull(beans); +// Preconditions.checkNotNull(fishingOperationId); +// +// CatchBatch catchBatch = batchHelper.getRootCatchBatchByFishingOperationId(fishingOperationId, false); +// +// Collection<SpeciesBatch> result = new ArrayList<>(); +// for (SpeciesBatch bean : beans) { +// +// SpeciesBatch created = createSpeciesBatch(bean, null, catchBatch, true); +// result.add(created); +// +// } +// return result; } protected SpeciesBatch createSpeciesBatch(SpeciesBatch bean, Integer parentBatchId, CatchBatch catchBatch, boolean computeRankOrder) { - - Preconditions.checkNotNull(bean); - Preconditions.checkArgument(TuttiEntities.isNew(bean)); - Preconditions.checkNotNull(bean.getSpecies()); - Preconditions.checkNotNull(bean.getSpecies().getId()); - Preconditions.checkNotNull(bean.getFishingOperation()); - Preconditions.checkNotNull(bean.getFishingOperation().getId()); - - SortingBatch batch = SortingBatch.Factory.newInstance(); - beanToEntity(bean, batch, parentBatchId, catchBatch, computeRankOrder); - bean = batchHelper.createSortingBatch(bean, catchBatch, batch); - - return bean; + return createSpeciesBatch0(bean, parentBatchId, catchBatch, computeRankOrder); +// Preconditions.checkNotNull(bean); +// Preconditions.checkArgument(TuttiEntities.isNew(bean)); +// Preconditions.checkNotNull(bean.getSpecies()); +// Preconditions.checkNotNull(bean.getSpecies().getId()); +// Preconditions.checkNotNull(bean.getFishingOperation()); +// Preconditions.checkNotNull(bean.getFishingOperation().getId()); +// +// SortingBatch batch = SortingBatch.Factory.newInstance(); +// beanToEntity(bean, batch, parentBatchId, catchBatch, computeRankOrder); +// bean = batchHelper.createSortingBatch(bean, catchBatch, batch); +// +// return bean; } @Override public SpeciesBatch saveSpeciesBatch(SpeciesBatch bean) { - Preconditions.checkNotNull(bean); - Preconditions.checkArgument(!TuttiEntities.isNew(bean)); - - Integer batchId = bean.getIdAsInt(); - - CatchBatch catchBatch = batchHelper.getRootCatchBatchByBatchId(batchId); - SortingBatch batch = batchHelper.getSortingBatchById(catchBatch, batchId); - Integer parentBatchId = null; - if (bean.getParentBatch() != null) { - parentBatchId = bean.getParentBatch().getIdAsInt(); - } - beanToEntity(bean, batch, parentBatchId, catchBatch, true); - batchHelper.updateSortingBatch(batch, catchBatch); - - return bean; + return saveSpeciesBatch0(bean); +// Preconditions.checkNotNull(bean); +// Preconditions.checkArgument(!TuttiEntities.isNew(bean)); +// +// Integer batchId = bean.getIdAsInt(); +// +// CatchBatch catchBatch = batchHelper.getRootCatchBatchByBatchId(batchId); +// SortingBatch batch = batchHelper.getSortingBatchById(catchBatch, batchId); +// Integer parentBatchId = null; +// if (bean.getParentBatch() != null) { +// parentBatchId = bean.getParentBatch().getIdAsInt(); +// } +// beanToEntity(bean, batch, parentBatchId, catchBatch, true); +// batchHelper.updateSortingBatch(batch, catchBatch); +// +// return bean; } @Override public void deleteSpeciesBatch(Integer id) { - Preconditions.checkNotNull(id); - batchHelper.deleteBatch(id); + deleteSpeciesBatch0(id); +// Preconditions.checkNotNull(id); +// batchHelper.deleteBatch(id); } @Override public void deleteSpeciesSubBatch(Integer id) { - Preconditions.checkNotNull(id); - batchHelper.deleteSpeciesSubBatch(id); + deleteSpeciesSubBatch0(id); +// Preconditions.checkNotNull(id); +// batchHelper.deleteSpeciesSubBatch(id); } @Override public void changeSpeciesBatchSpecies(Integer id, Species species) { - Preconditions.checkNotNull(id); - Preconditions.checkNotNull(species); - Preconditions.checkNotNull(species.getReferenceTaxonId()); - batchHelper.changeBatchSpecies(id, species); + changeSpeciesBatchSpecies0(id, species); +// Preconditions.checkNotNull(id); +// Preconditions.checkNotNull(species); +// Preconditions.checkNotNull(species.getReferenceTaxonId()); +// batchHelper.changeBatchSpecies(id, species); } @Override public List<SpeciesBatch> getAllSpeciesBatchToConfirm(Integer fishingOperationId) throws InvalidBatchModelException { - List<SpeciesBatch> batchesToConfirm = new ArrayList<>(); - - BatchContainer<SpeciesBatch> rootSpeciesBatch = getRootSpeciesBatch(fishingOperationId, false); - for (SpeciesBatch speciesBatch : rootSpeciesBatch.getChildren()) { - findSpeciesBatchesToConfirm(speciesBatch, batchesToConfirm); - } - - return batchesToConfirm; + return getAllSpeciesBatchToConfirm0(fishingOperationId); +// List<SpeciesBatch> batchesToConfirm = new ArrayList<>(); +// +// BatchContainer<SpeciesBatch> rootSpeciesBatch = getRootSpeciesBatch(fishingOperationId, false); +// for (SpeciesBatch speciesBatch : rootSpeciesBatch.getChildren()) { +// findSpeciesBatchesToConfirm(speciesBatch, batchesToConfirm); +// } +// +// return batchesToConfirm; } - protected void findSpeciesBatchesToConfirm(SpeciesBatch speciesBatch, List<SpeciesBatch> batchesToConfirm) { - if (speciesBatch.isSpeciesToConfirm()) { - batchesToConfirm.add(speciesBatch); - - } else if (!speciesBatch.isChildBatchsEmpty()) { - for (SpeciesBatch batch : speciesBatch.getChildBatchs()) { - findSpeciesBatchesToConfirm(batch, batchesToConfirm); - } - } - } +// protected void findSpeciesBatchesToConfirm(SpeciesBatch speciesBatch, List<SpeciesBatch> batchesToConfirm) { +// if (speciesBatch.isSpeciesToConfirm()) { +// batchesToConfirm.add(speciesBatch); +// +// } else if (!speciesBatch.isChildBatchsEmpty()) { +// for (SpeciesBatch batch : speciesBatch.getChildBatchs()) { +// findSpeciesBatchesToConfirm(batch, batchesToConfirm); +// } +// } +// } //------------------------------------------------------------------------// //-- SpeciesBatchFrequency methods --// //------------------------------------------------------------------------// @Override - public List<SpeciesBatchFrequency> getAllSpeciesBatchFrequency( - Integer speciesBatchId) { - Preconditions.checkNotNull(speciesBatchId); - - List<SortingBatch> frequencyChilds = - batchHelper.getFrequencies(speciesBatchId); - List<SpeciesBatchFrequency> results = Lists.newArrayList(); - for (SortingBatch child : frequencyChilds) { - SpeciesBatchFrequency target = - SpeciesBatchFrequencys.newSpeciesBatchFrequency(); - - batchHelper.entityToBatchFrequency(child, target); - results.add(target); - } - return Collections.unmodifiableList(results); + public List<SpeciesBatchFrequency> getAllSpeciesBatchFrequency(Integer speciesBatchId) { + return getAllSpeciesBatchFrequency0(speciesBatchId); +// Preconditions.checkNotNull(speciesBatchId); +// +// List<SortingBatch> frequencyChilds = +// batchHelper.getFrequencies(speciesBatchId); +// List<SpeciesBatchFrequency> results = Lists.newArrayList(); +// for (SortingBatch child : frequencyChilds) { +// SpeciesBatchFrequency target = +// SpeciesBatchFrequencys.newSpeciesBatchFrequency(); +// +// batchHelper.entityToBatchFrequency(child, target); +// results.add(target); +// } +// return Collections.unmodifiableList(results); } @Override public Multimap<Species, SpeciesBatchFrequency> getAllSpeciesBatchFrequencyForBatch(BatchContainer<SpeciesBatch> batchContainer) { - Multimap<Species, SpeciesBatchFrequency> result = ArrayListMultimap.create(); - for (SpeciesBatch speciesBatch : batchContainer.getChildren()) { - getAllSpeciesBatchFrequencyForBatch(speciesBatch, result); - } - return result; + return getAllSpeciesBatchFrequencyForBatch0(batchContainer); +// Multimap<Species, SpeciesBatchFrequency> result = ArrayListMultimap.create(); +// for (SpeciesBatch speciesBatch : batchContainer.getChildren()) { +// getAllSpeciesBatchFrequencyForBatch(speciesBatch, result); +// } +// return result; } - protected void getAllSpeciesBatchFrequencyForBatch(SpeciesBatch batch, - Multimap<Species, SpeciesBatchFrequency> result) { - - List<SpeciesBatchFrequency> speciesBatchFrequency = getAllSpeciesBatchFrequency(batch.getIdAsInt()); - result.putAll(batch.getSpecies(), speciesBatchFrequency); - - if (!batch.isChildBatchsEmpty()) { - for (SpeciesBatch child : batch.getChildBatchs()) { - getAllSpeciesBatchFrequencyForBatch(child, result); - } - } - } +// protected void getAllSpeciesBatchFrequencyForBatch(SpeciesBatch batch, +// Multimap<Species, SpeciesBatchFrequency> result) { +// +// List<SpeciesBatchFrequency> speciesBatchFrequency = getAllSpeciesBatchFrequency(batch.getIdAsInt()); +// result.putAll(batch.getSpecies(), speciesBatchFrequency); +// +// if (!batch.isChildBatchsEmpty()) { +// for (SpeciesBatch child : batch.getChildBatchs()) { +// getAllSpeciesBatchFrequencyForBatch(child, result); +// } +// } +// } @Override - public List<SpeciesBatchFrequency> saveSpeciesBatchFrequency( - Integer speciesBatchId, - List<SpeciesBatchFrequency> frequencies) { - Preconditions.checkNotNull(speciesBatchId); - Preconditions.checkNotNull(frequencies); - - List<SpeciesBatchFrequency> notNullFrequencies = Lists.newArrayList(); - - // Check that all frequencies have the same length PMFM (before doing any db call) - // and remove null frequencies - String pmfmId = null; - for (SpeciesBatchFrequency source : frequencies) { - - if (source.getLengthStepCaracteristic() != null) { - if (pmfmId == null) { - pmfmId = source.getLengthStepCaracteristic().getId(); - } else if (!pmfmId.equals(source.getLengthStepCaracteristic().getId())) { - throw new DataIntegrityViolationException("Batch frequencies under one SpeciesBatch must have all the same lengthStepCaracteristic"); - } - notNullFrequencies.add(source); - } - } - - CatchBatch catchBatch = batchHelper.getRootCatchBatchByBatchId(speciesBatchId); - - if (catchBatch == null) { - return notNullFrequencies; - } - - // Synchronization status - synchronizationStatusHelper.setDirty(catchBatch); - - // Retrieve parent - SortingBatch parentBatch = batchHelper.getSortingBatchById(catchBatch, speciesBatchId); - - // Remember child ids, to remove unchanged item (see at bottom in this method) - List<Integer> notUpdatedChildIds = Lists.newArrayList(); - List<SortingBatch> frequencyChilds = batchHelper.getFrequencyChilds(parentBatch); - notUpdatedChildIds.addAll(frequencyChilds.stream().map(SortingBatch::getId).collect(Collectors.toList())); - - short rankOrder = 0; - List<SortingBatch> batchsToUpdate = Lists.newArrayList(); - for (SpeciesBatchFrequency source : notNullFrequencies) { - rankOrder++; - - SortingBatch target; - if (source.getId() == null) { - - // Not existing batch - target = SortingBatch.Factory.newInstance(); - - // Fill the sorting batch from the source - batchHelper.beanToEntity(source, target, parentBatch, rankOrder); - - // Create the targeted batch, then update the source id - batchHelper.createSortingBatch(source, catchBatch, target); - - // push back id of created sortingBatch - source.setId(target.getId()); - - if (log.isDebugEnabled()) { - log.debug("Create frequency sortingBatch(" + rankOrder + "): " + target.getId()); - } - } else { - - // Existing batch - target = batchHelper.loadSortingBatch(source.getIdAsInt(), catchBatch); - - // Fill the sorting batch from the source - batchHelper.beanToEntity(source, target, parentBatch, rankOrder); - - // Add the batch into a list (will be update later, using this list) - batchsToUpdate.add(target); - - // Remove id from id to remove - notUpdatedChildIds.remove(target.getId()); - - if (log.isDebugEnabled()) { - log.debug("Update frequency sortingBatch(" + rankOrder + "): " + target.getId()); - } - } - } - - if (CollectionUtils.isNotEmpty(batchsToUpdate)) { - - // update some batchs - batchHelper.updateSortingBatch(batchsToUpdate, catchBatch); - } - - if (CollectionUtils.isNotEmpty(notUpdatedChildIds)) { - - // Remove obsolete frequencies - for (Integer batchId : notUpdatedChildIds) { - - if (log.isDebugEnabled()) { - log.debug("Remove obsolete frequency sortingBatch: " + batchId); - } - batchHelper.removeWithChildren(batchId, catchBatch); - } - } - - return Collections.unmodifiableList(notNullFrequencies); + public List<SpeciesBatchFrequency> saveSpeciesBatchFrequency(Integer speciesBatchId, List<SpeciesBatchFrequency> frequencies) { + return saveSpeciesBatchFrequency0(speciesBatchId, frequencies); +// Preconditions.checkNotNull(speciesBatchId); +// Preconditions.checkNotNull(frequencies); +// +// List<SpeciesBatchFrequency> notNullFrequencies = Lists.newArrayList(); +// +// // Check that all frequencies have the same length PMFM (before doing any db call) +// // and remove null frequencies +// String pmfmId = null; +// for (SpeciesBatchFrequency source : frequencies) { +// +// if (source.getLengthStepCaracteristic() != null) { +// if (pmfmId == null) { +// pmfmId = source.getLengthStepCaracteristic().getId(); +// } else if (!pmfmId.equals(source.getLengthStepCaracteristic().getId())) { +// throw new DataIntegrityViolationException("Batch frequencies under one SpeciesBatch must have all the same lengthStepCaracteristic"); +// } +// notNullFrequencies.add(source); +// } +// } +// +// CatchBatch catchBatch = batchHelper.getRootCatchBatchByBatchId(speciesBatchId); +// +// if (catchBatch == null) { +// return notNullFrequencies; +// } +// +// // Synchronization status +// synchronizationStatusHelper.setDirty(catchBatch); +// +// // Retrieve parent +// SortingBatch parentBatch = batchHelper.getSortingBatchById(catchBatch, speciesBatchId); +// +// // Remember child ids, to remove unchanged item (see at bottom in this method) +// List<Integer> notUpdatedChildIds = Lists.newArrayList(); +// List<SortingBatch> frequencyChilds = batchHelper.getFrequencyChilds(parentBatch); +// notUpdatedChildIds.addAll(frequencyChilds.stream().map(SortingBatch::getId).collect(Collectors.toList())); +// +// short rankOrder = 0; +// List<SortingBatch> batchsToUpdate = Lists.newArrayList(); +// for (SpeciesBatchFrequency source : notNullFrequencies) { +// rankOrder++; +// +// SortingBatch target; +// if (source.getId() == null) { +// +// // Not existing batch +// target = SortingBatch.Factory.newInstance(); +// +// // Fill the sorting batch from the source +// batchHelper.beanToEntity(source, target, parentBatch, rankOrder); +// +// // Create the targeted batch, then update the source id +// batchHelper.createSortingBatch(source, catchBatch, target); +// +// // push back id of created sortingBatch +// source.setId(target.getId()); +// +// if (log.isDebugEnabled()) { +// log.debug("Create frequency sortingBatch(" + rankOrder + "): " + target.getId()); +// } +// } else { +// +// // Existing batch +// target = batchHelper.loadSortingBatch(source.getIdAsInt(), catchBatch); +// +// // Fill the sorting batch from the source +// batchHelper.beanToEntity(source, target, parentBatch, rankOrder); +// +// // Add the batch into a list (will be update later, using this list) +// batchsToUpdate.add(target); +// +// // Remove id from id to remove +// notUpdatedChildIds.remove(target.getId()); +// +// if (log.isDebugEnabled()) { +// log.debug("Update frequency sortingBatch(" + rankOrder + "): " + target.getId()); +// } +// } +// } +// +// if (CollectionUtils.isNotEmpty(batchsToUpdate)) { +// +// // update some batchs +// batchHelper.updateSortingBatch(batchsToUpdate, catchBatch); +// } +// +// if (CollectionUtils.isNotEmpty(notUpdatedChildIds)) { +// +// // Remove obsolete frequencies +// for (Integer batchId : notUpdatedChildIds) { +// +// if (log.isDebugEnabled()) { +// log.debug("Remove obsolete frequency sortingBatch: " + batchId); +// } +// batchHelper.removeWithChildren(batchId, catchBatch); +// } +// } +// +// return Collections.unmodifiableList(notNullFrequencies); } //------------------------------------------------------------------------// //-- Internal methods --// //------------------------------------------------------------------------// - protected void beanToEntity(SpeciesBatch source, - SortingBatch target, - Integer parentBatchId, - CatchBatch catchBatch, - boolean computeRankOrder) { - - Preconditions.checkNotNull(source.getFishingOperation()); - Preconditions.checkNotNull(source.getFishingOperation().getId()); - - // If parent and root need to be set - if (target.getId() == null - || target.getRootBatch() == null - || (target.getParentBatch() != null && !target.getParentBatch().getId().equals(parentBatchId))) { - - batchHelper.setSpeciesBatchParents( - source.getSampleCategoryId(), - source.getSampleCategoryValue(), - target, - parentBatchId, - catchBatch - ); - } - - batchHelper.beanToEntity(parentBatchId, source, target, computeRankOrder); - - } +// protected void beanToEntity(SpeciesBatch source, +// SortingBatch target, +// Integer parentBatchId, +// CatchBatch catchBatch, +// boolean computeRankOrder) { +// +// Preconditions.checkNotNull(source.getFishingOperation()); +// Preconditions.checkNotNull(source.getFishingOperation().getId()); +// +// // If parent and root need to be set +// if (target.getId() == null +// || target.getRootBatch() == null +// || (target.getParentBatch() != null && !target.getParentBatch().getId().equals(parentBatchId))) { +// +// batchHelper.setSpeciesBatchParents( +// source.getSampleCategoryId(), +// source.getSampleCategoryValue(), +// target, +// parentBatchId, +// catchBatch +// ); +// } +// +// batchHelper.beanToEntity(parentBatchId, source, target, computeRankOrder); +// +// } } diff --git a/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/SpeciesBatchPersistenceServiceSupport.java b/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/SpeciesBatchPersistenceServiceSupport.java new file mode 100644 index 0000000..cdd6351 --- /dev/null +++ b/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/SpeciesBatchPersistenceServiceSupport.java @@ -0,0 +1,933 @@ +package fr.ifremer.tutti.persistence.service; + +/* + * #%L + * Tutti :: Persistence + * %% + * Copyright (C) 2012 - 2014 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.Preconditions; +import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.Iterables; +import com.google.common.collect.Lists; +import com.google.common.collect.Multimap; +import com.google.common.collect.Sets; +import fr.ifremer.adagio.core.dao.data.batch.Batch; +import fr.ifremer.adagio.core.dao.data.batch.CatchBatch; +import fr.ifremer.adagio.core.dao.data.batch.SortingBatch; +import fr.ifremer.adagio.core.dao.data.measure.SortingMeasurement; +import fr.ifremer.adagio.core.dao.referential.QualityFlag; +import fr.ifremer.adagio.core.dao.referential.QualityFlagCode; +import fr.ifremer.adagio.core.dao.referential.QualityFlagImpl; +import fr.ifremer.adagio.core.dao.referential.pmfm.Pmfm; +import fr.ifremer.adagio.core.dao.referential.pmfm.PmfmId; +import fr.ifremer.adagio.core.dao.referential.taxon.ReferenceTaxon; +import fr.ifremer.adagio.core.dao.referential.taxon.ReferenceTaxonImpl; +import fr.ifremer.tutti.persistence.InvalidBatchModelException; +import fr.ifremer.tutti.persistence.entities.TuttiEntities; +import fr.ifremer.tutti.persistence.entities.data.BatchContainer; +import fr.ifremer.tutti.persistence.entities.data.FishingOperation; +import fr.ifremer.tutti.persistence.entities.data.SampleCategoryModel; +import fr.ifremer.tutti.persistence.entities.data.SpeciesBatch; +import fr.ifremer.tutti.persistence.entities.data.SpeciesBatchFrequency; +import fr.ifremer.tutti.persistence.entities.data.SpeciesBatchs; +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.persistence.service.referential.CaracteristicPersistenceService; +import fr.ifremer.tutti.persistence.service.referential.SpeciesPersistenceService; +import fr.ifremer.tutti.persistence.service.util.BatchPersistenceHelper; +import fr.ifremer.tutti.persistence.service.util.MeasurementPersistenceHelper; +import fr.ifremer.tutti.persistence.service.util.tree.SpeciesBatchTreeHelperSupport; +import fr.ifremer.tutti.persistence.service.util.SynchronizationStatusHelper; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.dao.DataIntegrityViolationException; + +import javax.annotation.Resource; +import java.io.Serializable; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Objects; +import java.util.Set; +import java.util.function.Supplier; +import java.util.stream.Collectors; + +import static org.nuiton.i18n.I18n.t; + +/** + * Default implementation of {@link SpeciesBatchPersistenceService}. + * + * @author Tony Chemit - chemit@codelutin.com + * @since 1.2 + */ +public abstract class SpeciesBatchPersistenceServiceSupport extends AbstractPersistenceService { + + /** Logger. */ + private static final Log log = LogFactory.getLog(SpeciesBatchPersistenceServiceSupport.class); + + @Resource(name = "speciesPersistenceService") + private SpeciesPersistenceService speciesService; + + @Resource(name = "fishingOperationPersistenceService") + private FishingOperationPersistenceService fishingOperationPersistenceService; + + @Resource(name = "caracteristicPersistenceService") + private CaracteristicPersistenceService caracteristicService; + + @Resource(name = "batchPersistenceHelper") + private BatchPersistenceHelper batchHelper; + + @Resource(name = "measurementPersistenceHelper") + protected MeasurementPersistenceHelper measurementPersistenceHelper; + + @Resource(name = "synchronizationStatusHelper") + private SynchronizationStatusHelper synchronizationStatusHelper; + + private final String prefix; + + private final Supplier<SpeciesBatch> batchFactory; + private final Supplier<SpeciesBatchFrequency> frequencyFactory; + + protected SpeciesBatchPersistenceServiceSupport(String prefix, Supplier<SpeciesBatch> batchFactory, Supplier<SpeciesBatchFrequency> frequencyFactory) { + this.prefix = prefix; + this.batchFactory = batchFactory; + this.frequencyFactory = frequencyFactory; + } + //------------------------------------------------------------------------// + //-- SpeciesBatch methods --// + //------------------------------------------------------------------------// + + protected abstract SpeciesBatchTreeHelperSupport getBatchTreeHelper(); + + protected abstract void validate(BatchPersistenceHelper batchHelper, SampleCategoryModel sampleCategoryModel, BatchContainer<SpeciesBatch> result); + + protected final BatchContainer<SpeciesBatch> getRootSpeciesBatch0(Integer fishingOperationId, boolean validateTree) throws InvalidBatchModelException { + + Objects.requireNonNull(fishingOperationId); + + DateFormat df = new SimpleDateFormat("dd/MM/yyy"); + + CatchBatch catchBatch = batchHelper.getRootCatchBatchByFishingOperationId(fishingOperationId, false); + + // -- Vrac > Species > Alive Itemized + SortingBatch vracSpeciesBatch = getBatchTreeHelper().getVracAliveItemizedRootBatch(catchBatch); + + // container of speciesBatch is arbitraty put on vrac type (there is + // no common ancestor for all species batch). + BatchContainer<SpeciesBatch> result = new BatchContainer<>(); + + SampleCategoryModel sampleCategoryModel = getSampleCategoryModel(); + + if (vracSpeciesBatch != null) { + + result.setId(vracSpeciesBatch.getId()); + + for (Batch batch : vracSpeciesBatch.getChildBatchs()) { + SortingBatch source = (SortingBatch) batch; + ReferenceTaxon referenceTaxon = source.getReferenceTaxon(); + Objects.requireNonNull(referenceTaxon, "Can't have a rootSpeciesBatch with a null taxon, but was for " + batch.getId()); + if (log.isTraceEnabled()) { + log.trace("Loading CatchBatch Vrac > " + prefix + " > Alive Itemized > " + referenceTaxon.getId() + " - " + " (batch:" + source.getId() + ")"); + } + Species species = speciesService.getSpeciesByReferenceTaxonId(referenceTaxon.getId()); + if (species == null) { + FishingOperation fishingOperation = fishingOperationPersistenceService.getFishingOperation(fishingOperationId); + String fishingOperationName = fishingOperation.getStationNumber() + " - " + fishingOperation.getFishingOperationNumber() + " - " + df.format(fishingOperation.getGearShootingStartDate()); + throw new InvalidBatchModelException(t("tutti.persistence.speciesBatch.validation.unkonwn.taxon", fishingOperationName, source.getId(), referenceTaxon.getId())); + } + SpeciesBatch target = batchFactory.get(); + target.setSpecies(species); + + entityToBean(sampleCategoryModel, source, target); + result.addChildren(target); + + if (log.isDebugEnabled()) { + log.debug("Loaded CatchBatch Vrac > " + prefix + " > Alive Itemized > " + target.getSpecies().getReferenceTaxonId() + ": " + target.getId()); + } + } + } + + // -- Hors Vrac > Species + SortingBatch horsVracSpeciesBatch = getBatchTreeHelper().getHorsVracRootBatch(catchBatch); + + if (horsVracSpeciesBatch != null) { + for (Batch batch : horsVracSpeciesBatch.getChildBatchs()) { + SortingBatch source = (SortingBatch) batch; + ReferenceTaxon referenceTaxon = source.getReferenceTaxon(); + Objects.requireNonNull(referenceTaxon, "Can't have a rootSpeciesBatch with a null taxon, but was for " + source.getId()); + if (log.isTraceEnabled()) { + log.trace("Loading CatchBatch Hors Vrac > " + prefix + " > " + referenceTaxon.getId() + " - " + " (batch:" + source.getId() + ")"); + } + Species species = speciesService.getSpeciesByReferenceTaxonId(referenceTaxon.getId()); + if (species == null) { + FishingOperation fishingOperation = fishingOperationPersistenceService.getFishingOperation(fishingOperationId); + String fishingOperationName = fishingOperation.getStationNumber() + " - " + fishingOperation.getFishingOperationNumber() + " - " + df.format(fishingOperation.getGearShootingStartDate()); + throw new InvalidBatchModelException(t("tutti.persistence.speciesBatch.validation.unkonwn.taxon", fishingOperationName, source.getId(), referenceTaxon.getId())); + } + SpeciesBatch target = batchFactory.get(); + target.setSpecies(species); + entityToBean(sampleCategoryModel, source, target); + result.addChildren(target); + if (log.isDebugEnabled()) { + log.debug("Loaded CatchBatch Hors Vrac > " + prefix + " > " + target.getSpecies().getReferenceTaxonId() + ": " + target.getId()); + } + } + } + + if (validateTree) { + + // validate with given sample category model + validate(batchHelper, sampleCategoryModel, result); + } + + return result; + } + + protected SpeciesBatch createSpeciesBatch0(SpeciesBatch bean, Integer parentBatchId, boolean computeRankOrder) { + Objects.requireNonNull(bean); + Preconditions.checkArgument(TuttiEntities.isNew(bean)); + Objects.requireNonNull(bean.getSpecies()); + Objects.requireNonNull(bean.getSpecies().getId()); + Objects.requireNonNull(bean.getFishingOperation()); + Objects.requireNonNull(bean.getFishingOperation().getId()); + + CatchBatch catchBatch = batchHelper.getRootCatchBatchByFishingOperationId(bean.getFishingOperation().getIdAsInt(), false); + + return createSpeciesBatch0(bean, parentBatchId, catchBatch, computeRankOrder); + } + + + protected Collection<SpeciesBatch> createSpeciesBatches0(Integer fishingOperationId, Collection<SpeciesBatch> beans) { + + Objects.requireNonNull(beans); + Objects.requireNonNull(fishingOperationId); + + CatchBatch catchBatch = batchHelper.getRootCatchBatchByFishingOperationId(fishingOperationId, false); + + Collection<SpeciesBatch> result = new ArrayList<>(); + for (SpeciesBatch bean : beans) { + + SpeciesBatch created = createSpeciesBatch0(bean, null, catchBatch, true); + result.add(created); + + } + return result; + + } + + protected SpeciesBatch createSpeciesBatch0(SpeciesBatch bean, Integer parentBatchId, CatchBatch catchBatch, boolean computeRankOrder) { + + Objects.requireNonNull(bean); + Preconditions.checkArgument(TuttiEntities.isNew(bean)); + Objects.requireNonNull(bean.getSpecies()); + Objects.requireNonNull(bean.getSpecies().getId()); + Objects.requireNonNull(bean.getFishingOperation()); + Objects.requireNonNull(bean.getFishingOperation().getId()); + + SortingBatch batch = SortingBatch.Factory.newInstance(); + beanToEntity0(bean, batch, parentBatchId, catchBatch, computeRankOrder); + bean = batchHelper.createSortingBatch(bean, catchBatch, batch); + + return bean; + } + + protected SpeciesBatch saveSpeciesBatch0(SpeciesBatch bean) { + Objects.requireNonNull(bean); + Preconditions.checkArgument(!TuttiEntities.isNew(bean)); + + Integer batchId = bean.getIdAsInt(); + + CatchBatch catchBatch = batchHelper.getRootCatchBatchByBatchId(batchId); + SortingBatch batch = batchHelper.getSortingBatchById(catchBatch, batchId); + Integer parentBatchId = null; + if (bean.getParentBatch() != null) { + parentBatchId = bean.getParentBatch().getIdAsInt(); + } + beanToEntity0(bean, batch, parentBatchId, catchBatch, true); + batchHelper.updateSortingBatch(batch, catchBatch); + + return bean; + } + + protected void deleteSpeciesBatch0(Integer id) { + Objects.requireNonNull(id); + batchHelper.deleteBatch(id); + } + + protected void deleteSpeciesSubBatch0(Integer id) { + Objects.requireNonNull(id); + deleteSpeciesSubBatch(id); + } + + protected void changeSpeciesBatchSpecies0(Integer id, Species species) { + Objects.requireNonNull(id); + Objects.requireNonNull(species); + Objects.requireNonNull(species.getReferenceTaxonId()); + changeBatchSpecies(id, species); + } + + protected List<SpeciesBatch> getAllSpeciesBatchToConfirm0(Integer fishingOperationId) throws InvalidBatchModelException { + List<SpeciesBatch> batchesToConfirm = new ArrayList<>(); + + BatchContainer<SpeciesBatch> rootSpeciesBatch = getRootSpeciesBatch0(fishingOperationId, false); + for (SpeciesBatch speciesBatch : rootSpeciesBatch.getChildren()) { + findSpeciesBatchesToConfirm(speciesBatch, batchesToConfirm); + } + + return batchesToConfirm; + } + + private void findSpeciesBatchesToConfirm(SpeciesBatch speciesBatch, List<SpeciesBatch> batchesToConfirm) { + if (speciesBatch.isSpeciesToConfirm()) { + batchesToConfirm.add(speciesBatch); + + } else if (!speciesBatch.isChildBatchsEmpty()) { + for (SpeciesBatch batch : speciesBatch.getChildBatchs()) { + findSpeciesBatchesToConfirm(batch, batchesToConfirm); + } + } + } + + //------------------------------------------------------------------------// + //-- SpeciesBatchFrequency methods --// + //------------------------------------------------------------------------// + + protected List<SpeciesBatchFrequency> getAllSpeciesBatchFrequency0(Integer speciesBatchId) { + Objects.requireNonNull(speciesBatchId); + + List<SortingBatch> frequencyChilds = getFrequencies(speciesBatchId); + List<SpeciesBatchFrequency> results = new ArrayList<>(); + for (SortingBatch child : frequencyChilds) { + SpeciesBatchFrequency target = frequencyFactory.get(); + + entityToBatchFrequency(child, target); + results.add(target); + } + return Collections.unmodifiableList(results); + } + + protected Multimap<Species, SpeciesBatchFrequency> getAllSpeciesBatchFrequencyForBatch0(BatchContainer<SpeciesBatch> batchContainer) { + Multimap<Species, SpeciesBatchFrequency> result = ArrayListMultimap.create(); + for (SpeciesBatch speciesBatch : batchContainer.getChildren()) { + getAllSpeciesBatchFrequencyForBatch(speciesBatch, result); + } + return result; + } + + private void getAllSpeciesBatchFrequencyForBatch(SpeciesBatch batch, Multimap<Species, SpeciesBatchFrequency> result) { + + List<SpeciesBatchFrequency> speciesBatchFrequency = getAllSpeciesBatchFrequency0(batch.getIdAsInt()); + result.putAll(batch.getSpecies(), speciesBatchFrequency); + + if (!batch.isChildBatchsEmpty()) { + for (SpeciesBatch child : batch.getChildBatchs()) { + getAllSpeciesBatchFrequencyForBatch(child, result); + } + } + } + + protected List<SpeciesBatchFrequency> saveSpeciesBatchFrequency0(Integer speciesBatchId, List<SpeciesBatchFrequency> frequencies) { + Objects.requireNonNull(speciesBatchId); + Objects.requireNonNull(frequencies); + + List<SpeciesBatchFrequency> notNullFrequencies = new ArrayList<>(); + + // Check that all frequencies have the same length PMFM (before doing any db call) + // and remove null frequencies + String pmfmId = null; + for (SpeciesBatchFrequency source : frequencies) { + + if (source.getLengthStepCaracteristic() != null) { + if (pmfmId == null) { + pmfmId = source.getLengthStepCaracteristic().getId(); + } else if (!pmfmId.equals(source.getLengthStepCaracteristic().getId())) { + throw new DataIntegrityViolationException("Batch frequencies under one SpeciesBatch must have all the same lengthStepCaracteristic"); + } + notNullFrequencies.add(source); + } + } + + CatchBatch catchBatch = batchHelper.getRootCatchBatchByBatchId(speciesBatchId); + + if (catchBatch == null) { + return notNullFrequencies; + } + + // Synchronization status + synchronizationStatusHelper.setDirty(catchBatch); + + // Retrieve parent + SortingBatch parentBatch = batchHelper.getSortingBatchById(catchBatch, speciesBatchId); + + // Remember child ids, to remove unchanged item (see at bottom in this method) + List<Integer> notUpdatedChildIds = new ArrayList<>(); + List<SortingBatch> frequencyChilds = getFrequencyChilds(parentBatch); + notUpdatedChildIds.addAll(frequencyChilds.stream().map(SortingBatch::getId).collect(Collectors.toList())); + + short rankOrder = 0; + List<SortingBatch> batchsToUpdate = new ArrayList<>(); + for (SpeciesBatchFrequency source : notNullFrequencies) { + rankOrder++; + + SortingBatch target; + if (source.getId() == null) { + + // Not existing batch + target = SortingBatch.Factory.newInstance(); + + // Fill the sorting batch from the source + beanToEntity0(source, target, parentBatch, rankOrder); + + // Create the targeted batch, then update the source id + batchHelper.createSortingBatch(source, catchBatch, target); + + // push back id of created sortingBatch + source.setId(target.getId()); + + if (log.isDebugEnabled()) { + log.debug("Create frequency sortingBatch(" + rankOrder + "): " + target.getId()); + } + } else { + + // Existing batch + target = batchHelper.loadSortingBatch(source.getIdAsInt(), catchBatch); + + // Fill the sorting batch from the source + beanToEntity0(source, target, parentBatch, rankOrder); + + // Add the batch into a list (will be update later, using this list) + batchsToUpdate.add(target); + + // Remove id from id to remove + notUpdatedChildIds.remove(target.getId()); + + if (log.isDebugEnabled()) { + log.debug("Update frequency sortingBatch(" + rankOrder + "): " + target.getId()); + } + } + } + + if (CollectionUtils.isNotEmpty(batchsToUpdate)) { + + // update some batchs + batchHelper.updateSortingBatch(batchsToUpdate, catchBatch); + } + + if (CollectionUtils.isNotEmpty(notUpdatedChildIds)) { + + // Remove obsolete frequencies + for (Integer batchId : notUpdatedChildIds) { + + if (log.isDebugEnabled()) { + log.debug("Remove obsolete frequency sortingBatch: " + batchId); + } + batchHelper.removeWithChildren(batchId, catchBatch); + } + } + + return Collections.unmodifiableList(notNullFrequencies); + } + + //------------------------------------------------------------------------// + //-- Internal methods --// + //------------------------------------------------------------------------// + + protected void beanToEntity0(SpeciesBatch source, + SortingBatch target, + Integer parentBatchId, + CatchBatch catchBatch, + boolean computeRankOrder) { + + Objects.requireNonNull(source.getFishingOperation()); + Objects.requireNonNull(source.getFishingOperation().getId()); + + // If parent and root need to be set + if (target.getId() == null + || target.getRootBatch() == null + || (target.getParentBatch() != null && !target.getParentBatch().getId().equals(parentBatchId))) { + + getBatchTreeHelper().setBatchParents(source.getSampleCategoryId(), + source.getSampleCategoryValue(), + target, + parentBatchId, + catchBatch); + } + + // --- RankOrder (initialize once, at creation) --- // + { + if (target.getRankOrder() == null) { + + short rankOrder; + + if (computeRankOrder) { + + // Start rank order at 1 + rankOrder = (short) 1; + //FIXME : tchemit-2015-04-04 This code can not be used to save multiple batches at the same time, since it will always give the + //FIXME : tchemit-2015-04-04 same values for all batches + if (source.getParentBatch() != null && CollectionUtils.isNotEmpty(source.getParentBatch().getChildBatchs())) { + int maxRankOrder = 0; + for (SpeciesBatch batch : source.getParentBatch().getChildBatchs()) { + Integer r = batch.getRankOrder(); + if (r != null && r > maxRankOrder) { + maxRankOrder = r; + } + } + rankOrder += (short) maxRankOrder; + + } else { + + rankOrder = batchHelper.computeRankOrder(target); + + } + + } else { + + Preconditions.checkState(source.getRankOrder() != null, "Not using computeRankOrder requires source rankOrder to be not null, but was on batch: " + source); + rankOrder = (short) (int) source.getRankOrder(); + + } + + target.setRankOrder(rankOrder); + + } + } + + // --- Force subgroup count to '1', as Allegro --- // + target.setSubgroupCount(1f); + + // --- Individual count --- // + target.setIndividualCount(source.getNumber()); + + // --- Comments --- // + target.setComments(source.getComment()); + + // --- Exhaustive inventory (always true under a species batch) --- // + target.setExhaustiveInventory(true); + + // --- Species --- // + { + ReferenceTaxon referenceTaxon; + if (source.getSpecies() == null || parentBatchId != null) { + referenceTaxon = null; + } else { + referenceTaxon = load(ReferenceTaxonImpl.class, source.getSpecies().getReferenceTaxonId()); + } + target.setReferenceTaxon(referenceTaxon); + } + + // --- QualityFlag --- // + { + String qualityFlag; + if (source.isSpeciesToConfirm()) { + qualityFlag = QualityFlagCode.DOUBTFUL.getValue(); + } else { + qualityFlag = QualityFlagCode.NOTQUALIFIED.getValue(); + } + target.setQualityFlag(load(QualityFlagImpl.class, qualityFlag)); + } + + Float weight = source.getWeight(); + Float sampleCategoryWeight = source.getSampleCategoryWeight(); + + // --- Sampling Ratio + QuantificationMeasurement --- // + getBatchTreeHelper().setWeightAndSampleRatio(target, weight, sampleCategoryWeight); + + // --- Sorting measurement --- // + { + Collection<SortingMeasurement> sortingMeasurements = target.getSortingMeasurements(); + Set<SortingMeasurement> notChangedSortingMeasurements = Sets.newHashSet(); + if (sortingMeasurements != null) { + notChangedSortingMeasurements.addAll(sortingMeasurements); + } + + if (source.getSampleCategoryId() != null && source.getSampleCategoryValue() != null) { + Integer pmfmId = source.getSampleCategoryId(); + // Do not store sorting measurement if pmfm = SORTED (already store in an ancestor batch) + if (!pmfmId.equals(PmfmId.SORTED_UNSORTED.getValue())) { + SortingMeasurement sortingMeasurement = measurementPersistenceHelper.setSortingMeasurement( + target, + pmfmId, + source.getSampleCategoryValue()); + notChangedSortingMeasurements.remove(sortingMeasurement); + } + } + if (sortingMeasurements != null) { + sortingMeasurements.removeAll(notChangedSortingMeasurements); + } + } + + } + + private void beanToEntity0(SpeciesBatchFrequency source, + SortingBatch target, + SortingBatch parentBatch, + short rankOrder) { + Preconditions.checkNotNull(source.getBatch()); + Preconditions.checkNotNull(source.getBatch().getId()); + + // If parent and root need to be set + if (target.getId() == null + || target.getRootBatch() == null + || (target.getParentBatch() != null && !target.getParentBatch().getId().equals(parentBatch.getId()))) { + + target.setParentBatch(parentBatch); + target.setRootBatch(parentBatch.getRootBatch()); + } + + // --- RankOrder --- // + target.setRankOrder(rankOrder); + + // --- Individual count --- // + target.setIndividualCount(source.getNumber()); + + // --- Species --- // + target.setReferenceTaxon(null); + + // --- QualityFlag --- // + target.setQualityFlag(parentBatch.getQualityFlag()); + + // --- Exhaustive inventory (always true under a species batch) --- // + target.setExhaustiveInventory(true); + + // --- Sampling Ratio + QuantificationMeasurement --- // + getBatchTreeHelper().setWeightAndSampleRatio(target, source.getWeight(), null); + + // --- Sorting measurement --- // + { + Collection<SortingMeasurement> sortingMeasurements = target.getSortingMeasurements(); + Set<SortingMeasurement> notChangedSortingMeasurements = Sets.newHashSet(); + if (sortingMeasurements != null) { + notChangedSortingMeasurements.addAll(sortingMeasurements); + } + if ((source.getLengthStepCaracteristic() != null && source.getLengthStep() != null)) { + Integer pmfmId = source.getLengthStepCaracteristic().getIdAsInt(); + SortingMeasurement sortingMeasurement = measurementPersistenceHelper.setSortingMeasurement(target, pmfmId, + source.getLengthStep()); + notChangedSortingMeasurements.remove(sortingMeasurement); + } + if (sortingMeasurements != null) { + sortingMeasurements.removeAll(notChangedSortingMeasurements); + } + } + + } + + + private SpeciesBatch entityToBean(SampleCategoryModel sampleCategoryModel, + SortingBatch source, + SpeciesBatch target) { + + Preconditions.checkNotNull(target.getSpecies()); + + target.setId(source.getId().toString()); + + // Rank order + target.setRankOrder(Integer.valueOf(source.getRankOrder())); + + // Individual count + target.setNumber(source.getIndividualCount()); + + // Convert database weight (and sampling ratio) into UI weight and sampleCategoryWeight + if (source.getWeight() != null && source.getWeightBeforeSampling() == null) { + target.setSampleCategoryWeight(source.getWeight()); + } else { + target.setWeight(source.getWeight()); + target.setSampleCategoryWeight(source.getWeightBeforeSampling()); + +// if (Objects.equals(source.getWeight(), source.getWeightBeforeSampling())) { +// +// // after a allegro synchronize, can happen, we do not use quantification measurement on a not leaf node +// // the weight comes from sampleRatioText, but in facts there only one weight... +// target.setWeight(null); +// +// } + } + +// if (CollectionUtils.isNotEmpty(source.getChildBatchs()) && target.getWeight() != null) { +// +// // can't use this sample weight on a node +// // the weight comes from sampleRatioText, but must NOT be used here +// target.setWeight(null); +// +// } + + + // Comments + target.setComment(source.getComments()); + + // Sample category type (only one is applied) + SortingMeasurement sm = null; + if (source.getSortingMeasurements().size() == 1) { + sm = source.getSortingMeasurements().iterator().next(); + } else if (source.getReferenceTaxon() != null && source.getReferenceTaxon().getId() != null) { + sm = measurementPersistenceHelper.getInheritedSortingMeasurement(source); + } + if (sm != null) { + + boolean isFrequency = isFrequencyBatch(sampleCategoryModel, source); + + if (!isFrequency) { + Integer qualitativeId = null; + if (sm.getQualitativeValue() != null) { + qualitativeId = sm.getQualitativeValue().getId(); + } + setSampleCategoryQualitative( + target, + sm.getPmfm().getId(), + sm.getNumericalValue(), + sm.getAlphanumericalValue(), + qualitativeId); + } + } + + if (target.getSampleCategoryId() != null) { + List<SpeciesBatch> targetChilds = Lists.newArrayList(); + for (Batch batch : source.getChildBatchs()) { + SortingBatch sourceChild = (SortingBatch) batch; + SpeciesBatch targetChild = SpeciesBatchs.newInstance(target); + targetChild.setSpecies(target.getSpecies()); + entityToBean(sampleCategoryModel, sourceChild, targetChild); + if (log.isDebugEnabled()) { + log.debug("Loaded CatchBatch (Vrac|Hors Vrac) > Species > " + targetChild.getSpecies().getReferenceTaxonId() + " : " + target.getId()); + } + if (targetChild.getSampleCategoryValue() != null) { + targetChilds.add(targetChild); + targetChild.setParentBatch(target); + } + } + target.setChildBatchs(targetChilds); + + } + + //FIXME tchemit-2014-08-29 We can only do this if not an a leaf node (means with no frequencies...) + //FIXME tchemit-2014-08-29 But need to see if this is really need to do that . + // see https://forge.codelutin.com/issues/5698 + + if (CollectionUtils.isNotEmpty(source.getChildBatchs()) && target.getWeight() != null) { + + SortingBatch childBatch = (SortingBatch) Iterables.get(source.getChildBatchs(), 0); + + boolean isFrequency = isFrequencyBatch(sampleCategoryModel, childBatch); + + if (!isFrequency) { + + // can't use this sample weight on a node + // the weight comes from sampleRatioText, but must NOT be used here + // but we can only do this if childs are not frequencies + + target.setWeight(null); + + } + + } + + QualityFlag qualityFlag = source.getQualityFlag(); + target.setSpeciesToConfirm(qualityFlag != null && QualityFlagCode.DOUBTFUL.getValue().equals(qualityFlag.getCode())); + + return target; + + } + +// private short computeRankOrder(SortingBatch target) { +// +// // Start rank order at 1, nothing before it +// short rankOrder = (short) 1; +// if (target.getParentBatch() != null && target.getParentBatch().getChildBatchs() != null) { +// int maxRankOrder = 0; +// for (Batch batch : target.getParentBatch().getChildBatchs()) { +// Short r = batch.getRankOrder(); +// if (r != null && r > maxRankOrder) { +// maxRankOrder = r; +// } +// } +// rankOrder += maxRankOrder; +// } +// return rankOrder; +// +// } + + private void setSampleCategoryQualitative(SpeciesBatch target, + Integer pmfmId, + Float numericalvalue, + String alphanumericalValue, + Integer qualitativeValueId) { + // skip if null or corresponding to the SORTING_TYPE PMFM (Espèce, Benthos, Plancton, etc.) + if (pmfmId == null || pmfmId.equals(SpeciesBatchTreeHelperSupport.SORTING_TYPE_ID)) { + return; + } + SampleCategoryModel sampleCategoryModel = getSampleCategoryModel(); + + boolean isSamplingCategory = sampleCategoryModel.containsCategoryId(pmfmId); + Preconditions.checkNotNull(isSamplingCategory, "Unable to find corresponding SampleCategoryEnum for PMFM.ID : " + pmfmId); + + target.setSampleCategoryId(pmfmId); + Serializable categoryValue = getSampleCategoryQualitative( + pmfmId, + numericalvalue, + alphanumericalValue, + qualitativeValueId); + target.setSampleCategoryValue(categoryValue); + } + + /** + * Check if the given {@code sortingBatch} is a frequency one. + * + * We test that: + * <ul> + * <li>batch has exactly one measurement</li> + * <li>the measurement pmfm is not a sample category</li> + * </ul> + * + * @param sampleCategoryModel model of authorized sample categories + * @param sortingBatch batch to check + * @return {@code true} if given batch is a frequency batch, + * {@code false} otherwise. + */ + private boolean isFrequencyBatch(SampleCategoryModel sampleCategoryModel, SortingBatch sortingBatch) { + boolean result = false; + if (sortingBatch.getSortingMeasurements().size() == 1) { + SortingMeasurement sm + = sortingBatch.getSortingMeasurements().iterator().next(); + Pmfm pmfm = sm.getPmfm(); + + result = sortingBatch.getIndividualCount() != null && + !sampleCategoryModel.containsCategoryId(pmfm.getId()); + } + return result; + } + + private Serializable getSampleCategoryQualitative(Integer pmfmId, + Float numericalvalue, + String alphanumericalValue, + Integer qualitativeValueId) { + + if (numericalvalue != null) { + return numericalvalue; + } + if (alphanumericalValue != null) { + return alphanumericalValue; + } + + Caracteristic caracteristic = caracteristicService.getCaracteristic(pmfmId); + if (caracteristic == null || caracteristic.getCaracteristicType() != CaracteristicType.QUALITATIVE) { + return null; + } + CaracteristicQualitativeValue value = null; + for (CaracteristicQualitativeValue qv : caracteristic.getQualitativeValue()) { + if (qualitativeValueId.equals(qv.getIdAsInt())) { + value = qv; + break; + } + } + + return value; + } + + private void entityToBatchFrequency(SortingBatch source, + SpeciesBatchFrequency target) { + + target.setId(source.getId()); + + // Rank order + target.setRankOrder(Integer.valueOf(source.getRankOrder())); + + target.setNumber(source.getIndividualCount()); + target.setWeight(source.getWeight()); + + Preconditions.checkState(source.getSortingMeasurements().size() == 1, "SortingBatch [" + source.getId() + "] need exactly one sortingMeasurement (to store the length step category), but had " + source.getSortingMeasurements().size()); + SortingMeasurement sm = source.getSortingMeasurements().iterator().next(); + Preconditions.checkNotNull(sm.getPmfm(), "SortingMeasurement [" + sm.getId() + "] can not have a null pmfm"); + Preconditions.checkNotNull(sm.getPmfm().getId(), "SortingMeasurement [" + sm.getId() + "] can not have a pmfm with null id"); + + // Length step category + Caracteristic lengthStepCaracteristic = caracteristicService.getCaracteristic(sm.getPmfm().getId()); + target.setLengthStepCaracteristic(lengthStepCaracteristic); + + // Length + target.setLengthStep(sm.getNumericalValue()); + } + + private List<SortingBatch> getFrequencyChilds(SortingBatch sortingBatch) { + List<SortingBatch> result = Lists.newArrayList(); + + SampleCategoryModel sampleCategoryModel = getSampleCategoryModel(); + + for (Batch batch : sortingBatch.getChildBatchs()) { + SortingBatch child = (SortingBatch) batch; + if (isFrequencyBatch(sampleCategoryModel, child)) { + result.add(child); + } + } + return result; + } + + private List<SortingBatch> getFrequencies(Integer batchId) { + Preconditions.checkNotNull(batchId); + CatchBatch catchBatch = batchHelper.getRootCatchBatchByBatchId(batchId); + SortingBatch sortingBatch = batchHelper.getSortingBatchById(catchBatch, batchId); + + return getFrequencyChilds(sortingBatch); + } + + private void deleteSpeciesSubBatch(Integer speciesBatchId) { + Preconditions.checkNotNull(speciesBatchId); + + CatchBatch catchBatch = batchHelper.getRootCatchBatchByBatchId(speciesBatchId); + synchronizationStatusHelper.setDirty(catchBatch); + + SortingBatch sortingBatch = batchHelper.getSortingBatchById(catchBatch, speciesBatchId); + + // get his children + Collection<Batch> childBatchs = sortingBatch.getChildBatchs(); + + if (CollectionUtils.isNotEmpty(childBatchs)) { + + for (Batch childBatch : childBatchs) { + + // delete this child and all his children + Integer childBatchId = childBatch.getId(); + + if (log.isDebugEnabled()) { + log.debug("Delete child [" + childBatchId + "] of species batch: " + speciesBatchId); + } + batchHelper.removeWithChildren(childBatchId); + } + } + } + + private void changeBatchSpecies(Integer batchId, Species species) { + + Preconditions.checkNotNull(batchId); + Preconditions.checkNotNull(species); + Preconditions.checkNotNull(species.getReferenceTaxonId()); + + CatchBatch catchBatch = batchHelper.getRootCatchBatchByBatchId(batchId); + synchronizationStatusHelper.setDirty(catchBatch); + + batchHelper.setSortingBatchReferenceTaxon(batchId, species); + } +} diff --git a/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/batch/ScientificCruiseCatchBatchValidator.java b/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/batch/ScientificCruiseCatchBatchValidator.java index c03609b..f0a4ca2 100644 --- a/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/batch/ScientificCruiseCatchBatchValidator.java +++ b/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/batch/ScientificCruiseCatchBatchValidator.java @@ -27,12 +27,10 @@ import fr.ifremer.adagio.core.dao.data.batch.CatchBatch; import fr.ifremer.adagio.core.dao.data.batch.SortingBatch; import fr.ifremer.adagio.core.dao.data.batch.validator.CatchBatchValidationError; import fr.ifremer.tutti.persistence.entities.data.BatchContainer; -import fr.ifremer.tutti.persistence.entities.data.BenthosBatch; import fr.ifremer.tutti.persistence.entities.data.SampleCategoryModel; import fr.ifremer.tutti.persistence.entities.data.SampleCategoryModelEntry; -import fr.ifremer.tutti.persistence.entities.data.SpeciesAbleBatch; import fr.ifremer.tutti.persistence.entities.data.SpeciesBatch; -import fr.ifremer.tutti.persistence.service.util.BatchTreeHelper; +import fr.ifremer.tutti.persistence.service.util.tree.BatchTreeHelper; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.stereotype.Component; @@ -111,14 +109,14 @@ public class ScientificCruiseCatchBatchValidator implements TuttiCatchBatchValid @Override public List<CatchBatchValidationError> validateBenthos(SampleCategoryModel sampleCategoryModel, - BatchContainer<BenthosBatch> benthos) { + BatchContainer<SpeciesBatch> benthos) { List<CatchBatchValidationError> errors = Lists.newArrayList(); Map<Integer, SampleCategoryModelEntry> categoriesById = sampleCategoryModel.getCategoryMap(); - for (BenthosBatch benthosBatch : benthos.getChildren()) { + for (SpeciesBatch benthosBatch : benthos.getChildren()) { // check all sample categories are accepted validateSampleCategoriesUniverse( @@ -255,7 +253,7 @@ public class ScientificCruiseCatchBatchValidator implements TuttiCatchBatchValid protected void validateSampleCategoriesUniverse(SampleCategoryModel sampleCategoryModel, List<CatchBatchValidationError> errors, - SpeciesAbleBatch aBatch, + SpeciesBatch aBatch, String messageKey) { Integer sampleCategoryId = aBatch.getSampleCategoryId(); @@ -273,7 +271,7 @@ public class ScientificCruiseCatchBatchValidator implements TuttiCatchBatchValid if (!aBatch.isChildBatchsEmpty()) { - for (SpeciesAbleBatch speciesAbleBatch : aBatch.getChildBatchs()) { + for (SpeciesBatch speciesAbleBatch : aBatch.getChildBatchs()) { validateSampleCategoriesUniverse(sampleCategoryModel, errors, @@ -285,7 +283,7 @@ public class ScientificCruiseCatchBatchValidator implements TuttiCatchBatchValid protected void validateSampleCategoriesOrder(Map<Integer, SampleCategoryModelEntry> categoriesById, List<CatchBatchValidationError> errors, - SpeciesAbleBatch aBatch, + SpeciesBatch aBatch, SampleCategoryModelEntry lastSampleCategory, String messageKey) { @@ -320,7 +318,7 @@ public class ScientificCruiseCatchBatchValidator implements TuttiCatchBatchValid if (!aBatch.isChildBatchsEmpty()) { - for (SpeciesAbleBatch speciesAbleBatch : aBatch.getChildBatchs()) { + for (SpeciesBatch speciesAbleBatch : aBatch.getChildBatchs()) { validateSampleCategoriesOrder( categoriesById, diff --git a/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/batch/TuttiCatchBatchValidator.java b/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/batch/TuttiCatchBatchValidator.java index f1b77b2..5ff711b 100644 --- a/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/batch/TuttiCatchBatchValidator.java +++ b/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/batch/TuttiCatchBatchValidator.java @@ -25,7 +25,6 @@ package fr.ifremer.tutti.persistence.service.batch; import fr.ifremer.adagio.core.dao.data.batch.validator.CatchBatchValidationError; import fr.ifremer.adagio.core.dao.data.batch.validator.CatchBatchValidator; import fr.ifremer.tutti.persistence.entities.data.BatchContainer; -import fr.ifremer.tutti.persistence.entities.data.BenthosBatch; import fr.ifremer.tutti.persistence.entities.data.SampleCategoryModel; import fr.ifremer.tutti.persistence.entities.data.SpeciesBatch; @@ -43,5 +42,5 @@ public interface TuttiCatchBatchValidator extends CatchBatchValidator { BatchContainer<SpeciesBatch> species); List<CatchBatchValidationError> validateBenthos(SampleCategoryModel sampleCategoryModel, - BatchContainer<BenthosBatch> benthos); + BatchContainer<SpeciesBatch> benthos); } diff --git a/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/referential/CaracteristicPersistenceService.java b/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/referential/CaracteristicPersistenceService.java index e727fad..5f0595e 100644 --- a/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/referential/CaracteristicPersistenceService.java +++ b/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/referential/CaracteristicPersistenceService.java @@ -27,7 +27,7 @@ package fr.ifremer.tutti.persistence.service.referential; import com.google.common.base.Predicate; import fr.ifremer.tutti.persistence.TuttiPersistenceServiceImplementor; import fr.ifremer.tutti.persistence.entities.data.SampleCategoryModel; -import fr.ifremer.tutti.persistence.entities.data.SpeciesAbleBatch; +import fr.ifremer.tutti.persistence.entities.data.SpeciesBatch; import fr.ifremer.tutti.persistence.entities.referential.Caracteristic; import org.springframework.cache.annotation.Cacheable; import org.springframework.transaction.annotation.Transactional; @@ -109,10 +109,10 @@ public interface CaracteristicPersistenceService extends TuttiPersistenceService @Cacheable(value = "pmfmById", key = "#pmfmId") Caracteristic getCaracteristic(Integer pmfmId); - Predicate<SpeciesAbleBatch> getVracBatchPredicate(); + Predicate<SpeciesBatch> getVracBatchPredicate(); - boolean isVracBatch(SpeciesAbleBatch speciesBatch); + boolean isVracBatch(SpeciesBatch speciesBatch); - boolean isHorsVracBatch(SpeciesAbleBatch speciesBatch); + boolean isHorsVracBatch(SpeciesBatch speciesBatch); } diff --git a/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/referential/CaracteristicPersistenceServiceImpl.java b/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/referential/CaracteristicPersistenceServiceImpl.java index 0c233ee..6bafa05 100644 --- a/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/referential/CaracteristicPersistenceServiceImpl.java +++ b/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/referential/CaracteristicPersistenceServiceImpl.java @@ -31,8 +31,8 @@ import com.google.common.collect.Sets; import fr.ifremer.adagio.core.dao.referential.pmfm.PmfmId; import fr.ifremer.adagio.core.dao.referential.pmfm.QualitativeValueId; import fr.ifremer.adagio.core.dao.referential.pmfm.UnitId; -import fr.ifremer.tutti.persistence.entities.data.SpeciesAbleBatch; -import fr.ifremer.tutti.persistence.entities.data.SpeciesAbleBatchs; +import fr.ifremer.tutti.persistence.entities.data.SpeciesBatch; +import fr.ifremer.tutti.persistence.entities.data.SpeciesBatchs; import fr.ifremer.tutti.persistence.entities.referential.Caracteristic; import fr.ifremer.tutti.persistence.entities.referential.CaracteristicQualitativeValue; import fr.ifremer.tutti.persistence.entities.referential.CaracteristicQualitativeValues; @@ -68,13 +68,13 @@ public class CaracteristicPersistenceServiceImpl extends ReferentialPersistenceS protected Set<Integer> propertedPmfmIds; - protected Predicate<SpeciesAbleBatch> batchVracPredicate; + protected Predicate<SpeciesBatch> batchVracPredicate; @Override public void init() { super.init(); - batchVracPredicate = SpeciesAbleBatchs.newSpeciesAbleBatchCategoryPredicate( + batchVracPredicate = SpeciesBatchs.newSpeciesAbleBatchCategoryPredicate( PmfmId.SORTED_UNSORTED.getValue(), QualitativeValueId.SORTED_VRAC.getValue()); @@ -259,17 +259,17 @@ public class CaracteristicPersistenceServiceImpl extends ReferentialPersistenceS } @Override - public boolean isVracBatch(SpeciesAbleBatch speciesBatch) { + public boolean isVracBatch(SpeciesBatch speciesBatch) { return batchVracPredicate.apply(speciesBatch); } @Override - public boolean isHorsVracBatch(SpeciesAbleBatch speciesBatch) { + public boolean isHorsVracBatch(SpeciesBatch speciesBatch) { return !batchVracPredicate.apply(speciesBatch); } @Override - public Predicate<SpeciesAbleBatch> getVracBatchPredicate() { + public Predicate<SpeciesBatch> getVracBatchPredicate() { return batchVracPredicate; } diff --git a/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/util/BatchPersistenceHelper.java b/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/util/BatchPersistenceHelper.java index ce8b4c4..17e51fa 100644 --- a/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/util/BatchPersistenceHelper.java +++ b/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/util/BatchPersistenceHelper.java @@ -24,7 +24,6 @@ package fr.ifremer.tutti.persistence.service.util; import com.google.common.base.Joiner; import com.google.common.base.Preconditions; -import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import com.google.common.collect.Sets; import fr.ifremer.adagio.core.dao.data.batch.Batch; @@ -34,48 +33,26 @@ import fr.ifremer.adagio.core.dao.data.batch.SortingBatch; import fr.ifremer.adagio.core.dao.data.batch.SortingBatchDao; import fr.ifremer.adagio.core.dao.data.batch.validator.CatchBatchValidationError; import fr.ifremer.adagio.core.dao.data.batch.validator.CatchBatchValidationException; -import fr.ifremer.adagio.core.dao.data.measure.SortingMeasurement; import fr.ifremer.adagio.core.dao.data.operation.FishingOperation; import fr.ifremer.adagio.core.dao.data.operation.FishingOperationDao; import fr.ifremer.adagio.core.dao.referential.ObjectTypeCode; -import fr.ifremer.adagio.core.dao.referential.QualityFlag; -import fr.ifremer.adagio.core.dao.referential.QualityFlagCode; -import fr.ifremer.adagio.core.dao.referential.QualityFlagImpl; -import fr.ifremer.adagio.core.dao.referential.pmfm.Pmfm; import fr.ifremer.adagio.core.dao.referential.pmfm.PmfmId; -import fr.ifremer.adagio.core.dao.referential.pmfm.QualitativeValueId; -import fr.ifremer.adagio.core.dao.referential.taxon.ReferenceTaxon; -import fr.ifremer.adagio.core.dao.referential.taxon.ReferenceTaxonImpl; import fr.ifremer.tutti.persistence.InvalidBatchModelException; import fr.ifremer.tutti.persistence.entities.TuttiEntity; import fr.ifremer.tutti.persistence.entities.data.BatchContainer; -import fr.ifremer.tutti.persistence.entities.data.BenthosBatch; import fr.ifremer.tutti.persistence.entities.data.SampleCategoryModel; -import fr.ifremer.tutti.persistence.entities.data.SpeciesAbleBatch; -import fr.ifremer.tutti.persistence.entities.data.SpeciesAbleBatchFrequency; -import fr.ifremer.tutti.persistence.entities.data.SpeciesAbleBatchs; import fr.ifremer.tutti.persistence.entities.data.SpeciesBatch; -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.persistence.service.AbstractPersistenceService; import fr.ifremer.tutti.persistence.service.AccidentalBatchPersistenceService; import fr.ifremer.tutti.persistence.service.AttachmentPersistenceService; import fr.ifremer.tutti.persistence.service.IndividualObservationBatchPersistenceService; import fr.ifremer.tutti.persistence.service.batch.TuttiCatchBatchValidator; -import fr.ifremer.tutti.persistence.service.referential.CaracteristicPersistenceService; import org.apache.commons.collections4.CollectionUtils; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.springframework.dao.DataIntegrityViolationException; import org.springframework.dao.DataRetrievalFailureException; import org.springframework.stereotype.Component; import javax.annotation.Resource; -import java.io.Serializable; -import java.text.MessageFormat; -import java.util.Collection; import java.util.List; import java.util.Set; import java.util.stream.Collectors; @@ -91,12 +68,12 @@ import static org.nuiton.i18n.I18n.t; @Component("batchPersistenceHelper") public class BatchPersistenceHelper extends AbstractPersistenceService { - /** Logger. */ - private static final Log log = - LogFactory.getLog(BatchPersistenceHelper.class); +// /** Logger. */ +// private static final Log log = +// LogFactory.getLog(BatchPersistenceHelper.class); - @Resource(name = "caracteristicPersistenceService") - private CaracteristicPersistenceService caracteristicService; +// @Resource(name = "caracteristicPersistenceService") +// private CaracteristicPersistenceService caracteristicService; @Resource(name = "attachmentPersistenceService") protected AttachmentPersistenceService attachmentPersistenceService; @@ -122,8 +99,8 @@ public class BatchPersistenceHelper extends AbstractPersistenceService { @Resource(name = "measurementPersistenceHelper") protected MeasurementPersistenceHelper measurementPersistenceHelper; - @Resource(name = "batchTreeHelper") - protected BatchTreeHelper batchTreeHelper; +// @Resource(name = "batchTreeHelper") +// protected BatchTreeHelper batchTreeHelper; @Resource(name = "synchronizationStatusHelper") protected SynchronizationStatusHelper synchronizationStatusHelper; @@ -161,7 +138,7 @@ public class BatchPersistenceHelper extends AbstractPersistenceService { } public void validateBenthos(SampleCategoryModel sampleCategoryModel, - BatchContainer<BenthosBatch> benthos) throws InvalidBatchModelException { + BatchContainer<SpeciesBatch> benthos) throws InvalidBatchModelException { List<CatchBatchValidationError> errors = catchBatchValidator.validateBenthos(sampleCategoryModel, benthos); if (CollectionUtils.isNotEmpty(errors)) { @@ -263,28 +240,28 @@ public class BatchPersistenceHelper extends AbstractPersistenceService { catchBatchDao.updateSortingBatch(sortingBatch, parentCatchBatch); } - public List<SortingBatch> getFrequencyChilds(SortingBatch sortingBatch) { - List<SortingBatch> result = Lists.newArrayList(); - - SampleCategoryModel sampleCategoryModel = getSampleCategoryModel(); - - for (Batch batch : sortingBatch.getChildBatchs()) { - SortingBatch child = (SortingBatch) batch; - if (isFrequencyBatch(sampleCategoryModel, child)) { - result.add(child); - } - } - return result; - } - - public List<SortingBatch> getFrequencies(Integer batchId) { - Preconditions.checkNotNull(batchId); - CatchBatch catchBatch = getRootCatchBatchByBatchId(batchId); - SortingBatch sortingBatch = catchBatchDao.getSortingBatchById( - catchBatch, batchId); - - return getFrequencyChilds(sortingBatch); - } +// public List<SortingBatch> getFrequencyChilds(SortingBatch sortingBatch) { +// List<SortingBatch> result = Lists.newArrayList(); +// +// SampleCategoryModel sampleCategoryModel = getSampleCategoryModel(); +// +// for (Batch batch : sortingBatch.getChildBatchs()) { +// SortingBatch child = (SortingBatch) batch; +// if (isFrequencyBatch(sampleCategoryModel, child)) { +// result.add(child); +// } +// } +// return result; +// } + +// public List<SortingBatch> getFrequencies(Integer batchId) { +// Preconditions.checkNotNull(batchId); +// CatchBatch catchBatch = getRootCatchBatchByBatchId(batchId); +// SortingBatch sortingBatch = catchBatchDao.getSortingBatchById( +// catchBatch, batchId); +// +// return getFrequencyChilds(sortingBatch); +// } public fr.ifremer.adagio.core.dao.data.batch.CatchBatch getRootCatchBatchByFishingOperationId(Integer fishingOperationId, boolean validate) { Preconditions.checkNotNull(fishingOperationId); @@ -333,464 +310,466 @@ public class BatchPersistenceHelper extends AbstractPersistenceService { return result; } - public void setSpeciesBatchParents(Integer sampleCategoryId, - Serializable sampleCategoryValue, - SortingBatch target, - Integer parentBatchId, - CatchBatch catchBatch) { - - Preconditions.checkNotNull(target); - Preconditions.checkNotNull(catchBatch); - - target.setRootBatch(catchBatch); - - SortingBatch parentBatch; - if (parentBatchId != null) { - - // Load existing parent and root - parentBatch = catchBatchDao.getSortingBatchById(catchBatch, parentBatchId); - } else { - - // Or retrieve parent batch, from pmfm id - // Retrieve category type - if (!sampleCategoryId.equals(PmfmId.SORTED_UNSORTED.getValue())) { - throw new DataIntegrityViolationException(MessageFormat.format( - "A species or benthos batch with no parent should have a sampleCategoryType {0} (PMFM.ID={1})", - PmfmId.SORTED_UNSORTED.getValue(), - sampleCategoryId)); - } - - Integer qualitativeValueId = convertSampleCategoryValueIntoQualitativeId(sampleCategoryValue); - - if (QualitativeValueId.SORTED_VRAC.getValue().equals(qualitativeValueId)) { - - // -- Vrac > Species > Alive itemized - parentBatch = batchTreeHelper.getSpeciesVracAliveItemizedRootBatch(catchBatch); - - if (parentBatch == null) { - - // -- Vrac - SortingBatch vracBatch = batchTreeHelper.getVracBatch(catchBatch); - - if (vracBatch == null) { - vracBatch = batchTreeHelper.getOrCreateVracBatch(catchBatch, null, null); - } - - // -- Vrac > Species - SortingBatch vracSpeciesBatch = batchTreeHelper.getSpeciesVracRootBatch(vracBatch); - - if (vracSpeciesBatch == null) { - vracSpeciesBatch = batchTreeHelper.getOrCreateSpeciesVracRootBatch(catchBatch, vracBatch, null); - } - - // -- Vrac > Species > Alive itemized - parentBatch = batchTreeHelper.getOrCreateSpeciesVracAliveItemizedRootBatch(catchBatch, vracSpeciesBatch); - - } - } else if (QualitativeValueId.SORTED_HORS_VRAC.getValue().equals(qualitativeValueId)) { - - // -- Hors Vrac > Species - parentBatch = batchTreeHelper.getSpeciesHorsVracRootBatch(catchBatch); - - if (parentBatch == null) { - - // -- Hors Vrac - SortingBatch horsVracBatch = batchTreeHelper.getOrCreateHorsVracBatch(catchBatch); - - // -- Hors Vrac > Species - parentBatch = batchTreeHelper.getOrCreateSpeciesHorsVracRootBatch(catchBatch, horsVracBatch); - } - } else { - - // not possible - throw new DataIntegrityViolationException("Should have Vrac / Hor Vrac qualitative value, but had: " + qualitativeValueId); - } - - } - - Preconditions.checkNotNull(parentBatch); - target.setParentBatch(parentBatch); - } - - public void setBenthosBatchParents(Integer sampleCategoryType, - Serializable sampleCategoryValue, - SortingBatch target, - Integer parentBatchId, - CatchBatch catchBatch) { - - Preconditions.checkNotNull(target); - Preconditions.checkNotNull(catchBatch); - - target.setRootBatch(catchBatch); - - SortingBatch parentBatch; - if (parentBatchId != null) { - - // Load existing parent and root - parentBatch = catchBatchDao.getSortingBatchById(catchBatch, parentBatchId); - } else { - - // Or retrieve parent batch, from pmfm id - // Retrieve category type - if (!sampleCategoryType.equals(PmfmId.SORTED_UNSORTED.getValue())) { - throw new DataIntegrityViolationException(MessageFormat.format( - "A species or benthos batch with no parent should have a sampleCategoryType {0} (PMFM.ID={1})", - sampleCategoryType, - PmfmId.SORTED_UNSORTED.getValue())); - } - - Integer qualitativeValueId = convertSampleCategoryValueIntoQualitativeId(sampleCategoryValue); - - if (QualitativeValueId.SORTED_VRAC.getValue().equals(qualitativeValueId)) { - - // -- Vrac > Benthos > Alive Itemized - parentBatch = batchTreeHelper.getBenthosVracAliveItemizedRootBatch(catchBatch); - - if (parentBatch == null) { - - // -- Vrac - SortingBatch vracBatch = batchTreeHelper.getVracBatch(catchBatch); - - if (vracBatch == null) { - vracBatch = batchTreeHelper.getOrCreateVracBatch(catchBatch, null, null); - } - - // -- Vrac > Benthos - SortingBatch vracBenthosBatch = batchTreeHelper.getBenthosVracRootBatch(vracBatch); - - if (vracBenthosBatch == null) { - vracBenthosBatch = batchTreeHelper.getOrCreateBenthosVracRootBatch(catchBatch, vracBatch, null); - } - - // -- Vrac > Benthos > Alive itemized - parentBatch = batchTreeHelper.getOrCreateBenthosVracAliveItemizedRootBatch(catchBatch, vracBenthosBatch); - - } - } else if (QualitativeValueId.SORTED_HORS_VRAC.getValue().equals(qualitativeValueId)) { - - // -- Hors Vrac > Benthos - parentBatch = batchTreeHelper.getBenthosHorsVracRootBatch(catchBatch); - - if (parentBatch == null) { - - // -- Hors Vrac - SortingBatch horsVracBatch = batchTreeHelper.getOrCreateHorsVracBatch(catchBatch); - - // -- Hors Vrac > Benthos - parentBatch = batchTreeHelper.getOrCreateBenthosHorsVracRootBatch(catchBatch, horsVracBatch); - } - } else { - - // not possible - throw new DataIntegrityViolationException("Should have Vrac / Hors Vrac qualitative value, but had: " + qualitativeValueId); - } - } - - Preconditions.checkNotNull(parentBatch); - target.setParentBatch(parentBatch); - } - - public void setMarineLitterBatchParents(SortingBatch target, CatchBatch catchBatch) { - - Preconditions.checkNotNull(target); - - // -- Hors Vrac > Marine Litter - SortingBatch parentBatch = batchTreeHelper.getMarineLitterRootBatch(catchBatch); - - if (parentBatch == null) { - - // -- Hors Vrac - SortingBatch horsVracBatch = batchTreeHelper.getOrCreateHorsVracBatch(catchBatch); - - // -- Hors Vrac > Marine Litter - parentBatch = batchTreeHelper.getOrCreateMarineLitterRootBatch(catchBatch, horsVracBatch, null); - } - - target.setParentBatch(parentBatch); - target.setRootBatch(catchBatch); - } - - public void beanToEntity(Integer parentBatchId, - SpeciesAbleBatch source, - SortingBatch target, - boolean computeRankOrder) { - - // --- RankOrder (initialize once, at creation) --- // - { - if (target.getRankOrder() == null) { - - short rankOrder; - - if (computeRankOrder) { - - // Start rank order at 1 - rankOrder = (short) 1; - //FIXME : tchemit-2015-04-04 This code can not be used to save multiple batches at the same time, since it will always give the - //FIXME : tchemit-2015-04-04 same values for all batches - if (source.getParentBatch() != null && CollectionUtils.isNotEmpty(source.getParentBatch().getChildBatchs())) { - int maxRankOrder = 0; - for (SpeciesAbleBatch batch : source.getParentBatch().getChildBatchs()) { - Integer r = batch.getRankOrder(); - if (r != null && r > maxRankOrder) { - maxRankOrder = r; - } - } - rankOrder += (short) maxRankOrder; - - } else { - - rankOrder = computeRankOrder(target); - - } - - } else { - - Preconditions.checkState(source.getRankOrder() != null, "Not using computeRankOrder requires source rankOrder to be not null, but was on batch: " + source); - rankOrder = (short) (int) source.getRankOrder(); - - } - - target.setRankOrder(rankOrder); - - } - } - - // --- Force subgroup count to '1', as Allegro --- // - target.setSubgroupCount(1f); - - // --- Individual count --- // - target.setIndividualCount(source.getNumber()); - - // --- Comments --- // - target.setComments(source.getComment()); - - // --- Exhaustive inventory (always true under a species batch) --- // - target.setExhaustiveInventory(true); - - // --- Species --- // - { - ReferenceTaxon referenceTaxon; - if (source.getSpecies() == null || parentBatchId != null) { - referenceTaxon = null; - } else { - referenceTaxon = load(ReferenceTaxonImpl.class, source.getSpecies().getReferenceTaxonId()); - } - target.setReferenceTaxon(referenceTaxon); - } - - // --- QualityFlag --- // - { - String qualityFlag; - if (source.isSpeciesToConfirm()) { - qualityFlag = QualityFlagCode.DOUBTFUL.getValue(); - } else { - qualityFlag = QualityFlagCode.NOTQUALIFIED.getValue(); - } - target.setQualityFlag(load(QualityFlagImpl.class, qualityFlag)); - } - - Float weight = source.getWeight(); - Float sampleCategoryWeight = source.getSampleCategoryWeight(); - - // --- Sampling Ratio + QuantificationMeasurement --- // - batchTreeHelper.setWeightAndSampleRatio(target, weight, sampleCategoryWeight); - - // --- Sorting measurement --- // - { - Collection<SortingMeasurement> sortingMeasurements = target.getSortingMeasurements(); - Set<SortingMeasurement> notChangedSortingMeasurements = Sets.newHashSet(); - if (sortingMeasurements != null) { - notChangedSortingMeasurements.addAll(sortingMeasurements); - } - - if (source.getSampleCategoryId() != null && source.getSampleCategoryValue() != null) { - Integer pmfmId = source.getSampleCategoryId(); - // Do not store sorting measurement if pmfm = SORTED (already store in an ancestor batch) - if (!pmfmId.equals(PmfmId.SORTED_UNSORTED.getValue())) { - SortingMeasurement sortingMeasurement = measurementPersistenceHelper.setSortingMeasurement( - target, - pmfmId, - source.getSampleCategoryValue()); - notChangedSortingMeasurements.remove(sortingMeasurement); - } - } - if (sortingMeasurements != null) { - sortingMeasurements.removeAll(notChangedSortingMeasurements); - } - } - - } - - public void beanToEntity(SpeciesAbleBatchFrequency source, - SortingBatch target, - SortingBatch parentBatch, - short rankOrder) { - Preconditions.checkNotNull(source.getBatch()); - Preconditions.checkNotNull(source.getBatch().getId()); - - // If parent and root need to be set - if (target.getId() == null - || target.getRootBatch() == null - || (target.getParentBatch() != null && !target.getParentBatch().getId().equals(parentBatch.getId()))) { - - target.setParentBatch(parentBatch); - target.setRootBatch(parentBatch.getRootBatch()); - } - - // --- RankOrder --- // - target.setRankOrder(rankOrder); - - // --- Individual count --- // - target.setIndividualCount(source.getNumber()); - - // --- Species --- // - target.setReferenceTaxon(null); - - // --- QualityFlag --- // - target.setQualityFlag(parentBatch.getQualityFlag()); - - // --- Exhaustive inventory (always true under a species batch) --- // - target.setExhaustiveInventory(true); - - // --- Sampling Ratio + QuantificationMeasurement --- // - batchTreeHelper.setWeightAndSampleRatio(target, source.getWeight(), null); - - // --- Sorting measurement --- // - { - Collection<SortingMeasurement> sortingMeasurements = target.getSortingMeasurements(); - Set<SortingMeasurement> notChangedSortingMeasurements = Sets.newHashSet(); - if (sortingMeasurements != null) { - notChangedSortingMeasurements.addAll(sortingMeasurements); - } - if ((source.getLengthStepCaracteristic() != null && source.getLengthStep() != null)) { - Integer pmfmId = source.getLengthStepCaracteristic().getIdAsInt(); - SortingMeasurement sortingMeasurement = measurementPersistenceHelper.setSortingMeasurement(target, pmfmId, - source.getLengthStep()); - notChangedSortingMeasurements.remove(sortingMeasurement); - } - if (sortingMeasurements != null) { - sortingMeasurements.removeAll(notChangedSortingMeasurements); - } - } - + public void setSortingBatchReferenceTaxon(Integer batchId, Species species) { + catchBatchDao.setSortingBatchReferenceTaxon(String.valueOf(batchId), species.getReferenceTaxonId()); } +// public void setSpeciesBatchParents(Integer sampleCategoryId, +// Serializable sampleCategoryValue, +// SortingBatch target, +// Integer parentBatchId, +// CatchBatch catchBatch) { +// +// Preconditions.checkNotNull(target); +// Preconditions.checkNotNull(catchBatch); +// +// target.setRootBatch(catchBatch); +// +// SortingBatch parentBatch; +// if (parentBatchId != null) { +// +// // Load existing parent and root +// parentBatch = catchBatchDao.getSortingBatchById(catchBatch, parentBatchId); +// } else { +// +// // Or retrieve parent batch, from pmfm id +// // Retrieve category type +// if (!sampleCategoryId.equals(PmfmId.SORTED_UNSORTED.getValue())) { +// throw new DataIntegrityViolationException(MessageFormat.format( +// "A species or benthos batch with no parent should have a sampleCategoryType {0} (PMFM.ID={1})", +// PmfmId.SORTED_UNSORTED.getValue(), +// sampleCategoryId)); +// } +// +// Integer qualitativeValueId = convertSampleCategoryValueIntoQualitativeId(sampleCategoryValue); +// +// if (QualitativeValueId.SORTED_VRAC.getValue().equals(qualitativeValueId)) { +// +// // -- Vrac > Species > Alive itemized +// parentBatch = batchTreeHelper.getSpeciesVracAliveItemizedRootBatch(catchBatch); +// +// if (parentBatch == null) { +// +// // -- Vrac +// SortingBatch vracBatch = batchTreeHelper.getVracBatch(catchBatch); +// +// if (vracBatch == null) { +// vracBatch = batchTreeHelper.getOrCreateVracBatch(catchBatch, null, null); +// } +// +// // -- Vrac > Species +// SortingBatch vracSpeciesBatch = batchTreeHelper.getSpeciesVracRootBatch(vracBatch); +// +// if (vracSpeciesBatch == null) { +// vracSpeciesBatch = batchTreeHelper.getOrCreateSpeciesVracRootBatch(catchBatch, vracBatch, null); +// } +// +// // -- Vrac > Species > Alive itemized +// parentBatch = batchTreeHelper.getOrCreateSpeciesVracAliveItemizedRootBatch(catchBatch, vracSpeciesBatch); +// +// } +// } else if (QualitativeValueId.SORTED_HORS_VRAC.getValue().equals(qualitativeValueId)) { +// +// // -- Hors Vrac > Species +// parentBatch = batchTreeHelper.getSpeciesHorsVracRootBatch(catchBatch); +// +// if (parentBatch == null) { +// +// // -- Hors Vrac +// SortingBatch horsVracBatch = batchTreeHelper.getOrCreateHorsVracBatch(catchBatch); +// +// // -- Hors Vrac > Species +// parentBatch = batchTreeHelper.getOrCreateSpeciesHorsVracRootBatch(catchBatch, horsVracBatch); +// } +// } else { +// +// // not possible +// throw new DataIntegrityViolationException("Should have Vrac / Hors Vrac qualitative value, but had: " + qualitativeValueId); +// } +// +// } +// +// Preconditions.checkNotNull(parentBatch); +// target.setParentBatch(parentBatch); +// } + +// public void setBenthosBatchParents(Integer sampleCategoryType, +// Serializable sampleCategoryValue, +// SortingBatch target, +// Integer parentBatchId, +// CatchBatch catchBatch) { +// +// Preconditions.checkNotNull(target); +// Preconditions.checkNotNull(catchBatch); +// +// target.setRootBatch(catchBatch); +// +// SortingBatch parentBatch; +// if (parentBatchId != null) { +// +// // Load existing parent and root +// parentBatch = catchBatchDao.getSortingBatchById(catchBatch, parentBatchId); +// } else { +// +// // Or retrieve parent batch, from pmfm id +// // Retrieve category type +// if (!sampleCategoryType.equals(PmfmId.SORTED_UNSORTED.getValue())) { +// throw new DataIntegrityViolationException(MessageFormat.format( +// "A species or benthos batch with no parent should have a sampleCategoryType {0} (PMFM.ID={1})", +// sampleCategoryType, +// PmfmId.SORTED_UNSORTED.getValue())); +// } +// +// Integer qualitativeValueId = convertSampleCategoryValueIntoQualitativeId(sampleCategoryValue); +// +// if (QualitativeValueId.SORTED_VRAC.getValue().equals(qualitativeValueId)) { +// +// // -- Vrac > Benthos > Alive Itemized +// parentBatch = batchTreeHelper.getBenthosVracAliveItemizedRootBatch(catchBatch); +// +// if (parentBatch == null) { +// +// // -- Vrac +// SortingBatch vracBatch = batchTreeHelper.getVracBatch(catchBatch); +// +// if (vracBatch == null) { +// vracBatch = batchTreeHelper.getOrCreateVracBatch(catchBatch, null, null); +// } +// +// // -- Vrac > Benthos +// SortingBatch vracBenthosBatch = batchTreeHelper.getBenthosVracRootBatch(vracBatch); +// +// if (vracBenthosBatch == null) { +// vracBenthosBatch = batchTreeHelper.getOrCreateBenthosVracRootBatch(catchBatch, vracBatch, null); +// } +// +// // -- Vrac > Benthos > Alive itemized +// parentBatch = batchTreeHelper.getOrCreateBenthosVracAliveItemizedRootBatch(catchBatch, vracBenthosBatch); +// +// } +// } else if (QualitativeValueId.SORTED_HORS_VRAC.getValue().equals(qualitativeValueId)) { +// +// // -- Hors Vrac > Benthos +// parentBatch = batchTreeHelper.getBenthosHorsVracRootBatch(catchBatch); +// +// if (parentBatch == null) { +// +// // -- Hors Vrac +// SortingBatch horsVracBatch = batchTreeHelper.getOrCreateHorsVracBatch(catchBatch); +// +// // -- Hors Vrac > Benthos +// parentBatch = batchTreeHelper.getOrCreateBenthosHorsVracRootBatch(catchBatch, horsVracBatch); +// } +// } else { +// +// // not possible +// throw new DataIntegrityViolationException("Should have Vrac / Hors Vrac qualitative value, but had: " + qualitativeValueId); +// } +// } +// +// Preconditions.checkNotNull(parentBatch); +// target.setParentBatch(parentBatch); +// } - public <B extends SpeciesAbleBatch> B entityToBean(SampleCategoryModel sampleCategoryModel, - SortingBatch source, - B target) { - - Preconditions.checkNotNull(target.getSpecies()); - - target.setId(source.getId().toString()); - - // Rank order - target.setRankOrder(Integer.valueOf(source.getRankOrder())); - - // Individual count - target.setNumber(source.getIndividualCount()); - - // Convert database weight (and sampling ratio) into UI weight and sampleCategoryWeight - if (source.getWeight() != null && source.getWeightBeforeSampling() == null) { - target.setSampleCategoryWeight(source.getWeight()); - } else { - target.setWeight(source.getWeight()); - target.setSampleCategoryWeight(source.getWeightBeforeSampling()); - -// if (Objects.equals(source.getWeight(), source.getWeightBeforeSampling())) { +// public void setMarineLitterBatchParents(SortingBatch target, CatchBatch catchBatch) { // -// // after a allegro synchronize, can happen, we do not use quantification measurement on a not leaf node -// // the weight comes from sampleRatioText, but in facts there only one weight... -// target.setWeight(null); +// Preconditions.checkNotNull(target); +// +// // -- Hors Vrac > Marine Litter +// SortingBatch parentBatch = batchTreeHelper.getMarineLitterRootBatch(catchBatch); +// +// if (parentBatch == null) { +// +// // -- Hors Vrac +// SortingBatch horsVracBatch = batchTreeHelper.getOrCreateHorsVracBatch(catchBatch); +// +// // -- Hors Vrac > Marine Litter +// parentBatch = batchTreeHelper.getOrCreateMarineLitterRootBatch(catchBatch, horsVracBatch, null); +// } +// +// target.setParentBatch(parentBatch); +// target.setRootBatch(catchBatch); +// } + +// public void beanToEntity(Integer parentBatchId, +// SpeciesBatch source, +// SortingBatch target, +// boolean computeRankOrder) { +// +// // --- RankOrder (initialize once, at creation) --- // +// { +// if (target.getRankOrder() == null) { +// +// short rankOrder; +// +// if (computeRankOrder) { +// +// // Start rank order at 1 +// rankOrder = (short) 1; +// //FIXME : tchemit-2015-04-04 This code can not be used to save multiple batches at the same time, since it will always give the +// //FIXME : tchemit-2015-04-04 same values for all batches +// if (source.getParentBatch() != null && CollectionUtils.isNotEmpty(source.getParentBatch().getChildBatchs())) { +// int maxRankOrder = 0; +// for (SpeciesBatch batch : source.getParentBatch().getChildBatchs()) { +// Integer r = batch.getRankOrder(); +// if (r != null && r > maxRankOrder) { +// maxRankOrder = r; +// } +// } +// rankOrder += (short) maxRankOrder; +// +// } else { +// +// rankOrder = computeRankOrder(target); +// +// } +// +// } else { +// +// Preconditions.checkState(source.getRankOrder() != null, "Not using computeRankOrder requires source rankOrder to be not null, but was on batch: " + source); +// rankOrder = (short) (int) source.getRankOrder(); +// +// } +// +// target.setRankOrder(rankOrder); // // } - } +// } +// +// // --- Force subgroup count to '1', as Allegro --- // +// target.setSubgroupCount(1f); +// +// // --- Individual count --- // +// target.setIndividualCount(source.getNumber()); +// +// // --- Comments --- // +// target.setComments(source.getComment()); +// +// // --- Exhaustive inventory (always true under a species batch) --- // +// target.setExhaustiveInventory(true); +// +// // --- Species --- // +// { +// ReferenceTaxon referenceTaxon; +// if (source.getSpecies() == null || parentBatchId != null) { +// referenceTaxon = null; +// } else { +// referenceTaxon = load(ReferenceTaxonImpl.class, source.getSpecies().getReferenceTaxonId()); +// } +// target.setReferenceTaxon(referenceTaxon); +// } +// +// // --- QualityFlag --- // +// { +// String qualityFlag; +// if (source.isSpeciesToConfirm()) { +// qualityFlag = QualityFlagCode.DOUBTFUL.getValue(); +// } else { +// qualityFlag = QualityFlagCode.NOTQUALIFIED.getValue(); +// } +// target.setQualityFlag(load(QualityFlagImpl.class, qualityFlag)); +// } +// +// Float weight = source.getWeight(); +// Float sampleCategoryWeight = source.getSampleCategoryWeight(); +// +// // --- Sampling Ratio + QuantificationMeasurement --- // +// batchTreeHelper.setWeightAndSampleRatio(target, weight, sampleCategoryWeight); +// +// // --- Sorting measurement --- // +// { +// Collection<SortingMeasurement> sortingMeasurements = target.getSortingMeasurements(); +// Set<SortingMeasurement> notChangedSortingMeasurements = Sets.newHashSet(); +// if (sortingMeasurements != null) { +// notChangedSortingMeasurements.addAll(sortingMeasurements); +// } +// +// if (source.getSampleCategoryId() != null && source.getSampleCategoryValue() != null) { +// Integer pmfmId = source.getSampleCategoryId(); +// // Do not store sorting measurement if pmfm = SORTED (already store in an ancestor batch) +// if (!pmfmId.equals(PmfmId.SORTED_UNSORTED.getValue())) { +// SortingMeasurement sortingMeasurement = measurementPersistenceHelper.setSortingMeasurement( +// target, +// pmfmId, +// source.getSampleCategoryValue()); +// notChangedSortingMeasurements.remove(sortingMeasurement); +// } +// } +// if (sortingMeasurements != null) { +// sortingMeasurements.removeAll(notChangedSortingMeasurements); +// } +// } +// +// } + +// public void beanToEntity(SpeciesBatchFrequency source, +// SortingBatch target, +// SortingBatch parentBatch, +// short rankOrder) { +// Preconditions.checkNotNull(source.getBatch()); +// Preconditions.checkNotNull(source.getBatch().getId()); +// +// // If parent and root need to be set +// if (target.getId() == null +// || target.getRootBatch() == null +// || (target.getParentBatch() != null && !target.getParentBatch().getId().equals(parentBatch.getId()))) { +// +// target.setParentBatch(parentBatch); +// target.setRootBatch(parentBatch.getRootBatch()); +// } +// +// // --- RankOrder --- // +// target.setRankOrder(rankOrder); +// +// // --- Individual count --- // +// target.setIndividualCount(source.getNumber()); +// +// // --- Species --- // +// target.setReferenceTaxon(null); +// +// // --- QualityFlag --- // +// target.setQualityFlag(parentBatch.getQualityFlag()); +// +// // --- Exhaustive inventory (always true under a species batch) --- // +// target.setExhaustiveInventory(true); +// +// // --- Sampling Ratio + QuantificationMeasurement --- // +// batchTreeHelper.setWeightAndSampleRatio(target, source.getWeight(), null); +// +// // --- Sorting measurement --- // +// { +// Collection<SortingMeasurement> sortingMeasurements = target.getSortingMeasurements(); +// Set<SortingMeasurement> notChangedSortingMeasurements = Sets.newHashSet(); +// if (sortingMeasurements != null) { +// notChangedSortingMeasurements.addAll(sortingMeasurements); +// } +// if ((source.getLengthStepCaracteristic() != null && source.getLengthStep() != null)) { +// Integer pmfmId = source.getLengthStepCaracteristic().getIdAsInt(); +// SortingMeasurement sortingMeasurement = measurementPersistenceHelper.setSortingMeasurement(target, pmfmId, +// source.getLengthStep()); +// notChangedSortingMeasurements.remove(sortingMeasurement); +// } +// if (sortingMeasurements != null) { +// sortingMeasurements.removeAll(notChangedSortingMeasurements); +// } +// } +// +// } +// public SpeciesBatch entityToBean(SampleCategoryModel sampleCategoryModel, +// SortingBatch source, +// SpeciesBatch target) { +// +// Preconditions.checkNotNull(target.getSpecies()); +// +// target.setId(source.getId().toString()); +// +// // Rank order +// target.setRankOrder(Integer.valueOf(source.getRankOrder())); +// +// // Individual count +// target.setNumber(source.getIndividualCount()); +// +// // Convert database weight (and sampling ratio) into UI weight and sampleCategoryWeight +// if (source.getWeight() != null && source.getWeightBeforeSampling() == null) { +// target.setSampleCategoryWeight(source.getWeight()); +// } else { +// target.setWeight(source.getWeight()); +// target.setSampleCategoryWeight(source.getWeightBeforeSampling()); +// +//// if (Objects.equals(source.getWeight(), source.getWeightBeforeSampling())) { +//// +//// // after a allegro synchronize, can happen, we do not use quantification measurement on a not leaf node +//// // the weight comes from sampleRatioText, but in facts there only one weight... +//// target.setWeight(null); +//// +//// } +// } +// +//// if (CollectionUtils.isNotEmpty(source.getChildBatchs()) && target.getWeight() != null) { +//// +//// // can't use this sample weight on a node +//// // the weight comes from sampleRatioText, but must NOT be used here +//// target.setWeight(null); +//// +//// } +// +// +// // Comments +// target.setComment(source.getComments()); +// +// // Sample category type (only one is applied) +// SortingMeasurement sm = null; +// if (source.getSortingMeasurements().size() == 1) { +// sm = source.getSortingMeasurements().iterator().next(); +// } else if (source.getReferenceTaxon() != null && source.getReferenceTaxon().getId() != null) { +// sm = measurementPersistenceHelper.getInheritedSortingMeasurement(source); +// } +// if (sm != null) { +// +// boolean isFrequency = isFrequencyBatch(sampleCategoryModel, source); +// +// if (!isFrequency) { +// Integer qualitativeId = null; +// if (sm.getQualitativeValue() != null) { +// qualitativeId = sm.getQualitativeValue().getId(); +// } +// setSampleCategoryQualitative( +// target, +// sm.getPmfm().getId(), +// sm.getNumericalValue(), +// sm.getAlphanumericalValue(), +// qualitativeId); +// } +// } +// +// if (target.getSampleCategoryId() != null) { +// List<SpeciesBatch> targetChilds = Lists.newArrayList(); +// for (Batch batch : source.getChildBatchs()) { +// SortingBatch sourceChild = (SortingBatch) batch; +// SpeciesBatch targetChild = SpeciesBatchs.newInstance(target); +// targetChild.setSpecies(target.getSpecies()); +// entityToBean(sampleCategoryModel, sourceChild, targetChild); +// if (log.isDebugEnabled()) { +// log.debug("Loaded CatchBatch (Vrac|Hors Vrac) > Species > " + targetChild.getSpecies().getReferenceTaxonId() + " : " + target.getId()); +// } +// if (targetChild.getSampleCategoryValue() != null) { +// targetChilds.add(targetChild); +// targetChild.setParentBatch(target); +// } +// } +// target.setChildBatchs(targetChilds); +// +// } +// +// //FIXME tchemit-2014-08-29 We can only do this if not an a leaf node (means with no frequencies...) +// //FIXME tchemit-2014-08-29 But need to see if this is really need to do that . +// // see https://forge.codelutin.com/issues/5698 +// // if (CollectionUtils.isNotEmpty(source.getChildBatchs()) && target.getWeight() != null) { // -// // can't use this sample weight on a node -// // the weight comes from sampleRatioText, but must NOT be used here -// target.setWeight(null); +// SortingBatch childBatch = (SortingBatch) Iterables.get(source.getChildBatchs(), 0); +// +// boolean isFrequency = isFrequencyBatch(sampleCategoryModel, childBatch); +// +// if (!isFrequency) { +// +// // can't use this sample weight on a node +// // the weight comes from sampleRatioText, but must NOT be used here +// // but we can only do this if childs are not frequencies +// +// target.setWeight(null); +// +// } // // } - - - // Comments - target.setComment(source.getComments()); - - // Sample category type (only one is applied) - SortingMeasurement sm = null; - if (source.getSortingMeasurements().size() == 1) { - sm = source.getSortingMeasurements().iterator().next(); - } else if (source.getReferenceTaxon() != null && source.getReferenceTaxon().getId() != null) { - sm = measurementPersistenceHelper.getInheritedSortingMeasurement(source); - } - if (sm != null) { - - boolean isFrequency = isFrequencyBatch(sampleCategoryModel, source); - - if (!isFrequency) { - Integer qualitativeId = null; - if (sm.getQualitativeValue() != null) { - qualitativeId = sm.getQualitativeValue().getId(); - } - setSampleCategoryQualitative( - target, - sm.getPmfm().getId(), - sm.getNumericalValue(), - sm.getAlphanumericalValue(), - qualitativeId); - } - } - - if (target.getSampleCategoryId() != null) { - List<B> targetChilds = Lists.newArrayList(); - for (Batch batch : source.getChildBatchs()) { - SortingBatch sourceChild = (SortingBatch) batch; - B targetChild = SpeciesAbleBatchs.newInstance(target); - targetChild.setSpecies(target.getSpecies()); - entityToBean(sampleCategoryModel, sourceChild, targetChild); - if (log.isDebugEnabled()) { - log.debug("Loaded CatchBatch (Vrac|Hors Vrac) > Species > " + targetChild.getSpecies().getReferenceTaxonId() + " : " + target.getId()); - } - if (targetChild.getSampleCategoryValue() != null) { - targetChilds.add(targetChild); - SpeciesAbleBatchs.setParentBatch(target, targetChild); - } - } - - SpeciesAbleBatchs.setChildBatchs(target, targetChilds); - - } - - //FIXME tchemit-2014-08-29 We can only do this if not an a leaf node (means with no frequencies...) - //FIXME tchemit-2014-08-29 But need to see if this is really need to do that . - // see https://forge.codelutin.com/issues/5698 - - if (CollectionUtils.isNotEmpty(source.getChildBatchs()) && target.getWeight() != null) { - - SortingBatch childBatch = (SortingBatch) Iterables.get(source.getChildBatchs(), 0); - - boolean isFrequency = isFrequencyBatch(sampleCategoryModel, childBatch); - - if (!isFrequency) { - - // can't use this sample weight on a node - // the weight comes from sampleRatioText, but must NOT be used here - // but we can only do this if childs are not frequencies - - target.setWeight(null); - - } - - } - - QualityFlag qualityFlag = source.getQualityFlag(); - target.setSpeciesToConfirm(qualityFlag != null && QualityFlagCode.DOUBTFUL.getValue().equals(qualityFlag.getCode())); - - return target; - - } +// +// QualityFlag qualityFlag = source.getQualityFlag(); +// target.setSpeciesToConfirm(qualityFlag != null && QualityFlagCode.DOUBTFUL.getValue().equals(qualityFlag.getCode())); +// +// return target; +// +// } public short computeRankOrder(SortingBatch target) { @@ -810,19 +789,19 @@ public class BatchPersistenceHelper extends AbstractPersistenceService { } - public Integer convertSampleCategoryValueIntoQualitativeId(Serializable value) { - if (value == null) { - return null; - } - Integer qualitativeValueId = null; - if (value instanceof CaracteristicQualitativeValue) { - CaracteristicQualitativeValue cqValue = (CaracteristicQualitativeValue) value; - qualitativeValueId = cqValue.getIdAsInt(); - } else if (value instanceof String) { - qualitativeValueId = Integer.valueOf((String) value); - } - return qualitativeValueId; - } +// public Integer convertSampleCategoryValueIntoQualitativeId(Serializable value) { +// if (value == null) { +// return null; +// } +// Integer qualitativeValueId = null; +// if (value instanceof CaracteristicQualitativeValue) { +// CaracteristicQualitativeValue cqValue = (CaracteristicQualitativeValue) value; +// qualitativeValueId = cqValue.getIdAsInt(); +// } else if (value instanceof String) { +// qualitativeValueId = Integer.valueOf((String) value); +// } +// return qualitativeValueId; +// } public void deleteBatch(Integer batchId) { Preconditions.checkNotNull(batchId); @@ -833,144 +812,144 @@ public class BatchPersistenceHelper extends AbstractPersistenceService { removeWithChildren(batchId); } - public void deleteSpeciesSubBatch(Integer speciesBatchId) { - Preconditions.checkNotNull(speciesBatchId); - - CatchBatch catchBatch = getRootCatchBatchByBatchId(speciesBatchId); - synchronizationStatusHelper.setDirty(catchBatch); - - SortingBatch sortingBatch = getSortingBatchById(catchBatch, speciesBatchId); - - // get his children - Collection<Batch> childBatchs = sortingBatch.getChildBatchs(); - - if (CollectionUtils.isNotEmpty(childBatchs)) { - - for (Batch childBatch : childBatchs) { - - // delete this child and all his children - Integer childBatchId = childBatch.getId(); - - if (log.isDebugEnabled()) { - log.debug("Delete child [" + childBatchId + "] of species batch: " + speciesBatchId); - } - removeWithChildren(childBatchId); - } - } - } - - public void changeBatchSpecies(Integer batchId, Species species) { - - Preconditions.checkNotNull(batchId); - Preconditions.checkNotNull(species); - Preconditions.checkNotNull(species.getReferenceTaxonId()); - - CatchBatch catchBatch = getRootCatchBatchByBatchId(batchId); - synchronizationStatusHelper.setDirty(catchBatch); - - catchBatchDao.setSortingBatchReferenceTaxon(String.valueOf(batchId), species.getReferenceTaxonId()); - } - - public <S extends SpeciesAbleBatch> void setSampleCategoryQualitative(S target, - Integer pmfmId, - Float numericalvalue, - String alphanumericalValue, - Integer qualitativeValueId) { - // skip if null or corresponding to the SORTING_TYPE PMFM (Espèce, Benthos, Plancton, etc.) - if (pmfmId == null || pmfmId.equals(BatchTreeHelper.SORTING_TYPE_ID)) { - return; - } - SampleCategoryModel sampleCategoryModel = getSampleCategoryModel(); - - boolean isSamplingCategory = sampleCategoryModel.containsCategoryId(pmfmId); - Preconditions.checkNotNull(isSamplingCategory, "Unable to find corresponding SampleCategoryEnum for PMFM.ID : " + pmfmId); - - target.setSampleCategoryId(pmfmId); - Serializable categoryValue = getSampleCategoryQualitative( - pmfmId, - numericalvalue, - alphanumericalValue, - qualitativeValueId); - target.setSampleCategoryValue(categoryValue); - } - - public <F extends SpeciesAbleBatchFrequency & TuttiEntity> void entityToBatchFrequency(SortingBatch source, - F target) { - - target.setId(source.getId()); - - // Rank order - target.setRankOrder(Integer.valueOf(source.getRankOrder())); - - target.setNumber(source.getIndividualCount()); - target.setWeight(source.getWeight()); - - Preconditions.checkState(source.getSortingMeasurements().size() == 1, "SortingBatch [" + source.getId() + "] need exactly one sortingMeasurement (to store the length step category), but had " + source.getSortingMeasurements().size()); - SortingMeasurement sm = source.getSortingMeasurements().iterator().next(); - Preconditions.checkNotNull(sm.getPmfm(), "SortingMeasurement [" + sm.getId() + "] can not have a null pmfm"); - Preconditions.checkNotNull(sm.getPmfm().getId(), "SortingMeasurement [" + sm.getId() + "] can not have a pmfm with null id"); - - // Length step category - Caracteristic lengthStepCaracteristic = caracteristicService.getCaracteristic(sm.getPmfm().getId()); - target.setLengthStepCaracteristic(lengthStepCaracteristic); - - // Length - target.setLengthStep(sm.getNumericalValue()); - } - - public Serializable getSampleCategoryQualitative(Integer pmfmId, - Float numericalvalue, - String alphanumericalValue, - Integer qualitativeValueId) { - - if (numericalvalue != null) { - return numericalvalue; - } - if (alphanumericalValue != null) { - return alphanumericalValue; - } - - Caracteristic caracteristic = caracteristicService.getCaracteristic(pmfmId); - if (caracteristic == null || caracteristic.getCaracteristicType() != CaracteristicType.QUALITATIVE) { - return null; - } - CaracteristicQualitativeValue value = null; - for (CaracteristicQualitativeValue qv : caracteristic.getQualitativeValue()) { - if (qualitativeValueId.equals(qv.getIdAsInt())) { - value = qv; - break; - } - } - - return value; - } +// public void deleteSpeciesSubBatch(Integer speciesBatchId) { +// Preconditions.checkNotNull(speciesBatchId); +// +// CatchBatch catchBatch = getRootCatchBatchByBatchId(speciesBatchId); +// synchronizationStatusHelper.setDirty(catchBatch); +// +// SortingBatch sortingBatch = getSortingBatchById(catchBatch, speciesBatchId); +// +// // get his children +// Collection<Batch> childBatchs = sortingBatch.getChildBatchs(); +// +// if (CollectionUtils.isNotEmpty(childBatchs)) { +// +// for (Batch childBatch : childBatchs) { +// +// // delete this child and all his children +// Integer childBatchId = childBatch.getId(); +// +// if (log.isDebugEnabled()) { +// log.debug("Delete child [" + childBatchId + "] of species batch: " + speciesBatchId); +// } +// removeWithChildren(childBatchId); +// } +// } +// } - /** - * Check if the given {@code sortingBatch} is a frequency one. - * - * We test that: - * <ul> - * <li>batch has exactly one measurement</li> - * <li>the measurement pmfm is not a sample category</li> - * </ul> - * - * @param sampleCategoryModel model of authorized sample categories - * @param sortingBatch batch to check - * @return {@code true} if given batch is a frequency batch, - * {@code false} otherwise. - */ - public boolean isFrequencyBatch(SampleCategoryModel sampleCategoryModel, - SortingBatch sortingBatch) { - boolean result = false; - if (sortingBatch.getSortingMeasurements().size() == 1) { - SortingMeasurement sm - = sortingBatch.getSortingMeasurements().iterator().next(); - Pmfm pmfm = sm.getPmfm(); - - result = sortingBatch.getIndividualCount() != null && - !sampleCategoryModel.containsCategoryId(pmfm.getId()); - } - return result; - } +// public void changeBatchSpecies(Integer batchId, Species species) { +// +// Preconditions.checkNotNull(batchId); +// Preconditions.checkNotNull(species); +// Preconditions.checkNotNull(species.getReferenceTaxonId()); +// +// CatchBatch catchBatch = getRootCatchBatchByBatchId(batchId); +// synchronizationStatusHelper.setDirty(catchBatch); +// +// catchBatchDao.setSortingBatchReferenceTaxon(String.valueOf(batchId), species.getReferenceTaxonId()); +// } + +// public void setSampleCategoryQualitative(SpeciesBatch target, +// Integer pmfmId, +// Float numericalvalue, +// String alphanumericalValue, +// Integer qualitativeValueId) { +// // skip if null or corresponding to the SORTING_TYPE PMFM (Espèce, Benthos, Plancton, etc.) +// if (pmfmId == null || pmfmId.equals(BatchTreeHelper.SORTING_TYPE_ID)) { +// return; +// } +// SampleCategoryModel sampleCategoryModel = getSampleCategoryModel(); +// +// boolean isSamplingCategory = sampleCategoryModel.containsCategoryId(pmfmId); +// Preconditions.checkNotNull(isSamplingCategory, "Unable to find corresponding SampleCategoryEnum for PMFM.ID : " + pmfmId); +// +// target.setSampleCategoryId(pmfmId); +// Serializable categoryValue = getSampleCategoryQualitative( +// pmfmId, +// numericalvalue, +// alphanumericalValue, +// qualitativeValueId); +// target.setSampleCategoryValue(categoryValue); +// } + +// public void entityToBatchFrequency(SortingBatch source, +// SpeciesBatchFrequency target) { +// +// target.setId(source.getId()); +// +// // Rank order +// target.setRankOrder(Integer.valueOf(source.getRankOrder())); +// +// target.setNumber(source.getIndividualCount()); +// target.setWeight(source.getWeight()); +// +// Preconditions.checkState(source.getSortingMeasurements().size() == 1, "SortingBatch [" + source.getId() + "] need exactly one sortingMeasurement (to store the length step category), but had " + source.getSortingMeasurements().size()); +// SortingMeasurement sm = source.getSortingMeasurements().iterator().next(); +// Preconditions.checkNotNull(sm.getPmfm(), "SortingMeasurement [" + sm.getId() + "] can not have a null pmfm"); +// Preconditions.checkNotNull(sm.getPmfm().getId(), "SortingMeasurement [" + sm.getId() + "] can not have a pmfm with null id"); +// +// // Length step category +// Caracteristic lengthStepCaracteristic = caracteristicService.getCaracteristic(sm.getPmfm().getId()); +// target.setLengthStepCaracteristic(lengthStepCaracteristic); +// +// // Length +// target.setLengthStep(sm.getNumericalValue()); +// } + +// public Serializable getSampleCategoryQualitative(Integer pmfmId, +// Float numericalvalue, +// String alphanumericalValue, +// Integer qualitativeValueId) { +// +// if (numericalvalue != null) { +// return numericalvalue; +// } +// if (alphanumericalValue != null) { +// return alphanumericalValue; +// } +// +// Caracteristic caracteristic = caracteristicService.getCaracteristic(pmfmId); +// if (caracteristic == null || caracteristic.getCaracteristicType() != CaracteristicType.QUALITATIVE) { +// return null; +// } +// CaracteristicQualitativeValue value = null; +// for (CaracteristicQualitativeValue qv : caracteristic.getQualitativeValue()) { +// if (qualitativeValueId.equals(qv.getIdAsInt())) { +// value = qv; +// break; +// } +// } +// +// return value; +// } + +// /** +// * Check if the given {@code sortingBatch} is a frequency one. +// * +// * We test that: +// * <ul> +// * <li>batch has exactly one measurement</li> +// * <li>the measurement pmfm is not a sample category</li> +// * </ul> +// * +// * @param sampleCategoryModel model of authorized sample categories +// * @param sortingBatch batch to check +// * @return {@code true} if given batch is a frequency batch, +// * {@code false} otherwise. +// */ +// public boolean isFrequencyBatch(SampleCategoryModel sampleCategoryModel, +// SortingBatch sortingBatch) { +// boolean result = false; +// if (sortingBatch.getSortingMeasurements().size() == 1) { +// SortingMeasurement sm +// = sortingBatch.getSortingMeasurements().iterator().next(); +// Pmfm pmfm = sm.getPmfm(); +// +// result = sortingBatch.getIndividualCount() != null && +// !sampleCategoryModel.containsCategoryId(pmfm.getId()); +// } +// return result; +// } } diff --git a/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/util/BatchTreeHelper.java b/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/util/BatchTreeHelper.java deleted file mode 100644 index c441b01..0000000 --- a/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/util/BatchTreeHelper.java +++ /dev/null @@ -1,713 +0,0 @@ -package fr.ifremer.tutti.persistence.service.util; - -/* - * #%L - * Tutti :: Persistence - * %% - * Copyright (C) 2012 - 2014 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.Joiner; -import com.google.common.collect.Sets; -import fr.ifremer.adagio.core.dao.data.batch.Batch; -import fr.ifremer.adagio.core.dao.data.batch.Batchs; -import fr.ifremer.adagio.core.dao.data.batch.CatchBatch; -import fr.ifremer.adagio.core.dao.data.batch.CatchBatchExtendDao; -import fr.ifremer.adagio.core.dao.data.batch.SortingBatch; -import fr.ifremer.adagio.core.dao.data.measure.QuantificationMeasurement; -import fr.ifremer.adagio.core.dao.data.measure.SortingMeasurement; -import fr.ifremer.adagio.core.dao.referential.QualityFlagCode; -import fr.ifremer.adagio.core.dao.referential.QualityFlagImpl; -import fr.ifremer.adagio.core.dao.referential.pmfm.PmfmId; -import fr.ifremer.adagio.core.dao.referential.pmfm.QualitativeValueId; -import fr.ifremer.adagio.core.dao.referential.taxon.ReferenceTaxon; -import fr.ifremer.adagio.core.dao.referential.taxon.ReferenceTaxonImpl; -import fr.ifremer.tutti.persistence.service.AbstractPersistenceService; -import org.apache.commons.lang3.StringUtils; -import org.apache.commons.lang3.SystemUtils; -import org.apache.commons.lang3.builder.ToStringBuilder; -import org.apache.commons.lang3.builder.ToStringStyle; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.springframework.stereotype.Component; - -import javax.annotation.Resource; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.Comparator; -import java.util.List; -import java.util.Set; - -/** - * Helper to build or navigauet into the batch tree. - * - * Created on 4/20/14. - * - * @author Tony Chemit - chemit@codelutin.com - * @since 3.5 - */ -@Component("batchTreeHelper") -public class BatchTreeHelper extends AbstractPersistenceService { - - public static final Integer SORTING_TYPE_ID = PmfmId.SCIENTIFIC_CRUISE_SORTING_TYPE.getValue(); - - public static final Integer SORTING_TYPE2_ID = PmfmId.SCIENTIFIC_CRUISE_SORTING_TYPE2.getValue(); - - /** Logger. */ - private static final Log log = LogFactory.getLog(BatchTreeHelper.class); - - @Resource(name = "measurementPersistenceHelper") - protected MeasurementPersistenceHelper measurementPersistenceHelper; - - @Resource(name = "catchBatchDao") - protected CatchBatchExtendDao catchBatchDao; - - protected Comparator<Batch> batchComparator = Batchs.newRankOrderComparator(); - - //------------------------------------------------------------------------// - //-- Get CatchBatch navigation methods --// - //------------------------------------------------------------------------// - - public SortingBatch getSpeciesVracAliveItemizedRootBatch(CatchBatch batch) { - return getSortingBatch( - batch, - "Vrac > Species > Alive Itemized", - PmfmId.SORTED_UNSORTED.getValue(), QualitativeValueId.SORTED_VRAC.getValue(), - SORTING_TYPE_ID, QualitativeValueId.SORTING_TYPE_SPECIES.getValue(), - SORTING_TYPE2_ID, QualitativeValueId.SORTING_TYPE2_ALIVE_ITEMIZED.getValue() - ); - } - - public SortingBatch getBenthosVracAliveItemizedRootBatch(CatchBatch batch) { - return getSortingBatch( - batch, - "Vrac > Benthos > Alive itemized", - PmfmId.SORTED_UNSORTED.getValue(), QualitativeValueId.SORTED_VRAC.getValue(), - SORTING_TYPE_ID, QualitativeValueId.SORTING_TYPE_BENTHOS.getValue(), - SORTING_TYPE2_ID, QualitativeValueId.SORTING_TYPE2_ALIVE_ITEMIZED.getValue() - ); - } - - public SortingBatch getSpeciesHorsVracRootBatch(CatchBatch batch) { - return getSortingBatch( - batch, - "Hors Vrac > Species", - PmfmId.SORTED_UNSORTED.getValue(), QualitativeValueId.SORTED_HORS_VRAC.getValue(), - SORTING_TYPE_ID, QualitativeValueId.SORTING_TYPE_SPECIES.getValue() - ); - } - - public SortingBatch getBenthosHorsVracRootBatch(CatchBatch batch) { - return getSortingBatch( - batch, - "Hors Vrac > Benthos", - PmfmId.SORTED_UNSORTED.getValue(), QualitativeValueId.SORTED_HORS_VRAC.getValue(), - SORTING_TYPE_ID, QualitativeValueId.SORTING_TYPE_BENTHOS.getValue() - ); - } - - public SortingBatch getMarineLitterRootBatch(CatchBatch batch) { - return getSortingBatch( - batch, - "Hors Vrac > Marine Litter", - PmfmId.SORTED_UNSORTED.getValue(), - QualitativeValueId.SORTED_HORS_VRAC.getValue(), - SORTING_TYPE_ID, - QualitativeValueId.SORTING_TYPE_MARINE_LITTER.getValue() - ); - } - - public SortingBatch getVracBatch(CatchBatch batch) { - return getSortingBatch( - batch, - "Vrac", - PmfmId.SORTED_UNSORTED.getValue(), - QualitativeValueId.SORTED_VRAC.getValue() - ); - } - - public SortingBatch getHorsVracBatch(CatchBatch batch) { - return getSortingBatch( - batch, - "Hors Vrac", - PmfmId.SORTED_UNSORTED.getValue(), - QualitativeValueId.SORTED_HORS_VRAC.getValue() - ); - } - - public SortingBatch getRejectedBatch(CatchBatch batch) { - return getSortingBatch( - batch, - "Unsorted", - PmfmId.SORTED_UNSORTED.getValue(), - QualitativeValueId.UNSORTED.getValue() - ); - } - - //------------------------------------------------------------------------// - //-- Get SortingBatch navigation methods --// - //------------------------------------------------------------------------// - - public SortingBatch getSpeciesVracRootBatch(SortingBatch batch) { - return getSortingBatch( - batch, - "Vrac > Species", - SORTING_TYPE_ID, - QualitativeValueId.SORTING_TYPE_SPECIES.getValue() - ); - } - - public SortingBatch getBenthosVracRootBatch(SortingBatch batch) { - return getSortingBatch( - batch, - "Vrac > Benthos", - SORTING_TYPE_ID, - QualitativeValueId.SORTING_TYPE_BENTHOS.getValue() - ); - } - - public SortingBatch getSpeciesVracAliveNotItemizedRootBatch(SortingBatch batch) { - return getSortingBatch( - batch, - "Vrac > Species > Alive not itemized", - SORTING_TYPE2_ID, - QualitativeValueId.SORTING_TYPE2_ALIVE_NOT_ITEMIZED.getValue() - ); - } - - public SortingBatch getBenthosVracAliveNotItemizedRootBatch(SortingBatch batch) { - return getSortingBatch( - batch, - "Vrac > Benthos > Alive not itemized", - SORTING_TYPE2_ID, - QualitativeValueId.SORTING_TYPE2_ALIVE_NOT_ITEMIZED.getValue() - ); - } - - public SortingBatch getSpeciesVracInertRootBatch(SortingBatch batch) { - return getSortingBatch( - batch, - "Vrac > Species > Inert (not alive)", - SORTING_TYPE2_ID, - QualitativeValueId.SORTING_TYPE2_INERT.getValue() - ); - } - - public SortingBatch getBenthosVracInertRootBatch(SortingBatch batch) { - return getSortingBatch( - batch, - "Vrac > Benthos > Inert (not alive)", - SORTING_TYPE2_ID, - QualitativeValueId.SORTING_TYPE2_INERT.getValue() - ); - } - - public SortingBatch getSpeciesVracAliveItemizedRootBatch(SortingBatch batch) { - return getSortingBatch( - batch, - "Vrac > Species > Alive itemized", - SORTING_TYPE2_ID, - QualitativeValueId.SORTING_TYPE2_ALIVE_ITEMIZED.getValue() - ); - } - - public SortingBatch getBenthosVracAliveItemizedRootBatch(SortingBatch batch) { - return getSortingBatch( - batch, - "Vrac > Benthos > Alive itemized", - SORTING_TYPE2_ID, - QualitativeValueId.SORTING_TYPE2_ALIVE_ITEMIZED.getValue() - ); - } - - public SortingBatch getSpeciesHorsVracRootBatch(SortingBatch batch) { - return getSortingBatch( - batch, - "Hors Vrac > Species", - SORTING_TYPE_ID, - QualitativeValueId.SORTING_TYPE_SPECIES.getValue() - ); - } - - public SortingBatch getBenthosHorsVracRootBatch(SortingBatch batch) { - return getSortingBatch( - batch, - "Hors Vrac > Benthos", - SORTING_TYPE_ID, - QualitativeValueId.SORTING_TYPE_BENTHOS.getValue() - ); - } - - public SortingBatch getMarineLitterRootBatch(SortingBatch batch) { - return getSortingBatch( - batch, - "Hors Vrac > MarineLitter", - SORTING_TYPE_ID, - QualitativeValueId.SORTING_TYPE_MARINE_LITTER.getValue() - ); - } - - //------------------------------------------------------------------------// - //-- getOrCreate methods --// - //------------------------------------------------------------------------// - - public SortingBatch getOrCreateVracBatch(CatchBatch batch, - Float weight, - Float weightBeforeSampling) { - return getOrCreate( - batch, - batch, - "Vrac", - PmfmId.SORTED_UNSORTED.getValue(), - QualitativeValueId.SORTED_VRAC.getValue(), - weight, - weightBeforeSampling, - (short) 1 - ); - } - - public SortingBatch getOrCreateSpeciesVracRootBatch(CatchBatch target, - SortingBatch batch, - Float totalWeight) { - return getOrCreate( - target, - batch, - "Vrac > Species", - SORTING_TYPE_ID, - QualitativeValueId.SORTING_TYPE_SPECIES.getValue(), - totalWeight, - (short) 1 - ); - } - - public SortingBatch getOrCreateSpeciesVracAliveNotItemizedRootBatch(CatchBatch target, - SortingBatch batch, - Float totalWeight) { - return getOrCreate( - target, - batch, - "Vrac > Species > Alive Not Itemized", - SORTING_TYPE2_ID, - QualitativeValueId.SORTING_TYPE2_ALIVE_NOT_ITEMIZED.getValue(), - totalWeight, - (short) 1 - ); - } - - public SortingBatch getOrCreateSpeciesVracInertRootBatch(CatchBatch target, - SortingBatch batch, - Float totalWeight) { - return getOrCreate( - target, - batch, - "Vrac > Species > Inert", - SORTING_TYPE2_ID, - QualitativeValueId.SORTING_TYPE2_INERT.getValue(), - totalWeight, - (short) 2 - ); - } - - public SortingBatch getOrCreateSpeciesVracAliveItemizedRootBatch(CatchBatch target, - SortingBatch batch) { - return getOrCreate( - target, - batch, - "Vrac > Benthos > Alive Itemized", - SORTING_TYPE2_ID, - QualitativeValueId.SORTING_TYPE2_ALIVE_ITEMIZED.getValue(), - (short) 3 - ); - } - - public SortingBatch getOrCreateBenthosVracRootBatch(CatchBatch target, - SortingBatch batch, - Float totalWeight) { - return getOrCreate( - target, - batch, - "Vrac > Benthos", - SORTING_TYPE_ID, - QualitativeValueId.SORTING_TYPE_BENTHOS.getValue(), - totalWeight, - (short) 2 - ); - } - - public SortingBatch getOrCreateBenthosVracAliveNotItemizedRootBatch(CatchBatch target, - SortingBatch batch, - Float totalWeight) { - return getOrCreate( - target, - batch, - "Vrac > Benthos > Alive Not itemized", - SORTING_TYPE2_ID, - QualitativeValueId.SORTING_TYPE2_ALIVE_NOT_ITEMIZED.getValue(), - totalWeight, - (short) 1 - ); - } - - public SortingBatch getOrCreateBenthosVracInertRootBatch(fr.ifremer.adagio.core.dao.data.batch.CatchBatch target, - SortingBatch batch, - Float totalWeight) { - return getOrCreate( - target, - batch, - "Vrac > Benthos > Inert", - SORTING_TYPE2_ID, - QualitativeValueId.SORTING_TYPE2_INERT.getValue(), - totalWeight, - (short) 2 - ); - } - - public SortingBatch getOrCreateBenthosVracAliveItemizedRootBatch(CatchBatch target, - SortingBatch batch) { - return getOrCreate( - target, - batch, - "Vrac > Benthos > Alive Itemized", - SORTING_TYPE2_ID, - QualitativeValueId.SORTING_TYPE2_ALIVE_ITEMIZED.getValue(), - (short) 3 - ); - } - - public SortingBatch getOrCreateHorsVracBatch(CatchBatch batch) { - return getOrCreate( - batch, - batch, - "Hors Vrac", - PmfmId.SORTED_UNSORTED.getValue(), - QualitativeValueId.SORTED_HORS_VRAC.getValue(), - (short) 2 - ); - } - - public SortingBatch getOrCreateSpeciesHorsVracRootBatch(CatchBatch target, - SortingBatch batch) { - return getOrCreate( - target, - batch, - "Hors Vrac > Species", - SORTING_TYPE_ID, - QualitativeValueId.SORTING_TYPE_SPECIES.getValue(), - (short) 1 - ); - } - - public SortingBatch getOrCreateBenthosHorsVracRootBatch(CatchBatch target, - SortingBatch batch) { - return getOrCreate( - target, - batch, - "Hors Vrac > Benthos", - SORTING_TYPE_ID, - QualitativeValueId.SORTING_TYPE_BENTHOS.getValue(), - (short) 2 - ); - } - - public SortingBatch getOrCreateMarineLitterRootBatch(CatchBatch target, - SortingBatch batch, - Float totalWeight) { - return getOrCreate( - target, - batch, - "Hors Vrac > Marine Litter", - SORTING_TYPE_ID, - QualitativeValueId.SORTING_TYPE_MARINE_LITTER.getValue(), - totalWeight, - (short) 3 - ); - } - - public SortingBatch getOrCreateRejectedBatch(CatchBatch batch, - Float weight) { - - return getOrCreate( - batch, - batch, - "Unsorted", - PmfmId.SORTED_UNSORTED.getValue(), - QualitativeValueId.UNSORTED.getValue(), - weight, - (short) 3 - ); - } - - public void setWeightAndSampleRatio(SortingBatch target, Float weight, Float weightBeforeSampling) { - - catchBatchDao.setSortingBatchWeights(target, - weight, - weightBeforeSampling, - PmfmId.WEIGHT_MEASURED.getValue(), - measurementPersistenceHelper.getRecorderDepartmentId()); - - } - - public void setSortingSamplingRatio(SortingBatch target, Float weight, Float weightBeforeSampling) { - - catchBatchDao.setSortingSamplingRatio(target, - weight, - weightBeforeSampling); - - } - - //------------------------------------------------------------------------// - //-- Internal methods --// - //------------------------------------------------------------------------// - - protected SortingBatch get(Batch parentBatch, - Integer sortingPmfmId, - Integer sortingQualitativeValueId) { - return getSortingBatch( - parentBatch, - null, - sortingPmfmId, - sortingQualitativeValueId); - } - - protected SortingBatch getSortingBatch(Batch source, - String debugMessage, - Integer... ids) { - - return getSortingBatch(source.getChildBatchs(), debugMessage, ids); - } - - protected SortingBatch getSortingBatch(Collection<Batch> childs, - String debugMessage, - Integer... ids) { - - int nbParams = ids.length / 2; - - Object[] params = new Object[nbParams * 3]; - for (int i = 0; i < nbParams; i++) { - Integer sortingPmfmId = ids[2 * i]; - Integer sortingQualitativeValueId = ids[2 * i + 1]; - params[3 * i] = CatchBatchExtendDao.PMFM_ID; - params[3 * i + 1] = sortingPmfmId; - params[3 * i + 2] = sortingQualitativeValueId; - } - SortingBatch result = catchBatchDao.getSortingBatch(childs, params); - if (result != null && debugMessage != null && log.isDebugEnabled()) { - log.debug("Loaded " + debugMessage + ": " + result.getId()); - } - return result; - } - - protected SortingBatch getOrCreate(fr.ifremer.adagio.core.dao.data.batch.CatchBatch rootBatch, - Batch batch, - String debugMessage, - Integer sortingPmfmId, - Integer sortingQualitativeValueId, - short rankOrder) { - return getOrCreate( - rootBatch, - batch, - debugMessage, - sortingPmfmId, - sortingQualitativeValueId, - null, - null, - rankOrder - ); - } - - protected SortingBatch getOrCreate(fr.ifremer.adagio.core.dao.data.batch.CatchBatch rootBatch, - Batch batch, - String debugMessage, - Integer sortingPmfmId, - Integer sortingQualitativeValueId, - Float totalWeight, - short rankOrder) { - return getOrCreate( - rootBatch, - batch, - debugMessage, - sortingPmfmId, - sortingQualitativeValueId, - totalWeight, - null, - rankOrder - ); - } - - protected SortingBatch getOrCreate(fr.ifremer.adagio.core.dao.data.batch.CatchBatch rootBatch, - Batch batch, - String debugMessage, - Integer sortingPmfmId, - Integer sortingQualitativeValueId, - Float weight, - Float weightBeforeSampling, - short rankOrder) { - - SortingBatch result = getSortingBatch( - batch, - debugMessage, - sortingPmfmId, - sortingQualitativeValueId); - - if (result == null) { - - result = SortingBatch.Factory.newInstance(); - if (batch.getChildBatchs() == null) { - batch.setChildBatchs(Sets.<Batch>newHashSet()); - } - batch.getChildBatchs().add(result); - - // --- Some mandatory properties --- // - - QualityFlagImpl qualityFlag = load(QualityFlagImpl.class, QualityFlagCode.NOTQUALIFIED.getValue()); - result.setQualityFlag(qualityFlag); - result.setRootBatch(rootBatch); - result.setParentBatch(batch); - result.setExhaustiveInventory(true); - result.setRankOrder(rankOrder); - - // No taxon or taxon group - result.setReferenceTaxon(null); - result.setTaxonGroup(null); - - // --- Sorting measurement --- // - - Collection<SortingMeasurement> sortingMeasurements = result.getSortingMeasurements(); - Set<SortingMeasurement> notChangedSortingMeasurements = Sets.newHashSet(); - if (sortingMeasurements != null) { - notChangedSortingMeasurements.addAll(sortingMeasurements); - } - - if (sortingPmfmId != null && sortingQualitativeValueId != null) { - SortingMeasurement sm = measurementPersistenceHelper.setSortingMeasurement( - result, - sortingPmfmId, - sortingQualitativeValueId); - notChangedSortingMeasurements.remove(sm); - } - if (sortingMeasurements != null) { - sortingMeasurements.removeAll(notChangedSortingMeasurements); - } - - catchBatchDao.createSortingBatch(result, rootBatch); - } - - // --- Sampling Ratio + QuantificationMeasurement --- // - - setWeightAndSampleRatio(result, weight, weightBeforeSampling); - - return result; - } - - //------------------------------------------------------------------------// - //-- Debug methods --// - //------------------------------------------------------------------------// - - public void displayCatchBatch(CatchBatch result) { - StringBuilder sb = new StringBuilder(); - displayBatch(result, 0, sb); - log.info(sb.toString()); - } - - protected void displayBatch(Batch batch, int level, StringBuilder sb) { - - ToStringStyle style = new BatchTreeToStringStyle(); - - ToStringBuilder builder = new ToStringBuilder(batch, style); - builder.append("id", batch.getId()); - builder.append("rankOrder", batch.getRankOrder()); - - if (batch instanceof CatchBatch) { - CatchBatch catchBatch = (CatchBatch) batch; - builder.append("synchronizationStatus", catchBatch.getSynchronizationStatus()); - } - if (batch instanceof SortingBatch) { - SortingBatch sortingBatch = (SortingBatch) batch; - if (sortingBatch.getSamplingRatio() != null) { - builder.append("samplingRatio", sortingBatch.getSamplingRatio()); - } - if (sortingBatch.getSamplingRatioText() != null) { - builder.append("samplingRatioText", sortingBatch.getSamplingRatioText()); - } - if (sortingBatch.getIndividualCount() != null) { - builder.append("individualCount", sortingBatch.getIndividualCount()); - } - if (sortingBatch.getReferenceTaxon() != null) { - ReferenceTaxon referenceTaxon = sortingBatch.getReferenceTaxon(); - builder.append("referenceTaxon", load(ReferenceTaxonImpl.class, referenceTaxon.getId()).getName()); - } - - SortingMeasurement sm = null; - if (sortingBatch.getSortingMeasurements() != null && sortingBatch.getSortingMeasurements().size() == 1) { - sm = sortingBatch.getSortingMeasurements().iterator().next(); - } else if (sortingBatch.getReferenceTaxon() != null && sortingBatch.getReferenceTaxon().getId() != null) { - sm = measurementPersistenceHelper.getInheritedSortingMeasurement(sortingBatch); - } - if (sm != null) { - String sortingMeasurementStr = measurementPersistenceHelper.toString(sm); - builder.append("sortingMeasurement", sortingMeasurementStr); - } - } - QuantificationMeasurement quantificationMeasurement = measurementPersistenceHelper.getWeightMeasurementQuantificationMeasurement(batch); - if (quantificationMeasurement != null) { - String quantificationMeasurementStr = measurementPersistenceHelper.toString(quantificationMeasurement); - builder.append("weightQuantificationMeasurement", quantificationMeasurementStr); - builder.append("weightQuantificationMeasurement.isReferenceQuantification", quantificationMeasurement.getIsReferenceQuantification()); - } - if (batch.getWeight() != null) { - builder.append("weight", batch.getWeight()); - } - if (batch.getWeightBeforeSampling() != null) { - builder.append("weightBeforeSampling", batch.getWeightBeforeSampling()); - } - builder.append("qualityFlag", load(QualityFlagImpl.class, batch.getQualityFlag().getCode()).getName()); - - String prefix = "\n" + StringUtils.leftPad("", 2 * level); - String batchStr = Joiner.on(prefix).join(builder.build().split("\n")); - sb.append(prefix).append(batchStr); - Collection<Batch> childBatchs = batch.getChildBatchs(); - if (childBatchs != null) { - List<Batch> childBatchList = new ArrayList<>(childBatchs); - Collections.sort(childBatchList, batchComparator); - for (Batch childBatch : childBatchList) { - displayBatch(childBatch, level + 1, sb); - } - } - } - - public CatchBatch loadCatchBatch(Integer catchBatchId) { - return catchBatchDao.loadFullTreeWithCache(catchBatchId, PmfmId.WEIGHT_MEASURED.getValue(), true, true); - } - - - static final class BatchTreeToStringStyle extends ToStringStyle { - - private static final long serialVersionUID = 1L; - - BatchTreeToStringStyle() { - super(); - this.setUseClassName(true); - this.setUseShortClassName(true); - this.setContentStart(""); - this.setFieldSeparator(SystemUtils.LINE_SEPARATOR + " | "); - this.setFieldSeparatorAtStart(true); - this.setContentEnd(""); - } - } -} diff --git a/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/util/tree/BatchTreeHelper.java b/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/util/tree/BatchTreeHelper.java new file mode 100644 index 0000000..29481c3 --- /dev/null +++ b/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/util/tree/BatchTreeHelper.java @@ -0,0 +1,330 @@ +package fr.ifremer.tutti.persistence.service.util.tree; + +/* + * #%L + * Tutti :: Persistence + * %% + * Copyright (C) 2012 - 2014 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.adagio.core.dao.data.batch.CatchBatch; +import fr.ifremer.adagio.core.dao.data.batch.SortingBatch; +import fr.ifremer.adagio.core.dao.referential.pmfm.PmfmId; +import fr.ifremer.adagio.core.dao.referential.pmfm.QualitativeValueId; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; + +/** + * Helper to build or navigauet into the batch tree. + * + * Created on 4/20/14. + * + * @author Tony Chemit - chemit@codelutin.com + * @since 3.5 + */ +@Component("batchTreeHelper") +public class BatchTreeHelper extends BatchTreeHelperSupport { + +// /** Logger. */ +// private static final Log log = LogFactory.getLog(BatchTreeHelper.class); + + @Resource(name = "benthosBatchTreeHelper") + protected BenthosBatchTreeHelper benthosBatchTreeHelper; + + @Resource(name = "speciesBatchTreeHelper") + protected SpeciesBatchTreeHelper speciesBatchTreeHelper; + + //------------------------------------------------------------------------// + //-- Get CatchBatch navigation methods --// + //------------------------------------------------------------------------// + +// public SortingBatch getSpeciesVracAliveItemizedRootBatch(CatchBatch batch) { +// return speciesBatchTreeHelper.getVracAliveItemizedRootBatch(batch); +// } +// +// public SortingBatch getBenthosVracAliveItemizedRootBatch(CatchBatch batch) { +// return benthosBatchTreeHelper.getVracAliveItemizedRootBatch(batch); +// } +// +// public SortingBatch getSpeciesHorsVracRootBatch(CatchBatch batch) { +// return speciesBatchTreeHelper.getHorsVracRootBatch(batch); +// } +// +// public SortingBatch getBenthosHorsVracRootBatch(CatchBatch batch) { +// return benthosBatchTreeHelper.getHorsVracRootBatch(batch); +// } + + public SortingBatch getMarineLitterRootBatch(CatchBatch batch) { + return getSortingBatch(batch, + "Hors Vrac > Marine Litter", + PmfmId.SORTED_UNSORTED.getValue(), + QualitativeValueId.SORTED_HORS_VRAC.getValue(), + SORTING_TYPE_ID, + QualitativeValueId.SORTING_TYPE_MARINE_LITTER.getValue()); + } + +// public SortingBatch getVracBatch(CatchBatch batch) { +// return getSortingBatch( +// batch, +// "Vrac", +// PmfmId.SORTED_UNSORTED.getValue(), +// QualitativeValueId.SORTED_VRAC.getValue() +// ); +// } + +// public SortingBatch getHorsVracBatch(CatchBatch batch) { +// return getSortingBatch( +// batch, +// "Hors Vrac", +// PmfmId.SORTED_UNSORTED.getValue(), +// QualitativeValueId.SORTED_HORS_VRAC.getValue() +// ); +// } +// + public SortingBatch getRejectedBatch(CatchBatch batch) { + return getSortingBatch( + batch, + "Unsorted", + PmfmId.SORTED_UNSORTED.getValue(), + QualitativeValueId.UNSORTED.getValue() + ); + } + + //------------------------------------------------------------------------// + //-- Get SortingBatch navigation methods --// + //------------------------------------------------------------------------// + + public SortingBatch getSpeciesVracRootBatch(SortingBatch batch) { + return speciesBatchTreeHelper.getVracRootBatch(batch); + } + + public SortingBatch getBenthosVracRootBatch(SortingBatch batch) { + return benthosBatchTreeHelper.getVracRootBatch(batch); + } + + public SortingBatch getSpeciesVracAliveNotItemizedRootBatch(SortingBatch batch) { + return speciesBatchTreeHelper.getVracAliveNotItemizedRootBatch(batch); + } + + public SortingBatch getBenthosVracAliveNotItemizedRootBatch(SortingBatch batch) { + return benthosBatchTreeHelper.getVracAliveNotItemizedRootBatch(batch); + } + + public SortingBatch getSpeciesVracInertRootBatch(SortingBatch batch) { + return speciesBatchTreeHelper.getVracInertRootBatch(batch); + } + + public SortingBatch getBenthosVracInertRootBatch(SortingBatch batch) { + return benthosBatchTreeHelper.getVracInertRootBatch(batch); + } + + public SortingBatch getSpeciesVracAliveItemizedRootBatch(SortingBatch batch) { + return speciesBatchTreeHelper.getVracAliveItemizedRootBatch(batch); + } + + public SortingBatch getBenthosVracAliveItemizedRootBatch(SortingBatch batch) { + return benthosBatchTreeHelper.getVracAliveItemizedRootBatch(batch); + } + + public SortingBatch getSpeciesHorsVracRootBatch(SortingBatch batch) { + return speciesBatchTreeHelper.getHorsVracRootBatch(batch); + } + + public SortingBatch getBenthosHorsVracRootBatch(SortingBatch batch) { + return benthosBatchTreeHelper.getHorsVracRootBatch(batch); + } + + public SortingBatch getMarineLitterRootBatch(SortingBatch batch) { + return getSortingBatch( + batch, + "Hors Vrac > MarineLitter", + SORTING_TYPE_ID, + QualitativeValueId.SORTING_TYPE_MARINE_LITTER.getValue()); + } + + //------------------------------------------------------------------------// + //-- getOrCreate methods --// + //------------------------------------------------------------------------// + +// public SortingBatch getOrCreateVracBatch(CatchBatch batch, Float weight, Float weightBeforeSampling) { +// return getOrCreate(batch, +// batch, +// "Vrac", +// PmfmId.SORTED_UNSORTED.getValue(), +// QualitativeValueId.SORTED_VRAC.getValue(), +// weight, +// weightBeforeSampling, +// (short) 1); +// } + + public SortingBatch getOrCreateSpeciesVracRootBatch(CatchBatch target, SortingBatch batch, Float totalWeight) { + return speciesBatchTreeHelper.getOrCreateVracRootBatch(target, batch, totalWeight); +// target, +// batch, +// "Vrac > Species", +// SORTING_TYPE_ID, +// QualitativeValueId.SORTING_TYPE_SPECIES.getValue(), +// totalWeight, +// (short) 1 +// ); + } + + public SortingBatch getOrCreateSpeciesVracAliveNotItemizedRootBatch(CatchBatch target, SortingBatch batch, Float totalWeight) { + return speciesBatchTreeHelper.getOrCreateVracAliveNotItemizedRootBatch(target, batch, totalWeight); +// return getOrCreate( +// target, +// batch, +// "Vrac > Species > Alive Not Itemized", +// SORTING_TYPE2_ID, +// QualitativeValueId.SORTING_TYPE2_ALIVE_NOT_ITEMIZED.getValue(), +// totalWeight, +// (short) 1 +// ); + } + + public SortingBatch getOrCreateSpeciesVracInertRootBatch(CatchBatch target, SortingBatch batch, Float totalWeight) { + return speciesBatchTreeHelper.getOrCreateVracInertRootBatch(target, batch, totalWeight); +// return getOrCreate( +// target, +// batch, +// "Vrac > Species > Inert", +// SORTING_TYPE2_ID, +// QualitativeValueId.SORTING_TYPE2_INERT.getValue(), +// totalWeight, +// (short) 2 +// ); + } + + public SortingBatch getOrCreateSpeciesVracAliveItemizedRootBatch(CatchBatch target, SortingBatch batch) { + return speciesBatchTreeHelper.getOrCreateVracAliveItemizedRootBatch(target, batch); +// return getOrCreate( +// target, +// batch, +// "Vrac > Benthos > Alive Itemized", +// SORTING_TYPE2_ID, +// QualitativeValueId.SORTING_TYPE2_ALIVE_ITEMIZED.getValue(), +// (short) 3 +// ); + } + + public SortingBatch getOrCreateBenthosVracRootBatch(CatchBatch target, SortingBatch batch, Float totalWeight) { + return benthosBatchTreeHelper.getOrCreateVracRootBatch(target, batch, totalWeight); +// return getOrCreate( +// target, +// batch, +// "Vrac > Benthos", +// SORTING_TYPE_ID, +// QualitativeValueId.SORTING_TYPE_BENTHOS.getValue(), +// totalWeight, +// (short) 2 +// ); + } + + public SortingBatch getOrCreateBenthosVracAliveNotItemizedRootBatch(CatchBatch target, SortingBatch batch, Float totalWeight) { + return benthosBatchTreeHelper.getOrCreateVracAliveNotItemizedRootBatch(target, batch, totalWeight); +// return getOrCreate( +// target, +// batch, +// "Vrac > Benthos > Alive Not itemized", +// SORTING_TYPE2_ID, +// QualitativeValueId.SORTING_TYPE2_ALIVE_NOT_ITEMIZED.getValue(), +// totalWeight, +// (short) 1 +// ); + } + + public SortingBatch getOrCreateBenthosVracInertRootBatch(CatchBatch target, SortingBatch batch, Float totalWeight) { + return benthosBatchTreeHelper.getOrCreateVracInertRootBatch(target, batch, totalWeight); +// return getOrCreate( +// target, +// batch, +// "Vrac > Benthos > Inert", +// SORTING_TYPE2_ID, +// QualitativeValueId.SORTING_TYPE2_INERT.getValue(), +// totalWeight, +// (short) 2 +// ); + } + + public SortingBatch getOrCreateBenthosVracAliveItemizedRootBatch(CatchBatch target, SortingBatch batch) { + return benthosBatchTreeHelper.getOrCreateVracAliveItemizedRootBatch(target, batch); +// return getOrCreate( +// target, +// batch, +// "Vrac > Benthos > Alive Itemized", +// SORTING_TYPE2_ID, +// QualitativeValueId.SORTING_TYPE2_ALIVE_ITEMIZED.getValue(), +// (short) 3 +// ); + } + +// public SortingBatch getOrCreateHorsVracBatch(CatchBatch batch) { +// return getOrCreate(batch, +// batch, +// "Hors Vrac", +// PmfmId.SORTED_UNSORTED.getValue(), +// QualitativeValueId.SORTED_HORS_VRAC.getValue(), +// (short) 2); +// } + + public SortingBatch getOrCreateSpeciesHorsVracRootBatch(CatchBatch target, SortingBatch batch) { + return speciesBatchTreeHelper.getOrCreateHorsVracRootBatch(target, batch); +// return getOrCreate( +// target, +// batch, +// "Hors Vrac > Species", +// SORTING_TYPE_ID, +// QualitativeValueId.SORTING_TYPE_SPECIES.getValue(), +// (short) 1 +// ); + } + + public SortingBatch getOrCreateBenthosHorsVracRootBatch(CatchBatch target, SortingBatch batch) { + return benthosBatchTreeHelper.getOrCreateHorsVracRootBatch(target, batch); +// return getOrCreate( +// target, +// batch, +// "Hors Vrac > Benthos", +// SORTING_TYPE_ID, +// QualitativeValueId.SORTING_TYPE_BENTHOS.getValue(), +// (short) 2 +// ); + } + + public SortingBatch getOrCreateMarineLitterRootBatch(CatchBatch target, SortingBatch batch, Float totalWeight) { + return getOrCreate(target, + batch, + "Hors Vrac > Marine Litter", + SORTING_TYPE_ID, + QualitativeValueId.SORTING_TYPE_MARINE_LITTER.getValue(), + totalWeight, + (short) 3); + } + + public SortingBatch getOrCreateRejectedBatch(CatchBatch batch, Float weight) { + return getOrCreate(batch, + batch, + "Unsorted", + PmfmId.SORTED_UNSORTED.getValue(), + QualitativeValueId.UNSORTED.getValue(), + weight, + (short) 3); + } + +} diff --git a/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/util/tree/BatchTreeHelperSupport.java b/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/util/tree/BatchTreeHelperSupport.java new file mode 100644 index 0000000..b93a17b --- /dev/null +++ b/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/util/tree/BatchTreeHelperSupport.java @@ -0,0 +1,365 @@ +package fr.ifremer.tutti.persistence.service.util.tree; + +/* + * #%L + * Tutti :: Persistence + * %% + * Copyright (C) 2012 - 2014 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.Joiner; +import com.google.common.collect.Sets; +import fr.ifremer.adagio.core.dao.data.batch.Batch; +import fr.ifremer.adagio.core.dao.data.batch.Batchs; +import fr.ifremer.adagio.core.dao.data.batch.CatchBatch; +import fr.ifremer.adagio.core.dao.data.batch.CatchBatchExtendDao; +import fr.ifremer.adagio.core.dao.data.batch.SortingBatch; +import fr.ifremer.adagio.core.dao.data.measure.QuantificationMeasurement; +import fr.ifremer.adagio.core.dao.data.measure.SortingMeasurement; +import fr.ifremer.adagio.core.dao.referential.QualityFlagCode; +import fr.ifremer.adagio.core.dao.referential.QualityFlagImpl; +import fr.ifremer.adagio.core.dao.referential.pmfm.PmfmId; +import fr.ifremer.adagio.core.dao.referential.pmfm.QualitativeValueId; +import fr.ifremer.adagio.core.dao.referential.taxon.ReferenceTaxon; +import fr.ifremer.adagio.core.dao.referential.taxon.ReferenceTaxonImpl; +import fr.ifremer.tutti.persistence.service.AbstractPersistenceService; +import fr.ifremer.tutti.persistence.service.util.MeasurementPersistenceHelper; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.SystemUtils; +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import javax.annotation.Resource; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import java.util.Set; + +/** + * Helper to build or navigauet into the batch tree. + * + * Created on 4/20/14. + * + * @author Tony Chemit - chemit@codelutin.com + * @since 3.5 + */ +public abstract class BatchTreeHelperSupport extends AbstractPersistenceService { + + public static final Integer SORTING_TYPE_ID = PmfmId.SCIENTIFIC_CRUISE_SORTING_TYPE.getValue(); + + public static final Integer SORTING_TYPE2_ID = PmfmId.SCIENTIFIC_CRUISE_SORTING_TYPE2.getValue(); + + /** Logger. */ + private static final Log log = LogFactory.getLog(BatchTreeHelperSupport.class); + + @Resource(name = "measurementPersistenceHelper") + protected MeasurementPersistenceHelper measurementPersistenceHelper; + + @Resource(name = "catchBatchDao") + protected CatchBatchExtendDao catchBatchDao; + + protected Comparator<Batch> batchComparator = Batchs.newRankOrderComparator(); + + //------------------------------------------------------------------------// + //-- Get CatchBatch navigation methods --// + //------------------------------------------------------------------------// + + public final SortingBatch getVracBatch(CatchBatch batch) { + return getSortingBatch(batch, + "Vrac", + PmfmId.SORTED_UNSORTED.getValue(), + QualitativeValueId.SORTED_VRAC.getValue()); + } + + public final SortingBatch getHorsVracBatch(CatchBatch batch) { + return getSortingBatch(batch, + "Hors Vrac", + PmfmId.SORTED_UNSORTED.getValue(), + QualitativeValueId.SORTED_HORS_VRAC.getValue()); + } + + public final SortingBatch getOrCreateVracBatch(CatchBatch batch, Float weight, Float weightBeforeSampling) { + return getOrCreate(batch, + batch, + "Vrac", + PmfmId.SORTED_UNSORTED.getValue(), + QualitativeValueId.SORTED_VRAC.getValue(), + weight, + weightBeforeSampling, + (short) 1); + } + + public final SortingBatch getOrCreateHorsVracBatch(CatchBatch batch) { + return getOrCreate(batch, + batch, + "Hors Vrac", + PmfmId.SORTED_UNSORTED.getValue(), + QualitativeValueId.SORTED_HORS_VRAC.getValue(), + (short) 2); + } + + public final void setWeightAndSampleRatio(SortingBatch target, Float weight, Float weightBeforeSampling) { + + catchBatchDao.setSortingBatchWeights(target, + weight, + weightBeforeSampling, + PmfmId.WEIGHT_MEASURED.getValue(), + measurementPersistenceHelper.getRecorderDepartmentId()); + + } + + public final void setSortingSamplingRatio(SortingBatch target, Float weight, Float weightBeforeSampling) { + + catchBatchDao.setSortingSamplingRatio(target, weight, weightBeforeSampling); + + } + + //------------------------------------------------------------------------// + //-- Internal methods --// + //------------------------------------------------------------------------// + + protected final SortingBatch get(Batch parentBatch, Integer sortingPmfmId, Integer sortingQualitativeValueId) { + return getSortingBatch( + parentBatch, + null, + sortingPmfmId, + sortingQualitativeValueId); + } + + protected final SortingBatch getSortingBatch(Batch source, String debugMessage, Integer... ids) { + + return getSortingBatch(source.getChildBatchs(), debugMessage, ids); + } + + protected final SortingBatch getSortingBatch(Collection<Batch> childs, String debugMessage, Integer... ids) { + + int nbParams = ids.length / 2; + + Object[] params = new Object[nbParams * 3]; + for (int i = 0; i < nbParams; i++) { + Integer sortingPmfmId = ids[2 * i]; + Integer sortingQualitativeValueId = ids[2 * i + 1]; + params[3 * i] = CatchBatchExtendDao.PMFM_ID; + params[3 * i + 1] = sortingPmfmId; + params[3 * i + 2] = sortingQualitativeValueId; + } + SortingBatch result = catchBatchDao.getSortingBatch(childs, params); + if (result != null && debugMessage != null && log.isDebugEnabled()) { + log.debug("Loaded " + debugMessage + ": " + result.getId()); + } + return result; + } + + protected final SortingBatch getOrCreate(CatchBatch rootBatch, + Batch batch, + String debugMessage, + Integer sortingPmfmId, + Integer sortingQualitativeValueId, + short rankOrder) { + return getOrCreate(rootBatch, + batch, + debugMessage, + sortingPmfmId, + sortingQualitativeValueId, + null, + null, + rankOrder); + } + + protected final SortingBatch getOrCreate(CatchBatch rootBatch, + Batch batch, + String debugMessage, + Integer sortingPmfmId, + Integer sortingQualitativeValueId, + Float totalWeight, + short rankOrder) { + return getOrCreate(rootBatch, + batch, + debugMessage, + sortingPmfmId, + sortingQualitativeValueId, + totalWeight, + null, + rankOrder); + } + + protected final SortingBatch getOrCreate(CatchBatch rootBatch, + Batch batch, + String debugMessage, + Integer sortingPmfmId, + Integer sortingQualitativeValueId, + Float weight, + Float weightBeforeSampling, + short rankOrder) { + + SortingBatch result = getSortingBatch( + batch, + debugMessage, + sortingPmfmId, + sortingQualitativeValueId); + + if (result == null) { + + result = SortingBatch.Factory.newInstance(); + if (batch.getChildBatchs() == null) { + batch.setChildBatchs(Sets.<Batch>newHashSet()); + } + batch.getChildBatchs().add(result); + + // --- Some mandatory properties --- // + + QualityFlagImpl qualityFlag = load(QualityFlagImpl.class, QualityFlagCode.NOTQUALIFIED.getValue()); + result.setQualityFlag(qualityFlag); + result.setRootBatch(rootBatch); + result.setParentBatch(batch); + result.setExhaustiveInventory(true); + result.setRankOrder(rankOrder); + + // No taxon or taxon group + result.setReferenceTaxon(null); + result.setTaxonGroup(null); + + // --- Sorting measurement --- // + + Collection<SortingMeasurement> sortingMeasurements = result.getSortingMeasurements(); + Set<SortingMeasurement> notChangedSortingMeasurements = Sets.newHashSet(); + if (sortingMeasurements != null) { + notChangedSortingMeasurements.addAll(sortingMeasurements); + } + + if (sortingPmfmId != null && sortingQualitativeValueId != null) { + SortingMeasurement sm = measurementPersistenceHelper.setSortingMeasurement( + result, + sortingPmfmId, + sortingQualitativeValueId); + notChangedSortingMeasurements.remove(sm); + } + if (sortingMeasurements != null) { + sortingMeasurements.removeAll(notChangedSortingMeasurements); + } + + catchBatchDao.createSortingBatch(result, rootBatch); + } + + // --- Sampling Ratio + QuantificationMeasurement --- // + + setWeightAndSampleRatio(result, weight, weightBeforeSampling); + + return result; + } + + //------------------------------------------------------------------------// + //-- Debug methods --// + //------------------------------------------------------------------------// + + public final void displayCatchBatch(CatchBatch result) { + StringBuilder sb = new StringBuilder(); + displayBatch(result, 0, sb); + log.info(sb.toString()); + } + + protected final void displayBatch(Batch batch, int level, StringBuilder sb) { + + ToStringStyle style = new BatchTreeToStringStyle(); + + ToStringBuilder builder = new ToStringBuilder(batch, style); + builder.append("id", batch.getId()); + builder.append("rankOrder", batch.getRankOrder()); + + if (batch instanceof CatchBatch) { + CatchBatch catchBatch = (CatchBatch) batch; + builder.append("synchronizationStatus", catchBatch.getSynchronizationStatus()); + } + if (batch instanceof SortingBatch) { + SortingBatch sortingBatch = (SortingBatch) batch; + if (sortingBatch.getSamplingRatio() != null) { + builder.append("samplingRatio", sortingBatch.getSamplingRatio()); + } + if (sortingBatch.getSamplingRatioText() != null) { + builder.append("samplingRatioText", sortingBatch.getSamplingRatioText()); + } + if (sortingBatch.getIndividualCount() != null) { + builder.append("individualCount", sortingBatch.getIndividualCount()); + } + if (sortingBatch.getReferenceTaxon() != null) { + ReferenceTaxon referenceTaxon = sortingBatch.getReferenceTaxon(); + builder.append("referenceTaxon", load(ReferenceTaxonImpl.class, referenceTaxon.getId()).getName()); + } + + SortingMeasurement sm = null; + if (sortingBatch.getSortingMeasurements() != null && sortingBatch.getSortingMeasurements().size() == 1) { + sm = sortingBatch.getSortingMeasurements().iterator().next(); + } else if (sortingBatch.getReferenceTaxon() != null && sortingBatch.getReferenceTaxon().getId() != null) { + sm = measurementPersistenceHelper.getInheritedSortingMeasurement(sortingBatch); + } + if (sm != null) { + String sortingMeasurementStr = measurementPersistenceHelper.toString(sm); + builder.append("sortingMeasurement", sortingMeasurementStr); + } + } + QuantificationMeasurement quantificationMeasurement = measurementPersistenceHelper.getWeightMeasurementQuantificationMeasurement(batch); + if (quantificationMeasurement != null) { + String quantificationMeasurementStr = measurementPersistenceHelper.toString(quantificationMeasurement); + builder.append("weightQuantificationMeasurement", quantificationMeasurementStr); + builder.append("weightQuantificationMeasurement.isReferenceQuantification", quantificationMeasurement.getIsReferenceQuantification()); + } + if (batch.getWeight() != null) { + builder.append("weight", batch.getWeight()); + } + if (batch.getWeightBeforeSampling() != null) { + builder.append("weightBeforeSampling", batch.getWeightBeforeSampling()); + } + builder.append("qualityFlag", load(QualityFlagImpl.class, batch.getQualityFlag().getCode()).getName()); + + String prefix = "\n" + StringUtils.leftPad("", 2 * level); + String batchStr = Joiner.on(prefix).join(builder.build().split("\n")); + sb.append(prefix).append(batchStr); + Collection<Batch> childBatchs = batch.getChildBatchs(); + if (childBatchs != null) { + List<Batch> childBatchList = new ArrayList<>(childBatchs); + Collections.sort(childBatchList, batchComparator); + for (Batch childBatch : childBatchList) { + displayBatch(childBatch, level + 1, sb); + } + } + } + + public final CatchBatch loadCatchBatch(Integer catchBatchId) { + return catchBatchDao.loadFullTreeWithCache(catchBatchId, PmfmId.WEIGHT_MEASURED.getValue(), true, true); + } + + + static final class BatchTreeToStringStyle extends ToStringStyle { + + private static final long serialVersionUID = 1L; + + BatchTreeToStringStyle() { + super(); + this.setUseClassName(true); + this.setUseShortClassName(true); + this.setContentStart(""); + this.setFieldSeparator(SystemUtils.LINE_SEPARATOR + " | "); + this.setFieldSeparatorAtStart(true); + this.setContentEnd(""); + } + } +} diff --git a/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/util/tree/BenthosBatchTreeHelper.java b/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/util/tree/BenthosBatchTreeHelper.java new file mode 100644 index 0000000..a43b4f1 --- /dev/null +++ b/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/util/tree/BenthosBatchTreeHelper.java @@ -0,0 +1,43 @@ +package fr.ifremer.tutti.persistence.service.util.tree; + +/* + * #%L + * Tutti :: Persistence + * %% + * Copyright (C) 2012 - 2014 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.adagio.core.dao.referential.pmfm.QualitativeValueId; +import org.springframework.stereotype.Component; + +/** + * Helper to build or navigate into the benthos batch tree. + * + * Created on 4/20/14. + * + * @author Tony Chemit - chemit@codelutin.com + * @since 3.5 + */ +@Component("benthosBatchTreeHelper") +public class BenthosBatchTreeHelper extends SpeciesBatchTreeHelperSupport { + + public BenthosBatchTreeHelper() { + super("Benthos", QualitativeValueId.SORTING_TYPE_BENTHOS, (short)2); + } + +} diff --git a/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/util/tree/SpeciesBatchTreeHelper.java b/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/util/tree/SpeciesBatchTreeHelper.java new file mode 100644 index 0000000..7e313e2 --- /dev/null +++ b/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/util/tree/SpeciesBatchTreeHelper.java @@ -0,0 +1,43 @@ +package fr.ifremer.tutti.persistence.service.util.tree; + +/* + * #%L + * Tutti :: Persistence + * %% + * Copyright (C) 2012 - 2014 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.adagio.core.dao.referential.pmfm.QualitativeValueId; +import org.springframework.stereotype.Component; + +/** + * Helper to build or navigate into the species batch tree. + * + * Created on 4/20/14. + * + * @author Tony Chemit - chemit@codelutin.com + * @since 3.5 + */ +@Component("speciesBatchTreeHelper") +public class SpeciesBatchTreeHelper extends SpeciesBatchTreeHelperSupport { + + public SpeciesBatchTreeHelper() { + super("Species", QualitativeValueId.SORTING_TYPE_SPECIES, (short)1); + } + +} diff --git a/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/util/tree/SpeciesBatchTreeHelperSupport.java b/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/util/tree/SpeciesBatchTreeHelperSupport.java new file mode 100644 index 0000000..0ef0ec8 --- /dev/null +++ b/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/util/tree/SpeciesBatchTreeHelperSupport.java @@ -0,0 +1,424 @@ +package fr.ifremer.tutti.persistence.service.util.tree; + +/* + * #%L + * Tutti :: Persistence + * %% + * Copyright (C) 2012 - 2014 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.Preconditions; +import fr.ifremer.adagio.core.dao.data.batch.CatchBatch; +import fr.ifremer.adagio.core.dao.data.batch.SortingBatch; +import fr.ifremer.adagio.core.dao.referential.pmfm.PmfmId; +import fr.ifremer.adagio.core.dao.referential.pmfm.QualitativeValueId; +import fr.ifremer.tutti.persistence.entities.referential.CaracteristicQualitativeValue; +import org.springframework.dao.DataIntegrityViolationException; + +import java.io.Serializable; +import java.text.MessageFormat; + +/** + * Helper to build or navigauet into the batch tree. + * + * Created on 4/20/14. + * + * @author Tony Chemit - chemit@codelutin.com + * @since 3.5 + */ +public abstract class SpeciesBatchTreeHelperSupport extends BatchTreeHelperSupport { + + private final String prefix; + + private final QualitativeValueId sortingType; + + private final short rootBatchRankOrder; + + protected SpeciesBatchTreeHelperSupport(String prefix, QualitativeValueId sortingType, short rootBatchRankOrder) { + this.prefix = prefix; + this.sortingType = sortingType; + this.rootBatchRankOrder = rootBatchRankOrder; + } + + + public void setBatchParents(Integer sampleCategoryId, + Serializable sampleCategoryValue, + SortingBatch target, + Integer parentBatchId, + CatchBatch catchBatch) { + + Preconditions.checkNotNull(target); + Preconditions.checkNotNull(catchBatch); + + target.setRootBatch(catchBatch); + + SortingBatch parentBatch; + if (parentBatchId != null) { + + // Load existing parent and root + parentBatch = catchBatchDao.getSortingBatchById(catchBatch, parentBatchId); + } else { + + // Or retrieve parent batch, from pmfm id + // Retrieve category type + if (!sampleCategoryId.equals(PmfmId.SORTED_UNSORTED.getValue())) { + throw new DataIntegrityViolationException(MessageFormat.format( + "A species or benthos batch with no parent should have a sampleCategoryType {0} (PMFM.ID={1})", + PmfmId.SORTED_UNSORTED.getValue(), + sampleCategoryId)); + } + + Integer qualitativeValueId = convertSampleCategoryValueIntoQualitativeId(sampleCategoryValue); + + if (QualitativeValueId.SORTED_VRAC.getValue().equals(qualitativeValueId)) { + + // -- Vrac > Species > Alive itemized + parentBatch = getVracAliveItemizedRootBatch(catchBatch); + + if (parentBatch == null) { + + // -- Vrac + SortingBatch vracBatch = getVracBatch(catchBatch); + + if (vracBatch == null) { + vracBatch = getOrCreateVracBatch(catchBatch, null, null); + } + + // -- Vrac > Species + SortingBatch vracSpeciesBatch = getVracRootBatch(vracBatch); + + if (vracSpeciesBatch == null) { + vracSpeciesBatch = getOrCreateVracRootBatch(catchBatch, vracBatch, null); + } + + // -- Vrac > Species > Alive itemized + parentBatch = getOrCreateVracAliveItemizedRootBatch(catchBatch, vracSpeciesBatch); + + } + } else if (QualitativeValueId.SORTED_HORS_VRAC.getValue().equals(qualitativeValueId)) { + + // -- Hors Vrac > Species + parentBatch = getHorsVracRootBatch(catchBatch); + + if (parentBatch == null) { + + // -- Hors Vrac + SortingBatch horsVracBatch = getOrCreateHorsVracBatch(catchBatch); + + // -- Hors Vrac > Species + parentBatch = getOrCreateHorsVracRootBatch(catchBatch, horsVracBatch); + } + } else { + + // not possible + throw new DataIntegrityViolationException("Should have Vrac / Hors Vrac qualitative value, but had: " + qualitativeValueId); + } + + } + + Preconditions.checkNotNull(parentBatch); + target.setParentBatch(parentBatch); + } + + //------------------------------------------------------------------------// + //-- Get CatchBatch navigation methods --// + //------------------------------------------------------------------------// + + public SortingBatch getVracAliveItemizedRootBatch(CatchBatch batch) { + return getSortingBatch(batch, + "Vrac > " + prefix + " > Alive Itemized", + PmfmId.SORTED_UNSORTED.getValue(), QualitativeValueId.SORTED_VRAC.getValue(), + SORTING_TYPE_ID, sortingType.getValue(), + SORTING_TYPE2_ID, QualitativeValueId.SORTING_TYPE2_ALIVE_ITEMIZED.getValue()); + } + + public SortingBatch getHorsVracRootBatch(CatchBatch batch) { + return getSortingBatch(batch, + "Hors Vrac > " + prefix, + PmfmId.SORTED_UNSORTED.getValue(), QualitativeValueId.SORTED_HORS_VRAC.getValue(), + SORTING_TYPE_ID, sortingType.getValue()); + } + +// public SortingBatch getVracBatch(CatchBatch batch) { +// return getSortingBatch( +// batch, +// "Vrac", +// PmfmId.SORTED_UNSORTED.getValue(), +// QualitativeValueId.SORTED_VRAC.getValue() +// ); +// } +// +// public SortingBatch getHorsVracBatch(CatchBatch batch) { +// return getSortingBatch( +// batch, +// "Hors Vrac", +// PmfmId.SORTED_UNSORTED.getValue(), +// QualitativeValueId.SORTED_HORS_VRAC.getValue() +// ); +// } + +// public SortingBatch getRejectedBatch(CatchBatch batch) { +// return getSortingBatch( +// batch, +// "Unsorted", +// PmfmId.SORTED_UNSORTED.getValue(), +// QualitativeValueId.UNSORTED.getValue() +// ); +// } + + //------------------------------------------------------------------------// + //-- Get SortingBatch navigation methods --// + //------------------------------------------------------------------------// + + public SortingBatch getVracRootBatch(SortingBatch batch) { + return getSortingBatch(batch, + "Vrac > " + prefix, + SORTING_TYPE_ID, + sortingType.getValue()); + } + + public SortingBatch getVracAliveNotItemizedRootBatch(SortingBatch batch) { + return getSortingBatch(batch, + "Vrac > " + prefix + " > Alive not itemized", + SORTING_TYPE2_ID, + QualitativeValueId.SORTING_TYPE2_ALIVE_NOT_ITEMIZED.getValue()); + } + +// public SortingBatch getBenthosVracAliveNotItemizedRootBatch(SortingBatch batch) { +// return getSortingBatch( +// batch, +// "Vrac > Benthos > Alive not itemized", +// SORTING_TYPE2_ID, +// QualitativeValueId.SORTING_TYPE2_ALIVE_NOT_ITEMIZED.getValue() +// ); +// } + + public SortingBatch getVracInertRootBatch(SortingBatch batch) { + return getSortingBatch(batch, + "Vrac > " + prefix + " > Inert (not alive)", + SORTING_TYPE2_ID, + QualitativeValueId.SORTING_TYPE2_INERT.getValue()); + } + +// public SortingBatch getBenthosVracInertRootBatch(SortingBatch batch) { +// return getSortingBatch( +// batch, +// "Vrac > Benthos > Inert (not alive)", +// SORTING_TYPE2_ID, +// QualitativeValueId.SORTING_TYPE2_INERT.getValue() +// ); +// } + + public SortingBatch getVracAliveItemizedRootBatch(SortingBatch batch) { + return getSortingBatch(batch, + "Vrac > " + prefix + "> Alive itemized", + SORTING_TYPE2_ID, + QualitativeValueId.SORTING_TYPE2_ALIVE_ITEMIZED.getValue()); + } + +// public SortingBatch getBenthosVracAliveItemizedRootBatch(SortingBatch batch) { +// return getSortingBatch( +// batch, +// "Vrac > Benthos > Alive itemized", +// SORTING_TYPE2_ID, +// QualitativeValueId.SORTING_TYPE2_ALIVE_ITEMIZED.getValue() +// ); +// } + + public SortingBatch getHorsVracRootBatch(SortingBatch batch) { + return getSortingBatch(batch, + "Hors Vrac > " + prefix, + SORTING_TYPE_ID, + QualitativeValueId.SORTING_TYPE_SPECIES.getValue()); + } + +// public SortingBatch getBenthosHorsVracRootBatch(SortingBatch batch) { +// return getSortingBatch( +// batch, +// "Hors Vrac > Benthos", +// SORTING_TYPE_ID, +// QualitativeValueId.SORTING_TYPE_BENTHOS.getValue() +// ); +// } + + //------------------------------------------------------------------------// + //-- getOrCreate methods --// + //------------------------------------------------------------------------// + +// public SortingBatch getOrCreateVracBatch(CatchBatch batch, Float weight, Float weightBeforeSampling) { +// return getOrCreate(batch, +// batch, +// "Vrac", +// PmfmId.SORTED_UNSORTED.getValue(), +// QualitativeValueId.SORTED_VRAC.getValue(), +// weight, +// weightBeforeSampling, +// (short) 1); +// } + + public SortingBatch getOrCreateVracRootBatch(CatchBatch target, SortingBatch batch, Float totalWeight) { + return getOrCreate( + target, + batch, + "Vrac > " + prefix, + SORTING_TYPE_ID, + sortingType.getValue(), + totalWeight, + rootBatchRankOrder + ); + } + + public SortingBatch getOrCreateVracAliveNotItemizedRootBatch(CatchBatch target, SortingBatch batch, Float totalWeight) { + return getOrCreate( + target, + batch, + "Vrac > " + prefix + " > Alive Not Itemized", + SORTING_TYPE2_ID, + QualitativeValueId.SORTING_TYPE2_ALIVE_NOT_ITEMIZED.getValue(), + totalWeight, + (short) 1 + ); + } + + public SortingBatch getOrCreateVracInertRootBatch(CatchBatch target, SortingBatch batch, Float totalWeight) { + return getOrCreate( + target, + batch, + "Vrac > " + prefix + " > Inert", + SORTING_TYPE2_ID, + QualitativeValueId.SORTING_TYPE2_INERT.getValue(), + totalWeight, + (short) 2 + ); + } + + public SortingBatch getOrCreateVracAliveItemizedRootBatch(CatchBatch target, SortingBatch batch) { + return getOrCreate( + target, + batch, + "Vrac > " + prefix + " > Alive Itemized", + SORTING_TYPE2_ID, + QualitativeValueId.SORTING_TYPE2_ALIVE_ITEMIZED.getValue(), + (short) 3 + ); + } + +// public SortingBatch getOrCreateBenthosVracRootBatch(CatchBatch target, +// SortingBatch batch, +// Float totalWeight) { +// return getOrCreate( +// target, +// batch, +// "Vrac > Benthos", +// SORTING_TYPE_ID, +// QualitativeValueId.SORTING_TYPE_BENTHOS.getValue(), +// totalWeight, +// (short) 2 +// ); +// } + +// public SortingBatch getOrCreateBenthosVracAliveNotItemizedRootBatch(CatchBatch target, +// SortingBatch batch, +// Float totalWeight) { +// return getOrCreate( +// target, +// batch, +// "Vrac > Benthos > Alive Not itemized", +// SORTING_TYPE2_ID, +// QualitativeValueId.SORTING_TYPE2_ALIVE_NOT_ITEMIZED.getValue(), +// totalWeight, +// (short) 1 +// ); +// } + +// public SortingBatch getOrCreateBenthosVracInertRootBatch(CatchBatch target, +// SortingBatch batch, +// Float totalWeight) { +// return getOrCreate( +// target, +// batch, +// "Vrac > Benthos > Inert", +// SORTING_TYPE2_ID, +// QualitativeValueId.SORTING_TYPE2_INERT.getValue(), +// totalWeight, +// (short) 2 +// ); +// } + +// public SortingBatch getOrCreateBenthosVracAliveItemizedRootBatch(CatchBatch target, +// SortingBatch batch) { +// return getOrCreate( +// target, +// batch, +// "Vrac > Benthos > Alive Itemized", +// SORTING_TYPE2_ID, +// QualitativeValueId.SORTING_TYPE2_ALIVE_ITEMIZED.getValue(), +// (short) 3 +// ); +// } + +// public SortingBatch getOrCreateHorsVracBatch(CatchBatch batch) { +// return getOrCreate( +// batch, +// batch, +// "Hors Vrac", +// PmfmId.SORTED_UNSORTED.getValue(), +// QualitativeValueId.SORTED_HORS_VRAC.getValue(), +// (short) 2 +// ); +// } + + public SortingBatch getOrCreateHorsVracRootBatch(CatchBatch target, SortingBatch batch) { + return getOrCreate( + target, + batch, + "Hors Vrac > " + prefix, + SORTING_TYPE_ID, + sortingType.getValue(), + rootBatchRankOrder + ); + } + +// public SortingBatch getOrCreateBenthosHorsVracRootBatch(CatchBatch target, +// SortingBatch batch) { +// return getOrCreate( +// target, +// batch, +// "Hors Vrac > Benthos", +// SORTING_TYPE_ID, +// QualitativeValueId.SORTING_TYPE_BENTHOS.getValue(), +// (short) 2 +// ); +// } + + + private Integer convertSampleCategoryValueIntoQualitativeId(Serializable value) { + if (value == null) { + return null; + } + Integer qualitativeValueId = null; + if (value instanceof CaracteristicQualitativeValue) { + CaracteristicQualitativeValue cqValue = (CaracteristicQualitativeValue) value; + qualitativeValueId = cqValue.getIdAsInt(); + } else if (value instanceof String) { + qualitativeValueId = Integer.valueOf((String) value); + } + return qualitativeValueId; + } + +} diff --git a/tutti-persistence/src/main/xmi/tutti-persistence.properties b/tutti-persistence/src/main/xmi/tutti-persistence.properties index 9606d6f..d92795b 100644 --- a/tutti-persistence/src/main/xmi/tutti-persistence.properties +++ b/tutti-persistence/src/main/xmi/tutti-persistence.properties @@ -37,7 +37,6 @@ fr.ifremer.tutti.persistence.entities.data.FishingOperation.attribute.recorderPe fr.ifremer.tutti.persistence.entities.data.FishingOperation.attribute.secondaryVessel.stereotype=ordered fr.ifremer.tutti.persistence.entities.data.SpeciesBatch.attribute.childBatchs.stereotype=ordered -fr.ifremer.tutti.persistence.entities.data.BenthosBatch.attribute.childBatchs.stereotype=ordered fr.ifremer.tutti.persistence.entities.protocol.TuttiProtocol.attribute.caracteristicMapping.stereotype=ordered fr.ifremer.tutti.persistence.entities.protocol.TuttiProtocol.attribute.individualObservationPmfmId.stereotype=ordered diff --git a/tutti-persistence/src/main/xmi/tutti-persistence.zargo b/tutti-persistence/src/main/xmi/tutti-persistence.zargo index dd234c0..c721325 100644 Binary files a/tutti-persistence/src/main/xmi/tutti-persistence.zargo and b/tutti-persistence/src/main/xmi/tutti-persistence.zargo differ diff --git a/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/BenthosBatchPersistenceServiceReadTest.java b/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/BenthosBatchPersistenceServiceReadTest.java index ca8210d..4342cbd 100644 --- a/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/BenthosBatchPersistenceServiceReadTest.java +++ b/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/BenthosBatchPersistenceServiceReadTest.java @@ -25,7 +25,6 @@ package fr.ifremer.tutti.persistence.service; import com.google.common.collect.Maps; import fr.ifremer.adagio.core.dao.referential.pmfm.PmfmId; import fr.ifremer.tutti.persistence.entities.data.BatchContainer; -import fr.ifremer.tutti.persistence.entities.data.BenthosBatch; import fr.ifremer.tutti.persistence.entities.data.FishingOperation; import fr.ifremer.tutti.persistence.entities.data.SpeciesBatch; import fr.ifremer.tutti.persistence.entities.data.SpeciesBatchFrequency; @@ -158,18 +157,18 @@ public class BenthosBatchPersistenceServiceReadTest { } } - public BenthosBatch getBenthosBatch(Integer fishingOperationId, + public SpeciesBatch getBenthosBatch(Integer fishingOperationId, Integer speciesBatchId) { - BatchContainer<BenthosBatch> rootSpeciesBatch = service.getRootBenthosBatch(fishingOperationId, false); + BatchContainer<SpeciesBatch> rootSpeciesBatch = service.getRootBenthosBatch(fishingOperationId, false); return getBenthosBatch(speciesBatchId, rootSpeciesBatch.getChildren()); } - public static BenthosBatch getBenthosBatch(Integer speciesBatchId, - List<BenthosBatch> speciesBatchs) { + public static SpeciesBatch getBenthosBatch(Integer speciesBatchId, + List<SpeciesBatch> speciesBatchs) { if (speciesBatchs == null) { return null; } - for (BenthosBatch speciesBatch : speciesBatchs) { + for (SpeciesBatch speciesBatch : speciesBatchs) { if (speciesBatchId.equals(speciesBatch.getId())) { return speciesBatch; } diff --git a/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/BenthosBatchPersistenceServiceWriteTest.java b/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/BenthosBatchPersistenceServiceWriteTest.java index 3fd2ddd..6ccd158 100644 --- a/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/BenthosBatchPersistenceServiceWriteTest.java +++ b/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/BenthosBatchPersistenceServiceWriteTest.java @@ -25,14 +25,14 @@ package fr.ifremer.tutti.persistence.service; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import fr.ifremer.adagio.core.dao.referential.pmfm.PmfmId; -import fr.ifremer.tutti.persistence.entities.data.BenthosBatch; -import fr.ifremer.tutti.persistence.entities.data.BenthosBatchFrequency; -import fr.ifremer.tutti.persistence.entities.data.BenthosBatchFrequencys; -import fr.ifremer.tutti.persistence.entities.data.BenthosBatchs; import fr.ifremer.tutti.persistence.entities.data.CatchBatch; import fr.ifremer.tutti.persistence.entities.data.CatchBatchs; import fr.ifremer.tutti.persistence.entities.data.Cruise; import fr.ifremer.tutti.persistence.entities.data.FishingOperation; +import fr.ifremer.tutti.persistence.entities.data.SpeciesBatch; +import fr.ifremer.tutti.persistence.entities.data.SpeciesBatchFrequency; +import fr.ifremer.tutti.persistence.entities.data.SpeciesBatchFrequencys; +import fr.ifremer.tutti.persistence.entities.data.SpeciesBatchs; import fr.ifremer.tutti.persistence.entities.referential.Caracteristic; import fr.ifremer.tutti.persistence.entities.referential.CaracteristicQualitativeValue; import fr.ifremer.tutti.persistence.entities.referential.CaracteristicType; @@ -199,8 +199,8 @@ public class BenthosBatchPersistenceServiceWriteTest { List<Caracteristic> cara = caracteristicService.getAllCaracteristic(); for (Caracteristic caracteristic : cara) { if (caracteristic.getCaracteristicType() == CaracteristicType.NUMBER - && caracteristic.getPrecision() != null - && caracteristic.getPrecision() == 0.5f) { + && caracteristic.getPrecision() != null + && caracteristic.getPrecision() == 0.5f) { frequencyPMFM = caracteristic; break; } @@ -211,10 +211,10 @@ public class BenthosBatchPersistenceServiceWriteTest { @Test public void createAndSaveBenthosBatchAndFrequencies() { - BenthosBatch esp1Batch; - BenthosBatch esp2Batch; - BenthosBatch frequenciesParentBatch; - BenthosBatch batch; + SpeciesBatch esp1Batch; + SpeciesBatch esp2Batch; + SpeciesBatch frequenciesParentBatch; + SpeciesBatch batch; Species taxon1 = species.get(0); Species taxon2 = species.get(1); @@ -222,7 +222,7 @@ public class BenthosBatchPersistenceServiceWriteTest { // 1. Test with only mandatory properties // ----------------------------------------------------------------------------- // batch : "ESP1 - Vrac/5" - batch = BenthosBatchs.newBenthosBatch(); + batch = SpeciesBatchs.newBenthosBatch(); batch.setParentBatch(null); batch.setFishingOperation(fishingOperationNoCatchBatch); batch.setSpecies(taxon1); @@ -239,7 +239,7 @@ public class BenthosBatchPersistenceServiceWriteTest { // 2. Test child "Male/2" // ----------------------------------------------------------------------------- // Batch : ESP1 - Vrac/5 Male/2 ss-ech/1 Nombre/7 - batch = BenthosBatchs.newBenthosBatch(); + batch = SpeciesBatchs.newBenthosBatch(); batch.setId((String) null); batch.setParentBatch(esp1Batch); batch.setSpecies(taxon1); @@ -256,7 +256,7 @@ public class BenthosBatchPersistenceServiceWriteTest { // 3. Test child "Female/2" // ----------------------------------------------------------------------------- // Batch : ESP1 - Vrac/5 Female/3 Nombre/14 - batch = BenthosBatchs.newBenthosBatch(); + batch = SpeciesBatchs.newBenthosBatch(); batch.setId((String) null); batch.setParentBatch(esp1Batch); batch.setSpecies(taxon1); @@ -274,7 +274,7 @@ public class BenthosBatchPersistenceServiceWriteTest { // \- ESP2 - Vrac/7 UNK/2 ss-ech/1 Nombre/11 // ----------------------------------------------------------------------------- // batch : "ESP2 - Vrac/7 " - batch = BenthosBatchs.newBenthosBatch(); + batch = SpeciesBatchs.newBenthosBatch(); batch.setParentBatch(null); batch.setSpecies(taxon2); batch.setSampleCategoryId(PmfmId.SORTED_UNSORTED.getValue()); @@ -285,7 +285,7 @@ public class BenthosBatchPersistenceServiceWriteTest { esp2Batch = batch; // Batch : ESP2 - Vrac/7 UNK/2 ss-ech/1 Nombre/11 - batch = BenthosBatchs.newBenthosBatch(); + batch = SpeciesBatchs.newBenthosBatch(); batch.setId((String) null); batch.setParentBatch(esp2Batch); batch.setSpecies(taxon2); @@ -311,9 +311,9 @@ public class BenthosBatchPersistenceServiceWriteTest { batch.setNumber(99); // Save and reload, then check - BenthosBatch savedBatch = service.saveBenthosBatch(batch); + SpeciesBatch savedBatch = service.saveBenthosBatch(batch); assertBenthosBatch(savedBatch, batch, false); - BenthosBatch reloadedBatch = getBenthosBatch(fishingOperationWithEmptyBatch.getIdAsInt(), savedBatch.getIdAsInt()); + SpeciesBatch reloadedBatch = getBenthosBatch(fishingOperationWithEmptyBatch.getIdAsInt(), savedBatch.getIdAsInt()); assertBenthosBatch(savedBatch, reloadedBatch, true); // Save batch for later @@ -331,7 +331,7 @@ public class BenthosBatchPersistenceServiceWriteTest { // ----------------------------------------------------------------------------- // 7. Test get all root species // ----------------------------------------------------------------------------- - List<BenthosBatch> rootBenthosBatch = service.getRootBenthosBatch(fishingOperationWithEmptyBatch.getIdAsInt(), false).getChildren(); + List<SpeciesBatch> rootBenthosBatch = service.getRootBenthosBatch(fishingOperationWithEmptyBatch.getIdAsInt(), false).getChildren(); assertNotNull(rootBenthosBatch); assertEquals(2, rootBenthosBatch.size()); assertNotNull(rootBenthosBatch.get(0).getChildBatchs()); @@ -343,10 +343,10 @@ public class BenthosBatchPersistenceServiceWriteTest { // 8. Test batch frequency creation // ----------------------------------------------------------------------------- - List<BenthosBatchFrequency> frequencies = Lists.newArrayList(); + List<SpeciesBatchFrequency> frequencies = Lists.newArrayList(); float lengthStep = 0.5f; for (float length = lengthStep; length < lengthStep * 20; length += lengthStep) { - BenthosBatchFrequency frequency = BenthosBatchFrequencys.newBenthosBatchFrequency(); + SpeciesBatchFrequency frequency = SpeciesBatchFrequencys.newBenthosBatchFrequency(); frequency.setLengthStep(length); frequency.setNumber((int) (length * 2)); frequency.setWeight(0.01f * length * 2); @@ -354,13 +354,13 @@ public class BenthosBatchPersistenceServiceWriteTest { frequency.setBatch(frequenciesParentBatch); frequencies.add(frequency); } - List<BenthosBatchFrequency> createdFrequencies = assertCreateAndReloadBenthosBatchFrequency(frequencies, frequenciesParentBatch.getIdAsInt()); + List<SpeciesBatchFrequency> createdFrequencies = assertCreateAndReloadBenthosBatchFrequency(frequencies, frequenciesParentBatch.getIdAsInt()); // ----------------------------------------------------------------------------- // 9. Test batch frequency update // ----------------------------------------------------------------------------- // Update some batchs (1cm, 2cm, etc) - for (BenthosBatchFrequency speciesBatchFrequency : createdFrequencies) { + for (SpeciesBatchFrequency speciesBatchFrequency : createdFrequencies) { float length = speciesBatchFrequency.getLengthStep(); if ((float) (int) length == length) { speciesBatchFrequency.setNumber(12); @@ -371,21 +371,21 @@ public class BenthosBatchPersistenceServiceWriteTest { createdFrequencies = Lists.newArrayList(createdFrequencies); createdFrequencies.remove(createdFrequencies.size() - 1); - List<BenthosBatchFrequency> savedFrequencies = service.saveBenthosBatchFrequency(frequenciesParentBatch.getIdAsInt(), createdFrequencies); + List<SpeciesBatchFrequency> savedFrequencies = service.saveBenthosBatchFrequency(frequenciesParentBatch.getIdAsInt(), createdFrequencies); assertBatchFrequencies(createdFrequencies, savedFrequencies, true); } @Test public void deleteBenthosBatch(/* String id */) { - BenthosBatch esp1Batch; - BenthosBatch batch; + SpeciesBatch esp1Batch; + SpeciesBatch batch; Species taxon1 = species.get(0); // ----------------------------------------------------------------------------- // 1. Create two batchs (parent + child), then remove the parent batch // ----------------------------------------------------------------------------- // batch : ESP1 Vrac/5 - batch = BenthosBatchs.newBenthosBatch(); + batch = SpeciesBatchs.newBenthosBatch(); batch.setParentBatch(null); batch.setFishingOperation(fishingOperationWithEmptyBatch); batch.setSpecies(taxon1); @@ -396,7 +396,7 @@ public class BenthosBatchPersistenceServiceWriteTest { esp1Batch = batch; // batch : ESP1 Vrac/5 Male/2 - batch = BenthosBatchs.newBenthosBatch(); + batch = SpeciesBatchs.newBenthosBatch(); batch.setParentBatch(esp1Batch); batch.setFishingOperation(fishingOperationWithEmptyBatch); batch.setSpecies(taxon1); @@ -417,15 +417,15 @@ public class BenthosBatchPersistenceServiceWriteTest { } } - protected void assertCreateAndReloadBenthosBatch(BenthosBatch batch, Integer parentBatchId) { + protected void assertCreateAndReloadBenthosBatch(SpeciesBatch batch, Integer parentBatchId) { batch.setFishingOperation(fishingOperationWithEmptyBatch); // Create batch - BenthosBatch createdBatch = service.createBenthosBatch(batch, parentBatchId, true); + SpeciesBatch createdBatch = service.createBenthosBatch(batch, parentBatchId, true); assertBenthosBatch(batch, createdBatch, false); // then reload (for round trip check) - BenthosBatch reloadedBatch = getBenthosBatch(fishingOperationWithEmptyBatch.getIdAsInt(), createdBatch.getIdAsInt()); + SpeciesBatch reloadedBatch = getBenthosBatch(fishingOperationWithEmptyBatch.getIdAsInt(), createdBatch.getIdAsInt()); if (parentBatchId == null) { assertNull(reloadedBatch.getParentBatch()); } else { @@ -437,7 +437,7 @@ public class BenthosBatchPersistenceServiceWriteTest { batch.setId(createdBatch.getId()); } - protected void assertBenthosBatch(BenthosBatch expectedBatch, BenthosBatch actualBatch, boolean assertIdEquals) { + protected void assertBenthosBatch(SpeciesBatch expectedBatch, SpeciesBatch actualBatch, boolean assertIdEquals) { assertNotNull(actualBatch); assertNotNull(actualBatch.getId()); if (assertIdEquals && expectedBatch.getId() != null) { @@ -447,7 +447,7 @@ public class BenthosBatchPersistenceServiceWriteTest { assertEquals(expectedBatch.getSampleCategoryId(), actualBatch.getSampleCategoryId()); if (expectedBatch.getSampleCategoryValue() != null && expectedBatch.getSampleCategoryValue() instanceof CaracteristicQualitativeValue) { assertNotNull("Bad sampleCategoryValue : expected <" + ((CaracteristicQualitativeValue) expectedBatch.getSampleCategoryValue()).getId() - + "> but was <null>", + + "> but was <null>", actualBatch.getSampleCategoryValue() ); assertEquals( @@ -464,40 +464,40 @@ public class BenthosBatchPersistenceServiceWriteTest { // (Because getBenthosBatch(id) could not always retrieve the species) if (expectedBatch.getSpecies() != null && ( PmfmId.SORTED_UNSORTED.getValue().equals(expectedBatch.getSampleCategoryId()) - || actualBatch.getSpecies() != null)) { + || actualBatch.getSpecies() != null)) { assertNotNull(actualBatch.getSpecies()); assertEquals(expectedBatch.getSpecies().getId(), actualBatch.getSpecies().getId()); } } - protected List<BenthosBatchFrequency> assertCreateAndReloadBenthosBatchFrequency(List<BenthosBatchFrequency> frequencies, Integer parentBatchId) { + protected List<SpeciesBatchFrequency> assertCreateAndReloadBenthosBatchFrequency(List<SpeciesBatchFrequency> frequencies, Integer parentBatchId) { // Create batch - List<BenthosBatchFrequency> createdFrequencies = service.saveBenthosBatchFrequency(parentBatchId, frequencies); + List<SpeciesBatchFrequency> createdFrequencies = service.saveBenthosBatchFrequency(parentBatchId, frequencies); assertBatchFrequencies(frequencies, createdFrequencies, false); // then reload (for round trip check) - List<BenthosBatchFrequency> reloadedFrequencies = service.getAllBenthosBatchFrequency(parentBatchId); + List<SpeciesBatchFrequency> reloadedFrequencies = service.getAllBenthosBatchFrequency(parentBatchId); assertBatchFrequencies(createdFrequencies, reloadedFrequencies, true); return createdFrequencies; } - protected void assertBatchFrequencies(List<BenthosBatchFrequency> expectedFrequencies, - List<BenthosBatchFrequency> actualFrequencies, + protected void assertBatchFrequencies(List<SpeciesBatchFrequency> expectedFrequencies, + List<SpeciesBatchFrequency> actualFrequencies, boolean assertIdEquals) { assertNotNull(actualFrequencies); assertEquals(expectedFrequencies.size(), actualFrequencies.size()); // Store actual batches into a map, using the length as key - Map<Float, BenthosBatchFrequency> expectedLengthMap = Maps.newHashMap(); - for (BenthosBatchFrequency speciesBatchFrequency : expectedFrequencies) { + Map<Float, SpeciesBatchFrequency> expectedLengthMap = Maps.newHashMap(); + for (SpeciesBatchFrequency speciesBatchFrequency : expectedFrequencies) { expectedLengthMap.put(speciesBatchFrequency.getLengthStep(), speciesBatchFrequency); } // Store expected batches into a map, using the length as key - Map<Float, BenthosBatchFrequency> actualLengthMap = Maps.newHashMap(); - for (BenthosBatchFrequency speciesBatchFrequency : actualFrequencies) { + Map<Float, SpeciesBatchFrequency> actualLengthMap = Maps.newHashMap(); + for (SpeciesBatchFrequency speciesBatchFrequency : actualFrequencies) { assertFalse("Duplicate lengthStep found in batchFrequencies, for length=" + speciesBatchFrequency.getLengthStep(), actualLengthMap.containsKey(speciesBatchFrequency.getLengthStep())); actualLengthMap.put(speciesBatchFrequency.getLengthStep(), speciesBatchFrequency); @@ -505,8 +505,8 @@ public class BenthosBatchPersistenceServiceWriteTest { } for (Float lengthStep : expectedLengthMap.keySet()) { - BenthosBatchFrequency expectedBatchFrequency = expectedLengthMap.get(lengthStep); - BenthosBatchFrequency actualBatchFrequency = actualLengthMap.get(lengthStep); + SpeciesBatchFrequency expectedBatchFrequency = expectedLengthMap.get(lengthStep); + SpeciesBatchFrequency actualBatchFrequency = actualLengthMap.get(lengthStep); if (assertIdEquals) { assertEquals(expectedBatchFrequency.getId(), actualBatchFrequency.getId()); } @@ -519,15 +519,15 @@ public class BenthosBatchPersistenceServiceWriteTest { } } - protected BenthosBatch getBenthosBatch(Integer fishingOperationId, Integer speciesBatchId) { + protected SpeciesBatch getBenthosBatch(Integer fishingOperationId, Integer speciesBatchId) { return getBenthosBatch(speciesBatchId, service.getRootBenthosBatch(fishingOperationId, false).getChildren()); } - protected BenthosBatch getBenthosBatch(Integer speciesBatchId, List<BenthosBatch> speciesBatchs) { + protected SpeciesBatch getBenthosBatch(Integer speciesBatchId, List<SpeciesBatch> speciesBatchs) { if (speciesBatchs == null) { return null; } - for (BenthosBatch speciesBatch : speciesBatchs) { + for (SpeciesBatch speciesBatch : speciesBatchs) { if (speciesBatchId.equals(speciesBatch.getIdAsInt())) { return speciesBatch; } -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@codelutin.com>.