ItEr26S07CUAsignacionGrupoRecursosAPlanificacionItEr25S07: The ResourceAllocation stores ResourcesPerUnit instead of percentage

This commit is contained in:
Óscar González Fernández 2009-09-14 17:50:28 +02:00
parent d2e8aeebec
commit ebfb2de993
12 changed files with 83 additions and 58 deletions

View file

@ -33,6 +33,16 @@ public class GenericResourceAllocation extends ResourceAllocation {
return (GenericResourceAllocation) create(new GenericResourceAllocation());
}
public static GenericResourceAllocation createForTesting(
ResourcesPerDay resourcesPerDay, Task task) {
return (GenericResourceAllocation) create(new GenericResourceAllocation(
resourcesPerDay, task));
}
private GenericResourceAllocation(ResourcesPerDay resourcesPerDay, Task task) {
super(resourcesPerDay, task);
}
public GenericResourceAllocation() {
}
@ -120,6 +130,7 @@ public class GenericResourceAllocation extends ResourceAllocation {
resourcesPerDay);
assigmentsCreated.addAll(distributeForDay(day, totalForDay));
}
setResourcesPerDay(resourcesPerDay);
setAssigments(assigmentsCreated);
}

View file

@ -1,9 +1,6 @@
/**
*
*/
package org.navalplanner.business.planner.entities;
import java.math.BigDecimal;
import java.util.List;
import org.apache.commons.lang.Validate;
@ -22,12 +19,8 @@ public abstract class ResourceAllocation extends BaseEntity {
private AssigmentFunction assigmentFunction;
/**
* Allocation percentage of the resource.
*
* It's one based, instead of one hundred based.
*/
private BigDecimal percentage = new BigDecimal(0).setScale(2);
@NotNull
private ResourcesPerDay resourcesPerDay;
/**
* Constructor for hibernate. Do not use!
@ -36,6 +29,11 @@ public abstract class ResourceAllocation extends BaseEntity {
}
protected void setResourcesPerDay(ResourcesPerDay resourcesPerDay) {
Validate.notNull(resourcesPerDay);
this.resourcesPerDay = resourcesPerDay;
}
public ResourceAllocation(Task task) {
this(task, null);
}
@ -46,22 +44,16 @@ public abstract class ResourceAllocation extends BaseEntity {
assigmentFunction = assignmentFunction;
}
protected ResourceAllocation(ResourcesPerDay resourcesPerDay, Task task) {
this(task);
Validate.notNull(resourcesPerDay);
this.resourcesPerDay = resourcesPerDay;
}
public Task getTask() {
return task;
}
public BigDecimal getPercentage() {
return percentage;
}
/**
* @param proportion
* It's one based, instead of one hundred based.
*/
public void setPercentage(BigDecimal proportion) {
this.percentage = proportion;
}
public AssigmentFunction getAssigmentFunction() {
return assigmentFunction;
}
@ -76,4 +68,9 @@ public abstract class ResourceAllocation extends BaseEntity {
protected abstract List<? extends DayAssigment> getAssignments();
public ResourcesPerDay getResourcesPerDay() {
return resourcesPerDay;
}
}

View file

@ -25,6 +25,12 @@ public class SpecificResourceAllocation extends ResourceAllocation {
private Set<SpecificDayAssigment> specificDaysAssigment = new HashSet<SpecificDayAssigment>();
public static SpecificResourceAllocation createForTesting(
ResourcesPerDay resourcesPerDay, Task task) {
return (SpecificResourceAllocation) create(new SpecificResourceAllocation(
resourcesPerDay, task));
}
/**
* Constructor for hibernate. Do not use!
*/
@ -32,6 +38,11 @@ public class SpecificResourceAllocation extends ResourceAllocation {
}
private SpecificResourceAllocation(ResourcesPerDay resourcesPerDay,
Task task) {
super(resourcesPerDay, task);
}
private SpecificResourceAllocation(Task task) {
super(task);
}

View file

@ -12,7 +12,7 @@
</id>
<version name="version" access="property" type="long" />
<property name="percentage" type="java.math.BigDecimal" scale="8"/>
<property name="resourcesPerDay" type="org.navalplanner.business.planner.entities.hibernate.ResourcesPerDayType" />
<many-to-one class="Task" name="task" column="TASK" />

View file

@ -21,6 +21,7 @@ import org.navalplanner.business.planner.daos.IResourceAllocationDAO;
import org.navalplanner.business.planner.daos.ITaskElementDAO;
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.SpecificResourceAllocation;
import org.navalplanner.business.planner.entities.Task;
import org.navalplanner.business.resources.daos.IResourceDAO;
@ -95,7 +96,7 @@ public class ResourceAllocationDAOTest {
if (ResourceAllocationType.SPECIFIC_RESOURCE_ALLOCATION.equals(type)) {
SpecificResourceAllocation specificResourceAllocation = SpecificResourceAllocation
.create(task);
.createForTesting(ResourcesPerDay.amount(1), task);
Worker worker = (Worker) createValidWorker();
resourceDAO.save(worker);
specificResourceAllocation.setWorker(worker);
@ -104,7 +105,7 @@ public class ResourceAllocationDAOTest {
}
if (ResourceAllocationType.GENERIC_RESOURCE_ALLOCATION.equals(type)) {
GenericResourceAllocation specificResourceAllocation = GenericResourceAllocation
.create(task);
.createForTesting(ResourcesPerDay.amount(1), task);
return specificResourceAllocation;
}

View file

@ -177,6 +177,19 @@ public class GenericResourceAllocationTest {
}
}
@Test
public void theResourcesPerDayAreChangedWhenTheAllocationIsDone() {
givenTaskWithStartAndEnd(toInterval(new LocalDate(2006, 10, 5), Period
.days(2)));
givenGenericResourceAllocationForTask(task);
givenWorkersWithoutLoadAndWithoutCalendar();
ResourcesPerDay assignedResourcesPerDay = ResourcesPerDay.amount(5);
genericResourceAllocation.forResources(workers).allocate(
assignedResourcesPerDay);
assertThat(genericResourceAllocation.getResourcesPerDay(),
equalTo(assignedResourcesPerDay));
}
@Test
public void allocatingSeveralResourcesPerDayHavingJustOneResourceProducesOvertime() {
LocalDate start = new LocalDate(2006, 10, 5);

View file

@ -1,11 +1,11 @@
package org.navalplanner.web.planner.allocation;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.navalplanner.business.planner.entities.ResourceAllocation;
import org.navalplanner.business.planner.entities.ResourcesPerDay;
/**
* The information that must be introduced to create a
@ -27,7 +27,7 @@ public abstract class AllocationDTO {
private String name;
private BigDecimal percentage;
private ResourcesPerDay resourcesPerDay;
public String getName() {
return name;
@ -37,12 +37,12 @@ public abstract class AllocationDTO {
this.name = name;
}
public BigDecimal getPercentage() {
return percentage;
public ResourcesPerDay getResourcesPerDay() {
return this.resourcesPerDay;
}
public void setPercentage(BigDecimal percentage) {
this.percentage = percentage;
public void setResourcesPerDay(ResourcesPerDay resourcesPerDay) {
this.resourcesPerDay = resourcesPerDay;
}
public abstract boolean isGeneric();

View file

@ -3,6 +3,7 @@ package org.navalplanner.web.planner.allocation;
import static org.navalplanner.web.I18nHelper._;
import org.navalplanner.business.planner.entities.GenericResourceAllocation;
import org.navalplanner.business.planner.entities.ResourcesPerDay;
/**
* The information required for creating a {@link GenericResourceAllocation}
@ -13,13 +14,14 @@ public class GenericAllocationDTO extends AllocationDTO {
public static GenericAllocationDTO createDefault() {
GenericAllocationDTO result = new GenericAllocationDTO();
result.setName(_("Generic"));
result.setResourcesPerDay(ResourcesPerDay.amount(0));
return result;
}
public static GenericAllocationDTO from(
GenericResourceAllocation resourceAllocation) {
GenericAllocationDTO result = createDefault();
result.setPercentage(resourceAllocation.getPercentage());
result.setResourcesPerDay(resourceAllocation.getResourcesPerDay());
return result;
}

View file

@ -9,6 +9,7 @@ import java.util.Set;
import org.apache.commons.lang.StringUtils;
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.Criterion;
@ -33,6 +34,7 @@ import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.ListitemRenderer;
import org.zkoss.zul.SimpleConstraint;
import org.zkoss.zul.api.Window;
/**
@ -215,9 +217,8 @@ public class ResourceAllocationController extends GenericForwardComposer {
// Label fields are fixed, can only be viewed
appendLabel(item, getName(data.getResource()));
// appendLabel(item, resourceAllocation.getWorker().getNif());
// Percentage field is editable
bindPercentage(appendDecimalbox(item), data);
bindResourcesPerDay(appendDecimalbox(item), data);
// On click delete button
appendButton(item, _("Delete")).addEventListener("onClick",
new EventListener() {
@ -245,18 +246,9 @@ public class ResourceAllocationController extends GenericForwardComposer {
private void renderGenericResourceAllocation(Listitem item,
final GenericAllocationDTO data) throws Exception {
item.setValue(data);
// Set name
appendLabel(item, _("Generic"));
// Set percentage
BigDecimal percentage = data.getPercentage();
if (!new BigDecimal(0).equals(data.getPercentage())) {
percentage = (percentage != null) ? percentage
: new BigDecimal(0);
percentage = percentage.scaleByPowerOfTen(2).setScale(2,
BigDecimal.ROUND_HALF_EVEN);
}
appendLabel(item, percentage.toString());
bindResourcesPerDay(appendDecimalbox(item), data);
// No buttons
appendLabel(item, "");
}
@ -307,26 +299,22 @@ public class ResourceAllocationController extends GenericForwardComposer {
return decimalbox;
}
private void bindPercentage(final Decimalbox decimalbox,
final SpecificAllocationDTO data) {
private void bindResourcesPerDay(final Decimalbox decimalbox,
final AllocationDTO data) {
decimalbox.setConstraint(new SimpleConstraint(
SimpleConstraint.NO_NEGATIVE));
Util.bind(decimalbox, new Util.Getter<BigDecimal>() {
@Override
public BigDecimal get() {
return (data.getPercentage() != null) ? data
.getPercentage().scaleByPowerOfTen(2)
: new BigDecimal(0);
return data.getResourcesPerDay().getAmount();
}
}, new Util.Setter<BigDecimal>() {
@Override
public void set(BigDecimal value) {
if (value != null) {
value = value.setScale(2).divide(new BigDecimal(100),
BigDecimal.ROUND_HALF_EVEN);
data.setPercentage(value);
}
data.setResourcesPerDay(ResourcesPerDay.amount(value));
}
});
}

View file

@ -153,7 +153,7 @@ public class ResourceAllocationModel implements IResourceAllocationModel {
Set<ResourceAllocation> resourceAllocations) {
resourceAllocations.size();
for (ResourceAllocation resourceAllocation : resourceAllocations) {
resourceAllocation.getPercentage();
resourceAllocation.getResourcesPerDay();
if (resourceAllocation instanceof SpecificResourceAllocation) {
reattachSpecificResourceAllocation((SpecificResourceAllocation) resourceAllocation);
}

View file

@ -4,6 +4,7 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.navalplanner.business.planner.entities.ResourcesPerDay;
import org.navalplanner.business.planner.entities.SpecificResourceAllocation;
import org.navalplanner.business.resources.entities.Resource;
import org.navalplanner.business.resources.entities.Worker;
@ -46,7 +47,7 @@ public class SpecificAllocationDTO extends AllocationDTO {
public static SpecificAllocationDTO from(SpecificResourceAllocation specific) {
SpecificAllocationDTO result = forResource(specific.getWorker());
result.setPercentage(specific.getPercentage());
result.setResourcesPerDay(specific.getResourcesPerDay());
return result;
}
@ -54,6 +55,7 @@ public class SpecificAllocationDTO extends AllocationDTO {
SpecificAllocationDTO result = new SpecificAllocationDTO();
result.setName(worker.getName());
result.setResource(worker);
result.setResourcesPerDay(ResourcesPerDay.amount(1));
return result;
}

View file

@ -95,7 +95,7 @@
style="margin-bottom: 5px">
<listhead>
<listheader label="${i18n:_('Name')}" />
<listheader label="${i18n:_('Percentage')}" />
<listheader label="${i18n:_('Resources Per Day')}" />
<listheader label="" />
</listhead>
</listbox>