ItEr60S04ValidacionEProbasFuncionaisItEr59S04 : Fixing bug in Calendar Service.
It uses the DateConverter class in the CalendarConverter class, and it shows the error messages when the importation is incorrect.
This commit is contained in:
parent
831f0450e8
commit
f43b3f1c80
4 changed files with 34 additions and 55 deletions
|
|
@ -211,6 +211,11 @@ public class BaseCalendar extends IntegrationEntity implements IWorkHours {
|
|||
|
||||
public void addExceptionDay(CalendarException day)
|
||||
throws IllegalArgumentException {
|
||||
|
||||
if (day.getDate() == null) {
|
||||
throw new IllegalArgumentException(
|
||||
"This exception day has a incorrect date");
|
||||
}
|
||||
if (isExceptionDayAlreadyInExceptions(day)) {
|
||||
throw new IllegalArgumentException(
|
||||
"This day is already in the exception days");
|
||||
|
|
@ -459,6 +464,7 @@ public class BaseCalendar extends IntegrationEntity implements IWorkHours {
|
|||
|
||||
if (version.getExpiringDate().toDateTimeAtStartOfDay().toDate()
|
||||
.compareTo(new Date()) <= 0) {
|
||||
|
||||
throw new IllegalArgumentException(
|
||||
"You can not add a version with previous date than current date");
|
||||
}
|
||||
|
|
@ -1007,7 +1013,7 @@ public class BaseCalendar extends IntegrationEntity implements IWorkHours {
|
|||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
@AssertTrue(message = "the versions: the dates should be sorted and could not overlap ")
|
||||
@AssertTrue(message = "the versions: the dates should be corrects and sorted and could not overlap ")
|
||||
public boolean checkConstraintDateCouldNotOverlap() {
|
||||
|
||||
if (calendarDataVersions == null || calendarDataVersions.isEmpty()) {
|
||||
|
|
|
|||
|
|
@ -25,16 +25,12 @@ import static org.navalplanner.web.I18nHelper._;
|
|||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.xml.datatype.DatatypeConfigurationException;
|
||||
import javax.xml.datatype.DatatypeConstants;
|
||||
import javax.xml.datatype.DatatypeFactory;
|
||||
import javax.xml.datatype.XMLGregorianCalendar;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
|
@ -51,6 +47,7 @@ import org.navalplanner.ws.calendars.api.BaseCalendarDTO;
|
|||
import org.navalplanner.ws.calendars.api.CalendarDataDTO;
|
||||
import org.navalplanner.ws.calendars.api.CalendarExceptionDTO;
|
||||
import org.navalplanner.ws.calendars.api.HoursPerDayDTO;
|
||||
import org.navalplanner.ws.common.impl.DateConverter;
|
||||
|
||||
/**
|
||||
* Converter from/to {@link BaseCalendar} related entities to/from DTOs.
|
||||
|
|
@ -84,14 +81,8 @@ public final class CalendarConverter {
|
|||
|
||||
private final static CalendarExceptionDTO toDTO(
|
||||
CalendarException calendarException) {
|
||||
|
||||
XMLGregorianCalendar date = null;
|
||||
try {
|
||||
date = toXMLGregorianCalendar(calendarException.getDate());
|
||||
} catch (DatatypeConfigurationException e) {
|
||||
throw new ValidationException(e.getMessage());
|
||||
}
|
||||
|
||||
XMLGregorianCalendar date = DateConverter
|
||||
.toXMLGregorianCalendar(calendarException.getDate());
|
||||
return new CalendarExceptionDTO(calendarException.getCode(), date,
|
||||
calendarException.getHours(), calendarException.getType()
|
||||
.getCode());
|
||||
|
|
@ -106,15 +97,10 @@ public final class CalendarConverter {
|
|||
hoursPerDayDTOs.add(new HoursPerDayDTO(dayName, hours));
|
||||
}
|
||||
|
||||
XMLGregorianCalendar expiringDate = null;
|
||||
try {
|
||||
expiringDate = (calendarData.getExpiringDate() != null) ? toXMLGregorianCalendar(calendarData
|
||||
XMLGregorianCalendar expiringDate = (calendarData.getExpiringDate() != null) ? DateConverter
|
||||
.toXMLGregorianCalendar(calendarData
|
||||
.getExpiringDate())
|
||||
: null;
|
||||
} catch (DatatypeConfigurationException e) {
|
||||
throw new ValidationException(e.getMessage());
|
||||
}
|
||||
|
||||
String parentCalendar = (calendarData.getParent() != null) ? calendarData
|
||||
.getParent().getCode()
|
||||
: null;
|
||||
|
|
@ -155,7 +141,7 @@ public final class CalendarConverter {
|
|||
|
||||
LocalDate date = null;
|
||||
if (calendarExceptionDTO.date != null) {
|
||||
date = toLocalDate(calendarExceptionDTO.date);
|
||||
date = DateConverter.toLocalDate(calendarExceptionDTO.date);
|
||||
}
|
||||
|
||||
CalendarExceptionType type = findCalendarExceptionType(calendarExceptionDTO.calendarExceptionTypeCode);
|
||||
|
|
@ -167,7 +153,8 @@ public final class CalendarConverter {
|
|||
public final static CalendarData toEntity(CalendarDataDTO calendarDataDTO) {
|
||||
LocalDate expiringDate = null;
|
||||
if (calendarDataDTO.expiringDate != null) {
|
||||
expiringDate = toLocalDate(calendarDataDTO.expiringDate);
|
||||
expiringDate = DateConverter
|
||||
.toLocalDate(calendarDataDTO.expiringDate);
|
||||
}
|
||||
|
||||
BaseCalendar parent = findBaseCalendarParent(calendarDataDTO.parentCalendar);
|
||||
|
|
@ -208,7 +195,8 @@ public final class CalendarConverter {
|
|||
} catch (InstanceNotFoundException e) {
|
||||
// find by date
|
||||
CalendarException exception = baseCalendar
|
||||
.getOwnExceptionDay(toLocalDate(exceptionDTO.date));
|
||||
.getOwnExceptionDay(DateConverter
|
||||
.toLocalDate(exceptionDTO.date));
|
||||
if (exception != null) {
|
||||
throw new ValidationException(
|
||||
_("exception date already exists"));
|
||||
|
|
@ -269,7 +257,7 @@ public final class CalendarConverter {
|
|||
|
||||
LocalDate date = null;
|
||||
if (calendarExceptionDTO.date != null) {
|
||||
date = toLocalDate(calendarExceptionDTO.date);
|
||||
date = DateConverter.toLocalDate(calendarExceptionDTO.date);
|
||||
}
|
||||
|
||||
CalendarExceptionType type = findCalendarExceptionType(calendarExceptionDTO.calendarExceptionTypeCode);
|
||||
|
|
@ -282,7 +270,8 @@ public final class CalendarConverter {
|
|||
|
||||
LocalDate expiringDate = null;
|
||||
if (calendarDataDTO.expiringDate != null) {
|
||||
expiringDate = toLocalDate(calendarDataDTO.expiringDate);
|
||||
expiringDate = DateConverter
|
||||
.toLocalDate(calendarDataDTO.expiringDate);
|
||||
}
|
||||
|
||||
BaseCalendar parent = findBaseCalendarParent(calendarDataDTO.parentCalendar);
|
||||
|
|
@ -303,12 +292,14 @@ public final class CalendarConverter {
|
|||
Map<Integer, Integer> hoursPerDays = new HashMap<Integer, Integer>();
|
||||
if (hoursPerDayDTOs != null) {
|
||||
for (HoursPerDayDTO hoursPerDayDTO : hoursPerDayDTOs) {
|
||||
Integer day = CalendarData.Days.valueOf(hoursPerDayDTO.day)
|
||||
try {
|
||||
Integer day = CalendarData.Days.valueOf(hoursPerDayDTO.day)
|
||||
.ordinal();
|
||||
if (day != null) {
|
||||
hoursPerDays.put(day, hoursPerDayDTO.hours);
|
||||
} else {
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new ValidationException(_("a day is not valid"));
|
||||
} catch(NullPointerException e){
|
||||
throw new ValidationException(_("a day is empty"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -361,21 +352,4 @@ public final class CalendarConverter {
|
|||
return versions;
|
||||
}
|
||||
|
||||
public static XMLGregorianCalendar toXMLGregorianCalendar(
|
||||
LocalDate localDate) throws DatatypeConfigurationException {
|
||||
DatatypeFactory factory = DatatypeFactory.newInstance();
|
||||
return factory.newXMLGregorianCalendarDate(localDate.getYear(),
|
||||
localDate.getMonthOfYear(), localDate.getDayOfMonth(),
|
||||
DatatypeConstants.FIELD_UNDEFINED);
|
||||
}
|
||||
|
||||
public static XMLGregorianCalendar toXMLGregorianCalendar(Date date)
|
||||
throws DatatypeConfigurationException {
|
||||
return toXMLGregorianCalendar(LocalDate.fromDateFields(date));
|
||||
}
|
||||
public static LocalDate toLocalDate(XMLGregorianCalendar calendar) {
|
||||
return new LocalDate(calendar.getYear(), calendar.getMonth(), calendar
|
||||
.getDay());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -85,11 +85,11 @@ public class DateConverter {
|
|||
*
|
||||
* @throws DatatypeConfigurationException
|
||||
*/
|
||||
public final static XMLGregorianCalendar toXMLGregorianCalendar(Date date) {
|
||||
if (date == null) {
|
||||
public final static XMLGregorianCalendar toXMLGregorianCalendar(
|
||||
LocalDate localDate) {
|
||||
if (localDate == null) {
|
||||
return null;
|
||||
} else {
|
||||
LocalDate localDate = LocalDate.fromDateFields(date);
|
||||
DatatypeFactory factory;
|
||||
try {
|
||||
factory = DatatypeFactory.newInstance();
|
||||
|
|
@ -102,4 +102,8 @@ public class DateConverter {
|
|||
}
|
||||
}
|
||||
|
||||
public static XMLGregorianCalendar toXMLGregorianCalendar(Date date) {
|
||||
return toXMLGregorianCalendar(LocalDate.fromDateFields(date));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,7 +32,6 @@ import java.util.Calendar;
|
|||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import javax.xml.datatype.DatatypeConfigurationException;
|
||||
import javax.xml.datatype.XMLGregorianCalendar;
|
||||
|
||||
import org.hibernate.SessionFactory;
|
||||
|
|
@ -58,8 +57,8 @@ import org.navalplanner.ws.calendars.api.CalendarDataDTO;
|
|||
import org.navalplanner.ws.calendars.api.CalendarExceptionDTO;
|
||||
import org.navalplanner.ws.calendars.api.HoursPerDayDTO;
|
||||
import org.navalplanner.ws.calendars.api.ICalendarService;
|
||||
import org.navalplanner.ws.calendars.impl.CalendarConverter;
|
||||
import org.navalplanner.ws.common.api.InstanceConstraintViolationsDTO;
|
||||
import org.navalplanner.ws.common.impl.DateConverter;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.annotation.Rollback;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
|
@ -167,11 +166,7 @@ public class BaseCalendarServiceTest {
|
|||
}
|
||||
|
||||
private XMLGregorianCalendar toXml(Date date) {
|
||||
try {
|
||||
return CalendarConverter.toXMLGregorianCalendar(date);
|
||||
} catch (DatatypeConfigurationException e) {
|
||||
return null;
|
||||
}
|
||||
return DateConverter.toXMLGregorianCalendar(date);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue