r1547 - in trunk: src/site/rst/obsMer wao-business/src/main/java/fr/ifremer/wao wao-business/src/main/java/fr/ifremer/wao/io/csv2/models/operations wao-business/src/main/java/fr/ifremer/wao/service wao-business/src/main/resources/i18n wao-business/src/main/xmi wao-business/src/test/java/fr/ifremer/wao/business wao-business/src/test/java/fr/ifremer/wao/service wao-ui/src/main/java/fr/ifremer/wao/ui/pages wao-ui/src/main/webapp
Author: bleny Date: 2012-03-06 15:20:03 +0100 (Tue, 06 Mar 2012) New Revision: 1547 Url: http://forge.codelutin.com/repositories/revision/wao/1547 Log: #921 Permettre de d?\195?\169finir les codes des nouvelles lignes ?\195?\160 l'import du plan d'?\195?\169chantillonnage Modified: trunk/src/site/rst/obsMer/administrateur.rst trunk/wao-business/src/main/java/fr/ifremer/wao/WaoUtils.java trunk/wao-business/src/main/java/fr/ifremer/wao/io/csv2/models/operations/SampleRowCodeParserFormatter.java trunk/wao-business/src/main/java/fr/ifremer/wao/service/ServiceSamplingImpl.java trunk/wao-business/src/main/resources/i18n/wao-business_en_GB.properties trunk/wao-business/src/main/resources/i18n/wao-business_fr_FR.properties trunk/wao-business/src/main/xmi/wao.zargo trunk/wao-business/src/test/java/fr/ifremer/wao/business/ObsDebFieldWorkTest.java trunk/wao-business/src/test/java/fr/ifremer/wao/service/ServiceSamplingTest.java trunk/wao-ui/src/main/java/fr/ifremer/wao/ui/pages/SampleRowForm.java trunk/wao-ui/src/main/webapp/SampleRowForm.tml Modified: trunk/src/site/rst/obsMer/administrateur.rst =================================================================== --- trunk/src/site/rst/obsMer/administrateur.rst 2012-03-02 14:22:27 UTC (rev 1546) +++ trunk/src/site/rst/obsMer/administrateur.rst 2012-03-06 14:20:03 UTC (rev 1547) @@ -74,7 +74,7 @@ Format de fichier : -- PLAN_CODE (string) : Code de la ligne (unique, obligatoire) +- PLAN_CODE (string) : Code de la ligne (unique, obligatoire) au format AAAA_IIII (année, n° de ligne), exemple 2012_0003 - SOCIETE_NOM (string) : Nom de la société - PECHE_DIVISION (string) : Zones de pêche liés à la ligne, chaque zone est séparé par un " / " -> ex : VIIa / VIIb (obligatoire) - PECHE_AUTRE (string) : Autres information sur les zones de pêche pour la ligne Modified: trunk/wao-business/src/main/java/fr/ifremer/wao/WaoUtils.java =================================================================== --- trunk/wao-business/src/main/java/fr/ifremer/wao/WaoUtils.java 2012-03-02 14:22:27 UTC (rev 1546) +++ trunk/wao-business/src/main/java/fr/ifremer/wao/WaoUtils.java 2012-03-06 14:20:03 UTC (rev 1547) @@ -49,6 +49,7 @@ import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.regex.Pattern; import static org.nuiton.i18n.I18n.l_; @@ -67,6 +68,8 @@ private static WaoContext context; + protected static final Pattern SAMPLE_ROW_CODE_PATTERN = Pattern.compile("^(\\d{4})_(\\d{4})$"); + static void setContext(WaoContext context) { WaoUtils.context = context; } @@ -309,4 +312,8 @@ } return list; } + + public static boolean isValidSampleRowCode(String code) { + return SAMPLE_ROW_CODE_PATTERN.matcher(code).matches(); + } } Modified: trunk/wao-business/src/main/java/fr/ifremer/wao/io/csv2/models/operations/SampleRowCodeParserFormatter.java =================================================================== --- trunk/wao-business/src/main/java/fr/ifremer/wao/io/csv2/models/operations/SampleRowCodeParserFormatter.java 2012-03-02 14:22:27 UTC (rev 1546) +++ trunk/wao-business/src/main/java/fr/ifremer/wao/io/csv2/models/operations/SampleRowCodeParserFormatter.java 2012-03-06 14:20:03 UTC (rev 1547) @@ -5,12 +5,9 @@ import org.nuiton.util.csv.ValueParserFormatter; import java.text.ParseException; -import java.util.regex.Pattern; public class SampleRowCodeParserFormatter implements ValueParserFormatter<String> { - protected static final Pattern SAMPLE_ROW_CODE_PATTERN = Pattern.compile("^(\\d{4})_(\\d{4})$"); - @Override public String format(String value) { return value; @@ -20,9 +17,9 @@ public String parse(String value) throws ParseException { String sampleRowCode; if (StringUtils.isBlank(value)) { - sampleRowCode = null; + throw new IllegalArgumentException(WaoUtils._("wao.import.sampleRow.failure.missingSampleRowCode")); } else { - if (SAMPLE_ROW_CODE_PATTERN.matcher(value).matches()) { + if (WaoUtils.isValidSampleRowCode(value)) { sampleRowCode = value; } else { throw new IllegalArgumentException(WaoUtils._("wao.import.sampleRow.failure.wrongSampleRowCodeFormat", value.trim())); Modified: trunk/wao-business/src/main/java/fr/ifremer/wao/service/ServiceSamplingImpl.java =================================================================== --- trunk/wao-business/src/main/java/fr/ifremer/wao/service/ServiceSamplingImpl.java 2012-03-02 14:22:27 UTC (rev 1546) +++ trunk/wao-business/src/main/java/fr/ifremer/wao/service/ServiceSamplingImpl.java 2012-03-06 14:20:03 UTC (rev 1547) @@ -280,39 +280,13 @@ SampleRowDAO dao = WaoDAOHelper.getSampleRowDAO(transaction); - SampleRow oldRow = null; + SampleRow oldRow; if (StringUtils.isEmpty(row.getCode())) { - // all rows must have a unique code, the code has a year as prefix - // like '2011_' so we must know on which year the row is active - // the date from which year will be extracted (whatever the day of the year) - Date year; + throw new IllegalStateException(WaoUtils._("wao.import.sampleRow.failure.missingSampleRowCode")); - if (ObsProgram.OBSDEB.equals(row.getObsProgram())) { - // this line has a precise day, let's take the - // year of this day - year = row.getExpectedDate(); - } else { - if (CollectionUtils.isEmpty(row.getSampleMonth())) { - // if the row has no sample months, we can't know - // we take current year by default - year = context.getCurrentDate(); - } else { - // if the row has sample months, take the first month - year = row.getSampleMonth().get(0).getPeriodDate(); - } - } - - // now we have the year for prefix, let's get the whole new - // code (suffix depends on already used codes) - String code = executeGetNewSampleRowCode(transaction, year); - - row.setCode(code); - } else { - row.setCode(WaoUtils.padSampleRowCode(row.getCode())); - // We call the service in another transaction, to prevent // putting an entity in with the same id in the same session oldRow = getSampleRowByCode(null, row.getCode()); @@ -694,12 +668,8 @@ transaction.commitTransaction(); } - @Override - public String executeGetNewSampleRowCode(TopiaContext transaction, + protected String executeGetNewSampleRowCode(TopiaContext transaction, Date beginDate) throws TopiaException { - if (beginDate == null) { - return null; - } Calendar begin = new GregorianCalendar(); begin.setTime(beginDate); @@ -717,7 +687,7 @@ } int num = 1; - if (!StringUtils.isEmpty(maxCode)) { + if (StringUtils.isNotEmpty(maxCode)) { String[] part = maxCode.split("_"); num = Integer.parseInt(part[1]) + 1; } @@ -726,7 +696,7 @@ } @Override - public SampleRow executeNewSampleRow(ConnectedUser connectedUser) { + public SampleRow executeNewSampleRow(TopiaContext transaction, ConnectedUser connectedUser) throws TopiaException { SampleRow newSampleRow = new SampleRowImpl(); newSampleRow.setObsProgram(connectedUser.getProfile().getObsProgram()); newSampleRow.setElligibleBoat(new ArrayList<ElligibleBoat>()); @@ -735,7 +705,7 @@ newSampleRow.setCompany(connectedUser.getCompany()); } - if (connectedUser.isObsMer()) { + if (connectedUser.isObsDeb()) { newSampleRow.setObservers(new ArrayList<WaoUser>()); } else { newSampleRow.setProfession(new ProfessionImpl()); @@ -745,14 +715,42 @@ newSampleRow.setSamplingStrategy(SamplingStrategy.SIMULTANEOUS_ALL_SPECIES); } } + + // all rows must have a unique code, the code has a year as prefix + // like '2011_' so we must know on which year the row is active + + // the date from which year will be extracted (whatever the day of the year) + Date year; + + if (ObsProgram.OBSDEB.equals(newSampleRow.getObsProgram())) { + // this line has a precise day, let's take the + // year of this day + year = newSampleRow.getExpectedDate(); + } else { + if (CollectionUtils.isEmpty(newSampleRow.getSampleMonth())) { + // if the row has no sample months, we can't know + // we take current year by default + year = context.getCurrentDate(); + } else { + // if the row has sample months, take the first month + year = newSampleRow.getSampleMonth().get(0).getPeriodDate(); + } + } + + // now we have the year for prefix, let's get the whole new + // code (suffix depends on already used codes) + String code = executeGetNewSampleRowCode(transaction, year); + + newSampleRow.setCode(code); + return newSampleRow; } @Override protected SampleRow executeNewSampleRow(TopiaContext transaction, ConnectedUser connectedUser, - ObservationType observationType) { - SampleRow newSampleRow = executeNewSampleRow(connectedUser); + ObservationType observationType) throws TopiaException { + SampleRow newSampleRow = executeNewSampleRow(transaction, connectedUser); newSampleRow.setObservationType(observationType); return newSampleRow; } Modified: trunk/wao-business/src/main/resources/i18n/wao-business_en_GB.properties =================================================================== --- trunk/wao-business/src/main/resources/i18n/wao-business_en_GB.properties 2012-03-02 14:22:27 UTC (rev 1546) +++ trunk/wao-business/src/main/resources/i18n/wao-business_en_GB.properties 2012-03-06 14:20:03 UTC (rev 1547) @@ -316,7 +316,8 @@ wao.import.failure.wrongObsDebCode=There is no profession code having code '%s' wao.import.failure.wrongUser=There is no user with login '%s' wao.import.sampleRow.failure.fishingZoneMissing=You must precise at least one fishing zone +wao.import.sampleRow.failure.missingSampleRowCode=You must precise a code for the sample row wao.import.sampleRow.failure.wrongFishingZone=There is no fishing zone with the code '%s' wao.import.sampleRow.failure.wrongSampleRowCodeFormat=The sample row code '%s' is not compliant with the format "YYYY_IIII" wao.validation.sampleRow.conflictOnObserver=Observer %s cannot be associated to the line %s because he has to observer, on the same day, for row %s -wao.validation.sampleRow.observerNotInCompany=Observer %s doesn't work for company %s +wao.validation.sampleRow.observerNotInCompany=Observer %s doesn't work for company %s \ No newline at end of file Modified: trunk/wao-business/src/main/resources/i18n/wao-business_fr_FR.properties =================================================================== --- trunk/wao-business/src/main/resources/i18n/wao-business_fr_FR.properties 2012-03-02 14:22:27 UTC (rev 1546) +++ trunk/wao-business/src/main/resources/i18n/wao-business_fr_FR.properties 2012-03-06 14:20:03 UTC (rev 1547) @@ -315,6 +315,7 @@ wao.import.failure.wrongObsDebCode=Il n'y a pas de code métier ayant pour code '%s' wao.import.failure.wrongUser=Il n'y a pas d'utilisateur ayant pour identifiant '%s' wao.import.sampleRow.failure.fishingZoneMissing=Il faut préciser au moins une zone de pêche +wao.import.sampleRow.failure.missingSampleRowCode=Il faut préciser un code pour a ligne du plan wao.import.sampleRow.failure.wrongFishingZone=Le code '%s' ne correspond à aucune zone de pêche connue du référentiel wao.import.sampleRow.failure.wrongSampleRowCodeFormat=Le code '%s' n'est pas un code de ligne valide selon le format "AAAA_IIII" wao.validation.sampleRow.conflictOnObserver=L'observateur %s ne peut être associé à la ligne %s car il doit observer, le même jour, pour la ligne %s Modified: trunk/wao-business/src/main/xmi/wao.zargo =================================================================== (Binary files differ) Modified: trunk/wao-business/src/test/java/fr/ifremer/wao/business/ObsDebFieldWorkTest.java =================================================================== --- trunk/wao-business/src/test/java/fr/ifremer/wao/business/ObsDebFieldWorkTest.java 2012-03-02 14:22:27 UTC (rev 1546) +++ trunk/wao-business/src/test/java/fr/ifremer/wao/business/ObsDebFieldWorkTest.java 2012-03-06 14:20:03 UTC (rev 1547) @@ -79,7 +79,7 @@ + "2011_0890;GA;OBS1;13/02/2010;UO007;josh;N"; protected static final String SAMPLING_PLAN_WITH_CONFLICTING_SAMPLE_ROW_CSV = "PLAN_CODE;REGION_IFREMER_COD;OBSERVATEUR_COD;OBSERVATION_DATE;UNITE_OBSERVATION_COD;OBSERVATEURS;LIGNE_SANS_CONTACTS\n" - + ";MA;OBS2;13/02/2010;UO087;josh;N"; + + "2011_0892;MA;OBS2;13/02/2010;UO087;josh;N"; protected ObsDebFixtures fixtures = new ObsDebFixtures(manager); Modified: trunk/wao-business/src/test/java/fr/ifremer/wao/service/ServiceSamplingTest.java =================================================================== --- trunk/wao-business/src/test/java/fr/ifremer/wao/service/ServiceSamplingTest.java 2012-03-02 14:22:27 UTC (rev 1546) +++ trunk/wao-business/src/test/java/fr/ifremer/wao/service/ServiceSamplingTest.java 2012-03-06 14:20:03 UTC (rev 1547) @@ -27,6 +27,7 @@ import fr.ifremer.wao.WaoBusinessException; import fr.ifremer.wao.WaoDAOHelper; import fr.ifremer.wao.WaoException; +import fr.ifremer.wao.WaoUtils; import fr.ifremer.wao.bean.ConnectedUser; import fr.ifremer.wao.bean.FacadeRow; import fr.ifremer.wao.bean.ObsProgram; @@ -100,6 +101,16 @@ } @Test + public void testNewSampleRow() throws Exception { + manager.setCurrentDate(5, 3, 2012); + SampleRow sampleRow = serviceSampling.newSampleRow(obsMerFixtures.jeanMichmucheAsAdmin()); + String newSampleRowCode = sampleRow.getCode(); + log.debug("new sample row code is " + newSampleRowCode); + Assert.assertNotNull(newSampleRowCode); + Assert.assertTrue(WaoUtils.isValidSampleRowCode(newSampleRowCode)); + } + + @Test public void testCreateUpdateSampleRow() throws Exception { /** PREPARE DATA **/ TopiaContext transaction = manager.getContext().beginTransaction(); @@ -525,18 +536,7 @@ assertEquals(4, programs.size()); } - @Test - public void testGetNewSampleRowCode() throws Exception { - obsMerFixtures.samplingPlan(); - - Calendar begin = new GregorianCalendar(2009, 11, 1); - - String result = serviceSampling.getNewSampleRowCode(begin.getTime()); - Assert.assertEquals("2009_0007", result); - } - - @Test public void testNewSamplingFilter() throws Exception { manager.setCurrentDate(15, 3, 2012); Modified: trunk/wao-ui/src/main/java/fr/ifremer/wao/ui/pages/SampleRowForm.java =================================================================== --- trunk/wao-ui/src/main/java/fr/ifremer/wao/ui/pages/SampleRowForm.java 2012-03-02 14:22:27 UTC (rev 1546) +++ trunk/wao-ui/src/main/java/fr/ifremer/wao/ui/pages/SampleRowForm.java 2012-03-06 14:20:03 UTC (rev 1547) @@ -26,6 +26,7 @@ import fr.ifremer.wao.WaoBusinessException; import fr.ifremer.wao.WaoException; +import fr.ifremer.wao.WaoUtils; import fr.ifremer.wao.bean.ConnectedUser; import fr.ifremer.wao.bean.ObsProgram; import fr.ifremer.wao.bean.SamplingFilter; @@ -432,6 +433,9 @@ @InjectComponent private Field program; + @InjectComponent + private Field code; + private boolean periodChanged; public SelectModel getProgramSelectModel() throws WaoException { @@ -850,8 +854,11 @@ boats = serviceBoat.getBoatsByImmatriculations(immatriculations); // Update sampleRowCode from program only if changed } else if (isCreateMode()) { - String rowCode = serviceSampling.getNewSampleRowCode(periodBegin); - getSampleRow().setCode(rowCode); + if ( ! WaoUtils.isValidSampleRowCode(getSampleRow().getCode())) { + sampleRowForm.recordError(code, String.format( + messages.get(n_("wao.import.sampleRow.failure.wrongSampleRowCodeFormat")), + getSampleRow().getCode())); + } } if (sampleRow.getSamplingStrategy() == SamplingStrategy.SPECIFIC_STOCK) { Modified: trunk/wao-ui/src/main/webapp/SampleRowForm.tml =================================================================== --- trunk/wao-ui/src/main/webapp/SampleRowForm.tml 2012-03-02 14:22:27 UTC (rev 1546) +++ trunk/wao-ui/src/main/webapp/SampleRowForm.tml 2012-03-06 14:20:03 UTC (rev 1547) @@ -256,14 +256,13 @@ <div class="t-beaneditor"> <t:beaneditor t:id="sampleRowEditor" t:object="sampleRow" t:model="sampleRowModel"> <p:code> - <label>${message:wao.ui.field.SampleRow.code}</label> - <!--${sampleRow.code}--> - <t:if t:test="sampleRow.code"> - <strong>${sampleRow.code}</strong> + <label for="code"> + ${message:wao.ui.field.SampleRow.code} + </label> + <t:if t:test="createMode"> + <t:textfield t:id="code" t:value="sampleRow.code" size="9" /> <p:else> - <span class="code-missing"> - ${message:wao.ui.form.SampleRow.missingBeginDate} - </span> + ${sampleRow.code} </p:else> </t:if> </p:code>
participants (1)
-
bleny@users.forge.codelutin.com