r2110 - in trunk: wao-persistence/src/main/java/fr/ifremer/wao wao-persistence/src/main/resources/i18n wao-services/src/main/java/fr/ifremer/wao/services wao-services/src/main/java/fr/ifremer/wao/services/service wao-services/src/main/java/fr/ifremer/wao/services/service/csv wao-web/src/main/java/fr/ifremer/wao/web/action/obsmer wao-web/src/main/resources/i18n wao-web/src/main/webapp/WEB-INF/content/obsmer
Author: bleny Date: 2014-06-26 16:29:44 +0200 (Thu, 26 Jun 2014) New Revision: 2110 Url: http://forge.codelutin.com/projects/wao/repository/revisions/2110 Log: fixes #4726 export sampling plan with stats Added: trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/csv/ObsMerSamplingPlanExportModel.java trunk/wao-web/src/main/java/fr/ifremer/wao/web/action/obsmer/ExportSamplingPlanWithStatisticsAction.java Modified: trunk/wao-persistence/src/main/java/fr/ifremer/wao/WaoUtils.java trunk/wao-persistence/src/main/resources/i18n/wao-persistence_en_GB.properties trunk/wao-services/src/main/java/fr/ifremer/wao/services/AuthenticatedWaoUser.java trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/ObsMerSamplingPlanService.java trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/SamplingPlan.java trunk/wao-web/src/main/resources/i18n/wao-web_en_GB.properties trunk/wao-web/src/main/resources/i18n/wao-web_fr_FR.properties trunk/wao-web/src/main/webapp/WEB-INF/content/obsmer/sampling-plan.jsp Modified: trunk/wao-persistence/src/main/java/fr/ifremer/wao/WaoUtils.java =================================================================== --- trunk/wao-persistence/src/main/java/fr/ifremer/wao/WaoUtils.java 2014-06-26 14:29:00 UTC (rev 2109) +++ trunk/wao-persistence/src/main/java/fr/ifremer/wao/WaoUtils.java 2014-06-26 14:29:44 UTC (rev 2110) @@ -174,4 +174,8 @@ public static int collectionHashcode(Collection<?> a) { return a == null ? 0 : a.size(); } + + public static String getMonthPattern(Locale locale) { + return I18n.l(locale, "wao.month.pattern"); + } } Modified: trunk/wao-persistence/src/main/resources/i18n/wao-persistence_en_GB.properties =================================================================== --- trunk/wao-persistence/src/main/resources/i18n/wao-persistence_en_GB.properties 2014-06-26 14:29:00 UTC (rev 2109) +++ trunk/wao-persistence/src/main/resources/i18n/wao-persistence_en_GB.properties 2014-06-26 14:29:44 UTC (rev 2110) @@ -147,4 +147,5 @@ wao.dateyear.formatter=%1$tY wao.month.formatter=%1$tm-%1$tY wao.month.parser=MM-yyyy +wao.month.pattern= wao.time.formatter=%1$tH\:%1$tM Modified: trunk/wao-services/src/main/java/fr/ifremer/wao/services/AuthenticatedWaoUser.java =================================================================== --- trunk/wao-services/src/main/java/fr/ifremer/wao/services/AuthenticatedWaoUser.java 2014-06-26 14:29:00 UTC (rev 2109) +++ trunk/wao-services/src/main/java/fr/ifremer/wao/services/AuthenticatedWaoUser.java 2014-06-26 14:29:44 UTC (rev 2110) @@ -194,6 +194,10 @@ return userProfile.isAdmin() || userProfile.isCoordinatorOrObserver(); } + public boolean isAuthorizedToExportSamplingPlanWithStatistics() { + return userProfile.isAdmin() || userProfile.isCoordinator(); + } + public boolean isAuthorizedToViewContactsComments() { return ! userProfile.isProfessional(); } 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-26 14:29:00 UTC (rev 2109) +++ trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/ObsMerSamplingPlanService.java 2014-06-26 14:29:44 UTC (rev 2110) @@ -21,6 +21,7 @@ * #L% */ +import com.google.common.base.Charsets; import com.google.common.base.Optional; import com.google.common.cache.Cache; import com.google.common.collect.ImmutableSet; @@ -28,6 +29,7 @@ import com.google.common.collect.Sets; import fr.ifremer.wao.ContactsFilter; import fr.ifremer.wao.SampleRowsFilter; +import fr.ifremer.wao.WaoTechnicalException; import fr.ifremer.wao.WaoUtils; import fr.ifremer.wao.entity.Contact; import fr.ifremer.wao.entity.ContactState; @@ -36,13 +38,19 @@ import fr.ifremer.wao.entity.SampleRow; import fr.ifremer.wao.entity.SampleRowTopiaDao; import fr.ifremer.wao.services.AuthenticatedWaoUser; +import fr.ifremer.wao.services.service.csv.ObsMerSamplingPlanExportModel; +import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.tuple.Pair; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.nuiton.csv.Export; +import org.nuiton.csv.ExportModel; +import java.io.InputStream; import java.util.Collection; import java.util.Date; +import java.util.LinkedHashSet; import java.util.List; public class ObsMerSamplingPlanService extends SamplingPlanService { @@ -214,4 +222,42 @@ return Pair.of(real, estimated); } + + public InputStream exportSamplingPlanWithStatistics(AuthenticatedWaoUser authenticatedWaoUser, SampleRowsFilter filter) { + + ObsMerSamplingPlan samplingPlan = getSamplingPlan(authenticatedWaoUser, filter); + + Collection<ObsMerSamplingPlan.ObsMerSamplingPlanSampleRowPart> sampleRows = + new LinkedHashSet<>(); + for (SamplingPlan.SamplingPlanFacadePart facade : samplingPlan) { + for (SamplingPlan.SamplingPlanSectorPart<ObsMerSamplingPlan.ObsMerSamplingPlanSampleRowPart> sector : facade) { + for (ObsMerSamplingPlan.ObsMerSamplingPlanSampleRowPart obsMerSampleRowPart : sector) { + sampleRows.add(obsMerSampleRowPart); + } + } + } + + ExportModel<ObsMerSamplingPlan.ObsMerSamplingPlanSampleRowPart> exportModel = + new ObsMerSamplingPlanExportModel( + getLocale(), + filter.getPeriodFrom(), + filter.getPeriodTo()); + + Export<ObsMerSamplingPlan.ObsMerSamplingPlanSampleRowPart> export = + Export.newExport(exportModel, sampleRows); + + try { + + String csvContent = export.toString(Charsets.UTF_8); + + InputStream csvInputStream = IOUtils.toInputStream(csvContent, Charsets.UTF_8); + + return csvInputStream; + + } catch (Exception e) { + throw new WaoTechnicalException(e); + } + + } + } Modified: trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/SamplingPlan.java =================================================================== --- trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/SamplingPlan.java 2014-06-26 14:29:00 UTC (rev 2109) +++ trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/SamplingPlan.java 2014-06-26 14:29:44 UTC (rev 2110) @@ -6,6 +6,7 @@ import fr.ifremer.wao.entity.SampleMonth; import fr.ifremer.wao.entity.SampleRow; import fr.ifremer.wao.entity.SampleRowLog; +import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.builder.ToStringBuilder; import org.nuiton.util.DateUtil; @@ -281,6 +282,10 @@ */ protected SamplingPlanStatistics totalTides; + protected String facade; + + protected String sectors; + protected String fishingZones; protected String code; @@ -362,6 +367,8 @@ } } totalTides = new SamplingPlanStatistics(totalTidesExpected, totalTidesReal, totalTidesEstimated); + facade = sampleRow.getFacade(); + sectors = sampleRow.getSectors(); fishingZones = ""; for (FishingZone zone : sampleRow.getFishingZone()) { fishingZones += zone.getDistrictCode() + ", "; @@ -434,6 +441,14 @@ return code; } + public String getFacade() { + return facade; + } + + public String getSectors() { + return sectors; + } + public String getFishingZonesInfos() { return fishingZonesInfos; } @@ -545,6 +560,10 @@ public long getSampleRowContactCounts() { return sampleRowContactCounts; } + + public String getDcf5Codes() { + return StringUtils.join(getDcf5CodesAndDescriptions().keySet(), ", "); + } } } Added: trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/csv/ObsMerSamplingPlanExportModel.java =================================================================== --- trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/csv/ObsMerSamplingPlanExportModel.java (rev 0) +++ trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/csv/ObsMerSamplingPlanExportModel.java 2014-06-26 14:29:44 UTC (rev 2110) @@ -0,0 +1,102 @@ +package fr.ifremer.wao.services.service.csv; + +import fr.ifremer.wao.WaoUtils; +import fr.ifremer.wao.services.service.ObsMerSamplingPlan; +import org.nuiton.csv.Common; +import org.nuiton.csv.ValueGetter; +import org.nuiton.csv.ext.AbstractExportModel; +import org.nuiton.util.PeriodDates; + +import java.util.Collection; +import java.util.Date; +import java.util.Locale; + +public class ObsMerSamplingPlanExportModel extends AbstractExportModel<ObsMerSamplingPlan.ObsMerSamplingPlanSampleRowPart> { + + public ObsMerSamplingPlanExportModel(Locale locale, Date periodFrom, Date periodTo) { + + super(';'); + + Common.DateValue monthYearFormatter = + new Common.DateValue(WaoUtils.getMonthPattern(locale)); + + modelBuilder.newColumnForExport("PLAN_CODE", "code"); + modelBuilder.newColumnForExport("PROGRAMME_CODE", "programName"); + modelBuilder.newColumnForExport("PROGRAMME_DEBUT", "periodBegin", monthYearFormatter); + modelBuilder.newColumnForExport("PROGRAMME_DEBUT", "periodEnd", monthYearFormatter); + modelBuilder.newColumnForExport("SOCIETE_NOM", "companyName"); + modelBuilder.newColumnForExport("PECHE_FACADE", "facade"); + modelBuilder.newColumnForExport("PECHE_ZONE", "sectors"); + modelBuilder.newColumnForExport("ZONES_PECHE", "fishingZones"); + modelBuilder.newColumnForExport("ZONES_PECHE_INFOS", "fishingZonesInfos"); + modelBuilder.newColumnForExport("ZONES_PECHE_INFOS", "fishingZonesInfos"); + modelBuilder.newColumnForExport("METIER_MAILLAGE", "professionMeshSize"); + modelBuilder.newColumnForExport("METIER_TAILLE", "professionSize"); + modelBuilder.newColumnForExport("METIER_AUTRE", "professionOther"); + modelBuilder.newColumnForExport("METIER_LIBELLE", "professionLibelle"); + modelBuilder.newColumnForExport("METIER_ESPECES", "professionSpecies"); + modelBuilder.newColumnForImportExport("PLAN_DUREE_MOY", "averageTideTime", Common.DOUBLE_PRIMITIVE); + modelBuilder.newColumnForImportExport("PLAN_NB_OBSERV", "nbObservants", Common.PRIMITIVE_INTEGER); + modelBuilder.newColumnForImportExport("TAUX_COUVERTURE_APPLIQUE", "appliedCoverageRate", Common.DOUBLE); + + Collection<Date> months = new PeriodDates(periodFrom, periodTo).getMonths(); + + for (Date month : months) { + String headerName = monthYearFormatter.format(month); + modelBuilder.newColumnForExport(headerName + "_EFFORT", new ExpectedTidesValueGetter(month), Common.INTEGER); + modelBuilder.newColumnForExport(headerName + "_ESTIME", new EstimatedTidesValueGetter(month), Common.INTEGER); + modelBuilder.newColumnForExport(headerName + "_REALISE", new RealTidesValueGetter(month), Common.INTEGER); + } + + modelBuilder.newColumnForExport("TOTAL_EFFORT", "totalTidesExpected", Common.INTEGER); + modelBuilder.newColumnForExport("TOTAL_ESTIME", "totalTidesEstimated", Common.INTEGER); + modelBuilder.newColumnForExport("TOTAL_REALISE", "totalTidesReal", Common.INTEGER); + + modelBuilder.newColumnForExport("TOTAL_EFFORT_JOURS", "observationTimesInDaysExpected", Common.DOUBLE); + modelBuilder.newColumnForExport("TOTAL_ESTIME_JOURS", "observationTimesInDaysEstimated", Common.LONG); + modelBuilder.newColumnForExport("TOTAL_REALISE_JOURS", "observationTimesInDaysReal", Common.LONG); + + } + + protected static class ExpectedTidesValueGetter implements ValueGetter<ObsMerSamplingPlan.ObsMerSamplingPlanSampleRowPart, Integer> { + + protected Date month; + + public ExpectedTidesValueGetter(Date month) { + this.month = month; + } + + @Override + public Integer get(ObsMerSamplingPlan.ObsMerSamplingPlanSampleRowPart sampleRowPart) { + return sampleRowPart.getNbTidesExpected(month); + } + } + + protected static class EstimatedTidesValueGetter implements ValueGetter<ObsMerSamplingPlan.ObsMerSamplingPlanSampleRowPart, Integer> { + + protected Date month; + + public EstimatedTidesValueGetter(Date month) { + this.month = month; + } + + @Override + public Integer get(ObsMerSamplingPlan.ObsMerSamplingPlanSampleRowPart sampleRowPart) { + return sampleRowPart.getNbTidesExpected(month); + } + } + + protected static class RealTidesValueGetter implements ValueGetter<ObsMerSamplingPlan.ObsMerSamplingPlanSampleRowPart, Integer> { + + protected Date month; + + public RealTidesValueGetter(Date month) { + this.month = month; + } + + @Override + public Integer get(ObsMerSamplingPlan.ObsMerSamplingPlanSampleRowPart sampleRowPart) { + return sampleRowPart.getNbTidesReal(month); + } + } +} Copied: trunk/wao-web/src/main/java/fr/ifremer/wao/web/action/obsmer/ExportSamplingPlanWithStatisticsAction.java (from rev 2106, trunk/wao-web/src/main/java/fr/ifremer/wao/web/action/ExportSamplingPlanAction.java) =================================================================== --- trunk/wao-web/src/main/java/fr/ifremer/wao/web/action/obsmer/ExportSamplingPlanWithStatisticsAction.java (rev 0) +++ trunk/wao-web/src/main/java/fr/ifremer/wao/web/action/obsmer/ExportSamplingPlanWithStatisticsAction.java 2014-06-26 14:29:44 UTC (rev 2110) @@ -0,0 +1,72 @@ +package fr.ifremer.wao.web.action.obsmer; + +/* + * #%L + * Wao :: Web + * %% + * Copyright (C) 2009 - 2014 Ifremer + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero 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 Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ + +import com.opensymphony.xwork2.Preparable; +import fr.ifremer.wao.SampleRowsFilter; +import fr.ifremer.wao.services.service.ObsMerSamplingPlanService; +import fr.ifremer.wao.web.action.AbstractDownloadCsvAction; + +import java.io.InputStream; +import java.util.Date; + +public class ExportSamplingPlanWithStatisticsAction extends AbstractDownloadCsvAction implements Preparable { + + private static final long serialVersionUID = 1L; + + protected transient ObsMerSamplingPlanService service; + + protected SampleRowsFilter filter; + + public void setService(ObsMerSamplingPlanService service) { + this.service = service; + } + + public SampleRowsFilter getFilter() { + if (filter == null) { + prepare(); + } + return filter; + } + + @Override + public void prepare() { + + filter = service.newSampleRowsFilter(getAuthenticatedWaoUser()); + + } + + @Override + public String getFileName() { + + return "plan-stats_" + getObsProgram().name() + "_" + dateFormat.format(new Date()) + ".csv"; + + } + + @Override + public InputStream getInputStream() { + + return service.exportSamplingPlanWithStatistics(getAuthenticatedWaoUser(), filter); + + } + +} Modified: trunk/wao-web/src/main/resources/i18n/wao-web_en_GB.properties =================================================================== --- trunk/wao-web/src/main/resources/i18n/wao-web_en_GB.properties 2014-06-26 14:29:00 UTC (rev 2109) +++ trunk/wao-web/src/main/resources/i18n/wao-web_en_GB.properties 2014-06-26 14:29:44 UTC (rev 2110) @@ -61,6 +61,7 @@ wao.ui.action.editOrDeleteSampleRow=Edit or delete sample row wao.ui.action.enlargeView=Enlarge view wao.ui.action.exportBoats=Export boats with private infos +wao.ui.action.exportSamplingPlanCsvWithStatistics=Export the plan and the statistics to CSV wao.ui.action.filter=Apply filters wao.ui.action.goBackToSamplingPlan=Back to sampling plan wao.ui.action.hideFilters=Hide filters Modified: trunk/wao-web/src/main/resources/i18n/wao-web_fr_FR.properties =================================================================== --- trunk/wao-web/src/main/resources/i18n/wao-web_fr_FR.properties 2014-06-26 14:29:00 UTC (rev 2109) +++ trunk/wao-web/src/main/resources/i18n/wao-web_fr_FR.properties 2014-06-26 14:29:44 UTC (rev 2110) @@ -61,6 +61,7 @@ wao.ui.action.editOrDeleteSampleRow=Modifier ou supprimer la ligne wao.ui.action.enlargeView=Agrandir la vue wao.ui.action.exportBoats=Exporter les $s navires avec leurs informations privées +wao.ui.action.exportSamplingPlanCsvWithStatistics=Exporter au format CSV le plan avec le réalisé wao.ui.action.filter=Appliquer les filtres wao.ui.action.goBackToSamplingPlan=Retourner au plan d'échantillonnage wao.ui.action.hideFilters=Masquer les filtres Modified: trunk/wao-web/src/main/webapp/WEB-INF/content/obsmer/sampling-plan.jsp =================================================================== --- trunk/wao-web/src/main/webapp/WEB-INF/content/obsmer/sampling-plan.jsp 2014-06-26 14:29:00 UTC (rev 2109) +++ trunk/wao-web/src/main/webapp/WEB-INF/content/obsmer/sampling-plan.jsp 2014-06-26 14:29:44 UTC (rev 2110) @@ -147,6 +147,12 @@ </s:submit> </s:if> + <s:if test="authenticatedWaoUser.authorizedToExportSamplingPlanWithStatistics"> + <s:submit namespace="/obsmer" action="export-sampling-plan-with-statistics" type="button" cssClass="btn"> + <i class="icon-download"></i> <s:text name="wao.ui.action.exportSamplingPlanCsvWithStatistics" /> + </s:submit> + </s:if> + </div> </s:form>
participants (1)
-
bleny@users.forge.codelutin.com