This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository wao. See http://git.codelutin.com/wao.git commit 50588e180aed02fe197bbb2242e5f0e771e6afb6 Author: Brendan Le Ny <bleny@codelutin.com> Date: Fri Dec 19 11:44:13 2014 +0100 On adapte la génération de la liste des modifications pour considérer les changements sur une ligne scléro --- .../fr/ifremer/wao/entity/SampleRowLogImpl.java | 94 ++++++++++++++++++---- .../SclerochronologySamplingPlanServiceTest.java | 53 +++++++++++- .../ifremer/wao/services/service/WaoFixtures.java | 28 ++++--- 3 files changed, 149 insertions(+), 26 deletions(-) diff --git a/wao-persistence/src/main/java/fr/ifremer/wao/entity/SampleRowLogImpl.java b/wao-persistence/src/main/java/fr/ifremer/wao/entity/SampleRowLogImpl.java index 3253b65..bd39c1b 100644 --- a/wao-persistence/src/main/java/fr/ifremer/wao/entity/SampleRowLogImpl.java +++ b/wao-persistence/src/main/java/fr/ifremer/wao/entity/SampleRowLogImpl.java @@ -25,6 +25,8 @@ package fr.ifremer.wao.entity; import com.google.common.collect.Maps; +import fr.ifremer.wao.WaoUtils; +import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; import org.nuiton.topia.persistence.TopiaEntities; import org.nuiton.util.DateUtil; @@ -36,6 +38,7 @@ import java.util.Collection; import java.util.Collections; import java.util.Date; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.Set; @@ -48,6 +51,8 @@ import java.util.Set; */ public class SampleRowLogImpl extends SampleRowLogAbstract { + protected Locale locale = Locale.FRANCE; + // TODO brendan 29/04/14 i18n, i18n everywhere ! protected void addChange(String text) { String log = getLogText(); @@ -188,18 +193,18 @@ public class SampleRowLogImpl extends SampleRowLogAbstract { } } - protected void compareCompanies() { - Organisation oldCompany = oldRow == null ? null : oldRow.getOrganisation(); - Organisation newCompany = newRow.getOrganisation(); - if (oldCompany != null) { - String msg = "La ligne n'est plus associée à la société " + oldCompany.getName(); - if (newCompany != null && !newCompany.equals(oldCompany)) { - addChange(msg + " mais à la société " + newCompany.getName()); - } else if (newCompany == null) { + protected void compareOrganisations() { + Organisation oldOrganisation = oldRow == null ? null : oldRow.getOrganisation(); + Organisation newOrganisation = newRow.getOrganisation(); + if (oldOrganisation != null) { + String msg = "La ligne n'est plus associée à " + oldOrganisation.getFullName(); + if (newOrganisation != null && !newOrganisation.equals(oldOrganisation)) { + addChange(msg + " mais à la société " + newOrganisation.getName()); + } else if (newOrganisation == null) { addChange(msg); } - } else if (newCompany != null) { - addChange("La ligne est désormais associée à la société " + newCompany.getName()); + } else if (newOrganisation != null) { + addChange("La ligne est désormais associée à " + newOrganisation.getFullName()); } } @@ -397,6 +402,18 @@ public class SampleRowLogImpl extends SampleRowLogAbstract { } } + protected void compareSclerochronologySamplingContextInfos() { + String oldSclerochronologySamplingContextInfo = oldRow == null ? null : oldRow.getSclerochronologySamplingContextInfo(); + String newSclerochronologySamplingContextInfo = newRow.getSclerochronologySamplingContextInfo(); + if (!StringUtils.isEmpty(oldSclerochronologySamplingContextInfo)) { + if (!oldSclerochronologySamplingContextInfo.equals(newSclerochronologySamplingContextInfo)) { + addChange("Le complément d'information sur le contexte est passé de « " + oldSclerochronologySamplingContextInfo + " » à « " + newSclerochronologySamplingContextInfo + " »"); + } + } else if (!StringUtils.isEmpty(newSclerochronologySamplingContextInfo)) { + addChange("Le complément d'information sur le contexte est désormais « " + newSclerochronologySamplingContextInfo + " »"); + } + } + /** Compare a sample row before and after its modifications and generate * a differences summary and save it by setting log text. */ @@ -421,7 +438,7 @@ public class SampleRowLogImpl extends SampleRowLogAbstract { this.newRow = newRow; ObsProgram obsProgram = newRow.getObsProgram(); - if (ObsProgram.OBSMER.equals(obsProgram)) { + if (obsProgram.isObsMer()) { compareNbObservers(); compareAverageTidesTimes(); comparePrograms(); @@ -430,8 +447,7 @@ public class SampleRowLogImpl extends SampleRowLogAbstract { compareSampleMonths(); compareEligibleBoats(); compareFishingZones(); - } - if (ObsProgram.OBSVENTE.equals(obsProgram)) { + } else if (obsProgram.isObsVente()) { compareTerrestrialDistricts(); comparePrograms(); comparePeriods(); @@ -440,10 +456,60 @@ public class SampleRowLogImpl extends SampleRowLogAbstract { compareEligibleBoats(); compareFishingZones(); compareAverageObservationsCount(); + } else if (obsProgram.isSclerochronology()) { + comparePrograms(); + comparePeriods(); + compareSampleMonths(); + compareFishingZones(); + compareIndividualMeasurementStrategies(); + compareSclerochronologySamplingContexts(); + compareSclerochronologySamplingContextInfos(); + compareSpecies(); + } else { + throw new UnsupportedOperationException("not for " + obsProgram); } - compareCompanies(); + compareOrganisations(); this.oldRow = null; this.newRow = null; } + + protected void compareSclerochronologySamplingContexts() { + SclerochronologySamplingContext oldSclerochronologySamplingContext = oldRow.getSclerochronologySamplingContext(); + SclerochronologySamplingContext newSclerochronologySamplingContext = newRow.getSclerochronologySamplingContext(); + if (ObjectUtils.notEqual(oldSclerochronologySamplingContext, newSclerochronologySamplingContext)) { + String change = String.format( + "Le contexte est passé de %s à %s", + WaoUtils.l(locale, oldSclerochronologySamplingContext), + WaoUtils.l(locale, newSclerochronologySamplingContext) + ); + addChange(change); + } + } + + protected void compareIndividualMeasurementStrategies() { + IndividualMeasurementStrategy oldIndividualMeasurementStrategy = oldRow.getIndividualMeasurementStrategy(); + IndividualMeasurementStrategy newIndividualMeasurementStrategy = newRow.getIndividualMeasurementStrategy(); + if (ObjectUtils.notEqual(oldIndividualMeasurementStrategy, newIndividualMeasurementStrategy)) { + String change = String.format( + "Les caractéristiques à mesurer sont passées de %s à %s", + WaoUtils.l(locale, oldIndividualMeasurementStrategy), + WaoUtils.l(locale, newIndividualMeasurementStrategy) + ); + addChange(change); + } + } + + protected void compareSpecies() { + Species oldSpecies = oldRow.getSpecies(); + Species newSpecies = newRow.getSpecies(); + if (ObjectUtils.notEqual(oldSpecies, newSpecies)) { + String change = String.format( + "L'espèce est passée de %s à %s", + oldSpecies.getVernacularName(), + newSpecies.getVernacularName() + ); + addChange(change); + } + } } diff --git a/wao-services/src/test/java/fr/ifremer/wao/services/service/SclerochronologySamplingPlanServiceTest.java b/wao-services/src/test/java/fr/ifremer/wao/services/service/SclerochronologySamplingPlanServiceTest.java index 24b9d68..a729ace 100644 --- a/wao-services/src/test/java/fr/ifremer/wao/services/service/SclerochronologySamplingPlanServiceTest.java +++ b/wao-services/src/test/java/fr/ifremer/wao/services/service/SclerochronologySamplingPlanServiceTest.java @@ -1,8 +1,13 @@ package fr.ifremer.wao.services.service; +import com.google.common.base.Optional; +import com.google.common.collect.Iterables; import fr.ifremer.wao.SampleRowsFilter; +import fr.ifremer.wao.entity.SampleRow; +import fr.ifremer.wao.entity.SampleRowLog; import fr.ifremer.wao.services.AbstractWaoServiceTest; import fr.ifremer.wao.services.AuthenticatedWaoUser; +import fr.ifremer.wao.services.service.administration.UnknownBoatRegistrationCodesException; import org.apache.commons.io.IOUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -27,6 +32,7 @@ public class SclerochronologySamplingPlanServiceTest extends AbstractWaoServiceT @Before public void setUp() { + getApplicationContext().setDate(DateUtil.createDate(0, 30, 12, 15, 6, 2015)); service = newService(SclerochronologySamplingPlanService.class); fixtures = new SclerochronologyFixtures(newServiceContext()); } @@ -53,7 +59,6 @@ public class SclerochronologySamplingPlanServiceTest extends AbstractWaoServiceT @Test public void newSampleRowsFilterPeriodMustBeOnJanuaryToDecemberOfTheCurrentYear() { - applicationContext.setDate(DateUtil.createDate(15, 1, 2015)); fixtures.samplingPlan(); SampleRowsFilter filter = service.newSampleRowsFilter(fixtures.admin()); Assert.assertEquals(DateUtil.createDate(1, 1, 2015), filter.getPeriodFrom()); @@ -62,7 +67,6 @@ public class SclerochronologySamplingPlanServiceTest extends AbstractWaoServiceT @Test public void samplingPlan() { - applicationContext.setDate(DateUtil.createDate(15, 1, 2015)); fixtures.samplingPlan(); AuthenticatedWaoUser admin = fixtures.admin(); SampleRowsFilter filter = service.newSampleRowsFilter(admin); @@ -72,4 +76,49 @@ public class SclerochronologySamplingPlanServiceTest extends AbstractWaoServiceT Assert.assertEquals(3, samplingPlan.getFilterValues().getSclerochronologySamplingContexts().size()); Assert.assertEquals(2, samplingPlan.getFilterValues().getOrganisations().size()); } + + @Test + public void sampleRowModificationsAreLogged() { + + // dans le plan, on prend une ligne du plan + fixtures.samplingPlan(); + String sampleRowCode = "2015_S0001"; + String sampleRowId = service.getSampleRowDao().forCodeEquals(sampleRowCode).findUnique().getTopiaId(); + + // on la modifie en changeant diverses propriétés + UpdateSampleRowCommand updateSampleRowCommand = service.newUpdateSampleRowCommand(fixtures.admin(), Optional.of(sampleRowId)); + updateSampleRowCommand.getSampleRow().setSpecies( + Iterables.get(updateSampleRowCommand.getAllSpecies().values(), 2)); + updateSampleRowCommand.getSampleRow().setIndividualMeasurementStrategy( + Iterables.get(updateSampleRowCommand.getAllIndividualMeasurementStrategies().keySet(), 0)); + updateSampleRowCommand.getSampleRow().setSclerochronologySamplingContext( + Iterables.get(updateSampleRowCommand.getAllSclerochronologySamplingContexts().keySet(), 2)); + updateSampleRowCommand.getSampleRow().setSclerochronologySamplingContextInfo("note"); + updateSampleRowCommand.getSampleRowLog().setComment("test de modification"); + + try { + service.preValidate(fixtures.admin(), updateSampleRowCommand); + service.validate(fixtures.admin(), updateSampleRowCommand); + service.save(updateSampleRowCommand); + } catch (UnknownBoatRegistrationCodesException | SampleRowCodeMustBeUniqueException | SampleRowValidationException e) { + if (log.isDebugEnabled()) { + log.debug("unexpected exception", e); + } + Assert.fail("unexpected exception"); + } + + // on vérifie que les différentes modifications sont bien journalisées + SampleRow sampleRow = service.getSampleRowDao().forCodeEquals(sampleRowCode).findUnique(); + SampleRowLog sampleRowLog = Iterables.getOnlyElement(sampleRow.getSampleRowLog()); + + if (log.isDebugEnabled()) { + log.debug("log text is " + sampleRowLog.getLogText()); + } + + Assert.assertTrue(sampleRowLog.getLogText().contains("note")); + Assert.assertTrue(sampleRowLog.getLogText().contains("Échantillonnage à terre")); + Assert.assertTrue(sampleRowLog.getLogText().contains("Poids/Taille")); + Assert.assertTrue(sampleRowLog.getLogText().contains("Merlu")); + + } } \ No newline at end of file diff --git a/wao-services/src/test/java/fr/ifremer/wao/services/service/WaoFixtures.java b/wao-services/src/test/java/fr/ifremer/wao/services/service/WaoFixtures.java index 63e1885..7279bd3 100644 --- a/wao-services/src/test/java/fr/ifremer/wao/services/service/WaoFixtures.java +++ b/wao-services/src/test/java/fr/ifremer/wao/services/service/WaoFixtures.java @@ -1,5 +1,6 @@ package fr.ifremer.wao.services.service; +import com.google.common.collect.Iterables; import fr.ifremer.wao.WaoTechnicalException; import fr.ifremer.wao.entity.Company; import fr.ifremer.wao.entity.CompanyTopiaDao; @@ -9,6 +10,7 @@ import fr.ifremer.wao.entity.UserProfileImpl; import fr.ifremer.wao.entity.UserRole; import fr.ifremer.wao.entity.WaoUser; import fr.ifremer.wao.entity.WaoUserImpl; +import fr.ifremer.wao.entity.WaoUserTopiaDao; import fr.ifremer.wao.services.AuthenticatedWaoUser; import fr.ifremer.wao.services.WaoServiceContext; import fr.ifremer.wao.services.service.administration.ReferentialService; @@ -42,16 +44,22 @@ public class WaoFixtures { } protected AuthenticatedWaoUser newAuthenticatedWaoUser(String login, ObsProgram obsProgram, UserRole userRole) { - WaoUser waoUser = new WaoUserImpl(); - waoUser.setOrganisation(ifremer()); - waoUser.setLogin(login); - waoUser.setActive(true); - UserProfile userProfile = new UserProfileImpl(); - userProfile.setUserRole(userRole); - userProfile.setObsProgram(obsProgram); - userProfile.setCanWrite(true); - waoUser.addUserProfile(userProfile); - return new AuthenticatedWaoUser(waoUser, userProfile); + WaoUserTopiaDao waoUserDao = serviceContext.getPersistenceContext().getWaoUserDao(); + WaoUser waoUser = waoUserDao.forLoginEquals(login).findUniqueOrNull(); + if (waoUser == null) { + waoUser = new WaoUserImpl(); + waoUser.setOrganisation(ifremer()); + waoUser.setLogin(login); + waoUser.setActive(true); + UserProfile userProfile = new UserProfileImpl(); + userProfile.setUserRole(userRole); + userProfile.setObsProgram(obsProgram); + userProfile.setCanWrite(true); + waoUser.addUserProfile(userProfile); + waoUserDao.create(waoUser); + serviceContext.getPersistenceContext().commit(); + } + return new AuthenticatedWaoUser(waoUser, Iterables.getOnlyElement(waoUser.getUserProfile())); } public Company ifremer() { -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@codelutin.com>.