From da9fe42415e19a830e4dbcaae4f8adee7f2a540e Mon Sep 17 00:00:00 2001 From: Diego Pino Garcia Date: Sat, 7 May 2011 22:29:48 +0200 Subject: [PATCH] [Bug #975] Set last stretch with 100% completition as read-only This stretch is never added to the list of stretches, that means, it won't be saved. On the contrary, is inferred from the endDate of the task. FEA: ItEr74S04BugFixing --- .../business/planner/entities/Stretch.java | 10 +++- .../planner/entities/StretchesFunction.java | 48 ++++++++++++------- .../main/resources/db.changelog-initial.xml | 7 +++ .../StrechesFunctionConfiguration.java | 5 +- .../streches/StretchesFunctionModel.java | 8 +--- 5 files changed, 47 insertions(+), 31 deletions(-) diff --git a/navalplanner-business/src/main/java/org/navalplanner/business/planner/entities/Stretch.java b/navalplanner-business/src/main/java/org/navalplanner/business/planner/entities/Stretch.java index 3dafe63e2..c8e236c42 100644 --- a/navalplanner-business/src/main/java/org/navalplanner/business/planner/entities/Stretch.java +++ b/navalplanner-business/src/main/java/org/navalplanner/business/planner/entities/Stretch.java @@ -80,7 +80,13 @@ public class Stretch { } public static Stretch copy(Stretch stretch) { - return create(stretch.date, stretch.lengthPercentage, stretch.amountWorkPercentage); + Stretch result = new Stretch(); + result.date = stretch.date; + result.lengthPercentage = stretch.lengthPercentage; + result.amountWorkPercentage = stretch.amountWorkPercentage; + result.consolidated = stretch.consolidated; + result.readOnly = stretch.readOnly; + return result; } public static Stretch buildFromConsolidatedProgress(ResourceAllocation resourceAllocation) { @@ -172,7 +178,7 @@ public class Stretch { } public String toString() { - return String.format("(%s, %s, %s) ", date, lengthPercentage, amountWorkPercentage); + return String.format("(%s, %s, %s, readOnly: %s) ", date, lengthPercentage, amountWorkPercentage, readOnly); } public boolean isReadOnly() { diff --git a/navalplanner-business/src/main/java/org/navalplanner/business/planner/entities/StretchesFunction.java b/navalplanner-business/src/main/java/org/navalplanner/business/planner/entities/StretchesFunction.java index ecf317430..05d1c3c0a 100644 --- a/navalplanner-business/src/main/java/org/navalplanner/business/planner/entities/StretchesFunction.java +++ b/navalplanner-business/src/main/java/org/navalplanner/business/planner/entities/StretchesFunction.java @@ -207,6 +207,9 @@ public class StretchesFunction extends AssignmentFunction { // Transient. Calculated from resourceAllocation private Stretch consolidatedStretch; + // Transient. Used to calculate read-only last stretch + private LocalDate taskEndDate; + public static StretchesFunction create() { return (StretchesFunction) create(new StretchesFunction()); } @@ -249,34 +252,35 @@ public class StretchesFunction extends AssignmentFunction { result.type = type; result.desiredType = desiredType; result.consolidatedStretch = consolidatedStretch; + result.taskEndDate = taskEndDate; return result; } public void resetToStrechesFrom(StretchesFunction from) { this.removeAllStretches(); - for (Stretch each : from.getStretches()) { + for (Stretch each : from.getStretchesDefinedByUser()) { this.addStretch(Stretch.copy(each)); } this.consolidatedStretch = from.consolidatedStretch; } - public void setStretches(List stretches) { - this.stretches = stretches; - } - - private void sortStretches() { - Collections.sort(stretches, new Comparator() { - @Override - public int compare(Stretch o1, Stretch o2) { - return o1.getDate().compareTo(o2.getDate()); - } - }); + public List getStretchesDefinedByUser() { + return Collections.unmodifiableList(Stretch.sortByDate(stretches)); } @Valid public List getStretches() { - sortStretches(); - return Collections.unmodifiableList(stretches); + List result = new ArrayList(stretches); + if (taskEndDate != null) { + result.add(getLastStretch()); + } + return Collections.unmodifiableList(Stretch.sortByDate(result)); + } + + private Stretch getLastStretch() { + Stretch result = Stretch.create(taskEndDate, BigDecimal.ONE, BigDecimal.ONE); + result.readOnly(true); + return result; } public StretchesFunctionTypeEnum getType() { @@ -338,7 +342,7 @@ public class StretchesFunction extends AssignmentFunction { public List getStretchesPlusConsolidated() { List result = new ArrayList(); - result.addAll(stretches); + result.addAll(getStretches()); if (consolidatedStretch != null) { result.add(consolidatedStretch); } @@ -348,11 +352,10 @@ public class StretchesFunction extends AssignmentFunction { @AssertTrue(message = "Last stretch should have one hundred percent for " + "length and amount of work percentage") public boolean checkOneHundredPercent() { + List stretches = getStretchesPlusConsolidated(); if (stretches.isEmpty()) { return false; } - sortStretches(); - Stretch lastStretch = stretches.get(stretches.size() - 1); if (lastStretch.getLengthPercentage().compareTo(BigDecimal.ONE) != 0) { return false; @@ -360,7 +363,6 @@ public class StretchesFunction extends AssignmentFunction { if (lastStretch.getAmountWorkPercentage().compareTo(BigDecimal.ONE) != 0) { return false; } - return true; } @@ -373,10 +375,15 @@ public class StretchesFunction extends AssignmentFunction { if (resourceAllocation.getFirstNonConsolidatedDate() == null) { return; } + taskEndDate = getTaskEndDate(resourceAllocation); getDesiredType().applyTo(resourceAllocation, this); type = getDesiredType(); } + private LocalDate getTaskEndDate(ResourceAllocation resourceAllocation) { + return resourceAllocation.getTask().getEndAsLocalDate(); + } + @Override public String getName() { if (StretchesFunctionTypeEnum.INTERPOLATED.equals(type)) { @@ -401,6 +408,7 @@ public class StretchesFunction extends AssignmentFunction { } private void checkStretchesSumOneHundredPercent() { + List stretches = getStretchesPlusConsolidated(); BigDecimal sumOfProportions = stretches.isEmpty() ? BigDecimal.ZERO : last(stretches).getAmountWorkPercentage(); BigDecimal left = calculateLeftFor(sumOfProportions); @@ -441,4 +449,8 @@ public class StretchesFunction extends AssignmentFunction { return consolidatedStretch; } + public void setTaskEndDate(LocalDate taskEndDate) { + this.taskEndDate = taskEndDate; + } + } diff --git a/navalplanner-business/src/main/resources/db.changelog-initial.xml b/navalplanner-business/src/main/resources/db.changelog-initial.xml index 41ae0e59c..040dc3bb4 100644 --- a/navalplanner-business/src/main/resources/db.changelog-initial.xml +++ b/navalplanner-business/src/main/resources/db.changelog-initial.xml @@ -58,4 +58,11 @@ + + Removes all stretches which amountWorkPercentage value is 100 as now these stretches will be created automatically and never stored into DB + + DELETE FROM stretches WHERE amount_work_percentage = 1.00; + + + diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/allocation/streches/StrechesFunctionConfiguration.java b/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/allocation/streches/StrechesFunctionConfiguration.java index 5a22cdc21..b50dada59 100644 --- a/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/allocation/streches/StrechesFunctionConfiguration.java +++ b/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/allocation/streches/StrechesFunctionConfiguration.java @@ -94,10 +94,7 @@ public abstract class StrechesFunctionConfiguration implements @Override public void applyOn(ResourceAllocation resourceAllocation) { - StretchesFunction stretchesFunction = StretchesFunctionModel - .createDefaultStretchesFunction(resourceAllocation.getTask() - .getEndAsLocalDate()); - resourceAllocation.setAssignmentFunction(stretchesFunction); + resourceAllocation.setAssignmentFunction(StretchesFunction.create()); } } diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/allocation/streches/StretchesFunctionModel.java b/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/allocation/streches/StretchesFunctionModel.java index 6584ddc41..e9348df6f 100644 --- a/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/allocation/streches/StretchesFunctionModel.java +++ b/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/allocation/streches/StretchesFunctionModel.java @@ -63,13 +63,6 @@ import org.zkoss.util.Locales; @Scope(BeanDefinition.SCOPE_PROTOTYPE) public class StretchesFunctionModel implements IStretchesFunctionModel { - public static StretchesFunction createDefaultStretchesFunction(LocalDate endDate) { - StretchesFunction stretchesFunction = StretchesFunction.create(); - stretchesFunction.addStretch(Stretch.create(endDate, BigDecimal.ONE, - BigDecimal.ONE)); - return stretchesFunction; - } - /** * Conversation state */ @@ -108,6 +101,7 @@ public class StretchesFunctionModel implements IStretchesFunctionModel { this.taskEndDate = task.getEndDate(); // Initialize stretchesFunction + stretchesFunction.setTaskEndDate(task.getEndAsLocalDate()); this.originalStretchesFunction = stretchesFunction; this.stretchesFunction = stretchesFunction.copy(); this.stretchesFunction.changeTypeTo(type);