From 2745cf68c5a58d96044eedb67b46fe6bd413e080 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93scar=20Gonz=C3=A1lez=20Fern=C3=A1ndez?= Date: Thu, 2 Sep 2010 18:23:26 +0200 Subject: [PATCH] Add new methods that work with durations instead of hours The old methods are kept to not break existing code and allow a gradual phase out. They are marked as deprecated. FEA: ItEr60S19TimeUnitDataType --- .../web/planner/chart/ChartFiller.java | 123 ++++++++++++++++-- 1 file changed, 115 insertions(+), 8 deletions(-) diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/chart/ChartFiller.java b/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/chart/ChartFiller.java index 12d45a904..6b2dcd445 100644 --- a/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/chart/ChartFiller.java +++ b/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/chart/ChartFiller.java @@ -20,6 +20,8 @@ package org.navalplanner.web.planner.chart; +import static org.navalplanner.business.workingday.EffortDuration.zero; + import java.io.IOException; import java.io.PrintWriter; import java.math.BigDecimal; @@ -41,6 +43,7 @@ import javax.servlet.http.HttpServletResponse; import org.joda.time.Days; import org.joda.time.LocalDate; +import org.navalplanner.business.calendars.entities.BaseCalendar; import org.navalplanner.business.calendars.entities.ResourceCalendar; import org.navalplanner.business.calendars.entities.SameWorkHoursEveryDay; import org.navalplanner.business.planner.entities.DayAssignment; @@ -65,6 +68,7 @@ import org.zkoss.zk.ui.Executions; */ public abstract class ChartFiller implements IChartFiller { + @Deprecated protected abstract class HoursByDayCalculator { public SortedMap calculate( Collection elements) { @@ -94,6 +98,34 @@ public abstract class ChartFiller implements IChartFiller { } } + protected abstract class EffortByDayCalculator { + public SortedMap calculate( + Collection elements) { + SortedMap result = new TreeMap(); + if (elements.isEmpty()) { + return result; + } + for (T element : elements) { + if (included(element)) { + EffortDuration duration = getDurationFor(element); + LocalDate day = getDayFor(element); + EffortDuration previous = result.get(day); + previous = previous == null ? zero() : previous; + result.put(day, previous.plus(duration)); + } + } + return groupAsNeededByZoom(result); + } + + protected abstract LocalDate getDayFor(T element); + + protected abstract EffortDuration getDurationFor(T element); + + protected boolean included(T each) { + return true; + } + } + protected class DefaultDayAssignmentCalculator extends HoursByDayCalculator { public DefaultDayAssignmentCalculator() { @@ -110,6 +142,7 @@ public abstract class ChartFiller implements IChartFiller { } } + @Deprecated protected final int sumHoursForDay( Collection resources, LocalDate day) { @@ -120,6 +153,19 @@ public abstract class ChartFiller implements IChartFiller { return sum; } + protected static EffortDuration sumDurationsForDay( + Collection resources, LocalDate day) { + EffortDuration sum = zero(); + for (Resource resource : resources) { + sum = sum.plus(durationFor(resource, day)); + } + return sum; + } + + private static EffortDuration durationFor(Resource resource, LocalDate day) { + return resource.getCalendarOrDefault().getCapacityDurationAt(day); + } + private int hoursFor(Resource resource, LocalDate day) { int result = 0; ResourceCalendar calendar = resource.getCalendar(); @@ -316,7 +362,7 @@ public abstract class ChartFiller implements IChartFiller { } } - private LocalDate getThursdayOfThisWeek(LocalDate date) { + private static LocalDate getThursdayOfThisWeek(LocalDate date) { return date.dayOfWeek().withMinimumValue().plusDays(DAYS_TO_THURSDAY); } @@ -359,6 +405,7 @@ public abstract class ChartFiller implements IChartFiller { return result; } + @Deprecated protected SortedMap convertAsNeededByZoom( SortedMap map) { if (isZoomByDay()) { @@ -368,6 +415,41 @@ public abstract class ChartFiller implements IChartFiller { } } + protected SortedMap groupAsNeededByZoom( + SortedMap map) { + if (isZoomByDay()) { + return map; + } + return groupByWeekDurations(map); + } + + protected SortedMap groupByWeekDurations( + SortedMap map) { + return average(accumulatePerWeek(map)); + } + + private static SortedMap accumulatePerWeek( + SortedMap map) { + SortedMap result = new TreeMap(); + for (Entry each : map.entrySet()) { + LocalDate centerOfWeek = getThursdayOfThisWeek(each.getKey()); + EffortDuration accumulated = result.get(centerOfWeek); + accumulated = accumulated == null ? zero() : accumulated; + result.put(centerOfWeek, accumulated.plus(each.getValue())); + } + return result; + } + + private static SortedMap average( + SortedMap accumulatedPerWeek) { + SortedMap result = new TreeMap(); + for (Entry each : accumulatedPerWeek + .entrySet()) { + result.put(each.getKey(), each.getValue().divideBy(7)); + } + return result; + } + protected TimeGeometry getTimeGeometry(Interval interval) { LocalDate start = new LocalDate(interval.getStart()); LocalDate finish = new LocalDate(interval.getFinish()); @@ -401,21 +483,46 @@ public abstract class ChartFiller implements IChartFiller { return valueGeometry; } + @Deprecated protected SortedMap> groupDayAssignmentsByDayAndResource( List dayAssignments) { - SortedMap> map = new TreeMap>(); + SortedMap> original = groupDurationsByDayAndResource(dayAssignments); + SortedMap> result = new TreeMap>(); + for (Entry> each : original + .entrySet()) { + result.put(each.getKey(), toHoursInteger(each.getValue())); + } + return result; + } + + private Map toHoursInteger( + Map value) { + Map result = new HashMap(); + for (Entry each : value.entrySet()) { + result.put(each.getKey(), + BaseCalendar.roundToHours(each.getValue())); + } + return result; + } + + protected SortedMap> groupDurationsByDayAndResource( + List dayAssignments) { + SortedMap> map = new TreeMap>(); for (DayAssignment dayAssignment : dayAssignments) { final LocalDate day = dayAssignment.getDay(); - final int dayAssignmentHours = dayAssignment.getHours(); + final EffortDuration dayAssignmentDuration = dayAssignment + .getDuration(); Resource resource = dayAssignment.getResource(); if (map.get(day) == null) { - map.put(day, new HashMap()); + map.put(day, new HashMap()); } - Map forDay = map.get(day); - Integer previousHours = forDay.get(resource); - previousHours = previousHours != null ? previousHours : 0; - forDay.put(dayAssignment.getResource(), previousHours + dayAssignmentHours); + Map forDay = map.get(day); + EffortDuration previousDuration = forDay.get(resource); + previousDuration = previousDuration != null ? previousDuration + : EffortDuration.zero(); + forDay.put(dayAssignment.getResource(), + previousDuration.plus(dayAssignmentDuration)); } return map; }