diff --git a/navalplanner-business/src/main/java/org/navalplanner/business/planner/entities/GenericResourceAllocation.java b/navalplanner-business/src/main/java/org/navalplanner/business/planner/entities/GenericResourceAllocation.java index e397b811a..775bd235e 100644 --- a/navalplanner-business/src/main/java/org/navalplanner/business/planner/entities/GenericResourceAllocation.java +++ b/navalplanner-business/src/main/java/org/navalplanner/business/planner/entities/GenericResourceAllocation.java @@ -86,6 +86,13 @@ public class GenericResourceAllocation extends task)); } + public static GenericResourceAllocation create(Task task, + Collection criterions) { + GenericResourceAllocation result = new GenericResourceAllocation(task); + result.criterions = new HashSet(criterions); + return create(result); + } + private GenericResourceAllocation(Task task) { super(task); this.criterions = task.getCriterions(); diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/allocation/AllocationDTO.java b/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/allocation/AllocationDTO.java index 211868138..8b038a45d 100644 --- a/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/allocation/AllocationDTO.java +++ b/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/allocation/AllocationDTO.java @@ -28,7 +28,6 @@ import org.navalplanner.business.planner.entities.ResourceAllocation; import org.navalplanner.business.planner.entities.ResourcesPerDay; import org.navalplanner.business.planner.entities.Task; import org.navalplanner.business.planner.entities.allocationalgorithms.AllocationBeingModified; -import org.navalplanner.business.resources.entities.Resource; /** * The information that must be introduced to create a @@ -65,7 +64,7 @@ public abstract class AllocationDTO { private ResourcesPerDay resourcesPerDay; public abstract AllocationBeingModified toAllocationBeingModified( - Task task, List resourcesMatchingCriterions); + Task task); public boolean isCreating() { return origin == null; 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 857afd57d..161f77ade 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 @@ -224,14 +224,6 @@ class FormBinder { applyButton.setDisabled(false); } - public void markGenericAllocationMustBeNoZeroOrMoreAllocations( - AllocationDTO allocation) { - Decimalbox decimalbox = resourcesPerDayInputsByAllocationDTO - .get(allocation); - throw new WrongValueException(decimalbox, - _("it must be no zero or must add more allocations")); - } - public void markAssignedHoursMustBePositive() { throw new WrongValueException(assignedHoursComponent, _("it must be greater than zero")); @@ -242,14 +234,6 @@ class FormBinder { _("at least one no empty allocation is needed")); } - public void markNoWorkersMatchedByCriterion(GenericAllocationDTO generic) { - Decimalbox decimalbox = resourcesPerDayInputsByAllocationDTO - .get(generic); - throw new WrongValueException( - decimalbox, - _("there are no workers for required criteria. So the generic allocation can't be done")); - } - public void setAllocationsList(Listbox allocationsList) { this.allocationsList = allocationsList; } 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 9cffc7377..14aafaaef 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 @@ -25,13 +25,17 @@ import static org.navalplanner.web.I18nHelper._; import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.Set; +import org.apache.commons.lang.Validate; import org.navalplanner.business.planner.entities.GenericResourceAllocation; import org.navalplanner.business.planner.entities.ResourceAllocation; import org.navalplanner.business.planner.entities.ResourcesPerDay; import org.navalplanner.business.planner.entities.Task; import org.navalplanner.business.planner.entities.allocationalgorithms.AllocationBeingModified; +import org.navalplanner.business.resources.entities.Criterion; import org.navalplanner.business.resources.entities.Resource; +import org.navalplanner.web.resourceload.ResourceLoadModel; /** * The information required for creating a {@link GenericResourceAllocation} @@ -39,21 +43,38 @@ import org.navalplanner.business.resources.entities.Resource; */ public class GenericAllocationDTO extends AllocationDTO { - public static GenericAllocationDTO createDefault() { + private static GenericAllocationDTO createDefault() { GenericAllocationDTO result = new GenericAllocationDTO(); result.setName(_("Generic")); result.setResourcesPerDay(ResourcesPerDay.amount(0)); return result; } + public static GenericAllocationDTO create(Set criterions, + Collection resources) { + Validate.isTrue(!resources.isEmpty()); + Validate.notNull(criterions); + GenericAllocationDTO result = createDefault(); + result.criterions = criterions; + result.resources = new ArrayList(resources); + result.setName(ResourceLoadModel.getName(criterions)); + return result; + } + public static GenericAllocationDTO from( GenericResourceAllocation resourceAllocation) { GenericAllocationDTO result = createDefault(); result.setResourcesPerDay(resourceAllocation.getResourcesPerDay()); result.setOrigin(resourceAllocation); + result.criterions = resourceAllocation.getCriterions(); + result.resources = resourceAllocation.getAssociatedResources(); + result.setName(ResourceLoadModel.getName(result.criterions)); return result; } + private Set criterions; + private List resources; + @Override public boolean isGeneric() { return true; @@ -71,9 +92,10 @@ public class GenericAllocationDTO extends AllocationDTO { } @Override - public AllocationBeingModified toAllocationBeingModified(Task task, - List resources) { - return AllocationBeingModified.create(GenericResourceAllocation - .create(task), getResourcesPerDay(), resources); + public AllocationBeingModified toAllocationBeingModified(Task task) { + GenericResourceAllocation genericResourceAllocation = GenericResourceAllocation + .create(task, criterions); + return AllocationBeingModified.create(genericResourceAllocation, + getResourcesPerDay(), this.resources); } } 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 c5c2c8ced..e5a19cb4d 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 @@ -20,6 +20,7 @@ package org.navalplanner.web.planner.allocation; +import java.util.ArrayList; import java.util.Collection; import java.util.Date; import java.util.HashSet; @@ -88,12 +89,21 @@ public class ResourceAllocationModel implements IResourceAllocationModel { @Transactional(readOnly = true) public void addSpecific(Collection resources) { planningState.reassociateResourcesWithSession(resourceDAO); + for (Resource each : reloadResources(resources)) { + resourceAllocationsBeingEdited + .addSpecificResourceAllocationFor(each); + } + } + + private List reloadResources( + Collection resources) { + List result = new ArrayList(); for (Resource each : resources) { Resource reloaded = resourceDAO.findExistingEntity(each.getId()); reattachResource(reloaded); - resourceAllocationsBeingEdited - .addSpecificResourceAllocationFor(reloaded); + result.add(reloaded); } + return result; } @Override @@ -143,10 +153,6 @@ public class ResourceAllocationModel implements IResourceAllocationModel { previousLength); } - private List getResourcesMatchingCriterions() { - return resourceDAO.getAllByCriterions(getCriterions()); - } - @Override @Transactional(readOnly = true) public ResourceAllocationsBeingEdited initAllocationsFor(Task task, @@ -161,11 +167,10 @@ public class ResourceAllocationModel implements IResourceAllocationModel { loadCriterionsOfGenericAllocations(); reattachHoursGroup(this.task.getHoursGroup()); reattachCriterions(this.task.getHoursGroup().getValidCriterions()); - List currentAllocations = addDefaultGenericIfNeeded(AllocationDTO.toDTOs(this.task - .getResourceAllocations())); + List currentAllocations = AllocationDTO.toDTOs(this.task + .getResourceAllocations()); resourceAllocationsBeingEdited = ResourceAllocationsBeingEdited - .create(task, currentAllocations, resourceDAO, - reattachResources(getResourcesMatchingCriterions())); + .create(task, currentAllocations, resourceDAO); return resourceAllocationsBeingEdited; } @@ -212,14 +217,6 @@ public class ResourceAllocationModel implements IResourceAllocationModel { criterionType.getName(); } - private List reattachResources( - List resourcesMatchingCriterions) { - for (Resource resource : resourcesMatchingCriterions) { - reattachResource(resource); - } - return resourcesMatchingCriterions; - } - private void reattachResource(Resource resource) { resourceDAO.reattach(resource); reattachCriterionSatisfactions(resource.getCriterionSatisfactions()); @@ -239,16 +236,6 @@ public class ResourceAllocationModel implements IResourceAllocationModel { } } - private List addDefaultGenericIfNeeded( - List dtos) { - List currentGeneric = AllocationDTO - .getGeneric(dtos); - if (currentGeneric.isEmpty()) { - dtos.add(0, GenericAllocationDTO.createDefault()); - } - return dtos; - } - @Override public List getHoursAggregatedByCriterions() { return task.getAggregatedByCriterions(); 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 3fc906193..d3b286725 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 @@ -66,10 +66,8 @@ public class ResourceAllocationsBeingEdited { } public static ResourceAllocationsBeingEdited create(Task task, - List initialAllocations, IResourceDAO resourceDAO, - List resourcesBeingEdited) { - return new ResourceAllocationsBeingEdited(task, initialAllocations, - resourcesBeingEdited); + List initialAllocations, IResourceDAO resourceDAO) { + return new ResourceAllocationsBeingEdited(task, initialAllocations); } private final List currentAllocations; @@ -84,12 +82,9 @@ public class ResourceAllocationsBeingEdited { private Integer daysDuration; - private final List resourcesMatchingCriterions; - private ResourceAllocationsBeingEdited(Task task, - List initialAllocations, List resourcesMatchingCriterions) { + List initialAllocations) { this.task = task; - this.resourcesMatchingCriterions = resourcesMatchingCriterions; this.currentAllocations = new ArrayList( initialAllocations); this.calculatedValue = task.getCalculatedValue(); @@ -154,16 +149,8 @@ public class ResourceAllocationsBeingEdited { } public void checkInvalidValues() { - if (thereIsNoEmptyGenericResourceAllocation() - && noWorkersForCriterion()) { - formBinder.markNoWorkersMatchedByCriterion(getGenericAllocation()); - } - if (thereIsJustOneEmptyGenericResourceAllocation()) { - formBinder - .markGenericAllocationMustBeNoZeroOrMoreAllocations(getGenericAllocation()); - } if (formBinder.getCalculatedValue() != CalculatedValue.NUMBER_OF_HOURS - && formBinder.getAssignedHours() == 0) { + && formBinder.getAssignedHours() <= 0) { formBinder.markAssignedHoursMustBePositive(); } if (!thereIsLeastOneNoEmptyAllocation()) { @@ -171,19 +158,6 @@ public class ResourceAllocationsBeingEdited { } } - private boolean noWorkersForCriterion() { - return resourcesMatchingCriterions.isEmpty(); - } - - private boolean thereIsNoEmptyGenericResourceAllocation() { - return !getGenericAllocation().isEmptyResourcesPerDay(); - } - - private GenericAllocationDTO getGenericAllocation() { - return (GenericAllocationDTO) currentAllocations - .get(0); - } - private boolean thereIsLeastOneNoEmptyAllocation() { for (AllocationDTO allocationDTO : currentAllocations) { if (!allocationDTO.isEmptyResourcesPerDay()) { @@ -193,12 +167,6 @@ public class ResourceAllocationsBeingEdited { return false; } - private boolean thereIsJustOneEmptyGenericResourceAllocation() { - return currentAllocations.size() == 1 - && getGenericAllocation().isGeneric() - && getGenericAllocation().isEmptyResourcesPerDay(); - } - public AllocationResult doAllocation() { checkInvalidValues(); Map> fromDetachedToAttached = getAllocationsWithRelationshipsToOriginal(); @@ -247,7 +215,7 @@ public class ResourceAllocationsBeingEdited { private AllocationBeingModified instantiate( AllocationDTO key) { - return key.toAllocationBeingModified(task, resourcesMatchingCriterions); + return key.toAllocationBeingModified(task); } private Integer from(Date startDate, LocalDate end) { diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/allocation/SpecificAllocationDTO.java b/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/allocation/SpecificAllocationDTO.java index 592b00cd1..01415c762 100644 --- a/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/allocation/SpecificAllocationDTO.java +++ b/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/allocation/SpecificAllocationDTO.java @@ -99,8 +99,7 @@ public class SpecificAllocationDTO extends AllocationDTO { private Resource resource; @Override - public AllocationBeingModified toAllocationBeingModified(Task task, - List resources) { + public AllocationBeingModified toAllocationBeingModified(Task task) { SpecificResourceAllocation specific = SpecificResourceAllocation .create(task); specific.setResource(resource);