ItEr27S06CUAsignacionGrupoRecursosAPlanificacionItEr26S07: Moving responsability of recreating the ResourceAllocation to ResourceAllocationsBeingEdited

This commit is contained in:
Óscar González Fernández 2009-09-22 14:35:49 +02:00
parent 157eb18337
commit bf0cee74f3
2 changed files with 73 additions and 54 deletions

View file

@ -99,16 +99,21 @@ public class ResourceAllocationModel implements IResourceAllocationModel {
@Transactional(readOnly = true)
public void save() {
planningState.reassociateResourcesWithSession(resourceDAO);
removeDeletedAllocations();
mergeDTOsToTask();
}
private void removeDeletedAllocations() {
Set<ResourceAllocation<?>> allocationsRequestedToRemove = resourceAllocationsBeingEdited
.getAllocationsRequestedToRemove();
for (ResourceAllocation<?> resourceAllocation : allocationsRequestedToRemove) {
task.removeResourceAllocation(resourceAllocation);
}
mergeDTOsToTask();
}
private void mergeDTOsToTask() {
List<ResourceAllocationWithDesiredResourcesPerDay> resourceAllocations = toResourceAllocations();
List<ResourceAllocationWithDesiredResourcesPerDay> resourceAllocations = resourceAllocationsBeingEdited
.asResourceAllocationsFor(task);
if (task.isFixedDuration()) {
ResourceAllocation.allocating(resourceAllocations).withResources(
getResourcesMatchingCriterions()).allocateOnTaskLength();
@ -120,60 +125,10 @@ public class ResourceAllocationModel implements IResourceAllocationModel {
}
}
private List<ResourceAllocationWithDesiredResourcesPerDay> toResourceAllocations() {
List<ResourceAllocationWithDesiredResourcesPerDay> result = new ArrayList<ResourceAllocationWithDesiredResourcesPerDay>();
for (AllocationDTO allocation : resourceAllocationsBeingEdited
.getCurrentAllocations()) {
result.add(createOrModify(allocation).withDesiredResourcesPerDay(
allocation.getResourcesPerDay()));
}
return result;
}
private List<Resource> 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<AllocationDTO> currentAllocations = addDefaultGenericIfNeeded(asDTOs(this.task
.getResourceAllocations()));
resourceAllocationsBeingEdited = new ResourceAllocationsBeingEdited(
currentAllocations);
currentAllocations, resourceDAO);
}
private void reattachResourceAllocations(

View file

@ -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<ResourceAllocation<?>> requestedToRemove = new HashSet<ResourceAllocation<?>>();
public ResourceAllocationsBeingEdited(List<AllocationDTO> initialAllocations) {
private IResourceDAO resourceDAO;
public ResourceAllocationsBeingEdited(
List<AllocationDTO> initialAllocations, IResourceDAO resourceDAO) {
this.resourceDAO = resourceDAO;
this.currentAllocations = new ArrayList<AllocationDTO>(
initialAllocations);
}
@ -57,4 +67,58 @@ public class ResourceAllocationsBeingEdited {
return requestedToRemove;
}
public List<ResourceAllocationWithDesiredResourcesPerDay> asResourceAllocationsFor(
Task task) {
List<ResourceAllocationWithDesiredResourcesPerDay> result = new ArrayList<ResourceAllocationWithDesiredResourcesPerDay>();
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;
}
}