ItEr36S11CUCreacionUnidadesPlanificacionItEr35S12: Adding support for adding generic resource allocations

This commit is contained in:
Óscar González Fernández 2009-11-24 13:07:49 +01:00
parent 5e749a2fbb
commit b1e745be14
6 changed files with 119 additions and 68 deletions

View file

@ -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<Resource> resources) {
throw new WrongValueException(allocationsList, _(
"{0} already assigned to resource allocation list", StringUtils
.join(getResourcesDescriptions(resources), ", ")));
}
private List<String> getResourcesDescriptions(List<Resource> resources) {
List<String> resourcesDescriptions = new ArrayList<String>();
for (Resource each : resources) {
resourcesDescriptions.add(each.getDescription());
}
return resourcesDescriptions;
}
public void markNoWorkersMatchedByCriterions(
Collection<? extends Criterion> 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<Criterion> criterions) {
throw new WrongValueException(allocationsList,
_("for criterions {0} already exists an allocation"));
}
}

View file

@ -98,4 +98,8 @@ public class GenericAllocationDTO extends AllocationDTO {
return AllocationBeingModified.create(genericResourceAllocation,
getResourcesPerDay(), this.resources);
}
public boolean hasSameCriterions(Set<Criterion> criterions) {
return this.criterions.equals(criterions);
}
}

View file

@ -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<? extends Resource> resources);
public void addGeneric(Set<Criterion> criterions,
Collection<? extends Resource> resourcesMatched);
}

View file

@ -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<? extends Resource> 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

View file

@ -89,10 +89,8 @@ public class ResourceAllocationModel implements IResourceAllocationModel {
@Transactional(readOnly = true)
public void addSpecific(Collection<? extends Resource> resources) {
planningState.reassociateResourcesWithSession(resourceDAO);
for (Resource each : reloadResources(resources)) {
resourceAllocationsBeingEdited
.addSpecificResourceAllocationFor(each);
}
resourceAllocationsBeingEdited
.addSpecificResourceAllocationFor(reloadResources(resources));
}
private List<Resource> reloadResources(
@ -106,6 +104,18 @@ public class ResourceAllocationModel implements IResourceAllocationModel {
return result;
}
@Override
@Transactional(readOnly = true)
public void addGeneric(Set<Criterion> criterions,
Collection<? extends Resource> resourcesMatched) {
if (resourcesMatched.isEmpty() || criterions.isEmpty()) {
return;
}
planningState.reassociateResourcesWithSession(resourceDAO);
List<Resource> reloadResources = reloadResources(resourcesMatched);
resourceAllocationsBeingEdited.addGeneric(criterions, reloadResources);
}
@Override
public Set<Criterion> 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<AllocationDTO> currentAllocations = AllocationDTO.toDTOs(this.task
.getResourceAllocations());
resourceAllocationsBeingEdited = ResourceAllocationsBeingEdited
@ -196,6 +207,12 @@ public class ResourceAllocationModel implements IResourceAllocationModel {
}
}
private void loadResources(Set<ResourceAllocation<?>> resourceAllocations) {
for (ResourceAllocation<?> each : resourceAllocations) {
each.getAssociatedResources();
}
}
private void reattachTaskSource() {
TaskSource taskSource = task.getTaskSource();
taskSourceDAO.reattach(taskSource);

View file

@ -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> resource) {
List<Resource> alreadyPresent = new ArrayList<Resource>();
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<Criterion> criterions,
Collection<? extends Resource> 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<AllocationDTO> getCurrentAllocations() {
@ -110,6 +129,17 @@ public class ResourceAllocationsBeingEdited {
return !getAllocationsFor(resource).isEmpty();
}
private boolean alreadyExistsAllocationFor(Set<Criterion> criterions) {
List<GenericAllocationDTO> generic = AllocationDTO
.getGeneric(getCurrentAllocations());
for (GenericAllocationDTO each : generic) {
if (each.hasSameCriterions(criterions)) {
return true;
}
}
return false;
}
private List<SpecificAllocationDTO> getAllocationsFor(Resource resource) {
List<SpecificAllocationDTO> 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());