ItEr19S10CUIntroducionPartesTraballoManualmenteItEr18S11: [Refactoring] Validate contraints for WorkReport and WorkReportLine on saving WorkReport
This commit is contained in:
parent
2925e6038e
commit
15421e0e9e
6 changed files with 230 additions and 46 deletions
|
|
@ -4,6 +4,9 @@ import java.util.Date;
|
|||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.hibernate.validator.NotEmpty;
|
||||
import org.hibernate.validator.NotNull;
|
||||
|
||||
/**
|
||||
* @author Diego Pino García <dpino@igalia.com>
|
||||
*/
|
||||
|
|
@ -14,16 +17,22 @@ public class WorkReport {
|
|||
@SuppressWarnings("unused")
|
||||
private long version;
|
||||
|
||||
@NotNull
|
||||
Date date;
|
||||
|
||||
String place;
|
||||
|
||||
@NotEmpty
|
||||
String responsible;
|
||||
|
||||
WorkReportType workReportType;
|
||||
|
||||
Set<WorkReportLine> workReportLines = new HashSet<WorkReportLine>();
|
||||
|
||||
public static final String DATE = "date";
|
||||
|
||||
public static final String RESPONSIBLE = "responsible";
|
||||
|
||||
public WorkReport() {
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package org.navalplanner.business.workreports.entities;
|
|||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.hibernate.validator.NotNull;
|
||||
import org.navalplanner.business.orders.entities.OrderElement;
|
||||
import org.navalplanner.business.resources.entities.Criterion;
|
||||
import org.navalplanner.business.resources.entities.Resource;
|
||||
|
|
@ -20,14 +21,20 @@ public class WorkReportLine {
|
|||
|
||||
Integer numHours;
|
||||
|
||||
@NotNull
|
||||
Resource resource;
|
||||
|
||||
@NotNull
|
||||
OrderElement orderElement;
|
||||
|
||||
WorkReport workReport;
|
||||
|
||||
Set<Criterion> criterions = new HashSet<Criterion>();
|
||||
|
||||
public static final String RESOURCE = "resource";
|
||||
|
||||
public static final String ORDER_ELEMENT = "orderElement";
|
||||
|
||||
public WorkReportLine() {
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,8 +5,14 @@ import java.util.HashSet;
|
|||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.navalplanner.business.orders.daos.OrderElementDao;
|
||||
import org.navalplanner.business.orders.entities.OrderElement;
|
||||
import org.navalplanner.business.orders.entities.OrderLine;
|
||||
import org.navalplanner.business.resources.daos.impl.CriterionTypeDAO;
|
||||
import org.navalplanner.business.resources.daos.impl.ResourceDaoHibernate;
|
||||
import org.navalplanner.business.resources.entities.CriterionType;
|
||||
import org.navalplanner.business.resources.entities.Resource;
|
||||
import org.navalplanner.business.resources.entities.Worker;
|
||||
import org.navalplanner.business.workreports.daos.WorkReportTypeDAO;
|
||||
import org.navalplanner.business.workreports.entities.WorkReport;
|
||||
import org.navalplanner.business.workreports.entities.WorkReportLine;
|
||||
|
|
@ -15,56 +21,83 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||
|
||||
public abstract class AbstractWorkReportTest {
|
||||
|
||||
@Autowired
|
||||
CriterionTypeDAO criterionTypeDAO;
|
||||
@Autowired
|
||||
CriterionTypeDAO criterionTypeDAO;
|
||||
|
||||
@Autowired
|
||||
WorkReportTypeDAO workReportTypeDAO;
|
||||
@Autowired
|
||||
WorkReportTypeDAO workReportTypeDAO;
|
||||
|
||||
// Create a Set of CriterionType
|
||||
public Set<CriterionType> createValidCriterionTypes() {
|
||||
Set<CriterionType> criterionTypes = new HashSet<CriterionType>();
|
||||
@Autowired
|
||||
ResourceDaoHibernate resourceDAO;
|
||||
|
||||
CriterionType criterionType = new CriterionType(UUID.randomUUID()
|
||||
.toString());
|
||||
criterionTypeDAO.save(criterionType);
|
||||
criterionTypes.add(criterionType);
|
||||
@Autowired
|
||||
OrderElementDao orderElementDAO;
|
||||
|
||||
return criterionTypes;
|
||||
}
|
||||
// Create a Set of CriterionType
|
||||
public Set<CriterionType> createValidCriterionTypes() {
|
||||
Set<CriterionType> criterionTypes = new HashSet<CriterionType>();
|
||||
|
||||
public WorkReportType createValidWorkReportType() {
|
||||
Set<CriterionType> criterionTypes = createValidCriterionTypes();
|
||||
return new WorkReportType(UUID.randomUUID().toString(), criterionTypes);
|
||||
}
|
||||
CriterionType criterionType = new CriterionType(UUID.randomUUID()
|
||||
.toString());
|
||||
criterionTypeDAO.save(criterionType);
|
||||
criterionTypes.add(criterionType);
|
||||
|
||||
public WorkReportLine createValidWorkReportLine() {
|
||||
WorkReportLine workReportLine = new WorkReportLine();
|
||||
workReportLine.setNumHours(100);
|
||||
return workReportLine;
|
||||
}
|
||||
return criterionTypes;
|
||||
}
|
||||
|
||||
public Set<WorkReportLine> createValidWorkReportLines() {
|
||||
Set<WorkReportLine> workReportLines = new HashSet<WorkReportLine>();
|
||||
public WorkReportType createValidWorkReportType() {
|
||||
Set<CriterionType> criterionTypes = createValidCriterionTypes();
|
||||
return new WorkReportType(UUID.randomUUID().toString(), criterionTypes);
|
||||
}
|
||||
|
||||
WorkReportLine workReportLine = createValidWorkReportLine();
|
||||
workReportLines.add(workReportLine);
|
||||
public WorkReportLine createValidWorkReportLine() {
|
||||
WorkReportLine workReportLine = new WorkReportLine();
|
||||
workReportLine.setNumHours(100);
|
||||
workReportLine.setResource(createValidWorker());
|
||||
workReportLine.setOrderElement(createValidOrderElement());
|
||||
return workReportLine;
|
||||
}
|
||||
|
||||
return workReportLines;
|
||||
}
|
||||
private Resource createValidWorker() {
|
||||
Worker worker = new Worker();
|
||||
worker.setFirstName(UUID.randomUUID().toString());
|
||||
worker.setSurname(UUID.randomUUID().toString());
|
||||
worker.setNif(UUID.randomUUID().toString());
|
||||
worker.setDailyHours(10);
|
||||
resourceDAO.save(worker);
|
||||
return worker;
|
||||
}
|
||||
|
||||
public WorkReport createValidWorkReport() {
|
||||
WorkReport workReport = new WorkReport();
|
||||
private OrderElement createValidOrderElement() {
|
||||
OrderLine orderLine = new OrderLine();
|
||||
orderLine.setName(UUID.randomUUID().toString());
|
||||
orderLine.setCode(UUID.randomUUID().toString());
|
||||
orderElementDAO.save(orderLine);
|
||||
return orderLine;
|
||||
}
|
||||
|
||||
workReport.setDate(new Date());
|
||||
workReport.setPlace(UUID.randomUUID().toString());
|
||||
public Set<WorkReportLine> createValidWorkReportLines() {
|
||||
Set<WorkReportLine> workReportLines = new HashSet<WorkReportLine>();
|
||||
|
||||
WorkReportType workReportType = createValidWorkReportType();
|
||||
workReportTypeDAO.save(workReportType);
|
||||
workReport.setWorkReportType(workReportType);
|
||||
WorkReportLine workReportLine = createValidWorkReportLine();
|
||||
workReportLines.add(workReportLine);
|
||||
|
||||
workReport.setWorkReportLines(new HashSet<WorkReportLine>());
|
||||
return workReportLines;
|
||||
}
|
||||
|
||||
return workReport;
|
||||
}
|
||||
public WorkReport createValidWorkReport() {
|
||||
WorkReport workReport = new WorkReport();
|
||||
|
||||
workReport.setDate(new Date());
|
||||
workReport.setPlace(UUID.randomUUID().toString());
|
||||
workReport.setResponsible(UUID.randomUUID().toString());
|
||||
|
||||
WorkReportType workReportType = createValidWorkReportType();
|
||||
workReportTypeDAO.save(workReportType);
|
||||
workReport.setWorkReportType(workReportType);
|
||||
|
||||
workReport.setWorkReportLines(new HashSet<WorkReportLine>());
|
||||
|
||||
return workReport;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import java.util.HashSet;
|
|||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.hibernate.validator.InvalidValue;
|
||||
import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
|
||||
import org.navalplanner.business.common.exceptions.ValidationException;
|
||||
import org.navalplanner.business.orders.entities.Order;
|
||||
|
|
@ -28,6 +29,7 @@ import org.zkoss.zk.ui.WrongValueException;
|
|||
import org.zkoss.zk.ui.event.Event;
|
||||
import org.zkoss.zk.ui.event.EventListener;
|
||||
import org.zkoss.zk.ui.util.GenericForwardComposer;
|
||||
import org.zkoss.zul.Datebox;
|
||||
import org.zkoss.zul.Intbox;
|
||||
import org.zkoss.zul.Listbox;
|
||||
import org.zkoss.zul.Listcell;
|
||||
|
|
@ -64,6 +66,8 @@ public class WorkReportCRUDController extends GenericForwardComposer implements
|
|||
|
||||
private WorkReportListRenderer workReportListRenderer = new WorkReportListRenderer();
|
||||
|
||||
public final static String ID_WORK_REPORT_LINES = "workReportLines";
|
||||
|
||||
public List<WorkReport> getWorkReports() {
|
||||
return workReportModel.getWorkReports();
|
||||
}
|
||||
|
|
@ -92,10 +96,123 @@ public class WorkReportCRUDController extends GenericForwardComposer implements
|
|||
messagesForUser.showMessage(Level.ERROR,
|
||||
"Parte de traballo gardado");
|
||||
} catch (ValidationException e) {
|
||||
messagesForUser.showInvalidValues(e);
|
||||
showInvalidValues(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows invalid values for {@link WorkReport} and {@link WorkReportLine}
|
||||
* entities
|
||||
*
|
||||
* @param e
|
||||
*/
|
||||
private void showInvalidValues(ValidationException e) {
|
||||
for (InvalidValue invalidValue : e.getInvalidValues()) {
|
||||
Object value = invalidValue.getBean();
|
||||
if (value instanceof WorkReport) {
|
||||
validateWorkReport(invalidValue);
|
||||
}
|
||||
if (value instanceof WorkReportLine) {
|
||||
validateWorkReportLine(invalidValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates {@link WorkReport} data constraints
|
||||
*
|
||||
* @param invalidValue
|
||||
*/
|
||||
private void validateWorkReport(InvalidValue invalidValue) {
|
||||
String propertyName = invalidValue.getPropertyName();
|
||||
|
||||
if (WorkReport.DATE.equals(propertyName)) {
|
||||
Datebox datebox = (Datebox) createWindow.getFellowIfAny(propertyName);
|
||||
throw new WrongValueException(datebox, "Data non pode ser nulo");
|
||||
}
|
||||
if (WorkReport.RESPONSIBLE.equals(propertyName)) {
|
||||
Textbox textbox = (Textbox) createWindow.getFellowIfAny(propertyName);
|
||||
throw new WrongValueException(textbox,
|
||||
"Responsable non poder ser nulo");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates {@link WorkReportLine} data constraints
|
||||
*
|
||||
* @param invalidValue
|
||||
*/
|
||||
private void validateWorkReportLine(InvalidValue invalidValue) {
|
||||
Listbox listBox = (Listbox) createWindow
|
||||
.getFellowIfAny(ID_WORK_REPORT_LINES);
|
||||
if (listBox != null) {
|
||||
// Find which listItem contains workReportLine inside listBox
|
||||
Listitem listItem = findWorkReportLine(listBox.getItems(),
|
||||
(WorkReportLine) invalidValue.getBean());
|
||||
|
||||
if (listItem != null) {
|
||||
String propertyName = invalidValue.getPropertyName();
|
||||
|
||||
if (WorkReportLine.RESOURCE.equals(propertyName)) {
|
||||
// Locate TextboxResource
|
||||
Textbox txtResource = getTextboxResource(listItem);
|
||||
// Value is incorrect, clear
|
||||
txtResource.setValue("");
|
||||
throw new WrongValueException(txtResource,
|
||||
"Recurso non pode ser nulo");
|
||||
}
|
||||
if (WorkReportLine.ORDER_ELEMENT.equals(propertyName)) {
|
||||
// Locate TextboxOrder
|
||||
Textbox txtOrder = getTextboxOrder(listItem);
|
||||
// Value is incorrect, clear
|
||||
txtOrder.setValue("");
|
||||
throw new WrongValueException(txtOrder,
|
||||
"Código non pode ser nulo");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Locates which {@link Listitem} is bound to {@link WorkReportLine} in
|
||||
* listItems
|
||||
*
|
||||
* @param listItems
|
||||
* @param workReportLine
|
||||
* @return
|
||||
*/
|
||||
private Listitem findWorkReportLine(List<Listitem> listItems,
|
||||
WorkReportLine workReportLine) {
|
||||
for (Listitem listItem : listItems) {
|
||||
if (workReportLine.equals(listItem.getValue())) {
|
||||
return listItem;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Locates {@link Textbox} Resource in {@link Listitem}
|
||||
*
|
||||
* @param listItem
|
||||
* @return
|
||||
*/
|
||||
private Textbox getTextboxResource(Listitem listItem) {
|
||||
return (Textbox) ((Listcell) listItem.getChildren().get(0))
|
||||
.getChildren().get(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Locates {@link Textbox} Order in {@link Listitem}
|
||||
*
|
||||
* @param listItem
|
||||
* @return
|
||||
*/
|
||||
private Textbox getTextboxOrder(Listitem listItem) {
|
||||
return (Textbox) ((Listcell) listItem.getChildren().get(1))
|
||||
.getChildren().get(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void goToList() {
|
||||
getVisibility().showOnly(listWindow);
|
||||
|
|
@ -133,7 +250,7 @@ public class WorkReportCRUDController extends GenericForwardComposer implements
|
|||
* Appends a set of {@link CriterionType} to columns header
|
||||
*/
|
||||
private void appendCriterionTypesToHeader(Set<CriterionType> criterionTypes) {
|
||||
Listbox grid = (Listbox) createWindow.getFellow("listWorkReportLines");
|
||||
Listbox grid = (Listbox) createWindow.getFellow(ID_WORK_REPORT_LINES);
|
||||
for (CriterionType criterionType : criterionTypes) {
|
||||
appendCriterionTypeToColumns(criterionType, grid.getListhead());
|
||||
}
|
||||
|
|
|
|||
|
|
@ -39,6 +39,9 @@ public class WorkReportModel implements IWorkReportModel {
|
|||
private ClassValidator<WorkReport> workReportValidator = new ClassValidator<WorkReport>(
|
||||
WorkReport.class);
|
||||
|
||||
private ClassValidator<WorkReportLine> workReportLineValidator = new ClassValidator<WorkReportLine>(
|
||||
WorkReportLine.class);
|
||||
|
||||
@Autowired
|
||||
private WorkReportDAO workReportDAO;
|
||||
|
||||
|
|
@ -124,9 +127,20 @@ public class WorkReportModel implements IWorkReportModel {
|
|||
public void save() throws ValidationException {
|
||||
InvalidValue[] invalidValues = workReportValidator
|
||||
.getInvalidValues(workReport);
|
||||
|
||||
if (invalidValues.length > 0) {
|
||||
throw new ValidationException(invalidValues);
|
||||
}
|
||||
|
||||
// Check WorkReportLines
|
||||
for (WorkReportLine workReportLine : workReport.getWorkReportLines()) {
|
||||
invalidValues = workReportLineValidator
|
||||
.getInvalidValues(workReportLine);
|
||||
|
||||
if (invalidValues.length > 0) {
|
||||
throw new ValidationException(invalidValues);
|
||||
}
|
||||
}
|
||||
workReportDAO.save(workReport);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -8,12 +8,16 @@
|
|||
<row>
|
||||
<label value="Responsable:"
|
||||
style="font-weight:bold;" />
|
||||
<textbox
|
||||
<textbox id="responsible"
|
||||
value="@{controller.workReport.responsible}" />
|
||||
</row>
|
||||
<row>
|
||||
<label value="Fecha:" style="font-weight:bold;" />
|
||||
<datebox value="@{controller.workReport.date}" />
|
||||
<label value="Lugar:" style="font-weight:bold;" />
|
||||
<textbox value="@{controller.workReport.place}" />
|
||||
</row>
|
||||
<row>
|
||||
<label value="Data:" style="font-weight:bold;" />
|
||||
<datebox id="date" value="@{controller.workReport.date}" />
|
||||
</row>
|
||||
</rows>
|
||||
</grid>
|
||||
|
|
@ -21,14 +25,14 @@
|
|||
|
||||
<hbox>
|
||||
<button label="Añadir"
|
||||
onClick="controller.addWorkReportLine(listWorkReportLines);"
|
||||
onClick="controller.addWorkReportLine(workReportLines);"
|
||||
style="margin-top: -30px; margin-left: 300px" />
|
||||
<button label="Borrar"
|
||||
onClick="controller.deleteWorkReportLine(listWorkReportLines);"
|
||||
onClick="controller.deleteWorkReportLine(workReportLines);"
|
||||
style="margin-top: -30px; margin-left: 2px" />
|
||||
</hbox>
|
||||
|
||||
<listbox id="listWorkReportLines"
|
||||
<listbox id="workReportLines"
|
||||
model="@{controller.workReportLines}"
|
||||
itemRenderer="@{controller.renderer}" mold="paging"
|
||||
pageSize="10">
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue