r414 - in trunk/tutti-persistence: . src/main/java/fr/ifremer/tutti/persistence src/main/java/fr/ifremer/tutti/persistence/entities src/main/java/fr/ifremer/tutti/persistence/service src/main/java/fr/ifremer/tutti/persistence/service/synchro src/main/resources src/test/java/fr/ifremer/tutti/persistence src/test/java/fr/ifremer/tutti/persistence/service src/test/java/fr/ifremer/tutti/persistence/service/synchro
Author: tchemit Date: 2013-02-15 00:53:34 +0100 (Fri, 15 Feb 2013) New Revision: 414 Url: http://forge.codelutin.com/projects/tutti/repository/revisions/414 Log: improve synchro (add progressionModel in result) + add missingsvnproperties,... Added: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/ProgressionModel.java Modified: trunk/tutti-persistence/pom.xml trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/entities/TuttiEntities.java trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/BatchPersistenceService.java trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/BatchPersistenceServiceImpl.java trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/synchro/ReferentialSynchronizeHelper.java trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/synchro/ReferentialSynchronizeResult.java trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/synchro/ReferentialSynchronizeService.java trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/synchro/ReferentialSynchronizeServiceImpl.java trunk/tutti-persistence/src/main/resources/tutti-db-enumerations.properties trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/DatabaseResource.java trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/BatchPersistenceServiceReadTest.java trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/BatchPersistenceServiceWriteTest.java trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/synchro/ReferentialSynchronizeHelperTest.java trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/synchro/ReferentialSynchronizeServiceImplTest.java trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/synchro/ReferentialSynchronizeServiceImplUpTodateTest.java Modified: trunk/tutti-persistence/pom.xml =================================================================== --- trunk/tutti-persistence/pom.xml 2013-02-14 15:27:16 UTC (rev 413) +++ trunk/tutti-persistence/pom.xml 2013-02-14 23:53:34 UTC (rev 414) @@ -88,6 +88,11 @@ <artifactId>commons-dbcp</artifactId> </dependency> + <dependency> + <groupId>org.swinglabs.swingx</groupId> + <artifactId>swingx-common</artifactId> + </dependency> + <!-- Yaml --> <dependency> Added: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/ProgressionModel.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/ProgressionModel.java (rev 0) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/ProgressionModel.java 2013-02-14 23:53:34 UTC (rev 414) @@ -0,0 +1,101 @@ +package fr.ifremer.tutti.persistence; + +/* + * #%L + * Tutti :: Persistence + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2012 - 2013 Ifremer + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/gpl-3.0.html>. + * #L% + */ + +import org.jdesktop.beans.AbstractSerializableBean; + +/** + * Simple model for a progression long task. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.0 + */ +public class ProgressionModel extends AbstractSerializableBean { + + private static final long serialVersionUID = 1L; + + public static final String PROPERTY_TOTAL = "total"; + + public static final String PROPERTY_CURRENT = "current"; + + public static final String PROPERTY_RATE = "rate"; + + public static final String PROPERTY_MESSAGE = "message"; + + protected int total; + + protected int current; + + protected float rate; + + protected String message; + + public long getTotal() { + return total; + } + + public void setTotal(int total) { + Object oldValue = getTotal(); + this.total = total; + firePropertyChange(PROPERTY_TOTAL, oldValue, total); + setCurrent(0); + } + + public long getCurrent() { + return current; + } + + public void setCurrent(int current) { + Object oldValue = getCurrent(); + this.current = current; + firePropertyChange(PROPERTY_CURRENT, oldValue, current); + } + + public void increments(int nb) { + setCurrent(current + nb); + setRate(getCurrent() / getTotal()); + } + + public float getRate() { + return rate; + } + + public void setRate(float rate) { + Object oldValue = getRate(); + this.rate = rate; + firePropertyChange(PROPERTY_RATE, oldValue, rate); + + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + Object oldValue = getMessage(); + this.message = message; + firePropertyChange(PROPERTY_MESSAGE, oldValue, message); + } +} Property changes on: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/ProgressionModel.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/entities/TuttiEntities.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/entities/TuttiEntities.java 2013-02-14 15:27:16 UTC (rev 413) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/entities/TuttiEntities.java 2013-02-14 23:53:34 UTC (rev 414) @@ -223,6 +223,10 @@ p.put(Environment.PASS, password); } + public static String getJdbcUrl(File directory, String dbName) { + return "jdbc:hsqldb:file:" + directory.getAbsolutePath() + "/" + dbName; + } + protected static class IdPredicate<B extends IdAware> implements Predicate<B> { private final String id; Modified: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/BatchPersistenceService.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/BatchPersistenceService.java 2013-02-14 15:27:16 UTC (rev 413) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/BatchPersistenceService.java 2013-02-14 23:53:34 UTC (rev 414) @@ -1,8 +1,33 @@ -package fr.ifremer.tutti.persistence.service; - -import org.springframework.transaction.annotation.Transactional; - -@Transactional(readOnly = true) -public interface BatchPersistenceService extends CatchBatchPersistenceService, SpeciesBatchPersistenceService{ - -} +package fr.ifremer.tutti.persistence.service; + +/* + * #%L + * Tutti :: Persistence + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2012 - 2013 Ifremer + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/gpl-3.0.html>. + * #L% + */ + + +import org.springframework.transaction.annotation.Transactional; + +@Transactional(readOnly = true) +public interface BatchPersistenceService extends CatchBatchPersistenceService, SpeciesBatchPersistenceService { + +} \ No newline at end of file Property changes on: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/BatchPersistenceService.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/BatchPersistenceServiceImpl.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/BatchPersistenceServiceImpl.java 2013-02-14 15:27:16 UTC (rev 413) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/BatchPersistenceServiceImpl.java 2013-02-14 23:53:34 UTC (rev 414) @@ -1,1297 +1,1322 @@ -package fr.ifremer.tutti.persistence.service; - -import static org.nuiton.i18n.I18n._; - -import java.io.Serializable; -import java.text.MessageFormat; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import javax.annotation.Resource; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.hibernate.FlushMode; -import org.hibernate.type.IntegerType; -import org.springframework.cache.annotation.CacheEvict; -import org.springframework.dao.DataIntegrityViolationException; -import org.springframework.dao.DataRetrievalFailureException; -import org.springframework.stereotype.Service; - -import com.google.common.base.Preconditions; -import com.google.common.collect.Lists; - -import fr.ifremer.adagio.core.dao.data.batch.Batch; -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.batch.SortingBatchDao; -import fr.ifremer.adagio.core.dao.data.batch.denormalized.DenormalizedBatchDao; -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.batch.validator.CatchBatchValidator; -import fr.ifremer.adagio.core.dao.data.measure.QuantificationMeasurement; -import fr.ifremer.adagio.core.dao.data.measure.SortingMeasurement; -import fr.ifremer.adagio.core.dao.data.operation.FishingOperationImpl; -import fr.ifremer.adagio.core.dao.referential.QualityFlagImpl; -import fr.ifremer.adagio.core.dao.referential.taxon.ReferenceTaxon; -import fr.ifremer.adagio.core.dao.referential.taxon.ReferenceTaxonImpl; -import fr.ifremer.adagio.core.dao.technical.synchronization.SynchronizationStatus; -import fr.ifremer.tutti.persistence.entities.data.CatchBatch; -import fr.ifremer.tutti.persistence.entities.data.SampleCategoryEnum; -import fr.ifremer.tutti.persistence.entities.data.SpeciesBatch; -import fr.ifremer.tutti.persistence.entities.data.SpeciesBatchFrequency; -import fr.ifremer.tutti.persistence.entities.referential.Caracteristic; -import fr.ifremer.tutti.persistence.entities.referential.CaracteristicQualitativeValue; -import fr.ifremer.tutti.persistence.entities.referential.CaracteristicType; -import fr.ifremer.tutti.persistence.entities.referential.Species; -import fr.ifremer.tutti.persistence.service.measure.MeasurementPersistenceHelper; - -@Service("batchPersistenceService") -public class BatchPersistenceServiceImpl - extends AbstractPersistenceService implements BatchPersistenceService, CatchBatchValidator - { - - /** Logger. */ - private static final Log log = - LogFactory.getLog(BatchPersistenceServiceImpl.class); - - @Resource(name = "referentialPersistenceService") - protected ReferentialPersistenceService referentialService; - - @Resource(name = "denormalizedBatchDao") - protected DenormalizedBatchDao denormalizedBatchDao; - - @Resource(name = "sortingBatchDao") - protected SortingBatchDao sortingBatchDao; - - @Resource(name = "catchBatchDao") - protected CatchBatchExtendDao catchBatchDao; - - @Resource(name = "measurementPersistenceHelper") - protected MeasurementPersistenceHelper measurementHelper; - - @Override - public void init() { - super.init(); - catchBatchDao.registerCatchBatchValidator(this); - } - - @Override - public void close() { - catchBatchDao.unregisterCatchBatchValidator(this); - super.close(); - } - - @Override - public boolean isEnable(fr.ifremer.adagio.core.dao.data.batch.CatchBatch catchBatch) { - // Apply validation only on catch batch for fishingOperation - return (catchBatch.getFishingOperation() != null); - } - - @Override - public List<CatchBatchValidationError> validate(fr.ifremer.adagio.core.dao.data.batch.CatchBatch catchBatch) { - List<CatchBatchValidationError> errors = Lists.newArrayList(); - validate(catchBatch.getChildBatchs(), errors, 1); - return errors; - } - - @Override - public CatchBatch getCatchBatchFromFishingOperation(String fishingOperationId) { - Preconditions.checkNotNull(fishingOperationId); - - Integer catchBatchId = catchBatchDao.getIdByFishingOperationId(Integer.valueOf(fishingOperationId)); - - fr.ifremer.adagio.core.dao.data.batch.CatchBatch source; - try { - source = catchBatchDao.loadFullTree(catchBatchId, true, true); - } catch (CatchBatchValidationException e) { - throw new DataRetrievalFailureException("Invalid batch tree structure, for fishing operation id=" + fishingOperationId, e); - } - - CatchBatch result = new CatchBatch(); - result.setId(source.getId().toString()); - result.setCatchTotalWeight(source.getWeight()); - - // Vrac - SortingBatch vracBatch = catchBatchDao.getSortingBatch(source.getChildBatchs(), - "pmfmId", enumeration.PMFM_ID_SORTED_UNSORTED, enumeration.QUALITATIVE_VRAC_ID); - result.setCatchTotalSortedCarousselWeight(vracBatch.getWeight()); - result.setCatchTotalSortedTremisWeight(vracBatch.getWeightBeforeSampling()); - { - // Species - SortingBatch speciesBatch = catchBatchDao.getSortingBatch(vracBatch.getChildBatchs(), - "pmfmId", enumeration.PMFM_ID_SORTING_TYPE, enumeration.QUALITATIVE_ID_SORTING_TYPE_SPECIES); - - result.setSpeciesTotalSampleSortedWeight(speciesBatch.getWeight()); - result.setSpeciesTotalSortedWeight(speciesBatch.getWeightBeforeSampling()); - - { - // Inert - SortingBatch inertBatch = catchBatchDao.getSortingBatch(speciesBatch.getChildBatchs(), - "referenceTaxonId", enumeration.REFERENCE_TAXON_ID_INERT); - result.setSpeciesTotalInertWeight(inertBatch.getWeight()); - - // Alive no itemized - SortingBatch livingNotItemizedBatch = catchBatchDao.getSortingBatch(speciesBatch.getChildBatchs(), - "referenceTaxonId", enumeration.REFERENCE_TAXON_ID_LIFE); - result.setSpeciesTotalLivingNotItemizedWeight(livingNotItemizedBatch.getWeight()); - } - - // TODO : Benthos, Plancton... - } - - // Hors Vrac - SortingBatch horsVracBatch = catchBatchDao.getSortingBatch(source.getChildBatchs(), - "pmfmId", enumeration.PMFM_ID_SORTED_UNSORTED, enumeration.QUALITATIVE_HORS_VRAC_ID); - result.setCatchTotalUnsortedWeight(horsVracBatch.getWeight()); - { - // Species - SortingBatch speciesBatch = catchBatchDao.getSortingBatch(horsVracBatch.getChildBatchs(), - "pmfmId", enumeration.PMFM_ID_SORTING_TYPE, enumeration.QUALITATIVE_ID_SORTING_TYPE_SPECIES); - - result.setSpeciesTotalUnsortedWeight(speciesBatch.getWeight()); - - // TODO : Benthos, Plancton... - } - - // Non trié - SortingBatch unsortedBatch = catchBatchDao.getSortingBatch(source.getChildBatchs(), - "pmfmId", enumeration.PMFM_ID_SORTED_UNSORTED, enumeration.QUALITATIVE_UNSORTED_ID); - result.setCatchTotalRejectedWeight(unsortedBatch.getWeight()); - - return result; - } - - @Override - - public CatchBatch createCatchBatch(CatchBatch bean) { - Preconditions.checkNotNull(bean); - Preconditions.checkArgument(bean.getId() == null); - Preconditions.checkNotNull(bean.getFishingOperation()); - Preconditions.checkNotNull(bean.getFishingOperation().getId()); - - fr.ifremer.adagio.core.dao.data.batch.CatchBatch catchBatch = fr.ifremer.adagio.core.dao.data.batch.CatchBatch.Factory.newInstance(); - beanToEntity(bean, catchBatch, true); - catchBatch = catchBatchDao.create(catchBatch); - bean.setId(String.valueOf(catchBatch.getId())); - - // Link to fishing operation - getCurrentSession().flush(); - Integer fishingOperationId = Integer.valueOf(bean.getFishingOperation().getId()); - int rowUpdated = queryUpdate("updateFishingOperationCatchBatch", - "fishingOperationId", IntegerType.INSTANCE, fishingOperationId, - "catchBatchId", IntegerType.INSTANCE, catchBatch.getId()); - if (rowUpdated == 0) { - throw new DataIntegrityViolationException("Could not attach catch batch to the given operation : operation was not found."); - } - - return bean; - } - - @Override - @CacheEvict(value = "batchTree", key="#bean.fishingOperation.id") - public CatchBatch saveCatchBatch(CatchBatch bean) { - - Preconditions.checkNotNull(bean); - Preconditions.checkNotNull(bean.getId()); - - getCurrentSession().enableFetchProfile("batch-with-childs"); - getCurrentSession().setFlushMode(FlushMode.COMMIT); - fr.ifremer.adagio.core.dao.data.batch.CatchBatch catchBatch = catchBatchDao.load(Integer.valueOf(bean.getId())); - if (catchBatch == null) { - throw new DataRetrievalFailureException("Could not retrieve catch batch with id=" + bean.getId()); - } - - beanToEntity(bean, catchBatch, true); - catchBatchDao.update(catchBatch); - getCurrentSession().flush(); - - return bean; - } - - @Override - public List<SpeciesBatch> getAllRootSpeciesBatch(String fishingOperationId) { - - // Load batch tree - Integer catchBatchId = catchBatchDao.getIdByFishingOperationId(Integer.valueOf(fishingOperationId)); - fr.ifremer.adagio.core.dao.data.batch.CatchBatch catchBatch = catchBatchDao.loadFullTree(catchBatchId); - - List<SpeciesBatch> result = new ArrayList<SpeciesBatch>(); - - // Vrac / Species - SortingBatch vracSpeciesBatch = catchBatchDao.getSortingBatch(catchBatch.getChildBatchs(), - "pmfmId", enumeration.PMFM_ID_SORTED_UNSORTED, enumeration.QUALITATIVE_VRAC_ID, - "pmfmId", enumeration.PMFM_ID_SORTING_TYPE, enumeration.QUALITATIVE_ID_SORTING_TYPE_SPECIES); - for (Iterator<Batch> iterator = vracSpeciesBatch.getChildBatchs().iterator(); iterator.hasNext();) { - SortingBatch source = (SortingBatch) iterator.next(); - - // evict some special batches : Life (or Biota) and Inert - if (source.getReferenceTaxon() != null - && enumeration.REFERENCE_TAXON_ID_LIFE.equals(source.getReferenceTaxon().getId()) == false - && enumeration.REFERENCE_TAXON_ID_INERT.equals(source.getReferenceTaxon().getId()) == false) { - SpeciesBatch target = new SpeciesBatch(); - - entityToBean(source, target); - result.add(target); - } - } - - // Hors-Vrac / Species - SortingBatch horsVracSpeciesBatch = catchBatchDao.getSortingBatch(catchBatch.getChildBatchs(), - "pmfmId", enumeration.PMFM_ID_SORTED_UNSORTED, enumeration.QUALITATIVE_HORS_VRAC_ID, - "pmfmId", enumeration.PMFM_ID_SORTING_TYPE, enumeration.QUALITATIVE_ID_SORTING_TYPE_SPECIES); - for (Iterator<Batch> iterator = horsVracSpeciesBatch.getChildBatchs().iterator(); iterator.hasNext();) { - SortingBatch source = (SortingBatch) iterator.next(); - - SpeciesBatch target = new SpeciesBatch(); - - entityToBean(source, target); - result.add(target); - } - - return result; - } - - protected SpeciesBatch entityToBean(SortingBatch source, SpeciesBatch target) { - - target.setId(source.getId().toString()); - - // 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()); - } - - // 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 = measurementHelper.getInheritedSortingMeasurement(source, enumeration.PMFM_ID_SORTED_UNSORTED); - } - if (sm != null) { - SampleCategoryEnum sampleCategory = referentialService.getSampleCategoryByPmfmId(sm.getPmfm().getId()); - if (sampleCategory != null) { - setSampleCategoryQualitative(target, sm.getPmfm().getId(), sm.getNumericalValue(), sm.getAlphanumericalValue(), sm.getQualitativeValue().getId()); - } - } - - // Species - Integer referenceTaxonId = source.getInheritedReferenceTaxonId(); - if (referenceTaxonId == null && source.getReferenceTaxon() != null) { - referenceTaxonId = source.getReferenceTaxon().getId(); - } - if (referenceTaxonId != null) { - // TODO : add cache on getSpeciesByReferenceTaxonId - Species species = referentialService.getSpeciesByReferenceTaxonId(referenceTaxonId); - target.setSpecies(species); - } - - if (target.getSampleCategoryType() != null && source.getChildBatchs().size() > 0) { - List<SpeciesBatch> targetChilds = Lists.newArrayList(); - for (Iterator<Batch> iterator = source.getChildBatchs().iterator(); iterator.hasNext();) { - SortingBatch sourceChild = (SortingBatch) iterator.next(); - SpeciesBatch targetChild = new SpeciesBatch(); - entityToBean(sourceChild, targetChild); - if (targetChild.getSampleCategoryValue() != null) { - targetChilds.add(targetChild); - targetChild.setParentBatch(target); - } - } - target.setChildBatchs(targetChilds); - } - - return target; - } - - @Override - public SpeciesBatch createSpeciesBatch(SpeciesBatch bean, - String parentBatchId) { - Preconditions.checkNotNull(bean); - Preconditions.checkArgument(bean.getId() == null); - Preconditions.checkNotNull(bean.getSpecies()); - Preconditions.checkNotNull(bean.getSpecies().getId()); - Preconditions.checkNotNull(bean.getFishingOperation()); - Preconditions.checkNotNull(bean.getFishingOperation().getId()); - - getCurrentSession().setFlushMode(FlushMode.COMMIT); - - // Load full batch tree - Integer catchBatchId = catchBatchDao.getIdByFishingOperationId(Integer.valueOf(bean.getFishingOperation().getId())); - fr.ifremer.adagio.core.dao.data.batch.CatchBatch catchBatch = catchBatchDao.loadFullTree(catchBatchId); - - SortingBatch batch = SortingBatch.Factory.newInstance(); - beanToEntity(bean, batch, parentBatchId, catchBatch, true); - batch = sortingBatchDao.create(batch); - - bean.setId(String.valueOf(batch.getId())); - - getCurrentSession().flush(); - - return bean; - } - - @Override - public SpeciesBatch saveSpeciesBatch(SpeciesBatch bean) { - Preconditions.checkNotNull(bean); - Preconditions.checkNotNull(bean.getId()); - - // Load full batch tree - Integer catchBatchId = catchBatchDao.getIdBySortingBatchId(Integer.valueOf(bean.getId())); - fr.ifremer.adagio.core.dao.data.batch.CatchBatch catchBatch = catchBatchDao.loadFullTree(catchBatchId); - - getCurrentSession().setFlushMode(FlushMode.COMMIT); - - SortingBatch batch = catchBatchDao.getSortingBatchById(catchBatch, Integer.valueOf(bean.getId())); - String parentBatchId = null; - if (bean.getParentBatch() != null) { - parentBatchId = bean.getParentBatch().getId(); - } - beanToEntity(bean, batch, parentBatchId, catchBatch, true); - sortingBatchDao.update(batch); - getCurrentSession().flush(); - - return bean; - } - - @Override - public void deleteSpeciesBatch(String id) { - Preconditions.checkNotNull(id); - - getCurrentSession().setFlushMode(FlushMode.COMMIT); - - catchBatchDao.removeWithChildren(Integer.valueOf(id)); - - getCurrentSession().flush(); - } - - public void deleteSpeciesSubBatch(String id) { - deleteSpeciesBatch(id); - } - - public void changeSpeciesBatchSpecies(String batchId, Species species) { - Preconditions.checkNotNull(batchId); - Preconditions.checkNotNull(species); - Preconditions.checkNotNull(species.getReferenceTaxonId()); - - if (log.isDebugEnabled()) { - log.debug("Changing species for batch id=" + batchId); - } - - int rowUpdated = queryUpdate("updateBatchSpecies", - "batchId", IntegerType.INSTANCE, Integer.valueOf(batchId), - "referenceTaxonId", IntegerType.INSTANCE, species.getReferenceTaxonId()); - Preconditions.checkArgument(rowUpdated == 1, "Unable to update operation, to be linked with catch batch."); - } - - public List<SpeciesBatchFrequency> getAllSpeciesBatchFrequency( - String speciesBatchId) { - List<SpeciesBatchFrequency> results = Lists.newArrayList(); - - Integer catchBatchId = catchBatchDao.getIdBySortingBatchId(Integer.valueOf(speciesBatchId)); - fr.ifremer.adagio.core.dao.data.batch.CatchBatch catchBatch = catchBatchDao.loadFullTree(catchBatchId); - - if (catchBatchId == null) { - return results; - } - - SortingBatch sortingBatch = catchBatchDao.getSortingBatchById(catchBatch, Integer.valueOf(speciesBatchId)); - - for (Iterator<Batch> iterator = sortingBatch.getChildBatchs().iterator(); iterator.hasNext();) { - SortingBatch source = (SortingBatch) iterator.next(); - SpeciesBatchFrequency target = new SpeciesBatchFrequency(); - - entityToBean(source, target); - results.add(target); - } - return results; - } - - protected void entityToBean(SortingBatch source, SpeciesBatchFrequency target) { - - target.setId(source.getId().toString()); - - target.setNumber(source.getIndividualCount()); - target.setWeight(source.getWeight()); - target.setComment(source.getComments()); - - // Length step category - if (source.getSortingMeasurements().size() == 1) { - SortingMeasurement sm = source.getSortingMeasurements().iterator().next(); - if (sm.getPmfm() != null && sm.getPmfm().getId() != null) { - Caracteristic lengthStepCaracteristic = referentialService.getCaracteristic(sm.getPmfm().getId()); - target.setLengthStepCaracteristic(lengthStepCaracteristic); - } - - // Length - target.setLengthStep(sm.getNumericalValue()); - } - } - - public List<SpeciesBatchFrequency> saveSpeciesBatchFrequency( - String speciesBatchId, List<SpeciesBatchFrequency> frequencies) { - Preconditions.checkNotNull(speciesBatchId); - Preconditions.checkNotNull(frequencies); - - if (frequencies.size() == 0) { - return frequencies; - } - getCurrentSession().setFlushMode(FlushMode.COMMIT); - - Integer catchBatchId = catchBatchDao.getIdBySortingBatchId(Integer.valueOf(speciesBatchId)); - fr.ifremer.adagio.core.dao.data.batch.CatchBatch catchBatch = catchBatchDao.loadFullTree(catchBatchId); - - if (catchBatch == null) { - return frequencies; - } - - // Retrieve parent - SortingBatch parentBatch = catchBatchDao.getSortingBatchById(catchBatch, Integer.valueOf(speciesBatchId)); - - // Remember child ids, to remove unchanged item (see at bottom in this method) - List<Integer> notUpdatedChildIds = Lists.newArrayList(); - for (Iterator<Batch> iterator = parentBatch.getChildBatchs().iterator(); iterator.hasNext();) { - SortingBatch sortingBatch = (SortingBatch) iterator.next(); - notUpdatedChildIds.add(sortingBatch.getId()); - } - - short rankOrder = 0; - String pmfmId = null; - List<SortingBatch> batchsToUpdate = Lists.newArrayList(); - for (SpeciesBatchFrequency source : frequencies) { - rankOrder++; - - // Check that all frequencies have the same length PMFM - if (pmfmId == null) { - source.getLengthStepCaracteristic().getId(); - } else if (!pmfmId.equals(source.getLengthStepCaracteristic().getId())) { - throw new DataIntegrityViolationException("Batch frequencies under one Speciesbatch must have all the same lengthStepCaracteristic"); - } - - // Not existing batch - SortingBatch target = null; - if (source.getId() == null) { - target = SortingBatch.Factory.newInstance(); - - // Fill the sorting batch from the source - beanToEntity(source, target, parentBatch, rankOrder, true); - - // Create the targeted batch, then update the source id - sortingBatchDao.create(target); - source.setId(target.getId().toString()); - } - - // Existing batch - else { - target = sortingBatchDao.load(Integer.valueOf(source.getId())); - - // Fill the sorting batch from the source - beanToEntity(source, target, parentBatch, rankOrder, true); - - // 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 (batchsToUpdate.size() > 0) { - sortingBatchDao.update(batchsToUpdate); - } - - if (notUpdatedChildIds.size() > 0) { - for (Iterator<Integer> iterator = notUpdatedChildIds.iterator(); iterator.hasNext();) { - Integer batchId = iterator.next(); - catchBatchDao.removeWithChildren(batchId); - } - } - - getCurrentSession().flush(); - - return frequencies; - } - - // ------------------------------------------------------------------------// - // -- Internal methods --// - // ------------------------------------------------------------------------// - - protected void validate(Collection<Batch> batchs, List<CatchBatchValidationError> errors, int treeLevel) { - - List<Integer> expectedPmfms = null; - boolean expectedReferenceTaxon = false; - // First tree level : should have pmfm SORTED (Vrac, Hors Vrac, Non trié) - if (treeLevel == 1) { - expectedPmfms = Lists.newArrayList(enumeration.PMFM_ID_SORTED_UNSORTED); - } - // Second tree level : should have pmfm SORTING_TYPE (Species, Benthos...) - else if (treeLevel == 2) { - expectedPmfms = Lists.newArrayList(enumeration.PMFM_ID_SORTING_TYPE); - } - else if (treeLevel == 3) { - expectedReferenceTaxon = true; - } - else if (treeLevel > 3) { - expectedPmfms = referentialService.getSampleCategoryIds(); - expectedPmfms.remove(enumeration.PMFM_ID_SORTED_UNSORTED); - } - - // First level : should be only PMFM=SORTED - for (Iterator<Batch> iterator = batchs.iterator(); iterator.hasNext();) { - SortingBatch batch = (SortingBatch) iterator.next(); - boolean batchHasError = false; - - // For all sorting measurements - if (batch.getSortingMeasurements() != null && batch.getSortingMeasurements().size() > 0) { - for (Iterator<SortingMeasurement> iterator2 = batch.getSortingMeasurements().iterator(); iterator2.hasNext();) { - SortingMeasurement sm = iterator2.next(); - Integer pmfmId = sm.getPmfm().getId(); - if (expectedReferenceTaxon) { - CatchBatchValidationError error = new CatchBatchValidationError( - "tutti.persistence.batch.validation.onlyTaxonButPmfmFound", - _("tutti.persistence.batch.validation.onlyTaxonButPmfmFound", batch.getId(), pmfmId), - CatchBatchValidationError.GRAVITY_ERROR); - errors.add(error); - } - else if (expectedPmfms.contains(pmfmId) == false) { - if (sm.getNumericalValue() == null || batch.getChildBatchs().size() > 0) { - batchHasError = true; - CatchBatchValidationError error = new CatchBatchValidationError( - "tutti.persistence.batch.validation.pmfmNotFound", - _("tutti.persistence.batch.validation.pmfmNotFound", - batch.getId(), Arrays.toString(expectedPmfms.toArray()), pmfmId), - CatchBatchValidationError.GRAVITY_ERROR); - errors.add(error); - } - } - } - } - else if (expectedPmfms != null && expectedPmfms.size() > 0){ - batchHasError = true; - CatchBatchValidationError error = new CatchBatchValidationError( - "tutti.persistence.batch.validation.pmfmNotFound", - _("tutti.persistence.batch.validation.pmfmNotFound", - batch.getId(), Arrays.toString(expectedPmfms.toArray()), "null"), - CatchBatchValidationError.GRAVITY_ERROR); - errors.add(error); - } - - if (!batchHasError && expectedReferenceTaxon == true && batch.getReferenceTaxon() == null) { - CatchBatchValidationError error = new CatchBatchValidationError( - "tutti.persistence.batch.validation.referenceTaxonNotFound", - _("tutti.persistence.batch.validation.referenceTaxonNotFound", batch.getId()), - CatchBatchValidationError.GRAVITY_ERROR); - errors.add(error); - } - - if (!batchHasError) { - validate(batch.getChildBatchs(), errors, treeLevel+1); - } - } - } - - protected void beanToEntity(SpeciesBatch source, - SortingBatch target, - String parentBatchId, - fr.ifremer.adagio.core.dao.data.batch.CatchBatch catchBatch, - boolean copyIfNull) { - Preconditions.checkNotNull(source.getFishingOperation()); - Preconditions.checkNotNull(source.getFishingOperation().getId()); - - // Retrieve recorder department - Integer recorderDepartmentId = getRecorderDepartmentId(); - - // Create lists to store all updates, then remove not updated items - Set<QuantificationMeasurement> notChangedQuantificationMeasurements = new HashSet<QuantificationMeasurement>(); - if (target.getQuantificationMeasurements() != null) { - notChangedQuantificationMeasurements.addAll(target.getQuantificationMeasurements()); - } - Set<SortingMeasurement> notChangedSortingMeasurements = new HashSet<SortingMeasurement>(); - if (target.getSortingMeasurements() != null) { - notChangedSortingMeasurements.addAll(target.getSortingMeasurements()); - } - - // If parent and root need to be set - if (target.getId() == null - || target.getRootBatch() == null - || (target.getParentBatch() != null && !target.getParentBatch().getId().toString().equals(parentBatchId))) { - setBatchParents(source, target, parentBatchId, catchBatch); - } - - // RankOrder (initialize once, at creation) - if (target.getRankOrder() == null) { - // Start rank order at 3, because of special batches 'Biota' and 'Inert' - short rankOrder = (short) 3; - if (source.getParentBatch() != null && source.getParentBatch().getChildBatchs() != null) { - // TODO BL : vérifier cela (est-ce que l'item est ajouté à son père AVANT d'être passé au service ?) - rankOrder += (short) source.getParentBatch().getChildBatchs().size(); - } - target.setRankOrder(rankOrder); - } - - // Force subgroup count to '1', as Allegro - target.setSubgroupCount(1f); - - // Weight or SampleCategoryWeight - if (copyIfNull && source.getWeight() == null && source.getSampleCategoryWeight() == null) { - // Nothing to do : will be removed later, using notChangedSortingMeasurements - } else if (source.getSampleCategoryWeight() != null && source.getWeight() == null) { - QuantificationMeasurement quantificationMeasurement = measurementHelper.setQuantificationMeasurement(target, - enumeration.PMFM_ID_WEIGHT_MEASURED, recorderDepartmentId, source.getSampleCategoryWeight(), true); - notChangedQuantificationMeasurements.remove(quantificationMeasurement); - } else if (source.getWeight() != null && source.getSampleCategoryWeight() == null) { - QuantificationMeasurement quantificationMeasurement = measurementHelper.setQuantificationMeasurement(target, - enumeration.PMFM_ID_WEIGHT_MEASURED, recorderDepartmentId, source.getWeight(), true); - notChangedQuantificationMeasurements.remove(quantificationMeasurement); - } - - // Sampling Ratio - if (copyIfNull && (source.getSampleCategoryWeight() == null || source.getWeight() == null)) { - target.setSamplingRatio(null); - target.setSamplingRatioText(null); - } else if (source.getSampleCategoryWeight() != null && source.getWeight() != null) { - String samplingRatioText = source.getWeight() + "/" + source.getSampleCategoryWeight(); - samplingRatioText = samplingRatioText.replaceAll(",", "."); - target.setSamplingRatioText(samplingRatioText); - target.setSamplingRatio(source.getWeight() / source.getSampleCategoryWeight()); - QuantificationMeasurement quantificationMeasurement = measurementHelper.setQuantificationMeasurement(target, - enumeration.PMFM_ID_WEIGHT_MEASURED, recorderDepartmentId, source.getWeight(), true); - notChangedQuantificationMeasurements.remove(quantificationMeasurement); - } - - // Sorting measurement - if (copyIfNull && (source.getSampleCategoryType() == null || source.getSampleCategoryValue() == null)) { - // Nothing to do : will be removed later, using notChangedSortingMeasurements - } else if (source.getSampleCategoryType() != null && source.getSampleCategoryValue() != null) { - Integer pmfmId = measurementHelper.sampleCategory2PmfmId(source.getSampleCategoryType()); - // Do not store sorting measurement if pmfm = SORTED (already store in an ancestor batch) - if (!pmfmId.equals(enumeration.PMFM_ID_SORTED_UNSORTED)) { - SortingMeasurement sortingMeasurement = measurementHelper.setSortingMeasurement(target, recorderDepartmentId, - source.getSampleCategoryType(), source.getSampleCategoryValue()); - notChangedSortingMeasurements.remove(sortingMeasurement); - } - } - - // Individual count - if (copyIfNull && source.getNumber() == null) { - target.setIndividualCount(null); - } else if (source.getNumber() != null) { - target.setIndividualCount(source.getNumber()); - } - - // Species - if (copyIfNull && (source.getSpecies() == null || parentBatchId != null)) { - target.setReferenceTaxon(null); - } else if (source.getSpecies() != null && parentBatchId == null) { - ReferenceTaxon referenceTaxon = load(ReferenceTaxonImpl.class, source.getSpecies().getReferenceTaxonId()); - target.setReferenceTaxon(referenceTaxon); - } - - // QualityFlag - if (source.isSpeciesToConfirm()) { - target.setQualityFlag(load(QualityFlagImpl.class, enumeration.QUALITY_FLAG_CODE_DOUBTFUL)); - } else { - target.setQualityFlag(load(QualityFlagImpl.class, enumeration.QUALITY_FLAG_CODE_NOT_QUALIFIED)); - } - - // Comments - if (copyIfNull && source.getComment() == null) { - target.setComments(null); - } else if (source.getComment() != null) { - target.setComments(source.getComment()); - } - - // Exhaustive inventory (always true under a species batch) - target.setExhaustiveInventory(true); - - // Removed not changed measurements (in sorting and quantification measurement lists) - if (target.getQuantificationMeasurements() != null && notChangedQuantificationMeasurements.size() > 0) { - for (QuantificationMeasurement qm : notChangedQuantificationMeasurements) { - target.getQuantificationMeasurements().remove(qm); - } - } - if (target.getSortingMeasurements() != null && notChangedSortingMeasurements.size() > 0) { - for (SortingMeasurement sm : notChangedSortingMeasurements) { - target.getSortingMeasurements().remove(sm); - } - } - } - - public void setBatchParents(SpeciesBatch source, SortingBatch target, String parentBatchIdStr, - fr.ifremer.adagio.core.dao.data.batch.CatchBatch catchBatch) { - - Preconditions.checkNotNull(target); - Preconditions.checkNotNull(source.getFishingOperation()); - Preconditions.checkNotNull(source.getFishingOperation().getId()); - - // Load existing parent and root - SortingBatch parentBatch = null; - if (parentBatchIdStr != null) { - parentBatch = catchBatchDao.getSortingBatchById(catchBatch, Integer.valueOf(parentBatchIdStr)); - } - - // Or retrieve parent batch, from pmfm id - else { - // Retrieve category type - Integer pmfmId = measurementHelper.sampleCategory2PmfmId(source.getSampleCategoryType()); - if (pmfmId == null || !pmfmId.equals(enumeration.PMFM_ID_SORTED_UNSORTED)) { - throw new DataIntegrityViolationException(MessageFormat.format( - "A species batch with no parent should have a sampleCategoryType {0} (PMFM.ID={1})", - SampleCategoryEnum.sortedUnsorted.name(), - enumeration.PMFM_ID_SORTED_UNSORTED)); - } - Integer qualitativeValueId = convertSampleCategoryValueIntoQualitativeId(source.getSampleCategoryValue()); - - parentBatch = catchBatchDao.getSortingBatch(catchBatch.getChildBatchs(), - "pmfmId", pmfmId, qualitativeValueId, - "pmfmId", enumeration.PMFM_ID_SORTING_TYPE, enumeration.QUALITATIVE_ID_SORTING_TYPE_SPECIES); - - // Parent Batch - target.setParentBatch(parentBatch); - } - - - if (parentBatch == null) { - throw new DataIntegrityViolationException( - "Could not retrieve parent batch, for a given speciesBatch : invalid batch tree structure. Please make sure CatchBatch has been saved before to create a SpeciesBatch."); - } - - // Parent Batch - target.setParentBatch(parentBatch); - target.setRootBatch(catchBatch); - } - - Integer convertSampleCategoryValueIntoQualitativeId(Serializable value) { - if (value == null) - return null; - Integer qualitativeValueId = null; - if (value instanceof CaracteristicQualitativeValue) { - CaracteristicQualitativeValue cqValue = (CaracteristicQualitativeValue) value; - qualitativeValueId = Integer.valueOf(cqValue.getId()); - } else if (value instanceof String) { - qualitativeValueId = Integer.valueOf((String) value); - } - return qualitativeValueId; - } - - protected void setSampleCategoryQualitative(SpeciesBatch target, Integer pmfmId, Float numericalvalue, String alphanumericalValue, - Integer qualitativeValueId) { - // skip if null or corresponding to the SORTING_TYPE PMFM (Expèce, Benthos, Plancton, etc.) - if (pmfmId == null || pmfmId.equals(enumeration.PMFM_ID_SORTING_TYPE)) { - return; - } - - SampleCategoryEnum sampleCategory = referentialService.getSampleCategoryByPmfmId(pmfmId); - Preconditions.checkNotNull(sampleCategory, "Unable to find corresponding SampleCategoryEnum for PMFM.ID : " + pmfmId); - - target.setSampleCategoryType(sampleCategory); - if (numericalvalue != null) { - target.setSampleCategoryValue(numericalvalue); - return; - } - if (alphanumericalValue != null) { - target.setSampleCategoryValue(alphanumericalValue); - return; - } - - Caracteristic caracteristic = referentialService.getCaracteristic(pmfmId); - if (caracteristic == null || caracteristic.getCaracteristicType() != CaracteristicType.QUALITATIVE) { - return; - } - CaracteristicQualitativeValue value = null; - for (CaracteristicQualitativeValue qv : caracteristic.getQualitativeValue()) { - if (qualitativeValueId == Integer.parseInt(qv.getId())) { - value = qv; - break; - } - } - target.setSampleCategoryValue(value); - } - - protected void beanToEntity(SpeciesBatchFrequency source, SortingBatch target, SortingBatch parentBatch, short rankOrder, boolean copyIfNull) { - Preconditions.checkNotNull(source.getBatch()); - Preconditions.checkNotNull(source.getBatch().getId()); - - // Retrieve recorder department - Integer recorderDepartmentId = getRecorderDepartmentId(); - - // Create lists to store all updates, then remove not updated items - Set<QuantificationMeasurement> notChangedQuantificationMeasurements = new HashSet<QuantificationMeasurement>(); - if (target.getQuantificationMeasurements() != null) { - notChangedQuantificationMeasurements.addAll(target.getQuantificationMeasurements()); - } - Set<SortingMeasurement> notChangedSortingMeasurements = new HashSet<SortingMeasurement>(); - if (target.getSortingMeasurements() != null) { - notChangedSortingMeasurements.addAll(target.getSortingMeasurements()); - } - - // 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); - - // Weight or SampleCategoryWeight - if (copyIfNull && source.getWeight() == null) { - // Nothing to do : will be removed later, using notChangedSortingMeasurements - } else if (source.getWeight() != null) { - QuantificationMeasurement quantificationMeasurement = measurementHelper.setQuantificationMeasurement(target, - enumeration.PMFM_ID_WEIGHT_MEASURED, recorderDepartmentId, source.getWeight(), true); - notChangedQuantificationMeasurements.remove(quantificationMeasurement); - } - - // Sorting measurement - if (copyIfNull && (source.getLengthStepCaracteristic() == null || source.getLengthStep() == null)) { - // Nothing to do : will be removed later, using notChangedSortingMeasurements - } else if (source.getLengthStepCaracteristic() != null && source.getLengthStep() != null) { - Integer pmfmId = Integer.valueOf(source.getLengthStepCaracteristic().getId()); - SortingMeasurement sortingMeasurement = measurementHelper.setSortingMeasurement(target, recorderDepartmentId, pmfmId, - source.getLengthStep()); - notChangedSortingMeasurements.remove(sortingMeasurement); - } - - // Individual count - if (copyIfNull && source.getNumber() == null) { - target.setIndividualCount(null); - } else if (source.getNumber() != null) { - target.setIndividualCount(source.getNumber()); - } - - // Species - target.setReferenceTaxon(null); - - // QualityFlag - target.setQualityFlag(parentBatch.getQualityFlag()); - - // Comments - if (copyIfNull && source.getComment() == null) { - target.setComments(null); - } else if (source.getComment() != null) { - target.setComments(source.getComment()); - } - - // Exhaustive inventory (always true under a species batch) - target.setExhaustiveInventory(true); - - // Removed not changed measurements (in sorting and quantification measurement lists) - if (target.getQuantificationMeasurements() != null && notChangedQuantificationMeasurements.size() > 0) { - for (QuantificationMeasurement qm : notChangedQuantificationMeasurements) { - target.getQuantificationMeasurements().remove(qm); - } - } - if (target.getSortingMeasurements() != null && notChangedSortingMeasurements.size() > 0) { - for (SortingMeasurement sm : notChangedSortingMeasurements) { - target.getSortingMeasurements().remove(sm); - } - } - } - - protected UIWeight convertDatabase2UI(Float weight, Float samplingRatio, String samplingRatioText) { - UIWeight result = new UIWeight(); - - // Weight & sampleCategory Weight - if (samplingRatio == null) { - result.sampleCategoryWeight = weight; - } else if (weight != null) { - String startStr = weight.toString().replace(',', '.') + "/"; - if (samplingRatioText != null && samplingRatioText.startsWith(startStr)) { - String weightStr = samplingRatioText.substring(startStr.length()); - if (weightStr != null && !weightStr.isEmpty()) { - result.sampleCategoryWeight = Float.parseFloat(weightStr); - result.weight = weight; - } - } else { - result.sampleCategoryWeight = weight; - } - } - return result; - } - - static class UIWeight { - Float sampleCategoryWeight = null; - - Float weight = null; - } - - protected void beanToEntity(CatchBatch source, - fr.ifremer.adagio.core.dao.data.batch.CatchBatch target, - boolean copyIfNull) { - Preconditions.checkNotNull(source.getFishingOperation()); - Preconditions.checkNotNull(source.getFishingOperation().getId()); - - // Retrieve recorder department - Integer recorderDepartmentId = getRecorderDepartmentId(); - - // First initialization (when created) - Integer fishingOperationId = Integer.valueOf(source.getFishingOperation().getId()); - target.setFishingOperation(load(FishingOperationImpl.class, fishingOperationId)); - - target.setQualityFlag(load(QualityFlagImpl.class, enumeration.QUALITY_FLAG_CODE_NOT_QUALIFIED)); - target.setRankOrder((short) 1); - target.setSynchronizationStatus(SynchronizationStatus.DIRTY.getValue()); - - // Create lists to store all updates, then remove not updated items - Set<QuantificationMeasurement> notChangedQuantificationMeasurements = new HashSet<QuantificationMeasurement>(); - if (target.getQuantificationMeasurements() != null) { - notChangedQuantificationMeasurements.addAll(target.getQuantificationMeasurements()); - } - - // Total Weight - if (copyIfNull && source.getCatchTotalWeight() == null) { - // Nothing to do : will be removed later, using notChangedQuantificationMeasurements - } else if (source.getCatchTotalWeight() != null) { - QuantificationMeasurement quantificationMeasurement = measurementHelper.setQuantificationMeasurement(target, - enumeration.PMFM_ID_WEIGHT_MEASURED, recorderDepartmentId, source.getCatchTotalWeight(), true); - notChangedQuantificationMeasurements.remove(quantificationMeasurement); - } - - // Removed not changed measurements (in sorting and quantification measurement lists) - if (target.getQuantificationMeasurements() != null && notChangedQuantificationMeasurements.size() > 0) { - for (QuantificationMeasurement qm : notChangedQuantificationMeasurements) { - target.getQuantificationMeasurements().remove(qm); - } - } - Map<Integer, SortingBatch> catchBatchChilds = getChildsMap(target, enumeration.PMFM_ID_SORTED_UNSORTED); - - // ----------------------------------------------------------------------------- - // Sorted Vrac - // ----------------------------------------------------------------------------- - { - SortingBatch batch = catchBatchChilds.get(enumeration.QUALITATIVE_VRAC_ID); - if (batch == null) { - batch = SortingBatch.Factory.newInstance(); - target.getChildBatchs().add(batch); - } - beanToEntitySortingBatch(target, target, batch, recorderDepartmentId, - enumeration.PMFM_ID_SORTED_UNSORTED, enumeration.QUALITATIVE_VRAC_ID, - source.getCatchTotalSortedCarousselWeight(), source.getCatchTotalSortedTremisWeight(), - copyIfNull); - batch.setRankOrder((short) 1); - - // Manage childs : - { - Map<Integer, SortingBatch> batchChilds = getChildsMap(batch, enumeration.PMFM_ID_SORTING_TYPE); - - // ----------------------------------------------------------------------------- - // Sorted Vrac / Species - // ----------------------------------------------------------------------------- - SortingBatch speciesBatch = batchChilds.get(enumeration.QUALITATIVE_ID_SORTING_TYPE_SPECIES); - if (speciesBatch == null) { - speciesBatch = SortingBatch.Factory.newInstance(); - if (batch.getChildBatchs() == null) { - batch.setChildBatchs(Lists.newArrayList((Batch) speciesBatch)); - } else { - batch.getChildBatchs().add(speciesBatch); - } - } - beanToEntitySortingBatch(target, batch, speciesBatch, recorderDepartmentId, - enumeration.PMFM_ID_SORTING_TYPE, enumeration.QUALITATIVE_ID_SORTING_TYPE_SPECIES, - source.getSpeciesTotalSampleSortedWeight(), source.getSpeciesTotalSortedWeight(), - copyIfNull); - speciesBatch.setRankOrder((short) 1); - - { - - // ----------------------------------------------------------------------------- - // Sorted Vrac / Species / Alive not itemized - // ----------------------------------------------------------------------------- - SortingBatch aliveNotItemizedBatch = catchBatchDao.getSortingBatch(speciesBatch.getChildBatchs(), "referenceTaxonId", enumeration.REFERENCE_TAXON_ID_LIFE); - if (aliveNotItemizedBatch == null) { - aliveNotItemizedBatch = SortingBatch.Factory.newInstance(); - if (speciesBatch.getChildBatchs() == null) { - speciesBatch.setChildBatchs(Lists.newArrayList((Batch) aliveNotItemizedBatch)); - } else { - speciesBatch.getChildBatchs().add(aliveNotItemizedBatch); - } - } - beanToEntityReferenceTaxonBatch(target, speciesBatch, aliveNotItemizedBatch, recorderDepartmentId, - enumeration.REFERENCE_TAXON_ID_LIFE, - source.getSpeciesTotalLivingNotItemizedWeight(), null, - copyIfNull); - aliveNotItemizedBatch.setRankOrder((short) 1); - - // ----------------------------------------------------------------------------- - // Sorted Vrac / Species / Inert (not alive) - // ----------------------------------------------------------------------------- - SortingBatch inertBatch = catchBatchDao.getSortingBatch(speciesBatch.getChildBatchs(), "referenceTaxonId", enumeration.REFERENCE_TAXON_ID_INERT); - if (inertBatch == null) { - inertBatch = SortingBatch.Factory.newInstance(); - speciesBatch.getChildBatchs().add(inertBatch); - } - beanToEntityReferenceTaxonBatch(target, speciesBatch, inertBatch, recorderDepartmentId, - enumeration.REFERENCE_TAXON_ID_INERT, - source.getSpeciesTotalInertWeight(), null, - copyIfNull); - inertBatch.setRankOrder((short) 2); - } - } - // TODO BL : benthos, plancton, macro déchet... - } - - // ----------------------------------------------------------------------------- - // Sorted Hors Vrac - // ----------------------------------------------------------------------------- - { - SortingBatch batch = catchBatchChilds.get(enumeration.QUALITATIVE_HORS_VRAC_ID); - if (batch == null) { - batch = SortingBatch.Factory.newInstance(); - target.getChildBatchs().add(batch); - } - beanToEntitySortingBatch(target, target, batch, recorderDepartmentId, - enumeration.PMFM_ID_SORTED_UNSORTED, enumeration.QUALITATIVE_HORS_VRAC_ID, - source.getCatchTotalUnsortedWeight(), null, - copyIfNull); - batch.setRankOrder((short) 2); - - // Manage childs : - { - Map<Integer, SortingBatch> batchChilds = getChildsMap(batch, enumeration.PMFM_ID_SORTING_TYPE); - - // Species : - SortingBatch speciesBatch = batchChilds.get(enumeration.QUALITATIVE_ID_SORTING_TYPE_SPECIES); - if (speciesBatch == null) { - speciesBatch = SortingBatch.Factory.newInstance(); - if (batch.getChildBatchs() == null) { - batch.setChildBatchs(Lists.newArrayList((Batch) speciesBatch)); - } else { - batch.getChildBatchs().add(speciesBatch); - } - } - beanToEntitySortingBatch(target, batch, speciesBatch, recorderDepartmentId, - enumeration.PMFM_ID_SORTING_TYPE, enumeration.QUALITATIVE_ID_SORTING_TYPE_SPECIES, - source.getSpeciesTotalUnsortedWeight(), null, - copyIfNull); - speciesBatch.setRankOrder((short) 1); - } - - // TODO BL : benthos, plancton, macro déchet... - } - - // ----------------------------------------------------------------------------- - // Unsorted (=rejected) - // ----------------------------------------------------------------------------- - // Unsorted : - SortingBatch unsortedBatch = catchBatchChilds.get(enumeration.QUALITATIVE_UNSORTED_ID); - if (unsortedBatch == null) { - unsortedBatch = SortingBatch.Factory.newInstance(); - target.getChildBatchs().add(unsortedBatch); - } - beanToEntitySortingBatch(target, target, unsortedBatch, recorderDepartmentId, - enumeration.PMFM_ID_SORTED_UNSORTED, enumeration.QUALITATIVE_UNSORTED_ID, - source.getCatchTotalRejectedWeight(), null, - copyIfNull); - unsortedBatch.setRankOrder((short) 3); - - } - - protected Map<Integer, SortingBatch> getChildsMap(Batch parentBatch, Integer pmfmId) { - Map<Integer, SortingBatch> batchByQualitativeValueId = new HashMap<Integer, SortingBatch>(); - if (parentBatch.getChildBatchs() == null) { - return batchByQualitativeValueId; - } - for (Batch childBatch : parentBatch.getChildBatchs()) { - SortingBatch childSortingBatch = (SortingBatch) childBatch; - SortingMeasurement sm = measurementHelper.getSortingMeasurement((SortingBatch) childBatch, pmfmId, null, false); - if (sm != null && sm.getQualitativeValue() != null && sm.getQualitativeValue().getId() != null) { - batchByQualitativeValueId.put(sm.getQualitativeValue().getId(), childSortingBatch); - } - } - return batchByQualitativeValueId; - } - - /** - * Retrieve child batchs if species is "Life" or "No life" - * (need to retrieve: speciesTotalLivingNotItemizedWeight and speciesTotalInertWeight) - * - * @param parentBatch - * @return - */ - protected Map<Integer, SortingBatch> getChildAliveNotSortedOrInert(Batch parentBatch) { - Map<Integer, SortingBatch> batchByQualitativeValueId = new HashMap<Integer, SortingBatch>(); - if (parentBatch.getChildBatchs() == null) { - return batchByQualitativeValueId; - } - for (Batch childBatch : parentBatch.getChildBatchs()) { - SortingBatch childSortingBatch = (SortingBatch) childBatch; - if (childSortingBatch.getReferenceTaxon() != null && childSortingBatch.getId().intValue() < 0) { - if (enumeration.REFERENCE_TAXON_ID_LIFE.equals(childSortingBatch.getId())) { - batchByQualitativeValueId.put(enumeration.REFERENCE_TAXON_ID_LIFE, childSortingBatch); - } else if (enumeration.REFERENCE_TAXON_ID_LIFE.equals(childSortingBatch.getId())) { - batchByQualitativeValueId.put(enumeration.REFERENCE_TAXON_ID_LIFE, childSortingBatch); - } - } - } - return batchByQualitativeValueId; - } - - protected void beanToEntitySortingBatch( - fr.ifremer.adagio.core.dao.data.batch.CatchBatch rootBatch, - fr.ifremer.adagio.core.dao.data.batch.Batch parentBatch, - fr.ifremer.adagio.core.dao.data.batch.SortingBatch target, - Integer recorderDepartmentId, - Integer sortingPmfmId, - Integer sortingQualitativeValueId, - Float weight, - Float sampleWeight, - boolean copyIfNull) { - - // Create lists to store all updates, then remove not updated items - Set<QuantificationMeasurement> notChangedQuantificationMeasurements = new HashSet<QuantificationMeasurement>(); - if (target.getQuantificationMeasurements() != null) { - notChangedQuantificationMeasurements.addAll(target.getQuantificationMeasurements()); - } - Set<SortingMeasurement> notChangedSortingMeasurements = new HashSet<SortingMeasurement>(); - if (target.getSortingMeasurements() != null) { - notChangedSortingMeasurements.addAll(target.getSortingMeasurements()); - } - - // Some mandatory properties : - target.setQualityFlag(load(QualityFlagImpl.class, enumeration.QUALITY_FLAG_CODE_NOT_QUALIFIED)); - target.setRootBatch(rootBatch); - target.setParentBatch(parentBatch); - target.setExhaustiveInventory(true); - - // No taxon or taxon group - target.setReferenceTaxon(null); - target.setTaxonGroup(null); - - // Sorting measurement - if (copyIfNull && (sortingPmfmId == null || sortingQualitativeValueId == null)) { - // Nothing to do : will be removed later, using notChangedSortingMeasurements - } else if (sortingPmfmId != null && sortingQualitativeValueId != null) { - SortingMeasurement sm = measurementHelper.setSortingMeasurement(target, recorderDepartmentId, sortingPmfmId, sortingQualitativeValueId); - notChangedSortingMeasurements.remove(sm); - } - - // Sampling Ratio - if (copyIfNull && (sampleWeight == null || weight == null)) { - target.setSamplingRatio(null); - target.setSamplingRatioText(null); - } else if (sampleWeight != null && weight != null) { - String samplingRatioText = weight + "/" + sampleWeight; - samplingRatioText = samplingRatioText.replaceAll(",", "."); - target.setSamplingRatioText(samplingRatioText); - target.setSamplingRatio(weight / sampleWeight); - } - - // Weight - if (copyIfNull && (sampleWeight == null && weight == null)) { - // Nothing to do : will be removed later, using notChangedQuantificationMeasurements - } else if (sampleWeight != null || weight != null) { - Float batchReferenceWeight = weight; - if (batchReferenceWeight == null) { - batchReferenceWeight = sampleWeight; - } - QuantificationMeasurement quantificationMeasurement = measurementHelper.setQuantificationMeasurement(target, - enumeration.PMFM_ID_WEIGHT_MEASURED, recorderDepartmentId, batchReferenceWeight, true); - notChangedQuantificationMeasurements.remove(quantificationMeasurement); - } - - // Removed not changed measurements (in sorting and quantification measurement lists) - if (target.getQuantificationMeasurements() != null && notChangedQuantificationMeasurements.size() > 0) { - for (QuantificationMeasurement qm : notChangedQuantificationMeasurements) { - target.getQuantificationMeasurements().remove(qm); - } - } - if (target.getSortingMeasurements() != null && notChangedSortingMeasurements.size() > 0) { - for (SortingMeasurement sm : notChangedSortingMeasurements) { - target.getSortingMeasurements().remove(sm); - } - } - } - - protected void beanToEntityReferenceTaxonBatch( - fr.ifremer.adagio.core.dao.data.batch.CatchBatch rootBatch, - fr.ifremer.adagio.core.dao.data.batch.Batch parentBatch, - fr.ifremer.adagio.core.dao.data.batch.SortingBatch target, - Integer recorderDepartmentId, - Integer referenceTaxonId, - Float weight, - Float sampleWeight, - boolean copyIfNull) { - - // Create lists to store all updates, then remove not updated items - Set<QuantificationMeasurement> notChangedQuantificationMeasurements = new HashSet<QuantificationMeasurement>(); - if (target.getQuantificationMeasurements() != null) { - notChangedQuantificationMeasurements.addAll(target.getQuantificationMeasurements()); - } - Set<SortingMeasurement> notChangedSortingMeasurements = new HashSet<SortingMeasurement>(); - if (target.getSortingMeasurements() != null) { - notChangedSortingMeasurements.addAll(target.getSortingMeasurements()); - } - - // Some mandatory properties : - target.setQualityFlag(load(QualityFlagImpl.class, enumeration.QUALITY_FLAG_CODE_NOT_QUALIFIED)); - target.setRootBatch(rootBatch); - target.setParentBatch(parentBatch); - target.setExhaustiveInventory(true); - - // Reference taxon - target.setReferenceTaxon(load(ReferenceTaxonImpl.class, referenceTaxonId)); - target.setTaxonGroup(null); - - // Sampling Ratio - if (copyIfNull && (sampleWeight == null || weight == null)) { - target.setSamplingRatio(null); - target.setSamplingRatioText(null); - } else if (sampleWeight != null && weight != null) { - String samplingRatioText = weight + "/" + sampleWeight; - samplingRatioText = samplingRatioText.replaceAll(",", "."); - target.setSamplingRatioText(samplingRatioText); - target.setSamplingRatio(weight / sampleWeight); - } - - // Weight - if (copyIfNull && (sampleWeight == null && weight == null)) { - // Nothing to do : will be removed later, using notChangedQuantificationMeasurements - } else if (sampleWeight != null || weight != null) { - Float batchReferenceWeight = weight; - if (batchReferenceWeight == null) { - batchReferenceWeight = sampleWeight; - } - QuantificationMeasurement quantificationMeasurement = measurementHelper.setQuantificationMeasurement(target, - enumeration.PMFM_ID_WEIGHT_MEASURED, recorderDepartmentId, batchReferenceWeight, true); - notChangedQuantificationMeasurements.remove(quantificationMeasurement); - } - - // Removed not changed measurements (in sorting and quantification measurement lists) - if (target.getQuantificationMeasurements() != null && notChangedQuantificationMeasurements.size() > 0) { - for (QuantificationMeasurement qm : notChangedQuantificationMeasurements) { - target.getQuantificationMeasurements().remove(qm); - } - } - if (target.getSortingMeasurements() != null && notChangedSortingMeasurements.size() > 0) { - for (SortingMeasurement sm : notChangedSortingMeasurements) { - target.getSortingMeasurements().remove(sm); - } - } - } - - protected Integer getRecorderDepartmentId() { - // TODO BL : voir si on peut récupérer le departement (du 1er saisisseur ?) - return enumeration.DEPARTMENT_ID_UNKNOWN_RECORDER_DEPARTMENT; - } -} +package fr.ifremer.tutti.persistence.service; + +/* + * #%L + * Tutti :: Persistence + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2012 - 2013 Ifremer + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/gpl-3.0.html>. + * #L% + */ + + +import static org.nuiton.i18n.I18n._; + +import java.io.Serializable; +import java.text.MessageFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import javax.annotation.Resource; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.hibernate.FlushMode; +import org.hibernate.type.IntegerType; +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.dao.DataIntegrityViolationException; +import org.springframework.dao.DataRetrievalFailureException; +import org.springframework.stereotype.Service; + +import com.google.common.base.Preconditions; +import com.google.common.collect.Lists; + +import fr.ifremer.adagio.core.dao.data.batch.Batch; +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.batch.SortingBatchDao; +import fr.ifremer.adagio.core.dao.data.batch.denormalized.DenormalizedBatchDao; +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.batch.validator.CatchBatchValidator; +import fr.ifremer.adagio.core.dao.data.measure.QuantificationMeasurement; +import fr.ifremer.adagio.core.dao.data.measure.SortingMeasurement; +import fr.ifremer.adagio.core.dao.data.operation.FishingOperationImpl; +import fr.ifremer.adagio.core.dao.referential.QualityFlagImpl; +import fr.ifremer.adagio.core.dao.referential.taxon.ReferenceTaxon; +import fr.ifremer.adagio.core.dao.referential.taxon.ReferenceTaxonImpl; +import fr.ifremer.adagio.core.dao.technical.synchronization.SynchronizationStatus; +import fr.ifremer.tutti.persistence.entities.data.CatchBatch; +import fr.ifremer.tutti.persistence.entities.data.SampleCategoryEnum; +import fr.ifremer.tutti.persistence.entities.data.SpeciesBatch; +import fr.ifremer.tutti.persistence.entities.data.SpeciesBatchFrequency; +import fr.ifremer.tutti.persistence.entities.referential.Caracteristic; +import fr.ifremer.tutti.persistence.entities.referential.CaracteristicQualitativeValue; +import fr.ifremer.tutti.persistence.entities.referential.CaracteristicType; +import fr.ifremer.tutti.persistence.entities.referential.Species; +import fr.ifremer.tutti.persistence.service.measure.MeasurementPersistenceHelper; + +@Service("batchPersistenceService") +public class BatchPersistenceServiceImpl + extends AbstractPersistenceService implements BatchPersistenceService, CatchBatchValidator + { + + /** Logger. */ + private static final Log log = + LogFactory.getLog(BatchPersistenceServiceImpl.class); + + @Resource(name = "referentialPersistenceService") + protected ReferentialPersistenceService referentialService; + + @Resource(name = "denormalizedBatchDao") + protected DenormalizedBatchDao denormalizedBatchDao; + + @Resource(name = "sortingBatchDao") + protected SortingBatchDao sortingBatchDao; + + @Resource(name = "catchBatchDao") + protected CatchBatchExtendDao catchBatchDao; + + @Resource(name = "measurementPersistenceHelper") + protected MeasurementPersistenceHelper measurementHelper; + + @Override + public void init() { + super.init(); + catchBatchDao.registerCatchBatchValidator(this); + } + + @Override + public void close() { + catchBatchDao.unregisterCatchBatchValidator(this); + super.close(); + } + + @Override + public boolean isEnable(fr.ifremer.adagio.core.dao.data.batch.CatchBatch catchBatch) { + // Apply validation only on catch batch for fishingOperation + return (catchBatch.getFishingOperation() != null); + } + + @Override + public List<CatchBatchValidationError> validate(fr.ifremer.adagio.core.dao.data.batch.CatchBatch catchBatch) { + List<CatchBatchValidationError> errors = Lists.newArrayList(); + validate(catchBatch.getChildBatchs(), errors, 1); + return errors; + } + + @Override + public CatchBatch getCatchBatchFromFishingOperation(String fishingOperationId) { + Preconditions.checkNotNull(fishingOperationId); + + Integer catchBatchId = catchBatchDao.getIdByFishingOperationId(Integer.valueOf(fishingOperationId)); + + fr.ifremer.adagio.core.dao.data.batch.CatchBatch source; + try { + source = catchBatchDao.loadFullTree(catchBatchId, true, true); + } catch (CatchBatchValidationException e) { + throw new DataRetrievalFailureException("Invalid batch tree structure, for fishing operation id=" + fishingOperationId, e); + } + + CatchBatch result = new CatchBatch(); + result.setId(source.getId().toString()); + result.setCatchTotalWeight(source.getWeight()); + + // Vrac + SortingBatch vracBatch = catchBatchDao.getSortingBatch(source.getChildBatchs(), + "pmfmId", enumeration.PMFM_ID_SORTED_UNSORTED, enumeration.QUALITATIVE_VRAC_ID); + result.setCatchTotalSortedCarousselWeight(vracBatch.getWeight()); + result.setCatchTotalSortedTremisWeight(vracBatch.getWeightBeforeSampling()); + { + // Species + SortingBatch speciesBatch = catchBatchDao.getSortingBatch(vracBatch.getChildBatchs(), + "pmfmId", enumeration.PMFM_ID_SORTING_TYPE, enumeration.QUALITATIVE_ID_SORTING_TYPE_SPECIES); + + result.setSpeciesTotalSampleSortedWeight(speciesBatch.getWeight()); + result.setSpeciesTotalSortedWeight(speciesBatch.getWeightBeforeSampling()); + + { + // Inert + SortingBatch inertBatch = catchBatchDao.getSortingBatch(speciesBatch.getChildBatchs(), + "referenceTaxonId", enumeration.REFERENCE_TAXON_ID_INERT); + result.setSpeciesTotalInertWeight(inertBatch.getWeight()); + + // Alive no itemized + SortingBatch livingNotItemizedBatch = catchBatchDao.getSortingBatch(speciesBatch.getChildBatchs(), + "referenceTaxonId", enumeration.REFERENCE_TAXON_ID_LIFE); + result.setSpeciesTotalLivingNotItemizedWeight(livingNotItemizedBatch.getWeight()); + } + + // TODO : Benthos, Plancton... + } + + // Hors Vrac + SortingBatch horsVracBatch = catchBatchDao.getSortingBatch(source.getChildBatchs(), + "pmfmId", enumeration.PMFM_ID_SORTED_UNSORTED, enumeration.QUALITATIVE_HORS_VRAC_ID); + result.setCatchTotalUnsortedWeight(horsVracBatch.getWeight()); + { + // Species + SortingBatch speciesBatch = catchBatchDao.getSortingBatch(horsVracBatch.getChildBatchs(), + "pmfmId", enumeration.PMFM_ID_SORTING_TYPE, enumeration.QUALITATIVE_ID_SORTING_TYPE_SPECIES); + + result.setSpeciesTotalUnsortedWeight(speciesBatch.getWeight()); + + // TODO : Benthos, Plancton... + } + + // Non trié + SortingBatch unsortedBatch = catchBatchDao.getSortingBatch(source.getChildBatchs(), + "pmfmId", enumeration.PMFM_ID_SORTED_UNSORTED, enumeration.QUALITATIVE_UNSORTED_ID); + result.setCatchTotalRejectedWeight(unsortedBatch.getWeight()); + + return result; + } + + @Override + + public CatchBatch createCatchBatch(CatchBatch bean) { + Preconditions.checkNotNull(bean); + Preconditions.checkArgument(bean.getId() == null); + Preconditions.checkNotNull(bean.getFishingOperation()); + Preconditions.checkNotNull(bean.getFishingOperation().getId()); + + fr.ifremer.adagio.core.dao.data.batch.CatchBatch catchBatch = fr.ifremer.adagio.core.dao.data.batch.CatchBatch.Factory.newInstance(); + beanToEntity(bean, catchBatch, true); + catchBatch = catchBatchDao.create(catchBatch); + bean.setId(String.valueOf(catchBatch.getId())); + + // Link to fishing operation + getCurrentSession().flush(); + Integer fishingOperationId = Integer.valueOf(bean.getFishingOperation().getId()); + int rowUpdated = queryUpdate("updateFishingOperationCatchBatch", + "fishingOperationId", IntegerType.INSTANCE, fishingOperationId, + "catchBatchId", IntegerType.INSTANCE, catchBatch.getId()); + if (rowUpdated == 0) { + throw new DataIntegrityViolationException("Could not attach catch batch to the given operation : operation was not found."); + } + + return bean; + } + + @Override + @CacheEvict(value = "batchTree", key="#bean.fishingOperation.id") + public CatchBatch saveCatchBatch(CatchBatch bean) { + + Preconditions.checkNotNull(bean); + Preconditions.checkNotNull(bean.getId()); + + getCurrentSession().enableFetchProfile("batch-with-childs"); + getCurrentSession().setFlushMode(FlushMode.COMMIT); + fr.ifremer.adagio.core.dao.data.batch.CatchBatch catchBatch = catchBatchDao.load(Integer.valueOf(bean.getId())); + if (catchBatch == null) { + throw new DataRetrievalFailureException("Could not retrieve catch batch with id=" + bean.getId()); + } + + beanToEntity(bean, catchBatch, true); + catchBatchDao.update(catchBatch); + getCurrentSession().flush(); + + return bean; + } + + @Override + public List<SpeciesBatch> getAllRootSpeciesBatch(String fishingOperationId) { + + // Load batch tree + Integer catchBatchId = catchBatchDao.getIdByFishingOperationId(Integer.valueOf(fishingOperationId)); + fr.ifremer.adagio.core.dao.data.batch.CatchBatch catchBatch = catchBatchDao.loadFullTree(catchBatchId); + + List<SpeciesBatch> result = new ArrayList<SpeciesBatch>(); + + // Vrac / Species + SortingBatch vracSpeciesBatch = catchBatchDao.getSortingBatch(catchBatch.getChildBatchs(), + "pmfmId", enumeration.PMFM_ID_SORTED_UNSORTED, enumeration.QUALITATIVE_VRAC_ID, + "pmfmId", enumeration.PMFM_ID_SORTING_TYPE, enumeration.QUALITATIVE_ID_SORTING_TYPE_SPECIES); + for (Iterator<Batch> iterator = vracSpeciesBatch.getChildBatchs().iterator(); iterator.hasNext();) { + SortingBatch source = (SortingBatch) iterator.next(); + + // evict some special batches : Life (or Biota) and Inert + if (source.getReferenceTaxon() != null + && enumeration.REFERENCE_TAXON_ID_LIFE.equals(source.getReferenceTaxon().getId()) == false + && enumeration.REFERENCE_TAXON_ID_INERT.equals(source.getReferenceTaxon().getId()) == false) { + SpeciesBatch target = new SpeciesBatch(); + + entityToBean(source, target); + result.add(target); + } + } + + // Hors-Vrac / Species + SortingBatch horsVracSpeciesBatch = catchBatchDao.getSortingBatch(catchBatch.getChildBatchs(), + "pmfmId", enumeration.PMFM_ID_SORTED_UNSORTED, enumeration.QUALITATIVE_HORS_VRAC_ID, + "pmfmId", enumeration.PMFM_ID_SORTING_TYPE, enumeration.QUALITATIVE_ID_SORTING_TYPE_SPECIES); + for (Iterator<Batch> iterator = horsVracSpeciesBatch.getChildBatchs().iterator(); iterator.hasNext();) { + SortingBatch source = (SortingBatch) iterator.next(); + + SpeciesBatch target = new SpeciesBatch(); + + entityToBean(source, target); + result.add(target); + } + + return result; + } + + protected SpeciesBatch entityToBean(SortingBatch source, SpeciesBatch target) { + + target.setId(source.getId().toString()); + + // 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()); + } + + // 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 = measurementHelper.getInheritedSortingMeasurement(source, enumeration.PMFM_ID_SORTED_UNSORTED); + } + if (sm != null) { + SampleCategoryEnum sampleCategory = referentialService.getSampleCategoryByPmfmId(sm.getPmfm().getId()); + if (sampleCategory != null) { + setSampleCategoryQualitative(target, sm.getPmfm().getId(), sm.getNumericalValue(), sm.getAlphanumericalValue(), sm.getQualitativeValue().getId()); + } + } + + // Species + Integer referenceTaxonId = source.getInheritedReferenceTaxonId(); + if (referenceTaxonId == null && source.getReferenceTaxon() != null) { + referenceTaxonId = source.getReferenceTaxon().getId(); + } + if (referenceTaxonId != null) { + // TODO : add cache on getSpeciesByReferenceTaxonId + Species species = referentialService.getSpeciesByReferenceTaxonId(referenceTaxonId); + target.setSpecies(species); + } + + if (target.getSampleCategoryType() != null && source.getChildBatchs().size() > 0) { + List<SpeciesBatch> targetChilds = Lists.newArrayList(); + for (Iterator<Batch> iterator = source.getChildBatchs().iterator(); iterator.hasNext();) { + SortingBatch sourceChild = (SortingBatch) iterator.next(); + SpeciesBatch targetChild = new SpeciesBatch(); + entityToBean(sourceChild, targetChild); + if (targetChild.getSampleCategoryValue() != null) { + targetChilds.add(targetChild); + targetChild.setParentBatch(target); + } + } + target.setChildBatchs(targetChilds); + } + + return target; + } + + @Override + public SpeciesBatch createSpeciesBatch(SpeciesBatch bean, + String parentBatchId) { + Preconditions.checkNotNull(bean); + Preconditions.checkArgument(bean.getId() == null); + Preconditions.checkNotNull(bean.getSpecies()); + Preconditions.checkNotNull(bean.getSpecies().getId()); + Preconditions.checkNotNull(bean.getFishingOperation()); + Preconditions.checkNotNull(bean.getFishingOperation().getId()); + + getCurrentSession().setFlushMode(FlushMode.COMMIT); + + // Load full batch tree + Integer catchBatchId = catchBatchDao.getIdByFishingOperationId(Integer.valueOf(bean.getFishingOperation().getId())); + fr.ifremer.adagio.core.dao.data.batch.CatchBatch catchBatch = catchBatchDao.loadFullTree(catchBatchId); + + SortingBatch batch = SortingBatch.Factory.newInstance(); + beanToEntity(bean, batch, parentBatchId, catchBatch, true); + batch = sortingBatchDao.create(batch); + + bean.setId(String.valueOf(batch.getId())); + + getCurrentSession().flush(); + + return bean; + } + + @Override + public SpeciesBatch saveSpeciesBatch(SpeciesBatch bean) { + Preconditions.checkNotNull(bean); + Preconditions.checkNotNull(bean.getId()); + + // Load full batch tree + Integer catchBatchId = catchBatchDao.getIdBySortingBatchId(Integer.valueOf(bean.getId())); + fr.ifremer.adagio.core.dao.data.batch.CatchBatch catchBatch = catchBatchDao.loadFullTree(catchBatchId); + + getCurrentSession().setFlushMode(FlushMode.COMMIT); + + SortingBatch batch = catchBatchDao.getSortingBatchById(catchBatch, Integer.valueOf(bean.getId())); + String parentBatchId = null; + if (bean.getParentBatch() != null) { + parentBatchId = bean.getParentBatch().getId(); + } + beanToEntity(bean, batch, parentBatchId, catchBatch, true); + sortingBatchDao.update(batch); + getCurrentSession().flush(); + + return bean; + } + + @Override + public void deleteSpeciesBatch(String id) { + Preconditions.checkNotNull(id); + + getCurrentSession().setFlushMode(FlushMode.COMMIT); + + catchBatchDao.removeWithChildren(Integer.valueOf(id)); + + getCurrentSession().flush(); + } + + public void deleteSpeciesSubBatch(String id) { + deleteSpeciesBatch(id); + } + + public void changeSpeciesBatchSpecies(String batchId, Species species) { + Preconditions.checkNotNull(batchId); + Preconditions.checkNotNull(species); + Preconditions.checkNotNull(species.getReferenceTaxonId()); + + if (log.isDebugEnabled()) { + log.debug("Changing species for batch id=" + batchId); + } + + int rowUpdated = queryUpdate("updateBatchSpecies", + "batchId", IntegerType.INSTANCE, Integer.valueOf(batchId), + "referenceTaxonId", IntegerType.INSTANCE, species.getReferenceTaxonId()); + Preconditions.checkArgument(rowUpdated == 1, "Unable to update operation, to be linked with catch batch."); + } + + public List<SpeciesBatchFrequency> getAllSpeciesBatchFrequency( + String speciesBatchId) { + List<SpeciesBatchFrequency> results = Lists.newArrayList(); + + Integer catchBatchId = catchBatchDao.getIdBySortingBatchId(Integer.valueOf(speciesBatchId)); + fr.ifremer.adagio.core.dao.data.batch.CatchBatch catchBatch = catchBatchDao.loadFullTree(catchBatchId); + + if (catchBatchId == null) { + return results; + } + + SortingBatch sortingBatch = catchBatchDao.getSortingBatchById(catchBatch, Integer.valueOf(speciesBatchId)); + + for (Iterator<Batch> iterator = sortingBatch.getChildBatchs().iterator(); iterator.hasNext();) { + SortingBatch source = (SortingBatch) iterator.next(); + SpeciesBatchFrequency target = new SpeciesBatchFrequency(); + + entityToBean(source, target); + results.add(target); + } + return results; + } + + protected void entityToBean(SortingBatch source, SpeciesBatchFrequency target) { + + target.setId(source.getId().toString()); + + target.setNumber(source.getIndividualCount()); + target.setWeight(source.getWeight()); + target.setComment(source.getComments()); + + // Length step category + if (source.getSortingMeasurements().size() == 1) { + SortingMeasurement sm = source.getSortingMeasurements().iterator().next(); + if (sm.getPmfm() != null && sm.getPmfm().getId() != null) { + Caracteristic lengthStepCaracteristic = referentialService.getCaracteristic(sm.getPmfm().getId()); + target.setLengthStepCaracteristic(lengthStepCaracteristic); + } + + // Length + target.setLengthStep(sm.getNumericalValue()); + } + } + + public List<SpeciesBatchFrequency> saveSpeciesBatchFrequency( + String speciesBatchId, List<SpeciesBatchFrequency> frequencies) { + Preconditions.checkNotNull(speciesBatchId); + Preconditions.checkNotNull(frequencies); + + if (frequencies.size() == 0) { + return frequencies; + } + getCurrentSession().setFlushMode(FlushMode.COMMIT); + + Integer catchBatchId = catchBatchDao.getIdBySortingBatchId(Integer.valueOf(speciesBatchId)); + fr.ifremer.adagio.core.dao.data.batch.CatchBatch catchBatch = catchBatchDao.loadFullTree(catchBatchId); + + if (catchBatch == null) { + return frequencies; + } + + // Retrieve parent + SortingBatch parentBatch = catchBatchDao.getSortingBatchById(catchBatch, Integer.valueOf(speciesBatchId)); + + // Remember child ids, to remove unchanged item (see at bottom in this method) + List<Integer> notUpdatedChildIds = Lists.newArrayList(); + for (Iterator<Batch> iterator = parentBatch.getChildBatchs().iterator(); iterator.hasNext();) { + SortingBatch sortingBatch = (SortingBatch) iterator.next(); + notUpdatedChildIds.add(sortingBatch.getId()); + } + + short rankOrder = 0; + String pmfmId = null; + List<SortingBatch> batchsToUpdate = Lists.newArrayList(); + for (SpeciesBatchFrequency source : frequencies) { + rankOrder++; + + // Check that all frequencies have the same length PMFM + if (pmfmId == null) { + source.getLengthStepCaracteristic().getId(); + } else if (!pmfmId.equals(source.getLengthStepCaracteristic().getId())) { + throw new DataIntegrityViolationException("Batch frequencies under one Speciesbatch must have all the same lengthStepCaracteristic"); + } + + // Not existing batch + SortingBatch target = null; + if (source.getId() == null) { + target = SortingBatch.Factory.newInstance(); + + // Fill the sorting batch from the source + beanToEntity(source, target, parentBatch, rankOrder, true); + + // Create the targeted batch, then update the source id + sortingBatchDao.create(target); + source.setId(target.getId().toString()); + } + + // Existing batch + else { + target = sortingBatchDao.load(Integer.valueOf(source.getId())); + + // Fill the sorting batch from the source + beanToEntity(source, target, parentBatch, rankOrder, true); + + // 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 (batchsToUpdate.size() > 0) { + sortingBatchDao.update(batchsToUpdate); + } + + if (notUpdatedChildIds.size() > 0) { + for (Iterator<Integer> iterator = notUpdatedChildIds.iterator(); iterator.hasNext();) { + Integer batchId = iterator.next(); + catchBatchDao.removeWithChildren(batchId); + } + } + + getCurrentSession().flush(); + + return frequencies; + } + + // ------------------------------------------------------------------------// + // -- Internal methods --// + // ------------------------------------------------------------------------// + + protected void validate(Collection<Batch> batchs, List<CatchBatchValidationError> errors, int treeLevel) { + + List<Integer> expectedPmfms = null; + boolean expectedReferenceTaxon = false; + // First tree level : should have pmfm SORTED (Vrac, Hors Vrac, Non trié) + if (treeLevel == 1) { + expectedPmfms = Lists.newArrayList(enumeration.PMFM_ID_SORTED_UNSORTED); + } + // Second tree level : should have pmfm SORTING_TYPE (Species, Benthos...) + else if (treeLevel == 2) { + expectedPmfms = Lists.newArrayList(enumeration.PMFM_ID_SORTING_TYPE); + } + else if (treeLevel == 3) { + expectedReferenceTaxon = true; + } + else if (treeLevel > 3) { + expectedPmfms = referentialService.getSampleCategoryIds(); + expectedPmfms.remove(enumeration.PMFM_ID_SORTED_UNSORTED); + } + + // First level : should be only PMFM=SORTED + for (Iterator<Batch> iterator = batchs.iterator(); iterator.hasNext();) { + SortingBatch batch = (SortingBatch) iterator.next(); + boolean batchHasError = false; + + // For all sorting measurements + if (batch.getSortingMeasurements() != null && batch.getSortingMeasurements().size() > 0) { + for (Iterator<SortingMeasurement> iterator2 = batch.getSortingMeasurements().iterator(); iterator2.hasNext();) { + SortingMeasurement sm = iterator2.next(); + Integer pmfmId = sm.getPmfm().getId(); + if (expectedReferenceTaxon) { + CatchBatchValidationError error = new CatchBatchValidationError( + "tutti.persistence.batch.validation.onlyTaxonButPmfmFound", + _("tutti.persistence.batch.validation.onlyTaxonButPmfmFound", batch.getId(), pmfmId), + CatchBatchValidationError.GRAVITY_ERROR); + errors.add(error); + } + else if (expectedPmfms.contains(pmfmId) == false) { + if (sm.getNumericalValue() == null || batch.getChildBatchs().size() > 0) { + batchHasError = true; + CatchBatchValidationError error = new CatchBatchValidationError( + "tutti.persistence.batch.validation.pmfmNotFound", + _("tutti.persistence.batch.validation.pmfmNotFound", + batch.getId(), Arrays.toString(expectedPmfms.toArray()), pmfmId), + CatchBatchValidationError.GRAVITY_ERROR); + errors.add(error); + } + } + } + } + else if (expectedPmfms != null && expectedPmfms.size() > 0){ + batchHasError = true; + CatchBatchValidationError error = new CatchBatchValidationError( + "tutti.persistence.batch.validation.pmfmNotFound", + _("tutti.persistence.batch.validation.pmfmNotFound", + batch.getId(), Arrays.toString(expectedPmfms.toArray()), "null"), + CatchBatchValidationError.GRAVITY_ERROR); + errors.add(error); + } + + if (!batchHasError && expectedReferenceTaxon == true && batch.getReferenceTaxon() == null) { + CatchBatchValidationError error = new CatchBatchValidationError( + "tutti.persistence.batch.validation.referenceTaxonNotFound", + _("tutti.persistence.batch.validation.referenceTaxonNotFound", batch.getId()), + CatchBatchValidationError.GRAVITY_ERROR); + errors.add(error); + } + + if (!batchHasError) { + validate(batch.getChildBatchs(), errors, treeLevel+1); + } + } + } + + protected void beanToEntity(SpeciesBatch source, + SortingBatch target, + String parentBatchId, + fr.ifremer.adagio.core.dao.data.batch.CatchBatch catchBatch, + boolean copyIfNull) { + Preconditions.checkNotNull(source.getFishingOperation()); + Preconditions.checkNotNull(source.getFishingOperation().getId()); + + // Retrieve recorder department + Integer recorderDepartmentId = getRecorderDepartmentId(); + + // Create lists to store all updates, then remove not updated items + Set<QuantificationMeasurement> notChangedQuantificationMeasurements = new HashSet<QuantificationMeasurement>(); + if (target.getQuantificationMeasurements() != null) { + notChangedQuantificationMeasurements.addAll(target.getQuantificationMeasurements()); + } + Set<SortingMeasurement> notChangedSortingMeasurements = new HashSet<SortingMeasurement>(); + if (target.getSortingMeasurements() != null) { + notChangedSortingMeasurements.addAll(target.getSortingMeasurements()); + } + + // If parent and root need to be set + if (target.getId() == null + || target.getRootBatch() == null + || (target.getParentBatch() != null && !target.getParentBatch().getId().toString().equals(parentBatchId))) { + setBatchParents(source, target, parentBatchId, catchBatch); + } + + // RankOrder (initialize once, at creation) + if (target.getRankOrder() == null) { + // Start rank order at 3, because of special batches 'Biota' and 'Inert' + short rankOrder = (short) 3; + if (source.getParentBatch() != null && source.getParentBatch().getChildBatchs() != null) { + // TODO BL : vérifier cela (est-ce que l'item est ajouté à son père AVANT d'être passé au service ?) + rankOrder += (short) source.getParentBatch().getChildBatchs().size(); + } + target.setRankOrder(rankOrder); + } + + // Force subgroup count to '1', as Allegro + target.setSubgroupCount(1f); + + // Weight or SampleCategoryWeight + if (copyIfNull && source.getWeight() == null && source.getSampleCategoryWeight() == null) { + // Nothing to do : will be removed later, using notChangedSortingMeasurements + } else if (source.getSampleCategoryWeight() != null && source.getWeight() == null) { + QuantificationMeasurement quantificationMeasurement = measurementHelper.setQuantificationMeasurement(target, + enumeration.PMFM_ID_WEIGHT_MEASURED, recorderDepartmentId, source.getSampleCategoryWeight(), true); + notChangedQuantificationMeasurements.remove(quantificationMeasurement); + } else if (source.getWeight() != null && source.getSampleCategoryWeight() == null) { + QuantificationMeasurement quantificationMeasurement = measurementHelper.setQuantificationMeasurement(target, + enumeration.PMFM_ID_WEIGHT_MEASURED, recorderDepartmentId, source.getWeight(), true); + notChangedQuantificationMeasurements.remove(quantificationMeasurement); + } + + // Sampling Ratio + if (copyIfNull && (source.getSampleCategoryWeight() == null || source.getWeight() == null)) { + target.setSamplingRatio(null); + target.setSamplingRatioText(null); + } else if (source.getSampleCategoryWeight() != null && source.getWeight() != null) { + String samplingRatioText = source.getWeight() + "/" + source.getSampleCategoryWeight(); + samplingRatioText = samplingRatioText.replaceAll(",", "."); + target.setSamplingRatioText(samplingRatioText); + target.setSamplingRatio(source.getWeight() / source.getSampleCategoryWeight()); + QuantificationMeasurement quantificationMeasurement = measurementHelper.setQuantificationMeasurement(target, + enumeration.PMFM_ID_WEIGHT_MEASURED, recorderDepartmentId, source.getWeight(), true); + notChangedQuantificationMeasurements.remove(quantificationMeasurement); + } + + // Sorting measurement + if (copyIfNull && (source.getSampleCategoryType() == null || source.getSampleCategoryValue() == null)) { + // Nothing to do : will be removed later, using notChangedSortingMeasurements + } else if (source.getSampleCategoryType() != null && source.getSampleCategoryValue() != null) { + Integer pmfmId = measurementHelper.sampleCategory2PmfmId(source.getSampleCategoryType()); + // Do not store sorting measurement if pmfm = SORTED (already store in an ancestor batch) + if (!pmfmId.equals(enumeration.PMFM_ID_SORTED_UNSORTED)) { + SortingMeasurement sortingMeasurement = measurementHelper.setSortingMeasurement(target, recorderDepartmentId, + source.getSampleCategoryType(), source.getSampleCategoryValue()); + notChangedSortingMeasurements.remove(sortingMeasurement); + } + } + + // Individual count + if (copyIfNull && source.getNumber() == null) { + target.setIndividualCount(null); + } else if (source.getNumber() != null) { + target.setIndividualCount(source.getNumber()); + } + + // Species + if (copyIfNull && (source.getSpecies() == null || parentBatchId != null)) { + target.setReferenceTaxon(null); + } else if (source.getSpecies() != null && parentBatchId == null) { + ReferenceTaxon referenceTaxon = load(ReferenceTaxonImpl.class, source.getSpecies().getReferenceTaxonId()); + target.setReferenceTaxon(referenceTaxon); + } + + // QualityFlag + if (source.isSpeciesToConfirm()) { + target.setQualityFlag(load(QualityFlagImpl.class, enumeration.QUALITY_FLAG_CODE_DOUBTFUL)); + } else { + target.setQualityFlag(load(QualityFlagImpl.class, enumeration.QUALITY_FLAG_CODE_NOT_QUALIFIED)); + } + + // Comments + if (copyIfNull && source.getComment() == null) { + target.setComments(null); + } else if (source.getComment() != null) { + target.setComments(source.getComment()); + } + + // Exhaustive inventory (always true under a species batch) + target.setExhaustiveInventory(true); + + // Removed not changed measurements (in sorting and quantification measurement lists) + if (target.getQuantificationMeasurements() != null && notChangedQuantificationMeasurements.size() > 0) { + for (QuantificationMeasurement qm : notChangedQuantificationMeasurements) { + target.getQuantificationMeasurements().remove(qm); + } + } + if (target.getSortingMeasurements() != null && notChangedSortingMeasurements.size() > 0) { + for (SortingMeasurement sm : notChangedSortingMeasurements) { + target.getSortingMeasurements().remove(sm); + } + } + } + + public void setBatchParents(SpeciesBatch source, SortingBatch target, String parentBatchIdStr, + fr.ifremer.adagio.core.dao.data.batch.CatchBatch catchBatch) { + + Preconditions.checkNotNull(target); + Preconditions.checkNotNull(source.getFishingOperation()); + Preconditions.checkNotNull(source.getFishingOperation().getId()); + + // Load existing parent and root + SortingBatch parentBatch = null; + if (parentBatchIdStr != null) { + parentBatch = catchBatchDao.getSortingBatchById(catchBatch, Integer.valueOf(parentBatchIdStr)); + } + + // Or retrieve parent batch, from pmfm id + else { + // Retrieve category type + Integer pmfmId = measurementHelper.sampleCategory2PmfmId(source.getSampleCategoryType()); + if (pmfmId == null || !pmfmId.equals(enumeration.PMFM_ID_SORTED_UNSORTED)) { + throw new DataIntegrityViolationException(MessageFormat.format( + "A species batch with no parent should have a sampleCategoryType {0} (PMFM.ID={1})", + SampleCategoryEnum.sortedUnsorted.name(), + enumeration.PMFM_ID_SORTED_UNSORTED)); + } + Integer qualitativeValueId = convertSampleCategoryValueIntoQualitativeId(source.getSampleCategoryValue()); + + parentBatch = catchBatchDao.getSortingBatch(catchBatch.getChildBatchs(), + "pmfmId", pmfmId, qualitativeValueId, + "pmfmId", enumeration.PMFM_ID_SORTING_TYPE, enumeration.QUALITATIVE_ID_SORTING_TYPE_SPECIES); + + // Parent Batch + target.setParentBatch(parentBatch); + } + + + if (parentBatch == null) { + throw new DataIntegrityViolationException( + "Could not retrieve parent batch, for a given speciesBatch : invalid batch tree structure. Please make sure CatchBatch has been saved before to create a SpeciesBatch."); + } + + // Parent Batch + target.setParentBatch(parentBatch); + target.setRootBatch(catchBatch); + } + + Integer convertSampleCategoryValueIntoQualitativeId(Serializable value) { + if (value == null) + return null; + Integer qualitativeValueId = null; + if (value instanceof CaracteristicQualitativeValue) { + CaracteristicQualitativeValue cqValue = (CaracteristicQualitativeValue) value; + qualitativeValueId = Integer.valueOf(cqValue.getId()); + } else if (value instanceof String) { + qualitativeValueId = Integer.valueOf((String) value); + } + return qualitativeValueId; + } + + protected void setSampleCategoryQualitative(SpeciesBatch target, Integer pmfmId, Float numericalvalue, String alphanumericalValue, + Integer qualitativeValueId) { + // skip if null or corresponding to the SORTING_TYPE PMFM (Expèce, Benthos, Plancton, etc.) + if (pmfmId == null || pmfmId.equals(enumeration.PMFM_ID_SORTING_TYPE)) { + return; + } + + SampleCategoryEnum sampleCategory = referentialService.getSampleCategoryByPmfmId(pmfmId); + Preconditions.checkNotNull(sampleCategory, "Unable to find corresponding SampleCategoryEnum for PMFM.ID : " + pmfmId); + + target.setSampleCategoryType(sampleCategory); + if (numericalvalue != null) { + target.setSampleCategoryValue(numericalvalue); + return; + } + if (alphanumericalValue != null) { + target.setSampleCategoryValue(alphanumericalValue); + return; + } + + Caracteristic caracteristic = referentialService.getCaracteristic(pmfmId); + if (caracteristic == null || caracteristic.getCaracteristicType() != CaracteristicType.QUALITATIVE) { + return; + } + CaracteristicQualitativeValue value = null; + for (CaracteristicQualitativeValue qv : caracteristic.getQualitativeValue()) { + if (qualitativeValueId == Integer.parseInt(qv.getId())) { + value = qv; + break; + } + } + target.setSampleCategoryValue(value); + } + + protected void beanToEntity(SpeciesBatchFrequency source, SortingBatch target, SortingBatch parentBatch, short rankOrder, boolean copyIfNull) { + Preconditions.checkNotNull(source.getBatch()); + Preconditions.checkNotNull(source.getBatch().getId()); + + // Retrieve recorder department + Integer recorderDepartmentId = getRecorderDepartmentId(); + + // Create lists to store all updates, then remove not updated items + Set<QuantificationMeasurement> notChangedQuantificationMeasurements = new HashSet<QuantificationMeasurement>(); + if (target.getQuantificationMeasurements() != null) { + notChangedQuantificationMeasurements.addAll(target.getQuantificationMeasurements()); + } + Set<SortingMeasurement> notChangedSortingMeasurements = new HashSet<SortingMeasurement>(); + if (target.getSortingMeasurements() != null) { + notChangedSortingMeasurements.addAll(target.getSortingMeasurements()); + } + + // 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); + + // Weight or SampleCategoryWeight + if (copyIfNull && source.getWeight() == null) { + // Nothing to do : will be removed later, using notChangedSortingMeasurements + } else if (source.getWeight() != null) { + QuantificationMeasurement quantificationMeasurement = measurementHelper.setQuantificationMeasurement(target, + enumeration.PMFM_ID_WEIGHT_MEASURED, recorderDepartmentId, source.getWeight(), true); + notChangedQuantificationMeasurements.remove(quantificationMeasurement); + } + + // Sorting measurement + if (copyIfNull && (source.getLengthStepCaracteristic() == null || source.getLengthStep() == null)) { + // Nothing to do : will be removed later, using notChangedSortingMeasurements + } else if (source.getLengthStepCaracteristic() != null && source.getLengthStep() != null) { + Integer pmfmId = Integer.valueOf(source.getLengthStepCaracteristic().getId()); + SortingMeasurement sortingMeasurement = measurementHelper.setSortingMeasurement(target, recorderDepartmentId, pmfmId, + source.getLengthStep()); + notChangedSortingMeasurements.remove(sortingMeasurement); + } + + // Individual count + if (copyIfNull && source.getNumber() == null) { + target.setIndividualCount(null); + } else if (source.getNumber() != null) { + target.setIndividualCount(source.getNumber()); + } + + // Species + target.setReferenceTaxon(null); + + // QualityFlag + target.setQualityFlag(parentBatch.getQualityFlag()); + + // Comments + if (copyIfNull && source.getComment() == null) { + target.setComments(null); + } else if (source.getComment() != null) { + target.setComments(source.getComment()); + } + + // Exhaustive inventory (always true under a species batch) + target.setExhaustiveInventory(true); + + // Removed not changed measurements (in sorting and quantification measurement lists) + if (target.getQuantificationMeasurements() != null && notChangedQuantificationMeasurements.size() > 0) { + for (QuantificationMeasurement qm : notChangedQuantificationMeasurements) { + target.getQuantificationMeasurements().remove(qm); + } + } + if (target.getSortingMeasurements() != null && notChangedSortingMeasurements.size() > 0) { + for (SortingMeasurement sm : notChangedSortingMeasurements) { + target.getSortingMeasurements().remove(sm); + } + } + } + + protected UIWeight convertDatabase2UI(Float weight, Float samplingRatio, String samplingRatioText) { + UIWeight result = new UIWeight(); + + // Weight & sampleCategory Weight + if (samplingRatio == null) { + result.sampleCategoryWeight = weight; + } else if (weight != null) { + String startStr = weight.toString().replace(',', '.') + "/"; + if (samplingRatioText != null && samplingRatioText.startsWith(startStr)) { + String weightStr = samplingRatioText.substring(startStr.length()); + if (weightStr != null && !weightStr.isEmpty()) { + result.sampleCategoryWeight = Float.parseFloat(weightStr); + result.weight = weight; + } + } else { + result.sampleCategoryWeight = weight; + } + } + return result; + } + + static class UIWeight { + Float sampleCategoryWeight = null; + + Float weight = null; + } + + protected void beanToEntity(CatchBatch source, + fr.ifremer.adagio.core.dao.data.batch.CatchBatch target, + boolean copyIfNull) { + Preconditions.checkNotNull(source.getFishingOperation()); + Preconditions.checkNotNull(source.getFishingOperation().getId()); + + // Retrieve recorder department + Integer recorderDepartmentId = getRecorderDepartmentId(); + + // First initialization (when created) + Integer fishingOperationId = Integer.valueOf(source.getFishingOperation().getId()); + target.setFishingOperation(load(FishingOperationImpl.class, fishingOperationId)); + + target.setQualityFlag(load(QualityFlagImpl.class, enumeration.QUALITY_FLAG_CODE_NOT_QUALIFIED)); + target.setRankOrder((short) 1); + target.setSynchronizationStatus(SynchronizationStatus.DIRTY.getValue()); + + // Create lists to store all updates, then remove not updated items + Set<QuantificationMeasurement> notChangedQuantificationMeasurements = new HashSet<QuantificationMeasurement>(); + if (target.getQuantificationMeasurements() != null) { + notChangedQuantificationMeasurements.addAll(target.getQuantificationMeasurements()); + } + + // Total Weight + if (copyIfNull && source.getCatchTotalWeight() == null) { + // Nothing to do : will be removed later, using notChangedQuantificationMeasurements + } else if (source.getCatchTotalWeight() != null) { + QuantificationMeasurement quantificationMeasurement = measurementHelper.setQuantificationMeasurement(target, + enumeration.PMFM_ID_WEIGHT_MEASURED, recorderDepartmentId, source.getCatchTotalWeight(), true); + notChangedQuantificationMeasurements.remove(quantificationMeasurement); + } + + // Removed not changed measurements (in sorting and quantification measurement lists) + if (target.getQuantificationMeasurements() != null && notChangedQuantificationMeasurements.size() > 0) { + for (QuantificationMeasurement qm : notChangedQuantificationMeasurements) { + target.getQuantificationMeasurements().remove(qm); + } + } + Map<Integer, SortingBatch> catchBatchChilds = getChildsMap(target, enumeration.PMFM_ID_SORTED_UNSORTED); + + // ----------------------------------------------------------------------------- + // Sorted Vrac + // ----------------------------------------------------------------------------- + { + SortingBatch batch = catchBatchChilds.get(enumeration.QUALITATIVE_VRAC_ID); + if (batch == null) { + batch = SortingBatch.Factory.newInstance(); + target.getChildBatchs().add(batch); + } + beanToEntitySortingBatch(target, target, batch, recorderDepartmentId, + enumeration.PMFM_ID_SORTED_UNSORTED, enumeration.QUALITATIVE_VRAC_ID, + source.getCatchTotalSortedCarousselWeight(), source.getCatchTotalSortedTremisWeight(), + copyIfNull); + batch.setRankOrder((short) 1); + + // Manage childs : + { + Map<Integer, SortingBatch> batchChilds = getChildsMap(batch, enumeration.PMFM_ID_SORTING_TYPE); + + // ----------------------------------------------------------------------------- + // Sorted Vrac / Species + // ----------------------------------------------------------------------------- + SortingBatch speciesBatch = batchChilds.get(enumeration.QUALITATIVE_ID_SORTING_TYPE_SPECIES); + if (speciesBatch == null) { + speciesBatch = SortingBatch.Factory.newInstance(); + if (batch.getChildBatchs() == null) { + batch.setChildBatchs(Lists.newArrayList((Batch) speciesBatch)); + } else { + batch.getChildBatchs().add(speciesBatch); + } + } + beanToEntitySortingBatch(target, batch, speciesBatch, recorderDepartmentId, + enumeration.PMFM_ID_SORTING_TYPE, enumeration.QUALITATIVE_ID_SORTING_TYPE_SPECIES, + source.getSpeciesTotalSampleSortedWeight(), source.getSpeciesTotalSortedWeight(), + copyIfNull); + speciesBatch.setRankOrder((short) 1); + + { + + // ----------------------------------------------------------------------------- + // Sorted Vrac / Species / Alive not itemized + // ----------------------------------------------------------------------------- + SortingBatch aliveNotItemizedBatch = catchBatchDao.getSortingBatch(speciesBatch.getChildBatchs(), "referenceTaxonId", enumeration.REFERENCE_TAXON_ID_LIFE); + if (aliveNotItemizedBatch == null) { + aliveNotItemizedBatch = SortingBatch.Factory.newInstance(); + if (speciesBatch.getChildBatchs() == null) { + speciesBatch.setChildBatchs(Lists.newArrayList((Batch) aliveNotItemizedBatch)); + } else { + speciesBatch.getChildBatchs().add(aliveNotItemizedBatch); + } + } + beanToEntityReferenceTaxonBatch(target, speciesBatch, aliveNotItemizedBatch, recorderDepartmentId, + enumeration.REFERENCE_TAXON_ID_LIFE, + source.getSpeciesTotalLivingNotItemizedWeight(), null, + copyIfNull); + aliveNotItemizedBatch.setRankOrder((short) 1); + + // ----------------------------------------------------------------------------- + // Sorted Vrac / Species / Inert (not alive) + // ----------------------------------------------------------------------------- + SortingBatch inertBatch = catchBatchDao.getSortingBatch(speciesBatch.getChildBatchs(), "referenceTaxonId", enumeration.REFERENCE_TAXON_ID_INERT); + if (inertBatch == null) { + inertBatch = SortingBatch.Factory.newInstance(); + speciesBatch.getChildBatchs().add(inertBatch); + } + beanToEntityReferenceTaxonBatch(target, speciesBatch, inertBatch, recorderDepartmentId, + enumeration.REFERENCE_TAXON_ID_INERT, + source.getSpeciesTotalInertWeight(), null, + copyIfNull); + inertBatch.setRankOrder((short) 2); + } + } + // TODO BL : benthos, plancton, macro déchet... + } + + // ----------------------------------------------------------------------------- + // Sorted Hors Vrac + // ----------------------------------------------------------------------------- + { + SortingBatch batch = catchBatchChilds.get(enumeration.QUALITATIVE_HORS_VRAC_ID); + if (batch == null) { + batch = SortingBatch.Factory.newInstance(); + target.getChildBatchs().add(batch); + } + beanToEntitySortingBatch(target, target, batch, recorderDepartmentId, + enumeration.PMFM_ID_SORTED_UNSORTED, enumeration.QUALITATIVE_HORS_VRAC_ID, + source.getCatchTotalUnsortedWeight(), null, + copyIfNull); + batch.setRankOrder((short) 2); + + // Manage childs : + { + Map<Integer, SortingBatch> batchChilds = getChildsMap(batch, enumeration.PMFM_ID_SORTING_TYPE); + + // Species : + SortingBatch speciesBatch = batchChilds.get(enumeration.QUALITATIVE_ID_SORTING_TYPE_SPECIES); + if (speciesBatch == null) { + speciesBatch = SortingBatch.Factory.newInstance(); + if (batch.getChildBatchs() == null) { + batch.setChildBatchs(Lists.newArrayList((Batch) speciesBatch)); + } else { + batch.getChildBatchs().add(speciesBatch); + } + } + beanToEntitySortingBatch(target, batch, speciesBatch, recorderDepartmentId, + enumeration.PMFM_ID_SORTING_TYPE, enumeration.QUALITATIVE_ID_SORTING_TYPE_SPECIES, + source.getSpeciesTotalUnsortedWeight(), null, + copyIfNull); + speciesBatch.setRankOrder((short) 1); + } + + // TODO BL : benthos, plancton, macro déchet... + } + + // ----------------------------------------------------------------------------- + // Unsorted (=rejected) + // ----------------------------------------------------------------------------- + // Unsorted : + SortingBatch unsortedBatch = catchBatchChilds.get(enumeration.QUALITATIVE_UNSORTED_ID); + if (unsortedBatch == null) { + unsortedBatch = SortingBatch.Factory.newInstance(); + target.getChildBatchs().add(unsortedBatch); + } + beanToEntitySortingBatch(target, target, unsortedBatch, recorderDepartmentId, + enumeration.PMFM_ID_SORTED_UNSORTED, enumeration.QUALITATIVE_UNSORTED_ID, + source.getCatchTotalRejectedWeight(), null, + copyIfNull); + unsortedBatch.setRankOrder((short) 3); + + } + + protected Map<Integer, SortingBatch> getChildsMap(Batch parentBatch, Integer pmfmId) { + Map<Integer, SortingBatch> batchByQualitativeValueId = new HashMap<Integer, SortingBatch>(); + if (parentBatch.getChildBatchs() == null) { + return batchByQualitativeValueId; + } + for (Batch childBatch : parentBatch.getChildBatchs()) { + SortingBatch childSortingBatch = (SortingBatch) childBatch; + SortingMeasurement sm = measurementHelper.getSortingMeasurement((SortingBatch) childBatch, pmfmId, null, false); + if (sm != null && sm.getQualitativeValue() != null && sm.getQualitativeValue().getId() != null) { + batchByQualitativeValueId.put(sm.getQualitativeValue().getId(), childSortingBatch); + } + } + return batchByQualitativeValueId; + } + + /** + * Retrieve child batchs if species is "Life" or "No life" + * (need to retrieve: speciesTotalLivingNotItemizedWeight and speciesTotalInertWeight) + * + * @param parentBatch + * @return + */ + protected Map<Integer, SortingBatch> getChildAliveNotSortedOrInert(Batch parentBatch) { + Map<Integer, SortingBatch> batchByQualitativeValueId = new HashMap<Integer, SortingBatch>(); + if (parentBatch.getChildBatchs() == null) { + return batchByQualitativeValueId; + } + for (Batch childBatch : parentBatch.getChildBatchs()) { + SortingBatch childSortingBatch = (SortingBatch) childBatch; + if (childSortingBatch.getReferenceTaxon() != null && childSortingBatch.getId().intValue() < 0) { + if (enumeration.REFERENCE_TAXON_ID_LIFE.equals(childSortingBatch.getId())) { + batchByQualitativeValueId.put(enumeration.REFERENCE_TAXON_ID_LIFE, childSortingBatch); + } else if (enumeration.REFERENCE_TAXON_ID_LIFE.equals(childSortingBatch.getId())) { + batchByQualitativeValueId.put(enumeration.REFERENCE_TAXON_ID_LIFE, childSortingBatch); + } + } + } + return batchByQualitativeValueId; + } + + protected void beanToEntitySortingBatch( + fr.ifremer.adagio.core.dao.data.batch.CatchBatch rootBatch, + fr.ifremer.adagio.core.dao.data.batch.Batch parentBatch, + fr.ifremer.adagio.core.dao.data.batch.SortingBatch target, + Integer recorderDepartmentId, + Integer sortingPmfmId, + Integer sortingQualitativeValueId, + Float weight, + Float sampleWeight, + boolean copyIfNull) { + + // Create lists to store all updates, then remove not updated items + Set<QuantificationMeasurement> notChangedQuantificationMeasurements = new HashSet<QuantificationMeasurement>(); + if (target.getQuantificationMeasurements() != null) { + notChangedQuantificationMeasurements.addAll(target.getQuantificationMeasurements()); + } + Set<SortingMeasurement> notChangedSortingMeasurements = new HashSet<SortingMeasurement>(); + if (target.getSortingMeasurements() != null) { + notChangedSortingMeasurements.addAll(target.getSortingMeasurements()); + } + + // Some mandatory properties : + target.setQualityFlag(load(QualityFlagImpl.class, enumeration.QUALITY_FLAG_CODE_NOT_QUALIFIED)); + target.setRootBatch(rootBatch); + target.setParentBatch(parentBatch); + target.setExhaustiveInventory(true); + + // No taxon or taxon group + target.setReferenceTaxon(null); + target.setTaxonGroup(null); + + // Sorting measurement + if (copyIfNull && (sortingPmfmId == null || sortingQualitativeValueId == null)) { + // Nothing to do : will be removed later, using notChangedSortingMeasurements + } else if (sortingPmfmId != null && sortingQualitativeValueId != null) { + SortingMeasurement sm = measurementHelper.setSortingMeasurement(target, recorderDepartmentId, sortingPmfmId, sortingQualitativeValueId); + notChangedSortingMeasurements.remove(sm); + } + + // Sampling Ratio + if (copyIfNull && (sampleWeight == null || weight == null)) { + target.setSamplingRatio(null); + target.setSamplingRatioText(null); + } else if (sampleWeight != null && weight != null) { + String samplingRatioText = weight + "/" + sampleWeight; + samplingRatioText = samplingRatioText.replaceAll(",", "."); + target.setSamplingRatioText(samplingRatioText); + target.setSamplingRatio(weight / sampleWeight); + } + + // Weight + if (copyIfNull && (sampleWeight == null && weight == null)) { + // Nothing to do : will be removed later, using notChangedQuantificationMeasurements + } else if (sampleWeight != null || weight != null) { + Float batchReferenceWeight = weight; + if (batchReferenceWeight == null) { + batchReferenceWeight = sampleWeight; + } + QuantificationMeasurement quantificationMeasurement = measurementHelper.setQuantificationMeasurement(target, + enumeration.PMFM_ID_WEIGHT_MEASURED, recorderDepartmentId, batchReferenceWeight, true); + notChangedQuantificationMeasurements.remove(quantificationMeasurement); + } + + // Removed not changed measurements (in sorting and quantification measurement lists) + if (target.getQuantificationMeasurements() != null && notChangedQuantificationMeasurements.size() > 0) { + for (QuantificationMeasurement qm : notChangedQuantificationMeasurements) { + target.getQuantificationMeasurements().remove(qm); + } + } + if (target.getSortingMeasurements() != null && notChangedSortingMeasurements.size() > 0) { + for (SortingMeasurement sm : notChangedSortingMeasurements) { + target.getSortingMeasurements().remove(sm); + } + } + } + + protected void beanToEntityReferenceTaxonBatch( + fr.ifremer.adagio.core.dao.data.batch.CatchBatch rootBatch, + fr.ifremer.adagio.core.dao.data.batch.Batch parentBatch, + fr.ifremer.adagio.core.dao.data.batch.SortingBatch target, + Integer recorderDepartmentId, + Integer referenceTaxonId, + Float weight, + Float sampleWeight, + boolean copyIfNull) { + + // Create lists to store all updates, then remove not updated items + Set<QuantificationMeasurement> notChangedQuantificationMeasurements = new HashSet<QuantificationMeasurement>(); + if (target.getQuantificationMeasurements() != null) { + notChangedQuantificationMeasurements.addAll(target.getQuantificationMeasurements()); + } + Set<SortingMeasurement> notChangedSortingMeasurements = new HashSet<SortingMeasurement>(); + if (target.getSortingMeasurements() != null) { + notChangedSortingMeasurements.addAll(target.getSortingMeasurements()); + } + + // Some mandatory properties : + target.setQualityFlag(load(QualityFlagImpl.class, enumeration.QUALITY_FLAG_CODE_NOT_QUALIFIED)); + target.setRootBatch(rootBatch); + target.setParentBatch(parentBatch); + target.setExhaustiveInventory(true); + + // Reference taxon + target.setReferenceTaxon(load(ReferenceTaxonImpl.class, referenceTaxonId)); + target.setTaxonGroup(null); + + // Sampling Ratio + if (copyIfNull && (sampleWeight == null || weight == null)) { + target.setSamplingRatio(null); + target.setSamplingRatioText(null); + } else if (sampleWeight != null && weight != null) { + String samplingRatioText = weight + "/" + sampleWeight; + samplingRatioText = samplingRatioText.replaceAll(",", "."); + target.setSamplingRatioText(samplingRatioText); + target.setSamplingRatio(weight / sampleWeight); + } + + // Weight + if (copyIfNull && (sampleWeight == null && weight == null)) { + // Nothing to do : will be removed later, using notChangedQuantificationMeasurements + } else if (sampleWeight != null || weight != null) { + Float batchReferenceWeight = weight; + if (batchReferenceWeight == null) { + batchReferenceWeight = sampleWeight; + } + QuantificationMeasurement quantificationMeasurement = measurementHelper.setQuantificationMeasurement(target, + enumeration.PMFM_ID_WEIGHT_MEASURED, recorderDepartmentId, batchReferenceWeight, true); + notChangedQuantificationMeasurements.remove(quantificationMeasurement); + } + + // Removed not changed measurements (in sorting and quantification measurement lists) + if (target.getQuantificationMeasurements() != null && notChangedQuantificationMeasurements.size() > 0) { + for (QuantificationMeasurement qm : notChangedQuantificationMeasurements) { + target.getQuantificationMeasurements().remove(qm); + } + } + if (target.getSortingMeasurements() != null && notChangedSortingMeasurements.size() > 0) { + for (SortingMeasurement sm : notChangedSortingMeasurements) { + target.getSortingMeasurements().remove(sm); + } + } + } + + protected Integer getRecorderDepartmentId() { + // TODO BL : voir si on peut récupérer le departement (du 1er saisisseur ?) + return enumeration.DEPARTMENT_ID_UNKNOWN_RECORDER_DEPARTMENT; + } +} \ No newline at end of file Property changes on: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/BatchPersistenceServiceImpl.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/synchro/ReferentialSynchronizeHelper.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/synchro/ReferentialSynchronizeHelper.java 2013-02-14 15:27:16 UTC (rev 413) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/synchro/ReferentialSynchronizeHelper.java 2013-02-14 23:53:34 UTC (rev 414) @@ -27,6 +27,7 @@ import com.google.common.base.Predicate; import com.google.common.collect.Lists; import com.google.common.collect.Sets; +import fr.ifremer.tutti.persistence.ProgressionModel; import fr.ifremer.tutti.persistence.entities.TuttiEntities; import org.apache.commons.lang3.time.DateUtils; import org.apache.commons.logging.Log; @@ -83,26 +84,32 @@ * @param localProperties properties to connect to local database * @param remoteProperties properties to connect to remove database * @param dialect dialect used to seek metadata of databases. - * @return Result of the synchronize operation, if there is an error then - * the {@link ReferentialSynchronizeResult#isSuccess()} is {@code false} - * and you can get the error at - * {@link ReferentialSynchronizeResult#getError()} + * @param result Result of the synchronize operation, if there is an error then + * the {@link ReferentialSynchronizeResult#isSuccess()} is {@code false} + * and you can get the error at + * {@link ReferentialSynchronizeResult#getError()} */ - public ReferentialSynchronizeResult synchronize(Properties localProperties, - Properties remoteProperties, - Dialect dialect) { + public void prepare(Properties localProperties, + Properties remoteProperties, + Dialect dialect, + ReferentialSynchronizeResult result) { - ReferentialSynchronizeResult result = new ReferentialSynchronizeResult( - TuttiEntities.getUrl(localProperties), - TuttiEntities.getUrl(remoteProperties)); + result.setLocalUrl(TuttiEntities.getUrl(localProperties)); + result.setRemoteUrl(TuttiEntities.getUrl(remoteProperties)); + Connection localConnection = null; Connection remoteConnection = null; try { + ProgressionModel progressionModel = result.getProgressionModel(); + progressionModel.setMessage("Connection à la base de synchronisation"); + // create local connection localConnection = TuttiEntities.createConnection(localProperties); + progressionModel.setMessage("Connection à la base à synchroniser"); + // create remote Connection remoteConnection = TuttiEntities.createConnection(remoteProperties); @@ -112,6 +119,8 @@ TuttiDatabaseMetadata remoteMeta = loadDatabaseMetadata(remoteConnection, dialect); + progressionModel.setMessage("Vérification de la compatibilité des schémas"); + // check schema try { checkSchemas(localMeta, remoteMeta); @@ -127,6 +136,8 @@ String tableName = tuttiTable.name(); + progressionModel.setMessage("Lecture de la table " + tableName); + TuttiTableMetadata table = remoteMeta.getTable(tableName); if (log.isDebugEnabled()) { @@ -142,40 +153,100 @@ if (log.isInfoEnabled()) { log.info("Total rows to update: " + totalRows); } + localConnection.rollback(); + } + } catch (SQLException e) { + try { + if (localConnection != null) { + localConnection.rollback(); + } + } catch (SQLException e1) { - // prepare target (desactivate constraints) - prepareSynch(localConnection); + // ignore the rolback error + } + result.setError(e); + } finally { + JdbcUtils.closeConnection(remoteConnection); + JdbcUtils.closeConnection(localConnection); + } + } - try { - for (TuttiTable tuttiTable : TuttiTable.values()) { + /** + * Synchronize database given from {@code localProperties} (means the + * database to update) with remote database (means the database which + * contains the referential to use) given by {@code remoteProperties} + * using the given {@code dialect} to inspect databases metadatas. + * + * @param localProperties properties to connect to local database + * @param remoteProperties properties to connect to remove database + * @param dialect dialect used to seek metadata of databases. + * @return Result of the synchronize operation, if there is an error then + * the {@link ReferentialSynchronizeResult#isSuccess()} is {@code false} + * and you can get the error at + * {@link ReferentialSynchronizeResult#getError()} + */ + public ReferentialSynchronizeResult synchronize(Properties localProperties, + Properties remoteProperties, + Dialect dialect, + ReferentialSynchronizeResult result) { - String tableName = tuttiTable.name(); - TuttiTableMetadata table = remoteMeta.getTable(tableName); + Connection localConnection = null; + Connection remoteConnection = null; + try { - if (log.isInfoEnabled()) { - log.info("Synchronize table: " + tableName); - } - synchronizeTable(table, - localConnection, - remoteConnection, - result); - } + // create local connection + localConnection = TuttiEntities.createConnection(localProperties); + + // create remote Connection + remoteConnection = TuttiEntities.createConnection(remoteProperties); + + // load metas + TuttiDatabaseMetadata remoteMeta = + loadDatabaseMetadata(remoteConnection, dialect); + + // set total in progression model + ProgressionModel progressionModel = result.getProgressionModel(); + progressionModel.setTotal(result.getTotalRows()); + + // prepare target (desactivate constraints) + prepareSynch(localConnection); + + try { + + for (TuttiTable tuttiTable : TuttiTable.values()) { + + String tableName = tuttiTable.name(); + + progressionModel.setMessage("Mise à jour de la table " + tableName); + + TuttiTableMetadata table = remoteMeta.getTable(tableName); + if (log.isInfoEnabled()) { - long totalInserts = result.getTotalInserts(); - long totalUpdates = result.getTotalUpdates(); - log.info("Total rows to treat: " + totalRows); - log.info("Total rows inserted: " + totalInserts); - log.info("Total rows updated: " + totalUpdates); - log.info("Total rows treated: " + (totalInserts + totalUpdates)); + log.info("Synchronize table: " + tableName); } - } finally { - releaseSynch(localConnection); + synchronizeTable(table, + localConnection, + remoteConnection, + result); } + if (log.isInfoEnabled()) { + long totalInserts = result.getTotalInserts(); + long totalUpdates = result.getTotalUpdates(); + log.info("Total rows to treat: " + result.getTotalRows()); + log.info("Total rows inserted: " + totalInserts); + log.info("Total rows updated: " + totalUpdates); + log.info("Total rows treated: " + (totalInserts + totalUpdates)); + } + } finally { + releaseSynch(localConnection); + } - localConnection.commit(); - } + progressionModel.setMessage("Sauvegarde des données dans la base locale"); + + localConnection.commit(); + } catch (SQLException e) { try { if (localConnection != null) { @@ -446,6 +517,10 @@ countR++; + if (countR % 1000 == 0) { + result.getProgressionModel().increments(1000); + } + if (insertCount > 0 && insertCount % 1000 == 0) { insertStatement.executeBatch(); insertStatement.clearBatch(); @@ -480,6 +555,8 @@ log.debug(String.format("%s INSERT count: %s", tablePrefix, result.getNbInserts(tableName))); log.debug(String.format("%s UPDATE count: %s", tablePrefix, result.getNbUpdates(tableName))); } + + result.getProgressionModel().increments(countR % 1000); } protected String getTablePrefix(TuttiTableMetadata table) { @@ -494,6 +571,8 @@ String tableName = table.getName(); + result.getProgressionModel().setMessage("Mise à jour de la table " + tableName); + String tablePrefix = getTablePrefix(table); // get last updateDate used by local db Modified: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/synchro/ReferentialSynchronizeResult.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/synchro/ReferentialSynchronizeResult.java 2013-02-14 15:27:16 UTC (rev 413) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/synchro/ReferentialSynchronizeResult.java 2013-02-14 23:53:34 UTC (rev 414) @@ -27,6 +27,7 @@ import com.google.common.collect.ImmutableSet; import com.google.common.collect.Maps; import com.google.common.collect.Sets; +import fr.ifremer.tutti.persistence.ProgressionModel; import java.sql.Timestamp; import java.util.Map; @@ -77,15 +78,28 @@ */ protected final Set<String> tableNames = Sets.newHashSet(); - protected final String localUrl; + protected String localUrl; - protected final String remoteUrl; + protected String remoteUrl; + protected final ProgressionModel progressionModel = new ProgressionModel(); + + public ReferentialSynchronizeResult() { + } + public ReferentialSynchronizeResult(String localUrl, String remoteUrl) { this.localUrl = localUrl; this.remoteUrl = remoteUrl; } + public void setLocalUrl(String localUrl) { + this.localUrl = localUrl; + } + + public void setRemoteUrl(String remoteUrl) { + this.remoteUrl = remoteUrl; + } + public boolean isSuccess() { return error == null; } @@ -98,28 +112,32 @@ this.error = error; } + public ProgressionModel getProgressionModel() { + return progressionModel; + } + public Set<String> getTableNames() { return ImmutableSet.copyOf(tableNames); } - public long getTotalRows() { - long result = 0; + public int getTotalRows() { + int result = 0; for (Integer nb : rowHits.values()) { result += nb; } return result; } - public long getTotalInserts() { - long result = 0; + public int getTotalInserts() { + int result = 0; for (Integer nb : insertHits.values()) { result += nb; } return result; } - public long getTotalUpdates() { - long result = 0; + public int getTotalUpdates() { + int result = 0; for (Integer nb : updateHits.values()) { result += nb; } Modified: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/synchro/ReferentialSynchronizeService.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/synchro/ReferentialSynchronizeService.java 2013-02-14 15:27:16 UTC (rev 413) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/synchro/ReferentialSynchronizeService.java 2013-02-14 23:53:34 UTC (rev 414) @@ -55,13 +55,27 @@ Dialect getLocalDialect(); /** + * Prepare the synchronize operation from the local data database supported + * by this service, says just compute nb rows to update for each table and + * update the result model. + * + * @param remoteConnectionProperties connection properties of the remote + * database used to synchronize referential + * @param result result of the operation + * @see ReferentialSynchronizeResult + */ + void prepare(Properties remoteConnectionProperties, + ReferentialSynchronizeResult result); + + /** * Launch the synchronize operation from the local data database supported * by this service. * * @param remoteConnectionProperties connection properties of the remote * database used to synchronize referential - * @return result of the operation + * @param result model * @see ReferentialSynchronizeResult */ - ReferentialSynchronizeResult synchronize(Properties remoteConnectionProperties); + void synchronize(Properties remoteConnectionProperties, + ReferentialSynchronizeResult result); } Modified: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/synchro/ReferentialSynchronizeServiceImpl.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/synchro/ReferentialSynchronizeServiceImpl.java 2013-02-14 15:27:16 UTC (rev 413) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/synchro/ReferentialSynchronizeServiceImpl.java 2013-02-14 23:53:34 UTC (rev 414) @@ -24,6 +24,7 @@ * #L% */ +import com.google.common.base.Preconditions; import fr.ifremer.tutti.persistence.entities.TuttiEntities; import fr.ifremer.tutti.persistence.service.AbstractPersistenceService; import org.apache.commons.dbcp.BasicDataSource; @@ -75,10 +76,26 @@ } @Override - public ReferentialSynchronizeResult synchronize(Properties remoteConnectionProperties) { + public void prepare(Properties remoteConnectionProperties, ReferentialSynchronizeResult result) { + Preconditions.checkNotNull(result); + Preconditions.checkNotNull(remoteConnectionProperties); + helper.prepare(getLocalConnectionProperties(), + remoteConnectionProperties, + getLocalDialect(), + result); + } + + @Override + public void synchronize(Properties remoteConnectionProperties, ReferentialSynchronizeResult result) { //TODO Evict all caches, restart application (but not from here,...) - return helper.synchronize(getLocalConnectionProperties(), - remoteConnectionProperties, - getLocalDialect()); + + Preconditions.checkNotNull(result); + Preconditions.checkNotNull(remoteConnectionProperties); + Preconditions.checkState(result.isSuccess()); + + helper.synchronize(getLocalConnectionProperties(), + remoteConnectionProperties, + getLocalDialect(), + result); } } Modified: trunk/tutti-persistence/src/main/resources/tutti-db-enumerations.properties =================================================================== --- trunk/tutti-persistence/src/main/resources/tutti-db-enumerations.properties 2013-02-14 15:27:16 UTC (rev 413) +++ trunk/tutti-persistence/src/main/resources/tutti-db-enumerations.properties 2013-02-14 23:53:34 UTC (rev 414) @@ -380,5 +380,5 @@ #Reference taxon correspondant à "Biota" (ou "Vie") ReferenceTaxonId.LIFE=7637 #Reference taxon correspondant à "Non Biota" (ou "Non Vie" - matière inerte) -# TODO : a creer dans la base harmonie et a�changer ! +# TODO : a creer dans la base harmonie et achanger ! ReferenceTaxonId.INERT=1 \ No newline at end of file Modified: trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/DatabaseResource.java =================================================================== --- trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/DatabaseResource.java 2013-02-14 15:27:16 UTC (rev 413) +++ trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/DatabaseResource.java 2013-02-14 23:53:34 UTC (rev 414) @@ -270,7 +270,7 @@ if (p != null) { TuttiEntities.fillConnectionProperties( p, - "jdbc:hsqldb:file:" + target.getAbsolutePath() + "/" + config.getDbName(), + TuttiEntities.getJdbcUrl(target, config.getDbName()), config.getJdbcUsername(), config.getJdbcPassword()); } @@ -377,7 +377,7 @@ log.info("Create new db at " + directory); } addToDestroy(directory); - String jdbcUrl = "jdbc:hsqldb:file:" + directory.getAbsolutePath() + "/" + dbName; + String jdbcUrl = TuttiEntities.getJdbcUrl(directory, dbName); String user = "SA"; String password = ""; Modified: trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/BatchPersistenceServiceReadTest.java =================================================================== --- trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/BatchPersistenceServiceReadTest.java 2013-02-14 15:27:16 UTC (rev 413) +++ trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/BatchPersistenceServiceReadTest.java 2013-02-14 23:53:34 UTC (rev 414) @@ -1,187 +1,187 @@ -package fr.ifremer.tutti.persistence.service; - -/* - * #%L - * Tutti :: Persistence API - * $Id: CatchBatchPersistenceServiceReadTest.java 379 2013-02-10 13:41:29Z tchemit $ - * $HeadURL: http://svn.forge.codelutin.com/svn/tutti/trunk/tutti-persistence/src/test/ja... $ - * %% - * Copyright (C) 2012 - 2013 Ifremer - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/gpl-3.0.html>. - * #L% - */ - -import static org.junit.Assert.*; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - -import java.util.List; -import java.util.Map; - -import fr.ifremer.tutti.persistence.DatabaseResource; -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.SampleCategoryEnum; -import fr.ifremer.tutti.persistence.entities.data.SpeciesBatch; -import fr.ifremer.tutti.persistence.entities.data.SpeciesBatchFrequency; -import fr.ifremer.tutti.persistence.entities.referential.CaracteristicQualitativeValue; - -import org.junit.Before; -import org.junit.ClassRule; -import org.junit.Ignore; -import org.junit.Test; -import org.springframework.dao.DataRetrievalFailureException; - -import com.google.common.collect.Maps; - -/** - * To test {@link CatchBatchPersistenceService} for read operation. - * - * @author tchemit <chemit@codelutin.com> - * @since 1.0 - */ -public class BatchPersistenceServiceReadTest { - - @ClassRule - public static final DatabaseResource dbResource = DatabaseResource.readDb(); - - protected BatchPersistenceService service; - protected FishingOperationPersistenceService fishingOperationService; - protected FishingOperation fishingOperation; - - @Before - public void setUp() throws Exception { - service = TuttiPersistenceServiceLocator.getBatchPersistenceService(); - fishingOperationService = TuttiPersistenceServiceLocator.getFishingOperationPersistenceService(); - - List<FishingOperation> fishingOperations = fishingOperationService.getAllFishingOperation(dbResource.getFixtures().cruiseId()); - assertNotNull(fishingOperations); - assertTrue(fishingOperations.size() > 0); - fishingOperation = fishingOperations.get(0); - assertNotNull(fishingOperation); - assertNotNull(fishingOperation.getId()); - } - - @Test - public void getCatchBatchFromFishingOperation() throws Exception { - - try { - CatchBatch catchBatch = service.getCatchBatchFromFishingOperation(fishingOperation.getId()); - fail("batch tree must be invalid (old CGFS tree structure), for fishing operation id=" + fishingOperation.getId()); - } - catch(DataRetrievalFailureException drfe) { - // success - } - } - - @Test - public void getAllRootSpeciesBatch(/*String fishingOperationId*/) { - } - - @Test - public void getAllSpeciesBatchFrequency(/*String speciesBatchId*/) { - - } - - public static void assertSpeciesBatch(SpeciesBatch expectedBatch, SpeciesBatch actualBatch, boolean assertIdEquals) { - assertNotNull(actualBatch); - assertNotNull(actualBatch.getId()); - if (assertIdEquals && expectedBatch.getId() != null) { - assertEquals(expectedBatch.getId(), actualBatch.getId()); - } - assertEquals(expectedBatch.getWeight(), actualBatch.getWeight()); - assertEquals(expectedBatch.getSampleCategoryType(), actualBatch.getSampleCategoryType()); - if (expectedBatch.getSampleCategoryValue() != null && expectedBatch.getSampleCategoryValue() instanceof CaracteristicQualitativeValue) { - assertNotNull("Bad sampleCategoryValue : expected <" + ((CaracteristicQualitativeValue) expectedBatch.getSampleCategoryValue()).getId() + "> but was <null>", - actualBatch.getSampleCategoryValue()); - assertEquals( - ((CaracteristicQualitativeValue) expectedBatch.getSampleCategoryValue()).getId(), - ((CaracteristicQualitativeValue) actualBatch.getSampleCategoryValue()).getId()); - } else { - assertEquals(expectedBatch.getSampleCategoryValue(), actualBatch.getSampleCategoryValue()); - } - assertEquals(expectedBatch.getSampleCategoryWeight(), actualBatch.getSampleCategoryWeight()); - assertEquals(expectedBatch.getNumber(), actualBatch.getNumber()); - assertEquals(expectedBatch.getComment(), actualBatch.getComment()); - - // Check species only if Vrac/HorsVrac or if batch has been load throw getAllxxx method - // (Because getSpeciesBatch(id) could not always retrieve the species) - if (expectedBatch.getSpecies() != null && ( - expectedBatch.getSampleCategoryType() == SampleCategoryEnum.sortedUnsorted - || actualBatch.getSpecies() != null)) { - assertNotNull(actualBatch.getSpecies()); - assertEquals(expectedBatch.getSpecies().getId(), actualBatch.getSpecies().getId()); - } - } - - public static 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, 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, 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); - assertNotNull(speciesBatchFrequency.getId()); - } - - for (Float lengthStep : expectedLengthMap.keySet()) { - SpeciesBatchFrequency expectedBatchFrequency = expectedLengthMap.get(lengthStep); - SpeciesBatchFrequency actualBatchFrequency = actualLengthMap.get(lengthStep); - if (assertIdEquals) { - assertEquals(expectedBatchFrequency.getId(), actualBatchFrequency.getId()); - } - assertNotNull(expectedBatchFrequency.getLengthStepCaracteristic()); - assertEquals(expectedBatchFrequency.getLengthStepCaracteristic().getId(), actualBatchFrequency.getLengthStepCaracteristic().getId()); - assertEquals(expectedBatchFrequency.getNumber(), actualBatchFrequency.getNumber()); - assertEquals(expectedBatchFrequency.getWeight(), actualBatchFrequency.getWeight()); - assertEquals(expectedBatchFrequency.getComment(), actualBatchFrequency.getComment()); - //assertNotNull(expectedBatchFrequency.getBatch()); - //assertEquals(expectedBatchFrequency.getBatch().getId(), actualBatchFrequency.getBatch().getId()); - } - } - - public static SpeciesBatch getSpeciesBatch(String fishingOperationId, String speciesBatchId) { - return getSpeciesBatch(speciesBatchId, TuttiPersistenceServiceLocator.getBatchPersistenceService().getAllRootSpeciesBatch(fishingOperationId)); - } - - public static SpeciesBatch getSpeciesBatch(String speciesBatchId, List<SpeciesBatch> speciesBatchs) { - if (speciesBatchs == null) { - return null; - } - for (SpeciesBatch speciesBatch : speciesBatchs) { - if (speciesBatchId.equals(speciesBatch.getId())) { - return speciesBatch; - } - if (speciesBatch.getChildBatchs() != null) { - speciesBatch = getSpeciesBatch(speciesBatchId, speciesBatch.getChildBatchs()); - if (speciesBatch != null) { - return speciesBatch; - } - } - } - return null; - } - -} +package fr.ifremer.tutti.persistence.service; + +/* + * #%L + * Tutti :: Persistence API + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2012 - 2013 Ifremer + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/gpl-3.0.html>. + * #L% + */ + +import static org.junit.Assert.*; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.util.List; +import java.util.Map; + +import fr.ifremer.tutti.persistence.DatabaseResource; +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.SampleCategoryEnum; +import fr.ifremer.tutti.persistence.entities.data.SpeciesBatch; +import fr.ifremer.tutti.persistence.entities.data.SpeciesBatchFrequency; +import fr.ifremer.tutti.persistence.entities.referential.CaracteristicQualitativeValue; + +import org.junit.Before; +import org.junit.ClassRule; +import org.junit.Ignore; +import org.junit.Test; +import org.springframework.dao.DataRetrievalFailureException; + +import com.google.common.collect.Maps; + +/** + * To test {@link CatchBatchPersistenceService} for read operation. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.0 + */ +public class BatchPersistenceServiceReadTest { + + @ClassRule + public static final DatabaseResource dbResource = DatabaseResource.readDb(); + + protected BatchPersistenceService service; + protected FishingOperationPersistenceService fishingOperationService; + protected FishingOperation fishingOperation; + + @Before + public void setUp() throws Exception { + service = TuttiPersistenceServiceLocator.getBatchPersistenceService(); + fishingOperationService = TuttiPersistenceServiceLocator.getFishingOperationPersistenceService(); + + List<FishingOperation> fishingOperations = fishingOperationService.getAllFishingOperation(dbResource.getFixtures().cruiseId()); + assertNotNull(fishingOperations); + assertTrue(fishingOperations.size() > 0); + fishingOperation = fishingOperations.get(0); + assertNotNull(fishingOperation); + assertNotNull(fishingOperation.getId()); + } + + @Test + public void getCatchBatchFromFishingOperation() throws Exception { + + try { + CatchBatch catchBatch = service.getCatchBatchFromFishingOperation(fishingOperation.getId()); + fail("batch tree must be invalid (old CGFS tree structure), for fishing operation id=" + fishingOperation.getId()); + } + catch(DataRetrievalFailureException drfe) { + // success + } + } + + @Test + public void getAllRootSpeciesBatch(/*String fishingOperationId*/) { + } + + @Test + public void getAllSpeciesBatchFrequency(/*String speciesBatchId*/) { + + } + + public static void assertSpeciesBatch(SpeciesBatch expectedBatch, SpeciesBatch actualBatch, boolean assertIdEquals) { + assertNotNull(actualBatch); + assertNotNull(actualBatch.getId()); + if (assertIdEquals && expectedBatch.getId() != null) { + assertEquals(expectedBatch.getId(), actualBatch.getId()); + } + assertEquals(expectedBatch.getWeight(), actualBatch.getWeight()); + assertEquals(expectedBatch.getSampleCategoryType(), actualBatch.getSampleCategoryType()); + if (expectedBatch.getSampleCategoryValue() != null && expectedBatch.getSampleCategoryValue() instanceof CaracteristicQualitativeValue) { + assertNotNull("Bad sampleCategoryValue : expected <" + ((CaracteristicQualitativeValue) expectedBatch.getSampleCategoryValue()).getId() + "> but was <null>", + actualBatch.getSampleCategoryValue()); + assertEquals( + ((CaracteristicQualitativeValue) expectedBatch.getSampleCategoryValue()).getId(), + ((CaracteristicQualitativeValue) actualBatch.getSampleCategoryValue()).getId()); + } else { + assertEquals(expectedBatch.getSampleCategoryValue(), actualBatch.getSampleCategoryValue()); + } + assertEquals(expectedBatch.getSampleCategoryWeight(), actualBatch.getSampleCategoryWeight()); + assertEquals(expectedBatch.getNumber(), actualBatch.getNumber()); + assertEquals(expectedBatch.getComment(), actualBatch.getComment()); + + // Check species only if Vrac/HorsVrac or if batch has been load throw getAllxxx method + // (Because getSpeciesBatch(id) could not always retrieve the species) + if (expectedBatch.getSpecies() != null && ( + expectedBatch.getSampleCategoryType() == SampleCategoryEnum.sortedUnsorted + || actualBatch.getSpecies() != null)) { + assertNotNull(actualBatch.getSpecies()); + assertEquals(expectedBatch.getSpecies().getId(), actualBatch.getSpecies().getId()); + } + } + + public static 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, 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, 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); + assertNotNull(speciesBatchFrequency.getId()); + } + + for (Float lengthStep : expectedLengthMap.keySet()) { + SpeciesBatchFrequency expectedBatchFrequency = expectedLengthMap.get(lengthStep); + SpeciesBatchFrequency actualBatchFrequency = actualLengthMap.get(lengthStep); + if (assertIdEquals) { + assertEquals(expectedBatchFrequency.getId(), actualBatchFrequency.getId()); + } + assertNotNull(expectedBatchFrequency.getLengthStepCaracteristic()); + assertEquals(expectedBatchFrequency.getLengthStepCaracteristic().getId(), actualBatchFrequency.getLengthStepCaracteristic().getId()); + assertEquals(expectedBatchFrequency.getNumber(), actualBatchFrequency.getNumber()); + assertEquals(expectedBatchFrequency.getWeight(), actualBatchFrequency.getWeight()); + assertEquals(expectedBatchFrequency.getComment(), actualBatchFrequency.getComment()); + //assertNotNull(expectedBatchFrequency.getBatch()); + //assertEquals(expectedBatchFrequency.getBatch().getId(), actualBatchFrequency.getBatch().getId()); + } + } + + public static SpeciesBatch getSpeciesBatch(String fishingOperationId, String speciesBatchId) { + return getSpeciesBatch(speciesBatchId, TuttiPersistenceServiceLocator.getBatchPersistenceService().getAllRootSpeciesBatch(fishingOperationId)); + } + + public static SpeciesBatch getSpeciesBatch(String speciesBatchId, List<SpeciesBatch> speciesBatchs) { + if (speciesBatchs == null) { + return null; + } + for (SpeciesBatch speciesBatch : speciesBatchs) { + if (speciesBatchId.equals(speciesBatch.getId())) { + return speciesBatch; + } + if (speciesBatch.getChildBatchs() != null) { + speciesBatch = getSpeciesBatch(speciesBatchId, speciesBatch.getChildBatchs()); + if (speciesBatch != null) { + return speciesBatch; + } + } + } + return null; + } + +} Property changes on: trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/BatchPersistenceServiceReadTest.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/BatchPersistenceServiceWriteTest.java =================================================================== --- trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/BatchPersistenceServiceWriteTest.java 2013-02-14 15:27:16 UTC (rev 413) +++ trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/BatchPersistenceServiceWriteTest.java 2013-02-14 23:53:34 UTC (rev 414) @@ -1,621 +1,621 @@ -package fr.ifremer.tutti.persistence.service; - -/* - * #%L - * Tutti :: Persistence API - * $Id: CatchBatchPersistenceServiceWriteTest.java 379 2013-02-10 13:41:29Z tchemit $ - * $HeadURL: http://svn.forge.codelutin.com/svn/tutti/trunk/tutti-persistence/src/test/ja... $ - * %% - * Copyright (C) 2012 - 2013 Ifremer - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/gpl-3.0.html>. - * #L% - */ - -import fr.ifremer.tutti.persistence.DatabaseResource; -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.SampleCategoryEnum; -import fr.ifremer.tutti.persistence.entities.data.SpeciesBatch; -import fr.ifremer.tutti.persistence.entities.data.SpeciesBatchFrequency; -import fr.ifremer.tutti.persistence.entities.referential.Caracteristic; -import fr.ifremer.tutti.persistence.entities.referential.CaracteristicQualitativeValue; -import fr.ifremer.tutti.persistence.entities.referential.CaracteristicType; -import fr.ifremer.tutti.persistence.entities.referential.Species; -import org.junit.Before; -import org.junit.ClassRule; -import org.junit.Ignore; -import org.junit.Test; -import org.springframework.dao.DataRetrievalFailureException; - -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; - -import java.util.Calendar; -import java.util.Date; -import java.util.GregorianCalendar; -import java.util.List; -import java.util.Map; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.*; - -/** - * To test {@link CatchBatchPersistenceService} for write operation. - * - * @author tchemit <chemit@codelutin.com> - * @since 1.0 - */ -public class BatchPersistenceServiceWriteTest { - - @ClassRule - public static final DatabaseResource dbResource = DatabaseResource.writeDb(); - - protected BatchPersistenceService service; - - protected CruisePersistenceService cruiseService; - - protected FishingOperationPersistenceService fishingOperationService; - - protected CatchBatchPersistenceService catchBatchService; - - protected ReferentialPersistenceService referentialService; - - protected Cruise cruise; - - protected FishingOperation fishingOperationNoCatchBatch; - - protected FishingOperation fishingOperationWithEmptyBatch; - - protected CatchBatch catchBacth; - - protected List<Species> species; - - protected Caracteristic sortedUnsortedPMFM; - - protected CaracteristicQualitativeValue horsVracQualitativeValue; - - protected CaracteristicQualitativeValue vracQualitativeValue; - - protected Caracteristic maturityPMFM; - - protected CaracteristicQualitativeValue firstMaturityQualitativeValue; - - protected Caracteristic sexPMFM; - - protected CaracteristicQualitativeValue maleQualitativeValue; - - protected CaracteristicQualitativeValue femaleQualitativeValue; - - protected CaracteristicQualitativeValue unkQualitativeValue; - - protected Caracteristic frequencyPMFM; - - @Before - public void setUp() throws Exception { - service = TuttiPersistenceServiceLocator.getBatchPersistenceService(); - cruiseService = TuttiPersistenceServiceLocator.getCruisePersistenceService(); - fishingOperationService = TuttiPersistenceServiceLocator.getFishingOperationPersistenceService(); - catchBatchService = TuttiPersistenceServiceLocator.getCatchBatchPersistenceService(); - referentialService = TuttiPersistenceServiceLocator.getReferentialPersistenceService(); - - species = referentialService.getAllSpecies(); - assertNotNull(species); - assertTrue(species.size() > 2); - - cruise = cruiseService.getCruise(dbResource.getFixtures().cruiseId()); - cruise.setId(null); - Calendar calendar = new GregorianCalendar(); - cruise.setBeginDate(calendar.getTime()); - cruise.setYear(calendar.get(Calendar.YEAR)); - calendar.add(Calendar.MONTH, 1); // add one month - cruise.setEndDate(calendar.getTime()); - cruise = cruiseService.createCruise(cruise); - - // Create a first operation, with no cacth batch : to test CatchBatch insert/update : - List<FishingOperation> fishingOperations = fishingOperationService.getAllFishingOperation(dbResource.getFixtures().cruiseId()); - assertNotNull(fishingOperations); - assertTrue(fishingOperations.size() > 0); - fishingOperationNoCatchBatch = fishingOperations.get(0); - fishingOperationNoCatchBatch = fishingOperationService.getFishingOperation(fishingOperationNoCatchBatch.getId()); - fishingOperationNoCatchBatch.setId(null); - fishingOperationNoCatchBatch.setCruise(cruise); - calendar.setTime(new Date()); - calendar.set(Calendar.HOUR_OF_DAY, 1); - calendar.set(Calendar.MILLISECOND, 0); - fishingOperationNoCatchBatch.setGearShootingStartDate(calendar.getTime()); - calendar.setTime(new Date()); - calendar.set(Calendar.HOUR_OF_DAY, 10); - calendar.set(Calendar.MILLISECOND, 0); - fishingOperationNoCatchBatch.setGearShootingEndDate(calendar.getTime()); - fishingOperationNoCatchBatch = fishingOperationService.createFishingOperation(fishingOperationNoCatchBatch); - - // Create a second operation, with no cacth batch : to test CatchBatch insert/update : - fishingOperationWithEmptyBatch = fishingOperations.get(1); - fishingOperationWithEmptyBatch = fishingOperationService.getFishingOperation(fishingOperationWithEmptyBatch.getId()); - fishingOperationWithEmptyBatch.setId(null); - fishingOperationWithEmptyBatch.setCruise(cruise); - calendar.setTime(new Date()); - calendar.set(Calendar.HOUR_OF_DAY, 11); - calendar.set(Calendar.MILLISECOND, 0); - fishingOperationWithEmptyBatch.setGearShootingStartDate(calendar.getTime()); - calendar.setTime(new Date()); - calendar.set(Calendar.HOUR_OF_DAY, 12); - calendar.set(Calendar.MILLISECOND, 0); - fishingOperationWithEmptyBatch.setGearShootingEndDate(calendar.getTime()); - fishingOperationWithEmptyBatch = fishingOperationService.createFishingOperation(fishingOperationWithEmptyBatch); - - catchBacth = new CatchBatch(); - catchBacth.setFishingOperation(fishingOperationWithEmptyBatch); - catchBacth = catchBatchService.createCatchBatch(catchBacth); - - sortedUnsortedPMFM = referentialService.getSortedUnsortedCaracteristic(); - horsVracQualitativeValue = sortedUnsortedPMFM.getQualitativeValue(0); - vracQualitativeValue = sortedUnsortedPMFM.getQualitativeValue(1); - maturityPMFM = referentialService.getMaturityCaracteristic(); - firstMaturityQualitativeValue = maturityPMFM.getQualitativeValue(0); - sexPMFM = referentialService.getSexCaracteristic(); - maleQualitativeValue = sexPMFM.getQualitativeValue(1); - femaleQualitativeValue = sexPMFM.getQualitativeValue(2); - unkQualitativeValue = sexPMFM.getQualitativeValue(3); - - List<Caracteristic> cara = referentialService.getAllCaracteristic(); - for (Caracteristic caracteristic : cara) { - if (caracteristic.getCaracteristicType() == CaracteristicType.NUMBER - && caracteristic.getPrecision() != null - && caracteristic.getPrecision() == 0.5f) { - frequencyPMFM = caracteristic; - break; - } - } - assertNotNull("no numerical PMFM with a precision has been found. Could not define a PMFM for batch frequencies.", frequencyPMFM); - - } - - @Test - public void createAndSaveCatchBatch() throws Exception { - CatchBatch catchBatch; - - catchBatch = new CatchBatch(); - catchBatch.setFishingOperation(fishingOperationNoCatchBatch); - - // ----------------------------------------------------------------------------- - // 1. Test with only mandatory properties - // ----------------------------------------------------------------------------- - - // Create and reload (test round trip) - assertCreateAndReloadCatchBatch(catchBatch, fishingOperationNoCatchBatch.getId()); - - // ----------------------------------------------------------------------------- - // 2. Test with all properties - // ----------------------------------------------------------------------------- - catchBatch.setId(null); - // total weight : 100kg - catchBatch.setCatchTotalWeight(75f); - // Vrac : - { - // note : poids trie par la balance tremis (thalassa) (init par pupitri) - catchBatch.setCatchTotalSortedTremisWeight(50f); - // note : poids vrac caroussel (thalassa) (init par pupitri) (vrac trie) ou bien "poids trié fournie par la table de tri (Sum(Si) - catchBatch.setCatchTotalSortedCarousselWeight(45f); - - // Species - { - catchBatch.setSpeciesTotalSortedWeight(12f); - catchBatch.setSpeciesTotalSampleSortedWeight(8f); - catchBatch.setSpeciesTotalLivingNotItemizedWeight(0.2f); - catchBatch.setSpeciesTotalInertWeight(0.1f); - } - } - // Hors Vrac : 10kg - { - catchBatch.setCatchTotalUnsortedWeight(10f); - - // Species - { - catchBatch.setSpeciesTotalUnsortedWeight(10f); - } - } - // Rejet : 15kg - catchBatch.setCatchTotalRejectedWeight(15f); - - // Create and reload (test round trip) - assertCreateAndReloadCatchBatch(catchBatch, fishingOperationNoCatchBatch.getId()); - - // ----------------------------------------------------------------------------- - // 2. Test save after modification - // ----------------------------------------------------------------------------- - catchBatch.setCatchTotalSortedTremisWeight(null); - catchBatch.setCatchTotalSortedCarousselWeight(null); - catchBatch.setSpeciesTotalSortedWeight(null); - catchBatch.setSpeciesTotalSampleSortedWeight(null); - catchBatch.setCatchTotalUnsortedWeight(null); - catchBatch.setSpeciesTotalUnsortedWeight(null); - - assertSaveAndReloadCatchBatch(catchBatch, fishingOperationNoCatchBatch.getId()); - } - - @Test - public void createAndSaveSpeciesBatchAndFrequencies() { - SpeciesBatch esp1Batch = null; - SpeciesBatch esp2Batch = null; - SpeciesBatch frequenciesParentBatch = null; - SpeciesBatch batch = null; - Species taxon1 = species.get(0); - Species taxon2 = species.get(1); - - // ----------------------------------------------------------------------------- - // 1. Test with only mandatory properties - // ----------------------------------------------------------------------------- - // batch : "ESP1 - Vrac/5" - batch = new SpeciesBatch(); - batch.setParentBatch(null); - batch.setFishingOperation(fishingOperationNoCatchBatch); - batch.setSpecies(taxon1); - batch.setSampleCategoryType(SampleCategoryEnum.sortedUnsorted); - batch.setSampleCategoryValue(vracQualitativeValue); - batch.setSampleCategoryWeight(5f); - - assertCreateAndReloadSpeciesBatch(batch, null); - - // Save ESP1 batch - esp1Batch = batch; - - // ----------------------------------------------------------------------------- - // 2. Test child "Male/2" - // ----------------------------------------------------------------------------- - // Batch : ESP1 - Vrac/5 Male/2 ss-ech/1 Nombre/7 - batch = new SpeciesBatch(); - batch.setId(null); - batch.setParentBatch(esp1Batch); - batch.setSpecies(taxon1); - batch.setComment("ESP1 - Vrac/5 Male/2 ss-ech/1 Nombre/7"); - batch.setSampleCategoryType(SampleCategoryEnum.sex); - batch.setSampleCategoryValue(maleQualitativeValue); - batch.setSampleCategoryWeight(2f); - batch.setWeight(1f); - batch.setNumber(7); - - assertCreateAndReloadSpeciesBatch(batch, batch.getParentBatch().getId()); - - // ----------------------------------------------------------------------------- - // 3. Test child "Female/2" - // ----------------------------------------------------------------------------- - // Batch : ESP1 - Vrac/5 Female/3 Nombre/14 - batch = new SpeciesBatch(); - batch.setId(null); - batch.setParentBatch(esp1Batch); - batch.setSpecies(taxon1); - batch.setComment("ESP1 - Vrac/5 Female/3 Nombre/14"); - batch.setSampleCategoryType(SampleCategoryEnum.sex); - batch.setSampleCategoryValue(femaleQualitativeValue); - batch.setSampleCategoryWeight(3f); - batch.setWeight(null); - batch.setNumber(14); - - assertCreateAndReloadSpeciesBatch(batch, batch.getParentBatch().getId()); - - // ----------------------------------------------------------------------------- - // 4. Test : ESP2 - Vrac/7 - // \- ESP2 - Vrac/7 UNK/2 ss-ech/1 Nombre/11 - // ----------------------------------------------------------------------------- - // batch : "ESP2 - Vrac/7 " - batch = new SpeciesBatch(); - batch.setParentBatch(null); - batch.setSpecies(taxon2); - batch.setSampleCategoryType(SampleCategoryEnum.sortedUnsorted); - batch.setSampleCategoryValue(vracQualitativeValue); - batch.setSampleCategoryWeight(7f); - - assertCreateAndReloadSpeciesBatch(batch, null); - esp2Batch = batch; - - // Batch : ESP2 - Vrac/7 UNK/2 ss-ech/1 Nombre/11 - batch = new SpeciesBatch(); - batch.setId(null); - batch.setParentBatch(esp2Batch); - batch.setSpecies(taxon2); - batch.setComment("ESP2 - Vrac/7 UNK/2 ss-ech/1 Nombre/11"); - batch.setSampleCategoryType(SampleCategoryEnum.maturity); - batch.setSampleCategoryValue(firstMaturityQualitativeValue); - batch.setSampleCategoryWeight(2f); - batch.setWeight(1f); - batch.setNumber(11); - - assertCreateAndReloadSpeciesBatch(batch, batch.getParentBatch().getId()); - - // ----------------------------------------------------------------------------- - // 5. Test save after modifications - // ----------------------------------------------------------------------------- - // Batch : ESP2 - Vrac/7 UNK/1.75 ss-ech/1.11 Nombre/99 - batch.setComment("ESP2 - Vrac/7 UNK/1.75 ss-ech/1.11 Nombre/99"); - batch.setSampleCategoryType(SampleCategoryEnum.sex); - batch.setSampleCategoryValue(unkQualitativeValue); - batch.setSampleCategoryWeight(1.75f); - batch.setWeight(1.11f); - batch.setFishingOperation(fishingOperationWithEmptyBatch); - batch.setNumber(99); - - // Save and reload, then check - SpeciesBatch savedBatch = service.saveSpeciesBatch(batch); - assertSpeciesBatch(savedBatch, batch, false); - SpeciesBatch reloadedBatch = getSpeciesBatch(fishingOperationWithEmptyBatch.getId(), savedBatch.getId()); - assertSpeciesBatch(savedBatch, reloadedBatch, true); - - // Save batch for later - frequenciesParentBatch = batch; - - // ----------------------------------------------------------------------------- - // 6. Test change species - // ----------------------------------------------------------------------------- - esp2Batch.setSpecies(taxon1); - service.changeSpeciesBatchSpecies(esp2Batch.getId(), taxon1); - assertSpeciesBatch(savedBatch, batch, false); - reloadedBatch = getSpeciesBatch(fishingOperationWithEmptyBatch.getId(), esp2Batch.getId()); - assertSpeciesBatch(esp2Batch, reloadedBatch, true); - - // ----------------------------------------------------------------------------- - // 7. Test get all root species - // ----------------------------------------------------------------------------- - List<SpeciesBatch> rootSpeciesBatch = service.getAllRootSpeciesBatch(fishingOperationWithEmptyBatch.getId()); - assertNotNull(rootSpeciesBatch); - assertEquals(2, rootSpeciesBatch.size()); - assertNotNull(rootSpeciesBatch.get(0).getChildBatchs()); - assertTrue(rootSpeciesBatch.get(0).getChildBatchs().size() > 0); - assertNotNull(rootSpeciesBatch.get(1).getChildBatchs()); - assertTrue(rootSpeciesBatch.get(1).getChildBatchs().size() > 0); - - // ----------------------------------------------------------------------------- - // 8. Test batch frequency creation - // ----------------------------------------------------------------------------- - - List<SpeciesBatchFrequency> frequencies = Lists.newArrayList(); - float lengthStep = 0.5f; - for (float length = lengthStep; length < lengthStep * 20; length += lengthStep) { - SpeciesBatchFrequency frequency = new SpeciesBatchFrequency(); - frequency.setLengthStep(length); - frequency.setNumber((int) (length * 2)); - frequency.setWeight(0.01f * length * 2); - frequency.setLengthStepCaracteristic(frequencyPMFM); - frequency.setBatch(frequenciesParentBatch); - frequency.setComment("comments"); - frequencies.add(frequency); - } - List<SpeciesBatchFrequency> createdFrequencies = assertCreateAndReloadSpeciesBatchFrequency(frequencies, frequenciesParentBatch.getId()); - - // ----------------------------------------------------------------------------- - // 9. Test batch frequency update - // ----------------------------------------------------------------------------- - // Update some batchs (1cm, 2cm, etc) - for (SpeciesBatchFrequency speciesBatchFrequency : createdFrequencies) { - float length = speciesBatchFrequency.getLengthStep(); - if ((float) (int) length == length) { - speciesBatchFrequency.setNumber(12); - speciesBatchFrequency.setComment(null); - } - } - // And remove the last item (should be deleted in DB) - createdFrequencies.remove(createdFrequencies.size()-1); - List<SpeciesBatchFrequency> savedFrequencies = service.saveSpeciesBatchFrequency(frequenciesParentBatch.getId(), frequencies); - assertBatchFrequencies(createdFrequencies, savedFrequencies, true); - } - - @Test - public void deleteSpeciesBatch(/*String id*/) { - 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 = new SpeciesBatch(); - batch.setParentBatch(null); - batch.setFishingOperation(fishingOperationWithEmptyBatch); - batch.setSpecies(taxon1); - batch.setSampleCategoryType(SampleCategoryEnum.sortedUnsorted); - batch.setSampleCategoryValue(vracQualitativeValue); - batch.setSampleCategoryWeight(5f); - assertCreateAndReloadSpeciesBatch(batch, null); - esp1Batch = batch; - - // batch : ESP1 Vrac/5 Male/2 - batch = new SpeciesBatch(); - batch.setParentBatch(esp1Batch); - batch.setFishingOperation(fishingOperationWithEmptyBatch); - batch.setSpecies(taxon1); - batch.setSampleCategoryType(SampleCategoryEnum.sex); - batch.setSampleCategoryValue(maleQualitativeValue); - batch.setSampleCategoryWeight(2f); - assertCreateAndReloadSpeciesBatch(batch, esp1Batch.getId()); - - // Try to remove - service.deleteSpeciesBatch(esp1Batch.getId()); - - // Check if remove - try { - batch = getSpeciesBatch(fishingOperationWithEmptyBatch.getId(), esp1Batch.getId()); - assertNull(batch); - } catch (DataRetrievalFailureException drfe) { - assertNotNull(drfe); - } - } - - protected void assertCreateAndReloadSpeciesBatch(SpeciesBatch batch, String parentBatchId) { - batch.setFishingOperation(fishingOperationWithEmptyBatch); - - // Create batch - SpeciesBatch createdBatch = service.createSpeciesBatch(batch, parentBatchId); - assertSpeciesBatch(batch, createdBatch, false); - - // then reload (for round trip check) - SpeciesBatch reloadedBatch = getSpeciesBatch(fishingOperationWithEmptyBatch.getId(), createdBatch.getId()); - if (parentBatchId == null) { - assertNull(reloadedBatch.getParentBatch()); - } else { - assertNotNull(reloadedBatch.getParentBatch()); - assertEquals(parentBatchId, reloadedBatch.getParentBatch().getId()); - } - assertSpeciesBatch(createdBatch, reloadedBatch, false); - - batch.setId(createdBatch.getId()); - } - - protected void assertCreateAndReloadCatchBatch(CatchBatch catchBatch, String fishingOperationId) { - CatchBatch createdCatchBatch = service.createCatchBatch(catchBatch); - assertNotNull(createdCatchBatch); - assertNotNull(createdCatchBatch.getId()); - assertCatchBatch(catchBatch, createdCatchBatch, false); - - CatchBatch reloadedCatchBatch = service.getCatchBatchFromFishingOperation(fishingOperationId); - assertCatchBatch(createdCatchBatch, reloadedCatchBatch, true); - - catchBatch.setId(createdCatchBatch.getId()); - } - - protected void assertSaveAndReloadCatchBatch(CatchBatch catchBatch, String fishingOperationId) { - CatchBatch savedCatchBatch = service.saveCatchBatch(catchBatch); - assertNotNull(savedCatchBatch); - assertNotNull(savedCatchBatch.getId()); - assertCatchBatch(catchBatch, savedCatchBatch, false); - - CatchBatch reloadedCatchBatch = service.getCatchBatchFromFishingOperation(fishingOperationId); - assertCatchBatch(savedCatchBatch, reloadedCatchBatch, true); - } - - protected void assertCatchBatch(CatchBatch expectedCatchBatch,CatchBatch actualCatchBatch, boolean assertIdEquals) { - if (expectedCatchBatch == null) { - assertNull(actualCatchBatch); - return; - } - - assertNotNull(actualCatchBatch); - if (assertIdEquals) { - assertEquals(expectedCatchBatch.getId(), actualCatchBatch.getId()); - } - assertEquals(expectedCatchBatch.getCatchTotalWeight(), actualCatchBatch.getCatchTotalWeight()); - assertEquals(expectedCatchBatch.getCatchTotalSortedCarousselWeight(), actualCatchBatch.getCatchTotalSortedCarousselWeight()); - assertEquals(expectedCatchBatch.getCatchTotalSortedTremisWeight(), actualCatchBatch.getCatchTotalSortedTremisWeight()); - assertEquals(expectedCatchBatch.getCatchTotalUnsortedWeight(), actualCatchBatch.getCatchTotalUnsortedWeight()); - - assertEquals(expectedCatchBatch.getSpeciesTotalSampleSortedWeight(), actualCatchBatch.getSpeciesTotalSampleSortedWeight()); - assertEquals(expectedCatchBatch.getSpeciesTotalSortedWeight(), actualCatchBatch.getSpeciesTotalSortedWeight()); - assertEquals(expectedCatchBatch.getSpeciesTotalUnsortedWeight(), actualCatchBatch.getSpeciesTotalUnsortedWeight()); - assertEquals(expectedCatchBatch.getSpeciesTotalInertWeight(), actualCatchBatch.getSpeciesTotalInertWeight()); - assertEquals(expectedCatchBatch.getSpeciesTotalLivingNotItemizedWeight(), actualCatchBatch.getSpeciesTotalLivingNotItemizedWeight()); - } - - protected void assertSpeciesBatch(SpeciesBatch expectedBatch, SpeciesBatch actualBatch, boolean assertIdEquals) { - assertNotNull(actualBatch); - assertNotNull(actualBatch.getId()); - if (assertIdEquals && expectedBatch.getId() != null) { - assertEquals(expectedBatch.getId(), actualBatch.getId()); - } - assertEquals(expectedBatch.getWeight(), actualBatch.getWeight()); - assertEquals(expectedBatch.getSampleCategoryType(), actualBatch.getSampleCategoryType()); - if (expectedBatch.getSampleCategoryValue() != null && expectedBatch.getSampleCategoryValue() instanceof CaracteristicQualitativeValue) { - assertNotNull("Bad sampleCategoryValue : expected <" + ((CaracteristicQualitativeValue) expectedBatch.getSampleCategoryValue()).getId() + "> but was <null>", - actualBatch.getSampleCategoryValue()); - assertEquals( - ((CaracteristicQualitativeValue) expectedBatch.getSampleCategoryValue()).getId(), - ((CaracteristicQualitativeValue) actualBatch.getSampleCategoryValue()).getId()); - } else { - assertEquals(expectedBatch.getSampleCategoryValue(), actualBatch.getSampleCategoryValue()); - } - assertEquals(expectedBatch.getSampleCategoryWeight(), actualBatch.getSampleCategoryWeight()); - assertEquals(expectedBatch.getNumber(), actualBatch.getNumber()); - assertEquals(expectedBatch.getComment(), actualBatch.getComment()); - - // Check species only if Vrac/HorsVrac or if batch has been load throw getAllxxx method - // (Because getSpeciesBatch(id) could not always retrieve the species) - if (expectedBatch.getSpecies() != null && ( - expectedBatch.getSampleCategoryType() == SampleCategoryEnum.sortedUnsorted - || actualBatch.getSpecies() != null)) { - assertNotNull(actualBatch.getSpecies()); - assertEquals(expectedBatch.getSpecies().getId(), actualBatch.getSpecies().getId()); - } - } - protected List<SpeciesBatchFrequency> assertCreateAndReloadSpeciesBatchFrequency(List<SpeciesBatchFrequency> frequencies, String parentBatchId) { - - // Create batch - List<SpeciesBatchFrequency> createdFrequencies = service.saveSpeciesBatchFrequency(parentBatchId, frequencies); - assertBatchFrequencies(frequencies, createdFrequencies, false); - - // then reload (for round trip check) - List<SpeciesBatchFrequency> reloadedFrequencies = service.getAllSpeciesBatchFrequency(parentBatchId); - assertBatchFrequencies(createdFrequencies, reloadedFrequencies, true); - - return createdFrequencies; - } - - 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, 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, 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); - assertNotNull(speciesBatchFrequency.getId()); - } - - for (Float lengthStep : expectedLengthMap.keySet()) { - SpeciesBatchFrequency expectedBatchFrequency = expectedLengthMap.get(lengthStep); - SpeciesBatchFrequency actualBatchFrequency = actualLengthMap.get(lengthStep); - if (assertIdEquals) { - assertEquals(expectedBatchFrequency.getId(), actualBatchFrequency.getId()); - } - assertNotNull(expectedBatchFrequency.getLengthStepCaracteristic()); - assertEquals(expectedBatchFrequency.getLengthStepCaracteristic().getId(), actualBatchFrequency.getLengthStepCaracteristic().getId()); - assertEquals(expectedBatchFrequency.getNumber(), actualBatchFrequency.getNumber()); - assertEquals(expectedBatchFrequency.getWeight(), actualBatchFrequency.getWeight()); - assertEquals(expectedBatchFrequency.getComment(), actualBatchFrequency.getComment()); - //assertNotNull(expectedBatchFrequency.getBatch()); - //assertEquals(expectedBatchFrequency.getBatch().getId(), actualBatchFrequency.getBatch().getId()); - } - } - - protected SpeciesBatch getSpeciesBatch(String fishingOperationId, String speciesBatchId) { - return getSpeciesBatch(speciesBatchId, service.getAllRootSpeciesBatch(fishingOperationId)); - } - - protected SpeciesBatch getSpeciesBatch(String speciesBatchId, List<SpeciesBatch> speciesBatchs) { - if (speciesBatchs == null) { - return null; - } - for (SpeciesBatch speciesBatch : speciesBatchs) { - if (speciesBatchId.equals(speciesBatch.getId())) { - return speciesBatch; - } - if (speciesBatch.getChildBatchs() != null) { - speciesBatch = getSpeciesBatch(speciesBatchId, speciesBatch.getChildBatchs()); - if (speciesBatch != null) { - return speciesBatch; - } - } - } - return null; - } -} +package fr.ifremer.tutti.persistence.service; + +/* + * #%L + * Tutti :: Persistence API + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2012 - 2013 Ifremer + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/gpl-3.0.html>. + * #L% + */ + +import fr.ifremer.tutti.persistence.DatabaseResource; +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.SampleCategoryEnum; +import fr.ifremer.tutti.persistence.entities.data.SpeciesBatch; +import fr.ifremer.tutti.persistence.entities.data.SpeciesBatchFrequency; +import fr.ifremer.tutti.persistence.entities.referential.Caracteristic; +import fr.ifremer.tutti.persistence.entities.referential.CaracteristicQualitativeValue; +import fr.ifremer.tutti.persistence.entities.referential.CaracteristicType; +import fr.ifremer.tutti.persistence.entities.referential.Species; +import org.junit.Before; +import org.junit.ClassRule; +import org.junit.Ignore; +import org.junit.Test; +import org.springframework.dao.DataRetrievalFailureException; + +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; + +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.List; +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.*; + +/** + * To test {@link CatchBatchPersistenceService} for write operation. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.0 + */ +public class BatchPersistenceServiceWriteTest { + + @ClassRule + public static final DatabaseResource dbResource = DatabaseResource.writeDb(); + + protected BatchPersistenceService service; + + protected CruisePersistenceService cruiseService; + + protected FishingOperationPersistenceService fishingOperationService; + + protected CatchBatchPersistenceService catchBatchService; + + protected ReferentialPersistenceService referentialService; + + protected Cruise cruise; + + protected FishingOperation fishingOperationNoCatchBatch; + + protected FishingOperation fishingOperationWithEmptyBatch; + + protected CatchBatch catchBacth; + + protected List<Species> species; + + protected Caracteristic sortedUnsortedPMFM; + + protected CaracteristicQualitativeValue horsVracQualitativeValue; + + protected CaracteristicQualitativeValue vracQualitativeValue; + + protected Caracteristic maturityPMFM; + + protected CaracteristicQualitativeValue firstMaturityQualitativeValue; + + protected Caracteristic sexPMFM; + + protected CaracteristicQualitativeValue maleQualitativeValue; + + protected CaracteristicQualitativeValue femaleQualitativeValue; + + protected CaracteristicQualitativeValue unkQualitativeValue; + + protected Caracteristic frequencyPMFM; + + @Before + public void setUp() throws Exception { + service = TuttiPersistenceServiceLocator.getBatchPersistenceService(); + cruiseService = TuttiPersistenceServiceLocator.getCruisePersistenceService(); + fishingOperationService = TuttiPersistenceServiceLocator.getFishingOperationPersistenceService(); + catchBatchService = TuttiPersistenceServiceLocator.getCatchBatchPersistenceService(); + referentialService = TuttiPersistenceServiceLocator.getReferentialPersistenceService(); + + species = referentialService.getAllSpecies(); + assertNotNull(species); + assertTrue(species.size() > 2); + + cruise = cruiseService.getCruise(dbResource.getFixtures().cruiseId()); + cruise.setId(null); + Calendar calendar = new GregorianCalendar(); + cruise.setBeginDate(calendar.getTime()); + cruise.setYear(calendar.get(Calendar.YEAR)); + calendar.add(Calendar.MONTH, 1); // add one month + cruise.setEndDate(calendar.getTime()); + cruise = cruiseService.createCruise(cruise); + + // Create a first operation, with no cacth batch : to test CatchBatch insert/update : + List<FishingOperation> fishingOperations = fishingOperationService.getAllFishingOperation(dbResource.getFixtures().cruiseId()); + assertNotNull(fishingOperations); + assertTrue(fishingOperations.size() > 0); + fishingOperationNoCatchBatch = fishingOperations.get(0); + fishingOperationNoCatchBatch = fishingOperationService.getFishingOperation(fishingOperationNoCatchBatch.getId()); + fishingOperationNoCatchBatch.setId(null); + fishingOperationNoCatchBatch.setCruise(cruise); + calendar.setTime(new Date()); + calendar.set(Calendar.HOUR_OF_DAY, 1); + calendar.set(Calendar.MILLISECOND, 0); + fishingOperationNoCatchBatch.setGearShootingStartDate(calendar.getTime()); + calendar.setTime(new Date()); + calendar.set(Calendar.HOUR_OF_DAY, 10); + calendar.set(Calendar.MILLISECOND, 0); + fishingOperationNoCatchBatch.setGearShootingEndDate(calendar.getTime()); + fishingOperationNoCatchBatch = fishingOperationService.createFishingOperation(fishingOperationNoCatchBatch); + + // Create a second operation, with no cacth batch : to test CatchBatch insert/update : + fishingOperationWithEmptyBatch = fishingOperations.get(1); + fishingOperationWithEmptyBatch = fishingOperationService.getFishingOperation(fishingOperationWithEmptyBatch.getId()); + fishingOperationWithEmptyBatch.setId(null); + fishingOperationWithEmptyBatch.setCruise(cruise); + calendar.setTime(new Date()); + calendar.set(Calendar.HOUR_OF_DAY, 11); + calendar.set(Calendar.MILLISECOND, 0); + fishingOperationWithEmptyBatch.setGearShootingStartDate(calendar.getTime()); + calendar.setTime(new Date()); + calendar.set(Calendar.HOUR_OF_DAY, 12); + calendar.set(Calendar.MILLISECOND, 0); + fishingOperationWithEmptyBatch.setGearShootingEndDate(calendar.getTime()); + fishingOperationWithEmptyBatch = fishingOperationService.createFishingOperation(fishingOperationWithEmptyBatch); + + catchBacth = new CatchBatch(); + catchBacth.setFishingOperation(fishingOperationWithEmptyBatch); + catchBacth = catchBatchService.createCatchBatch(catchBacth); + + sortedUnsortedPMFM = referentialService.getSortedUnsortedCaracteristic(); + horsVracQualitativeValue = sortedUnsortedPMFM.getQualitativeValue(0); + vracQualitativeValue = sortedUnsortedPMFM.getQualitativeValue(1); + maturityPMFM = referentialService.getMaturityCaracteristic(); + firstMaturityQualitativeValue = maturityPMFM.getQualitativeValue(0); + sexPMFM = referentialService.getSexCaracteristic(); + maleQualitativeValue = sexPMFM.getQualitativeValue(1); + femaleQualitativeValue = sexPMFM.getQualitativeValue(2); + unkQualitativeValue = sexPMFM.getQualitativeValue(3); + + List<Caracteristic> cara = referentialService.getAllCaracteristic(); + for (Caracteristic caracteristic : cara) { + if (caracteristic.getCaracteristicType() == CaracteristicType.NUMBER + && caracteristic.getPrecision() != null + && caracteristic.getPrecision() == 0.5f) { + frequencyPMFM = caracteristic; + break; + } + } + assertNotNull("no numerical PMFM with a precision has been found. Could not define a PMFM for batch frequencies.", frequencyPMFM); + + } + + @Test + public void createAndSaveCatchBatch() throws Exception { + CatchBatch catchBatch; + + catchBatch = new CatchBatch(); + catchBatch.setFishingOperation(fishingOperationNoCatchBatch); + + // ----------------------------------------------------------------------------- + // 1. Test with only mandatory properties + // ----------------------------------------------------------------------------- + + // Create and reload (test round trip) + assertCreateAndReloadCatchBatch(catchBatch, fishingOperationNoCatchBatch.getId()); + + // ----------------------------------------------------------------------------- + // 2. Test with all properties + // ----------------------------------------------------------------------------- + catchBatch.setId(null); + // total weight : 100kg + catchBatch.setCatchTotalWeight(75f); + // Vrac : + { + // note : poids trie par la balance tremis (thalassa) (init par pupitri) + catchBatch.setCatchTotalSortedTremisWeight(50f); + // note : poids vrac caroussel (thalassa) (init par pupitri) (vrac trie) ou bien "poids trié fournie par la table de tri (Sum(Si) + catchBatch.setCatchTotalSortedCarousselWeight(45f); + + // Species + { + catchBatch.setSpeciesTotalSortedWeight(12f); + catchBatch.setSpeciesTotalSampleSortedWeight(8f); + catchBatch.setSpeciesTotalLivingNotItemizedWeight(0.2f); + catchBatch.setSpeciesTotalInertWeight(0.1f); + } + } + // Hors Vrac : 10kg + { + catchBatch.setCatchTotalUnsortedWeight(10f); + + // Species + { + catchBatch.setSpeciesTotalUnsortedWeight(10f); + } + } + // Rejet : 15kg + catchBatch.setCatchTotalRejectedWeight(15f); + + // Create and reload (test round trip) + assertCreateAndReloadCatchBatch(catchBatch, fishingOperationNoCatchBatch.getId()); + + // ----------------------------------------------------------------------------- + // 2. Test save after modification + // ----------------------------------------------------------------------------- + catchBatch.setCatchTotalSortedTremisWeight(null); + catchBatch.setCatchTotalSortedCarousselWeight(null); + catchBatch.setSpeciesTotalSortedWeight(null); + catchBatch.setSpeciesTotalSampleSortedWeight(null); + catchBatch.setCatchTotalUnsortedWeight(null); + catchBatch.setSpeciesTotalUnsortedWeight(null); + + assertSaveAndReloadCatchBatch(catchBatch, fishingOperationNoCatchBatch.getId()); + } + + @Test + public void createAndSaveSpeciesBatchAndFrequencies() { + SpeciesBatch esp1Batch = null; + SpeciesBatch esp2Batch = null; + SpeciesBatch frequenciesParentBatch = null; + SpeciesBatch batch = null; + Species taxon1 = species.get(0); + Species taxon2 = species.get(1); + + // ----------------------------------------------------------------------------- + // 1. Test with only mandatory properties + // ----------------------------------------------------------------------------- + // batch : "ESP1 - Vrac/5" + batch = new SpeciesBatch(); + batch.setParentBatch(null); + batch.setFishingOperation(fishingOperationNoCatchBatch); + batch.setSpecies(taxon1); + batch.setSampleCategoryType(SampleCategoryEnum.sortedUnsorted); + batch.setSampleCategoryValue(vracQualitativeValue); + batch.setSampleCategoryWeight(5f); + + assertCreateAndReloadSpeciesBatch(batch, null); + + // Save ESP1 batch + esp1Batch = batch; + + // ----------------------------------------------------------------------------- + // 2. Test child "Male/2" + // ----------------------------------------------------------------------------- + // Batch : ESP1 - Vrac/5 Male/2 ss-ech/1 Nombre/7 + batch = new SpeciesBatch(); + batch.setId(null); + batch.setParentBatch(esp1Batch); + batch.setSpecies(taxon1); + batch.setComment("ESP1 - Vrac/5 Male/2 ss-ech/1 Nombre/7"); + batch.setSampleCategoryType(SampleCategoryEnum.sex); + batch.setSampleCategoryValue(maleQualitativeValue); + batch.setSampleCategoryWeight(2f); + batch.setWeight(1f); + batch.setNumber(7); + + assertCreateAndReloadSpeciesBatch(batch, batch.getParentBatch().getId()); + + // ----------------------------------------------------------------------------- + // 3. Test child "Female/2" + // ----------------------------------------------------------------------------- + // Batch : ESP1 - Vrac/5 Female/3 Nombre/14 + batch = new SpeciesBatch(); + batch.setId(null); + batch.setParentBatch(esp1Batch); + batch.setSpecies(taxon1); + batch.setComment("ESP1 - Vrac/5 Female/3 Nombre/14"); + batch.setSampleCategoryType(SampleCategoryEnum.sex); + batch.setSampleCategoryValue(femaleQualitativeValue); + batch.setSampleCategoryWeight(3f); + batch.setWeight(null); + batch.setNumber(14); + + assertCreateAndReloadSpeciesBatch(batch, batch.getParentBatch().getId()); + + // ----------------------------------------------------------------------------- + // 4. Test : ESP2 - Vrac/7 + // \- ESP2 - Vrac/7 UNK/2 ss-ech/1 Nombre/11 + // ----------------------------------------------------------------------------- + // batch : "ESP2 - Vrac/7 " + batch = new SpeciesBatch(); + batch.setParentBatch(null); + batch.setSpecies(taxon2); + batch.setSampleCategoryType(SampleCategoryEnum.sortedUnsorted); + batch.setSampleCategoryValue(vracQualitativeValue); + batch.setSampleCategoryWeight(7f); + + assertCreateAndReloadSpeciesBatch(batch, null); + esp2Batch = batch; + + // Batch : ESP2 - Vrac/7 UNK/2 ss-ech/1 Nombre/11 + batch = new SpeciesBatch(); + batch.setId(null); + batch.setParentBatch(esp2Batch); + batch.setSpecies(taxon2); + batch.setComment("ESP2 - Vrac/7 UNK/2 ss-ech/1 Nombre/11"); + batch.setSampleCategoryType(SampleCategoryEnum.maturity); + batch.setSampleCategoryValue(firstMaturityQualitativeValue); + batch.setSampleCategoryWeight(2f); + batch.setWeight(1f); + batch.setNumber(11); + + assertCreateAndReloadSpeciesBatch(batch, batch.getParentBatch().getId()); + + // ----------------------------------------------------------------------------- + // 5. Test save after modifications + // ----------------------------------------------------------------------------- + // Batch : ESP2 - Vrac/7 UNK/1.75 ss-ech/1.11 Nombre/99 + batch.setComment("ESP2 - Vrac/7 UNK/1.75 ss-ech/1.11 Nombre/99"); + batch.setSampleCategoryType(SampleCategoryEnum.sex); + batch.setSampleCategoryValue(unkQualitativeValue); + batch.setSampleCategoryWeight(1.75f); + batch.setWeight(1.11f); + batch.setFishingOperation(fishingOperationWithEmptyBatch); + batch.setNumber(99); + + // Save and reload, then check + SpeciesBatch savedBatch = service.saveSpeciesBatch(batch); + assertSpeciesBatch(savedBatch, batch, false); + SpeciesBatch reloadedBatch = getSpeciesBatch(fishingOperationWithEmptyBatch.getId(), savedBatch.getId()); + assertSpeciesBatch(savedBatch, reloadedBatch, true); + + // Save batch for later + frequenciesParentBatch = batch; + + // ----------------------------------------------------------------------------- + // 6. Test change species + // ----------------------------------------------------------------------------- + esp2Batch.setSpecies(taxon1); + service.changeSpeciesBatchSpecies(esp2Batch.getId(), taxon1); + assertSpeciesBatch(savedBatch, batch, false); + reloadedBatch = getSpeciesBatch(fishingOperationWithEmptyBatch.getId(), esp2Batch.getId()); + assertSpeciesBatch(esp2Batch, reloadedBatch, true); + + // ----------------------------------------------------------------------------- + // 7. Test get all root species + // ----------------------------------------------------------------------------- + List<SpeciesBatch> rootSpeciesBatch = service.getAllRootSpeciesBatch(fishingOperationWithEmptyBatch.getId()); + assertNotNull(rootSpeciesBatch); + assertEquals(2, rootSpeciesBatch.size()); + assertNotNull(rootSpeciesBatch.get(0).getChildBatchs()); + assertTrue(rootSpeciesBatch.get(0).getChildBatchs().size() > 0); + assertNotNull(rootSpeciesBatch.get(1).getChildBatchs()); + assertTrue(rootSpeciesBatch.get(1).getChildBatchs().size() > 0); + + // ----------------------------------------------------------------------------- + // 8. Test batch frequency creation + // ----------------------------------------------------------------------------- + + List<SpeciesBatchFrequency> frequencies = Lists.newArrayList(); + float lengthStep = 0.5f; + for (float length = lengthStep; length < lengthStep * 20; length += lengthStep) { + SpeciesBatchFrequency frequency = new SpeciesBatchFrequency(); + frequency.setLengthStep(length); + frequency.setNumber((int) (length * 2)); + frequency.setWeight(0.01f * length * 2); + frequency.setLengthStepCaracteristic(frequencyPMFM); + frequency.setBatch(frequenciesParentBatch); + frequency.setComment("comments"); + frequencies.add(frequency); + } + List<SpeciesBatchFrequency> createdFrequencies = assertCreateAndReloadSpeciesBatchFrequency(frequencies, frequenciesParentBatch.getId()); + + // ----------------------------------------------------------------------------- + // 9. Test batch frequency update + // ----------------------------------------------------------------------------- + // Update some batchs (1cm, 2cm, etc) + for (SpeciesBatchFrequency speciesBatchFrequency : createdFrequencies) { + float length = speciesBatchFrequency.getLengthStep(); + if ((float) (int) length == length) { + speciesBatchFrequency.setNumber(12); + speciesBatchFrequency.setComment(null); + } + } + // And remove the last item (should be deleted in DB) + createdFrequencies.remove(createdFrequencies.size()-1); + List<SpeciesBatchFrequency> savedFrequencies = service.saveSpeciesBatchFrequency(frequenciesParentBatch.getId(), frequencies); + assertBatchFrequencies(createdFrequencies, savedFrequencies, true); + } + + @Test + public void deleteSpeciesBatch(/*String id*/) { + 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 = new SpeciesBatch(); + batch.setParentBatch(null); + batch.setFishingOperation(fishingOperationWithEmptyBatch); + batch.setSpecies(taxon1); + batch.setSampleCategoryType(SampleCategoryEnum.sortedUnsorted); + batch.setSampleCategoryValue(vracQualitativeValue); + batch.setSampleCategoryWeight(5f); + assertCreateAndReloadSpeciesBatch(batch, null); + esp1Batch = batch; + + // batch : ESP1 Vrac/5 Male/2 + batch = new SpeciesBatch(); + batch.setParentBatch(esp1Batch); + batch.setFishingOperation(fishingOperationWithEmptyBatch); + batch.setSpecies(taxon1); + batch.setSampleCategoryType(SampleCategoryEnum.sex); + batch.setSampleCategoryValue(maleQualitativeValue); + batch.setSampleCategoryWeight(2f); + assertCreateAndReloadSpeciesBatch(batch, esp1Batch.getId()); + + // Try to remove + service.deleteSpeciesBatch(esp1Batch.getId()); + + // Check if remove + try { + batch = getSpeciesBatch(fishingOperationWithEmptyBatch.getId(), esp1Batch.getId()); + assertNull(batch); + } catch (DataRetrievalFailureException drfe) { + assertNotNull(drfe); + } + } + + protected void assertCreateAndReloadSpeciesBatch(SpeciesBatch batch, String parentBatchId) { + batch.setFishingOperation(fishingOperationWithEmptyBatch); + + // Create batch + SpeciesBatch createdBatch = service.createSpeciesBatch(batch, parentBatchId); + assertSpeciesBatch(batch, createdBatch, false); + + // then reload (for round trip check) + SpeciesBatch reloadedBatch = getSpeciesBatch(fishingOperationWithEmptyBatch.getId(), createdBatch.getId()); + if (parentBatchId == null) { + assertNull(reloadedBatch.getParentBatch()); + } else { + assertNotNull(reloadedBatch.getParentBatch()); + assertEquals(parentBatchId, reloadedBatch.getParentBatch().getId()); + } + assertSpeciesBatch(createdBatch, reloadedBatch, false); + + batch.setId(createdBatch.getId()); + } + + protected void assertCreateAndReloadCatchBatch(CatchBatch catchBatch, String fishingOperationId) { + CatchBatch createdCatchBatch = service.createCatchBatch(catchBatch); + assertNotNull(createdCatchBatch); + assertNotNull(createdCatchBatch.getId()); + assertCatchBatch(catchBatch, createdCatchBatch, false); + + CatchBatch reloadedCatchBatch = service.getCatchBatchFromFishingOperation(fishingOperationId); + assertCatchBatch(createdCatchBatch, reloadedCatchBatch, true); + + catchBatch.setId(createdCatchBatch.getId()); + } + + protected void assertSaveAndReloadCatchBatch(CatchBatch catchBatch, String fishingOperationId) { + CatchBatch savedCatchBatch = service.saveCatchBatch(catchBatch); + assertNotNull(savedCatchBatch); + assertNotNull(savedCatchBatch.getId()); + assertCatchBatch(catchBatch, savedCatchBatch, false); + + CatchBatch reloadedCatchBatch = service.getCatchBatchFromFishingOperation(fishingOperationId); + assertCatchBatch(savedCatchBatch, reloadedCatchBatch, true); + } + + protected void assertCatchBatch(CatchBatch expectedCatchBatch,CatchBatch actualCatchBatch, boolean assertIdEquals) { + if (expectedCatchBatch == null) { + assertNull(actualCatchBatch); + return; + } + + assertNotNull(actualCatchBatch); + if (assertIdEquals) { + assertEquals(expectedCatchBatch.getId(), actualCatchBatch.getId()); + } + assertEquals(expectedCatchBatch.getCatchTotalWeight(), actualCatchBatch.getCatchTotalWeight()); + assertEquals(expectedCatchBatch.getCatchTotalSortedCarousselWeight(), actualCatchBatch.getCatchTotalSortedCarousselWeight()); + assertEquals(expectedCatchBatch.getCatchTotalSortedTremisWeight(), actualCatchBatch.getCatchTotalSortedTremisWeight()); + assertEquals(expectedCatchBatch.getCatchTotalUnsortedWeight(), actualCatchBatch.getCatchTotalUnsortedWeight()); + + assertEquals(expectedCatchBatch.getSpeciesTotalSampleSortedWeight(), actualCatchBatch.getSpeciesTotalSampleSortedWeight()); + assertEquals(expectedCatchBatch.getSpeciesTotalSortedWeight(), actualCatchBatch.getSpeciesTotalSortedWeight()); + assertEquals(expectedCatchBatch.getSpeciesTotalUnsortedWeight(), actualCatchBatch.getSpeciesTotalUnsortedWeight()); + assertEquals(expectedCatchBatch.getSpeciesTotalInertWeight(), actualCatchBatch.getSpeciesTotalInertWeight()); + assertEquals(expectedCatchBatch.getSpeciesTotalLivingNotItemizedWeight(), actualCatchBatch.getSpeciesTotalLivingNotItemizedWeight()); + } + + protected void assertSpeciesBatch(SpeciesBatch expectedBatch, SpeciesBatch actualBatch, boolean assertIdEquals) { + assertNotNull(actualBatch); + assertNotNull(actualBatch.getId()); + if (assertIdEquals && expectedBatch.getId() != null) { + assertEquals(expectedBatch.getId(), actualBatch.getId()); + } + assertEquals(expectedBatch.getWeight(), actualBatch.getWeight()); + assertEquals(expectedBatch.getSampleCategoryType(), actualBatch.getSampleCategoryType()); + if (expectedBatch.getSampleCategoryValue() != null && expectedBatch.getSampleCategoryValue() instanceof CaracteristicQualitativeValue) { + assertNotNull("Bad sampleCategoryValue : expected <" + ((CaracteristicQualitativeValue) expectedBatch.getSampleCategoryValue()).getId() + "> but was <null>", + actualBatch.getSampleCategoryValue()); + assertEquals( + ((CaracteristicQualitativeValue) expectedBatch.getSampleCategoryValue()).getId(), + ((CaracteristicQualitativeValue) actualBatch.getSampleCategoryValue()).getId()); + } else { + assertEquals(expectedBatch.getSampleCategoryValue(), actualBatch.getSampleCategoryValue()); + } + assertEquals(expectedBatch.getSampleCategoryWeight(), actualBatch.getSampleCategoryWeight()); + assertEquals(expectedBatch.getNumber(), actualBatch.getNumber()); + assertEquals(expectedBatch.getComment(), actualBatch.getComment()); + + // Check species only if Vrac/HorsVrac or if batch has been load throw getAllxxx method + // (Because getSpeciesBatch(id) could not always retrieve the species) + if (expectedBatch.getSpecies() != null && ( + expectedBatch.getSampleCategoryType() == SampleCategoryEnum.sortedUnsorted + || actualBatch.getSpecies() != null)) { + assertNotNull(actualBatch.getSpecies()); + assertEquals(expectedBatch.getSpecies().getId(), actualBatch.getSpecies().getId()); + } + } + protected List<SpeciesBatchFrequency> assertCreateAndReloadSpeciesBatchFrequency(List<SpeciesBatchFrequency> frequencies, String parentBatchId) { + + // Create batch + List<SpeciesBatchFrequency> createdFrequencies = service.saveSpeciesBatchFrequency(parentBatchId, frequencies); + assertBatchFrequencies(frequencies, createdFrequencies, false); + + // then reload (for round trip check) + List<SpeciesBatchFrequency> reloadedFrequencies = service.getAllSpeciesBatchFrequency(parentBatchId); + assertBatchFrequencies(createdFrequencies, reloadedFrequencies, true); + + return createdFrequencies; + } + + 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, 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, 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); + assertNotNull(speciesBatchFrequency.getId()); + } + + for (Float lengthStep : expectedLengthMap.keySet()) { + SpeciesBatchFrequency expectedBatchFrequency = expectedLengthMap.get(lengthStep); + SpeciesBatchFrequency actualBatchFrequency = actualLengthMap.get(lengthStep); + if (assertIdEquals) { + assertEquals(expectedBatchFrequency.getId(), actualBatchFrequency.getId()); + } + assertNotNull(expectedBatchFrequency.getLengthStepCaracteristic()); + assertEquals(expectedBatchFrequency.getLengthStepCaracteristic().getId(), actualBatchFrequency.getLengthStepCaracteristic().getId()); + assertEquals(expectedBatchFrequency.getNumber(), actualBatchFrequency.getNumber()); + assertEquals(expectedBatchFrequency.getWeight(), actualBatchFrequency.getWeight()); + assertEquals(expectedBatchFrequency.getComment(), actualBatchFrequency.getComment()); + //assertNotNull(expectedBatchFrequency.getBatch()); + //assertEquals(expectedBatchFrequency.getBatch().getId(), actualBatchFrequency.getBatch().getId()); + } + } + + protected SpeciesBatch getSpeciesBatch(String fishingOperationId, String speciesBatchId) { + return getSpeciesBatch(speciesBatchId, service.getAllRootSpeciesBatch(fishingOperationId)); + } + + protected SpeciesBatch getSpeciesBatch(String speciesBatchId, List<SpeciesBatch> speciesBatchs) { + if (speciesBatchs == null) { + return null; + } + for (SpeciesBatch speciesBatch : speciesBatchs) { + if (speciesBatchId.equals(speciesBatch.getId())) { + return speciesBatch; + } + if (speciesBatch.getChildBatchs() != null) { + speciesBatch = getSpeciesBatch(speciesBatchId, speciesBatch.getChildBatchs()); + if (speciesBatch != null) { + return speciesBatch; + } + } + } + return null; + } +} Property changes on: trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/BatchPersistenceServiceWriteTest.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/synchro/ReferentialSynchronizeHelperTest.java =================================================================== --- trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/synchro/ReferentialSynchronizeHelperTest.java 2013-02-14 15:27:16 UTC (rev 413) +++ trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/synchro/ReferentialSynchronizeHelperTest.java 2013-02-14 23:53:34 UTC (rev 414) @@ -631,82 +631,6 @@ updateTable(TuttiTable.AGGREGATION_LEVEL, internalDb, internalConnection, externalConnection, fromDate, 1, 0); } - @Test - public void synchronize() throws IOException, SQLException { - - createInternalConnection(); - - TuttiDatabaseMetadata internalDb = - helper.loadDatabaseMetadata(internalConnection, dialect); - Assert.assertNotNull(internalDb); - - createExternalDb(); - - // synchronize from main db to empty db - - ReferentialSynchronizeResult result = - helper.synchronize(externalConnectionProperties, - localConnectionProperties, - dialect); - - // same referentiel in both db - Assert.assertNotNull(result); - Assert.assertNull(result.getError()); - - DatabaseSynchroFixtures fixturesExternal; - DatabaseSynchroFixtures fixturesInternal; - - fixturesInternal = new DatabaseSynchroFixtures( - helper, internalDb, internalConnection); - fixturesExternal = new DatabaseSynchroFixtures( - helper, internalDb, externalConnection); - - for (TuttiTable tuttiTable : TuttiTable.values()) { - String tableName = tuttiTable.name(); - int nbInserts = result.getNbInserts(tableName); - int nbUpdates = result.getNbUpdates(tableName); - int expected = fixturesInternal.count(tuttiTable); - Assert.assertEquals(String.format("Table %s Should found %s inserts, but was only %s", tableName, expected, nbInserts), expected, nbInserts, 0); - Assert.assertEquals(String.format("Table %s Should found %s updates, but was only %s", tableName, 0, nbUpdates), 0, nbUpdates, 0); - Assert.assertEquals(String.format("Table %s Should found %s rows, but was only %s", tableName, fixturesInternal.count(tuttiTable), nbUpdates), fixturesInternal.count(tuttiTable), fixturesExternal.count(tuttiTable), 0); - } - - // add a new status - internalConnection.prepareStatement("INSERT INTO STATUS(CODE, NAME) VALUES (4, 'NEW')").executeUpdate(); - - // add a new aggregation_level - internalConnection.prepareStatement("INSERT INTO AGGREGATION_LEVEL (ID, NAME, RANK_ORDER, UPDATE_DATE) VALUES (-1000, 'NAME', -1000, NULL)").executeUpdate(); - - // update a unit - PreparedStatement preparedStatement = internalConnection.prepareStatement("UPDATE UNIT SET SYMBOL = 'Auncun un?', UPDATE_DATE=? WHERE ID=1"); - preparedStatement.setDate(1, new java.sql.Date(System.currentTimeMillis())); - preparedStatement.executeUpdate(); - - internalConnection.commit(); - - fixturesInternal = new DatabaseSynchroFixtures( - helper, internalDb, internalConnection); - - result = helper.synchronize(externalConnectionProperties, - localConnectionProperties, - dialect); - Assert.assertNotNull(result); - Assert.assertNull(result.getError()); - - fixturesExternal = new DatabaseSynchroFixtures( - helper, internalDb, externalConnection); - - for (TuttiTable tuttiTable : TuttiTable.values()) { - String tableName = tuttiTable.name(); - - int internalCount = fixturesInternal.count(tuttiTable); - int externalCount = fixturesExternal.count(tuttiTable); - - Assert.assertEquals(String.format("Table %s Should found %s rows, but was only %s", tableName, internalCount, externalCount), internalCount, externalCount, 0); - } - - } - protected ResultSet getDataToUpdate(TuttiTable tuttiTable, TuttiDatabaseMetadata db, Connection connection, Modified: trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/synchro/ReferentialSynchronizeServiceImplTest.java =================================================================== --- trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/synchro/ReferentialSynchronizeServiceImplTest.java 2013-02-14 15:27:16 UTC (rev 413) +++ trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/synchro/ReferentialSynchronizeServiceImplTest.java 2013-02-14 23:53:34 UTC (rev 414) @@ -25,6 +25,7 @@ */ import fr.ifremer.tutti.persistence.DatabaseResource; +import fr.ifremer.tutti.persistence.ProgressionModel; import fr.ifremer.tutti.persistence.entities.TuttiEntities; import fr.ifremer.tutti.persistence.service.TuttiPersistenceServiceLocator; import org.hibernate.dialect.Dialect; @@ -37,6 +38,12 @@ import org.junit.rules.TestName; import org.springframework.jdbc.support.JdbcUtils; +import javax.swing.JDialog; +import javax.swing.JProgressBar; +import java.awt.Frame; +import java.awt.GraphicsEnvironment; +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; @@ -69,6 +76,8 @@ protected ReferentialSynchronizeHelper helper; + protected JDialog dialog; + @Before public void setUp() throws Exception { @@ -91,6 +100,9 @@ @After public void tearDown() throws Exception { + if (dialog != null) { + dialog.setVisible(false); + } service = null; remoteConnectionProperties = null; dialect = null; @@ -107,7 +119,6 @@ DatabaseSynchroFixtures fixturesRemote; DatabaseSynchroFixtures fixturesLocal; - ReferentialSynchronizeResult result; // add some stuff on remote db @@ -127,12 +138,69 @@ fixturesRemote = new DatabaseSynchroFixtures( helper, remoteSchema, remoteConnection); - result = service.synchronize(remoteConnectionProperties); + fixturesLocal = new DatabaseSynchroFixtures( + helper, remoteSchema, localConnection); - // same referentiel in both db + ReferentialSynchronizeResult result = + new ReferentialSynchronizeResult(); + + + dialog = createProgressBar(result); + + service.prepare(remoteConnectionProperties, result); + Assert.assertNotNull(result); Assert.assertNull(result.getError()); + Assert.assertTrue(result.isSuccess()); + // on entities, only updates in table is with no update_date + for (TuttiTable tuttiTable : TuttiTable.getEntityTables()) { + String tableName = tuttiTable.name(); + int nbLocal = fixturesLocal.count(tuttiTable); + int nbRemote = fixturesRemote.count(tuttiTable); + int nbRows = result.getNbRows(tableName); + int expectedRows = 0; + TuttiTableMetadata table = remoteSchema.getTable(tableName); + if (!table.isWithUpdateDateColumn()) { + // updates everything + expectedRows = nbRemote; + } + + switch (tuttiTable) { + case STATUS: + nbLocal++; + break; + + case UNIT: + expectedRows = 1; + break; + + case AGGREGATION_LEVEL: + nbLocal++; + expectedRows = 1; + break; + } + Assert.assertEquals(String.format("Table %s Should found %s rows, but was only %s", tableName, nbRemote, nbLocal), nbRemote, nbLocal, 0); + Assert.assertEquals(String.format("Table %s Should found %s rows to treat, but was only %s", tableName, expectedRows, nbRows), expectedRows, nbRows, 0); + } + + // on associations, only inserts + for (TuttiTable tuttiTable : TuttiTable.getAssociationTables()) { + String tableName = tuttiTable.name(); + int nbLocal = fixturesLocal.count(tuttiTable); + int nbRemote = fixturesRemote.count(tuttiTable); + int nbRows = result.getNbRows(tableName); + int expectedRows = nbRemote; + Assert.assertEquals(String.format("Table %s Should found %s rows, but was only %s", tableName, nbRemote, nbLocal), nbRemote, nbLocal, 0); + Assert.assertEquals(String.format("Table %s Should found %s rows to treat, but was only %s", tableName, expectedRows, nbRows), expectedRows, nbRows, 0); + } + + service.synchronize(remoteConnectionProperties, result); + + Assert.assertEquals(result.getTotalRows(), result.getProgressionModel().getTotal()); + Assert.assertEquals(result.getProgressionModel().getTotal(), result.getProgressionModel().getCurrent()); + Assert.assertEquals(1.0, result.getProgressionModel().getRate(), 0.01); + fixturesLocal = new DatabaseSynchroFixtures( helper, remoteSchema, localConnection); @@ -187,6 +255,45 @@ Assert.assertEquals(String.format("Table %s Should found %s updates, but was only %s", tableName, expectedUpdates, nbUpdates), expectedUpdates, nbUpdates, 0); Assert.assertEquals(String.format("Table %s Should found %s rows treated, but was only %s", tableName, expectedRows, nbInserts + nbUpdates), expectedRows, nbInserts + nbUpdates, 0); } + } + public static JDialog createProgressBar(ReferentialSynchronizeResult result) { + + JDialog dialog; + if (GraphicsEnvironment.isHeadless()) { + dialog = null; + } else { + dialog = new JDialog((Frame) null, + "synchronisation " + result.getLocalUrl() + " from " + result.getRemoteUrl(), + false); + dialog.setSize(600, 400); + final JProgressBar progressBar = new JProgressBar(); + progressBar.setStringPainted(true); + progressBar.setIndeterminate(false); + ProgressionModel progressionModel = result.getProgressionModel(); + progressionModel.addPropertyChangeListener(ProgressionModel.PROPERTY_MESSAGE, new PropertyChangeListener() { + @Override + public void propertyChange(PropertyChangeEvent evt) { + progressBar.setString((String) evt.getNewValue()); + } + }); + progressionModel.addPropertyChangeListener(ProgressionModel.PROPERTY_TOTAL, new PropertyChangeListener() { + @Override + public void propertyChange(PropertyChangeEvent evt) { + progressBar.setMinimum(0); + progressBar.setMaximum((Integer) evt.getNewValue()); + } + }); + progressionModel.addPropertyChangeListener(ProgressionModel.PROPERTY_CURRENT, new PropertyChangeListener() { + @Override + public void propertyChange(PropertyChangeEvent evt) { + progressBar.setValue((Integer) evt.getNewValue()); + + } + }); + dialog.add(progressBar); + dialog.setVisible(true); + } + return dialog; } } Modified: trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/synchro/ReferentialSynchronizeServiceImplUpTodateTest.java =================================================================== --- trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/synchro/ReferentialSynchronizeServiceImplUpTodateTest.java 2013-02-14 15:27:16 UTC (rev 413) +++ trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/synchro/ReferentialSynchronizeServiceImplUpTodateTest.java 2013-02-14 23:53:34 UTC (rev 414) @@ -37,6 +37,8 @@ import org.junit.rules.TestName; import org.springframework.jdbc.support.JdbcUtils; +import javax.swing.JDialog; +import java.awt.GraphicsEnvironment; import java.sql.Connection; import java.sql.SQLException; import java.util.Properties; @@ -67,6 +69,8 @@ protected ReferentialSynchronizeHelper helper; + protected JDialog dialog; + @Before public void setUp() throws Exception { @@ -89,6 +93,9 @@ @After public void tearDown() throws Exception { + if (dialog!=null) { + dialog.setVisible(false); + } service = null; remoteConnectionProperties = null; dialect = null; @@ -105,17 +112,53 @@ DatabaseSynchroFixtures fixturesRemote; DatabaseSynchroFixtures fixturesLocal; - ReferentialSynchronizeResult result; fixturesRemote = new DatabaseSynchroFixtures( helper, remoteSchema, remoteConnection); - result = service.synchronize(remoteConnectionProperties); + fixturesLocal = new DatabaseSynchroFixtures( + helper, remoteSchema, localConnection); - // same referentiel in both db + ReferentialSynchronizeResult result = new ReferentialSynchronizeResult(); + + dialog = ReferentialSynchronizeServiceImplTest.createProgressBar(result); + + service.prepare(remoteConnectionProperties, result); + Assert.assertNotNull(result); Assert.assertNull(result.getError()); + Assert.assertTrue(result.isSuccess()); + // on entities, only updates in table is with no update_date + for (TuttiTable tuttiTable : TuttiTable.getEntityTables()) { + String tableName = tuttiTable.name(); + int nbLocal = fixturesLocal.count(tuttiTable); + int nbRemote = fixturesRemote.count(tuttiTable); + int nbRows = result.getNbRows(tableName); + int expectedRows = 0; + TuttiTableMetadata table = remoteSchema.getTable(tableName); + if (!table.isWithUpdateDateColumn()) { + // updates everything + expectedRows = nbRemote; + } + + Assert.assertEquals(String.format("Table %s Should found %s rows, but was only %s", tableName, nbRemote, nbLocal), nbRemote, nbLocal, 0); + Assert.assertEquals(String.format("Table %s Should found %s rows to treat, but was only %s", tableName, expectedRows, nbRows), expectedRows, nbRows, 0); + } + + // on associations, only inserts + for (TuttiTable tuttiTable : TuttiTable.getAssociationTables()) { + String tableName = tuttiTable.name(); + int nbLocal = fixturesLocal.count(tuttiTable); + int nbRemote = fixturesRemote.count(tuttiTable); + int nbRows = result.getNbRows(tableName); + int expectedRows = nbRemote; + Assert.assertEquals(String.format("Table %s Should found %s rows, but was only %s", tableName, nbRemote, nbLocal), nbRemote, nbLocal, 0); + Assert.assertEquals(String.format("Table %s Should found %s rows to treat, but was only %s", tableName, expectedRows, nbRows), expectedRows, nbRows, 0); + } + + service.synchronize(remoteConnectionProperties, result); + fixturesLocal = new DatabaseSynchroFixtures( helper, remoteSchema, localConnection);
participants (1)
-
tchemit@users.forge.codelutin.com