private static WorkReportLine toEntity(WorkReportLineDTO workReportLineDTO,
WorkReport workReport)
throws InstanceNotFoundException {
WorkReportLine workReportLine = WorkReportLine.create(workReport);
// Mandatory fields
workReportLine.setCode(workReportLineDTO.code);
workReportLine.setEffort(EffortDuration
.parseFromFormattedString(workReportLineDTO.numHours));
if (workReportLineDTO.typeOfWorkHours != null) {
try {
TypeOfWorkHours typeOfWorkHours = Registry
.getTypeOfWorkHoursDAO().findUniqueByCode(
workReportLineDTO.typeOfWorkHours);
workReportLine.setTypeOfWorkHours(typeOfWorkHours);
} catch (InstanceNotFoundException e) {
throw new ValidationException(
"There is no type of work hours with this code");
}
}
// Optional fields
if (workReportLineDTO.date != null) {
workReportLine
.setDate(DateConverter.toDate(workReportLineDTO.date));
}
bindingStrategy.assignOrderElementsToWorkReportLine(workReportLine,
bindingStrategy.getOrderElementsBound(workReportLineDTO));
if (workReportLineDTO.resource != null) {
try {
Resource resource = Registry.getResourceDAO().findByCode(
workReportLineDTO.resource);
workReportLine.setResource(resource);
} catch (InstanceNotFoundException e) {
workReportLine.setResource(null);
throw new ValidationException(
"There is no resource with this code");
}
}
if (workReportLineDTO.clockStart != null) {
workReportLine.setClockStart(DateConverter
.toLocalTime(workReportLineDTO.clockStart));
}
if (workReportLineDTO.clockFinish != null) {
workReportLine.setClockFinish(DateConverter
.toLocalTime(workReportLineDTO.clockFinish));
}
if (workReportLineDTO.labels != null
&& !workReportLineDTO.labels.isEmpty()) {
workReportLine.setLabels(LabelReferenceConverter
.toEntity(workReportLineDTO.labels));
}
if (workReportLineDTO.descriptionValues != null) {
workReportLine
.setDescriptionValues(toEntity(workReportLineDTO.descriptionValues));
}
return workReportLine;
}