ItEr36S11CUCreacionUnidadesPlanificacionItEr35S12: Using the resources carried by AllocationBeingModified.

This commit is contained in:
Óscar González Fernández 2009-11-23 13:29:43 +01:00
parent 128c64cdbb
commit fb073540a5
7 changed files with 35 additions and 99 deletions

View file

@ -26,10 +26,8 @@ import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import org.apache.commons.lang.Validate;
@ -109,7 +107,7 @@ public abstract class ResourceAllocation<T extends DayAssignment> extends
public static class AllocationsCurried {
private final List<AllocationBeingModified> resourceAllocations;
private final List<AllocationBeingModified> allocations;
private final Task task;
@ -121,7 +119,7 @@ public abstract class ResourceAllocation<T extends DayAssignment> extends
checkNoOneHasNullTask(resourceAllocations);
checkAllHaveSameTask(resourceAllocations);
checkNoAllocationWithZeroResourcesPerDay(resourceAllocations);
this.resourceAllocations = resourceAllocations;
this.allocations = resourceAllocations;
this.task = resourceAllocations.get(0).getBeingModified()
.getTask();
}
@ -165,51 +163,9 @@ public abstract class ResourceAllocation<T extends DayAssignment> extends
}
}
public AllocationsAndResourcesCurried withResources(
List<Resource> resources) {
Validate.noNullElements(resources);
return new AllocationsAndResourcesCurried(task, resources,
resourceAllocations);
}
public AllocationsAndResourcesCurried withExistentResources() {
return new AllocationsAndResourcesCurried(task,
getResourcesFrom(AllocationBeingModified
.stripResourcesPerDay(resourceAllocations)),
resourceAllocations);
}
private List<Resource> getResourcesFrom(
List<ResourceAllocation<?>> allocations) {
Set<Resource> resources = new HashSet<Resource>();
for (ResourceAllocation<?> resourceAllocation : allocations) {
resources.addAll(resourceAllocation.getAssociatedResources());
}
return new ArrayList<Resource>(resources);
}
}
public static class AllocationsAndResourcesCurried {
private List<Resource> resources;
private List<AllocationBeingModified> allocations;
private final Task task;
public AllocationsAndResourcesCurried(Task task,
List<Resource> resources,
List<AllocationBeingModified> allocations) {
this.task = task;
this.resources = resources;
this.allocations = allocations;
}
public LocalDate untilAllocating(int hoursToAllocate) {
if (thereIsGenericAllocation()) {
Validate.notEmpty(resources, "there must exist workers");
}
AllocatorForSpecifiedResourcesPerDayAndHours allocator = new AllocatorForSpecifiedResourcesPerDayAndHours(
task, resources, allocations) {
task, allocations) {
@Override
protected List<DayAssignment> createAssignmentsAtDay(
@ -232,18 +188,9 @@ public abstract class ResourceAllocation<T extends DayAssignment> extends
return allocator.untilAllocating(hoursToAllocate);
}
private boolean thereIsGenericAllocation() {
for (AllocationBeingModified r : allocations) {
if (r.getBeingModified() instanceof GenericResourceAllocation) {
return true;
}
}
return false;
}
public void allocateOnTaskLength() {
AllocatorForTaskDurationAndSpecifiedResourcesPerDay allocator = new AllocatorForTaskDurationAndSpecifiedResourcesPerDay(
allocations, resources);
allocations);
allocator.allocateOnTaskLength();
}
}

View file

@ -293,12 +293,10 @@ public class Task extends TaskElement {
}
switch (calculatedValue) {
case NUMBER_OF_HOURS:
ResourceAllocation.allocating(allocations).withExistentResources()
.allocateOnTaskLength();
ResourceAllocation.allocating(allocations).allocateOnTaskLength();
break;
case END_DATE:
LocalDate end = ResourceAllocation.allocating(allocations)
.withExistentResources()
.untilAllocating(getAssignedHours());
setEndDate(end.toDateTimeAtStartOfDay().toDate());
break;

View file

@ -28,6 +28,7 @@ import java.util.List;
import org.apache.commons.lang.Validate;
import org.navalplanner.business.planner.entities.ResourceAllocation;
import org.navalplanner.business.planner.entities.ResourcesPerDay;
import org.navalplanner.business.planner.entities.SpecificResourceAllocation;
import org.navalplanner.business.resources.entities.Resource;
public class AllocationBeingModified {
@ -55,6 +56,10 @@ public class AllocationBeingModified {
ResourceAllocation<?> resourceAllocation,
ResourcesPerDay resourcesPerDay,
Collection<? extends Resource> resources) {
Validate.noNullElements(resources);
Validate
.isTrue(resourceAllocation instanceof SpecificResourceAllocation
|| !resources.isEmpty());
this.beingModified = resourceAllocation;
this.goal = resourcesPerDay;
this.resourcesOnWhichApplyAllocation = Collections
@ -78,4 +83,8 @@ public class AllocationBeingModified {
}
return result;
}
public List<Resource> getResources() {
return resourcesOnWhichApplyAllocation;
}
}

View file

@ -40,17 +40,13 @@ public abstract class AllocatorForSpecifiedResourcesPerDayAndHours {
private final Task task;
private List<Resource> resources;
private List<AllocationBeingModified> allocations;
private Map<AllocationBeingModified, List<DayAssignment>> resultAssignments = new HashMap<AllocationBeingModified, List<DayAssignment>>();
public AllocatorForSpecifiedResourcesPerDayAndHours(Task task,
List<Resource> resources,
List<AllocationBeingModified> allocations) {
this.task = task;
this.resources = resources;
this.allocations = allocations;
initializeResultsMap();
}
@ -105,7 +101,8 @@ public abstract class AllocatorForSpecifiedResourcesPerDayAndHours {
ResourcesPerDay resourcesPerDay = withResourcesPerDay
.getGoal();
List<DayAssignment> assignments = createAssignmentsAtDay(
resourceAllocation, resources, day,
resourceAllocation, withResourcesPerDay.getResources(),
day,
resourcesPerDay, maxPerAllocations.get(i));
resultAssignments.get(withResourcesPerDay).addAll(assignments);
total += DayAssignment.sum(assignments);

View file

@ -32,27 +32,25 @@ public class AllocatorForTaskDurationAndSpecifiedResourcesPerDay {
private List<AllocationBeingModified> allocations;
private List<Resource> resources;
public AllocatorForTaskDurationAndSpecifiedResourcesPerDay(
List<AllocationBeingModified> allocations,
List<Resource> resources) {
List<AllocationBeingModified> allocations) {
this.allocations = allocations;
this.resources = resources;
}
public void allocateOnTaskLength() {
for (AllocationBeingModified allocation : allocations) {
doAllocationForFixedTask(allocation.getBeingModified(),
allocation.getGoal());
doAllocationForFixedTask(allocation);
}
}
private void doAllocationForFixedTask(ResourceAllocation<?> allocation,
ResourcesPerDay resourcesPerDay) {
private void doAllocationForFixedTask(
AllocationBeingModified allocationBeingModified) {
ResourceAllocation<?> allocation = allocationBeingModified
.getBeingModified();
ResourcesPerDay resourcesPerDay = allocationBeingModified.getGoal();
if (allocation instanceof GenericResourceAllocation) {
doAllocation((GenericResourceAllocation) allocation,
resourcesPerDay);
resourcesPerDay, allocationBeingModified.getResources());
} else {
SpecificResourceAllocation specific = (SpecificResourceAllocation) allocation;
doAllocation(specific, resourcesPerDay);
@ -65,7 +63,7 @@ public class AllocatorForTaskDurationAndSpecifiedResourcesPerDay {
}
private void doAllocation(GenericResourceAllocation generic,
ResourcesPerDay resourcesPerDay) {
ResourcesPerDay resourcesPerDay, List<Resource> resources) {
generic.forResources(resources).allocate(resourcesPerDay);
}

View file

@ -75,15 +75,14 @@ public class AllocationUntilFillingHoursTest {
public void theNewEndDateIsWhenAllTheHoursAreAllocated() {
givenSpecificAllocations(ResourcesPerDay.amount(2));
LocalDate endDate = ResourceAllocation.allocating(allocations)
.withResources(resources).untilAllocating(32);
.untilAllocating(32);
assertThat(endDate, equalTo(startDate.plusDays(2)));
}
@Test
public void allTheRequestedHoursAreAssignedFor() {
givenSpecificAllocations(ResourcesPerDay.amount(2));
ResourceAllocation.allocating(allocations).withResources(resources)
.untilAllocating(32);
ResourceAllocation.allocating(allocations).untilAllocating(32);
ResourceAllocation<?> allocation = allocations.get(0)
.getBeingModified();
assertThat(allocation.getAssignments(), haveHours(16, 16));
@ -93,8 +92,7 @@ public class AllocationUntilFillingHoursTest {
public void worksWellForSeveralSpecificAllocations() {
givenSpecificAllocations(ResourcesPerDay.amount(1), ResourcesPerDay
.amount(1));
ResourceAllocation.allocating(allocations).withResources(resources)
.untilAllocating(32);
ResourceAllocation.allocating(allocations).untilAllocating(32);
ResourceAllocation<?> first = allocations.get(0)
.getBeingModified();
ResourceAllocation<?> second = allocations.get(1)
@ -107,8 +105,7 @@ public class AllocationUntilFillingHoursTest {
public void theRemainderIsProportinallyDistributed() {
givenSpecificAllocations(ResourcesPerDay.amount(2), ResourcesPerDay
.amount(1));
ResourceAllocation.allocating(allocations).withResources(resources)
.untilAllocating(60);
ResourceAllocation.allocating(allocations).untilAllocating(60);
ResourceAllocation<?> first = allocations.get(0)
.getBeingModified();
ResourceAllocation<?> second = allocations.get(1)
@ -121,8 +118,7 @@ public class AllocationUntilFillingHoursTest {
public void withUnequalRatioWorksOk() {
givenSpecificAllocations(ResourcesPerDay.amount(1), ResourcesPerDay
.amount(new BigDecimal(0.5)));
ResourceAllocation.allocating(allocations).withResources(resources)
.untilAllocating(36);
ResourceAllocation.allocating(allocations).untilAllocating(36);
ResourceAllocation<?> first = allocations.get(0)
.getBeingModified();
ResourceAllocation<?> second = allocations.get(1)
@ -137,8 +133,7 @@ public class AllocationUntilFillingHoursTest {
givenGenericAllocation(ResourcesPerDay.amount(2));
givenSpecificAllocations(ResourcesPerDay.amount(1), ResourcesPerDay
.amount(1));
ResourceAllocation.allocating(allocations).withResources(resources)
.untilAllocating(64);
ResourceAllocation.allocating(allocations).untilAllocating(64);
ResourceAllocation<?> generic = allocations.get(0)
.getBeingModified();
ResourceAllocation<?> firstSpecific = allocations.get(1)
@ -154,26 +149,21 @@ public class AllocationUntilFillingHoursTest {
public void withGenericAllocationWithNoResourcesPerDay() {
givenWorkers(1);
givenGenericAllocation(ResourcesPerDay.amount(0));
ResourceAllocation.allocating(allocations).withResources(resources)
.untilAllocating(100);
ResourceAllocation.allocating(allocations).untilAllocating(100);
}
@Test(expected = IllegalArgumentException.class)
public void cannotDoAGenericAllocationWithoutWorkers() {
givenWorkers(0);
givenGenericAllocation(ResourcesPerDay.amount(2));
ResourceAllocation.allocating(allocations)
.withResources(resources)
.untilAllocating(100);
ResourceAllocation.allocating(allocations).untilAllocating(100);
}
@Test
public void withoutWorkersYouCanDoSpecificAllocation() {
givenWorkers(0);
givenSpecificAllocations(ResourcesPerDay.amount(2));
ResourceAllocation.allocating(allocations).withResources(resources)
.untilAllocating(100);
ResourceAllocation.allocating(allocations).untilAllocating(100);
}
private void givenGenericAllocation(ResourcesPerDay resourcesPerDay) {

View file

@ -207,14 +207,11 @@ public class ResourceAllocationsBeingEdited {
List<AllocationBeingModified> allocations = asList(fromDetachedToAttached);
switch (calculatedValue) {
case NUMBER_OF_HOURS:
ResourceAllocation.allocating(allocations)
.withResources(
resourcesMatchingCriterions).allocateOnTaskLength();
ResourceAllocation.allocating(allocations).allocateOnTaskLength();
daysDuration = task.getDaysDuration();
break;
case END_DATE:
LocalDate end = ResourceAllocation.allocating(allocations)
.withResources(resourcesMatchingCriterions)
.untilAllocating(formBinder.getAssignedHours());
daysDuration = from(task.getStartDate(), end);
break;