Author: blavenier Date: 2013-01-31 16:37:38 +0100 (Thu, 31 Jan 2013) New Revision: 278 Url: http://forge.codelutin.com/projects/tutti/repository/revisions/278 Log: ref refs #1920: [Persistence] Adagio Donnees thematiques - Configure cache on getAllFishingVessels as eternal (disk persistence) - Add impl in SpeciesBatchPersistenceService : getAllRootSpeciesbatch, getAllSpeciesBatch and saveSpeciesBatch - Remove ehcache.xml : now additional cache are include into applicationContext-service-tutti.xml Removed: trunk/tutti-persistence-adagio/src/main/resources/ehcache.xml Modified: trunk/tutti-persistence-adagio/src/main/java/fr/ifremer/tutti/persistence/service/ReferentialPersistenceServiceImpl.java trunk/tutti-persistence-adagio/src/main/java/fr/ifremer/tutti/persistence/service/SpeciesBatchPersistenceServiceImpl.java trunk/tutti-persistence-adagio/src/main/java/fr/ifremer/tutti/persistence/service/TuttiPersistenceServiceLocator.java trunk/tutti-persistence-adagio/src/main/resources/applicationContext-service-tutti.xml trunk/tutti-persistence-adagio/src/main/resources/queries-override.hbm.xml trunk/tutti-persistence-adagio/src/test/java/fr/ifremer/tutti/persistence/service/ReferentialPersistenceServiceTest.java trunk/tutti-persistence-adagio/src/test/java/fr/ifremer/tutti/persistence/service/SpeciesBatchPersistenceServiceTest.java Modified: trunk/tutti-persistence-adagio/src/main/java/fr/ifremer/tutti/persistence/service/ReferentialPersistenceServiceImpl.java =================================================================== --- trunk/tutti-persistence-adagio/src/main/java/fr/ifremer/tutti/persistence/service/ReferentialPersistenceServiceImpl.java 2013-01-31 15:26:25 UTC (rev 277) +++ trunk/tutti-persistence-adagio/src/main/java/fr/ifremer/tutti/persistence/service/ReferentialPersistenceServiceImpl.java 2013-01-31 15:37:38 UTC (rev 278) @@ -213,7 +213,7 @@ } @Override - @Cacheable(value = "allFishingVessel") + @Cacheable(value = "tuttiAllFishingVessel") public List<Vessel> getAllFishingVessel() { Iterator<Object[]> list = queryListWithStatus( "allVessels", Modified: trunk/tutti-persistence-adagio/src/main/java/fr/ifremer/tutti/persistence/service/SpeciesBatchPersistenceServiceImpl.java =================================================================== --- trunk/tutti-persistence-adagio/src/main/java/fr/ifremer/tutti/persistence/service/SpeciesBatchPersistenceServiceImpl.java 2013-01-31 15:26:25 UTC (rev 277) +++ trunk/tutti-persistence-adagio/src/main/java/fr/ifremer/tutti/persistence/service/SpeciesBatchPersistenceServiceImpl.java 2013-01-31 15:37:38 UTC (rev 278) @@ -25,10 +25,15 @@ */ import java.io.Serializable; +import java.text.MessageFormat; +import java.util.ArrayList; +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 java.util.logging.SimpleFormatter; import javax.annotation.Resource; @@ -41,6 +46,7 @@ import org.springframework.stereotype.Service; import com.google.common.base.Preconditions; +import com.google.common.collect.Lists; import com.google.common.collect.Sets; import fr.ifremer.adagio.core.dao.administration.user.DepartmentImpl; @@ -51,6 +57,7 @@ import fr.ifremer.adagio.core.dao.data.batch.CatchBatchImpl; 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.SortingBatchImpl; import fr.ifremer.adagio.core.dao.data.batch.denormalized.DenormalizedBatch; import fr.ifremer.adagio.core.dao.data.batch.denormalized.DenormalizedBatchDao; import fr.ifremer.adagio.core.dao.data.batch.denormalized.DenormalizedBatchImpl; @@ -106,12 +113,71 @@ @Override public List<SpeciesBatch> getAllRootSpeciesBatch(String fishingOperationId) { - return null; + Iterator<Object[]> list = queryList("allRootSpeciesBatch", + "fishingOperationId", IntegerType.INSTANCE, Integer.valueOf(fishingOperationId), + "qualitativeIdSortingType", IntegerType.INSTANCE, enumeration.QUALITATIVE_ID_SORTING_TYPE_SPECIES); + + List<SpeciesBatch> result = new ArrayList<SpeciesBatch>(); + while (list.hasNext()) { + Object[] source = list.next(); + SpeciesBatch speciesBatch = loadSpeciesBatch(source, true); + result.add(speciesBatch); + } + + return result; } @Override public List<SpeciesBatch> getAllSpeciesBatch(String fishingOperationId) { - return null; + Iterator<Object[]> list = queryList("allSpeciesBatch", + "fishingOperationId", IntegerType.INSTANCE, Integer.valueOf(fishingOperationId)); + + List<SpeciesBatch> result = new ArrayList<SpeciesBatch>(); + List<SpeciesBatch> rootBatchs = new ArrayList<SpeciesBatch>(); + + Map<String, SpeciesBatch> batchMapById = new HashMap<String, SpeciesBatch>(); + Map<String, String> parentBatchMapById = new HashMap<String, String>(); + while (list.hasNext()) { + Object[] source = list.next(); + SpeciesBatch speciesBatch = loadSpeciesBatch(source, false); + batchMapById.put(speciesBatch.getId(), speciesBatch); + + Integer parentbatchId = (Integer)source[11]; + if (parentbatchId != null) { + parentBatchMapById.put(speciesBatch.getId(), parentbatchId.toString()); + } + } + + for (Iterator iterator = batchMapById.values().iterator(); iterator.hasNext();) { + SpeciesBatch speciesBatch = (SpeciesBatch) iterator.next(); + + // Retrieve the parent SpeciesBatch + String parentbatchId = parentBatchMapById.get(speciesBatch.getId()); + if (parentbatchId != null) { + SpeciesBatch parentSpeciesBatch = batchMapById.get(parentbatchId); + + // If found, link the batch with its parent : + if (parentSpeciesBatch != null) { + speciesBatch.setParentBatch(parentSpeciesBatch); + if (parentSpeciesBatch.getChildBatchs() == null) { + parentSpeciesBatch.setChildBatchs(Lists.newArrayList(speciesBatch)); + } + else { + parentSpeciesBatch.addChildBatchs(speciesBatch); + } + } + + // If no parent found = batch is a child of the catch batch + else { + rootBatchs.add(speciesBatch); + } + } + } + + // Apply inheritance, starting with the catch batch children + applyInheritedProperties(rootBatchs, null, null, result); + + return result; } @Override @@ -122,56 +188,8 @@ if (source == null) { throw new DataRetrievalFailureException("Could not retrieve speciesBatch with id=" + id); } - SpeciesBatch result = new SpeciesBatch(); - result.setId(id); - - int colIndex = 0; - // Individual count - result.setNumber((Integer)source[colIndex++]); - - // Weight & sampleCategory Weight - Float sampleWeight = (Float)source[colIndex++]; - Float samplingRatio = (Float)source[colIndex++]; - String samplingRatioText = (String)source[colIndex++]; - if (samplingRatio == null) { - result.setWeight(sampleWeight); - } - else { - String startStr = sampleWeight.toString().replace(',', '.') + "/"; - if (samplingRatioText != null && samplingRatioText.startsWith(startStr)) { - String weightStr = samplingRatioText.substring(startStr.length()); - if (weightStr != null && !weightStr.isEmpty()) { - result.setSampleCategoryWeight(sampleWeight); - result.setWeight(Float.parseFloat(weightStr)); - } - } - } - - // Comments - result.setComment((String)source[colIndex++]); - - // Sample category type - Integer pmfmId = (Integer)source[colIndex++]; - if (pmfmId != null) { - SampleCategoryEnum sampleCategory = measurementHelper.pmfmId2SampleCategory(pmfmId); - result.setSampleCategoryType(sampleCategory); - } - - // Sample category value - Integer qvValue = (Integer)source[colIndex++]; - Float numValue = (Float)source[colIndex++]; - String alphaValue = (String)source[colIndex++]; - Serializable value = (qvValue!=null)?qvValue.toString():((numValue!=null)?numValue:alphaValue); - result.setSampleCategoryValue(value); - - // Species - Integer referenceTaxonId = (Integer)source[colIndex++]; - if (referenceTaxonId !=null) { - Species species = new Species(); - species.setId(referenceTaxonId.toString()); - result.setSpecies(species); - } - + + SpeciesBatch result = loadSpeciesBatch(source, true); return result; } @@ -182,6 +200,10 @@ Preconditions.checkArgument(bean.getId() == null); Preconditions.checkNotNull(bean.getSpecies()); Preconditions.checkNotNull(bean.getSpecies().getId()); + Preconditions.checkNotNull(bean.getFishingOperation()); + Preconditions.checkNotNull(bean.getFishingOperation().getId()); + // TODO BLA uncomment this after v1.0 : + // Preconditions.checkNotNull(bean.getCatchBatch()); SortingBatch batch = SortingBatch.Factory.newInstance(); beanToEntity(bean, batch, parentBatchId, true); @@ -241,48 +263,23 @@ // TODO BLA : prendre le service du 1er saisisseur ? Integer recorderDepartmentId = enumeration.DEPARTMENT_ID_UNKNOWN_RECORDER_DEPARTMENT; - // Retrieve entity Root batch - //source.getFishingOperation().get - Integer fishingOperationId = Integer.valueOf(source.getFishingOperation().getId()); - Integer rootBatchId = getRootBatchId(fishingOperationId); - if (rootBatchId == null) { - CatchBatch rootBatch = CatchBatch.Factory.newInstance(); - rootBatch.setFishingOperation(load(FishingOperationImpl.class, fishingOperationId)); - rootBatch.setQualityFlag(load(QualityFlagImpl.class, enumeration.QUALITY_FLAG_CODE_NOT_QUALIFIED)); - rootBatch.setRankOrder((short)1); - rootBatch.setSynchronizationStatus(SynchronizationStatus.DIRTY.getValue()); - rootBatch = catchBatchDao.create(rootBatch); - rootBatchId = rootBatch.getId(); - target.setRootBatch(rootBatch); - - //fr.ifremer.adagio.core.dao.data.operation.FishingOperation operation = load(FishingOperationImpl.class, fishingOperationId); - //operation.setCatchBatch(rootBatch); - getCurrentSession().flush(); - int rowUpdated = queryUpdate("updateFishingOperationCatchBatch", - "fishingOperationId", IntegerType.INSTANCE, fishingOperationId, - "catchBatchId", IntegerType.INSTANCE, rootBatchId); - if (rowUpdated == 0) { - throw new DataIntegrityViolationException("Could not attach catch batch to the given operation : operation was not found."); - } - } - else { - target.setRootBatch(load(CatchBatchImpl.class, Integer.valueOf(rootBatchId))); - } - // Parent batch - Integer targetParentBatchId = null;; - if (parentBatchId == null || parentBatchId.isEmpty()) { - targetParentBatchId = rootBatchId; - } - else { - targetParentBatchId = Integer.valueOf(parentBatchId); + // 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(parentBatchId))) { + setBatchParents(source, target, parentBatchId, notChangedSortingMeasurements); } - if (copyIfNull && targetParentBatchId == null) { - target.setParentBatch(null); - } - else if (targetParentBatchId != null){ - target.setParentBatch(load(BatchImpl.class, targetParentBatchId)); - } // RankOrder if (target.getRankOrder() == null) { @@ -293,16 +290,6 @@ } target.setRankOrder(rankOrder); } - - // 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()); - } // Weight or SampleCategoryWeight if (copyIfNull && source.getWeight() == null) { @@ -329,13 +316,17 @@ target.setSamplingRatio(source.getSampleCategoryWeight().floatValue() / source.getWeight().floatValue()); } - // SampleCategoryWeight + // 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) { - SortingMeasurement sortingMeasurement = measurementHelper.setSortingMeasurement(target, recorderDepartmentId, source.getSampleCategoryType(), source.getSampleCategoryValue()); - notChangedSortingMeasurements.remove(sortingMeasurement); + 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 @@ -384,11 +375,153 @@ } } - public Integer getRootBatchId(Integer fishingOperationId) { - Integer catchBatchId = queryUniqueTyped("fishingOperationCatchBatch", - "fishingOperationId", IntegerType.INSTANCE, fishingOperationId); - return catchBatchId; + public void setBatchParents(SpeciesBatch source, SortingBatch target, String parentBatchIdStr, + Set<SortingMeasurement> notChangedSortingMeasurements) { + + Preconditions.checkNotNull(target); + Preconditions.checkNotNull(source.getFishingOperation()); + Preconditions.checkNotNull(source.getFishingOperation().getId()); + + // Retrieve parent and root batch + if (parentBatchIdStr == null) { + // 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 with id={0}.", + new Object[]{enumeration.PMFM_ID_SORTED_UNSORTED})); + } + Integer qualitativeValueId = Integer.valueOf(source.getSampleCategoryValue().toString()); + + Object[] cols = queryUnique("parentBatch", + "fishingOperationId", IntegerType.INSTANCE, Integer.valueOf(source.getFishingOperation().getId()), + "pmfmIdSorted", IntegerType.INSTANCE, enumeration.PMFM_ID_SORTED_UNSORTED, + "qualitativeIdSorted", IntegerType.INSTANCE, qualitativeValueId, + "pmfmIdSortingType", IntegerType.INSTANCE, enumeration.PMFM_ID_SORTING_TYPE, + "qualitativeIdSortingType", IntegerType.INSTANCE, enumeration.QUALITATIVE_ID_SORTING_TYPE_SPECIES); + + // Parent Batch + Integer parentBatchId = (Integer)cols[0]; + SortingBatch parentBatch = load(SortingBatchImpl.class, parentBatchId); + target.setParentBatch(parentBatch); + + // Root Batch + Integer rootBatchId = (Integer)cols[1]; + target.setRootBatch(load(CatchBatchImpl.class, rootBatchId)); + } + + // Load existing parent and root + else { + SortingBatch parentBatch = load(SortingBatchImpl.class, Integer.valueOf(parentBatchIdStr)); + target.setParentBatch(parentBatch); + target.setRootBatch(parentBatch.getRootBatch()); + } } + + protected SpeciesBatch loadSpeciesBatch(Object[] source, boolean sourceHasPmfmGrandParent) { + int colIndex = 0; + + SpeciesBatch result = new SpeciesBatch(); + result.setId(source[colIndex++].toString()); + + // Individual count + result.setNumber((Integer)source[colIndex++]); + + // Weight & sampleCategory Weight + Float sampleWeight = (Float)source[colIndex++]; + Float samplingRatio = (Float)source[colIndex++]; + String samplingRatioText = (String)source[colIndex++]; + if (samplingRatio == null) { + result.setWeight(sampleWeight); + } + else if (sampleWeight != null){ + String startStr = sampleWeight.toString().replace(',', '.') + "/"; + if (samplingRatioText != null && samplingRatioText.startsWith(startStr)) { + String weightStr = samplingRatioText.substring(startStr.length()); + if (weightStr != null && !weightStr.isEmpty()) { + result.setSampleCategoryWeight(sampleWeight); + result.setWeight(Float.parseFloat(weightStr)); + } + } + } + // Comments + result.setComment((String)source[colIndex++]); + + // Sample category type + Integer pmfmId = (Integer)source[colIndex++]; + + // Sample category value + Integer qvValue = (Integer)source[colIndex++]; + Float numValue = (Float)source[colIndex++]; + String alphaValue = (String)source[colIndex++]; + Serializable value = (qvValue!=null)?qvValue.toString():((numValue!=null)?numValue:alphaValue); + // Species + Integer referenceTaxonId = (Integer)source[colIndex++]; + if (referenceTaxonId !=null) { + Species species = new Species(); + species.setId(referenceTaxonId.toString()); + result.setSpecies(species); + } + + if (sourceHasPmfmGrandParent && colIndex == source.length-2) { + Integer pmfmIdGrandFather = (Integer)source[colIndex++]; + Integer qualitativeIdGrandFather = (Integer)source[colIndex++]; + + if (pmfmId != null && value != null) { + SampleCategoryEnum sampleCategory = measurementHelper.pmfmId2SampleCategory(pmfmId); + result.setSampleCategoryType(sampleCategory); + result.setSampleCategoryValue(value); + } + + // When no sorting measurement found, retrieve the sorted/unsorted from an ancestor batch + else { + if (referenceTaxonId != null + && enumeration.PMFM_ID_SORTED_UNSORTED.equals(pmfmIdGrandFather) + && qualitativeIdGrandFather != null){ + result.setSampleCategoryType(SampleCategoryEnum.sortedUnsorted); + result.setSampleCategoryValue(qualitativeIdGrandFather.toString()); + } + else { + // TODO TC : manage this exception in the UI => 'format des captures incompatibles avec tutti...' + // TODO TC : add a throws 'InvalidBatchTreeException' in the interface method ? + throw new DataRetrievalFailureException("Invalid batch tree found in database (no sample category Vrac/HorsVrac found in parents) : could not be load batch with id=" + result.getId()); + } + } + } + + return result; + } + + protected void applyInheritedProperties(List<SpeciesBatch> speciesBatchs, Serializable inheritedSampleCategoryValue, Species inheritedSpecies, List<SpeciesBatch> result) { + if (speciesBatchs == null || speciesBatchs.size() == 0) { + return; + } + boolean hasAppliedCategoryValue = false; + for (Iterator iterator = speciesBatchs.iterator(); iterator.hasNext();) { + SpeciesBatch speciesBatch = (SpeciesBatch) iterator.next(); + + if (speciesBatch.getSpecies() != null && speciesBatch.getSampleCategoryType() == null) { + speciesBatch.setSampleCategoryType(SampleCategoryEnum.sortedUnsorted); + speciesBatch.setSampleCategoryValue(inheritedSampleCategoryValue); + hasAppliedCategoryValue = true; + + // Remove the link to the parent (not need after inheritance) + speciesBatch.setParentBatch(null); + } + else if (speciesBatch.getSpecies() == null && inheritedSpecies != null) { + speciesBatch.setSpecies(inheritedSpecies); + } + + // Store into result list only if species has been set (ignore upper batch) + if (speciesBatch.getSpecies() != null) { + result.add(speciesBatch); + } + + // Recursive call : propage species but not category if already applied + applyInheritedProperties(speciesBatch.getChildBatchs(), + (hasAppliedCategoryValue ? null : inheritedSampleCategoryValue), + speciesBatch.getSpecies(), result); + } + } } Modified: trunk/tutti-persistence-adagio/src/main/java/fr/ifremer/tutti/persistence/service/TuttiPersistenceServiceLocator.java =================================================================== --- trunk/tutti-persistence-adagio/src/main/java/fr/ifremer/tutti/persistence/service/TuttiPersistenceServiceLocator.java 2013-01-31 15:26:25 UTC (rev 277) +++ trunk/tutti-persistence-adagio/src/main/java/fr/ifremer/tutti/persistence/service/TuttiPersistenceServiceLocator.java 2013-01-31 15:37:38 UTC (rev 278) @@ -25,6 +25,7 @@ */ import fr.ifremer.adagio.core.service.ServiceLocator; +import fr.ifremer.adagio.core.service.technical.CacheService; import fr.ifremer.tutti.persistence.TuttiPersistence; import fr.ifremer.tutti.persistence.service.synchro.ReferentialSynchronizeService; @@ -118,7 +119,7 @@ return instance().getService("referentialSynchronizeService", ReferentialSynchronizeService.class); } - + public static void close() { instance().shutdown(); } Modified: trunk/tutti-persistence-adagio/src/main/resources/applicationContext-service-tutti.xml =================================================================== --- trunk/tutti-persistence-adagio/src/main/resources/applicationContext-service-tutti.xml 2013-01-31 15:26:25 UTC (rev 277) +++ trunk/tutti-persistence-adagio/src/main/resources/applicationContext-service-tutti.xml 2013-01-31 15:37:38 UTC (rev 278) @@ -37,4 +37,30 @@ <bean id="tuttiEnumerationFile" init-method="init" class="fr.ifremer.tutti.persistence.service.TuttiEnumerationFile"/> + <!-- Specific caches for tutti (not defined in ehcache.xml) --> + <bean id="tuttiAbstractCache" abstract="true" class="org.springframework.cache.ehcache.EhCacheFactoryBean"> + <property name="cacheManager" ref="ehcache"/> + <property name="maxElementsInMemory" value="5000"/> + <property name="eternal" value="false"/> + <property name="timeToLive" value="300"/> + <property name="timeToIdle" value="300"/> + <property name="overflowToDisk" value="false"/> + <property name="diskPersistent" value="false"/> + <property name="diskExpiryThreadIntervalSeconds" value="300"/> + </bean> + + <bean id="tuttiAllFishingVesselCache" parent="tuttiAbstractCache"> + <property name="cacheName" value="tuttiAllFishingVessel" /> + <property name="eternal" value="true"/> + <property name="overflowToDisk" value="true"/> + <property name="diskPersistent" value="true"/> + <!-- removed inherited properties --> + <property name="timeToLive" value="0"/> + <property name="timeToIdle" value="0"/> + </bean> + + <!-- Example to use to create a new cache area : + <bean id="tuttiOtherCache" parent="tuttiPersistenceDefaultCache"> + </bean> --> + </beans> \ No newline at end of file Deleted: trunk/tutti-persistence-adagio/src/main/resources/ehcache.xml =================================================================== --- trunk/tutti-persistence-adagio/src/main/resources/ehcache.xml 2013-01-31 15:26:25 UTC (rev 277) +++ trunk/tutti-persistence-adagio/src/main/resources/ehcache.xml 2013-01-31 15:37:38 UTC (rev 278) @@ -1,86 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<!-- - #%L - Tutti :: Persistence Adagio (impl) - $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% - --> - -<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://ehcache.org/ehcache.xsd" - updateCheck="false"> - - <!-- Sets the path to the directory where cache .data files are created. - - If the path is a Java System Property it is replaced by - its value in the running VM. - - The following properties are translated: - user.home - User's home directory - user.dir - User's current working directory - java.io.tmpdir - Default temp file path --> - <diskStore path="java.io.tmpdir/ehcache"/> - - <!--Default Cache configuration. These will applied to caches programmatically created through - the CacheManager. - - The following attributes are required for defaultCache: - - maxInMemory - Sets the maximum number of objects that will be created in memory - eternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the element - is never expired. - timeToIdleSeconds - Sets the time to idle for an element before it expires. - i.e. The maximum amount of time between accesses before an element expires - Is only used if the element is not eternal. - Optional attribute. A value of 0 means that an Element can idle for infinity - timeToLiveSeconds - Sets the time to live for an element before it expires. - i.e. The maximum time between creation time and when an element expires. - Is only used if the element is not eternal. - overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache - has reached the maxInMemory limit. - - --> - <defaultCache - maxElementsInMemory="10000" - eternal="false" - timeToIdleSeconds="120" - timeToLiveSeconds="120" - overflowToDisk="true" /> - - <!-- memoryStoreEvictionPolicy : - LRU - least recently used - LFU - least frequently used - FIFO - first in first out, the oldest element by creation time - --> - <cache name="allFishingVessel" eternal="true" - maxElementsInMemory="1" - memoryStoreEvictionPolicy="LFU"/> - - <cache name="org.hibernate.cache.UpdateTimestampsCache" - maxElementsInMemory="5000" timeToIdleSeconds="300" - timeToLiveSeconds="300" overflowToDisk="true" diskPersistent="false" - diskExpiryThreadIntervalSeconds="300" memoryStoreEvictionPolicy="LRU" /> - - <cache name="org.hibernate.cache.StandardQueryCache" - maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="300" - timeToLiveSeconds="300" overflowToDisk="true" diskPersistent="false" - diskExpiryThreadIntervalSeconds="300" memoryStoreEvictionPolicy="LRU" /> - -</ehcache> \ No newline at end of file Modified: trunk/tutti-persistence-adagio/src/main/resources/queries-override.hbm.xml =================================================================== --- trunk/tutti-persistence-adagio/src/main/resources/queries-override.hbm.xml 2013-01-31 15:26:25 UTC (rev 277) +++ trunk/tutti-persistence-adagio/src/main/resources/queries-override.hbm.xml 2013-01-31 15:37:38 UTC (rev 278) @@ -46,7 +46,7 @@ </query> <!-- [DAT-02] Get all cruises for a given program (to list with no detail) --> - <query cacheable="true" name="allCruises"> + <query name="allCruises"> <![CDATA[ SELECT c.id, @@ -63,7 +63,7 @@ </query> <!-- [DAT-03] Get a detail program --> - <query cacheable="true" name="program"> + <query name="program"> <![CDATA[ SELECT p.code, @@ -78,7 +78,7 @@ </query> <!-- [DAT-03-1] Get a program locations --> - <query cacheable="true" name="allProgramLocations"> + <query name="allProgramLocations"> <![CDATA[ SELECT l.id, @@ -94,7 +94,7 @@ </query> <!-- [DAT-04] Get a detail cruise --> - <query cacheable="true" name="cruise"> + <query name="cruise"> <![CDATA[ SELECT sc.program.code AS programCode, @@ -123,7 +123,7 @@ <query-param name="countryLocationLevelId" type="java.lang.Integer"/> </query> - <query cacheable="true" name="allCruiseGears"> + <query name="allCruiseGears"> <![CDATA[ SELECT gpf.gear.id AS gearId, @@ -148,7 +148,7 @@ <query-param name="pmfmIdTrawlNet" type="java.lang.Integer"/> </query> - <query cacheable="true" name="allCruiseSecondaryVessels"> + <query name="allCruiseSecondaryVessels"> <![CDATA[ SELECT va.operationVesselAssociationPk.vessel.code AS associatedVesselCode @@ -165,7 +165,7 @@ <query-param name="cruiseId" type="java.lang.Integer"/> </query> - <query cacheable="true" name="allFishingOperations"> + <query name="allFishingOperations"> <![CDATA[ SELECT o.id AS id, @@ -188,7 +188,7 @@ <query-param name="pmfmIdMultirigAggregation" type="java.lang.Integer"/> </query> - <query cacheable="true" name="fishingOperation"> + <query name="fishingOperation"> <![CDATA[ SELECT o.name AS name, @@ -218,7 +218,7 @@ <query-param name="locationLevelIdLocalite" type="java.lang.Integer"/> </query> - <query cacheable="true" name="fishingOperationRankOrder"> + <query name="fishingOperationRankOrder"> <![CDATA[ SELECT count(o1.id)+1 as fishingOperationRankOrder @@ -235,7 +235,7 @@ - <query cacheable="true" name="fishingOperationEnvironment"> + <query name="fishingOperationEnvironment"> <![CDATA[ SELECT vum.pmfm.id as pmfmId, @@ -251,7 +251,7 @@ <query-param name="fishingOperationId" type="java.lang.Integer"/> </query> - <query cacheable="true" name="fishingOperationGearShooting"> + <query name="fishingOperationGearShooting"> <![CDATA[ SELECT gum.pmfm.id as pmfmId, @@ -278,7 +278,7 @@ <query-param name="catchBatchId" type="java.lang.Integer"/> </query> - <query cacheable="true" name="catchBatch"> + <query name="catchBatch"> <![CDATA[ SELECT cb.id AS catchBatchId, @@ -304,83 +304,130 @@ ) ORDER BY b1.id, b2.id ]]> - <!-- Other equivalente query (but slower) + <query-param name="fishingOperationId" type="java.lang.Integer"/> + <query-param name="pmfmIdSorted" type="java.lang.Integer"/> + <query-param name="pmfmIdSortingType" type="java.lang.Integer"/> + </query> + + <query name="allRootSpeciesBatch"> + <![CDATA[ SELECT - cb.id AS catchBatchId, - cbQm.numericalValue as totalWeight, - b1.id AS id1, - b1.samplingRatioText as samplingRatioText1, - qm2.numericalValue AS weight1, - sm1.qualitativeValue.id AS qv2, - b2.id AS id2, - b2.samplingRatioText AS samplingRatioText2, - qm1.numericalValue AS weight2, - sm2.qualitativeValue.id AS qv2 + b.id as batchId, + b.individualCount AS individualCount, + qm.numericalValue AS weight, + b.samplingRatio AS samplingRatio, + b.samplingRatioText AS samplingRatioText, + b.comments AS comments, + sm.pmfm.id AS pmfmId, + sm.qualitativeValue.id as qvValue, + sm.numericalValue AS numValue, + sm.alphanumericalValue AS alphaValue, + b.referenceTaxon.id AS referenceTaxonId, + smSorted.pmfm.id AS pmfmIdGrandFather, + smSorted.qualitativeValue.id AS qualitativeIdGrandFather + FROM + CatchBatchImpl cb + INNER JOIN cb.childBatchs batchSorted + INNER JOIN batchSorted.childBatchs batchSortingType + INNER JOIN batchSortingType.childBatchs b + INNER JOIN batchSortingType.sortingMeasurements smSortingType + LEFT OUTER JOIN batchSorted.sortingMeasurements smSorted + LEFT OUTER JOIN b.sortingMeasurements sm + LEFT OUTER JOIN b.quantificationMeasurements qm + WHERE + cb.fishingOperation.id=:fishingOperationId + AND ( + qm is null + OR qm.isReferenceQuantification=true + ) + AND smSortingType.qualitativeValue.id=:qualitativeIdSortingType + ORDER BY b.id + ]]> + <query-param name="fishingOperationId" type="java.lang.Integer"/> + <query-param name="qualitativeIdSortingType" type="java.lang.Integer"/> + </query> + + <query name="allSpeciesBatch"> + <![CDATA[ + SELECT + b.id as batchId, + b.individualCount AS individualCount, + qm.numericalValue AS weight, + b.samplingRatio AS samplingRatio, + b.samplingRatioText AS samplingRatioText, + b.comments AS comments, + sm.pmfm.id AS pmfmId, + sm.qualitativeValue.id as qvValue, + sm.numericalValue AS numValue, + sm.alphanumericalValue AS alphaValue, + b.referenceTaxon.id as referenceTaxonId, + b.parentBatch.id as parentBatchId + FROM + SortingBatchImpl b + INNER JOIN b.rootBatch cb + LEFT OUTER JOIN b.sortingMeasurements sm + LEFT OUTER JOIN b.quantificationMeasurements qm + WHERE + cb.fishingOperation.id=:fishingOperationId + AND ( + qm is null + OR qm.isReferenceQuantification=true + ) + ORDER BY b.id + ]]> + <query-param name="fishingOperationId" type="java.lang.Integer"/> + </query> + + <query name="parentBatch"> + <![CDATA[ + SELECT + b2.id as parentBatchId, + cb.id as rootBatchId FROM - CatchBatchImpl cb + CatchBatchImpl cb INNER JOIN cb.childBatchs b1 - INNER JOIN b1.childBatchs b2 - LEFT OUTER JOIN cb.quantificationMeasurements cbQm - LEFT OUTER JOIN b1.quantificationMeasurements qm1 - LEFT OUTER JOIN b2.quantificationMeasurements qm2, + INNER JOIN b1.childBatchs b2, SortingMeasurementImpl sm1, SortingMeasurementImpl sm2 WHERE cb.fishingOperation.id = :fishingOperationId - AND ( - cbQm is null - OR cbQm.isReferenceQuantification=true - ) - AND sm1.sortingBatch.id=b1.id + AND sm1.sortingBatch.id=b1.id AND sm1.pmfm.id=:pmfmIdSorted - AND ( - qm2 is null - OR qm2.isReferenceQuantification=true - ) + AND sm1.qualitativeValue.id=:qualitativeIdSorted AND sm2.sortingBatch.id=b2.id AND sm2.pmfm.id=:pmfmIdSortingType - AND ( - qm1 is null - OR qm1.isReferenceQuantification=true - ) - ORDER BY b1.id, b2.id - --> + AND sm2.qualitativeValue.id=:qualitativeIdSortingType + ]]> <query-param name="fishingOperationId" type="java.lang.Integer"/> <query-param name="pmfmIdSorted" type="java.lang.Integer"/> + <query-param name="qualitativeIdSorted" type="java.lang.Integer"/> <query-param name="pmfmIdSortingType" type="java.lang.Integer"/> - </query> + <query-param name="qualitativeIdSortingType" type="java.lang.Integer"/> + </query> - <!--query cacheable="true" name="allFishingOperationBatchs"> + <query name="speciesBatch"> <![CDATA[ - select - db.id - from - BatchImpl db - WHERE - db.id = :fishingOperationId - ORDER BY - db.flatRankOrder - ]]> - <query-param name="fishingOperationId" type="java.lang.Integer"/> - </query--> - - <query cacheable="true" name="speciesBatch"> - <![CDATA[ SELECT + b.id AS batchId, b.individualCount AS individualCount, qm.numericalValue AS weight, b.samplingRatio AS samplingRatio, b.samplingRatioText AS samplingRatioText, b.comments AS comments, - sm.pmfm.id as pmfmId, + sm.pmfm.id AS pmfmId, sm.qualitativeValue.id as qvValue, sm.numericalValue AS numValue, sm.alphanumericalValue AS alphaValue, - b.referenceTaxon.id as referenceTaxonId + b.referenceTaxon.id AS referenceTaxonId, + smSorted.pmfm.id AS pmfmIdGrandFather, + smSorted.qualitativeValue.id AS qualitativeIdGrandFather FROM - SortingBatchImpl b - LEFT OUTER JOIN b.sortingMeasurements sm - LEFT OUTER JOIN b.quantificationMeasurements qm + SortingBatchImpl b + INNER JOIN b.parentBatch batchSortingType + INNER JOIN batchSortingType.parentBatch batchSorted + LEFT OUTER JOIN batchSorted.sortingMeasurements smSorted + LEFT OUTER JOIN b.sortingMeasurements sm + LEFT OUTER JOIN b.quantificationMeasurements qm WHERE b.id = :batchId AND ( Modified: trunk/tutti-persistence-adagio/src/test/java/fr/ifremer/tutti/persistence/service/ReferentialPersistenceServiceTest.java =================================================================== --- trunk/tutti-persistence-adagio/src/test/java/fr/ifremer/tutti/persistence/service/ReferentialPersistenceServiceTest.java 2013-01-31 15:26:25 UTC (rev 277) +++ trunk/tutti-persistence-adagio/src/test/java/fr/ifremer/tutti/persistence/service/ReferentialPersistenceServiceTest.java 2013-01-31 15:37:38 UTC (rev 278) @@ -25,6 +25,8 @@ */ import com.google.common.base.Preconditions; + +import fr.ifremer.adagio.core.service.technical.CacheService; import fr.ifremer.tutti.persistence.DatabaseResource; import fr.ifremer.tutti.persistence.TuttiPersistenceDevImpl; import fr.ifremer.tutti.persistence.config.TuttiPersistenceDevConfig; @@ -68,6 +70,8 @@ protected static TuttiPersistenceDevImpl storage; protected ReferentialPersistenceService service; + + protected CacheService cacheService; @BeforeClass public static void beforeClass() throws IOException { @@ -114,6 +118,10 @@ @Before public void setUp() throws Exception { service = TuttiPersistenceServiceLocator.getReferentialPersistenceService(); + cacheService = TuttiPersistenceServiceLocator.instance().getCacheService(); + + // This is need for test : getAllFishingVessel() + cacheService.clearAllCaches(); } @Test @@ -199,13 +207,23 @@ assertSize(result, storage.getAllScientificVessel()); } - //FIXME : requete trop longue (index manquant, trop de jointures?) @Test - @Ignore public void getAllFishingVessel() { + long time = System.currentTimeMillis(); List<Vessel> result = service.getAllFishingVessel(); + long delta1 = System.currentTimeMillis() - time; + Assert.assertNotNull(result); - Assert.assertEquals(31620, result.size()); + Assert.assertEquals(24805, result.size()); + + // try again, to check cache is enable + time = System.currentTimeMillis(); + result = service.getAllFishingVessel(); + long delta2 = System.currentTimeMillis() - time; + + float reduceFactor = delta2 * 100 / delta1; + Assert.assertTrue("The cache on getAllFishingVessel() should have speed up more than factor 10. Make sure EhCache is well configured.", reduceFactor < 0.1); + persistList(Vessel.class, "Fishing", result); assertSize(result, storage.getAllFishingVessel()); } Modified: trunk/tutti-persistence-adagio/src/test/java/fr/ifremer/tutti/persistence/service/SpeciesBatchPersistenceServiceTest.java =================================================================== --- trunk/tutti-persistence-adagio/src/test/java/fr/ifremer/tutti/persistence/service/SpeciesBatchPersistenceServiceTest.java 2013-01-31 15:26:25 UTC (rev 277) +++ trunk/tutti-persistence-adagio/src/test/java/fr/ifremer/tutti/persistence/service/SpeciesBatchPersistenceServiceTest.java 2013-01-31 15:37:38 UTC (rev 278) @@ -30,6 +30,7 @@ import java.util.List; 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; @@ -47,7 +48,6 @@ * @author tchemit <chemit@codelutin.com> * @since 0.3 */ -@Ignore public class SpeciesBatchPersistenceServiceTest { @ClassRule @@ -57,18 +57,23 @@ protected CruisePersistenceService cruiseService; protected FishingOperationPersistenceService fishingOperationService; + protected CatchBatchPersistenceService catchBatchService; protected ReferentialPersistenceService referentialService; protected Cruise cruise; protected FishingOperation fishingOperation; + protected CatchBatch catchBacth; protected List<Species> species; + protected String speciesBacthId = null; + @Before public void setUp() throws Exception { service = TuttiPersistenceServiceLocator.getSpeciesBatchPersistenceService(); cruiseService = TuttiPersistenceServiceLocator.getCruisePersistenceService(); fishingOperationService = TuttiPersistenceServiceLocator.getFishingOperationPersistenceService(); referentialService = TuttiPersistenceServiceLocator.getReferentialPersistenceService(); + catchBatchService = TuttiPersistenceServiceLocator.getCatchBatchPersistenceService(); cruise = cruiseService.getCruise(dbResource.getFixtures().cruiseId()); cruise.setId(null); @@ -96,6 +101,10 @@ fishingOperation.setGearShootingEndDate(calendar.getTime()); fishingOperation = fishingOperationService.createFishingOperation(fishingOperation); + + catchBacth = new CatchBatch(); + catchBacth.setFishingOperation(fishingOperation); + catchBacth = catchBatchService.createCatchBatch(catchBacth); species = referentialService.getAllSpecies(); assertNotNull(species); @@ -103,73 +112,46 @@ } @Test - @Ignore - public void getAllRootSpeciesBatch(/*String fishingOperationId*/) { - - List<SpeciesBatch> speciesBatch = service.getAllRootSpeciesBatch(fishingOperation.getId()); - assertNotNull(speciesBatch); - assertTrue(speciesBatch.size() > 0); - - } - - @Test - public void getAllSpeciesBatch(/*String fishingOperationId*/) { - - } - - @Test - public void getSpeciesBatch(/*String id*/) { - - } - - @Test public void createSpeciesBatch(/*SpeciesBatch bean, String parentBatchId*/) { Caracteristic sortedUnsortedPMFM = referentialService.getSortedUnsortedCaracteristic(); String horsVracQualitativeValueId = sortedUnsortedPMFM.getQualitativeValue(0).getId(); String vracQualitativeValueId = sortedUnsortedPMFM.getQualitativeValue(1).getId(); + Caracteristic maturityPMFM = referentialService.getMaturityCaracteristic(); + String firstMaturityQualitativeValueId = maturityPMFM.getQualitativeValue(0).getId(); Species taxon1 = species.get(0); Species taxon2 = species.get(1); Caracteristic sexPMFM = referentialService.getSexCaracteristic(); String maleQualitativeValueId = sexPMFM.getQualitativeValue(1).getId(); String femaleQualitativeValueId = sexPMFM.getQualitativeValue(2).getId(); + String unkQualitativeValueId = sexPMFM.getQualitativeValue(3).getId(); SpeciesBatch esp1Batch = null; + SpeciesBatch esp2Batch = null; SpeciesBatch batch = null; - SpeciesBatch createdBatch = null; - SpeciesBatch reloadedBatch = null; // ----------------------------------------------------------------------------- // 1. Test with only mandatory properties // ----------------------------------------------------------------------------- // batch : "ESP1 - Vrac/5" batch = new SpeciesBatch(); + batch.setParentBatch(null); batch.setFishingOperation(fishingOperation); + // TODO TC : add link between Speciesbatch and CatchBatch + //batch.setCatchBatch(catchBatch); batch.setSpecies(taxon1); batch.setSampleCategoryType(SampleCategoryEnum.sortedUnsorted); batch.setSampleCategoryValue(vracQualitativeValueId); batch.setWeight(5f); - createdBatch = service.createSpeciesBatch(batch, null); - assertNotNull(createdBatch); - assertNotNull(createdBatch.getId()); - reloadedBatch = service.getSpeciesBatch(createdBatch.getId()); - assertNotNull(reloadedBatch); - assertNull(reloadedBatch.getFishingOperation()); - assertEquals(createdBatch.getId(), reloadedBatch.getId()); - assertEquals(batch.getWeight(), reloadedBatch.getWeight()); - assertEquals(batch.getNumber(), reloadedBatch.getNumber()); - assertEquals(batch.getComment(), reloadedBatch.getComment()); - assertEquals(batch.getSampleCategoryType(), reloadedBatch.getSampleCategoryType()); - assertEquals(batch.getSampleCategoryValue(), reloadedBatch.getSampleCategoryValue()); - assertNotNull(reloadedBatch.getSpecies()); - assertEquals(batch.getSpecies().getId(), reloadedBatch.getSpecies().getId()); + assertCreateAndReloadSpeciesBatch(batch, null); // Save ESP1 batch - esp1Batch = reloadedBatch; + 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); @@ -180,22 +162,13 @@ batch.setSampleCategoryWeight(1f); batch.setNumber(7); - createdBatch = service.createSpeciesBatch(batch, batch.getParentBatch().getId()); - assertNotNull(createdBatch); - assertNotNull(createdBatch.getId()); - reloadedBatch = service.getSpeciesBatch(createdBatch.getId()); - assertNotNull(reloadedBatch); - assertNull(reloadedBatch.getParentBatch()); - assertEquals(batch.getNumber(), reloadedBatch.getNumber()); - assertEquals(batch.getSampleCategoryType(), reloadedBatch.getSampleCategoryType()); - assertEquals(batch.getSampleCategoryValue(), reloadedBatch.getSampleCategoryValue()); - assertEquals(batch.getWeight(), reloadedBatch.getWeight()); - assertEquals(batch.getSampleCategoryWeight(), reloadedBatch.getSampleCategoryWeight()); + 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); @@ -206,42 +179,144 @@ batch.setSampleCategoryWeight(null); batch.setNumber(14); - createdBatch = service.createSpeciesBatch(batch, batch.getParentBatch().getId()); - assertNotNull(createdBatch); - assertNotNull(createdBatch.getId()); - reloadedBatch = service.getSpeciesBatch(createdBatch.getId()); - assertNotNull(reloadedBatch); - assertNull(reloadedBatch.getParentBatch()); - assertEquals(batch.getNumber(), reloadedBatch.getNumber()); - assertEquals(batch.getSampleCategoryType(), reloadedBatch.getSampleCategoryType()); - assertEquals(batch.getSampleCategoryValue(), reloadedBatch.getSampleCategoryValue()); - assertEquals(batch.getWeight(), reloadedBatch.getWeight()); - assertEquals(batch.getSampleCategoryWeight(), reloadedBatch.getSampleCategoryWeight()); + 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); + // TODO TC : add link between Speciesbatch and CatchBatch + //batch.setCatchBatch(catchBatch); + batch.setSpecies(taxon2); + batch.setSampleCategoryType(SampleCategoryEnum.sortedUnsorted); + batch.setSampleCategoryValue(vracQualitativeValueId); + batch.setWeight(7f); + assertCreateAndReloadSpeciesBatch(batch, null); + esp2Batch = batch; - /* - reloadedBatch.setSampleCategoryWeight(1.5f); - reloadedBatch.setParentBatch(createdBatch.getParentBatch()); - reloadedBatch.setFishingOperation(fishingOperation); - service.saveSpeciesBatch(reloadedBatch); - */ + // Batch : ESP2 - Vrac/7 UNK/2 ss-ech/1 Nombre/11 + batch = new SpeciesBatch(); + batch.setId(null); + batch.setParentBatch(esp2Batch); + batch.setSpecies(taxon1); + batch.setComment("ESP2 - Vrac/7 UNK/2 ss-ech/1 Nombre/11"); + batch.setSampleCategoryType(SampleCategoryEnum.maturity); + batch.setSampleCategoryValue(firstMaturityQualitativeValueId); + batch.setWeight(2f); + batch.setSampleCategoryWeight(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.setSampleCategoryType(SampleCategoryEnum.sex); + batch.setSampleCategoryValue(unkQualitativeValueId); + batch.setComment("ESP2 - Vrac/7 UNK/1.75 ss-ech/1.11 Nombre/99"); + batch.setWeight(1.75f); + batch.setSampleCategoryWeight(1.11f); + batch.setFishingOperation(fishingOperation); + batch.setNumber(99); + + // Save and reload, then check + SpeciesBatch savedBatch = service.saveSpeciesBatch(batch); + assertSpeciesBatch(savedBatch, batch, false); + SpeciesBatch reloadedBatch = service.getSpeciesBatch(savedBatch.getId()); + assertSpeciesBatch(reloadedBatch, savedBatch, true); + + // ----------------------------------------------------------------------------- + // 6. Test get all root species + // ----------------------------------------------------------------------------- + List<SpeciesBatch> rootSpeciesBatch = service.getAllRootSpeciesBatch(fishingOperation.getId()); + assertNotNull(rootSpeciesBatch); + assertEquals(2, rootSpeciesBatch.size()); + + // ----------------------------------------------------------------------------- + // 4. Test get all species + // ----------------------------------------------------------------------------- + List<SpeciesBatch> allSpeciesBatch = service.getAllSpeciesBatch(fishingOperation.getId()); + assertNotNull(allSpeciesBatch); + assertEquals(5, allSpeciesBatch.size()); } @Test + @Ignore public void saveSpeciesBatch(/*SpeciesBatch bean*/) { + + } + + @Test + @Ignore + public void getSpeciesBatch(/*String id*/) { + if (speciesBacthId == null) return; + SpeciesBatch batch = service.getSpeciesBatch(speciesBacthId); + assertNotNull(batch); } @Test + @Ignore + public void getAllSpeciesBatch(/*String fishingOperationId*/) { + + } + @Test + @Ignore + public void getAllRootSpeciesBatch(/*String fishingOperationId*/) { + } + + @Test + @Ignore public void deleteSpeciesBatch(/*String id*/) { } @Test + @Ignore public void deleteSpeciesSubBatch(/*String id*/) { } @Test + @Ignore public void getAllSpeciesBatchFrequency(/*String speciesBatchId*/) { } + + protected void assertCreateAndReloadSpeciesBatch(SpeciesBatch batch, String parentBatchId) { + batch.setFishingOperation(fishingOperation); + + // Create batch + SpeciesBatch createdBatch = service.createSpeciesBatch(batch, parentBatchId); + assertSpeciesBatch(createdBatch, batch, false); + + // then reload (for round trip check) + SpeciesBatch reloadedBatch = service.getSpeciesBatch(createdBatch.getId()); + assertNull(reloadedBatch.getParentBatch()); + assertSpeciesBatch(reloadedBatch, batch, false); + + batch.setId(createdBatch.getId()); + } + + 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()); + assertEquals(expectedBatch.getSampleCategoryValue(), actualBatch.getSampleCategoryValue()); + assertEquals(expectedBatch.getSampleCategoryWeight(), actualBatch.getSampleCategoryWeight()); + assertEquals(expectedBatch.getNumber(), actualBatch.getNumber()); + assertEquals(expectedBatch.getComment(), actualBatch.getComment()); + if (expectedBatch.getSpecies() != null){ + assertNotNull(actualBatch.getSpecies()); + assertEquals(expectedBatch.getSpecies().getId(), actualBatch.getSpecies().getId()); + } + } + }