From bf0cee74f3474e118c2c8b725ef7c9af3e00a6bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93scar=20Gonz=C3=A1lez=20Fern=C3=A1ndez?= Date: Tue, 22 Sep 2009 14:35:49 +0200 Subject: [PATCH] ItEr27S06CUAsignacionGrupoRecursosAPlanificacionItEr26S07: Moving responsability of recreating the ResourceAllocation to ResourceAllocationsBeingEdited --- .../allocation/ResourceAllocationModel.java | 61 +++-------------- .../ResourceAllocationsBeingEdited.java | 66 ++++++++++++++++++- 2 files changed, 73 insertions(+), 54 deletions(-) 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 01a8ad873..df8a1e54e 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 @@ -99,16 +99,21 @@ public class ResourceAllocationModel implements IResourceAllocationModel { @Transactional(readOnly = true) public void save() { planningState.reassociateResourcesWithSession(resourceDAO); + removeDeletedAllocations(); + mergeDTOsToTask(); + } + + private void removeDeletedAllocations() { Set> allocationsRequestedToRemove = resourceAllocationsBeingEdited .getAllocationsRequestedToRemove(); for (ResourceAllocation resourceAllocation : allocationsRequestedToRemove) { task.removeResourceAllocation(resourceAllocation); } - mergeDTOsToTask(); } private void mergeDTOsToTask() { - List resourceAllocations = toResourceAllocations(); + List resourceAllocations = resourceAllocationsBeingEdited + .asResourceAllocationsFor(task); if (task.isFixedDuration()) { ResourceAllocation.allocating(resourceAllocations).withResources( getResourcesMatchingCriterions()).allocateOnTaskLength(); @@ -120,60 +125,10 @@ public class ResourceAllocationModel implements IResourceAllocationModel { } } - private List toResourceAllocations() { - List result = new ArrayList(); - for (AllocationDTO allocation : resourceAllocationsBeingEdited - .getCurrentAllocations()) { - result.add(createOrModify(allocation).withDesiredResourcesPerDay( - allocation.getResourcesPerDay())); - } - return result; - } - private List getResourcesMatchingCriterions() { return resourceDAO.getAllByCriterions(getCriterions()); } - private ResourceAllocation createOrModify(AllocationDTO allocation) { - if (allocation.isModifying()) { - return reloadResourceIfNeeded(allocation.getOrigin()); - } else { - ResourceAllocation result = createAllocation(allocation); - task.addResourceAllocation(result); - return result; - } - } - - private ResourceAllocation reloadResourceIfNeeded( - ResourceAllocation origin) { - if (origin instanceof SpecificResourceAllocation) { - SpecificResourceAllocation specific = (SpecificResourceAllocation) origin; - specific.setResource(getFromDB(specific.getResource())); - } - return origin; - } - - private ResourceAllocation createAllocation(AllocationDTO allocation) { - if (allocation instanceof SpecificAllocationDTO) { - SpecificAllocationDTO specific = (SpecificAllocationDTO) allocation; - return createSpecific(specific.getResource()); - } else { - return GenericResourceAllocation.create(task); - } - } - - private ResourceAllocation createSpecific(Resource resource) { - resource = getFromDB(resource); - SpecificResourceAllocation result = SpecificResourceAllocation - .create(task); - result.setResource(resource); - return result; - } - - private Resource getFromDB(Resource resource) { - return resourceDAO.findExistingEntity(resource.getId()); - } - @Override @Transactional(readOnly = true) public void initAllocationsFor(Task task, @@ -190,7 +145,7 @@ public class ResourceAllocationModel implements IResourceAllocationModel { List currentAllocations = addDefaultGenericIfNeeded(asDTOs(this.task .getResourceAllocations())); resourceAllocationsBeingEdited = new ResourceAllocationsBeingEdited( - currentAllocations); + currentAllocations, resourceDAO); } private void reattachResourceAllocations( 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 b36a696cc..38f29a30e 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,7 +7,13 @@ import java.util.HashSet; import java.util.List; import java.util.Set; +import org.navalplanner.business.planner.entities.GenericResourceAllocation; import org.navalplanner.business.planner.entities.ResourceAllocation; +import org.navalplanner.business.planner.entities.SpecificResourceAllocation; +import org.navalplanner.business.planner.entities.Task; +import org.navalplanner.business.planner.entities.allocationalgorithms.ResourceAllocationWithDesiredResourcesPerDay; +import org.navalplanner.business.resources.daos.IResourceDAO; +import org.navalplanner.business.resources.entities.Resource; import org.navalplanner.business.resources.entities.Worker; public class ResourceAllocationsBeingEdited { @@ -16,7 +22,11 @@ public class ResourceAllocationsBeingEdited { private final Set> requestedToRemove = new HashSet>(); - public ResourceAllocationsBeingEdited(List initialAllocations) { + private IResourceDAO resourceDAO; + + public ResourceAllocationsBeingEdited( + List initialAllocations, IResourceDAO resourceDAO) { + this.resourceDAO = resourceDAO; this.currentAllocations = new ArrayList( initialAllocations); } @@ -57,4 +67,58 @@ public class ResourceAllocationsBeingEdited { return requestedToRemove; } + public List asResourceAllocationsFor( + Task task) { + List result = new ArrayList(); + for (AllocationDTO allocation : currentAllocations) { + result + .add(createOrModify(allocation, task) + .withDesiredResourcesPerDay( + allocation.getResourcesPerDay())); + } + return result; + } + + private ResourceAllocation createOrModify(AllocationDTO allocation, + Task task) { + if (allocation.isModifying()) { + return reloadResourceIfNeeded(allocation.getOrigin()); + } else { + ResourceAllocation result = createAllocation(allocation, task); + task.addResourceAllocation(result); + return result; + } + } + + private ResourceAllocation reloadResourceIfNeeded( + ResourceAllocation origin) { + if (origin instanceof SpecificResourceAllocation) { + SpecificResourceAllocation specific = (SpecificResourceAllocation) origin; + specific.setResource(getFromDB(specific.getResource())); + } + return origin; + } + + private Resource getFromDB(Resource resource) { + return resourceDAO.findExistingEntity(resource.getId()); + } + + private ResourceAllocation createAllocation(AllocationDTO allocation, + Task task) { + if (allocation instanceof SpecificAllocationDTO) { + SpecificAllocationDTO specific = (SpecificAllocationDTO) allocation; + return createSpecific(specific.getResource(), task); + } else { + return GenericResourceAllocation.create(task); + } + } + + private ResourceAllocation createSpecific(Resource resource, Task task) { + resource = getFromDB(resource); + SpecificResourceAllocation result = SpecificResourceAllocation + .create(task); + result.setResource(resource); + return result; + } + }