Add related specific allocations second level

FEA: ItEr70S08CriteriaLoadRefinementItEr69S10
This commit is contained in:
Óscar González Fernández 2011-02-08 13:38:48 +01:00
parent efb28b8404
commit 48717f3624
2 changed files with 124 additions and 86 deletions

View file

@ -125,10 +125,10 @@ public abstract class ResourceAllocation<T extends DayAssignment> extends
return result;
}
public static Map<Task, List<ResourceAllocation<?>>> byTask(
List<ResourceAllocation<?>> allocations) {
Map<Task, List<ResourceAllocation<?>>> result = new HashMap<Task, List<ResourceAllocation<?>>>();
for (ResourceAllocation<?> resourceAllocation : allocations) {
public static <R extends ResourceAllocation<?>> Map<Task, List<R>> byTask(
List<? extends R> allocations) {
Map<Task, List<R>> result = new HashMap<Task, List<R>>();
for (R resourceAllocation : allocations) {
if (resourceAllocation.getTask() != null) {
Task task = resourceAllocation.getTask();
initializeIfNeeded(result, task);

View file

@ -57,7 +57,6 @@ import org.navalplanner.business.planner.entities.TaskElement;
import org.navalplanner.business.resources.daos.ICriterionDAO;
import org.navalplanner.business.resources.daos.IResourceDAO;
import org.navalplanner.business.resources.entities.Criterion;
import org.navalplanner.business.resources.entities.CriterionSatisfaction;
import org.navalplanner.business.resources.entities.Resource;
import org.navalplanner.business.scenarios.IScenarioManager;
import org.navalplanner.business.users.daos.IOrderAuthorizationDAO;
@ -222,34 +221,34 @@ public class ResourceLoadModel implements IResourceLoadModel {
if (filterByResources) {
result.addAll(groupsFor(resourcesToShow()));
} else {
calculatedGenericAllocationsByCriterion =
genericAllocationsByCriterion();
result.addAll(groupsFor(calculatedGenericAllocationsByCriterion));
allocationsByCriterion = allocationsByCriterion();
result.addAll(groupsFor(allocationsByCriterion));
}
return result;
}
Map<Criterion, List<GenericResourceAllocation>> calculatedGenericAllocationsByCriterion;
private Map<Criterion, List<ResourceAllocation<?>>> allocationsByCriterion;
private Map<Criterion, List<GenericResourceAllocation>> genericAllocationsByCriterion() {
private Map<Criterion, List<ResourceAllocation<?>>> allocationsByCriterion() {
if (!criteriaToShowList.isEmpty()) {
reattachCriteriaToShow();
// reattaching criterions so the query returns the same criteria as
// keys
allCriteriaList = new ArrayList<Criterion>(criteriaToShowList);
return findAllocationsGroupedByCriteria(criteriaToShowList);
return withAssociatedSpecific(findAllocationsGroupedByCriteria(criteriaToShowList));
}
Map<Criterion, List<GenericResourceAllocation>> result = allocationsByCriterion();
Map<Criterion, List<GenericResourceAllocation>> result = findAllocationsByCriterion();
allCriteriaList = Criterion.sortByTypeAndName(result.keySet());
if (pageFilterPosition == -1) {
return result;
return withAssociatedSpecific(result);
}
List<Criterion> criteriaReallyShown = allCriteriaList.subList(
pageFilterPosition, getEndPositionForCriterionPageFilter());
return onlyForThePagesShown(criteriaReallyShown, result);
return withAssociatedSpecific(onlyForThePagesShown(criteriaReallyShown,
result));
}
private Map<Criterion, List<GenericResourceAllocation>> allocationsByCriterion() {
private Map<Criterion, List<GenericResourceAllocation>> findAllocationsByCriterion() {
if (filter()) {
List<Task> tasks = justTasks(filterBy
.getAllChildrenAssociatedTaskElements());
@ -261,19 +260,36 @@ public class ResourceLoadModel implements IResourceLoadModel {
private Map<Criterion, List<GenericResourceAllocation>> findAllocationsGroupedByCriteria(
List<Criterion> relatedWith) {
return resourceAllocationDAO.findGenericAllocationsBySomeCriterion(
relatedWith, asDate(initDateFilter), asDate(endDateFilter));
return resourceAllocationDAO
.findGenericAllocationsBySomeCriterion(
relatedWith, asDate(initDateFilter), asDate(endDateFilter));
}
private Map<Criterion, List<GenericResourceAllocation>> findAllocationsGroupedByCriteria() {
return resourceAllocationDAO.findGenericAllocationsByCriterion(
return resourceAllocationDAO
.findGenericAllocationsByCriterion(
asDate(initDateFilter), asDate(endDateFilter));
}
private Map<Criterion, List<GenericResourceAllocation>> onlyForThePagesShown(
private Map<Criterion, List<ResourceAllocation<?>>> withAssociatedSpecific(
Map<Criterion, List<GenericResourceAllocation>> genericAllocationsByCriterion) {
Map<Criterion, List<ResourceAllocation<?>>> result = new HashMap<Criterion, List<ResourceAllocation<?>>>();
for (Entry<Criterion, List<GenericResourceAllocation>> each : genericAllocationsByCriterion
.entrySet()) {
List<ResourceAllocation<?>> both = new ArrayList<ResourceAllocation<?>>();
both.addAll(each.getValue());
both.addAll(resourceAllocationDAO.findSpecificAllocationsRelatedTo(
each.getKey(), asDate(initDateFilter),
asDate(endDateFilter)));
result.put(each.getKey(), both);
}
return result;
}
private <R extends ResourceAllocation<?>> Map<Criterion, List<R>> onlyForThePagesShown(
List<Criterion> criteriaReallyShown,
Map<Criterion, List<GenericResourceAllocation>> allocationsByCriteria) {
Map<Criterion, List<GenericResourceAllocation>> result = new HashMap<Criterion, List<GenericResourceAllocation>>();
Map<Criterion, List<R>> allocationsByCriteria) {
Map<Criterion, List<R>> result = new HashMap<Criterion, List<R>>();
for (Criterion each : criteriaReallyShown) {
if (allocationsByCriteria.get(each) != null) {
result.put(each, allocationsByCriteria.get(each));
@ -408,24 +424,24 @@ public class ResourceLoadModel implements IResourceLoadModel {
}
/**
* @param genericAllocationsByCriterion
* @param allocationsByCriterion
* @return
*/
private List<LoadTimeLine> groupsFor(
Map<Criterion, List<GenericResourceAllocation>> genericAllocationsByCriterion) {
Map<Criterion, List<ResourceAllocation<?>>> allocationsByCriterion) {
List<LoadTimeLine> result = new ArrayList<LoadTimeLine>();
for(Criterion criterion : criteriaToShow()) {
if (genericAllocationsByCriterion.get(criterion) == null) {
if (allocationsByCriterion.get(criterion) == null) {
// no allocations found for criterion
continue;
}
List<GenericResourceAllocation> allocations = ResourceAllocation
.sortedByStartDate(genericAllocationsByCriterion
List<ResourceAllocation<?>> allocations = ResourceAllocation
.sortedByStartDate(allocationsByCriterion
.get(criterion));
TimeLineRole<BaseEntity> role = getCurrentTimeLineRole(criterion);
LoadTimeLine group = new LoadTimeLine(createPrincipal(criterion,
allocations, role),
buildSecondLevel(criterion, allocations));
allocations, role), buildSecondaryLevels(criterion,
allocations));
if (!group.isEmpty()) {
result.add(group);
}
@ -433,8 +449,30 @@ public class ResourceLoadModel implements IResourceLoadModel {
return result;
}
private List<LoadTimeLine> buildSecondLevel(Criterion criterion,
List<GenericResourceAllocation> allocations) {
private List<LoadTimeLine> buildSecondaryLevels(Criterion criterion,
List<? extends ResourceAllocation<?>> allocations) {
List<LoadTimeLine> result = new ArrayList<LoadTimeLine>();
result.addAll(buildSubLevels(criterion, ResourceAllocation.getOfType(
GenericResourceAllocation.class, allocations)));
result.add(buildRelatedSpecificAllocations(criterion, allocations));
return result;
}
private LoadTimeLine buildRelatedSpecificAllocations(Criterion criterion,
List<? extends ResourceAllocation<?>> allocations) {
List<SpecificResourceAllocation> specific = ResourceAllocation
.getOfType(SpecificResourceAllocation.class, allocations);
LoadTimeLine main = new LoadTimeLine(_("Specific Allocations"),
createPeriods(criterion, specific), "related-specific",
getCurrentTimeLineRole(criterion));
List<LoadTimeLine> children = buildGroupsFor(ResourceAllocation
.byResource(new ArrayList<ResourceAllocation<?>>(specific)));
return new LoadTimeLine(main, children);
}
private List<LoadTimeLine> buildSubLevels(Criterion criterion,
List<? extends ResourceAllocation<?>> allocations) {
List<LoadTimeLine> result = new ArrayList<LoadTimeLine>();
Map<Order, List<ResourceAllocation<?>>> byOrder = byOrder(new ArrayList<ResourceAllocation<?>>(
allocations));
@ -443,8 +481,8 @@ public class ResourceLoadModel implements IResourceLoadModel {
if (filter()) {
// build time lines for current order
if (byOrder.get(filterBy) != null) {
result.addAll(buildTimeLinesForOrder(criterion, byOrder
.get(filterBy)));
result.addAll(buildTimeLinesForOrder(filterBy, criterion,
byOrder.get(filterBy)));
}
byOrder.remove(filterBy);
// build time lines for other orders
@ -459,10 +497,14 @@ public class ResourceLoadModel implements IResourceLoadModel {
return result;
}
private List<LoadTimeLine> buildTimeLinesForOrder(Criterion criterion,
private List<LoadTimeLine> buildTimeLinesForOrder(Order order,
Criterion criterion,
List<ResourceAllocation<?>> allocations) {
List<LoadTimeLine> result = new ArrayList<LoadTimeLine>();
result.addAll(buildTimeLinesForEachTask(criterion, allocations));
result.addAll(buildTimeLinesForEachTask(criterion,
onlyGeneric(allocations)));
result.addAll(buildTimeLinesForEachResource(criterion,
onlySpecific(allocations), getCurrentTimeLineRole(order)));
return result;
}
@ -490,21 +532,22 @@ public class ResourceLoadModel implements IResourceLoadModel {
continue;
}
TimeLineRole<BaseEntity> role = getCurrentTimeLineRole(order);
result.add(new LoadTimeLine(buildTimeLine(criterion, order
.getName(), "global-generic", byOrder.get(order), role),
buildTimeLinesForOrder(
criterion, byOrder.get(order))));
result.add(new LoadTimeLine(
buildTimeLine(criterion, order.getName(), "global-generic",
byOrder.get(order), role), buildTimeLinesForOrder(
order, criterion, byOrder.get(order))));
}
return result;
}
private List<LoadTimeLine> buildTimeLinesForEachTask(Criterion criterion,
List<ResourceAllocation<?>> allocations) {
Map<Task, List<ResourceAllocation<?>>> byTask = ResourceAllocation
List<GenericResourceAllocation> allocations) {
Map<Task, List<GenericResourceAllocation>> byTask = ResourceAllocation
.byTask(allocations);
List<LoadTimeLine> secondLevel = new ArrayList<LoadTimeLine>();
for (Entry<Task, List<ResourceAllocation<?>>> entry : byTask.entrySet()) {
for (Entry<Task, List<GenericResourceAllocation>> entry : byTask
.entrySet()) {
Task task = entry.getKey();
@ -537,24 +580,23 @@ public class ResourceLoadModel implements IResourceLoadModel {
}
private Map<Set<Criterion>, List<GenericResourceAllocation>> getAllocationsWithSameCriteria(
List<ResourceAllocation<?>> genericAllocations) {
return GenericResourceAllocation
.byCriterions(onlyGeneric(genericAllocations));
List<GenericResourceAllocation> genericAllocations) {
return GenericResourceAllocation.byCriterions(genericAllocations);
}
private List<LoadTimeLine> buildTimeLinesForEachResource(
Criterion criterion, List<GenericResourceAllocation> allocations,
Criterion criterion,
List<? extends ResourceAllocation<?>> allocations,
TimeLineRole<BaseEntity> role) {
Map<Resource, List<GenericResourceAllocation>> byResource = GenericResourceAllocation
Map<Resource, List<ResourceAllocation<?>>> byResource = ResourceAllocation
.byResource(allocations);
List<LoadTimeLine> secondLevel = new ArrayList<LoadTimeLine>();
for (Entry<Resource, List<GenericResourceAllocation>> entry : byResource
for (Entry<Resource, List<ResourceAllocation<?>>> entry : byResource
.entrySet()) {
Resource resource = entry.getKey();
List<GenericResourceAllocation> resourceAllocations = entry
.getValue();
String descriptionTimeLine = getDescriptionResourceWithCriterions(resource);
List<ResourceAllocation<?>> resourceAllocations = entry.getValue();
String descriptionTimeLine = resource.getShortDescription();
LoadTimeLine timeLine = buildTimeLine(resource,
descriptionTimeLine, resourceAllocations, "generic", role);
@ -566,34 +608,16 @@ public class ResourceLoadModel implements IResourceLoadModel {
return secondLevel;
}
private String getDescriptionResourceWithCriterions(Resource resource) {
Set<CriterionSatisfaction> criterionSatisfactions = resource
.getCriterionSatisfactions();
return resource.getShortDescription();
}
private String getCriterionSatisfactionDescription(
Set<CriterionSatisfaction> satisfactions) {
if (satisfactions.isEmpty()) {
return "";
}
List<Criterion> criterions = new ArrayList<Criterion>();
for (CriterionSatisfaction satisfaction : satisfactions) {
criterions.add(satisfaction.getCriterion());
}
return " :: " + Criterion.getCaptionFor(criterions);
}
private LoadTimeLine createPrincipal(Criterion criterion,
List<GenericResourceAllocation> orderedAllocations,
List<? extends ResourceAllocation<?>> orderedAllocations,
TimeLineRole<BaseEntity> role) {
return new LoadTimeLine(criterion.getType().getName() + ": " + criterion.getName(),
createPeriods(criterion, orderedAllocations), "global-generic", role);
}
private List<LoadPeriod> createPeriods(Criterion criterion,
List<GenericResourceAllocation> value) {
if(initDateFilter != null || endDateFilter != null) {
List<? extends ResourceAllocation<?>> value) {
if (initDateFilter != null || endDateFilter != null) {
return PeriodsBuilder.build(
LoadPeriodGenerator.onCriterion(criterion, resourcesDAO),
value, asDate(initDateFilter), asDate(endDateFilter));
@ -604,31 +628,45 @@ public class ResourceLoadModel implements IResourceLoadModel {
}
private List<LoadTimeLine> groupsFor(List<Resource> allResources) {
List<LoadTimeLine> result = new ArrayList<LoadTimeLine>();
return buildGroupsFor(eachWithAllocations(allResources));
}
private Map<Resource, List<ResourceAllocation<?>>> eachWithAllocations(
List<Resource> allResources) {
Map<Resource, List<ResourceAllocation<?>>> map = new HashMap<Resource, List<ResourceAllocation<?>>>();
for (Resource resource : allResources) {
LoadTimeLine group = buildGroup(resource);
if (!group.isEmpty()) {
result.add(group);
map.put(resource, ResourceAllocation
.sortedByStartDate(resourceAllocationDAO
.findAllocationsRelatedTo(resource, initDateFilter,
endDateFilter)));
}
return map;
}
private List<LoadTimeLine> buildGroupsFor(
Map<Resource, List<ResourceAllocation<?>>> map) {
List<LoadTimeLine> result = new ArrayList<LoadTimeLine>();
for (Entry<Resource, List<ResourceAllocation<?>>> each : map
.entrySet()) {
LoadTimeLine l = buildGroupFor(each.getKey(), each.getValue());
if (!l.isEmpty()) {
result.add(l);
}
}
return result;
}
private LoadTimeLine buildGroup(Resource resource) {
List<ResourceAllocation<?>> sortedByStartDate = ResourceAllocation
.sortedByStartDate(resourceAllocationDAO
.findAllocationsRelatedTo(resource, initDateFilter,
endDateFilter));
private LoadTimeLine buildGroupFor(Resource resource,
List<? extends ResourceAllocation<?>> sortedByStartDate) {
TimeLineRole<BaseEntity> role = getCurrentTimeLineRole(resource);
LoadTimeLine result = new LoadTimeLine(buildTimeLine(resource, resource
.getName(), sortedByStartDate, "resource", role),
buildSecondLevel(resource, sortedByStartDate));
return result;
}
private List<LoadTimeLine> buildSecondLevel(Resource resource,
List<ResourceAllocation<?>> sortedByStartDate) {
List<? extends ResourceAllocation<?>> sortedByStartDate) {
List<LoadTimeLine> result = new ArrayList<LoadTimeLine>();
Map<Order, List<ResourceAllocation<?>>> byOrder = byOrder(sortedByStartDate);
@ -694,7 +732,7 @@ public class ResourceLoadModel implements IResourceLoadModel {
@Transactional(readOnly = true)
public Map<Order, List<ResourceAllocation<?>>> byOrder(
Collection<ResourceAllocation<?>> allocations) {
Collection<? extends ResourceAllocation<?>> allocations) {
Map<Order, List<ResourceAllocation<?>>> result = new HashMap<Order, List<ResourceAllocation<?>>>();
for (ResourceAllocation<?> resourceAllocation : allocations) {
if ((resourceAllocation.isSatisfied())
@ -929,10 +967,10 @@ public class ResourceLoadModel implements IResourceLoadModel {
}
else {
List<DayAssignment> dayAssignments = new ArrayList<DayAssignment>();
for(Entry<Criterion, List<GenericResourceAllocation>> entry :
calculatedGenericAllocationsByCriterion.entrySet()) {
for (Entry<Criterion, List<ResourceAllocation<?>>> entry : allocationsByCriterion
.entrySet()) {
for(GenericResourceAllocation allocation : entry.getValue()) {
for (ResourceAllocation<?> allocation : entry.getValue()) {
dayAssignments.addAll(allocation.getAssignments());
}
}