From b1e745be14d11b22cb457a3de80bb83df15d191c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93scar=20Gonz=C3=A1lez=20Fern=C3=A1ndez?= Date: Tue, 24 Nov 2009 13:07:49 +0100 Subject: [PATCH] ItEr36S11CUCreacionUnidadesPlanificacionItEr35S12: Adding support for adding generic resource allocations --- .../web/planner/allocation/FormBinder.java | 38 ++++++++++- .../allocation/GenericAllocationDTO.java | 4 ++ .../allocation/INewAllocationsAdder.java | 4 ++ .../ResourceAllocationController.java | 64 ++++--------------- .../allocation/ResourceAllocationModel.java | 25 ++++++-- .../ResourceAllocationsBeingEdited.java | 52 +++++++++++---- 6 files changed, 119 insertions(+), 68 deletions(-) diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/allocation/FormBinder.java b/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/allocation/FormBinder.java index 161f77ade..a03916147 100644 --- a/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/allocation/FormBinder.java +++ b/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/allocation/FormBinder.java @@ -23,12 +23,18 @@ package org.navalplanner.web.planner.allocation; import static org.navalplanner.web.I18nHelper._; import java.util.ArrayList; +import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Set; +import org.apache.commons.lang.StringUtils; import org.navalplanner.business.planner.entities.AggregateOfResourceAllocations; import org.navalplanner.business.planner.entities.CalculatedValue; +import org.navalplanner.business.resources.entities.Criterion; +import org.navalplanner.business.resources.entities.Resource; +import org.navalplanner.web.resourceload.ResourceLoadModel; import org.zkoss.zk.ui.Component; import org.zkoss.zk.ui.WrongValueException; import org.zkoss.zk.ui.event.Event; @@ -209,7 +215,7 @@ class FormBinder { return result; } - public void setDeleteButtonFor(SpecificAllocationDTO data, + public void setDeleteButtonFor(AllocationDTO data, Button deleteButton) { deleteButton.addEventListener(Events.ON_CLICK, new EventListener() { @@ -220,7 +226,7 @@ class FormBinder { }); } - public void newSpecificAllocation() { + public void newAllocationAdded() { applyButton.setDisabled(false); } @@ -229,6 +235,29 @@ class FormBinder { _("it must be greater than zero")); } + public void markRepeatedResource(List resources) { + throw new WrongValueException(allocationsList, _( + "{0} already assigned to resource allocation list", StringUtils + .join(getResourcesDescriptions(resources), ", "))); + } + + private List getResourcesDescriptions(List resources) { + List resourcesDescriptions = new ArrayList(); + for (Resource each : resources) { + resourcesDescriptions.add(each.getDescription()); + } + return resourcesDescriptions; + } + + public void markNoWorkersMatchedByCriterions( + Collection criterions) { + throw new WrongValueException( + allocationsList, + _( + "there are no workers for required criteria: {0}. So the generic allocation can't be added", + ResourceLoadModel.getName(criterions))); + } + public void markThereMustBeAtLeastOneNoEmptyAllocation() { throw new WrongValueException(allocationsList, _("at least one no empty allocation is needed")); @@ -247,4 +276,9 @@ class FormBinder { } } + public void markThereisAlreadyAssignmentWith(Set criterions) { + throw new WrongValueException(allocationsList, + _("for criterions {0} already exists an allocation")); + } + } diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/allocation/GenericAllocationDTO.java b/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/allocation/GenericAllocationDTO.java index 14aafaaef..f6e767d4c 100644 --- a/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/allocation/GenericAllocationDTO.java +++ b/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/allocation/GenericAllocationDTO.java @@ -98,4 +98,8 @@ public class GenericAllocationDTO extends AllocationDTO { return AllocationBeingModified.create(genericResourceAllocation, getResourcesPerDay(), this.resources); } + + public boolean hasSameCriterions(Set criterions) { + return this.criterions.equals(criterions); + } } diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/allocation/INewAllocationsAdder.java b/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/allocation/INewAllocationsAdder.java index 3ea8c4cdb..9840c5451 100644 --- a/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/allocation/INewAllocationsAdder.java +++ b/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/allocation/INewAllocationsAdder.java @@ -20,7 +20,9 @@ package org.navalplanner.web.planner.allocation; import java.util.Collection; +import java.util.Set; +import org.navalplanner.business.resources.entities.Criterion; import org.navalplanner.business.resources.entities.Resource; /** @@ -31,4 +33,6 @@ public interface INewAllocationsAdder { public void addSpecific(Collection resources); + public void addGeneric(Set criterions, + Collection resourcesMatched); } 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 ce0a56b33..c9a891c37 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 @@ -24,7 +24,6 @@ import static org.navalplanner.web.I18nHelper._; import java.math.BigDecimal; import java.util.Arrays; -import java.util.Collection; import java.util.Collections; import java.util.List; @@ -37,8 +36,6 @@ import org.navalplanner.business.planner.entities.ResourceAllocation; import org.navalplanner.business.planner.entities.ResourcesPerDay; import org.navalplanner.business.planner.entities.SpecificResourceAllocation; import org.navalplanner.business.planner.entities.Task; -import org.navalplanner.business.resources.entities.Resource; -import org.navalplanner.business.resources.entities.Worker; import org.navalplanner.web.common.IMessagesForUser; import org.navalplanner.web.common.MessagesForUser; import org.navalplanner.web.common.Util; @@ -163,24 +160,10 @@ public class ResourceAllocationController extends GenericForwardComposer { orderElementHoursGrid.setModel(new ListModelList( resourceAllocationModel.getHoursAggregatedByCriterions())); orderElementHoursGrid.setRowRenderer(createOrderElementHoursRenderer()); - newAllocationSelector - .setAllocationsAdder(modifyApplyButtonIfNeeded(resourceAllocationModel)); + newAllocationSelector.setAllocationsAdder(resourceAllocationModel); showWindow(); } - private INewAllocationsAdder modifyApplyButtonIfNeeded( - final INewAllocationsAdder decorated) { - return new INewAllocationsAdder() { - @Override - public void addSpecific(Collection resources) { - decorated.addSpecific(resources); - if (!resources.isEmpty()) { - formBinder.newSpecificAllocation(); - } - } - }; - } - public enum HoursRendererColumn { CRITERIONS { @@ -238,9 +221,12 @@ public class ResourceAllocationController extends GenericForwardComposer { * @param e */ public void onSelectWorkers(Event e) { - newAllocationSelector.addChoosen(); - tbResourceAllocation.setSelected(true); - Util.reloadBindings(allocationsList); + try { + newAllocationSelector.addChoosen(); + } finally { + tbResourceAllocation.setSelected(true); + Util.reloadBindings(allocationsList); + } } /** @@ -428,21 +414,15 @@ public class ResourceAllocationController extends GenericForwardComposer { @Override public void render(Listitem item, Object data) throws Exception { - if (data instanceof SpecificAllocationDTO) { - renderSpecificResourceAllocation(item, - (SpecificAllocationDTO) data); - } else if (data instanceof GenericAllocationDTO) { - renderGenericResourceAllocation(item, - (GenericAllocationDTO) data); - } + renderResourceAllocation(item, (AllocationDTO) data); } - private void renderSpecificResourceAllocation(Listitem item, - final SpecificAllocationDTO data) throws Exception { + private void renderResourceAllocation(Listitem item, + final AllocationDTO data) throws Exception { item.setValue(data); // Label fields are fixed, can only be viewed - appendLabel(item, getName(data.getResource())); + appendLabel(item, data.getName()); bindResourcesPerDay(appendDecimalbox(item), data); // On click delete button @@ -452,34 +432,16 @@ public class ResourceAllocationController extends GenericForwardComposer { @Override public void onEvent(Event event) throws Exception { - removeSpecificResourceAllocation(data); + removeAllocation(data); } }); } - private String getName(Resource resource) { - if (resource instanceof Worker) { - Worker worker = (Worker) resource; - return worker.getName(); - } - return resource.getDescription(); - } - - private void removeSpecificResourceAllocation(SpecificAllocationDTO data) { + private void removeAllocation(AllocationDTO data) { allocationsBeingEdited.remove(data); Util.reloadBindings(allocationsList); } - private void renderGenericResourceAllocation(Listitem item, - final GenericAllocationDTO data) throws Exception { - item.setValue(data); - // Set name - appendLabel(item, _("Generic")); - bindResourcesPerDay(appendDecimalbox(item), data); - // No buttons - appendLabel(item, ""); - } - /** * Appends {@link Label} to {@link Listitem} * @param listitem 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 e5a19cb4d..cbb652c75 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 @@ -89,10 +89,8 @@ public class ResourceAllocationModel implements IResourceAllocationModel { @Transactional(readOnly = true) public void addSpecific(Collection resources) { planningState.reassociateResourcesWithSession(resourceDAO); - for (Resource each : reloadResources(resources)) { - resourceAllocationsBeingEdited - .addSpecificResourceAllocationFor(each); - } + resourceAllocationsBeingEdited + .addSpecificResourceAllocationFor(reloadResources(resources)); } private List reloadResources( @@ -106,6 +104,18 @@ public class ResourceAllocationModel implements IResourceAllocationModel { return result; } + @Override + @Transactional(readOnly = true) + public void addGeneric(Set criterions, + Collection resourcesMatched) { + if (resourcesMatched.isEmpty() || criterions.isEmpty()) { + return; + } + planningState.reassociateResourcesWithSession(resourceDAO); + List reloadResources = reloadResources(resourcesMatched); + resourceAllocationsBeingEdited.addGeneric(criterions, reloadResources); + } + @Override public Set getCriterions() { return (task != null) ? task.getHoursGroup().getValidCriterions() @@ -167,6 +177,7 @@ public class ResourceAllocationModel implements IResourceAllocationModel { loadCriterionsOfGenericAllocations(); reattachHoursGroup(this.task.getHoursGroup()); reattachCriterions(this.task.getHoursGroup().getValidCriterions()); + loadResources(this.task.getResourceAllocations()); List currentAllocations = AllocationDTO.toDTOs(this.task .getResourceAllocations()); resourceAllocationsBeingEdited = ResourceAllocationsBeingEdited @@ -196,6 +207,12 @@ public class ResourceAllocationModel implements IResourceAllocationModel { } } + private void loadResources(Set> resourceAllocations) { + for (ResourceAllocation each : resourceAllocations) { + each.getAssociatedResources(); + } + } + private void reattachTaskSource() { TaskSource taskSource = task.getTaskSource(); taskSourceDAO.reattach(taskSource); 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 d3b286725..555f5bde9 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 @@ -20,8 +20,6 @@ package org.navalplanner.web.planner.allocation; -import static org.navalplanner.web.I18nHelper._; - import java.util.ArrayList; import java.util.Collection; import java.util.Date; @@ -40,6 +38,7 @@ import org.navalplanner.business.planner.entities.ResourceAllocation; import org.navalplanner.business.planner.entities.Task; import org.navalplanner.business.planner.entities.allocationalgorithms.AllocationBeingModified; import org.navalplanner.business.resources.daos.IResourceDAO; +import org.navalplanner.business.resources.entities.Criterion; import org.navalplanner.business.resources.entities.Resource; public class ResourceAllocationsBeingEdited { @@ -91,15 +90,35 @@ public class ResourceAllocationsBeingEdited { this.daysDuration = task.getDaysDuration(); } - public void addSpecificResourceAllocationFor(Resource resource) { - if (alreadyExistsAllocationFor(resource)) { - throw new IllegalArgumentException(_( - "{0} already assigned to resource allocation list", - resource.getDescription())); + public void addSpecificResourceAllocationFor(List resource) { + List alreadyPresent = new ArrayList(); + for (Resource each : resource) { + if (alreadyExistsAllocationFor(each)) { + alreadyPresent.add(each); + } else { + currentAllocations.add(SpecificAllocationDTO.forResource(each)); + formBinder.newAllocationAdded(); + } + } + if (!alreadyPresent.isEmpty()) { + formBinder.markRepeatedResource(alreadyPresent); + } + } + + public void addGeneric(Set criterions, + Collection resourcesMatched) { + if (resourcesMatched.isEmpty()) { + formBinder.markNoWorkersMatchedByCriterions(criterions); + } else { + GenericAllocationDTO genericAllocationDTO = GenericAllocationDTO + .create(criterions, resourcesMatched); + if (alreadyExistsAllocationFor(criterions)) { + formBinder.markThereisAlreadyAssignmentWith(criterions); + } else { + currentAllocations.add(genericAllocationDTO); + formBinder.newAllocationAdded(); + } } - SpecificAllocationDTO allocation = SpecificAllocationDTO - .forResource(resource); - currentAllocations.add(allocation); } public List getCurrentAllocations() { @@ -110,6 +129,17 @@ public class ResourceAllocationsBeingEdited { return !getAllocationsFor(resource).isEmpty(); } + private boolean alreadyExistsAllocationFor(Set criterions) { + List generic = AllocationDTO + .getGeneric(getCurrentAllocations()); + for (GenericAllocationDTO each : generic) { + if (each.hasSameCriterions(criterions)) { + return true; + } + } + return false; + } + private List getAllocationsFor(Resource resource) { List found = SpecificAllocationDTO .withResource(SpecificAllocationDTO @@ -117,7 +147,7 @@ public class ResourceAllocationsBeingEdited { return found; } - public void remove(SpecificAllocationDTO allocation) { + public void remove(AllocationDTO allocation) { currentAllocations.remove(allocation); if (allocation.isModifying()) { requestedToRemove.add(allocation.getOrigin());