ItEr25S07CUAsignacionGrupoRecursosAPlanificacionItEr24S08: Extracting superclass for doing allocation

This commit is contained in:
Óscar González Fernández 2009-09-15 23:43:53 +02:00
parent 5d2a856431
commit b7765d3e24
2 changed files with 68 additions and 46 deletions

View file

@ -8,7 +8,6 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import org.joda.time.Days;
import org.joda.time.LocalDate;
import org.navalplanner.business.calendars.entities.BaseCalendar;
import org.navalplanner.business.calendars.entities.CombinedWorkHours;
@ -85,13 +84,14 @@ public class GenericResourceAllocation extends ResourceAllocation {
return Collections.unmodifiableSet(criterions);
}
private class Allocation implements IAllocatable {
private class GenericAllocation extends
AssignmentsAllocation<GenericDayAssigment> {
private final List<Resource> resources;
private final List<IWorkHours> workHours;
public Allocation(List<Resource> resources) {
public GenericAllocation(List<Resource> resources) {
this.resources = resources;
this.workHours = new ArrayList<IWorkHours>();
for (Resource resource : resources) {
@ -99,7 +99,7 @@ public class GenericResourceAllocation extends ResourceAllocation {
}
}
private IWorkHours generateWorkHoursFor(Resource resource) {
private final IWorkHours generateWorkHoursFor(Resource resource) {
List<BaseCalendar> calendars = new ArrayList<BaseCalendar>();
if (resource.getCalendar() != null) {
calendars.add(resource.getCalendar());
@ -116,47 +116,7 @@ public class GenericResourceAllocation extends ResourceAllocation {
}
@Override
public void allocate(ResourcesPerDay resourcesPerDay) {
Task task = getTask();
LocalDate startInclusive = new LocalDate(task.getStartDate());
List<GenericDayAssigment> assigmentsCreated = new ArrayList<GenericDayAssigment>();
for (int i = 0; i < getDaysElapsedAt(task); i++) {
LocalDate day = startInclusive.plusDays(i);
int totalForDay = calculateTotalToDistribute(day,
resourcesPerDay);
assigmentsCreated.addAll(distributeForDay(day, totalForDay));
}
setResourcesPerDay(resourcesPerDay);
setAssigments(assigmentsCreated);
}
private int calculateTotalToDistribute(LocalDate day,
ResourcesPerDay resourcesPerDay) {
Integer workableHours = getWorkableHoursAt(day);
return resourcesPerDay.asHoursGivenResourceWorkingDayOf(workableHours);
}
private Integer getWorkableHoursAt(LocalDate day) {
if (getTaskCalendar() == null) {
return SameWorkHoursEveryDay.getDefaultWorkingDay()
.getWorkableHours(day);
} else {
return getTaskCalendar().getWorkableHours(day);
}
}
private BaseCalendar getTaskCalendar() {
return getTask().getCalendar();
}
private int getDaysElapsedAt(Task task) {
LocalDate endExclusive = new LocalDate(task.getEndDate());
Days daysBetween = Days.daysBetween(new LocalDate(task
.getStartDate()), endExclusive);
return daysBetween.getDays();
}
private List<GenericDayAssigment> distributeForDay(LocalDate day,
protected List<GenericDayAssigment> distributeForDay(LocalDate day,
int totalHours) {
List<GenericDayAssigment> result = new ArrayList<GenericDayAssigment>();
List<Share> shares = currentSharesFor(day);
@ -188,10 +148,15 @@ public class GenericResourceAllocation extends ResourceAllocation {
return shares;
}
@Override
protected void resetAssignmentsTo(List<GenericDayAssigment> assignments) {
setAssigments(assignments);
}
}
public IAllocatable forResources(Collection<? extends Resource> resources) {
return new Allocation(new ArrayList<Resource>(resources));
return new GenericAllocation(new ArrayList<Resource>(resources));
}
private void setAssigments(List<GenericDayAssigment> assignmentsCreated) {

View file

@ -1,10 +1,15 @@
package org.navalplanner.business.planner.entities;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.Validate;
import org.hibernate.validator.NotNull;
import org.joda.time.Days;
import org.joda.time.LocalDate;
import org.navalplanner.business.calendars.entities.BaseCalendar;
import org.navalplanner.business.calendars.entities.SameWorkHoursEveryDay;
import org.navalplanner.business.common.BaseEntity;
/**
@ -54,6 +59,58 @@ public abstract class ResourceAllocation extends BaseEntity {
return task;
}
protected abstract class AssignmentsAllocation<T extends DayAssigment>
implements IAllocatable {
@Override
public final void allocate(ResourcesPerDay resourcesPerDay) {
Task task = getTask();
LocalDate startInclusive = new LocalDate(task.getStartDate());
List<T> assigmentsCreated = new ArrayList<T>();
for (int i = 0; i < getDaysElapsedAt(task); i++) {
LocalDate day = startInclusive.plusDays(i);
int totalForDay = calculateTotalToDistribute(day,
resourcesPerDay);
assigmentsCreated.addAll(distributeForDay(day, totalForDay));
}
setResourcesPerDay(resourcesPerDay);
resetAssignmentsTo(assigmentsCreated);
}
protected abstract void resetAssignmentsTo(List<T> assignments);
private int calculateTotalToDistribute(LocalDate day,
ResourcesPerDay resourcesPerDay) {
Integer workableHours = getWorkableHoursAt(day);
return resourcesPerDay
.asHoursGivenResourceWorkingDayOf(workableHours);
}
private Integer getWorkableHoursAt(LocalDate day) {
if (getTaskCalendar() == null) {
return SameWorkHoursEveryDay.getDefaultWorkingDay()
.getWorkableHours(day);
} else {
return getTaskCalendar().getWorkableHours(day);
}
}
protected final BaseCalendar getTaskCalendar() {
return getTask().getCalendar();
}
private int getDaysElapsedAt(Task task) {
LocalDate endExclusive = new LocalDate(task.getEndDate());
Days daysBetween = Days.daysBetween(new LocalDate(task
.getStartDate()), endExclusive);
return daysBetween.getDays();
}
protected abstract List<T> distributeForDay(LocalDate day,
int totalHours);
}
public AssigmentFunction getAssigmentFunction() {
return assigmentFunction;
}