ItEr46S12CUVisualizacionResponsabilidadesTRaballoNaPlanificacion: Adding method thereAreAvailableHoursFrom to BaseCalendar.
It checks if from a date there is enough capacity
This commit is contained in:
parent
6f45f7b2a6
commit
e95de39cb6
3 changed files with 114 additions and 0 deletions
|
|
@ -813,4 +813,69 @@ public class BaseCalendar extends BaseEntity implements IWorkHours {
|
|||
return workableHours;
|
||||
}
|
||||
|
||||
protected boolean thereAreAvailableHoursFrom(LocalDate start,
|
||||
ResourcesPerDay resourcesPerDay, int hoursToAllocate) {
|
||||
LocalDate expiringDate = getAvailabilityExpiringDate();
|
||||
if (expiringDate == null) {
|
||||
return !onlyGivesZeroHours();
|
||||
}
|
||||
return thereAreHoursUntil(hoursToAllocate, resourcesPerDay, start,
|
||||
expiringDate);
|
||||
}
|
||||
|
||||
public boolean onlyGivesZeroHours() {
|
||||
CalendarData last = lastCalendarData();
|
||||
return last.isEmpty();
|
||||
}
|
||||
|
||||
public boolean onlyGivesZeroHours(Days each) {
|
||||
CalendarData last = lastCalendarData();
|
||||
return last.isEmptyFor(each);
|
||||
}
|
||||
|
||||
private CalendarData lastCalendarData() {
|
||||
return calendarDataVersions.get(calendarDataVersions.size() - 1);
|
||||
}
|
||||
|
||||
private boolean thereAreHoursUntil(int hoursToAllocate,
|
||||
ResourcesPerDay resourcesPerDay, LocalDate start,
|
||||
LocalDate expiringDate) {
|
||||
int hoursSum = 0;
|
||||
int days = org.joda.time.Days.daysBetween(start, expiringDate)
|
||||
.getDays();
|
||||
for (int i = 0; i < days; i++) {
|
||||
LocalDate current = start.plusDays(i);
|
||||
hoursSum += toHours(current, resourcesPerDay);
|
||||
if (hoursSum >= hoursToAllocate) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private LocalDate getAvailabilityExpiringDate() {
|
||||
List<CalendarAvailability> availabilities = getCalendarAvailabilities();
|
||||
if (availabilities.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
CalendarAvailability last = availabilities.get(0);
|
||||
for (CalendarAvailability each : availabilities) {
|
||||
LocalDate startDate = each.getStartDate();
|
||||
if (startDate.compareTo(last.getStartDate()) > 0) {
|
||||
last = each;
|
||||
}
|
||||
}
|
||||
return last.getEndDate();
|
||||
}
|
||||
|
||||
private List<CalendarException> getExceptionsFrom(LocalDate start) {
|
||||
List<CalendarException> result = new ArrayList<CalendarException>();
|
||||
for (CalendarException each : exceptions) {
|
||||
if (each.getDate().compareTo(start) >= 0) {
|
||||
result.add(each);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -132,4 +132,28 @@ public class CalendarData extends BaseEntity {
|
|||
this.expiringDate = null;
|
||||
}
|
||||
|
||||
public boolean isPosteriorTo(LocalDate date) {
|
||||
return expiringDate == null || expiringDate.compareTo(date) > 0;
|
||||
}
|
||||
|
||||
boolean isEmpty() {
|
||||
for (Days each : Days.values()) {
|
||||
if (!isEmptyFor(each)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
boolean isEmptyFor(Days day) {
|
||||
Integer hours = getHours(day);
|
||||
if (!isDefault(day) && hours > 0) {
|
||||
return false;
|
||||
} else if (isDefault(day) && getParent() != null
|
||||
&& !parent.onlyGivesZeroHours(day)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -106,6 +106,31 @@ public class BaseCalendarTest {
|
|||
calendar.addExceptionDay(christmasDay);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnlyGivesZeroHoursWhenThereIsNoParent() {
|
||||
BaseCalendar calendar = createBasicCalendar();
|
||||
assertFalse(calendar.onlyGivesZeroHours());
|
||||
initializeAllToZeroHours(calendar);
|
||||
assertTrue(calendar.onlyGivesZeroHours());
|
||||
}
|
||||
|
||||
private void initializeAllToZeroHours(BaseCalendar calendar) {
|
||||
for (Days each : Days.values()) {
|
||||
calendar.setHours(each, 0);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnlyGivesZeroHoursWhenThereIsParent() {
|
||||
BaseCalendar calendar = createBasicCalendar();
|
||||
initializeAllToZeroHours(calendar);
|
||||
BaseCalendar parent = createBasicCalendar();
|
||||
calendar.setParent(parent);
|
||||
assertTrue(calendar.onlyGivesZeroHours());
|
||||
calendar.setDefault(Days.MONDAY);
|
||||
assertFalse(calendar.onlyGivesZeroHours());
|
||||
}
|
||||
|
||||
public static BaseCalendar createChristmasCalendar() {
|
||||
BaseCalendar calendar = createBasicCalendar();
|
||||
addChristmasAsExceptionDay(calendar);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue