diff --git a/navalplanner-business/src/main/java/org/navalplanner/business/orders/entities/HoursGroup.java b/navalplanner-business/src/main/java/org/navalplanner/business/orders/entities/HoursGroup.java index d1d3f08db..0b8908db8 100644 --- a/navalplanner-business/src/main/java/org/navalplanner/business/orders/entities/HoursGroup.java +++ b/navalplanner-business/src/main/java/org/navalplanner/business/orders/entities/HoursGroup.java @@ -54,11 +54,18 @@ public class HoursGroup implements Cloneable { return workingHours; } - public void setPercentage(BigDecimal percentage) + /** + * @param proportion + * It's one based, instead of one hundred based + * @throws IllegalArgumentException + * if the new sum of percentages in the parent {@link OrderLine} + * surpasses one + */ + public void setPercentage(BigDecimal proportion) throws IllegalArgumentException { BigDecimal oldPercentage = this.percentage; - this.percentage = percentage; + this.percentage = proportion; if (!parentOrderLine.isPercentageValid()) { this.percentage = oldPercentage; @@ -153,7 +160,7 @@ public class HoursGroup implements Cloneable { } public boolean isTransient() { - // FIXME Review reattachment + // FIXME Review reattachment return id == null; } diff --git a/navalplanner-business/src/main/java/org/navalplanner/business/orders/entities/OrderLine.java b/navalplanner-business/src/main/java/org/navalplanner/business/orders/entities/OrderLine.java index 7c752d119..41761cefd 100644 --- a/navalplanner-business/src/main/java/org/navalplanner/business/orders/entities/OrderLine.java +++ b/navalplanner-business/src/main/java/org/navalplanner/business/orders/entities/OrderLine.java @@ -8,6 +8,16 @@ import java.util.Set; public class OrderLine extends OrderElement { + public static OrderLine createOrderLineWithUnfixedHours(int hours) { + OrderLine result = new OrderLine(); + HoursGroup hoursGroup = new HoursGroup(); + result.addHoursGroup(hoursGroup); + hoursGroup.setFixedPercentage(true); + hoursGroup.setPercentage(new BigDecimal(1)); + hoursGroup.setWorkingHours(hours); + return result; + } + private Set hoursGroups = new HashSet(); @Override @@ -55,7 +65,6 @@ public class OrderLine extends OrderElement { /** * Set the total working hours of the {@link OrderLine} taking into account * the {@link HoursGroup} policies. - * * @param workHours * The desired value to set as total working hours * @throws IllegalArgumentException @@ -89,20 +98,13 @@ public class OrderLine extends OrderElement { /** * Makes the needed modifications in hoursGroups attribute in order to set - * the desired value of working hours. - * - * This method takes into account the different {@link HoursGroup} policies: - * - * If policy is FIXED_PERCENTAGE the new value is calculated for each - * {@link HoursGroup} with this policy. Using round down in order to avoid - * problems. - * - * Hours are proportionally distributed when there're {@link HoursGroup} - * with NO_FIXED policy. - * + * the desired value of working hours. This method takes into account the + * different {@link HoursGroup} policies: If policy is FIXED_PERCENTAGE the + * new value is calculated for each {@link HoursGroup} with this policy. + * Using round down in order to avoid problems. Hours are proportionally + * distributed when there're {@link HoursGroup} with NO_FIXED policy. * Finally, it creates new {@link HoursGroup} if the're some remaining hours * (it could happen because of the round down used for operations). - * * @param workHours * The value to set as total working hours */ @@ -175,7 +177,6 @@ public class OrderLine extends OrderElement { /** * Checks if the desired total number of hours is valid taking into account * {@link HoursGroup} policy restrictions. - * * @param total * The desired value * @return true if the value is valid @@ -202,9 +203,7 @@ public class OrderLine extends OrderElement { /** * Checks if the percentage is or not valid. That means, if the pertentage * of all {@link HoursGroup} with FIXED_PERCENTAGE isn't more than 100%. - * * This method is called from setPercentage at {@link HoursGroup} class. - * * @return true if the percentage is valid */ public boolean isPercentageValid() { @@ -227,7 +226,6 @@ public class OrderLine extends OrderElement { /** * Calculates the total number of working hours in a set of * {@link HoursGroup}. - * * @param hoursGroups * A {@link HoursGroup} set * @return The sum of working hours @@ -244,7 +242,6 @@ public class OrderLine extends OrderElement { * Calculates the total number of working hours in a set of * {@link HoursGroup} taking into account just {@link HoursGroup} with * NO_FIXED as policy. - * * @param hoursGroups * A {@link HoursGroup} set * @return The sum of NO_FIXED {@link HoursGroup} @@ -263,7 +260,6 @@ public class OrderLine extends OrderElement { * Re-calculates the working hours and percentages in the {@link HoursGroup} * set of the current {@link OrderLine}, taking into account the policy of * each {@link HoursGroup}. - * */ public void recalculateHoursGroups() { Integer total = calculateTotalHours(hoursGroups); diff --git a/navalplanner-business/src/test/java/org/navalplanner/business/test/orders/entities/OrderLineTest.java b/navalplanner-business/src/test/java/org/navalplanner/business/test/orders/entities/OrderLineTest.java index 95416cabd..1e32d0b2b 100644 --- a/navalplanner-business/src/test/java/org/navalplanner/business/test/orders/entities/OrderLineTest.java +++ b/navalplanner-business/src/test/java/org/navalplanner/business/test/orders/entities/OrderLineTest.java @@ -10,20 +10,17 @@ import java.math.BigDecimal; import org.junit.Test; import org.navalplanner.business.orders.entities.HoursGroup; import org.navalplanner.business.orders.entities.OrderLine; + /** * Tests for {@link OrderLine}.
- * * @author Manuel Rego Casasnovas */ public class OrderLineTest { /** - * An empty {@link OrderLine} without any {@link HoursGroup}. - * - * Trying to set work hours of {@link OrderLine} to 100h. - * - * Expected: {@link OrderLine} with 100h, with one {@link HoursGroup} with - * 100h NO_FIXED. + * An empty {@link OrderLine} without any {@link HoursGroup}. Trying to set + * work hours of {@link OrderLine} to 100h. Expected: {@link OrderLine} with + * 100h, with one {@link HoursGroup} with 100h NO_FIXED. */ @Test public void testSetWorkHoursHoursEmptyOrderLine() { @@ -48,11 +45,8 @@ public class OrderLineTest { } /** - * An empty {@link OrderLine} without any {@link HoursGroup}. - * - * Trying to set work hours of {@link OrderLine} to -100h. - * - * Expected: Exception. + * An empty {@link OrderLine} without any {@link HoursGroup}. Trying to set + * work hours of {@link OrderLine} to -100h. Expected: Exception. */ @Test public void testSetWorkHoursHoursEmptyOrderLineIllegal() { @@ -75,10 +69,8 @@ public class OrderLineTest { /** * An {@link OrderLine} with just one {@link HoursGroup} of 100h NO_FIXED. - * - * Trying to set work hours of {@link OrderLine} to 120h. - * - * Expected: {@link OrderLine} with 120h. {@link HoursGroup} with 120h. + * Trying to set work hours of {@link OrderLine} to 120h. Expected: + * {@link OrderLine} with 120h. {@link HoursGroup} with 120h. */ @Test public void testSetWorkHoursHoursGroupNoFixedIncreaseValue() { @@ -103,10 +95,8 @@ public class OrderLineTest { /** * An {@link OrderLine} with just one {@link HoursGroup} of 100h NO_FIXED. - * - * Trying to set work hours of {@link OrderLine} to 75h. - * - * Expected: {@link OrderLine} with 75h. {@link HoursGroup} with 75h. + * Trying to set work hours of {@link OrderLine} to 75h. Expected: + * {@link OrderLine} with 75h. {@link HoursGroup} with 75h. */ @Test public void testSetWorkHoursHoursGroupNoFixedDecreaseValue() { @@ -132,10 +122,7 @@ public class OrderLineTest { /** * An {@link OrderLine} with just one {@link HoursGroup} of 100h 100% - * FIXED_PERCENTAGE. - * - * Trying to set work hours of {@link OrderLine} to 120h. - * + * FIXED_PERCENTAGE. Trying to set work hours of {@link OrderLine} to 120h. * Expected: {@link OrderLine} with 120h. {@link HoursGroup} with 120h 100%. */ @Test @@ -167,10 +154,7 @@ public class OrderLineTest { /** * An {@link OrderLine} with just one {@link HoursGroup} of 100h 100% - * FIXED_PERCENTAGE. - * - * Trying to set work hours of {@link OrderLine} to 75h. - * + * FIXED_PERCENTAGE. Trying to set work hours of {@link OrderLine} to 75h. * Expected: {@link OrderLine} with 100h. {@link HoursGroup} with 75h 100%. */ @Test @@ -202,10 +186,7 @@ public class OrderLineTest { /** * An {@link OrderLine} with two {@link HoursGroup} of 100h and 50h - * NO_FIXED. - * - * Trying to set work hours of {@link OrderLine} to 200h. - * + * NO_FIXED. Trying to set work hours of {@link OrderLine} to 200h. * Expected: {@link OrderLine} with 200h. {@link HoursGroup} with 133h and * HoursGroup with 66h. */ @@ -237,11 +218,8 @@ public class OrderLineTest { /** * An {@link OrderLine} with two {@link HoursGroup} of 100h and 50h - * NO_FIXED. - * - * Trying to set work hours of {@link OrderLine} to 50h. - * - * Expected: {@link OrderLine} with 50h. {@link HoursGroup} with 33h and + * NO_FIXED. Trying to set work hours of {@link OrderLine} to 50h. Expected: + * {@link OrderLine} with 50h. {@link HoursGroup} with 33h and * {@link HoursGroup} with 16h. */ @Test @@ -272,12 +250,9 @@ public class OrderLineTest { /** * An {@link OrderLine} with two {@link HoursGroup} of 75h 75% - * FIXED_PERCENTAGE and 25h NO_FIXED. - * - * Trying to set work hours of {@link OrderLine} to 200h. - * - * Expected: {@link OrderLine} with 200h. {@link HoursGroup} with 150h 75% - * and {@link HoursGroup} with 50h. + * FIXED_PERCENTAGE and 25h NO_FIXED. Trying to set work hours of + * {@link OrderLine} to 200h. Expected: {@link OrderLine} with 200h. + * {@link HoursGroup} with 150h 75% and {@link HoursGroup} with 50h. */ @Test public void testSetWorkHoursHoursGroupFixedPercentageAndHoursGroupNoFixedIncreaseValue() { @@ -312,12 +287,9 @@ public class OrderLineTest { /** * An {@link OrderLine} with two {@link HoursGroup} of 75h 75% - * FIXED_PERCENTAGE and 25h NO_FIXED. - * - * Trying to set work hours of {@link OrderLine} to 50h. - * - * Expected: {@link OrderLine} with 50h. {@link HoursGroup} with 37h 75% and - * HoursGroup with 13h. + * FIXED_PERCENTAGE and 25h NO_FIXED. Trying to set work hours of + * {@link OrderLine} to 50h. Expected: {@link OrderLine} with 50h. + * {@link HoursGroup} with 37h 75% and HoursGroup with 13h. */ @Test public void testSetWorkHoursHoursGroupFixedPercentageAndHoursGroupNoFixedDecreaseValue() { @@ -352,10 +324,7 @@ public class OrderLineTest { /** * An {@link OrderLine} with two {@link HoursGroup} of 75h 75% and 25h 25% - * FIXED_PERCENTAGE. - * - * Trying to set work hours of {@link OrderLine} to 200h. - * + * FIXED_PERCENTAGE. Trying to set work hours of {@link OrderLine} to 200h. * Expected: {@link OrderLine} with 200h. {@link HoursGroup} with 150h 75% * and {@link HoursGroup} with 50h 25%. */ @@ -396,10 +365,7 @@ public class OrderLineTest { /** * An {@link OrderLine} with two {@link HoursGroup} of 75h 75% and 25h 25% - * FIXED_PERCENTAGE. - * - * Trying to set work hours of {@link OrderLine} to 80h. - * + * FIXED_PERCENTAGE. Trying to set work hours of {@link OrderLine} to 80h. * Expected: {@link OrderLine} with 80h. {@link HoursGroup} with 60h 75% and * {@link HoursGroup} with 20h 25%. */ @@ -440,12 +406,10 @@ public class OrderLineTest { /** * An {@link OrderLine} with three {@link HoursGroup} of 50h, 50h NO_FIXED - * and 100h 50% FIXED_PERCENTAGE. - * - * Trying to set work hours of {@link OrderLine} to 300h. - * - * Expected: {@link OrderLine} with 300h. {@link HoursGroup} with 75h, - * {@link HoursGroup} with 75h and {@link HoursGroup} with 150h 50%. + * and 100h 50% FIXED_PERCENTAGE. Trying to set work hours of + * {@link OrderLine} to 300h. Expected: {@link OrderLine} with 300h. + * {@link HoursGroup} with 75h, {@link HoursGroup} with 75h and + * {@link HoursGroup} with 150h 50%. */ @Test public void testSetWorkHoursHoursGroupNoFixedAndHoursGroupNoFixedAndHoursGroupFixedPercentageIncreaseValue() { @@ -484,12 +448,10 @@ public class OrderLineTest { /** * An {@link OrderLine} with three {@link HoursGroup} of 40h, 60h NO_FIXED - * and 100h 50% FIXED_PERCENTAGE. - * - * Trying to set work hours of {@link OrderLine} to 100h. - * - * Expected: {@link OrderLine} with 100h. {@link HoursGroup} with 20h, - * {@link HoursGroup} with 30h and {@link HoursGroup} with 50h 50%. + * and 100h 50% FIXED_PERCENTAGE. Trying to set work hours of + * {@link OrderLine} to 100h. Expected: {@link OrderLine} with 100h. + * {@link HoursGroup} with 20h, {@link HoursGroup} with 30h and + * {@link HoursGroup} with 50h 50%. */ @Test public void testSetWorkHoursHoursGroupNoFixedAndHoursGroupNoFixedAndHoursGroupFixedPercentageDecreaseValue() { @@ -528,12 +490,10 @@ public class OrderLineTest { /** * An {@link OrderLine} with three {@link HoursGroup} of 50h NO_FIXED, 50h - * 25% and 100h 50% FIXED_PERCENTAGE. - * - * Trying to set work hours of {@link OrderLine} to 400h. - * - * Expected: {@link OrderLine} with 400h. {@link HoursGroup} with 100h, - * {@link HoursGroup} with 100h 25% and {@link HoursGroup} with 200h 50%. + * 25% and 100h 50% FIXED_PERCENTAGE. Trying to set work hours of + * {@link OrderLine} to 400h. Expected: {@link OrderLine} with 400h. + * {@link HoursGroup} with 100h, {@link HoursGroup} with 100h 25% and + * {@link HoursGroup} with 200h 50%. */ @Test public void testSetWorkHoursHoursGroupNoFixedAndHoursGroupFixedPercentageAndHoursGroupFixedPercentageIncreaseValue() { @@ -576,12 +536,10 @@ public class OrderLineTest { /** * An {@link OrderLine} with three {@link HoursGroup} of 50h NO_FIXED, 50h - * 25% and 100h 50% FIXED_PERCENTAGE. - * - * Trying to set work hours of {@link OrderLine} to 100h. - * - * Expected: {@link OrderLine} with 400h. {@link HoursGroup} with 25h, - * {@link HoursGroup} with 25h 25% and {@link HoursGroup} with 50h 50%. + * 25% and 100h 50% FIXED_PERCENTAGE. Trying to set work hours of + * {@link OrderLine} to 100h. Expected: {@link OrderLine} with 400h. + * {@link HoursGroup} with 25h, {@link HoursGroup} with 25h 25% and + * {@link HoursGroup} with 50h 50%. */ @Test public void testSetWorkHoursHoursGroupNoFixedAndHoursGroupFixedPercentageAndHoursGroupFixedPercentageDecreaseValue() { @@ -751,4 +709,18 @@ public class OrderLineTest { assertThat(hoursGroup3.getWorkingHours(), equalTo(114)); } + @Test + public void createOrderLineWithAnHoursGroupTakingAll() { + int[] hoursValues = { 0, 100, 10, 30 }; + for (int hours : hoursValues) { + OrderLine orderLine = OrderLine + .createOrderLineWithUnfixedHours(hours); + assertThat(orderLine.getWorkHours(), equalTo(hours)); + assertThat(orderLine.getHoursGroups().size(), equalTo(1)); + orderLine.setWorkHours(20); + assertThat(orderLine.getWorkHours(), equalTo(20)); + assertThat(orderLine.getHoursGroups().size(), equalTo(1)); + } + } + } diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/web/orders/OrderElementTreeModel.java b/navalplanner-webapp/src/main/java/org/navalplanner/web/orders/OrderElementTreeModel.java index 1c0271929..c3eb6f97d 100644 --- a/navalplanner-webapp/src/main/java/org/navalplanner/web/orders/OrderElementTreeModel.java +++ b/navalplanner-webapp/src/main/java/org/navalplanner/web/orders/OrderElementTreeModel.java @@ -13,7 +13,6 @@ import org.zkoss.zul.SimpleTreeNode; /** * Model for a the {@link OrderElement} tree for a {@link Order}
- * * @author Lorenzo Tilve Álvaro */ public class OrderElementTreeModel extends SimpleTreeModel { @@ -28,11 +27,11 @@ public class OrderElementTreeModel extends SimpleTreeModel { private static SimpleTreeNode asNode(OrderElement orderElement) { orderElement.forceLoadHourGroups(); - return new SimpleTreeNode(orderElement, asNodes(orderElement.getChildren())); + return new SimpleTreeNode(orderElement, asNodes(orderElement + .getChildren())); } - private static SimpleTreeNode createRootNodeAndDescendants( - Order order) { + private static SimpleTreeNode createRootNodeAndDescendants(Order order) { return new SimpleTreeNode(order, asNodes(order.getOrderElements())); } @@ -53,7 +52,8 @@ public class OrderElementTreeModel extends SimpleTreeModel { } private OrderElement createNewOrderElement() { - OrderElement newOrderElement = new OrderLine(); + OrderElement newOrderElement = OrderLine + .createOrderLineWithUnfixedHours(0); newOrderElement.setName("New Order Element"); return newOrderElement; } @@ -82,8 +82,7 @@ public class OrderElementTreeModel extends SimpleTreeModel { SimpleTreeNode selectedForTurningIntoContainer) { IOrderLineGroup parentContainer = asOrderLineGroup(getParent(selectedForTurningIntoContainer)); if (selectedForTurningIntoContainer.getData() instanceof IOrderLineGroup) - return (IOrderLineGroup) selectedForTurningIntoContainer - .getData(); + return (IOrderLineGroup) selectedForTurningIntoContainer.getData(); OrderElement toBeTurned = asOrderLine(selectedForTurningIntoContainer); OrderLineGroup asContainer = toBeTurned.toContainer(); parentContainer.replace(toBeTurned, asContainer);