[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
This commit is contained in:
Óscar González Fernández 2011-02-01 17:00:36 +01:00
parent 49db11f855
commit 8848d7938f
3 changed files with 48 additions and 27 deletions

View file

@ -245,9 +245,21 @@ public class AvailabilityTimeLine {
public static class Interval implements
Comparable<Interval> {
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 <code>null</code> is interpreted as start of time.
* @param end
* if <code>null</code> 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() {

View file

@ -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)) {

View file

@ -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<TaskElement, SortedMap<LocalDate, BigDecimal>> estimatedCostPerTask =
databaseSnapshots.snapshotEstimatedCostPerTask();
Collection<TaskElement> list = filterTasksByDate(
estimatedCostPerTask.keySet(),
asDate(filterStartDate), asDate(filterFinishDate));
estimatedCostPerTask.keySet(), getFilterInterval());
SortedMap<LocalDate, BigDecimal> estimatedCost = new TreeMap<LocalDate, BigDecimal>();
@ -853,7 +857,7 @@ public abstract class CompanyPlanningModel implements ICompanyPlanningModel {
Collection<WorkReportLine> 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<TaskElement, SortedMap<LocalDate, BigDecimal>> advanceCostPerTask =
databaseSnapshots.snapshotAdvanceCostPerTask();
Collection<TaskElement> list = filterTasksByDate(
advanceCostPerTask.keySet(),
asDate(filterStartDate), asDate(filterFinishDate));
advanceCostPerTask.keySet(), getFilterInterval());
SortedMap<LocalDate, BigDecimal> advanceCost = new TreeMap<LocalDate, BigDecimal>();
@ -896,32 +899,30 @@ public abstract class CompanyPlanningModel implements ICompanyPlanningModel {
return getEarnedValueSelectedIndicators();
}
private Collection<TaskElement> filterTasksByDate(
Collection<TaskElement> tasks, Date startDate, Date endDate) {
if(startDate == null && endDate == null) {
return tasks;
}
private List<TaskElement> filterTasksByDate(
Collection<TaskElement> tasks,
AvailabilityTimeLine.Interval interval) {
List<TaskElement> result = new ArrayList<TaskElement>();
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<WorkReportLine> filterWorkReportLinesByDate(
Collection<WorkReportLine> lines, Date startDate, Date endDate) {
if(startDate == null && endDate == null) {
return lines;
}
private List<WorkReportLine> filterWorkReportLinesByDate(
Collection<WorkReportLine> lines,
AvailabilityTimeLine.Interval interval) {
List<WorkReportLine> result = new ArrayList<WorkReportLine>();
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;
}
}