From 8848d7938fe6892449cecd9d17d59dfa7df2929a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93scar=20Gonz=C3=A1lez=20Fern=C3=A1ndez?= Date: Tue, 1 Feb 2011 17:00:36 +0100 Subject: [PATCH] [Bug #835] Fix bug A collection must not be modified while it's being iterated. Otherwise a ConcurrentModificationException can happen. The filtering methods have been refactored. FEA: ItEr69S04BugFixing --- .../entities/AvailabilityTimeLine.java | 18 +++++-- .../workreports/entities/WorkReportLine.java | 8 +++ .../planner/company/CompanyPlanningModel.java | 49 ++++++++++--------- 3 files changed, 48 insertions(+), 27 deletions(-) diff --git a/navalplanner-business/src/main/java/org/navalplanner/business/calendars/entities/AvailabilityTimeLine.java b/navalplanner-business/src/main/java/org/navalplanner/business/calendars/entities/AvailabilityTimeLine.java index a26c4b13d..4d2f0559f 100644 --- a/navalplanner-business/src/main/java/org/navalplanner/business/calendars/entities/AvailabilityTimeLine.java +++ b/navalplanner-business/src/main/java/org/navalplanner/business/calendars/entities/AvailabilityTimeLine.java @@ -245,9 +245,21 @@ public class AvailabilityTimeLine { public static class Interval implements Comparable { - static Interval create(LocalDate start, LocalDate end) { - return new Interval(new FixedPoint(start), new FixedPoint( - end)); + /** + * Creates an interval. Null values can be provided. + * + * @param start + * if null is interpreted as start of time. + * @param end + * if null is interpreted as end of time + * @return an interval from start to end + */ + public static Interval create(LocalDate start, LocalDate end) { + DatePoint startPoint = start == null ? new StartOfTime() + : new FixedPoint(start); + DatePoint endPoint = end == null ? new EndOfTime() + : new FixedPoint(end); + return new Interval(startPoint, endPoint); } static Interval all() { diff --git a/navalplanner-business/src/main/java/org/navalplanner/business/workreports/entities/WorkReportLine.java b/navalplanner-business/src/main/java/org/navalplanner/business/workreports/entities/WorkReportLine.java index 52ecb0ba7..c02ffbbb5 100644 --- a/navalplanner-business/src/main/java/org/navalplanner/business/workreports/entities/WorkReportLine.java +++ b/navalplanner-business/src/main/java/org/navalplanner/business/workreports/entities/WorkReportLine.java @@ -30,6 +30,7 @@ import org.hibernate.validator.AssertTrue; import org.hibernate.validator.NotNull; import org.hibernate.validator.Valid; import org.joda.time.Hours; +import org.joda.time.LocalDate; import org.joda.time.LocalTime; import org.navalplanner.business.common.IntegrationEntity; import org.navalplanner.business.common.Registry; @@ -155,6 +156,13 @@ public class WorkReportLine extends IntegrationEntity implements Comparable { return date; } + public LocalDate getLocalDate() { + if (getDate() == null) { + return null; + } + return LocalDate.fromDateFields(getDate()); + } + public void setDate(Date date) { this.date = date; if ((workReport != null) && (workReport.getWorkReportType() != null)) { diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/company/CompanyPlanningModel.java b/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/company/CompanyPlanningModel.java index d6625a779..26c50dd4c 100644 --- a/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/company/CompanyPlanningModel.java +++ b/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/company/CompanyPlanningModel.java @@ -22,7 +22,6 @@ package org.navalplanner.web.planner.company; import static org.navalplanner.web.I18nHelper._; -import static org.navalplanner.web.resourceload.ResourceLoadModel.asDate; import java.math.BigDecimal; import java.util.ArrayList; @@ -40,6 +39,7 @@ import java.util.SortedMap; import java.util.TreeMap; import org.joda.time.LocalDate; +import org.navalplanner.business.calendars.entities.AvailabilityTimeLine; import org.navalplanner.business.calendars.entities.BaseCalendar; import org.navalplanner.business.common.IAdHocTransactionService; import org.navalplanner.business.common.IOnTransaction; @@ -755,6 +755,11 @@ public abstract class CompanyPlanningModel implements ICompanyPlanningModel { return filterFinishDate; } + private AvailabilityTimeLine.Interval getFilterInterval() { + return AvailabilityTimeLine.Interval.create(getFilterStartDate(), + getFilterFinishDate()); + } + private class CompanyLoadChartFiller extends ChartFiller { @Override @@ -824,8 +829,7 @@ public abstract class CompanyPlanningModel implements ICompanyPlanningModel { Map> estimatedCostPerTask = databaseSnapshots.snapshotEstimatedCostPerTask(); Collection list = filterTasksByDate( - estimatedCostPerTask.keySet(), - asDate(filterStartDate), asDate(filterFinishDate)); + estimatedCostPerTask.keySet(), getFilterInterval()); SortedMap estimatedCost = new TreeMap(); @@ -853,7 +857,7 @@ public abstract class CompanyPlanningModel implements ICompanyPlanningModel { Collection workReportLines = filterWorkReportLinesByDate( databaseSnapshots.snapshotWorkReportLines(), - asDate(filterStartDate), asDate(filterFinishDate)); + getFilterInterval()); if (workReportLines.isEmpty()) { return result; @@ -876,8 +880,7 @@ public abstract class CompanyPlanningModel implements ICompanyPlanningModel { Map> advanceCostPerTask = databaseSnapshots.snapshotAdvanceCostPerTask(); Collection list = filterTasksByDate( - advanceCostPerTask.keySet(), - asDate(filterStartDate), asDate(filterFinishDate)); + advanceCostPerTask.keySet(), getFilterInterval()); SortedMap advanceCost = new TreeMap(); @@ -896,32 +899,30 @@ public abstract class CompanyPlanningModel implements ICompanyPlanningModel { return getEarnedValueSelectedIndicators(); } - private Collection filterTasksByDate( - Collection tasks, Date startDate, Date endDate) { - if(startDate == null && endDate == null) { - return tasks; - } + private List filterTasksByDate( + Collection tasks, + AvailabilityTimeLine.Interval interval) { + List result = new ArrayList(); for(TaskElement task : tasks) { - if((startDate != null && task.getEndDate().compareTo(startDate)<0) || - (endDate != null && task.getStartDate().compareTo(endDate)>0)) { - tasks.remove(task); + if (interval.includes(task.getStartAsLocalDate()) + || interval.includes(task.getEndAsLocalDate())) { + result.add(task); } } - return tasks; + return result; } - private Collection filterWorkReportLinesByDate( - Collection lines, Date startDate, Date endDate) { - if(startDate == null && endDate == null) { - return lines; - } + + private List filterWorkReportLinesByDate( + Collection lines, + AvailabilityTimeLine.Interval interval) { + List result = new ArrayList(); for(WorkReportLine line: lines) { - if((startDate != null && line.getDate().compareTo(startDate)<0) || - (endDate != null && line.getDate().compareTo(endDate)>0)) { - lines.remove(line); + if (interval.includes(line.getLocalDate())) { + result.add(line); } } - return lines; + return result; } }