[Bug #903] Limiting resource allocation window does not respect activation periods for the resources
The activation periods of the Calendar assigned to the Resource were not being considered when doing a limiting allocation. A gap should be constrained according to the activation periods of time of the Calendar. FEA: ItEr71S04BugFixing
This commit is contained in:
parent
16b7458d5f
commit
6f02dda4d6
2 changed files with 141 additions and 1 deletions
|
|
@ -0,0 +1,129 @@
|
|||
/*
|
||||
* This file is part of NavalPlan
|
||||
*
|
||||
* Copyright (C) 2011 Igalia,S.L
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.navalplanner.business.planner.limiting.entities;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import org.joda.time.LocalDate;
|
||||
import org.navalplanner.business.calendars.entities.CalendarAvailability;
|
||||
import org.navalplanner.business.resources.entities.Resource;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Diego Pino Garcia <dpino@igalia.com>
|
||||
*
|
||||
* Represents the interval of time of a Gap.
|
||||
*
|
||||
* It's possible for a calendar assigned to a resource to define
|
||||
* activation periods of time. This class is used to check if a gap is
|
||||
* activated in a period of time. The method delimitByActivationPeriods
|
||||
* returns the interval of time in which the gap in activated according
|
||||
* to the calendar
|
||||
*
|
||||
*
|
||||
*/
|
||||
public class GapInterval {
|
||||
|
||||
protected DateAndHour start;
|
||||
|
||||
protected DateAndHour end;
|
||||
|
||||
public static GapInterval create(DateAndHour start, DateAndHour end) {
|
||||
return new GapInterval(start, end);
|
||||
}
|
||||
|
||||
public static Collection<? extends Gap> gapsOn(
|
||||
List<GapInterval> intervals, Resource resource) {
|
||||
List<Gap> result = new ArrayList<Gap>();
|
||||
for (GapInterval each: intervals) {
|
||||
result.add(each.gapOn(resource));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public GapInterval(DateAndHour start, DateAndHour end) {
|
||||
this.start = start;
|
||||
this.end = end;
|
||||
}
|
||||
|
||||
public List<GapInterval> delimitByActivationPeriods(
|
||||
List<CalendarAvailability> activationPeriods) {
|
||||
List<GapInterval> result = new ArrayList<GapInterval>();
|
||||
for (CalendarAvailability interval: activationPeriods) {
|
||||
GapInterval gapInterval = delimitByInterval(interval);
|
||||
if (gapInterval != null) {
|
||||
result.add(gapInterval);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private GapInterval delimitByInterval(CalendarAvailability interval) {
|
||||
LocalDate start = interval.getStartDate();
|
||||
LocalDate end = interval.getEndDate();
|
||||
|
||||
LocalDate newStart = max(start, interval.getStartDate());
|
||||
LocalDate newEnd = min(end, interval.getEndDate());
|
||||
if (newEnd != null && newStart.isAfter(newEnd)) {
|
||||
// The period of time is not valid, as it's not period of time
|
||||
// activated according to calendar
|
||||
return null;
|
||||
}
|
||||
return GapInterval.create(DateAndHour.from(newStart),
|
||||
newEnd != null ? DateAndHour.from(newEnd) : null);
|
||||
}
|
||||
|
||||
private LocalDate max(LocalDate date1, LocalDate date2) {
|
||||
if (date1 == null && date2 == null) {
|
||||
return null;
|
||||
}
|
||||
if (date1 != null && date2 == null) {
|
||||
return date1;
|
||||
}
|
||||
if (date1 == null && date2 != null) {
|
||||
return date2;
|
||||
}
|
||||
return date1.isAfter(date2) ? date1 : date2;
|
||||
}
|
||||
|
||||
private LocalDate min(LocalDate date1, LocalDate date2) {
|
||||
if (date1 == null && date2 == null) {
|
||||
return null;
|
||||
}
|
||||
if (date1 != null && date2 == null) {
|
||||
return date1;
|
||||
}
|
||||
if (date1 == null && date2 != null) {
|
||||
return date2;
|
||||
}
|
||||
return date1.isBefore(date2) || date1.isEqual(date2) ? date1 : date2;
|
||||
}
|
||||
|
||||
public Gap gapOn(Resource resource) {
|
||||
return Gap.create(resource, start, end);
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return String.format("[%s, %s]", start, end);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -33,10 +33,13 @@ import java.util.Set;
|
|||
import java.util.SortedSet;
|
||||
import java.util.TreeSet;
|
||||
|
||||
import org.navalplanner.business.calendars.entities.CalendarAvailability;
|
||||
import org.navalplanner.business.calendars.entities.ResourceCalendar;
|
||||
import org.navalplanner.business.common.BaseEntity;
|
||||
import org.navalplanner.business.planner.limiting.entities.DateAndHour;
|
||||
import org.navalplanner.business.planner.limiting.entities.Gap;
|
||||
import org.navalplanner.business.planner.limiting.entities.Gap.GapOnQueue;
|
||||
import org.navalplanner.business.planner.limiting.entities.GapInterval;
|
||||
import org.navalplanner.business.planner.limiting.entities.InsertionRequirements;
|
||||
import org.navalplanner.business.planner.limiting.entities.LimitingResourceQueueElement;
|
||||
/**
|
||||
|
|
@ -104,10 +107,18 @@ public class LimitingResourceQueue extends BaseEntity {
|
|||
private List<GapOnQueue> calculateGaps() {
|
||||
List<Gap> result = new ArrayList<Gap>();
|
||||
DateAndHour previousEnd = null;
|
||||
ResourceCalendar calendar = resource.getCalendar();
|
||||
List<CalendarAvailability> activationPeriods = calendar.getCalendarAvailabilities();
|
||||
|
||||
for (LimitingResourceQueueElement each : limitingResourceQueueElements) {
|
||||
DateAndHour startTime = each.getStartTime();
|
||||
if (previousEnd == null || startTime.isAfter(previousEnd)) {
|
||||
result.add(Gap.create(resource, previousEnd, startTime));
|
||||
List<GapInterval> gapIntervals = GapInterval.
|
||||
create(previousEnd, startTime).
|
||||
delimitByActivationPeriods(activationPeriods);
|
||||
if (!gapIntervals.isEmpty()) {
|
||||
result.addAll(GapInterval.gapsOn(gapIntervals, resource));
|
||||
}
|
||||
}
|
||||
previousEnd = each.getEndTime();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue