From 1b44fdcce63a611edfb9f9ca059dddf03a6fd9f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93scar=20Gonz=C3=A1lez=20Fern=C3=A1ndez?= Date: Tue, 22 Sep 2009 22:03:43 +0200 Subject: [PATCH] ItEr27S06CUAsignacionGrupoRecursosAPlanificacionItEr26S07: Connecting form binder and ResourceAllocationsBeingEdited --- .../ResourceAllocationController.java | 8 +- .../ResourceAllocationFormBinder.java | 24 +++--- .../allocation/ResourceAllocationModel.java | 13 +-- .../ResourceAllocationsBeingEdited.java | 79 +++++++++++++++---- 4 files changed, 84 insertions(+), 40 deletions(-) diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/allocation/ResourceAllocationController.java b/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/allocation/ResourceAllocationController.java index 8c32e606d..e5ff82234 100644 --- a/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/allocation/ResourceAllocationController.java +++ b/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/allocation/ResourceAllocationController.java @@ -88,8 +88,7 @@ public class ResourceAllocationController extends GenericForwardComposer { allocationsBeingEdited = resourceAllocationModel.initAllocationsFor( task, ganttTask, planningState); - formBinder = new ResourceAllocationFormBinder( - getCurrentCalculatedValue(task), resourceAllocationModel); + formBinder = allocationsBeingEdited.createFormBinder(); formBinder.setAssignedHoursComponent(assignedHoursComponent); Util.reloadBindings(window); try { @@ -101,11 +100,6 @@ public class ResourceAllocationController extends GenericForwardComposer { } } - private CalculatedValue getCurrentCalculatedValue(Task task) { - // TODO retrieve the calculated value from task - return CalculatedValue.NUMBER_OF_HOURS; - } - /** * Shows WorkerSearch window, add picked workers as * {@link SpecificResourceAllocation} to {@link ResourceAllocation} list diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/allocation/ResourceAllocationFormBinder.java b/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/allocation/ResourceAllocationFormBinder.java index 07a38cd66..548eb0d1e 100644 --- a/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/allocation/ResourceAllocationFormBinder.java +++ b/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/allocation/ResourceAllocationFormBinder.java @@ -1,29 +1,27 @@ package org.navalplanner.web.planner.allocation; -import org.apache.commons.lang.Validate; +import org.navalplanner.business.planner.entities.AggregateOfResourceAllocations; import org.navalplanner.business.planner.entities.CalculatedValue; import org.zkoss.zul.Intbox; public class ResourceAllocationFormBinder { - private CalculatedValue calculatedValue; - private final IResourceAllocationModel resourceAllocationModel; private Intbox assignedHoursComponent; + private final ResourceAllocationsBeingEdited resourceAllocationsBeingEdited; + private AggregateOfResourceAllocations aggregate; - public ResourceAllocationFormBinder(CalculatedValue calculatedValue, - IResourceAllocationModel resourceAllocationModel) { - Validate.notNull(resourceAllocationModel); - Validate.notNull(resourceAllocationModel); - this.calculatedValue = calculatedValue; - this.resourceAllocationModel = resourceAllocationModel; + public ResourceAllocationFormBinder( + ResourceAllocationsBeingEdited resourceAllocationsBeingEdited) { + this.resourceAllocationsBeingEdited = resourceAllocationsBeingEdited; + this.aggregate = this.resourceAllocationsBeingEdited + .getInitialAggregate(); } public void setAssignedHoursComponent(Intbox assignedHoursComponent) { this.assignedHoursComponent = assignedHoursComponent; - this.assignedHoursComponent - .setDisabled(calculatedValue == CalculatedValue.NUMBER_OF_HOURS); - this.assignedHoursComponent.setValue(resourceAllocationModel.getTask() - .getAssignedHours()); + this.assignedHoursComponent.setDisabled(resourceAllocationsBeingEdited + .getCalculatedValue() == CalculatedValue.NUMBER_OF_HOURS); + this.assignedHoursComponent.setValue(aggregate.getTotalHours()); } } 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 cafe59190..8a92d5cd8 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 @@ -95,8 +95,10 @@ public class ResourceAllocationModel implements IResourceAllocationModel { } private void mergeDTOsToTask() { - List resourceAllocations = resourceAllocationsBeingEdited - .asResourceAllocationsFor(task); + ResourceAllocationsBeingEdited taskModifying = resourceAllocationsBeingEdited + .taskModifying(); + List resourceAllocations = taskModifying + .asResourceAllocations(); if (task.isFixedDuration()) { ResourceAllocation.allocating(resourceAllocations).withResources( getResourcesMatchingCriterions()).allocateOnTaskLength(); @@ -127,8 +129,9 @@ public class ResourceAllocationModel implements IResourceAllocationModel { reattachCriterions(this.task.getHoursGroup().getCriterions()); List currentAllocations = addDefaultGenericIfNeeded(AllocationDTO.toDTOs(this.task .getResourceAllocations())); - resourceAllocationsBeingEdited = new ResourceAllocationsBeingEdited( - currentAllocations, resourceDAO); + resourceAllocationsBeingEdited = ResourceAllocationsBeingEdited + .noTaskModifying( + task, currentAllocations, resourceDAO); return resourceAllocationsBeingEdited; } @@ -136,7 +139,7 @@ public class ResourceAllocationModel implements IResourceAllocationModel { Set> resourceAllocations) { resourceAllocations.size(); for (ResourceAllocation resourceAllocation : resourceAllocations) { - resourceAllocation.getResourcesPerDay(); + resourceAllocation.getAssignments(); if (resourceAllocation instanceof SpecificResourceAllocation) { reattachSpecificResourceAllocation((SpecificResourceAllocation) resourceAllocation); } diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/allocation/ResourceAllocationsBeingEdited.java b/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/allocation/ResourceAllocationsBeingEdited.java index 38f29a30e..bb6d0f155 100644 --- a/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/allocation/ResourceAllocationsBeingEdited.java +++ b/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/allocation/ResourceAllocationsBeingEdited.java @@ -7,6 +7,8 @@ import java.util.HashSet; import java.util.List; import java.util.Set; +import org.navalplanner.business.planner.entities.AggregateOfResourceAllocations; +import org.navalplanner.business.planner.entities.CalculatedValue; import org.navalplanner.business.planner.entities.GenericResourceAllocation; import org.navalplanner.business.planner.entities.ResourceAllocation; import org.navalplanner.business.planner.entities.SpecificResourceAllocation; @@ -18,15 +20,30 @@ import org.navalplanner.business.resources.entities.Worker; public class ResourceAllocationsBeingEdited { + public static ResourceAllocationsBeingEdited noTaskModifying(Task task, + List initialAllocations, IResourceDAO resourceDAO) { + return new ResourceAllocationsBeingEdited(task, initialAllocations, + resourceDAO, false); + } + private final List currentAllocations; private final Set> requestedToRemove = new HashSet>(); private IResourceDAO resourceDAO; - public ResourceAllocationsBeingEdited( - List initialAllocations, IResourceDAO resourceDAO) { + private final Task task; + + private final boolean modifyTask; + + private ResourceAllocationFormBinder formBinder = null; + + private ResourceAllocationsBeingEdited(Task task, + List initialAllocations, IResourceDAO resourceDAO, + boolean modifyTask) { + this.task = task; this.resourceDAO = resourceDAO; + this.modifyTask = modifyTask; this.currentAllocations = new ArrayList( initialAllocations); } @@ -67,24 +84,32 @@ public class ResourceAllocationsBeingEdited { return requestedToRemove; } - public List asResourceAllocationsFor( - Task task) { + public List asResourceAllocations() { List result = new ArrayList(); for (AllocationDTO allocation : currentAllocations) { - result - .add(createOrModify(allocation, task) - .withDesiredResourcesPerDay( + result.add(createOrModify(allocation).withDesiredResourcesPerDay( allocation.getResourcesPerDay())); } return result; } - private ResourceAllocation createOrModify(AllocationDTO allocation, - Task task) { + private List> stripResourcesPerDay( + List withResourcesPerDay) { + List> result = new ArrayList>(); + for (ResourceAllocationWithDesiredResourcesPerDay r : withResourcesPerDay) { + result.add(r.getResourceAllocation()); + } + return result; + } + + private ResourceAllocation createOrModify(AllocationDTO allocation) { + if (!modifyTask) { + return createAllocation(allocation); + } if (allocation.isModifying()) { return reloadResourceIfNeeded(allocation.getOrigin()); } else { - ResourceAllocation result = createAllocation(allocation, task); + ResourceAllocation result = createAllocation(allocation); task.addResourceAllocation(result); return result; } @@ -103,22 +128,46 @@ public class ResourceAllocationsBeingEdited { return resourceDAO.findExistingEntity(resource.getId()); } - private ResourceAllocation createAllocation(AllocationDTO allocation, - Task task) { + private ResourceAllocation createAllocation(AllocationDTO allocation) { if (allocation instanceof SpecificAllocationDTO) { SpecificAllocationDTO specific = (SpecificAllocationDTO) allocation; - return createSpecific(specific.getResource(), task); + return createSpecific(specific.getResource()); } else { return GenericResourceAllocation.create(task); } } - private ResourceAllocation createSpecific(Resource resource, Task task) { - resource = getFromDB(resource); + private ResourceAllocation createSpecific(Resource resource) { SpecificResourceAllocation result = SpecificResourceAllocation .create(task); result.setResource(resource); return result; } + public ResourceAllocationsBeingEdited taskModifying() { + return new ResourceAllocationsBeingEdited(task, currentAllocations, + resourceDAO, true); + } + + public ResourceAllocationFormBinder createFormBinder() { + if (formBinder != null) + throw new IllegalStateException( + "there is already a binder associated with this object"); + formBinder = new ResourceAllocationFormBinder(this); + return formBinder; + } + + private CalculatedValue getCurrentCalculatedValue(Task task) { + // TODO retrieve the calculated value from task + return CalculatedValue.NUMBER_OF_HOURS; + } + + public CalculatedValue getCalculatedValue() { + return getCurrentCalculatedValue(task); + } + + public AggregateOfResourceAllocations getInitialAggregate() { + return new AggregateOfResourceAllocations(task.getResourceAllocations()); + } + }