Author: tchemit Date: 2013-04-04 10:17:26 +0200 (Thu, 04 Apr 2013) New Revision: 729 Url: http://forge.codelutin.com/projects/tutti/repository/revisions/729 Log: refs #1865: [MACRODECHET] - Gestion de la saisie des Macrod?\195?\169chets Modified: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/MarineLitterBatchPersistenceServiceImpl.java trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/MarineLitterBatchPersistenceServiceWriteTest.java 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-04-04 08:17:10 UTC (rev 728) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/service/MarineLitterBatchPersistenceServiceImpl.java 2013-04-04 08:17:26 UTC (rev 729) @@ -39,7 +39,6 @@ import fr.ifremer.tutti.persistence.entities.TuttiEntities; import fr.ifremer.tutti.persistence.entities.data.BatchContainer; import fr.ifremer.tutti.persistence.entities.data.MarineLitterBatch; -import fr.ifremer.tutti.persistence.entities.data.TuttiBatchEntity; import fr.ifremer.tutti.persistence.entities.referential.Caracteristic; import fr.ifremer.tutti.persistence.entities.referential.CaracteristicQualitativeValue; import fr.ifremer.tutti.persistence.service.batch.BatchPersistenceHelper; @@ -103,21 +102,12 @@ Integer catchBatchId = catchBatchDao.getIdByFishingOperationId(Integer.valueOf(fishingOperationId)); Preconditions.checkNotNull(catchBatchId); - fr.ifremer.adagio.core.dao.data.batch.CatchBatch catchBatch = catchBatchDao.loadFullTree(catchBatchId); + CatchBatch catchBatch = catchBatchDao.loadFullTree(catchBatchId); Preconditions.checkNotNull(catchBatch); - Collection<Batch> catchBatchChilds = catchBatch.getChildBatchs(); + // Get marine litter root batch + SortingBatch vracMarineLitterBatch = getMarineLitterParentBatch(catchBatch); - // Vrac / Marine Litter - SortingBatch vracMarineLitterBatch = catchBatchDao.getSortingBatch( - catchBatchChilds, - BatchPersistenceHelper.BATCH_PMFM_ID, - enumeration.PMFM_ID_SORTED_UNSORTED, - enumeration.QUALITATIVE_HORS_VRAC_ID, - BatchPersistenceHelper.BATCH_PMFM_ID, - enumeration.PMFM_ID_SORTING_TYPE, - enumeration.QUALITATIVE_ID_SORTING_TYPE_MARINE_LITTER); - BatchContainer<MarineLitterBatch> result = new BatchContainer<>(); result.setId(vracMarineLitterBatch.getId()); @@ -144,16 +134,18 @@ public MarineLitterBatch createMarineLitterBatch(MarineLitterBatch bean) { Preconditions.checkNotNull(bean); Preconditions.checkArgument(bean.getId() == null); + Preconditions.checkNotNull(bean.getFishingOperation()); + Preconditions.checkNotNull(bean.getFishingOperation().getId()); Preconditions.checkNotNull(bean.getMarineLitterCategory()); Preconditions.checkNotNull(bean.getMarineLitterSizeCategory()); -// Preconditions.checkNotNull(bean.getNumber()); + Preconditions.checkNotNull(bean.getNumber()); // Preconditions.checkNotNull(bean.getWeight()); getCurrentSession().setFlushMode(FlushMode.COMMIT); // Load full batch tree Integer catchBatchId = catchBatchDao.getIdByFishingOperationId(bean.getFishingOperation().getIdAsInt()); - fr.ifremer.adagio.core.dao.data.batch.CatchBatch catchBatch = catchBatchDao.loadFullTree(catchBatchId); + CatchBatch catchBatch = catchBatchDao.loadFullTree(catchBatchId); SortingBatch batch = SortingBatch.Factory.newInstance(); marineLitterBatchToEntity(bean, batch, catchBatch); @@ -175,7 +167,7 @@ Integer catchBatchId = catchBatchDao.getIdBySortingBatchId(bean.getIdAsInt()); Preconditions.checkNotNull(catchBatchId); - fr.ifremer.adagio.core.dao.data.batch.CatchBatch catchBatch = catchBatchDao.loadFullTree(catchBatchId); + CatchBatch catchBatch = catchBatchDao.loadFullTree(catchBatchId); Preconditions.checkNotNull(catchBatch); getCurrentSession().setFlushMode(FlushMode.COMMIT); @@ -234,7 +226,7 @@ protected void marineLitterBatchToEntity(MarineLitterBatch source, SortingBatch target, - fr.ifremer.adagio.core.dao.data.batch.CatchBatch catchBatch) { + CatchBatch catchBatch) { Preconditions.checkNotNull(source.getFishingOperation()); Preconditions.checkNotNull(source.getFishingOperation().getId()); @@ -258,7 +250,7 @@ // If parent and root need to be set if (target.getId() == null || target.getRootBatch() == null) { - setBatchParents(source, target, catchBatch); + setBatchParents(target, catchBatch); } // RankOrder (initialize once, at creation) @@ -325,9 +317,8 @@ target.setWeight(source.getWeight()); // QualityFlag - String qualityFlag; - qualityFlag = enumeration.QUALITY_FLAG_CODE_NOT_QUALIFIED; - target.setQualityFlag(load(QualityFlagImpl.class, qualityFlag)); + QualityFlagImpl qualityFlag = load(QualityFlagImpl.class, enumeration.QUALITY_FLAG_CODE_NOT_QUALIFIED); + target.setQualityFlag(qualityFlag); // Comments target.setComments(source.getComment()); @@ -342,23 +333,14 @@ if (sortingMeasurements != null) { sortingMeasurements.removeAll(notChangedSortingMeasurements); } - } - public void setBatchParents(TuttiBatchEntity source, - SortingBatch target, - CatchBatch catchBatch) { + public void setBatchParents(SortingBatch target, CatchBatch catchBatch) { Preconditions.checkNotNull(target); - Preconditions.checkNotNull(source.getFishingOperation()); - Preconditions.checkNotNull(source.getFishingOperation().getId()); // Load existing parent and root - SortingBatch parentBatch = catchBatchDao.getSortingBatch( - catchBatch.getChildBatchs(), - BatchPersistenceHelper.BATCH_PMFM_ID, - enumeration.PMFM_ID_SORTING_TYPE, - enumeration.QUALITATIVE_ID_SORTING_TYPE_MARINE_LITTER); + SortingBatch parentBatch = getMarineLitterParentBatch(catchBatch); // Parent Batch @@ -371,4 +353,16 @@ target.setParentBatch(parentBatch); target.setRootBatch(catchBatch); } + + protected SortingBatch getMarineLitterParentBatch(CatchBatch catchBatch) { + SortingBatch parentBatch = catchBatchDao.getSortingBatch( + catchBatch.getChildBatchs(), + BatchPersistenceHelper.BATCH_PMFM_ID, // hors vrac + enumeration.PMFM_ID_SORTED_UNSORTED, + enumeration.QUALITATIVE_HORS_VRAC_ID, + BatchPersistenceHelper.BATCH_PMFM_ID, // marine litter + enumeration.PMFM_ID_SORTING_TYPE, + enumeration.QUALITATIVE_ID_SORTING_TYPE_MARINE_LITTER); + return parentBatch; + } } Modified: trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/MarineLitterBatchPersistenceServiceWriteTest.java =================================================================== --- trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/MarineLitterBatchPersistenceServiceWriteTest.java 2013-04-04 08:17:10 UTC (rev 728) +++ trunk/tutti-persistence/src/test/java/fr/ifremer/tutti/persistence/service/MarineLitterBatchPersistenceServiceWriteTest.java 2013-04-04 08:17:26 UTC (rev 729) @@ -25,18 +25,34 @@ */ import fr.ifremer.tutti.persistence.DatabaseResource; +import fr.ifremer.tutti.persistence.entities.TuttiBeanFactory; +import fr.ifremer.tutti.persistence.entities.data.BatchContainer; +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.MarineLitterBatch; +import fr.ifremer.tutti.persistence.entities.referential.Caracteristic; +import fr.ifremer.tutti.persistence.entities.referential.TuttiLocation; +import org.junit.Assert; import org.junit.Before; import org.junit.ClassRule; -import org.junit.Ignore; import org.junit.Test; +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + /** * To test {@link MarineLitterBatchPersistenceService} for write operation. * * @author tchemit <chemit@codelutin.com> * @since 0.3 */ -@Ignore public class MarineLitterBatchPersistenceServiceWriteTest { @ClassRule @@ -44,22 +60,176 @@ protected MarineLitterBatchPersistenceService service; + /* + * Entities prepared in setUp() : + * */ + protected FishingOperation fishingOperationNoCatchBatch; + + protected FishingOperation fishingOperationWithEmptyBatch; + + protected CatchBatch catchBacth; + + protected Caracteristic marineLitterCategoryCaracteristic; + + protected Caracteristic marineLitterSizeCategoryCaracteristic; + @Before public void setUp() throws Exception { service = TuttiPersistenceServiceLocator.getMarineLitterBatchPersistenceService(); + + + CruisePersistenceService cruiseService = TuttiPersistenceServiceLocator.getCruisePersistenceService(); + CatchBatchPersistenceService catchBatchService = TuttiPersistenceServiceLocator.getCatchBatchPersistenceService(); + FishingOperationPersistenceService fishingOperationService = TuttiPersistenceServiceLocator.getFishingOperationPersistenceService(); + ReferentialPersistenceService referentialService = TuttiPersistenceServiceLocator.getReferentialPersistenceService(); + + marineLitterCategoryCaracteristic = referentialService.getMarineLitterCategoryCaracteristic(); + + marineLitterSizeCategoryCaracteristic = referentialService.getMarineLitterSizeCategoryCaracteristic(); + + Cruise cruise = cruiseService.getCruise(dbResource.getFixtures().cruiseId()); + cruise.setId((String) null); + Calendar calendar = new GregorianCalendar(); + cruise.setBeginDate(calendar.getTime()); + calendar.add(Calendar.MONTH, 1); // add one month + cruise.setEndDate(calendar.getTime()); + List<TuttiLocation> allHarbour = referentialService.getAllHarbour(); + Assert.assertNotNull(allHarbour); + Assert.assertTrue(allHarbour.size() > 1); + cruise.setDepartureLocation(allHarbour.get(0)); + cruise.setReturnLocation(allHarbour.get(1)); + + cruise = cruiseService.createCruise(cruise); + + // Create a first operation, with no cacth batch : to test CatchBatch insert/update : + List<FishingOperation> fishingOperations = fishingOperationService.getAllFishingOperation(dbResource.getFixtures().cruiseId()); + assertNotNull(fishingOperations); + assertTrue(fishingOperations.size() > 0); + fishingOperationNoCatchBatch = fishingOperations.get(0); + fishingOperationNoCatchBatch = fishingOperationService.getFishingOperation(fishingOperationNoCatchBatch.getId()); + fishingOperationNoCatchBatch.setId((String) null); + fishingOperationNoCatchBatch.setCruise(cruise); + calendar.setTime(new Date()); + calendar.set(Calendar.HOUR_OF_DAY, 1); + calendar.set(Calendar.MILLISECOND, 0); + fishingOperationNoCatchBatch.setGearShootingStartDate(calendar.getTime()); + calendar.setTime(new Date()); + calendar.set(Calendar.HOUR_OF_DAY, 10); + calendar.set(Calendar.MILLISECOND, 0); + fishingOperationNoCatchBatch.setGearShootingEndDate(calendar.getTime()); + fishingOperationNoCatchBatch = fishingOperationService.createFishingOperation(fishingOperationNoCatchBatch); + + // Create a second operation, with no cacth batch : to test CatchBatch insert/update : + fishingOperationWithEmptyBatch = fishingOperations.get(1); + fishingOperationWithEmptyBatch = fishingOperationService.getFishingOperation(fishingOperationWithEmptyBatch.getId()); + fishingOperationWithEmptyBatch.setId((String) null); + fishingOperationWithEmptyBatch.setCruise(cruise); + calendar.setTime(new Date()); + calendar.set(Calendar.HOUR_OF_DAY, 11); + calendar.set(Calendar.MILLISECOND, 0); + fishingOperationWithEmptyBatch.setGearShootingStartDate(calendar.getTime()); + calendar.setTime(new Date()); + calendar.set(Calendar.HOUR_OF_DAY, 12); + calendar.set(Calendar.MILLISECOND, 0); + fishingOperationWithEmptyBatch.setGearShootingEndDate(calendar.getTime()); + fishingOperationWithEmptyBatch = fishingOperationService.createFishingOperation(fishingOperationWithEmptyBatch); + + catchBacth = TuttiBeanFactory.newCatchBatch(); + catchBacth.setFishingOperation(fishingOperationWithEmptyBatch); + catchBacth = catchBatchService.createCatchBatch(catchBacth); } @Test public void createMarineLitterBatch(/*MarineLitterBatch bean*/) { + MarineLitterBatch newMarineLitter = + TuttiBeanFactory.newMarineLitterBatch(); + + newMarineLitter.setFishingOperation(fishingOperationWithEmptyBatch); + newMarineLitter.setMarineLitterCategory(marineLitterCategoryCaracteristic.getQualitativeValue(0)); + newMarineLitter.setMarineLitterSizeCategory(marineLitterSizeCategoryCaracteristic.getQualitativeValue(0)); + newMarineLitter.setNumber(10); + + MarineLitterBatch createdBatch = service.createMarineLitterBatch(newMarineLitter); + + assertMarineLitterBatch(newMarineLitter, createdBatch); + + // then reload (for round trip check) + MarineLitterBatch reloadedBatch = getMarineLitterBatch( + newMarineLitter.getFishingOperation().getId(), createdBatch.getId()); + + assertMarineLitterBatch(newMarineLitter, reloadedBatch); } @Test public void saveMarineLitterBatch(/*MarineLitterBatch bean*/) { + MarineLitterBatch newMarineLitter = + TuttiBeanFactory.newMarineLitterBatch(); + + newMarineLitter.setFishingOperation(fishingOperationWithEmptyBatch); + newMarineLitter.setMarineLitterCategory(marineLitterCategoryCaracteristic.getQualitativeValue(0)); + newMarineLitter.setMarineLitterSizeCategory(marineLitterSizeCategoryCaracteristic.getQualitativeValue(0)); + newMarineLitter.setNumber(10); + + MarineLitterBatch createdBatch = service.createMarineLitterBatch(newMarineLitter); + newMarineLitter.setId(createdBatch.getId()); + + // modify some values + newMarineLitter.setComment("A comment"); + newMarineLitter.setWeight(5.f); + newMarineLitter.setMarineLitterCategory(marineLitterCategoryCaracteristic.getQualitativeValue(1)); + MarineLitterBatch savedMarineLitterBatch = service.saveMarineLitterBatch(newMarineLitter); + + assertMarineLitterBatch(newMarineLitter, savedMarineLitterBatch); } @Test public void deleteMarineLitterBatch(/*String id*/) { + MarineLitterBatch newMarineLitter = + TuttiBeanFactory.newMarineLitterBatch(); + + newMarineLitter.setFishingOperation(fishingOperationWithEmptyBatch); + newMarineLitter.setMarineLitterCategory(marineLitterCategoryCaracteristic.getQualitativeValue(0)); + newMarineLitter.setMarineLitterSizeCategory(marineLitterSizeCategoryCaracteristic.getQualitativeValue(0)); + newMarineLitter.setNumber(10); + + MarineLitterBatch createdMarineLitterBatch = service.createMarineLitterBatch(newMarineLitter); + + BatchContainer<MarineLitterBatch> rootMarineLitterBatch = service.getRootMarineLitterBatch(fishingOperationWithEmptyBatch.getId()); + Assert.assertNotNull(rootMarineLitterBatch); + Assert.assertFalse(rootMarineLitterBatch.getChildren().isEmpty()); + + // delete it + + service.deleteMarineLitterBatch(createdMarineLitterBatch.getId()); + + rootMarineLitterBatch = service.getRootMarineLitterBatch(fishingOperationWithEmptyBatch.getId()); + Assert.assertNotNull(rootMarineLitterBatch); + Assert.assertTrue(rootMarineLitterBatch.getChildren().isEmpty()); + } + + protected void assertMarineLitterBatch(MarineLitterBatch expectedBatch, + MarineLitterBatch actualBatch) { + assertNotNull(actualBatch); + assertNotNull(actualBatch.getId()); + assertEquals(expectedBatch.getId(), actualBatch.getId()); + + assertEquals(expectedBatch.getMarineLitterCategory(), actualBatch.getMarineLitterCategory()); + assertEquals(expectedBatch.getMarineLitterSizeCategory(), actualBatch.getMarineLitterSizeCategory()); + assertEquals(expectedBatch.getNumber(), actualBatch.getNumber()); + assertEquals(expectedBatch.getWeight(), actualBatch.getWeight()); + assertEquals(expectedBatch.getComment(), actualBatch.getComment()); + } + + protected MarineLitterBatch getMarineLitterBatch(String fishingOperationId, String id) { + BatchContainer<MarineLitterBatch> rootMarineLitterBatch = service.getRootMarineLitterBatch(fishingOperationId); + for (MarineLitterBatch marineLitterBatch : rootMarineLitterBatch.getChildren()) { + if (id.equals(marineLitterBatch.getId())) { + return marineLitterBatch; + } + } + return null; + } }