diff --git a/libreplan-business/src/main/java/org/libreplan/business/planner/entities/IMoneyCostCalculator.java b/libreplan-business/src/main/java/org/libreplan/business/planner/entities/IMoneyCostCalculator.java index 2f6f21230..21d1a9ea2 100644 --- a/libreplan-business/src/main/java/org/libreplan/business/planner/entities/IMoneyCostCalculator.java +++ b/libreplan-business/src/main/java/org/libreplan/business/planner/entities/IMoneyCostCalculator.java @@ -50,4 +50,10 @@ public interface IMoneyCostCalculator { */ BigDecimal getMoneyCost(OrderElement orderElement); + /** + * Resets the map used to save cached values of money cost for each + * {@link OrderElement} + */ + void resetMoneyCostMap(); + } diff --git a/libreplan-business/src/main/java/org/libreplan/business/planner/entities/MoneyCostCalculator.java b/libreplan-business/src/main/java/org/libreplan/business/planner/entities/MoneyCostCalculator.java index 6089c2e13..38129f43a 100644 --- a/libreplan-business/src/main/java/org/libreplan/business/planner/entities/MoneyCostCalculator.java +++ b/libreplan-business/src/main/java/org/libreplan/business/planner/entities/MoneyCostCalculator.java @@ -21,7 +21,9 @@ package org.libreplan.business.planner.entities; import java.math.BigDecimal; import java.math.RoundingMode; +import java.util.HashMap; import java.util.List; +import java.util.Map; import org.libreplan.business.costcategories.daos.IHourCostDAO; import org.libreplan.business.orders.entities.OrderElement; @@ -33,17 +35,16 @@ import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; /** - * Class to calculate the money cost of a {@link TaskElement}. + * Class to calculate the money cost of a {@link TaskElement}.
* * Money cost is calculated checking the hours reported for that task and using - * the cost category of each resource in the different dates. + * the cost category of each resource in the different dates.
+ * + * Money cost is stored in a map that will be cached in memeroy. This map could + * be reseted when needed with method {@code resetMoneyCostMap}. * * @author Manuel Rego Casasnovas */ -/** - * @author mrego - * - */ @Component @Scope(BeanDefinition.SCOPE_SINGLETON) public class MoneyCostCalculator implements IMoneyCostCalculator { @@ -54,10 +55,35 @@ public class MoneyCostCalculator implements IMoneyCostCalculator { @Autowired private IHourCostDAO hourCostDAO; + private Map moneyCostMap = new HashMap(); + + @Override + public void resetMoneyCostMap() { + moneyCostMap = new HashMap(); + } + @Override public BigDecimal getMoneyCost(OrderElement orderElement) { + BigDecimal result = moneyCostMap.get(orderElement); + if (result != null) { + return result; + } + + result = BigDecimal.ZERO.setScale(2); + for (OrderElement each : orderElement.getChildren()) { + result = result.add(getMoneyCost(each)); + } + + result = result.add(getMoneyCostFromOwnWorkReportLines(orderElement)) + .setScale(2, RoundingMode.HALF_UP); + + moneyCostMap.put(orderElement, result); + return result; + } + + private BigDecimal getMoneyCostFromOwnWorkReportLines(OrderElement orderElement) { List workReportLines = workReportLineDAO - .findByOrderElementAndChildren(orderElement, false); + .findByOrderElement(orderElement); BigDecimal result = BigDecimal.ZERO.setScale(2); for (WorkReportLine workReportLine : workReportLines) { @@ -78,7 +104,7 @@ public class MoneyCostCalculator implements IMoneyCostCalculator { result = result.add(cost); } - return result.setScale(2, RoundingMode.HALF_UP); + return result; } /** diff --git a/libreplan-webapp/src/main/java/org/libreplan/web/planner/order/PlanningStateCreator.java b/libreplan-webapp/src/main/java/org/libreplan/web/planner/order/PlanningStateCreator.java index f5dec3374..ff8af3495 100644 --- a/libreplan-webapp/src/main/java/org/libreplan/web/planner/order/PlanningStateCreator.java +++ b/libreplan-webapp/src/main/java/org/libreplan/web/planner/order/PlanningStateCreator.java @@ -55,6 +55,7 @@ import org.libreplan.business.planner.entities.DayAssignment; import org.libreplan.business.planner.entities.Dependency; import org.libreplan.business.planner.entities.DerivedAllocation; import org.libreplan.business.planner.entities.GenericResourceAllocation; +import org.libreplan.business.planner.entities.IMoneyCostCalculator; import org.libreplan.business.planner.entities.ResourceAllocation; import org.libreplan.business.planner.entities.ResourceAllocation.IVisitor; import org.libreplan.business.planner.entities.SpecificResourceAllocation; @@ -171,6 +172,9 @@ public class PlanningStateCreator { @Autowired private IOrderAuthorizationDAO orderAuthorizationDAO; + @Autowired + private IMoneyCostCalculator moneyCostCalculator; + void synchronizeWithSchedule(Order order, IOptionalPersistence persistence) { List synchronizationsNeeded = order .calculateSynchronizationsNeeded(); @@ -274,6 +278,9 @@ public class PlanningStateCreator { currentScenario); forceLoadOfWorkingHours(result.getInitial()); + + moneyCostCalculator.resetMoneyCostMap(); + return result; }