Bug #1562: Fix issue filtering properly resources according to their activation periods

Now the dates filtering the resource load window are checked against the
resources activation periods. Using the following algorithm:

1) If the resource has not activation periods then it should always appear

2) The filter can have null for start and end dates and the activation periods
   can have null for end date. So we should define what to do in each case:

  Let's define some acronyms:
  * Filter Start Date: FSD
  * Filter End Date: FED
  * Activation Period Start Date: APSD (cannot be null)
  * Activation Period End Date: APED

  2.1) FSD is null and FED are null: The resource should appear regardless its
       activation periods

  2.2) FSD is null:
    2.2.1) APED is null: Check if APSD is lower or equals than FED
    2.2.2) APED is not null: Check if APSD is lower or equals than FED or APED
           is lower or equals than FED

  2.3) FED is null:
    2.3.1) APED is null: The resource should appear
    2.3.2) APED is not null: Check if APSD is later or equals than FSD or APED
           is later or equals than FSD

  2.4) FSD is not null and FED is not null:
    2.4.1) APED is null: Check if APSD is between FSD and FED
    2.4.2) APED is not null: Check if activation period overlaps filter period
           at any point

FEA: ItEr77S04BugFixing
This commit is contained in:
Manuel Rego Casasnovas 2012-11-23 17:44:10 +01:00
parent a323eb400c
commit 05132fd5a7
5 changed files with 74 additions and 1 deletions

View file

@ -878,6 +878,18 @@ public class BaseCalendar extends IntegrationEntity implements ICalendar,
return false;
}
public boolean isActiveBetween(LocalDate startDate, LocalDate endDate) {
if (getCalendarAvailabilities().isEmpty()) {
return true;
}
for (CalendarAvailability calendarAvailability : getCalendarAvailabilities()) {
if (calendarAvailability.isActiveBetween(startDate, endDate)) {
return true;
}
}
return false;
}
public boolean canWorkOn(LocalDate date) {
Capacity capacity = findCapacityAt(date);
return capacity.allowsWorking();

View file

@ -25,6 +25,7 @@ import java.util.Comparator;
import java.util.Date;
import org.hibernate.validator.NotNull;
import org.joda.time.Interval;
import org.joda.time.LocalDate;
import org.libreplan.business.calendars.daos.ICalendarAvailabilityDAO;
import org.libreplan.business.common.IntegrationEntity;
@ -126,4 +127,40 @@ public class CalendarAvailability extends IntegrationEntity {
return Registry.getCalendarAvailabilityDAO();
}
public boolean isActiveBetween(LocalDate filterStartDate,
LocalDate filterEndDate) {
if (filterStartDate == null && filterEndDate == null) {
return true;
}
if (filterStartDate == null) {
if (endDate == null) {
return startDate.compareTo(filterEndDate) <= 0;
}
return startDate.compareTo(filterEndDate) <= 0
|| endDate.compareTo(filterEndDate) <= 0;
}
if (filterEndDate == null) {
if (endDate == null) {
return true;
}
return startDate.compareTo(filterStartDate) >= 0
|| endDate.compareTo(filterStartDate) >= 0;
}
if (endDate == null) {
return startDate.compareTo(filterStartDate) >= 0
&& startDate.compareTo(filterEndDate) <= 0;
}
Interval filterPeriod = new Interval(
filterStartDate.toDateTimeAtStartOfDay(), filterEndDate
.plusDays(1).toDateTimeAtStartOfDay());
Interval activationPeriod = new Interval(
startDate.toDateTimeAtStartOfDay(), endDate.plusDays(1)
.toDateTimeAtStartOfDay());
return filterPeriod.overlaps(activationPeriod);
}
}

View file

@ -88,4 +88,5 @@ public class ResourceCalendar extends BaseCalendar {
public Boolean isCodeAutogenerated() {
return true;
}
}

View file

@ -1199,4 +1199,8 @@ public abstract class Resource extends IntegrationEntity implements
return true;
}
public boolean isActiveBetween(LocalDate startDate, LocalDate endDate) {
return calendar.isActiveBetween(startDate, endDate);
}
}

View file

@ -309,7 +309,9 @@ public class ResourceLoadModel implements IResourceLoadModel {
if (parameters.thereIsCurrentOrder()) {
return resourcesForActiveTasks();
} else {
return allResources();
return allResourcesActiveBetween(
parameters.getInitDateFilter(),
parameters.getEndDateFilter());
}
}
@ -319,6 +321,23 @@ public class ResourceLoadModel implements IResourceLoadModel {
.getResourcesRelatedWithAllocations());
}
private List<Resource> allResourcesActiveBetween(
LocalDate startDate, LocalDate endDate) {
List<Resource> allResources = allResources();
if (startDate == null && endDate == null) {
return allResources;
}
List<Resource> resources = new ArrayList<Resource>();
for (Resource resource : allResources) {
if (resource
.isActiveBetween(startDate, endDate)) {
resources.add(resource);
}
}
return resources;
}
private List<Resource> allResources() {
return Resource.sortByName(resourcesDAO
.list(Resource.class));