Remove references to hours in CalendarException.

The signature of the factory methods is changed, now they receive
EffortDuration instead of a Integer. CalendarException#getHours method
removed.

FEA: ItEr60S19TimeUnitDataType
This commit is contained in:
Óscar González Fernández 2010-08-25 19:46:14 +02:00
parent f76e93815a
commit 5590dd6ff6
7 changed files with 69 additions and 54 deletions

View file

@ -247,15 +247,15 @@ public class BaseCalendar extends IntegrationEntity implements IWorkHours {
exceptions.remove(day);
}
public void updateExceptionDay(Date date, Integer hours,
public void updateExceptionDay(Date date, EffortDuration duration,
CalendarExceptionType type) throws IllegalArgumentException {
updateExceptionDay(new LocalDate(date), hours, type);
updateExceptionDay(new LocalDate(date), duration, type);
}
public void updateExceptionDay(LocalDate date, Integer hours,
public void updateExceptionDay(LocalDate date, EffortDuration duration,
CalendarExceptionType type) throws IllegalArgumentException {
removeExceptionDay(date);
CalendarException day = CalendarException.create(date, hours, type);
CalendarException day = CalendarException.create(date, duration, type);
addExceptionDay(day);
}
@ -1030,7 +1030,8 @@ public class BaseCalendar extends IntegrationEntity implements IWorkHours {
}
private boolean canWorkAt(CalendarException each) {
return each.getHours() != 0 || each.getType().isOverAssignable();
return !each.getDuration().isZero()
|| each.getType().isOverAssignable();
}
@Override

View file

@ -22,6 +22,7 @@ package org.navalplanner.business.calendars.entities;
import java.util.Date;
import org.apache.commons.lang.Validate;
import org.hibernate.validator.NotNull;
import org.joda.time.LocalDate;
import org.navalplanner.business.calendars.daos.ICalendarExceptionDAO;
@ -39,19 +40,20 @@ import org.navalplanner.business.workingday.EffortDuration;
*/
public class CalendarException extends IntegrationEntity {
public static CalendarException create(Date date, Integer hours,
public static CalendarException create(Date date, EffortDuration duration,
CalendarExceptionType type) {
return create(new CalendarException(new LocalDate(date), hours, type));
return create(new CalendarException(new LocalDate(date), duration, type));
}
public static CalendarException create(LocalDate date, Integer hours,
public static CalendarException create(LocalDate date,
EffortDuration duration,
CalendarExceptionType type) {
return create(new CalendarException(date, hours, type));
return create(new CalendarException(date, duration, type));
}
public static CalendarException create(String code, LocalDate date,
Integer hours, CalendarExceptionType type) {
return create(new CalendarException(date, hours, type), code);
EffortDuration duration, CalendarExceptionType type) {
return create(new CalendarException(date, duration, type), code);
}
private static EffortDuration fromHours(Integer hours) {
@ -86,10 +88,11 @@ public class CalendarException extends IntegrationEntity {
}
private CalendarException(LocalDate date, Integer hours,
private CalendarException(LocalDate date, EffortDuration duration,
CalendarExceptionType type) {
Validate.notNull(duration);
this.date = date;
this.duration = fromHours(hours);
this.duration = duration;
this.type = type;
}
@ -99,10 +102,6 @@ public class CalendarException extends IntegrationEntity {
}
@NotNull
public Integer getHours() {
return duration != null ? duration.getHours() : 0;
}
public EffortDuration getDuration() {
return duration;
}

View file

@ -51,6 +51,7 @@ import org.navalplanner.business.resources.daos.IResourceDAO;
import org.navalplanner.business.resources.entities.Worker;
import org.navalplanner.business.test.calendars.entities.BaseCalendarTest;
import org.navalplanner.business.test.resources.daos.ResourceDAOTest;
import org.navalplanner.business.workingday.EffortDuration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.test.context.ContextConfiguration;
@ -115,7 +116,8 @@ public class BaseCalendarDAOTest {
CalendarExceptionType type = calendarExceptionTypeDAO.list(
CalendarExceptionType.class).get(0);
CalendarException christmasDay = CalendarException.create(
BaseCalendarTest.CHRISTMAS_DAY_LOCAL_DATE, 0, type);
BaseCalendarTest.CHRISTMAS_DAY_LOCAL_DATE,
EffortDuration.zero(), type);
calendar.addExceptionDay(christmasDay);
}

View file

@ -26,6 +26,8 @@ import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.navalplanner.business.workingday.EffortDuration.hours;
import static org.navalplanner.business.workingday.EffortDuration.zero;
import java.util.Set;
@ -76,7 +78,7 @@ public class BaseCalendarTest {
calendar.setName("Test");
EffortDuration eightHours = EffortDuration.hours(8);
EffortDuration eightHours = hours(8);
calendar.setDurationAt(Days.MONDAY, eightHours);
calendar.setDurationAt(Days.TUESDAY, eightHours);
calendar.setDurationAt(Days.WEDNESDAY, eightHours);
@ -102,7 +104,8 @@ public class BaseCalendarTest {
public static void addChristmasAsExceptionDay(BaseCalendar calendar) {
CalendarException christmasDay = CalendarException.create(
CHRISTMAS_DAY_LOCAL_DATE, 0, createCalendarExceptionType());
CHRISTMAS_DAY_LOCAL_DATE, EffortDuration.zero(),
createCalendarExceptionType());
calendar.addExceptionDay(christmasDay);
}
@ -195,7 +198,7 @@ public class BaseCalendarTest {
BaseCalendar calendar = createBasicCalendar().newDerivedCalendar();
CalendarException day = CalendarException.create(WEDNESDAY_LOCAL_DATE,
4, createCalendarExceptionType());
hours(4), createCalendarExceptionType());
calendar.addExceptionDay(day);
int mondayHours = calendar.getCapacityAt(MONDAY_LOCAL_DATE);
@ -213,7 +216,8 @@ public class BaseCalendarTest {
BaseCalendar calendar = createChristmasCalendar().newDerivedCalendar();
CalendarException day = CalendarException.create(
CHRISTMAS_DAY_LOCAL_DATE, 4, createCalendarExceptionType());
CHRISTMAS_DAY_LOCAL_DATE, hours(4),
createCalendarExceptionType());
calendar.addExceptionDay(day);
int hours = calendar.getCapacityAt(CHRISTMAS_DAY_LOCAL_DATE);
@ -241,12 +245,12 @@ public class BaseCalendarTest {
public void testAddTwoExceptionDaysInTheSameDate() {
BaseCalendar calendar = createBasicCalendar();
CalendarException day = CalendarException.create(MONDAY_LOCAL_DATE, 8,
createCalendarExceptionType());
CalendarException day = CalendarException.create(MONDAY_LOCAL_DATE,
hours(8), createCalendarExceptionType());
calendar.addExceptionDay(day);
CalendarException day2 = CalendarException.create(MONDAY_LOCAL_DATE, 4,
createCalendarExceptionType());
CalendarException day2 = CalendarException.create(MONDAY_LOCAL_DATE,
hours(4), createCalendarExceptionType());
calendar.addExceptionDay(day2);
}
@ -291,8 +295,8 @@ public class BaseCalendarTest {
BaseCalendar calendar = createBasicCalendar();
calendar.newVersion(MONDAY_LOCAL_DATE);
calendar.setDurationAt(Days.WEDNESDAY, EffortDuration.hours(4));
calendar.setDurationAt(Days.SUNDAY, EffortDuration.hours(4));
calendar.setDurationAt(Days.WEDNESDAY, hours(4));
calendar.setDurationAt(Days.SUNDAY, hours(4));
assertThat(calendar.getCapacityAt(WEDNESDAY_LOCAL_DATE), equalTo(4));
@ -311,8 +315,8 @@ public class BaseCalendarTest {
BaseCalendar calendar = createBasicCalendar();
calendar.newVersion(MONDAY_LOCAL_DATE);
calendar.setDurationAt(Days.MONDAY, EffortDuration.hours(1));
calendar.setDurationAt(Days.SUNDAY, EffortDuration.hours(2));
calendar.setDurationAt(Days.MONDAY, hours(1));
calendar.setDurationAt(Days.SUNDAY, hours(2));
assertThat(calendar.getCapacityAt(MONDAY_LOCAL_DATE), equalTo(1));
@ -354,15 +358,15 @@ public class BaseCalendarTest {
@Test
public void testGettWorkableHoursNewVersionFromChristmasCalendar() {
BaseCalendar calendar = createChristmasCalendar();
CalendarException day = CalendarException.create(CHRISTMAS_DAY_LOCAL_DATE
.plusYears(1), 0,
CalendarException day = CalendarException.create(
CHRISTMAS_DAY_LOCAL_DATE.plusYears(1), EffortDuration.zero(),
createCalendarExceptionType());
calendar.addExceptionDay(day);
calendar.newVersion(CHRISTMAS_DAY_LOCAL_DATE.plusDays(1));
calendar
.updateExceptionDay(CHRISTMAS_DAY_LOCAL_DATE.plusYears(1), 8,
calendar.updateExceptionDay(CHRISTMAS_DAY_LOCAL_DATE.plusYears(1),
hours(8),
createCalendarExceptionType());
assertThat(calendar
@ -373,7 +377,7 @@ public class BaseCalendarTest {
}
public static void setHoursForAllDays(BaseCalendar calendar, Integer hours) {
EffortDuration duration = EffortDuration.hours(hours);
EffortDuration duration = hours(hours);
for (Days each : Days.values()) {
calendar.setDurationAt(each, duration);
}
@ -427,7 +431,8 @@ public class BaseCalendarTest {
.plusDays(1));
CalendarException day = CalendarException.create(
CHRISTMAS_DAY_LOCAL_DATE, 0, createCalendarExceptionType());
CHRISTMAS_DAY_LOCAL_DATE, EffortDuration.zero(),
createCalendarExceptionType());
calendar.addExceptionDay(day);
assertThat(calendar.getExceptions().size(), equalTo(1));
@ -560,7 +565,7 @@ public class BaseCalendarTest {
@Test(expected = IllegalArgumentException.class)
public void testSetHoursInvalid() {
BaseCalendar calendar = createBasicCalendar();
calendar.setDurationAt(Days.MONDAY, EffortDuration.hours(-5));
calendar.setDurationAt(Days.MONDAY, hours(-5));
}
@Test
@ -591,10 +596,10 @@ public class BaseCalendarTest {
BaseCalendar calendar = createBasicCalendar();
calendar.newVersion(WEDNESDAY_LOCAL_DATE);
calendar.addExceptionDay(CalendarException.create(MONDAY_LOCAL_DATE, 0,
createCalendarExceptionType()));
calendar.addExceptionDay(CalendarException.create(FRIDAY_LOCAL_DATE, 0,
createCalendarExceptionType()));
calendar.addExceptionDay(CalendarException.create(MONDAY_LOCAL_DATE,
zero(), createCalendarExceptionType()));
calendar.addExceptionDay(CalendarException.create(FRIDAY_LOCAL_DATE,
zero(), createCalendarExceptionType()));
assertThat(calendar.getCapacityAt(MONDAY_LOCAL_DATE),
equalTo(0));
@ -610,7 +615,8 @@ public class BaseCalendarTest {
BaseCalendar calendar = createBasicCalendar();
LocalDate pastMonth = (new LocalDate()).minusMonths(1);
CalendarException exceptionDay = CalendarException.create(pastMonth, 0,
CalendarException exceptionDay = CalendarException.create(pastMonth,
zero(),
createCalendarExceptionType());
calendar.addExceptionDay(exceptionDay);
@ -621,8 +627,8 @@ public class BaseCalendarTest {
BaseCalendar calendar = createBasicCalendar();
LocalDate pastMonth = (new LocalDate()).minusMonths(1);
CalendarException exceptionDay = CalendarException.create(pastMonth, 0,
createCalendarExceptionType());
CalendarException exceptionDay = CalendarException.create(pastMonth,
zero(), createCalendarExceptionType());
calendar.addExceptionDay(exceptionDay);
calendar.removeExceptionDay(pastMonth);

View file

@ -29,6 +29,7 @@ import org.joda.time.LocalDate;
import org.junit.Test;
import org.navalplanner.business.calendars.entities.CalendarException;
import org.navalplanner.business.calendars.entities.CalendarExceptionType;
import org.navalplanner.business.workingday.EffortDuration;
/**
* Tests for {@link CalendarException}.
@ -43,10 +44,11 @@ public class CalendarExceptionTest {
CalendarExceptionType type = BaseCalendarTest
.createCalendarExceptionType();
CalendarException day = CalendarException.create(date, 8, type);
CalendarException day = CalendarException.create(date,
EffortDuration.hours(8), type);
assertThat(day.getDate(), equalTo(new LocalDate(date)));
assertThat(day.getHours(), equalTo(8));
assertThat(day.getDuration(), equalTo(EffortDuration.hours(8)));
assertThat(day.getType(), equalTo(type));
}

View file

@ -267,13 +267,15 @@ public class BaseCalendarModel implements IBaseCalendarModel {
@Override
public void createException(CalendarExceptionType type, Date startDate,
Date endDate, Integer hours) {
EffortDuration duration = EffortDuration.hours(hours);
for (LocalDate date = new LocalDate(startDate); date
.compareTo(new LocalDate(endDate)) <= 0; date = date
.plusDays(1)) {
if (getTypeOfDay(date).equals(DayType.OWN_EXCEPTION)) {
getBaseCalendar().updateExceptionDay(date, hours, type);
getBaseCalendar().updateExceptionDay(date, duration, type);
} else {
CalendarException day = CalendarException.create(date, hours,
CalendarException day = CalendarException.create(date,
duration,
type);
getBaseCalendar().addExceptionDay(day);
}
@ -570,6 +572,7 @@ public class BaseCalendarModel implements IBaseCalendarModel {
@Override
public void updateException(CalendarExceptionType type, Date startDate,
Date endDate, Integer hours) {
EffortDuration duration = EffortDuration.hours(hours);
for (LocalDate date = new LocalDate(startDate); date
.compareTo(new LocalDate(endDate)) <= 0; date = date
.plusDays(1)) {
@ -577,12 +580,12 @@ public class BaseCalendarModel implements IBaseCalendarModel {
if (type == null) {
getBaseCalendar().removeExceptionDay(date);
} else {
getBaseCalendar().updateExceptionDay(date, hours, type);
getBaseCalendar().updateExceptionDay(date, duration, type);
}
} else {
if (type != null) {
CalendarException day = CalendarException.create(date,
hours, type);
duration, type);
getBaseCalendar().addExceptionDay(day);
}
}

View file

@ -37,12 +37,13 @@ import org.apache.commons.lang.StringUtils;
import org.joda.time.LocalDate;
import org.navalplanner.business.calendars.entities.BaseCalendar;
import org.navalplanner.business.calendars.entities.CalendarData;
import org.navalplanner.business.calendars.entities.CalendarData.Days;
import org.navalplanner.business.calendars.entities.CalendarException;
import org.navalplanner.business.calendars.entities.CalendarExceptionType;
import org.navalplanner.business.calendars.entities.CalendarData.Days;
import org.navalplanner.business.common.Registry;
import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
import org.navalplanner.business.common.exceptions.ValidationException;
import org.navalplanner.business.workingday.EffortDuration;
import org.navalplanner.ws.calendars.api.BaseCalendarDTO;
import org.navalplanner.ws.calendars.api.CalendarDataDTO;
import org.navalplanner.ws.calendars.api.CalendarExceptionDTO;
@ -83,9 +84,10 @@ public final class CalendarConverter {
CalendarException calendarException) {
XMLGregorianCalendar date = DateConverter
.toXMLGregorianCalendar(calendarException.getDate());
int hours = calendarException.getDuration().getHours();
String code = calendarException.getType().getCode();
return new CalendarExceptionDTO(calendarException.getCode(), date,
calendarException.getHours(), calendarException.getType()
.getCode());
hours, code);
}
private final static CalendarDataDTO toDTO(CalendarData calendarData) {
@ -147,7 +149,7 @@ public final class CalendarConverter {
CalendarExceptionType type = findCalendarExceptionType(calendarExceptionDTO.calendarExceptionTypeCode);
return CalendarException.create(calendarExceptionDTO.code, date,
calendarExceptionDTO.hours, type);
EffortDuration.hours(calendarExceptionDTO.hours), type);
}
public final static CalendarData toEntity(CalendarDataDTO calendarDataDTO) {