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 ef348fb71..01a8ad873 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 @@ -1,7 +1,5 @@ package org.navalplanner.web.planner.allocation; -import static org.navalplanner.web.I18nHelper._; - import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -57,10 +55,10 @@ public class ResourceAllocationModel implements IResourceAllocationModel { private org.zkoss.ganttz.data.Task ganttTask; - private List currentAllocations; - private PlanningState planningState; + private ResourceAllocationsBeingEdited resourceAllocationsBeingEdited; + @Override public Task getTask() { return task; @@ -69,25 +67,7 @@ public class ResourceAllocationModel implements IResourceAllocationModel { @Override @Transactional(readOnly = true) public void addSpecificResourceAllocation(Worker worker) throws Exception { - - if (alreadyExistsAllocationFor(worker)) { - throw new IllegalArgumentException(_( - "{0} already assigned to resource allocation list", worker - .getName())); - } - SpecificAllocationDTO allocation = SpecificAllocationDTO - .forResource(worker); - currentAllocations.add(allocation); - } - - private boolean alreadyExistsAllocationFor(Worker worker) { - return !getAllocationsFor(worker).isEmpty(); - } - - private List getAllocationsFor(Worker worker) { - List found = SpecificAllocationDTO.withResource( - SpecificAllocationDTO.getSpecific(currentAllocations), worker); - return found; + resourceAllocationsBeingEdited.addSpecificResorceAllocationFor(worker); } @Override @@ -98,30 +78,32 @@ public class ResourceAllocationModel implements IResourceAllocationModel { @Override public List getAllocations() { - if (currentAllocations == null) { + if (resourceAllocationsBeingEdited == null) { return Collections.emptyList(); } - return currentAllocations; + return resourceAllocationsBeingEdited.getCurrentAllocations(); } @Override public void removeSpecificResourceAllocation( SpecificAllocationDTO allocation) { - currentAllocations.remove(allocation); - if (allocation.isModifying()) { - task.removeResourceAllocation(allocation.getOrigin()); - } + resourceAllocationsBeingEdited.remove(allocation); } @Override public void cancel() { - currentAllocations = null; + resourceAllocationsBeingEdited = null; } @Override @Transactional(readOnly = true) public void save() { planningState.reassociateResourcesWithSession(resourceDAO); + Set> allocationsRequestedToRemove = resourceAllocationsBeingEdited + .getAllocationsRequestedToRemove(); + for (ResourceAllocation resourceAllocation : allocationsRequestedToRemove) { + task.removeResourceAllocation(resourceAllocation); + } mergeDTOsToTask(); } @@ -140,7 +122,8 @@ public class ResourceAllocationModel implements IResourceAllocationModel { private List toResourceAllocations() { List result = new ArrayList(); - for (AllocationDTO allocation : currentAllocations) { + for (AllocationDTO allocation : resourceAllocationsBeingEdited + .getCurrentAllocations()) { result.add(createOrModify(allocation).withDesiredResourcesPerDay( allocation.getResourcesPerDay())); } @@ -204,8 +187,10 @@ public class ResourceAllocationModel implements IResourceAllocationModel { hoursGroupDAO.save(this.task.getHoursGroup()); reattachHoursGroup(this.task.getHoursGroup()); reattachCriterions(this.task.getHoursGroup().getCriterions()); - currentAllocations = addDefaultGenericIfNeeded(asDTOs(this.task + List currentAllocations = addDefaultGenericIfNeeded(asDTOs(this.task .getResourceAllocations())); + resourceAllocationsBeingEdited = new ResourceAllocationsBeingEdited( + currentAllocations); } 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 new file mode 100644 index 000000000..b36a696cc --- /dev/null +++ b/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/allocation/ResourceAllocationsBeingEdited.java @@ -0,0 +1,60 @@ +package org.navalplanner.web.planner.allocation; + +import static org.navalplanner.web.I18nHelper._; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import org.navalplanner.business.planner.entities.ResourceAllocation; +import org.navalplanner.business.resources.entities.Worker; + +public class ResourceAllocationsBeingEdited { + + private final List currentAllocations; + + private final Set> requestedToRemove = new HashSet>(); + + public ResourceAllocationsBeingEdited(List initialAllocations) { + this.currentAllocations = new ArrayList( + initialAllocations); + } + + public void addSpecificResorceAllocationFor(Worker worker) { + if (alreadyExistsAllocationFor(worker)) { + throw new IllegalArgumentException(_( + "{0} already assigned to resource allocation list", worker + .getName())); + } + SpecificAllocationDTO allocation = SpecificAllocationDTO + .forResource(worker); + currentAllocations.add(allocation); + } + + public List getCurrentAllocations() { + return new ArrayList(currentAllocations); + } + + private boolean alreadyExistsAllocationFor(Worker worker) { + return !getAllocationsFor(worker).isEmpty(); + } + + private List getAllocationsFor(Worker worker) { + List found = SpecificAllocationDTO.withResource( + SpecificAllocationDTO.getSpecific(currentAllocations), worker); + return found; + } + + public void remove(SpecificAllocationDTO allocation) { + currentAllocations.remove(allocation); + if (allocation.isModifying()) { + requestedToRemove.add(allocation.getOrigin()); + } + } + + public Set> getAllocationsRequestedToRemove() { + return requestedToRemove; + } + +}