Author: bleny Date: 2014-06-17 13:41:54 +0200 (Tue, 17 Jun 2014) New Revision: 2044 Url: http://forge.codelutin.com/projects/wao/repository/revisions/2044 Log: fixes #5224 use a select to choose dcf5code Modified: trunk/wao-persistence/src/main/resources/db/migration/V4_0__clean_database.sql trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/ObsMerSamplingPlanService.java trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/UpdateSampleRowCommand.java trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/administration/ReferentialService.java trunk/wao-web/src/main/java/fr/ifremer/wao/web/action/obsmer/EditSampleRowAction.java trunk/wao-web/src/main/webapp/WEB-INF/content/obsmer/edit-sample-row-input.jsp Modified: trunk/wao-persistence/src/main/resources/db/migration/V4_0__clean_database.sql =================================================================== --- trunk/wao-persistence/src/main/resources/db/migration/V4_0__clean_database.sql 2014-06-17 11:34:57 UTC (rev 2043) +++ trunk/wao-persistence/src/main/resources/db/migration/V4_0__clean_database.sql 2014-06-17 11:41:54 UTC (rev 2044) @@ -29,4 +29,10 @@ -- On ferme l'accès à ObsDeb -delete from userProfile where obsProgramOrdinal=2 +delete from userProfile where obsProgramOrdinal=2; + +-- Erreur de l'unicité dans le référentiel des codes DCF + +update dcf5code_sampleRow set dcf5code='fr.ifremer.wao.entities.DCF5Code#1289320029000#798260963OTB' where dcf5code='fr.ifremer.wao.entities.DCF5Code#1289320029000#798260963OTBDES'; + +delete from dcf5code where topiaId='fr.ifremer.wao.entities.DCF5Code#1289320029000#798260963OTBDES'; Modified: trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/ObsMerSamplingPlanService.java =================================================================== --- trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/ObsMerSamplingPlanService.java 2014-06-17 11:34:57 UTC (rev 2043) +++ trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/ObsMerSamplingPlanService.java 2014-06-17 11:41:54 UTC (rev 2044) @@ -67,8 +67,6 @@ import fr.ifremer.wao.services.AuthenticatedWaoUser; import fr.ifremer.wao.services.service.administration.ReferentialService; import fr.ifremer.wao.services.service.administration.UnknownBoatImmatriculationsException; -import fr.ifremer.wao.services.service.administration.UnknownFishingGearDcfCodeException; -import fr.ifremer.wao.services.service.administration.UnknownTargetSpeciesDcfCodeException; import fr.ifremer.wao.services.service.csv.ObsMerObsVenteSamplingPlanImportExportModel; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.BooleanUtils; @@ -140,12 +138,6 @@ String elligibleBoatImmatriculations = Boats.toImmatriculations(elligibleBoats); updateSampleRowCommand.setElligibleBoatImmatriculations(elligibleBoatImmatriculations); - Set<String> dcf5Codes = new HashSet<>(); - for (DCF5Code dcf5Code : sampleRow.getdCF5Code()) { - dcf5Codes.add(dcf5Code.getCode()); - } - updateSampleRowCommand.setDcf5Codes(StringUtils.join(dcf5Codes, ' ')); - } else { SampleRow newSampleRow = new SampleRowImpl(); @@ -179,6 +171,13 @@ ); updateSampleRowCommand.setAllCompanies(allCompanies); + ImmutableMap<String, DCF5Code> allDcf5Codes = + Maps.uniqueIndex( + getReferentialService().getAllDcf5Codes(), + TopiaEntities.getTopiaIdFunction() + ); + updateSampleRowCommand.setAllDcf5Codes(allDcf5Codes); + return updateSampleRowCommand; } @@ -228,7 +227,7 @@ } public void preValidate(AuthenticatedWaoUser authenticatedWaoUser, UpdateSampleRowCommand updateSampleRowCommand) - throws UnknownBoatImmatriculationsException, UnknownFishingGearDcfCodeException, UnknownTargetSpeciesDcfCodeException, SampleRowCodeMustBeUniqueException { + throws UnknownBoatImmatriculationsException, SampleRowCodeMustBeUniqueException { SampleRow sampleRow = updateSampleRowCommand.getSampleRow(); @@ -275,11 +274,6 @@ } } - String dcf5Codes = updateSampleRowCommand.getDcf5Codes(); - - sampleRow.cleardCF5Code(); - sampleRow.addAlldCF5Code(getReferentialService().getDcf5Codes(dcf5Codes)); - SampleMonthTopiaDao sampleMonthDao = getSampleMonthDao(); Set<SampleMonth> sampleMonthsToRemove = new HashSet<>(sampleRow.getSampleMonth()); Modified: trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/UpdateSampleRowCommand.java =================================================================== --- trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/UpdateSampleRowCommand.java 2014-06-17 11:34:57 UTC (rev 2043) +++ trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/UpdateSampleRowCommand.java 2014-06-17 11:41:54 UTC (rev 2044) @@ -26,6 +26,7 @@ import com.google.common.collect.Iterables; import com.google.common.collect.Sets; import fr.ifremer.wao.entity.Company; +import fr.ifremer.wao.entity.DCF5Code; import fr.ifremer.wao.entity.FishingZone; import fr.ifremer.wao.entity.SampleMonth; import fr.ifremer.wao.entity.SampleRow; @@ -61,10 +62,10 @@ protected ImmutableMap<String, Company> allCompanies; + protected ImmutableMap<String, DCF5Code> allDcf5Codes; + protected String elligibleBoatImmatriculations; - protected String dcf5Codes; - /** * La clé est un mois au format YYYY-MM */ @@ -107,6 +108,18 @@ sampleRow.setCompany(company); } + public Set<String> getDcf5CodesIds() { + return sampleRow.getdCF5CodeTopiaIds(); + } + + public void setDcf5CodesIds(Set<String> dcf5CodesIds) { + sampleRow.cleardCF5Code(); + for (String dcf5CodesId : dcf5CodesIds) { + DCF5Code dcf5Code = allDcf5Codes.get(dcf5CodesId); + sampleRow.adddCF5Code(dcf5Code); + } + } + public void setCode(String code) { Preconditions.checkState(isCreation(), "cannot change code for sample row " + sampleRow); sampleRow.setCode(code); @@ -181,6 +194,14 @@ this.allCompanies = allCompanies; } + public ImmutableMap<String, DCF5Code> getAllDcf5Codes() { + return allDcf5Codes; + } + + public void setAllDcf5Codes(ImmutableMap<String, DCF5Code> allDcf5Codes) { + this.allDcf5Codes = allDcf5Codes; + } + public String getElligibleBoatImmatriculations() { return elligibleBoatImmatriculations; } @@ -189,14 +210,6 @@ this.elligibleBoatImmatriculations = elligibleBoatImmatriculations; } - public String getDcf5Codes() { - return dcf5Codes; - } - - public void setDcf5Codes(String dcf5Codes) { - this.dcf5Codes = dcf5Codes; - } - public boolean isObservationAlreadyStarted() { return sampleRow.hasSampleMonthRealTideTime(); } Modified: trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/administration/ReferentialService.java =================================================================== --- trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/administration/ReferentialService.java 2014-06-17 11:34:57 UTC (rev 2043) +++ trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/administration/ReferentialService.java 2014-06-17 11:41:54 UTC (rev 2044) @@ -80,6 +80,7 @@ import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.tuple.Pair; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.nuiton.csv.Import; @@ -741,6 +742,43 @@ log.info(nbCodes + " dcf codes in database"); } + DCF5CodeTopiaDao dcf5CodeDao = getDCF5CodeDao(); + + if (log.isInfoEnabled()) { + log.info(dcf5CodeDao.count() + " dcf5 codes in database before filling"); + } + + List<FishingGearDCF> fishingGearDCFs = fishingGearDCFDao.findAll(); + List<TargetSpeciesDCF> targetSpeciesDCFs = targetSpeciesDCFDao.findAll(); + + Set<Pair<FishingGearDCF, TargetSpeciesDCF>> pairs = new HashSet<>(); + + for (FishingGearDCF fishingGearDCF : fishingGearDCFs) { + for (TargetSpeciesDCF targetSpeciesDCF : targetSpeciesDCFs) { + pairs.add(Pair.of(fishingGearDCF, targetSpeciesDCF)); + } + pairs.add(Pair.of(fishingGearDCF, (TargetSpeciesDCF) null)); + } + + for (Pair<FishingGearDCF, TargetSpeciesDCF> pair : pairs) { + + FishingGearDCF fishingGearDCF = pair.getLeft(); + TargetSpeciesDCF targetSpeciesDCF = pair.getRight(); + + Map<String, Object> properties = new HashMap<>(); + properties.put(DCF5Code.PROPERTY_FISHING_GEAR_DCF, fishingGearDCF); + properties.put(DCF5Code.PROPERTY_TARGET_SPECIES_DCF, targetSpeciesDCF); + + DCF5Code dcf5code = dcf5CodeDao.forProperties(properties).findUniqueOrNull(); + if (dcf5code == null) { + dcf5CodeDao.create(properties); + } + } + + if (log.isInfoEnabled()) { + log.info(dcf5CodeDao.count() + " dcf5 codes in database after filling"); + } + commit(); } @@ -1085,4 +1123,11 @@ return dcf5Codes; } + public List<DCF5Code> getAllDcf5Codes() { + + List<DCF5Code> dcf5Codes = getDCF5CodeDao().findAll(); + + return dcf5Codes; + + } } Modified: trunk/wao-web/src/main/java/fr/ifremer/wao/web/action/obsmer/EditSampleRowAction.java =================================================================== --- trunk/wao-web/src/main/java/fr/ifremer/wao/web/action/obsmer/EditSampleRowAction.java 2014-06-17 11:34:57 UTC (rev 2043) +++ trunk/wao-web/src/main/java/fr/ifremer/wao/web/action/obsmer/EditSampleRowAction.java 2014-06-17 11:41:54 UTC (rev 2044) @@ -31,8 +31,6 @@ import fr.ifremer.wao.services.service.SampleRowCodeMustBeUniqueException; import fr.ifremer.wao.services.service.UpdateSampleRowCommand; import fr.ifremer.wao.services.service.administration.UnknownBoatImmatriculationsException; -import fr.ifremer.wao.services.service.administration.UnknownFishingGearDcfCodeException; -import fr.ifremer.wao.services.service.administration.UnknownTargetSpeciesDcfCodeException; import fr.ifremer.wao.web.WaoJspActionSupport; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; @@ -92,10 +90,6 @@ } catch (UnknownBoatImmatriculationsException e) { addFieldError("updateSampleRowCommand.elligibleBoatImmatriculations", t("wao.ui.unknownBoatImmatriculations", Joiner.on(", ").join(e.getImmatriculations()))); - } catch (UnknownFishingGearDcfCodeException e) { - addFieldError("updateSampleRowCommand.dcf5Codes", t("wao.ui.form.SampleRow.error.unknownFishingGearDcfCode", e.getCode())); - } catch (UnknownTargetSpeciesDcfCodeException e) { - addFieldError("updateSampleRowCommand.dcf5Codes", t("wao.ui.form.SampleRow.error.unknownTargetSpeciesDcfCode", e.getCode())); } catch (SampleRowCodeMustBeUniqueException e) { addFieldError("updateSampleRowCommand.code", t("wao.ui.form.SampleRow.error.codeMustBeUnique")); } Modified: trunk/wao-web/src/main/webapp/WEB-INF/content/obsmer/edit-sample-row-input.jsp =================================================================== --- trunk/wao-web/src/main/webapp/WEB-INF/content/obsmer/edit-sample-row-input.jsp 2014-06-17 11:34:57 UTC (rev 2043) +++ trunk/wao-web/src/main/webapp/WEB-INF/content/obsmer/edit-sample-row-input.jsp 2014-06-17 11:41:54 UTC (rev 2044) @@ -142,10 +142,14 @@ disabled="updateSampleRowCommand.observationAlreadyStarted" cssClass="input-xxlarge" /> - <s:textfield name="updateSampleRowCommand.dcf5Codes" - label="%{getText('wao.ui.field.SampleRow.dcf5Codes')}" - disabled="updateSampleRowCommand.observationAlreadyStarted" - cssClass="input-xxlarge" /> + <s:select name="updateSampleRowCommand.dcf5CodesIds" + label="%{getText('wao.ui.field.SampleRow.dcf5Codes')}" + disabled="updateSampleRowCommand.observationAlreadyStarted" + list="updateSampleRowCommand.allDcf5Codes" + listValue="%{value.code}" + cssClass="input-xxlarge select2" + multiple="true" + /> </fieldset>