Add a map in MoneyCostCalculator to cache calculated values

This prevents to recalculate money cost when it was already calculated before.
The map with the cached values is reseted every time you enter in a project
(when PlanningState is created).

FEA: ItEr76S17MoneyCostMonitoringSystem
This commit is contained in:
Manuel Rego Casasnovas 2012-03-26 10:39:15 +02:00
parent 120534e6ff
commit 65fcc3e0f1
3 changed files with 47 additions and 8 deletions

View file

@ -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();
}

View file

@ -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}.<br />
*
* 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.<br />
*
* 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 <mrego@igalia.com>
*/
/**
* @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<OrderElement, BigDecimal> moneyCostMap = new HashMap<OrderElement, BigDecimal>();
@Override
public void resetMoneyCostMap() {
moneyCostMap = new HashMap<OrderElement, BigDecimal>();
}
@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<WorkReportLine> 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;
}
/**

View file

@ -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<TaskSourceSynchronization> synchronizationsNeeded = order
.calculateSynchronizationsNeeded();
@ -274,6 +278,9 @@ public class PlanningStateCreator {
currentScenario);
forceLoadOfWorkingHours(result.getInitial());
moneyCostCalculator.resetMoneyCostMap();
return result;
}