ConfigurationOrderElementConverter configuration)
throws ValidationException {
if (orderElementDTO instanceof OrderLineDTO) {
if (!(orderElement instanceof OrderLine)) {
throw new ValidationException(MessageFormat.format(
"Task {0}: Task group is incompatible type with {1}",
orderElement.getCode(), orderElement.getClass()
.getName()));
}
if (configuration.isHoursGroups()) {
for (HoursGroupDTO hoursGroupDTO : ((OrderLineDTO) orderElementDTO).hoursGroups) {
if (((OrderLine) orderElement)
.containsHoursGroup(hoursGroupDTO.code)) {
update(((OrderLine) orderElement)
.getHoursGroup(hoursGroupDTO.code),
hoursGroupDTO, configuration);
} else {
((OrderLine) orderElement)
.addHoursGroup(toEntity(
hoursGroupDTO, configuration));
}
}
}
} else { // orderElementDTO instanceof OrderLineGroupDTO
if (orderElementDTO instanceof OrderDTO) {
if (!(orderElement instanceof Order)) {
throw new ValidationException(MessageFormat.format(
"Task {0}: Project is incompatible type with {1}",
orderElement.getCode(), orderElement.getClass()
.getName()));
}
Order order = (Order) orderElement;
orderVersion = order.getOrderVersionFor(Registry
.getScenarioManager()
.getCurrent());
order.useSchedulingDataFor(orderVersion);
Boolean dependenciesConstraintsHavePriority = ((OrderDTO) orderElementDTO).dependenciesConstraintsHavePriority;
if (dependenciesConstraintsHavePriority != null) {
((Order) orderElement)
.setDependenciesConstraintsHavePriority(dependenciesConstraintsHavePriority);
}
String calendarName = ((OrderDTO) orderElementDTO).calendarName;
if (calendarName != null) {
if (!((Order) orderElement).getCalendar().getName().equals(
calendarName)) {
List<BaseCalendar> calendars = Registry
.getBaseCalendarDAO()
.findByName(
((OrderDTO) orderElementDTO).calendarName);
if (calendars.size() == 1) {
((Order) orderElement)
.setCalendar(calendars.get(0));
}
}
}
} else { // orderElementDTO instanceof OrderLineGroupDTO
if (!(orderElement instanceof OrderLineGroup)) {
throw new ValidationException(
MessageFormat
.format("Task {0}: Task group is incompatible type with {1}",
orderElement.getCode(),
orderElement.getClass().getName()));
}
}
for (OrderElementDTO childDTO : ((OrderLineGroupDTO) orderElementDTO).children) {
if (orderElement.containsOrderElement(childDTO.code)) {
update(orderVersion,
orderElement.getOrderElement(childDTO.code),
childDTO, configuration);
} else {
if (checkConstraintUniqueOrderCode(childDTO)) {
throw new ValidationException(
MessageFormat.format(
"Task {0}: Duplicate code in DB",
childDTO.code));
}
if (checkConstraintUniqueHoursGroupCode(childDTO)) {
throw new ValidationException(MessageFormat.format(
"Hours Group {0}: Duplicate code in DB",
childDTO.code));
}
((OrderLineGroup) orderElement).add(toEntity(orderVersion,
childDTO, configuration));
}
}
}
if (configuration.isLabels()) {
for (LabelReferenceDTO labelDTO : orderElementDTO.labels) {
if (!orderElement.containsLabel(labelDTO.code)) {
try {
orderElement.addLabel(LabelReferenceConverter
.toEntity(labelDTO));
} catch (InstanceNotFoundException e) {
throw new ValidationException("Label " + labelDTO.code
+ " not found");
} catch (IllegalArgumentException e) {
throw new ValidationException(e.getMessage());
}
}
}
}