r925 - in trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service: . batch
Author: tchemit Date: 2013-05-07 15:07:36 +0200 (Tue, 07 May 2013) New Revision: 925 Url: http://forge.codelutin.com/projects/tutti/repository/revisions/925 Log: fixes #2404: [CAPTURES] Les pic?\195?\168es jointes ne sont pas toutes supprim?\195?\169es improve sample helper api improve delete attachments api Added: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/SamplePersistenceHelper.java Removed: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/SampleMeasurementPersistenceHelper.java Modified: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/AccidentalBatchPersistenceServiceImpl.java trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/AttachmentPersistenceService.java trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/AttachmentPersistenceServiceImpl.java trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/BenthosBatchPersistenceServiceImpl.java trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/FishingOperationPersistenceServiceImpl.java trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/IndividualObservationBatchPersistenceServiceImpl.java trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/MarineLitterBatchPersistenceServiceImpl.java trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/PlanktonBatchPersistenceServiceImpl.java trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/SpeciesBatchPersistenceServiceImpl.java trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/batch/BatchPersistenceHelper.java Modified: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/AccidentalBatchPersistenceServiceImpl.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/AccidentalBatchPersistenceServiceImpl.java 2013-05-07 13:00:45 UTC (rev 924) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/AccidentalBatchPersistenceServiceImpl.java 2013-05-07 13:07:36 UTC (rev 925) @@ -31,7 +31,6 @@ import fr.ifremer.adagio.core.dao.administration.user.PersonImpl; import fr.ifremer.adagio.core.dao.data.operation.FishingOperationImpl; import fr.ifremer.adagio.core.dao.data.sample.Sample; -import fr.ifremer.adagio.core.dao.data.sample.SampleDao; import fr.ifremer.adagio.core.dao.referential.QualityFlagImpl; import fr.ifremer.adagio.core.dao.referential.pmfm.Matrix; import fr.ifremer.adagio.core.dao.referential.pmfm.MatrixImpl; @@ -41,7 +40,6 @@ import fr.ifremer.tutti.persistence.entities.TuttiBeanFactory; import fr.ifremer.tutti.persistence.entities.TuttiEntities; import fr.ifremer.tutti.persistence.entities.data.AccidentalBatch; -import fr.ifremer.tutti.persistence.entities.data.AttachementObjectTypeEnum; import fr.ifremer.tutti.persistence.entities.referential.Caracteristic; import fr.ifremer.tutti.persistence.entities.referential.CaracteristicQualitativeValue; import fr.ifremer.tutti.persistence.entities.referential.Species; @@ -73,15 +71,9 @@ @Autowired protected ReferentialPersistenceService referentialService; - @Autowired - protected AttachmentPersistenceService attachmentPersistenceService; + @Resource(name = "samplePersistenceHelper") + protected SamplePersistenceHelper samplePersistenceHelper; - @Autowired - protected SampleMeasurementPersistenceHelper sampleMeasurementPersistenceHelper; - - @Resource(name = "sampleDao") - protected SampleDao sampleDao; - @Override public List<AccidentalBatch> getAllAccidentalBatch(String fishingOperationId) { Preconditions.checkNotNull(fishingOperationId); @@ -132,7 +124,7 @@ Sample sample = Sample.Factory.newInstance(); beanToEntity(bean, sample); - sampleDao.create(sample); + samplePersistenceHelper.create(sample); bean.setId(String.valueOf(sample.getId())); return bean; } @@ -145,13 +137,13 @@ Preconditions.checkNotNull(bean.getFishingOperation()); Preconditions.checkState(!TuttiEntities.isNew(bean.getFishingOperation())); - Sample sample = sampleDao.load(bean.getIdAsInt()); + Sample sample = samplePersistenceHelper.load(bean.getIdAsInt()); if (sample == null) { throw new DataRetrievalFailureException( "Could not retrieve sample id=" + bean.getId()); } beanToEntity(bean, sample); - sampleDao.update(sample); + samplePersistenceHelper.update(sample); return bean; } @@ -164,11 +156,7 @@ if (log.isInfoEnabled()) { log.info("Will delete accidental batch: " + batchId); } - sampleDao.remove(batchId); - - attachmentPersistenceService.deleteAllAttachment( - AttachementObjectTypeEnum.SAMPLE, - batchId); + samplePersistenceHelper.deleteSample(batchId); } @Override @@ -295,8 +283,8 @@ source.getSize()); } - sampleMeasurementPersistenceHelper.setSampleMeasurements(target, - caracteristics); + samplePersistenceHelper.setSampleMeasurements(target, + caracteristics); } @@ -308,7 +296,7 @@ CaracteristicMap result = batch.getCaracteristics(); - sampleMeasurementPersistenceHelper.fillSampleMeasurements( + samplePersistenceHelper.fillSampleMeasurements( result, batch.getIdAsInt()); CaracteristicQualitativeValue deadOrAliveValue = Modified: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/AttachmentPersistenceService.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/AttachmentPersistenceService.java 2013-05-07 13:00:45 UTC (rev 924) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/AttachmentPersistenceService.java 2013-05-07 13:07:36 UTC (rev 925) @@ -88,13 +88,13 @@ void deleteAttachment(String attachmentId); /** - * Deletes all attachments of the given object id. + * Deletes all attachments of the given object ids. * * @param objectType type of attachment - * @param objectId id of the object + * @param objectIds ids of object */ @Transactional(readOnly = false) void deleteAllAttachment(AttachementObjectTypeEnum objectType, - Integer objectId); + Integer... objectIds); } Modified: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/AttachmentPersistenceServiceImpl.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/AttachmentPersistenceServiceImpl.java 2013-05-07 13:00:45 UTC (rev 924) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/AttachmentPersistenceServiceImpl.java 2013-05-07 13:07:36 UTC (rev 925) @@ -246,10 +246,12 @@ @Override public void deleteAllAttachment(AttachementObjectTypeEnum objectType, - Integer objectId) { - List<Attachment> attachments = getAllAttachments(objectType, objectId); - for (Attachment attachment : attachments) { - delete(attachment); + Integer... objectIds) { + for (Integer objectId : objectIds) { + List<Attachment> attachments = getAllAttachments(objectType, objectId); + for (Attachment attachment : attachments) { + delete(attachment); + } } } Modified: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/BenthosBatchPersistenceServiceImpl.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/BenthosBatchPersistenceServiceImpl.java 2013-05-07 13:00:45 UTC (rev 924) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/BenthosBatchPersistenceServiceImpl.java 2013-05-07 13:07:36 UTC (rev 925) @@ -37,7 +37,6 @@ import fr.ifremer.adagio.core.dao.referential.taxon.ReferenceTaxon; import fr.ifremer.adagio.core.dao.referential.taxon.ReferenceTaxonImpl; import fr.ifremer.tutti.persistence.entities.TuttiBeanFactory; -import fr.ifremer.tutti.persistence.entities.data.AttachementObjectTypeEnum; import fr.ifremer.tutti.persistence.entities.data.BatchContainer; import fr.ifremer.tutti.persistence.entities.data.BenthosBatch; import fr.ifremer.tutti.persistence.entities.data.BenthosBatchFrequency; @@ -76,9 +75,6 @@ @Autowired protected ReferentialPersistenceService referentialService; - @Autowired - protected AttachmentPersistenceService attachmentPersistenceService; - @Resource(name = "batchPersistenceHelper") protected BatchPersistenceHelper batchHelper; @@ -191,10 +187,6 @@ Integer batchId = Integer.valueOf(id); batchHelper.removeWithChildren(batchId); - - attachmentPersistenceService.deleteAllAttachment( - AttachementObjectTypeEnum.BATCH, - batchId); } @Override @@ -221,11 +213,6 @@ log.info("Delete child [" + childBatchId + "] of species batch: " + id); } batchHelper.removeWithChildren(childBatchId); - - // delete his attachment (if any) - attachmentPersistenceService.deleteAllAttachment( - AttachementObjectTypeEnum.BATCH, - childBatchId); } } } Modified: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/FishingOperationPersistenceServiceImpl.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/FishingOperationPersistenceServiceImpl.java 2013-05-07 13:00:45 UTC (rev 924) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/FishingOperationPersistenceServiceImpl.java 2013-05-07 13:07:36 UTC (rev 925) @@ -366,8 +366,18 @@ @Override public FishingOperation saveFishingOperation(FishingOperation bean) { - checkFishingOperation(bean); + Preconditions.checkNotNull(bean); + Preconditions.checkNotNull(bean.getId()); + Preconditions.checkNotNull(bean.getCruise()); + Preconditions.checkNotNull(bean.getCruise().getId()); + //TODO-TC Voir si il n'y a pas d'autre données obligatoires + if (bean.getGearShootingStartDate() != null && bean.getGearShootingEndDate() != null) { + + //make sure not same date + Preconditions.checkArgument(!bean.getGearShootingStartDate().equals(bean.getGearShootingEndDate())); + } + getCurrentSession().clear(); getCurrentSession().setFlushMode(FlushMode.COMMIT); fr.ifremer.adagio.core.dao.data.operation.FishingOperation fishingOperation = fishingOperationDao.load(Integer.valueOf(bean.getId())); @@ -484,9 +494,6 @@ // remove operation vessel associations removeAllOperationVesselAssociation(fishingOperation); -// if (CollectionUtils.isNotEmpty(fishingOperation.getOperationVesselAssociations())) { -// fishingOperation.getOperationVesselAssociations().clear(); -// } // remove fishing areas if (CollectionUtils.isNotEmpty(fishingAreas)) { @@ -521,20 +528,6 @@ //-- Internal methods --// //------------------------------------------------------------------------// - protected void checkFishingOperation(FishingOperation bean) { - Preconditions.checkNotNull(bean); - Preconditions.checkNotNull(bean.getId()); - Preconditions.checkNotNull(bean.getCruise()); - Preconditions.checkNotNull(bean.getCruise().getId()); - //TODO-TC Voir si il n'y a pas d'autre données obligatoires - - if (bean.getGearShootingStartDate() != null && bean.getGearShootingEndDate() != null) { - - //make sure not same date - Preconditions.checkArgument(!bean.getGearShootingStartDate().equals(bean.getGearShootingEndDate())); - } - } - protected void getVesselUseCaracteristics(String fishingOperationId, FishingOperation result) { // retrieve fishing operation caracteristics Iterator<Object[]> list = queryList( Modified: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/IndividualObservationBatchPersistenceServiceImpl.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/IndividualObservationBatchPersistenceServiceImpl.java 2013-05-07 13:00:45 UTC (rev 924) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/IndividualObservationBatchPersistenceServiceImpl.java 2013-05-07 13:07:36 UTC (rev 925) @@ -33,7 +33,6 @@ import fr.ifremer.adagio.core.dao.data.batch.CatchBatch; import fr.ifremer.adagio.core.dao.data.operation.FishingOperationImpl; import fr.ifremer.adagio.core.dao.data.sample.Sample; -import fr.ifremer.adagio.core.dao.data.sample.SampleDao; import fr.ifremer.adagio.core.dao.referential.QualityFlagImpl; import fr.ifremer.adagio.core.dao.referential.pmfm.Matrix; import fr.ifremer.adagio.core.dao.referential.pmfm.MatrixImpl; @@ -42,7 +41,6 @@ import fr.ifremer.tutti.persistence.entities.CaracteristicMap; import fr.ifremer.tutti.persistence.entities.TuttiBeanFactory; import fr.ifremer.tutti.persistence.entities.TuttiEntities; -import fr.ifremer.tutti.persistence.entities.data.AttachementObjectTypeEnum; import fr.ifremer.tutti.persistence.entities.data.FishingOperation; import fr.ifremer.tutti.persistence.entities.data.IndividualObservationBatch; import fr.ifremer.tutti.persistence.entities.referential.Caracteristic; @@ -79,15 +77,12 @@ @Autowired protected AttachmentPersistenceService attachmentPersistenceService; - @Autowired - protected SampleMeasurementPersistenceHelper sampleMeasurementPersistenceHelper; + @Resource(name = "samplePersistenceHelper") + protected SamplePersistenceHelper samplePersistenceHelper; @Resource(name = "batchPersistenceHelper") protected BatchPersistenceHelper batchHelper; - @Resource(name = "sampleDao") - protected SampleDao sampleDao; - @Override public List<IndividualObservationBatch> getAllIndividualObservationBatch(String fishingOperationId) { Preconditions.checkNotNull(fishingOperationId); @@ -152,7 +147,7 @@ Sample sample = Sample.Factory.newInstance(); beanToEntity(bean, sample, catchBatch); - sampleDao.create(sample); + samplePersistenceHelper.create(sample); bean.setId(String.valueOf(sample.getId())); return bean; } @@ -172,13 +167,13 @@ // Get catch Batch Batch catchBatch = getBatch(fishingOperation.getIdAsInt()); - Sample sample = sampleDao.load(bean.getIdAsInt()); + Sample sample = samplePersistenceHelper.load(bean.getIdAsInt()); if (sample == null) { throw new DataRetrievalFailureException( "Could not retrieve sample id: " + bean.getId()); } beanToEntity(bean, sample, catchBatch); - sampleDao.update(sample); + samplePersistenceHelper.update(sample); return bean; } @@ -192,15 +187,7 @@ log.info("Will delete individual observation batch: " + batchId); } - //FIXME See why this code does not work -// sampleDao.remove(batchId); - Sample sample = sampleDao.load(batchId); - if (sample != null) { - sample.getSampleMeasurements().clear(); - sampleDao.remove(sample); - attachmentPersistenceService.deleteAllAttachment(AttachementObjectTypeEnum.SAMPLE, - batchId); - } + samplePersistenceHelper.deleteSample(batchId); } @Override @@ -327,8 +314,8 @@ source.getSize()); } - sampleMeasurementPersistenceHelper.setSampleMeasurements(target, - caracteristics); + samplePersistenceHelper.setSampleMeasurements(target, + caracteristics); } protected Batch getBatch(Integer operationId) { @@ -342,7 +329,7 @@ CaracteristicMap result = batch.getCaracteristics(); - sampleMeasurementPersistenceHelper.fillSampleMeasurements( + samplePersistenceHelper.fillSampleMeasurements( result, batch.getIdAsInt()); String sampleCode = (String) result.remove(referentialService.getSampleIdCaracteristic()); Modified: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/MarineLitterBatchPersistenceServiceImpl.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/MarineLitterBatchPersistenceServiceImpl.java 2013-05-07 13:00:45 UTC (rev 924) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/MarineLitterBatchPersistenceServiceImpl.java 2013-05-07 13:07:36 UTC (rev 925) @@ -35,7 +35,6 @@ import fr.ifremer.adagio.core.dao.referential.pmfm.QualitativeValue; import fr.ifremer.tutti.persistence.entities.TuttiBeanFactory; import fr.ifremer.tutti.persistence.entities.TuttiEntities; -import fr.ifremer.tutti.persistence.entities.data.AttachementObjectTypeEnum; import fr.ifremer.tutti.persistence.entities.data.BatchContainer; import fr.ifremer.tutti.persistence.entities.data.MarineLitterBatch; import fr.ifremer.tutti.persistence.entities.referential.Caracteristic; @@ -65,9 +64,6 @@ @Autowired protected ReferentialPersistenceService referentialService; - @Autowired - protected AttachmentPersistenceService attachmentPersistenceService; - @Resource(name = "batchPersistenceHelper") protected BatchPersistenceHelper batchHelper; @@ -162,9 +158,6 @@ Integer batchId = Integer.valueOf(id); batchHelper.removeWithChildren(batchId); - - attachmentPersistenceService.deleteAllAttachment( - AttachementObjectTypeEnum.BATCH, batchId); } public MarineLitterBatch entityToMarineLitterBatch(SortingBatch source, Modified: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/PlanktonBatchPersistenceServiceImpl.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/PlanktonBatchPersistenceServiceImpl.java 2013-05-07 13:00:45 UTC (rev 924) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/PlanktonBatchPersistenceServiceImpl.java 2013-05-07 13:07:36 UTC (rev 925) @@ -26,7 +26,6 @@ import com.google.common.base.Preconditions; import com.google.common.collect.Lists; -import fr.ifremer.tutti.persistence.entities.data.AttachementObjectTypeEnum; import fr.ifremer.tutti.persistence.entities.data.PlanktonBatch; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -46,9 +45,6 @@ private static final Log log = LogFactory.getLog(PlanktonBatchPersistenceServiceImpl.class); - @Autowired - protected AttachmentPersistenceService attachmentPersistenceService; - @Override public List<PlanktonBatch> getAllPlanktonBatch(String fishingOperationId) { List<PlanktonBatch> result = Lists.newArrayList(); @@ -71,8 +67,5 @@ @Override public void deletePlanktonBatch(String id) { Preconditions.checkNotNull(id); - attachmentPersistenceService.deleteAllAttachment( - AttachementObjectTypeEnum.BATCH, - Integer.valueOf(id)); } } Deleted: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/SampleMeasurementPersistenceHelper.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/SampleMeasurementPersistenceHelper.java 2013-05-07 13:00:45 UTC (rev 924) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/SampleMeasurementPersistenceHelper.java 2013-05-07 13:07:36 UTC (rev 925) @@ -1,145 +0,0 @@ -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 com.google.common.collect.Sets; -import fr.ifremer.adagio.core.dao.data.measure.SampleMeasurement; -import fr.ifremer.adagio.core.dao.data.sample.Sample; -import fr.ifremer.adagio.core.dao.referential.QualityFlagImpl; -import fr.ifremer.adagio.core.dao.referential.pmfm.PmfmImpl; -import fr.ifremer.tutti.persistence.entities.CaracteristicMap; -import fr.ifremer.tutti.persistence.entities.TuttiEntities; -import fr.ifremer.tutti.persistence.entities.referential.Caracteristic; -import org.hibernate.type.IntegerType; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -import java.io.Serializable; -import java.util.Iterator; -import java.util.Set; - -/** - * Helper around {@link SampleMeasurement}. - * - * @author tchemit <chemit@codelutin.com> - * @since 1.6 - */ -@Component("sampleMeasurementPersistenceHelper") -public class SampleMeasurementPersistenceHelper extends AbstractPersistenceService { - - @Autowired - protected ReferentialPersistenceService referentialService; - - @Autowired - protected MeasurementPersistenceHelper measurementPersistenceHelper; - - public void setSampleMeasurements(Sample target, - CaracteristicMap caracteristics) { - - Set<SampleMeasurement> notChangedSampleMeasurements = Sets.newHashSet(); - if (target.getSampleMeasurements() != null) { - notChangedSampleMeasurements.addAll(target.getSampleMeasurements()); - } - - for (Caracteristic caracteristic : caracteristics.keySet()) { - SampleMeasurement vum = setSampleMeasurement( - target, - caracteristic, - caracteristics.get(caracteristic)); - notChangedSampleMeasurements.remove(vum); - } - - if (target.getSampleMeasurements() != null && - notChangedSampleMeasurements.size() > 0) { - target.getSampleMeasurements().removeAll(notChangedSampleMeasurements); - } - } - - public void fillSampleMeasurements(CaracteristicMap result, - Integer sampleId) { - Iterator<Object[]> list = queryList( - "sampleMeasurements", - "sampleId", IntegerType.INSTANCE, sampleId); - while (list.hasNext()) { - int colIndex = 0; - Object[] source = list.next(); - Integer pmfmId = (Integer) source[colIndex++]; - Float numericalValue = (Float) source[colIndex++]; - String alphanumericalValue = (String) source[colIndex++]; - Integer qualitativeValueId = (Integer) source[colIndex]; - - Caracteristic caracteristic = referentialService.getCaracteristic(pmfmId); - Serializable value = null; - switch (caracteristic.getCaracteristicType()) { - - case NUMBER: - value = numericalValue; - break; - case QUALITATIVE: - value = TuttiEntities.getQualitativeValue(caracteristic, - qualitativeValueId); - break; - case TEXT: - value = alphanumericalValue; - break; - } - result.put(caracteristic, value); - } - } - - protected SampleMeasurement setSampleMeasurement(Sample sample, - Caracteristic caracteristic, - Serializable value) { - - Integer pmfmId = caracteristic.getIdAsInt(); - - SampleMeasurement result = null; - if (sample.getSampleMeasurements() != null) { - for (SampleMeasurement vum : sample.getSampleMeasurements()) { - if (pmfmId.equals(vum.getPmfm().getId())) { - result = vum; - break; - } - } - } - if (result == null) { - - result = SampleMeasurement.Factory.newInstance(); - - result.setSample(sample); - if (sample.getSampleMeasurements() == null) { - sample.setSampleMeasurements(Sets.newHashSet(result)); - } else { - sample.getSampleMeasurements().add(result); - } - result.setQualityFlag(load(QualityFlagImpl.class, enumeration.QUALITY_FLAG_CODE_NOT_QUALIFIED)); - result.setDepartment(sample.getRecorderDepartment()); - result.setPmfm(load(PmfmImpl.class, pmfmId)); - } - measurementPersistenceHelper.setMeasurement(result, caracteristic, value); - return result; - } - -} Copied: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/SamplePersistenceHelper.java (from rev 920, trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/SampleMeasurementPersistenceHelper.java) =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/SamplePersistenceHelper.java (rev 0) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/SamplePersistenceHelper.java 2013-05-07 13:07:36 UTC (rev 925) @@ -0,0 +1,178 @@ +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 com.google.common.collect.Sets; +import fr.ifremer.adagio.core.dao.data.measure.SampleMeasurement; +import fr.ifremer.adagio.core.dao.data.sample.Sample; +import fr.ifremer.adagio.core.dao.data.sample.SampleDao; +import fr.ifremer.adagio.core.dao.referential.QualityFlagImpl; +import fr.ifremer.adagio.core.dao.referential.pmfm.PmfmImpl; +import fr.ifremer.tutti.persistence.entities.CaracteristicMap; +import fr.ifremer.tutti.persistence.entities.TuttiEntities; +import fr.ifremer.tutti.persistence.entities.data.AttachementObjectTypeEnum; +import fr.ifremer.tutti.persistence.entities.referential.Caracteristic; +import org.hibernate.type.IntegerType; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import java.io.Serializable; +import java.util.Iterator; +import java.util.Set; + +/** + * Helper around {@link Sample}. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.6 + */ +@Component("samplePersistenceHelper") +public class SamplePersistenceHelper extends AbstractPersistenceService { + + @Autowired + protected ReferentialPersistenceService referentialService; + + @Autowired + protected MeasurementPersistenceHelper measurementPersistenceHelper; + + @Autowired + protected AttachmentPersistenceService attachmentPersistenceService; + + @Resource(name = "sampleDao") + protected SampleDao sampleDao; + + public Sample create(Sample sample) { + return sampleDao.create(sample); + } + + public void update(Sample sample) { + sampleDao.update(sample); + } + + public Sample load(Integer id) { + return sampleDao.load(id); + } + + public void deleteSample(Integer sampleId) { + + Sample sample = sampleDao.load(sampleId); + if (sample != null) { + sample.getSampleMeasurements().clear(); + sampleDao.remove(sample); + attachmentPersistenceService.deleteAllAttachment( + AttachementObjectTypeEnum.SAMPLE, + sampleId); + } + } + + public void setSampleMeasurements(Sample target, + CaracteristicMap caracteristics) { + + Set<SampleMeasurement> notChangedSampleMeasurements = Sets.newHashSet(); + if (target.getSampleMeasurements() != null) { + notChangedSampleMeasurements.addAll(target.getSampleMeasurements()); + } + + for (Caracteristic caracteristic : caracteristics.keySet()) { + SampleMeasurement vum = setSampleMeasurement( + target, + caracteristic, + caracteristics.get(caracteristic)); + notChangedSampleMeasurements.remove(vum); + } + + if (target.getSampleMeasurements() != null && + notChangedSampleMeasurements.size() > 0) { + target.getSampleMeasurements().removeAll(notChangedSampleMeasurements); + } + } + + public void fillSampleMeasurements(CaracteristicMap result, + Integer sampleId) { + Iterator<Object[]> list = queryList( + "sampleMeasurements", + "sampleId", IntegerType.INSTANCE, sampleId); + while (list.hasNext()) { + int colIndex = 0; + Object[] source = list.next(); + Integer pmfmId = (Integer) source[colIndex++]; + Float numericalValue = (Float) source[colIndex++]; + String alphanumericalValue = (String) source[colIndex++]; + Integer qualitativeValueId = (Integer) source[colIndex]; + + Caracteristic caracteristic = referentialService.getCaracteristic(pmfmId); + Serializable value = null; + switch (caracteristic.getCaracteristicType()) { + + case NUMBER: + value = numericalValue; + break; + case QUALITATIVE: + value = TuttiEntities.getQualitativeValue(caracteristic, + qualitativeValueId); + break; + case TEXT: + value = alphanumericalValue; + break; + } + result.put(caracteristic, value); + } + } + + protected SampleMeasurement setSampleMeasurement(Sample sample, + Caracteristic caracteristic, + Serializable value) { + + Integer pmfmId = caracteristic.getIdAsInt(); + + SampleMeasurement result = null; + if (sample.getSampleMeasurements() != null) { + for (SampleMeasurement vum : sample.getSampleMeasurements()) { + if (pmfmId.equals(vum.getPmfm().getId())) { + result = vum; + break; + } + } + } + if (result == null) { + + result = SampleMeasurement.Factory.newInstance(); + + result.setSample(sample); + if (sample.getSampleMeasurements() == null) { + sample.setSampleMeasurements(Sets.newHashSet(result)); + } else { + sample.getSampleMeasurements().add(result); + } + result.setQualityFlag(load(QualityFlagImpl.class, enumeration.QUALITY_FLAG_CODE_NOT_QUALIFIED)); + result.setDepartment(sample.getRecorderDepartment()); + result.setPmfm(load(PmfmImpl.class, pmfmId)); + } + measurementPersistenceHelper.setMeasurement(result, caracteristic, value); + return result; + } + +} Modified: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/SpeciesBatchPersistenceServiceImpl.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/SpeciesBatchPersistenceServiceImpl.java 2013-05-07 13:00:45 UTC (rev 924) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/SpeciesBatchPersistenceServiceImpl.java 2013-05-07 13:07:36 UTC (rev 925) @@ -38,7 +38,6 @@ import fr.ifremer.adagio.core.dao.referential.taxon.ReferenceTaxonImpl; import fr.ifremer.tutti.persistence.entities.TuttiBeanFactory; import fr.ifremer.tutti.persistence.entities.TuttiEntities; -import fr.ifremer.tutti.persistence.entities.data.AttachementObjectTypeEnum; import fr.ifremer.tutti.persistence.entities.data.BatchContainer; import fr.ifremer.tutti.persistence.entities.data.SampleCategoryEnum; import fr.ifremer.tutti.persistence.entities.data.SpeciesBatch; @@ -75,9 +74,6 @@ LogFactory.getLog(SpeciesBatchPersistenceServiceImpl.class); @Autowired - protected AttachmentPersistenceService attachmentPersistenceService; - - @Autowired protected ReferentialPersistenceService referentialService; @Resource(name = "batchPersistenceHelper") @@ -202,10 +198,6 @@ Integer batchId = Integer.valueOf(id); batchHelper.removeWithChildren(batchId); - - attachmentPersistenceService.deleteAllAttachment( - AttachementObjectTypeEnum.BATCH, - batchId); } @Override @@ -232,11 +224,6 @@ log.info("Delete child [" + childBatchId + "] of species batch: " + id); } batchHelper.removeWithChildren(childBatchId); - - // delete his attachment (if any) - attachmentPersistenceService.deleteAllAttachment( - AttachementObjectTypeEnum.BATCH, - childBatchId); } } } Modified: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/batch/BatchPersistenceHelper.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/batch/BatchPersistenceHelper.java 2013-05-07 13:00:45 UTC (rev 924) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/batch/BatchPersistenceHelper.java 2013-05-07 13:07:36 UTC (rev 925) @@ -132,11 +132,9 @@ getCurrentSession().flush(); - for (Integer id : ids) { - attachmentPersistenceService.deleteAllAttachment( - AttachementObjectTypeEnum.BATCH, - id); - } + attachmentPersistenceService.deleteAllAttachment( + AttachementObjectTypeEnum.BATCH, + ids.toArray(new Integer[ids.size()])); } public <D extends TuttiEntity> D createSortingBatch(D bean, CatchBatch catchBatch, SortingBatch batch) { @@ -146,11 +144,23 @@ } public void removeWithChildren(Integer batchId) { + List<Integer> ids = catchBatchDao.getAllChildrenIds(batchId); + ids.add(batchId); catchBatchDao.removeWithChildren(batchId); + + attachmentPersistenceService.deleteAllAttachment( + AttachementObjectTypeEnum.BATCH, + ids.toArray(new Integer[ids.size()])); } public void removeWithChildren(Integer batchId, CatchBatch parentCatchBatch) { + List<Integer> ids = catchBatchDao.getAllChildrenIds(batchId); + ids.add(batchId); catchBatchDao.removeWithChildren(batchId, parentCatchBatch); + + attachmentPersistenceService.deleteAllAttachment( + AttachementObjectTypeEnum.BATCH, + ids.toArray(new Integer[ids.size()])); } public void updateSortingBatch(List<SortingBatch> sortingBatchs, CatchBatch parentCatchBatch) {
participants (1)
-
tchemit@users.forge.codelutin.com