From fce8818b414fbd7c7d32739b7bffbb69a09de375 Mon Sep 17 00:00:00 2001 From: Susana Montes Pedreira Date: Tue, 9 Mar 2010 13:00:31 +0100 Subject: [PATCH] ItEr50S13AdaptacionServiciosRESTItEr49S18 : Adds class validations. Adds the checks of the constraints about labels and description field values in WorkReport and WorkReportLine. --- .../workreports/entities/WorkReport.java | 82 ++++++++++++++++++ .../workreports/entities/WorkReportLine.java | 85 +++++++++++++++++++ 2 files changed, 167 insertions(+) diff --git a/navalplanner-business/src/main/java/org/navalplanner/business/workreports/entities/WorkReport.java b/navalplanner-business/src/main/java/org/navalplanner/business/workreports/entities/WorkReport.java index df28b4d43..fd9ba34f5 100644 --- a/navalplanner-business/src/main/java/org/navalplanner/business/workreports/entities/WorkReport.java +++ b/navalplanner-business/src/main/java/org/navalplanner/business/workreports/entities/WorkReport.java @@ -25,12 +25,15 @@ import java.util.Date; import java.util.HashSet; import java.util.Set; +import org.apache.commons.lang.StringUtils; import org.hibernate.validator.AssertTrue; import org.hibernate.validator.NotNull; import org.hibernate.validator.Valid; import org.navalplanner.business.common.IntegrationEntity; import org.navalplanner.business.common.Registry; +import org.navalplanner.business.common.exceptions.InstanceNotFoundException; import org.navalplanner.business.labels.entities.Label; +import org.navalplanner.business.labels.entities.LabelType; import org.navalplanner.business.orders.entities.OrderElement; import org.navalplanner.business.resources.entities.Resource; import org.navalplanner.business.workreports.daos.IWorkReportDAO; @@ -234,6 +237,85 @@ public class WorkReport extends IntegrationEntity { return true; } + @SuppressWarnings("unused") + @AssertTrue(message = "label type:the work report have not assigned this label type") + public boolean checkConstraintAssignedLabelTypes() { + if (this.workReportType == null) { + return true; + } + + if (this.workReportType.getHeadingLabels().size() != this.labels.size()) { + return false; + } + + for (WorkReportLabelTypeAssigment typeAssigment : this.workReportType + .getHeadingLabels()) { + try { + getLabelByType(typeAssigment.getLabelType()); + } catch (InstanceNotFoundException e) { + return false; + } + } + return true; + } + + @SuppressWarnings("unused") + @AssertTrue(message = "description value:the work report have not assigned the description field") + public boolean checkConstraintAssignedDescriptionValues() { + if (this.workReportType == null) { + return true; + } + + if (this.workReportType.getHeadingFields().size() != this.descriptionValues + .size()) { + return false; + } + + for(DescriptionField field : this.workReportType.getHeadingFields()){ + try{ + getDescriptionValueByFieldName(field.getFieldName()); + } + catch(InstanceNotFoundException e){ + return false; + } + } + return true; + } + + public DescriptionValue getDescriptionValueByFieldName(String fieldName) + throws InstanceNotFoundException { + + if (StringUtils.isBlank(fieldName)) { + throw new InstanceNotFoundException(fieldName, + DescriptionValue.class.getName()); + } + + for (DescriptionValue v : this.descriptionValues) { + if (v.getFieldName().equalsIgnoreCase(StringUtils.trim(fieldName))) { + return v; + } + } + + throw new InstanceNotFoundException(fieldName, DescriptionValue.class + .getName()); + } + + public Label getLabelByType(LabelType type) + throws InstanceNotFoundException { + + if (type == null) { + throw new InstanceNotFoundException(type, LabelType.class.getName()); + } + + for (Label l : this.labels) { + if (l.getType().getId().equals(type.getId())) { + return l; + } + } + + throw new InstanceNotFoundException(type, LabelType.class.getName()); + } + private void updateItsFieldsAndLabels(WorkReportType workReportType) { assignItsDescriptionValues(workReportType); assignItsLabels(workReportType); diff --git a/navalplanner-business/src/main/java/org/navalplanner/business/workreports/entities/WorkReportLine.java b/navalplanner-business/src/main/java/org/navalplanner/business/workreports/entities/WorkReportLine.java index a8979751d..a94651ba8 100644 --- a/navalplanner-business/src/main/java/org/navalplanner/business/workreports/entities/WorkReportLine.java +++ b/navalplanner-business/src/main/java/org/navalplanner/business/workreports/entities/WorkReportLine.java @@ -24,6 +24,7 @@ import java.util.Date; import java.util.HashSet; import java.util.Set; +import org.apache.commons.lang.StringUtils; import org.hibernate.validator.AssertTrue; import org.hibernate.validator.NotNull; import org.hibernate.validator.Valid; @@ -31,8 +32,10 @@ import org.joda.time.Hours; import org.joda.time.LocalTime; import org.navalplanner.business.common.IntegrationEntity; import org.navalplanner.business.common.Registry; +import org.navalplanner.business.common.exceptions.InstanceNotFoundException; import org.navalplanner.business.costcategories.entities.TypeOfWorkHours; import org.navalplanner.business.labels.entities.Label; +import org.navalplanner.business.labels.entities.LabelType; import org.navalplanner.business.orders.entities.OrderElement; import org.navalplanner.business.resources.entities.Resource; import org.navalplanner.business.workreports.daos.IWorkReportLineDAO; @@ -391,4 +394,86 @@ public class WorkReportLine extends IntegrationEntity implements Comparable { && (orderElement != null); } + @SuppressWarnings("unused") + @AssertTrue(message = "label type:the work report have not assigned this label type") + public boolean checkConstraintAssignedLabelTypes() { + if (this.workReport == null + || this.workReport.getWorkReportType() == null) { + return true; + } + + if (this.workReport.getWorkReportType().getLineLabels().size() != this.labels + .size()) { + return false; + } + + for (WorkReportLabelTypeAssigment typeAssigment : this.workReport + .getWorkReportType().getLineLabels()) { + try { + getLabelByType(typeAssigment.getLabelType()); + } catch (InstanceNotFoundException e) { + return false; + } + } + return true; + } + + @SuppressWarnings("unused") + @AssertTrue(message = "description value:the work report have not assigned the description field") + public boolean checkConstraintAssignedDescriptionValues() { + if (this.workReport == null + || this.workReport.getWorkReportType() == null) { + return true; + } + + if (this.workReport.getWorkReportType().getLineFields().size() != this.descriptionValues + .size()) { + return false; + } + + for (DescriptionField field : this.workReport.getWorkReportType() + .getLineFields()) { + try { + getDescriptionValueByFieldName(field.getFieldName()); + } catch (InstanceNotFoundException e) { + return false; + } + } + return true; + } + + public DescriptionValue getDescriptionValueByFieldName(String fieldName) + throws InstanceNotFoundException { + + if (StringUtils.isBlank(fieldName)) { + throw new InstanceNotFoundException(fieldName, + DescriptionValue.class.getName()); + } + + for (DescriptionValue v : this.descriptionValues) { + if (v.getFieldName().equalsIgnoreCase(StringUtils.trim(fieldName))) { + return v; + } + } + + throw new InstanceNotFoundException(fieldName, DescriptionValue.class + .getName()); + } + + public Label getLabelByType(LabelType type) + throws InstanceNotFoundException { + + if (type == null) { + throw new InstanceNotFoundException(type, LabelType.class.getName()); + } + + for (Label l : this.labels) { + if (l.getType().getId().equals(type.getId())) { + return l; + } + } + + throw new InstanceNotFoundException(type, LabelType.class.getName()); + } + }