public final static void updateWorkReport(WorkReport workReport,
WorkReportDTO workReportDTO) throws ValidationException {
if (StringUtils.isBlank(workReportDTO.code)) {
throw new ValidationException("missing code in a work report.");
}
/*
* 1: Update the existing work report line or add new
* work report line.
*/
for (WorkReportLineDTO lineDTO : workReportDTO.workReportLines) {
/* Step 1.1: requires each work report line DTO to have a code. */
if (StringUtils.isBlank(lineDTO.code)) {
throw new ValidationException(
"missing code in a work report line");
}
try {
WorkReportLine line = workReport
.getWorkReportLineByCode(lineDTO.code);
updateWorkReportLine(line, lineDTO);
} catch (InstanceNotFoundException e) {
try {
workReport.addWorkReportLine(toEntity(lineDTO, workReport));
} catch (InstanceNotFoundException o) {
throw new ValidationException(
"missing type of work hours in a work report line");
}
}
}
/*
* 2: Update the existing labels
*/
if (workReportDTO.labels != null) {
for (LabelReferenceDTO labelDTO : workReportDTO.labels) {
/* Step 2.1: requires each label reference DTO to have a code. */
if (StringUtils.isBlank(labelDTO.code)) {
throw new ValidationException("missing code in a label");
}
try {
Set<Label> labels = workReport.getLabels();
updateLabel(labelDTO, labels);
} catch (InstanceNotFoundException e) {
throw new ValidationException(
"work report has not this label type assigned");
}
}
}
/*
* 3: Update the existing description values
*/
if (workReportDTO.descriptionValues != null) {
for (DescriptionValueDTO valueDTO : workReportDTO.descriptionValues) {
/* Step 3.1: requires each description value DTO to have a code. */
if (StringUtils.isBlank(valueDTO.fieldName)) {
throw new ValidationException(
"missing field name in a description value");
}
try {
DescriptionValue value = workReport
.getDescriptionValueByFieldName(valueDTO.fieldName);
value.setValue(StringUtils.trim(valueDTO.value));
} catch (InstanceNotFoundException e) {
throw new ValidationException(
"work report has not any description value with this field name");
}
}
}
/*
* 4: Update basic properties in existing work report
*/
/* Step 4.1: Update the date. */
Date date = DateConverter.toDate(workReportDTO.date);
workReport.setDate(date);
/* Step 4.2: Update the resource. */
String resourceCode = workReportDTO.resource;
if (!Strings.isBlank(resourceCode)) {
try {
Resource resource = Registry.getResourceDAO().findByCode(
resourceCode);
workReport.setResource(resource);
} catch (InstanceNotFoundException e) {
throw new ValidationException(
"There is no resource with this code");
}
}
/* Step 4.3: Update the order element. */
String orderElementCode = workReportDTO.orderElement;
if ((orderElementCode != null) && (!orderElementCode.isEmpty())) {
try {
OrderElement orderElement = Registry.getOrderElementDAO()
.findUniqueByCode(orderElementCode);
workReport.setOrderElement(orderElement);
} catch (InstanceNotFoundException e) {
throw new ValidationException("There is no task with this code");
}
}
}