diff --git a/navalplanner-business/src/main/java/org/navalplanner/business/planner/entities/AggregateOfResourceAllocations.java b/navalplanner-business/src/main/java/org/navalplanner/business/planner/entities/AggregateOfResourceAllocations.java index 1c6d51a8d..5f3425d53 100644 --- a/navalplanner-business/src/main/java/org/navalplanner/business/planner/entities/AggregateOfResourceAllocations.java +++ b/navalplanner-business/src/main/java/org/navalplanner/business/planner/entities/AggregateOfResourceAllocations.java @@ -81,4 +81,32 @@ public class AggregateOfResourceAllocations { return sum; } + public LocalDate getStart() { + if(isEmpty()){ + throw new IllegalStateException("the aggregate is empty"); + } + return getAllocationsSortedByStartDate().get(0).getStartDate(); + } + + public LocalDate getEnd(){ + if(isEmpty()){ + throw new IllegalStateException("the aggregate is empty"); + } + LocalDate result = null; + for (ResourceAllocation allocation : resourceAllocations) { + result = bigger(allocation.getEndDate(), result); + } + return result; + } + + private LocalDate bigger(LocalDate one, LocalDate other) { + if (one == null) { + return other; + } + if (other == null) { + return one; + } + return one.compareTo(other) > 0 ? one : other; + } + } diff --git a/navalplanner-business/src/main/java/org/navalplanner/business/planner/entities/Task.java b/navalplanner-business/src/main/java/org/navalplanner/business/planner/entities/Task.java index d20ecdb1c..7246798c7 100644 --- a/navalplanner-business/src/main/java/org/navalplanner/business/planner/entities/Task.java +++ b/navalplanner-business/src/main/java/org/navalplanner/business/planner/entities/Task.java @@ -30,6 +30,7 @@ import org.apache.commons.lang.Validate; import org.hibernate.validator.NotNull; import org.joda.time.DateTime; import org.joda.time.Days; +import org.joda.time.LocalDate; import org.navalplanner.business.orders.entities.HoursGroup; import org.navalplanner.business.resources.entities.Criterion; import org.navalplanner.business.resources.entities.Resource; @@ -246,10 +247,14 @@ public class Task extends TaskElement { } public void mergeAllocation(CalculatedValue calculatedValue, - Integer daysDuration, List> newAllocations, + AggregateOfResourceAllocations aggregate, + List> newAllocations, List modifications) { this.calculatedValue = calculatedValue; - setDaysDuration(daysDuration); + final LocalDate start = aggregate.getStart(); + final LocalDate end = aggregate.getEnd(); + setStartDate(start.toDateTimeAtStartOfDay().toDate()); + setDaysDuration(Days.daysBetween(start, end).getDays()); addAllocations(newAllocations); for (ModifiedAllocation pair : modifications) { Validate.isTrue(resourceAllocations.contains(pair.getOriginal())); diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/allocation/AllocationResult.java b/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/allocation/AllocationResult.java index 7069cad4b..08d6f46e1 100644 --- a/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/allocation/AllocationResult.java +++ b/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/allocation/AllocationResult.java @@ -62,8 +62,6 @@ public class AllocationResult { private final CalculatedValue calculatedValue; - private List> allSortedByStartDate; - private final Task task; AllocationResult( @@ -119,16 +117,12 @@ public class AllocationResult { } public void applyTo(Task task) { - task.mergeAllocation(getCalculatedValue(), getDaysDuration(), getNew(), + task.mergeAllocation(getCalculatedValue(), aggregate, getNew(), getModified()); } public List> getAllSortedByStartDate() { - if (allSortedByStartDate != null) { - return allSortedByStartDate; - } - return allSortedByStartDate = aggregate - .getAllocationsSortedByStartDate(); + return aggregate.getAllocationsSortedByStartDate(); } public Task getTask() { diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/allocation/ResourceAllocationModel.java b/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/allocation/ResourceAllocationModel.java index e529bfe9c..0d32630d7 100644 --- a/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/allocation/ResourceAllocationModel.java +++ b/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/allocation/ResourceAllocationModel.java @@ -20,6 +20,7 @@ package org.navalplanner.web.planner.allocation; +import java.util.Date; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -130,7 +131,13 @@ public class ResourceAllocationModel implements IResourceAllocationModel { private void doTheAllocation(AllocationResult allocationResult) { allocationResult.applyTo(task); - ganttTask.setEndDate(task.getEndDate()); + ganttTask.setBeginDate(toDate(allocationResult.getAggregate() + .getStart())); + ganttTask.setEndDate(toDate(allocationResult.getAggregate().getEnd())); + } + + private Date toDate(LocalDate start) { + return start.toDateTimeAtStartOfDay().toDate(); } private List getResourcesMatchingCriterions() {