From ed44373b2c05ff6b0c733dcd590bbb7f652f622a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93scar=20Gonz=C3=A1lez=20Fern=C3=A1ndez?= Date: Fri, 3 Sep 2010 13:22:06 +0200 Subject: [PATCH] Migrate OrderPlanningModel from BigDecimals to EffortDurations FEA: ItEr60S19TimeUnitDataType --- .../web/planner/chart/ChartFiller.java | 2 +- .../web/planner/order/OrderPlanningModel.java | 159 +++++++++--------- 2 files changed, 80 insertions(+), 81 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 ecde837ea..025159395 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 @@ -167,7 +167,7 @@ public abstract class ChartFiller implements IChartFiller { return sum; } - private static EffortDuration availabilityFor(Resource resource, + protected static EffortDuration availabilityFor(Resource resource, LocalDate day) { return resource.getCalendarOrDefault().getCapacityDurationAt(day); } diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/order/OrderPlanningModel.java b/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/order/OrderPlanningModel.java index 051c11477..723550cdd 100644 --- a/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/order/OrderPlanningModel.java +++ b/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/order/OrderPlanningModel.java @@ -21,6 +21,7 @@ package org.navalplanner.web.planner.order; import static org.navalplanner.business.common.AdHocTransactionService.readOnlyProxy; +import static org.navalplanner.business.workingday.EffortDuration.zero; import static org.navalplanner.web.I18nHelper._; import java.math.BigDecimal; @@ -42,8 +43,6 @@ import org.apache.commons.logging.LogFactory; import org.hibernate.Hibernate; import org.joda.time.DateTime; import org.joda.time.LocalDate; -import org.navalplanner.business.calendars.entities.BaseCalendar; -import org.navalplanner.business.calendars.entities.SameWorkHoursEveryDay; import org.navalplanner.business.common.IAdHocTransactionService; import org.navalplanner.business.common.IOnTransaction; import org.navalplanner.business.common.daos.IConfigurationDAO; @@ -81,6 +80,7 @@ import org.navalplanner.business.users.entities.OrderAuthorization; import org.navalplanner.business.users.entities.OrderAuthorizationType; import org.navalplanner.business.users.entities.User; import org.navalplanner.business.users.entities.UserRole; +import org.navalplanner.business.workingday.EffortDuration; import org.navalplanner.web.calendars.BaseCalendarModel; import org.navalplanner.web.common.ViewSwitcher; import org.navalplanner.web.planner.ITaskElementAdapter; @@ -1140,11 +1140,11 @@ public abstract class OrderPlanningModel implements IOrderPlanningModel { private final Order order; - private SortedMap mapOrderLoad = new TreeMap(); - private SortedMap mapOrderOverload = new TreeMap(); - private SortedMap mapMaxCapacity = new TreeMap(); - private SortedMap mapOtherLoad = new TreeMap(); - private SortedMap mapOtherOverload = new TreeMap(); + private SortedMap mapOrderLoad = new TreeMap(); + private SortedMap mapOrderOverload = new TreeMap(); + private SortedMap mapMaxCapacity = new TreeMap(); + private SortedMap mapOtherLoad = new TreeMap(); + private SortedMap mapOtherOverload = new TreeMap(); public OrderLoadChartFiller(Order orderReloaded) { this.order = orderReloaded; @@ -1162,28 +1162,28 @@ public abstract class OrderPlanningModel implements IOrderPlanningModel { resetMaps(); List orderDayAssignments = order.getDayAssignments(); - SortedMap> orderDayAssignmentsGrouped = groupDayAssignmentsByDayAndResource(orderDayAssignments); + SortedMap> orderDayAssignmentsGrouped = groupDurationsByDayAndResource(orderDayAssignments); List resourcesDayAssignments = new ArrayList(); for (Resource resource : order.getResources()) { resourcesDayAssignments.addAll(assigmentsOnResourceCalculator .getAssignments(resource)); } - SortedMap> resourceDayAssignmentsGrouped = groupDayAssignmentsByDayAndResource(resourcesDayAssignments); + SortedMap> resourceDayAssignmentsGrouped = groupDurationsByDayAndResource(resourcesDayAssignments); fillMaps(orderDayAssignmentsGrouped, resourceDayAssignmentsGrouped); convertAsNeededByZoomMaps(); - Plotinfo plotOrderLoad = createPlotinfo( + Plotinfo plotOrderLoad = createPlotinfoFromDurations( mapOrderLoad, interval); - Plotinfo plotOrderOverload = createPlotinfo( + Plotinfo plotOrderOverload = createPlotinfoFromDurations( mapOrderOverload, interval); - Plotinfo plotMaxCapacity = createPlotinfo( + Plotinfo plotMaxCapacity = createPlotinfoFromDurations( mapMaxCapacity, interval); - Plotinfo plotOtherLoad = createPlotinfo( + Plotinfo plotOtherLoad = createPlotinfoFromDurations( mapOtherLoad, interval); - Plotinfo plotOtherOverload = createPlotinfo( + Plotinfo plotOtherOverload = createPlotinfoFromDurations( mapOtherOverload, interval); @@ -1228,96 +1228,95 @@ public abstract class OrderPlanningModel implements IOrderPlanningModel { } private void convertAsNeededByZoomMaps() { - mapOrderLoad = convertAsNeededByZoom(mapOrderLoad); - mapOrderOverload = convertAsNeededByZoom(mapOrderOverload); - mapMaxCapacity = convertAsNeededByZoom(mapMaxCapacity); - mapOtherLoad = convertAsNeededByZoom(mapOtherLoad); - mapOtherOverload = convertAsNeededByZoom(mapOtherOverload); + mapOrderLoad = groupAsNeededByZoom(mapOrderLoad); + mapOrderOverload = groupAsNeededByZoom(mapOrderOverload); + mapMaxCapacity = groupAsNeededByZoom(mapMaxCapacity); + mapOtherLoad = groupAsNeededByZoom(mapOtherLoad); + mapOtherOverload = groupAsNeededByZoom(mapOtherOverload); } private void fillMaps( - SortedMap> orderDayAssignmentsGrouped, - SortedMap> resourceDayAssignmentsGrouped) { + SortedMap> orderDayAssignmentsGrouped, + SortedMap> resourceDayAssignmentsGrouped) { for (LocalDate day : orderDayAssignmentsGrouped.keySet()) { - Integer maxCapacity = getMaxCapcity(orderDayAssignmentsGrouped, - day); - mapMaxCapacity.put(day, new BigDecimal(maxCapacity)); + final EffortDuration maxCapacity = getSumCapacities( + orderDayAssignmentsGrouped, day); - Integer orderLoad = 0; - Integer orderOverload = 0; - Integer otherLoad = 0; - Integer otherOverload = 0; + EffortDuration orderLoad = zero(); + EffortDuration orderOverload = zero(); + EffortDuration otherLoad = zero(); + EffortDuration otherOverload = zero(); for (Resource resource : orderDayAssignmentsGrouped.get(day) .keySet()) { - int workableHours = getWorkableHours(resource, day); + final EffortDuration resourceCapacityHours = availabilityFor( + resource, day); - Integer hoursOrder = orderDayAssignmentsGrouped.get(day).get( - resource); + final EffortDuration durationAtOrder = orderDayAssignmentsGrouped + .get(day).get(resource); - // FIXME review why is null sometimes - Integer hoursOther = 0; - if ((resourceDayAssignmentsGrouped.get(day) != null) && (resourceDayAssignmentsGrouped - .get(day).get(resource) != null)) { - hoursOther = resourceDayAssignmentsGrouped.get(day) - .get(resource) - - hoursOrder; - } + final EffortDuration totalDurationForResource; + totalDurationForResource = retrieveTotalDurationForResource( + resourceDayAssignmentsGrouped, day, resource); - if (hoursOrder <= workableHours) { - orderLoad += hoursOrder; - orderOverload += 0; - } else { - orderLoad += workableHours; - orderOverload += hoursOrder - workableHours; - } + final EffortDuration durationOther; + durationOther = totalDurationForResource.minus(min( + durationAtOrder, totalDurationForResource)); - if ((hoursOrder + hoursOther) <= workableHours) { - otherLoad += hoursOther; - otherOverload += 0; - } else { - if (hoursOrder <= workableHours) { - otherLoad += (workableHours - hoursOrder); - otherOverload += hoursOrder + hoursOther - - workableHours; - } else { - otherLoad += 0; - otherOverload += hoursOther; - } - } + final EffortDuration allDuration = durationAtOrder + .plus(durationOther); + + final EffortDuration orderLoadIncrement; + orderLoadIncrement = min(durationAtOrder, + resourceCapacityHours); + + final EffortDuration orderOverloadIncrement; + orderOverloadIncrement = durationAtOrder + .minus(orderLoadIncrement); + + final EffortDuration otherLoadIncrement; + otherLoadIncrement = min(allDuration, resourceCapacityHours) + .minus(min(resourceCapacityHours, durationAtOrder)); + + final EffortDuration otherOverloadIncrement; + otherOverloadIncrement = durationOther + .minus(otherLoadIncrement); + + orderLoad = orderLoad.plus(orderLoadIncrement); + orderOverload = orderOverload.plus(orderOverloadIncrement); + otherLoad = otherLoad.plus(otherLoadIncrement); + otherOverload = otherOverload.plus(otherOverloadIncrement); } - - mapOrderLoad.put(day, new BigDecimal(orderLoad)); - mapOrderOverload.put(day, new BigDecimal(orderOverload - + maxCapacity)); - mapOtherLoad.put(day, new BigDecimal(otherLoad + orderLoad)); - mapOtherOverload.put(day, new BigDecimal(otherOverload - + orderOverload + maxCapacity)); + mapMaxCapacity.put(day, maxCapacity); + mapOrderLoad.put(day, orderLoad); + mapOrderOverload.put(day, orderOverload.plus(maxCapacity)); + mapOtherLoad.put(day, otherLoad.plus(orderLoad)); + mapOtherOverload.put(day, otherOverload.plus(orderOverload) + .plus(maxCapacity)); } } - private int getMaxCapcity( - SortedMap> orderDayAssignmentsGrouped, + private EffortDuration getSumCapacities( + SortedMap> orderDayAssignmentsGrouped, LocalDate day) { - int maxCapacity = 0; + EffortDuration result = zero(); for (Resource resource : orderDayAssignmentsGrouped.get(day) .keySet()) { - maxCapacity += getWorkableHours(resource, day); + result = result.plus(availabilityFor(resource, day)); } - return maxCapacity; + return result; } - private int getWorkableHours(Resource resource, LocalDate day) { - BaseCalendar calendar = resource.getCalendar(); - - int workableHours = SameWorkHoursEveryDay.getDefaultWorkingDay() - .getCapacityAt(day); - if (calendar != null) { - workableHours = calendar.getCapacityAt(day); + private EffortDuration retrieveTotalDurationForResource( + SortedMap> resourceDayAssignmentsGrouped, + LocalDate day, Resource resource) { + // FIXME review why is null sometimes + if (resourceDayAssignmentsGrouped.get(day) != null + && resourceDayAssignmentsGrouped.get(day).get(resource) != null) { + return resourceDayAssignmentsGrouped.get(day).get(resource); } - - return workableHours; + return zero(); } }