r1759 - in trunk: wao-persistence/src/main/java/fr/ifremer/wao/entity wao-services/src/main/java/fr/ifremer/wao/services/service wao-web/src/main/webapp/WEB-INF/content/obsmer
Author: tchemit Date: 2014-03-26 16:36:29 +0100 (Wed, 26 Mar 2014) New Revision: 1759 Url: http://forge.codelutin.com/projects/wao/repository/revisions/1759 Log: refs-60 #4483 fix total values + sampleRowsFilter query Modified: trunk/wao-persistence/src/main/java/fr/ifremer/wao/entity/SampleRowTopiaDao.java trunk/wao-persistence/src/main/java/fr/ifremer/wao/entity/SampleRows.java trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/ObsMerSamplingPlan.java trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/ObsMerSamplingPlanBuilder.java trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/ObsMerSamplingPlanService.java trunk/wao-web/src/main/webapp/WEB-INF/content/obsmer/sampling-plan.jsp Modified: trunk/wao-persistence/src/main/java/fr/ifremer/wao/entity/SampleRowTopiaDao.java =================================================================== --- trunk/wao-persistence/src/main/java/fr/ifremer/wao/entity/SampleRowTopiaDao.java 2014-03-26 15:09:58 UTC (rev 1758) +++ trunk/wao-persistence/src/main/java/fr/ifremer/wao/entity/SampleRowTopiaDao.java 2014-03-26 15:36:29 UTC (rev 1759) @@ -95,7 +95,7 @@ query.addWhereClause("sr." + SampleRow.PROPERTY_EXPECTED_DATE + " <= :periodTo", ImmutableMap.of("periodTo", (Object) filter.getPeriodTo())); } else { Date periodToMonth = DateUtils.truncate(filter.getPeriodTo(), Calendar.MONTH); - query.addWhereClause("sr." + SampleRow.PROPERTY_PERIOD_END + " <= :periodToMonth", ImmutableMap.of("periodToMonth", (Object) periodToMonth)); + query.addWhereClause("sr." + SampleRow.PROPERTY_PERIOD_BEGIN + " <= :periodToMonth", ImmutableMap.of("periodToMonth", (Object) periodToMonth)); } } Modified: trunk/wao-persistence/src/main/java/fr/ifremer/wao/entity/SampleRows.java =================================================================== --- trunk/wao-persistence/src/main/java/fr/ifremer/wao/entity/SampleRows.java 2014-03-26 15:09:58 UTC (rev 1758) +++ trunk/wao-persistence/src/main/java/fr/ifremer/wao/entity/SampleRows.java 2014-03-26 15:36:29 UTC (rev 1759) @@ -23,6 +23,8 @@ import com.google.common.base.Function; +import java.util.Date; + public class SampleRows { public static Function<SampleRow, String> getCode() { @@ -36,4 +38,34 @@ return input.getCode(); } } + + public static Integer getExpectedTidesValue(SampleRow sampleRow, Date month) { + Integer result = null; + SampleMonth sampleMonth = sampleRow.getSampleMonth(month); + if (sampleMonth != null) { + int tidesValue = sampleMonth.getExpectedTidesValue(); + result = tidesValue; + } + return result; + } + + public static Integer getRealTidesValue(SampleRow sampleRow, Date month) { + Integer result = null; + SampleMonth sampleMonth = sampleRow.getSampleMonth(month); + if (sampleMonth != null) { + int tidesValue = sampleMonth.getRealTidesValue(); + result = tidesValue; + } + return result; + } + + public static Integer getEstimatedTidesValue(SampleRow sampleRow, Date month) { + Integer result = null; + SampleMonth sampleMonth = sampleRow.getSampleMonth(month); + if (sampleMonth != null) { + int tidesValue = sampleMonth.getEstimatedTidesValue(); + result = tidesValue; + } + return result; + } } Modified: trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/ObsMerSamplingPlan.java =================================================================== --- trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/ObsMerSamplingPlan.java 2014-03-26 15:09:58 UTC (rev 1758) +++ trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/ObsMerSamplingPlan.java 2014-03-26 15:36:29 UTC (rev 1759) @@ -23,8 +23,8 @@ import fr.ifremer.wao.WaoUtils; import fr.ifremer.wao.entity.FishingZone; -import fr.ifremer.wao.entity.SampleMonth; import fr.ifremer.wao.entity.SampleRow; +import fr.ifremer.wao.entity.SampleRows; import java.io.Serializable; import java.text.NumberFormat; @@ -275,6 +275,21 @@ protected SampleRow sampleRow; /** + * Total of expected tides. + */ + protected int totalTidesExpected; + + /** + * Total of real tides. + */ + protected int totalTidesReal; + + /** + * Total of estimated tides. + */ + protected int totalTidesEstimated; + + /** * Expected Times of observation days. * <p/> * FIXME Voir si on doit garder un *D*ouble @@ -297,10 +312,16 @@ public ObsMerSamplingPlanSampleRowPart(SampleRow sampleRow, + int totalTidesExpected, + int totalTidesReal, + int totalTidesEstimated, Double observationTimesInDaysExpected, Long observationTimesInDaysReal, Long observationTimesInDaysEstimated) { this.sampleRow = sampleRow; + this.totalTidesExpected = totalTidesExpected; + this.totalTidesReal = totalTidesReal; + this.totalTidesEstimated = totalTidesEstimated; this.observationTimesInDaysExpected = observationTimesInDaysExpected; this.observationTimesInDaysReal = observationTimesInDaysReal; this.observationTimesInDaysEstimated = observationTimesInDaysEstimated; @@ -310,30 +331,30 @@ return sampleRow; } + public int getTotalTidesExpected() { + return totalTidesExpected; + } + + public int getTotalTidesReal() { + return totalTidesReal; + } + + public int getTotalTidesEstimated() { + return totalTidesEstimated; + } + public Integer getNbTidesExpected(Date month) { - Integer result = null; - SampleMonth sampleMonth = sampleRow.getSampleMonth(month); - if (sampleMonth != null) { - result = sampleMonth.getExpectedTidesValue(); - } + Integer result = SampleRows.getExpectedTidesValue(sampleRow, month); return result; } public Integer getNbTidesReal(Date month) { - Integer result = null; - SampleMonth sampleMonth = sampleRow.getSampleMonth(month); - if (sampleMonth != null) { - result = sampleMonth.getRealTidesValue(); - } + Integer result = SampleRows.getRealTidesValue(sampleRow, month); return result; } public Integer getNbTidesEstimated(Date month) { - Integer result = null; - SampleMonth sampleMonth = sampleRow.getSampleMonth(month); - if (sampleMonth != null) { - result = sampleMonth.getEstimatedTidesValue(); - } + Integer result = SampleRows.getEstimatedTidesValue(sampleRow, month); return result; } @@ -341,7 +362,7 @@ public String getTotalPercentage() { String result; if (sampleRow.getTotalTidesExpected() > 0) { - double percentage = (double) sampleRow.getTotalTidesReal() / (double) sampleRow.getTotalTidesExpected(); + double percentage = (double) totalTidesReal / (double) totalTidesExpected; result = NumberFormat.getPercentInstance().format(percentage); } else { result = "- %"; Modified: trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/ObsMerSamplingPlanBuilder.java =================================================================== --- trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/ObsMerSamplingPlanBuilder.java 2014-03-26 15:09:58 UTC (rev 1758) +++ trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/ObsMerSamplingPlanBuilder.java 2014-03-26 15:36:29 UTC (rev 1759) @@ -25,6 +25,7 @@ import com.google.common.collect.Ordering; import fr.ifremer.wao.SampleRowsFilter; import fr.ifremer.wao.entity.SampleRow; +import fr.ifremer.wao.entity.SampleRows; import org.apache.commons.lang3.mutable.MutableDouble; import org.apache.commons.lang3.mutable.MutableInt; import org.apache.commons.lang3.mutable.MutableLong; @@ -148,6 +149,12 @@ PeriodDates periodDates = new PeriodDates(sampleRowsFilter.getPeriodFrom(), sampleRowsFilter.getPeriodTo()); this.months = periodDates.getMonths(); + + for (Date month : months) { + totalExpectedForMonths.put(month, new MutableInt()); + totalRealForMonths.put(month, new MutableInt()); + totalEstimatedForMonths.put(month, new MutableInt()); + } } public ObsMerSamplingPlanBuilder addSampleRow(SampleRow sampleRow, @@ -170,16 +177,22 @@ // get sector context ServiceContext sectorPart = facadeContext.getOrAddSectorContext(sectors); + // compute nb expected tides for sample row + int totalTidesExpected = getNbTidesExpected(sampleRow); + // compute nb real tides for sample row + int totalTidesReal = getNbTidesReal(sampleRow); + // compute nb estimated tides for sample row + int totalTidesEstimated = getNbTidesEstimated(sampleRow); + // add sample row - ObsMerSamplingPlan.ObsMerSamplingPlanSampleRowPart sampleRowPart = - sectorPart.addSampleRow(sampleRow, - observationTimesInDaysExpected, - observationTimesInDaysReal, - observationTimesInDaysEstimated); + sectorPart.addSampleRow(sampleRow, + totalTidesExpected, + totalTidesReal, + totalTidesEstimated, + observationTimesInDaysExpected, + observationTimesInDaysReal, + observationTimesInDaysEstimated); - // update totals - updateTotals(sampleRowPart); - // Sum total observation times in days observationTimesInDaysTotalExpected.add(observationTimesInDaysExpected); observationTimesInDaysTotalReal.add(observationTimesInDaysReal); @@ -233,45 +246,43 @@ return result; } - protected void updateTotals(ObsMerSamplingPlan.ObsMerSamplingPlanSampleRowPart sampleRowPart) { + protected int getNbTidesExpected(SampleRow sampleRow) { + int result = 0; for (Date month : months) { - - Integer expected = sampleRowPart.getNbTidesExpected(month); - Integer real = sampleRowPart.getNbTidesReal(month); - Integer estimated = sampleRowPart.getNbTidesEstimated(month); - - if (expected != null) { - - MutableInt total = totalExpectedForMonths.get(month); - if (total == null) { - total = new MutableInt(); - totalExpectedForMonths.put(month, total); - } - total.add(expected); - highTotalExpected.add(total); + Integer tidesValue = SampleRows.getExpectedTidesValue(sampleRow, month); + if (tidesValue != null) { + result += tidesValue; + totalExpectedForMonths.get(month).add(tidesValue); } + } + highTotalExpected.add(result); + return result; + } - if (estimated != null) { - - MutableInt total = totalEstimatedForMonths.get(month); - if (total == null) { - total = new MutableInt(); - totalEstimatedForMonths.put(month, total); - } - total.add(expected); - highTotalEstimated.add(total); + protected int getNbTidesReal(SampleRow sampleRow) { + int result = 0; + for (Date month : months) { + Integer tidesValue = SampleRows.getRealTidesValue(sampleRow, month); + if (tidesValue != null) { + result += tidesValue; + totalRealForMonths.get(month).add(tidesValue); } + } + highTotalReal.add(result); + return result; + } - if (real != null) { - MutableInt total = totalRealForMonths.get(month); - if (total == null) { - total = new MutableInt(); - totalRealForMonths.put(month, total); - } - total.add(real); - highTotalReal.add(total); + protected int getNbTidesEstimated(SampleRow sampleRow) { + int result = 0; + for (Date month : months) { + Integer tidesValue = SampleRows.getEstimatedTidesValue(sampleRow, month); + if (tidesValue != null) { + result += tidesValue; + totalEstimatedForMonths.get(month).add(tidesValue); } } + highTotalEstimated.add(result); + return result; } protected static class FacadeContext { @@ -328,12 +339,18 @@ } protected ObsMerSamplingPlan.ObsMerSamplingPlanSampleRowPart addSampleRow(SampleRow row, + int totalTidesExpected, + int totalTidesReal, + int totalTidesEstimated, Double observationTimesInDaysExpected, Long observationTimesInDaysReal, Long observationTimesInDaysEstimated) { ObsMerSamplingPlan.ObsMerSamplingPlanSampleRowPart rowPart = new ObsMerSamplingPlan.ObsMerSamplingPlanSampleRowPart(row, + totalTidesExpected, + totalTidesReal, + totalTidesEstimated, observationTimesInDaysExpected, observationTimesInDaysReal, observationTimesInDaysEstimated); 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-03-26 15:09:58 UTC (rev 1758) +++ trunk/wao-services/src/main/java/fr/ifremer/wao/services/service/ObsMerSamplingPlanService.java 2014-03-26 15:36:29 UTC (rev 1759) @@ -21,12 +21,14 @@ * #L% */ +import com.google.common.base.Charsets; import com.google.common.base.Optional; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Iterables; import com.google.common.collect.Maps; +import com.google.common.collect.Range; import com.google.common.collect.Sets; import fr.ifremer.wao.ContactsFilter; import fr.ifremer.wao.SampleRowsFilter; @@ -79,7 +81,6 @@ import org.nuiton.util.DateUtil; import java.io.InputStream; -import java.nio.charset.Charset; import java.text.ParseException; import java.util.ArrayList; import java.util.Calendar; @@ -375,13 +376,36 @@ SampleRowTopiaDao dao = getSampleRowDao(); + // recuperation des lignes du plan List<SampleRow> sampleRows = dao.findAll(sampleRowsFilter); + + // creation du plan d'echantillonnage ObsMerSamplingPlanBuilder builder = new ObsMerSamplingPlanBuilder(sampleRowsFilter); + + Date periodFrom = sampleRowsFilter.getPeriodFrom(); + Date periodTo = sampleRowsFilter.getPeriodTo(); + for (SampleRow sampleRow : sampleRows) { - Double observationTimesInDaysExpected = getObservationTimesInDayExpected(sampleRow); - Pair<Long, Long> realAndEstimated = getObservationTimesInDayRealAndEstimated(sampleRowsFilter, sampleRow); + + // calcul effort plannifie + Double observationTimesInDaysExpected = + getObservationTimesInDayExpected(periodFrom, + periodTo, + sampleRow); + + // calcul effort réalisé et estimé + SampleRowsFilter sampleRowsFilter1 = new SampleRowsFilter(); + sampleRowsFilter1.setObsProgram(sampleRowsFilter.getObsProgram()); + sampleRowsFilter1.setPeriodFrom(periodFrom); + sampleRowsFilter1.setPeriodTo(periodTo); + sampleRowsFilter1.setSampleRowIds(ImmutableSet.of(sampleRow.getTopiaId())); + + Pair<Long, Long> realAndEstimated = + getSampleRowObservationTimesInDayRealAndEstimated(sampleRowsFilter1); Long observationTimesInDaysReal = realAndEstimated.getLeft(); Long observationTimesInDaysEstimated = realAndEstimated.getRight(); + + // ajout de la ligne au build de plan builder.addSampleRow(sampleRow, observationTimesInDaysExpected, observationTimesInDaysReal, @@ -555,9 +579,9 @@ try { - String csvContent = export.toString(Charset.forName("UTF-8")); + String csvContent = export.toString(Charsets.UTF_8); - InputStream csvInputStream = IOUtils.toInputStream(csvContent, "UTF-8"); + InputStream csvInputStream = IOUtils.toInputStream(csvContent, Charsets.UTF_8); return csvInputStream; @@ -567,7 +591,21 @@ } - protected double getObservationTimesInDayExpected(SampleRow sampleRow) { + /** + * Récupérer l'effort d'observation en nombre de jours plannifié poru + * le sample row donné pendant la période donnée. + * + * @param periodFrom début de période où rechercher les contacts + * @param periodTo fin de période où rechercher les contacts + * @param sampleRow la ligne à filtrer + * @return l'effort plannifié calculé + */ + public double getObservationTimesInDayExpected(Date periodFrom, + Date periodTo, + SampleRow sampleRow) { + + Range<Date> period = Range.closed(periodFrom, periodTo); + double expected; // On prend le nombre d'observation prévue pour cette ligne du plan @@ -575,7 +613,11 @@ // chaque mois. int expectedObservationCount = 0; for (SampleMonth sampleMonth : sampleRow.getSampleMonth()) { - expectedObservationCount += sampleMonth.getExpectedTidesValue(); + Date periodDate = sampleMonth.getPeriodDate(); + + if (period.contains(periodDate)) { + expectedObservationCount += sampleMonth.getExpectedTidesValue(); + } } // le prévisionnel est égal au nombre d'observations attendues × // le nombre de jours moyen d'une observation @@ -583,20 +625,26 @@ return expected; } - protected Pair<Long, Long> getObservationTimesInDayRealAndEstimated(SampleRowsFilter sampleRowFilter, - SampleRow sampleRow) { + /** + * Récupérer l'effort d'observation en nombre de jours réalisé (partie gauche) et estimé (partie droite) + * pour le filtre d'échantillonnage donné. + * + * @param sampleRowsFilter la filtre du plan d'échantillonnage + * @return la paire (effort réalisé - effort estimé) calculée + */ + public Pair<Long, Long> getSampleRowObservationTimesInDayRealAndEstimated(SampleRowsFilter sampleRowsFilter) { // D'abord, on a besoin de récupérer toutes les observations réalisées // pour cette ligne du plan ContactsFilter contactFilter = new ContactsFilter(); - sampleRowFilter.setSampleRowIds(ImmutableSet.of(sampleRow.getTopiaId())); contactFilter.setFilterOnObservationBeginDate(true); - contactFilter.setPeriodFrom(sampleRowFilter.getPeriodFrom()); - contactFilter.setPeriodTo(sampleRowFilter.getPeriodTo()); + contactFilter.setPeriodFrom(sampleRowsFilter.getPeriodFrom()); + contactFilter.setPeriodTo(sampleRowsFilter.getPeriodTo()); + contactFilter.setSampleRowFilter(sampleRowsFilter); - contactFilter.setSampleRowFilter(sampleRowFilter); + contactFilter.setContactStates(ImmutableSet.of(ContactState.OBSERVATION_DONE)); // on exclue les contacts qui sont invalidés par le programme contactFilter.setProgramAcceptations(Sets.newHashSet(true, null)); @@ -627,9 +675,6 @@ // estimated observation estimated += contactInDays; } - - sampleRowFilter.setSampleRowIds(null); - return Pair.of(real, estimated); } 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-03-26 15:09:58 UTC (rev 1758) +++ trunk/wao-web/src/main/webapp/WEB-INF/content/obsmer/sampling-plan.jsp 2014-03-26 15:36:29 UTC (rev 1759) @@ -256,9 +256,9 @@ </s:iterator> <td> <span> - <s:property value="#sampleRow.totalTidesExpected" /> + <s:property value="#samplingPlanRow.totalTidesExpected" /> <s:if test="!authenticatedWaoUser.guest"> - <em>(<s:property value="#sampleRow.totalTidesReal" />)</em> + <em>(<s:property value="#samplingPlanRow.totalTidesReal" />)</em> <br/> <strong><s:property value="#samplingPlanRow.totalPercentage" /></strong> </s:if> @@ -368,7 +368,7 @@ </td> </s:iterator> <td> - <s:property value="#samplingPlan.highTotalExpected" /> + <s:property value="samplingPlan.highTotalExpected" /> <s:if test="!authenticatedWaoUser.guest"> <em>(<s:property value="samplingPlan.highTotalReal" />)</em> <strong><s:property value="samplingPlan.highTotalRatio" /></strong>
participants (1)
-
tchemit@users.forge.codelutin.com