r525 - in trunk: tutti-persistence/src/main/java/fr/ifremer/tutti/persistence tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/entities/data tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service tutti-persistence/src/main/resources tutti-persistence/src/main/xmi tutti-persistence/src/test/java/fr/ifremer/tutti/persistence tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service tutti-service/src/main/java/fr/ifremer/tutti/service
Author: tchemit Date: 2013-03-04 10:10:58 +0100 (Mon, 04 Mar 2013) New Revision: 525 Url: http://forge.codelutin.com/projects/tutti/repository/revisions/525 Log: fixes #2073: [TECH] Mise en place de la persistence des pi?\195?\168ces-jointes Added: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/entities/data/AttachementObjectTypeEnum.java trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/TuttiRunner.java trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/AttachmentPersistenceServiceWriteTest.java Modified: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/TuttiPersistence.java trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/TuttiPersistenceImpl.java trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/TuttiPersistenceNoDbImpl.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/TuttiEnumerationFile.java trunk/tutti-persistence/src/main/resources/queries-override.hbm.xml trunk/tutti-persistence/src/main/resources/tutti-db-enumerations.properties trunk/tutti-persistence/src/main/xmi/tutti-persistence.zargo trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/DatabaseResource.java trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/PersistenceService.java Modified: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/TuttiPersistence.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/TuttiPersistence.java 2013-03-04 08:45:29 UTC (rev 524) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/TuttiPersistence.java 2013-03-04 09:10:58 UTC (rev 525) @@ -261,10 +261,11 @@ /** * Get all attachments for the given object {@code objectId}. * + * * @param objectId id of the object * @return list of all attachments for the given {@code objectId}. */ - List<Attachment> getAllAttachments(String objectId); + List<Attachment> getAllAttachments(Integer objectId); /** * Get the file of the given {@code attachmentId}. Modified: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/TuttiPersistenceImpl.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/TuttiPersistenceImpl.java 2013-03-04 08:45:29 UTC (rev 524) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/TuttiPersistenceImpl.java 2013-03-04 09:10:58 UTC (rev 525) @@ -348,7 +348,7 @@ //------------------------------------------------------------------------// @Override - public List<Attachment> getAllAttachments(String objectId) { + public List<Attachment> getAllAttachments(Integer objectId) { return attachmentService.getAllAttachments(objectId); } Modified: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/TuttiPersistenceNoDbImpl.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/TuttiPersistenceNoDbImpl.java 2013-03-04 08:45:29 UTC (rev 524) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/TuttiPersistenceNoDbImpl.java 2013-03-04 09:10:58 UTC (rev 525) @@ -489,7 +489,7 @@ } @Override - public List<Attachment> getAllAttachments(String objectId) { + public List<Attachment> getAllAttachments(Integer objectId) { throw new RuntimeException("method not implemented"); } Added: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/entities/data/AttachementObjectTypeEnum.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/entities/data/AttachementObjectTypeEnum.java (rev 0) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/entities/data/AttachementObjectTypeEnum.java 2013-03-04 09:10:58 UTC (rev 525) @@ -0,0 +1,86 @@ +package fr.ifremer.tutti.persistence.entities.data; + +/* + * #%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.base.Preconditions; +import fr.ifremer.adagio.core.dao.referential.ObjectType; +import fr.ifremer.tutti.persistence.service.TuttiEnumerationFile; + +/** + * Define all usable {@link ObjectType} in {@link Attachment}. + * + * <strong>Note: </strong>This enumeration implements {@link TuttiEnumerationFile.TuttiEnumerable}, + * so all values must be synched to the enumeration file and before usage we + * must be invoke the {@link TuttiEnumerationFile#init()} method. + * + * @author tchemit <chemit@codelutin.com> + * @see Attachment#getObjectType() + * @since 1.0.2 + */ +public enum AttachementObjectTypeEnum implements TuttiEnumerationFile.TuttiEnumerable<String> { + + SCIENTIFIC_CRUISE("OBJECT_TYPE_SCIENTIFIC_CRUISE"), + OPERATION("OBJECT_TYPE_OPERATION"), + CATCH_BATCH("OBJECT_TYPE_CATCH_BATCH"), + SAMPLE("OBJECT_TYPE_SAMPLE"); + + private final String fieldName; + + private String fieldValue; + + private boolean init; + + AttachementObjectTypeEnum(String fieldName) { + this.fieldName = fieldName; + } + + @Override + public String getFieldName() { + return fieldName; + } + + @Override + public String getFieldValue() { + Preconditions.checkState( + isInit(), + "Enumeration " + getClass() + " was not init!"); + return fieldValue; + } + + @Override + public void setFieldValue(Object fieldValue) { + this.fieldValue = String.valueOf(fieldValue); + } + + @Override + public boolean isInit() { + return init; + } + + @Override + public void setInit(boolean init) { + this.init = init; + } +} Property changes on: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/entities/data/AttachementObjectTypeEnum.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/AttachmentPersistenceService.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/AttachmentPersistenceService.java 2013-03-04 08:45:29 UTC (rev 524) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/AttachmentPersistenceService.java 2013-03-04 09:10:58 UTC (rev 525) @@ -46,7 +46,7 @@ * @param objectId id of the object * @return list of all attachments for the given {@code objectId}. */ - List<Attachment> getAllAttachments(String objectId); + List<Attachment> getAllAttachments(Integer objectId); /** * Get the file of the given {@code attachmentId}. 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-03-04 08:45:29 UTC (rev 524) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/AttachmentPersistenceServiceImpl.java 2013-03-04 09:10:58 UTC (rev 525) @@ -32,12 +32,12 @@ import fr.ifremer.adagio.core.dao.referential.QualityFlagImpl; import fr.ifremer.adagio.core.dao.referential.pmfm.PmfmImpl; import fr.ifremer.tutti.TuttiIOUtil; +import fr.ifremer.tutti.persistence.entities.data.AttachementObjectTypeEnum; import fr.ifremer.tutti.persistence.entities.data.Attachment; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.hibernate.type.IntegerType; -import org.hibernate.type.StringType; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataRetrievalFailureException; import org.springframework.dao.InvalidDataAccessResourceUsageException; @@ -62,7 +62,7 @@ LogFactory.getLog(AttachmentPersistenceServiceImpl.class); protected static final String ATTACHMENT_PATH_FORMAT = - "%1$s/OBJ-%2$s/%1$s-%2$s-%3$s.%4$s"; + "%1$s/OBJ%2$s/%1$s-OBJ%2$s-%3$s.%4$s"; @Autowired protected ReferentialPersistenceService referentialService; @@ -87,10 +87,11 @@ //------------------------------------------------------------------------// @Override - public List<Attachment> getAllAttachments(String objectId) { + public List<Attachment> getAllAttachments(Integer objectId) { Iterator<Object[]> list = queryList( "allAttachment", - "objectId", StringType.INSTANCE, objectId); + "objectId", IntegerType.INSTANCE, objectId, + "pmfmId", IntegerType.INSTANCE, enumeration.PMFM_ID_MEASUREMENT_FILE); List<Attachment> result = Lists.newArrayList(); while (list.hasNext()) { @@ -106,7 +107,7 @@ public File getAttachmentFile(String attachmentId) { Object[] source = queryUnique( "attachment", - "attachmetId", IntegerType.INSTANCE, Integer.valueOf(attachmentId)); + "attachmentId", IntegerType.INSTANCE, Integer.valueOf(attachmentId)); if (source == null) { throw new DataRetrievalFailureException( @@ -123,15 +124,12 @@ public Attachment createAttachment(Attachment attachment, File file) { Preconditions.checkNotNull(attachment); Preconditions.checkNotNull(attachment.getObjectType()); - Preconditions.checkNotNull(attachment.getObjectType().getId()); + Preconditions.checkNotNull(attachment.getObjectId()); Preconditions.checkArgument( attachment.getId() == null, "Attachment 'id' must be null to call createAttachment()."); Preconditions.checkNotNull(file); - // Use first a fake filePath (we don't know the id of attachment entity) - attachment.setPath("FAKE-" + System.nanoTime()); - // Create measurement file MeasurementFile measurementFile = MeasurementFile.Factory.newInstance(); @@ -142,8 +140,14 @@ measurementFile.setQualityFlag(load(QualityFlagImpl.class, enumeration.QUALITY_FLAG_CODE_NOT_QUALIFIED)); // set objectType - measurementFile.setObjectType(load(ObjectTypeImpl.class, attachment.getObjectType().getId())); + measurementFile.setObjectType(load(ObjectTypeImpl.class, attachment.getObjectType().getFieldValue())); + // set objectId + measurementFile.setObjectId(attachment.getObjectId()); + + // Use first a fake filePath (we don't know the id of attachment entity) + measurementFile.setPath("FAKE-" + System.nanoTime()); + // copy our property attachmentToEntity(attachment, measurementFile); measurementFileDao.create(measurementFile); @@ -177,15 +181,17 @@ file, targetFile, "Could not copy attachment file " + file + " to " + targetFile); - return null; + return attachment; } @Override public Attachment saveAttachment(Attachment attachment) { Preconditions.checkNotNull(attachment); Preconditions.checkNotNull(attachment.getObjectType()); - Preconditions.checkNotNull(attachment.getObjectType().getId()); - Preconditions.checkNotNull(attachment.getId(), "Attachment 'id' must not be null or empty to be saved."); + Preconditions.checkNotNull(attachment.getObjectId()); + Preconditions.checkNotNull( + attachment.getId(), + "Attachment 'id' must not be null or empty to be saved."); MeasurementFile measurementFile = measurementFileDao.load(Integer.valueOf(attachment.getId())); if (measurementFile == null) { @@ -193,13 +199,21 @@ } // can't change the objectType - if (ObjectUtils.notEqual(attachment.getObjectType().getId(), - measurementFile.getObjectType().getCode())) { + String oldObjectTypeCode = measurementFile.getObjectType().getCode(); + if (ObjectUtils.notEqual(attachment.getObjectType().getFieldValue(), + oldObjectTypeCode)) { throw new InvalidDataAccessResourceUsageException( - "Can't change objectId, was before " + - measurementFile.getObjectType().getCode()); + "Can't change objectType, was before " + oldObjectTypeCode); } + // can't change either objectId + Integer oldObjectId = measurementFile.getObjectId(); + if (ObjectUtils.notEqual(attachment.getObjectId(), + oldObjectId)) { + throw new InvalidDataAccessResourceUsageException( + "Can't change objectId, was before " + oldObjectId); + } + attachmentToEntity(attachment, measurementFile); measurementFileDao.update(measurementFile); return attachment; @@ -210,7 +224,7 @@ Integer id = Integer.valueOf(attachmentId); Object[] source = queryUnique( "attachment", - "attachmetId", IntegerType.INSTANCE, id); + "attachmentId", IntegerType.INSTANCE, id); if (source == null) { throw new DataRetrievalFailureException( @@ -234,10 +248,12 @@ protected void loadAttachment(Object[] source, Attachment target) { - target.setId(String.valueOf(source[0])); - target.setPath((String) source[1]); - target.setName((String) source[2]); - target.setComment((String) source[3]); + target.setObjectType(AttachementObjectTypeEnum.valueOf((String) source[0])); + target.setObjectId((Integer) source[1]); + target.setId(String.valueOf(source[2])); + target.setPath((String) source[3]); + target.setName((String) source[4]); + target.setComment((String) source[5]); } protected void attachmentToEntity(Attachment attachment, Modified: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/TuttiEnumerationFile.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/TuttiEnumerationFile.java 2013-03-04 08:45:29 UTC (rev 524) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/TuttiEnumerationFile.java 2013-03-04 09:10:58 UTC (rev 525) @@ -25,10 +25,13 @@ */ import com.google.common.base.Preconditions; +import com.google.common.collect.Maps; +import fr.ifremer.tutti.persistence.entities.data.AttachementObjectTypeEnum; import org.springframework.beans.factory.annotation.Value; import org.springframework.util.ReflectionUtils; import java.lang.reflect.Field; +import java.util.Map; /** * Contains all constants usable via the enumeration file. @@ -189,9 +192,8 @@ public final Integer PMFM_ID_SURVEY_PART = null; @Value("${PmfmId.MEASUREMENT_FILE}") - public final Integer PMFM_ID_MEASUREMENT_FILE= null; + public final Integer PMFM_ID_MEASUREMENT_FILE = null; - @Value("${ProgramCode.SCIENTIFIC_CRUISE_PREFIX}") public final String PROGRAM_CODE_SCIENTIFIC_CRUISE_PREFIX = null; @@ -204,8 +206,45 @@ @Value("${ReferenceTaxonId.INERT}") public final Integer REFERENCE_TAXON_ID_INERT = null; + @Value("${ObjectTypeCode.SCIENTIFIC_CRUISE}") + public final String OBJECT_TYPE_SCIENTIFIC_CRUISE = null; + + @Value("${ObjectTypeCode.CATCH_BATCH}") + public final String OBJECT_TYPE_CATCH_BATCH = null; + + @Value("${ObjectTypeCode.OPERATION}") + public final String OBJECT_TYPE_OPERATION = null; + + @Value("${ObjectTypeCode.SAMPLE}") + public final String OBJECT_TYPE_SAMPLE = null; + + + /** + * Contract to place on enumeration that must be synched to field inside this class. + * <p/> + * {@link #getFieldValue()} will be filled when {@link #init()} method is + * invoked and then the {@link #isInit()} will return true. + * + * @param <O> type of fieldValue + * @since 1.0.2 + */ + public interface TuttiEnumerable<O> { + + String getFieldName(); + + O getFieldValue(); + + void setFieldValue(Object fieldValue); + + boolean isInit(); + + void setInit(boolean init); + } + public void init() { + Map<String, Object> annotatedFieldValues = Maps.newTreeMap(); + Field[] declaredFields = getClass().getDeclaredFields(); for (Field declaredField : declaredFields) { Value annotation = declaredField.getAnnotation(Value.class); @@ -213,11 +252,33 @@ // check on that field that his value is here Object fieldValue = ReflectionUtils.getField(declaredField, this); + String fieldName = declaredField.getName(); Preconditions.checkNotNull( fieldValue, annotation.value() + " constant not found (field " + - declaredField.getName() + ")"); + fieldName + ")"); + annotatedFieldValues.put(fieldName, fieldValue); } } + + // init enums + initEnum(AttachementObjectTypeEnum.class, annotatedFieldValues); } + + protected <O, E extends Enum<E> & TuttiEnumerable<O>> void initEnum( + Class<E> enumType, + Map<String, Object> annotatedFieldValues) { + for (E e : enumType.getEnumConstants()) { + if (!e.isInit()) { + String fieldName = e.getFieldName(); + Object field = annotatedFieldValues.get(fieldName); + Preconditions.checkNotNull( + field, + "Could not find field " + fieldName + ")"); + e.setFieldValue(field); + e.setInit(true); + } + + } + } } Modified: trunk/tutti-persistence/src/main/resources/queries-override.hbm.xml =================================================================== --- trunk/tutti-persistence/src/main/resources/queries-override.hbm.xml 2013-03-04 08:45:29 UTC (rev 524) +++ trunk/tutti-persistence/src/main/resources/queries-override.hbm.xml 2013-03-04 09:10:58 UTC (rev 525) @@ -358,6 +358,8 @@ <query name="allAttachment"> <![CDATA[ SELECT + m.objectType.code as attachmentObjectType, + m.objectId AS attachmentObjectId, m.id AS attachmentId, m.path AS attachmentPath, m.name AS attachmentName, @@ -376,6 +378,8 @@ <query name="attachment"> <![CDATA[ SELECT + m.objectType.code as attachmentObjectType, + m.objectId AS attachmentObjectId, m.id AS attachmentId, m.path AS attachmentPath, m.name AS attachmentName, @@ -383,9 +387,9 @@ FROM MeasurementFileImpl m WHERE - m.id = :id + m.id = :attachmentId ]]> - <query-param name="id" type="java.lang.Integer"/> + <query-param name="attachmentId" type="java.lang.Integer"/> </query> <!-- ===================================================================== --> Modified: trunk/tutti-persistence/src/main/resources/tutti-db-enumerations.properties =================================================================== --- trunk/tutti-persistence/src/main/resources/tutti-db-enumerations.properties 2013-03-04 08:45:29 UTC (rev 524) +++ trunk/tutti-persistence/src/main/resources/tutti-db-enumerations.properties 2013-03-04 09:10:58 UTC (rev 525) @@ -391,4 +391,10 @@ ReferenceTaxonId.LIFE=7637 #Reference taxon correspondant à "Non Biota" (ou "Non Vie" - matière inerte) # TODO : a creer dans la base harmonie et achanger ! -ReferenceTaxonId.INERT=1 \ No newline at end of file +ReferenceTaxonId.INERT=1 + +# TODO : A Creer (dans les énumerations Allegro) +ObjectTypeCode.SCIENTIFIC_CRUISE=SCIENTIFIC_CRUISE +ObjectTypeCode.OPERATION=OPERATION +ObjectTypeCode.CATCH_BATCH=CATCH_BATCH +ObjectTypeCode.SAMPLE=SAMPLE \ No newline at end of file Modified: trunk/tutti-persistence/src/main/xmi/tutti-persistence.zargo =================================================================== (Binary files differ) 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-03-04 08:45:29 UTC (rev 524) +++ trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/DatabaseResource.java 2013-03-04 09:10:58 UTC (rev 525) @@ -200,6 +200,8 @@ copyDb(config.getDbDirectory(), !writeDb, null); } + toDetroy.add(config.getDbAttachmentDirectory()); + // load db config File dbConfig = new File(config.getDbDirectory(), config.getDbName() + ".properties"); Properties p = new Properties(); Added: trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/TuttiRunner.java =================================================================== --- trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/TuttiRunner.java (rev 0) +++ trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/TuttiRunner.java 2013-03-04 09:10:58 UTC (rev 525) @@ -0,0 +1,64 @@ +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.junit.runner.notification.RunNotifier; +import org.junit.runners.BlockJUnit4ClassRunner; +import org.junit.runners.model.FrameworkMethod; +import org.junit.runners.model.InitializationError; + +/** + * To use {@link TuttiRunListener}. + * <p/> + * Add this on top of your test (when run in IDE, otherise maven already add + * the listener for you, so no need of the runner). + * <pre> + * \@RunWith(TuttiRunner.class) + * </pre> + * + * @author tchemit <chemit@codelutin.com> + * @since 1.0.2 + */ + +public class TuttiRunner extends BlockJUnit4ClassRunner { + + public static final TuttiRunListener TUTTI_RUN_LISTENER = new TuttiRunListener(); + + /** + * Creates a BlockJUnit4ClassRunner to run {@code klass} + * + * @throws InitializationError if the test class is malformed. + */ + public TuttiRunner(Class<?> klass) throws InitializationError { + super(klass); + } + + @Override + protected void runChild(FrameworkMethod method, RunNotifier notifier) { + notifier.removeListener(TUTTI_RUN_LISTENER); + notifier.addListener(TUTTI_RUN_LISTENER); + super.runChild(method, notifier); + } +} Property changes on: trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/TuttiRunner.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/AttachmentPersistenceServiceWriteTest.java =================================================================== --- trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/AttachmentPersistenceServiceWriteTest.java (rev 0) +++ trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/AttachmentPersistenceServiceWriteTest.java 2013-03-04 09:10:58 UTC (rev 525) @@ -0,0 +1,144 @@ +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 fr.ifremer.tutti.persistence.DatabaseResource; +import fr.ifremer.tutti.persistence.TuttiRunner; +import fr.ifremer.tutti.persistence.entities.data.AttachementObjectTypeEnum; +import fr.ifremer.tutti.persistence.entities.data.Attachment; +import org.junit.Assert; +import org.junit.Before; +import org.junit.ClassRule; +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.io.File; +import java.util.List; + +/** + * To test {@link AttachmentPersistenceService} for write operation. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.0.2 + */ +@RunWith(TuttiRunner.class) +public class AttachmentPersistenceServiceWriteTest { + + @ClassRule + public static final DatabaseResource dbResource = DatabaseResource.writeDb(); + + protected AttachmentPersistenceService service; + + @Before + public void setUp() throws Exception { + service = TuttiPersistenceServiceLocator.getAttachmentPersistenceService(); + } + + @Test + public void createAndSaveAttachment() { + + List<Attachment> allAttachments; + + Integer cruiseId = Integer.valueOf(dbResource.getFixtures().cruiseId()); + + allAttachments = service.getAllAttachments(cruiseId); + Assert.assertTrue(allAttachments.isEmpty()); + + // + // create attachment + // + + Attachment attachment = new Attachment(); + attachment.setObjectType(AttachementObjectTypeEnum.SCIENTIFIC_CRUISE); + attachment.setObjectId(cruiseId); + long buildTime = System.nanoTime(); + attachment.setName("AttachmentName-" + buildTime); + attachment.setComment("AttachmentComment-" + buildTime); + File fileToAttach = dbResource.getConfig().getDbConfigurationPath(); + Attachment savedAttachment = service.createAttachment(attachment, fileToAttach); + assertAttachment(attachment, savedAttachment); + + // + // get Attachment file + // + + File attachmentFile = service.getAttachmentFile(attachment.getId()); + Assert.assertNotNull(attachmentFile); + Assert.assertTrue(attachmentFile.exists()); + Assert.assertEquals(fileToAttach.length(), attachmentFile.length()); + + // + // reload it + // + + allAttachments = service.getAllAttachments(cruiseId); + Assert.assertFalse(allAttachments.isEmpty()); + Assert.assertEquals(1, allAttachments.size()); + + Attachment reloadedAttachment = allAttachments.get(0); + assertAttachment(attachment, reloadedAttachment); + + // + // modifiy it and save it + // + + attachment.setName(attachment.getName() + "-2"); + attachment.setComment(attachment.getComment() + "-2"); + + savedAttachment = service.saveAttachment(attachment); + assertAttachment(attachment, savedAttachment); + + // + // reload it + // + + allAttachments = service.getAllAttachments(cruiseId); + Assert.assertFalse(allAttachments.isEmpty()); + Assert.assertEquals(1, allAttachments.size()); + + reloadedAttachment = allAttachments.get(0); + assertAttachment(attachment, reloadedAttachment); + + // + // delete it + // + + service.deleteAttachment(attachment.getId()); + allAttachments = service.getAllAttachments(cruiseId); + Assert.assertTrue(allAttachments.isEmpty()); + Assert.assertFalse(attachmentFile.exists()); + } + + protected void assertAttachment(Attachment expected, Attachment actual) { + Assert.assertNotNull(expected); + Assert.assertNotNull(actual); + Assert.assertEquals(expected.getId(), actual.getId()); + Assert.assertEquals(expected.getPath(), actual.getPath()); + Assert.assertEquals(expected.getName(), actual.getName()); + Assert.assertEquals(expected.getComment(), actual.getComment()); + Assert.assertEquals(expected.getObjectType(), actual.getObjectType()); + Assert.assertEquals(expected.getObjectId(), actual.getObjectId()); + } +} Property changes on: trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/AttachmentPersistenceServiceWriteTest.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/PersistenceService.java =================================================================== --- trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/PersistenceService.java 2013-03-04 08:45:29 UTC (rev 524) +++ trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/PersistenceService.java 2013-03-04 09:10:58 UTC (rev 525) @@ -373,7 +373,7 @@ //------------------------------------------------------------------------// @Override - public List<Attachment> getAllAttachments(String objectId) { + public List<Attachment> getAllAttachments(Integer objectId) { return driver.getAllAttachments(objectId); }
participants (1)
-
tchemit@users.forge.codelutin.com