ItEr22S08CUAltaCalendarioLaboralItEr21S10: Added new methods to entities that will be needed in the use case implementation.
This commit is contained in:
parent
bf4f40b0fe
commit
1f1f147e0e
3 changed files with 219 additions and 3 deletions
|
|
@ -52,6 +52,10 @@ public class BaseCalendar extends BaseEntity implements IValidable {
|
|||
|
||||
private Set<ExceptionDay> exceptions = new HashSet<ExceptionDay>();
|
||||
|
||||
public enum DayType {
|
||||
NORMAL, ZERO_HOURS, OWN_EXCEPTION, ANCESTOR_EXCEPTION;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for hibernate. Do not use!
|
||||
*/
|
||||
|
|
@ -79,6 +83,14 @@ public class BaseCalendar extends BaseEntity implements IValidable {
|
|||
}
|
||||
}
|
||||
|
||||
public boolean isDefaultMonday() {
|
||||
return (this.monday == null);
|
||||
}
|
||||
|
||||
public void setDefaultMonday() {
|
||||
this.monday = null;
|
||||
}
|
||||
|
||||
private Integer valueIfNotNullElseDefaultValue(Integer hours) {
|
||||
if (hours == null) {
|
||||
return DEFAULT_VALUE;
|
||||
|
|
@ -98,6 +110,14 @@ public class BaseCalendar extends BaseEntity implements IValidable {
|
|||
}
|
||||
}
|
||||
|
||||
public boolean isDefaultTuesday() {
|
||||
return (this.tuesday == null);
|
||||
}
|
||||
|
||||
public void setDefaultTuesday() {
|
||||
this.tuesday = null;
|
||||
}
|
||||
|
||||
public void setWednesday(Integer wednesday) {
|
||||
this.wednesday = wednesday;
|
||||
}
|
||||
|
|
@ -110,6 +130,14 @@ public class BaseCalendar extends BaseEntity implements IValidable {
|
|||
}
|
||||
}
|
||||
|
||||
public boolean isDefaultWednesday() {
|
||||
return (this.wednesday == null);
|
||||
}
|
||||
|
||||
public void setDefaultWednesday() {
|
||||
this.wednesday = null;
|
||||
}
|
||||
|
||||
public void setThursday(Integer thursday) {
|
||||
this.thursday = thursday;
|
||||
}
|
||||
|
|
@ -122,6 +150,14 @@ public class BaseCalendar extends BaseEntity implements IValidable {
|
|||
}
|
||||
}
|
||||
|
||||
public boolean isDefaultThursday() {
|
||||
return (this.thursday == null);
|
||||
}
|
||||
|
||||
public void setDefaultThursday() {
|
||||
this.thursday = null;
|
||||
}
|
||||
|
||||
public void setFriday(Integer friday) {
|
||||
this.friday = friday;
|
||||
}
|
||||
|
|
@ -134,6 +170,14 @@ public class BaseCalendar extends BaseEntity implements IValidable {
|
|||
}
|
||||
}
|
||||
|
||||
public boolean isDefaultFriday() {
|
||||
return (this.friday == null);
|
||||
}
|
||||
|
||||
public void setDefaultFriday() {
|
||||
this.friday = null;
|
||||
}
|
||||
|
||||
public void setSaturday(Integer saturday) {
|
||||
this.saturday = saturday;
|
||||
}
|
||||
|
|
@ -146,6 +190,14 @@ public class BaseCalendar extends BaseEntity implements IValidable {
|
|||
}
|
||||
}
|
||||
|
||||
public boolean isDefaultSaturday() {
|
||||
return (this.saturday == null);
|
||||
}
|
||||
|
||||
public void setDefaultSaturday() {
|
||||
this.saturday = null;
|
||||
}
|
||||
|
||||
public void setSunday(Integer sunday) {
|
||||
this.sunday = sunday;
|
||||
}
|
||||
|
|
@ -158,10 +210,22 @@ public class BaseCalendar extends BaseEntity implements IValidable {
|
|||
}
|
||||
}
|
||||
|
||||
public boolean isDefaultSunday() {
|
||||
return (this.sunday == null);
|
||||
}
|
||||
|
||||
public void setDefaultSunday() {
|
||||
this.sunday = null;
|
||||
}
|
||||
|
||||
public BaseCalendar getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
public boolean isDerived() {
|
||||
return (parent != null);
|
||||
}
|
||||
|
||||
public BaseCalendar getPreviousCalendar() {
|
||||
return previousCalendar;
|
||||
}
|
||||
|
|
@ -174,6 +238,10 @@ public class BaseCalendar extends BaseEntity implements IValidable {
|
|||
return expiringDate;
|
||||
}
|
||||
|
||||
public Set<ExceptionDay> getOwnExceptions() {
|
||||
return Collections.unmodifiableSet(exceptions);
|
||||
}
|
||||
|
||||
public Set<ExceptionDay> getExceptions() {
|
||||
Set<ExceptionDay> exceptionDays = new HashSet<ExceptionDay>();
|
||||
exceptionDays.addAll(exceptions);
|
||||
|
|
@ -226,7 +294,7 @@ public class BaseCalendar extends BaseEntity implements IValidable {
|
|||
} else if (shouldUseNextCalendar(date)) {
|
||||
nextCalendar.removeExceptionDay(date);
|
||||
} else {
|
||||
ExceptionDay day = getExceptionDay(date);
|
||||
ExceptionDay day = getOwnExceptionDay(date);
|
||||
if (day == null) {
|
||||
throw new IllegalArgumentException(
|
||||
"There is not an exception day on that date");
|
||||
|
|
@ -236,6 +304,11 @@ public class BaseCalendar extends BaseEntity implements IValidable {
|
|||
}
|
||||
}
|
||||
|
||||
public void updateExceptionDay(Date date, Integer hours)
|
||||
throws IllegalArgumentException {
|
||||
updateExceptionDay(new LocalDate(date), hours);
|
||||
}
|
||||
|
||||
public void updateExceptionDay(LocalDate date, Integer hours)
|
||||
throws IllegalArgumentException {
|
||||
removeExceptionDay(date);
|
||||
|
|
@ -243,7 +316,11 @@ public class BaseCalendar extends BaseEntity implements IValidable {
|
|||
addExceptionDay(day);
|
||||
}
|
||||
|
||||
private ExceptionDay getExceptionDay(LocalDate date) {
|
||||
public ExceptionDay getOwnExceptionDay(Date date) {
|
||||
return getOwnExceptionDay(new LocalDate(date));
|
||||
}
|
||||
|
||||
public ExceptionDay getOwnExceptionDay(LocalDate date) {
|
||||
for (ExceptionDay exceptionDay : exceptions) {
|
||||
if (exceptionDay.getDate().equals(date)) {
|
||||
return exceptionDay;
|
||||
|
|
@ -253,6 +330,20 @@ public class BaseCalendar extends BaseEntity implements IValidable {
|
|||
return null;
|
||||
}
|
||||
|
||||
public ExceptionDay getExceptionDay(Date date) {
|
||||
return getExceptionDay(new LocalDate(date));
|
||||
}
|
||||
|
||||
public ExceptionDay getExceptionDay(LocalDate date) {
|
||||
for (ExceptionDay exceptionDay : getExceptions()) {
|
||||
if (exceptionDay.getDate().equals(date)) {
|
||||
return exceptionDay;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private boolean shouldUsePreviousCalendar(LocalDate date) {
|
||||
return ((previousCalendar != null) && (date.compareTo(previousCalendar
|
||||
.getExpiringDate()) < 0));
|
||||
|
|
@ -463,4 +554,24 @@ public class BaseCalendar extends BaseEntity implements IValidable {
|
|||
return copy;
|
||||
}
|
||||
|
||||
public DayType getType(Date date) {
|
||||
return getType(new LocalDate(date));
|
||||
}
|
||||
|
||||
public DayType getType(LocalDate date) {
|
||||
ExceptionDay exceptionDay = getExceptionDay(date);
|
||||
if (exceptionDay != null) {
|
||||
if (exceptions.contains(exceptionDay)) {
|
||||
return DayType.OWN_EXCEPTION;
|
||||
}
|
||||
return DayType.ANCESTOR_EXCEPTION;
|
||||
}
|
||||
|
||||
if (getWorkableHours(date) == 0) {
|
||||
return DayType.ZERO_HOURS;
|
||||
}
|
||||
|
||||
return DayType.NORMAL;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package org.navalplanner.business.test.calendars.daos;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
|
@ -56,4 +57,27 @@ public class BaseCalendarDAOTest {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void saveDerivedCalendar() {
|
||||
BaseCalendar calendar = BaseCalendarTest.createBasicCalendar();
|
||||
baseCalendarDAO.save(calendar);
|
||||
|
||||
BaseCalendar derivedCalendar = calendar.newDerivedCalendar();
|
||||
baseCalendarDAO.save(derivedCalendar);
|
||||
|
||||
try {
|
||||
|
||||
BaseCalendar savedCalendar = baseCalendarDAO.find(calendar.getId());
|
||||
assertFalse(savedCalendar.isDerived());
|
||||
|
||||
BaseCalendar savedDerivedCalendar = baseCalendarDAO
|
||||
.find(derivedCalendar.getId());
|
||||
assertTrue(savedDerivedCalendar.isDerived());
|
||||
|
||||
} catch (InstanceNotFoundException e) {
|
||||
fail("It should not throw an exception");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,13 +1,18 @@
|
|||
package org.navalplanner.business.test.calendars.entities;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.notNullValue;
|
||||
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.junit.Assert.fail;
|
||||
|
||||
import org.joda.time.LocalDate;
|
||||
import org.junit.Test;
|
||||
import org.navalplanner.business.calendars.entities.BaseCalendar;
|
||||
import org.navalplanner.business.calendars.entities.ExceptionDay;
|
||||
import org.navalplanner.business.calendars.entities.BaseCalendar.DayType;
|
||||
import org.navalplanner.business.common.exceptions.ValidationException;
|
||||
|
||||
/**
|
||||
|
|
@ -407,11 +412,87 @@ public class BaseCalendarTest {
|
|||
thenForAllDaysReturnsZero();
|
||||
}
|
||||
|
||||
|
||||
private void thenForAllDaysReturnsZero() {
|
||||
for (LocalDate localDate : DAYS_OF_A_WEEK_EXAMPLE) {
|
||||
assertThat(calendarFixture.getWorkableHours(localDate), equalTo(0));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void anUnitializedCalendarShouldHaveDefaultValues() {
|
||||
givenUnitializedCalendar();
|
||||
thenForAllDaysValueByDefault();
|
||||
}
|
||||
|
||||
private void thenForAllDaysValueByDefault() {
|
||||
assertTrue(calendarFixture.isDefaultMonday());
|
||||
assertTrue(calendarFixture.isDefaultTuesday());
|
||||
assertTrue(calendarFixture.isDefaultWednesday());
|
||||
assertTrue(calendarFixture.isDefaultThursday());
|
||||
assertTrue(calendarFixture.isDefaultFriday());
|
||||
assertTrue(calendarFixture.isDefaultSaturday());
|
||||
assertTrue(calendarFixture.isDefaultSunday());
|
||||
}
|
||||
|
||||
@Test(expected = ValidationException.class)
|
||||
public void testDefaultValues() throws ValidationException {
|
||||
BaseCalendar calendar = createBasicCalendar();
|
||||
|
||||
assertFalse(calendar.isDefaultMonday());
|
||||
|
||||
calendar.setDefaultMonday();
|
||||
assertTrue(calendar.isDefaultMonday());
|
||||
|
||||
calendar.checkValid();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsDerivedCalendar() {
|
||||
BaseCalendar calendar = BaseCalendar.create();
|
||||
BaseCalendar derivedCalendar = calendar.newDerivedCalendar();
|
||||
|
||||
assertFalse(calendar.isDerived());
|
||||
assertTrue(derivedCalendar.isDerived());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetExceptionDay() {
|
||||
BaseCalendar calendar = createChristmasCalendar();
|
||||
BaseCalendar derived = calendar.newDerivedCalendar();
|
||||
|
||||
assertThat(calendar.getExceptionDay(CHRISTMAS_DAY_LOCAL_DATE),
|
||||
notNullValue());
|
||||
assertThat(derived.getExceptionDay(CHRISTMAS_DAY_LOCAL_DATE),
|
||||
notNullValue());
|
||||
|
||||
assertThat(calendar.getOwnExceptionDay(CHRISTMAS_DAY_LOCAL_DATE),
|
||||
notNullValue());
|
||||
assertThat(derived.getOwnExceptionDay(CHRISTMAS_DAY_LOCAL_DATE),
|
||||
nullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetType() {
|
||||
BaseCalendar calendar = createChristmasCalendar();
|
||||
|
||||
assertThat(calendar.getType(MONDAY_LOCAL_DATE), equalTo(DayType.NORMAL));
|
||||
assertThat(calendar.getType(SUNDAY_LOCAL_DATE),
|
||||
equalTo(DayType.ZERO_HOURS));
|
||||
assertThat(calendar.getType(CHRISTMAS_DAY_LOCAL_DATE),
|
||||
equalTo(DayType.OWN_EXCEPTION));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetTypeDerivedCalendar() {
|
||||
BaseCalendar calendar = createChristmasCalendar();
|
||||
BaseCalendar derived = calendar.newDerivedCalendar();
|
||||
|
||||
assertThat(derived.getType(MONDAY_LOCAL_DATE), equalTo(DayType.NORMAL));
|
||||
assertThat(derived.getType(SUNDAY_LOCAL_DATE), equalTo(DayType.ZERO_HOURS));
|
||||
assertThat(derived.getType(CHRISTMAS_DAY_LOCAL_DATE),
|
||||
equalTo(DayType.ANCESTOR_EXCEPTION));
|
||||
|
||||
assertThat(calendar.getType(CHRISTMAS_DAY_LOCAL_DATE),
|
||||
equalTo(DayType.OWN_EXCEPTION));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue