ItEr39S05ValidacionEProbasFuncionaisItEr38S05 : it avoids adding duplicated description values in the work report.

It avoids adding duplicated description values by not allowing being a field of a value object null.
This commit is contained in:
Susana Montes Pedreira 2009-12-16 22:12:02 +01:00 committed by Javier Moran Rua
parent df91f31f60
commit 7f9432d4e5
6 changed files with 148 additions and 4 deletions

View file

@ -149,6 +149,7 @@ public class WorkReport extends BaseEntity {
workReportLines.remove(workReportLine);
}
@Valid
public Set<DescriptionValue> getDescriptionValues() {
return Collections.unmodifiableSet(descriptionValues);
}

View file

@ -26,6 +26,7 @@ import java.util.Set;
import org.hibernate.validator.AssertTrue;
import org.hibernate.validator.NotNull;
import org.hibernate.validator.Valid;
import org.navalplanner.business.common.BaseEntity;
import org.navalplanner.business.costcategories.entities.TypeOfWorkHours;
import org.navalplanner.business.labels.entities.Label;
@ -172,6 +173,7 @@ public class WorkReportLine extends BaseEntity implements Comparable {
updatesAllSharedDataByLines();
}
@Valid
public Set<DescriptionValue> getDescriptionValues() {
return descriptionValues;
}

View file

@ -20,6 +20,7 @@
package org.navalplanner.business.workreports.valueobjects;
import org.hibernate.validator.NotEmpty;
import org.navalplanner.business.INewObject;
/**
@ -65,6 +66,7 @@ public class DescriptionValue implements INewObject {
this.newObject = newObject;
}
@NotEmpty(message = "field name not specified or empty")
public String getFieldName() {
return fieldName;
}
@ -73,6 +75,7 @@ public class DescriptionValue implements INewObject {
this.fieldName = fieldName;
}
@NotEmpty(message = "value not specified or empty")
public String getValue() {
return value;
}

View file

@ -196,4 +196,19 @@ public interface IWorkReportModel {
* @return
*/
Integer getLength(DescriptionValue descriptionValue);
/**
* Check if the {@link DescriptionValue} value assigned to current
* {@link WorkReport} is null or empty
* @return
*/
DescriptionValue validateWorkReportDescriptionValues();
/**
* Check if the {@link DescriptionValue} value assigned to
* {@link WorkReportLine} is null or empty
* @return
*/
DescriptionValue validateWorkReportLineDescriptionValues(WorkReportLine line);
}

View file

@ -68,6 +68,7 @@ import org.zkoss.zul.Intbox;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Row;
import org.zkoss.zul.RowRenderer;
import org.zkoss.zul.Rows;
import org.zkoss.zul.SimpleListModel;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Timebox;
@ -201,17 +202,23 @@ public class WorkReportCRUDController extends GenericForwardComposer implements
private void showInvalidValues(ValidationException e) {
for (InvalidValue invalidValue : e.getInvalidValues()) {
Object value = invalidValue.getBean();
if (value instanceof WorkReport) {
validateWorkReport();
}
if (value instanceof WorkReportLine) {
validateWorkReportLine((WorkReportLine) invalidValue.getBean());
}
if (value instanceof DescriptionValue) {
validateDescriptionValue((DescriptionValue) value);
}
}
}
private void showInvalidProperty() {
if (getWorkReport() != null) {
if (!validateWorkReport())
return;
for (WorkReportLine workReportLine : getWorkReport()
.getWorkReportLines()) {
if (!validateWorkReportLine(workReportLine))
@ -219,6 +226,26 @@ public class WorkReportCRUDController extends GenericForwardComposer implements
}
}
}
/**
* Validates {@link DescriptionValue} data constraints
* @param invalidValue
*/
private void validateDescriptionValue(DescriptionValue value) {
if ((getWorkReport() != null)
&& (getWorkReport().getDescriptionValues().contains(value))) {
showInvalidWorkReportDescriptionValue(value);
} else {
for (WorkReportLine line : getWorkReport().getWorkReportLines()) {
if (line.getDescriptionValues().contains(value)) {
Row row = findWorkReportLine(listWorkReportLines.getRows()
.getChildren(), line);
showInvalidWorkReportLineDescriptionValue(row, value);
}
}
}
}
/**
* Validates {@link WorkReport} data constraints
* @param invalidValue
@ -245,9 +272,66 @@ public class WorkReportCRUDController extends GenericForwardComposer implements
_("Order Element code cannot be null"));
return false;
}
return validateWorkReportDescriptionValues();
}
private boolean validateWorkReportDescriptionValues() {
DescriptionValue descriptionValue = workReportModel
.validateWorkReportDescriptionValues();
if (descriptionValue != null) {
showInvalidWorkReportDescriptionValue(descriptionValue);
return false;
}
return true;
}
private void showInvalidWorkReportDescriptionValue(DescriptionValue value) {
// Find which row contains the description value inside grid
Row row = findRowByWorkReportDescriptionValue(headingFieldsAndLabels
.getRows(), value);
Textbox textboxValue = (Textbox) row.getChildren().get(1);
throw new WrongValueException(textboxValue,
_("The description field valuemust be not null and not empty"));
}
private void showInvalidWorkReportLineDescriptionValue(Row row,
DescriptionValue descriptionValue) {
Integer position = findPositionByWorkReportLineDescriptionValue(descriptionValue);
if ((row != null) && (position != null)) {
Textbox textboxValue = getTextboxInPosition(row, position);
String message = _("The value must be spicified.");
showInvalidMessage(textboxValue, message);
}
}
private Row findRowByWorkReportDescriptionValue(Rows rows,
DescriptionValue value) {
List<Row> listRows = (List<Row>) rows.getChildren();
for (Row row : listRows) {
if ((row.getValue() instanceof DescriptionValue)
&& (value.equals(row.getValue()))) {
return row;
}
}
return null;
}
private Integer findPositionByWorkReportLineDescriptionValue(
DescriptionValue value) {
Columns columns = this.listWorkReportLines.getColumns();
List<Component> listColumns = columns.getChildren();
Integer position = new Integer(0);
for(Component child : listColumns){
if ((child instanceof NewDataSortableColumn)
&& (((NewDataSortableColumn) child).getLabel().equals(value
.getFieldName()))) {
return position;
}
position++;
}
return null;
}
/**
* Validates {@link WorkReportLine} data constraints
*
@ -327,6 +411,14 @@ public class WorkReportCRUDController extends GenericForwardComposer implements
showInvalidMessage(autoTypeOfHours,message);
return false;
}
DescriptionValue descriptionValue = workReportModel
.validateWorkReportLineDescriptionValues(workReportLine);
if (descriptionValue != null) {
showInvalidWorkReportLineDescriptionValue(row,
descriptionValue);
return false;
}
}
}
return true;
@ -433,6 +525,15 @@ public class WorkReportCRUDController extends GenericForwardComposer implements
return (Textbox) row.getChildren().get(position);
}
/**
* Locates {@link Textbox} in {@link Row} in the specified position
* @param row
* @return
*/
private Textbox getTextboxInPosition(Row row, Integer position) {
return (Textbox) row.getChildren().get(position);
}
@Override
public void goToList() {
getVisibility().showOnly(listWindow);
@ -620,7 +721,6 @@ public class WorkReportCRUDController extends GenericForwardComposer implements
public void addWorkReportLine() {
WorkReportLine workReportLine = workReportModel.addWorkReportLine();
reloadWorkReportLines();
// listWorkReportLines.getRows().appendChild(createWorkReportLine(workReportLine));
}
private void removeWorkReportLine(WorkReportLine workReportLine) {
@ -741,7 +841,7 @@ public class WorkReportCRUDController extends GenericForwardComposer implements
@Override
public void set(String value) {
if (value.length() > 0) {
if (!value.isEmpty()) {
try {
workReportLine.setOrderElement(workReportModel
.findOrderElement(value));
@ -1005,9 +1105,8 @@ public class WorkReportCRUDController extends GenericForwardComposer implements
/**
* RowRenderer for a @{WorkReportLine} element
*
* @author Diego Pino García <dpino@igalia.com>
*
* @author Susana Montes Pedreira <smontes@wirelessgalicia.com>
*/
public class WorkReportListRenderer implements RowRenderer {

View file

@ -448,4 +448,28 @@ public class WorkReportModel implements IWorkReportModel {
return descriptionField.getLength();
}
@Override
public DescriptionValue validateWorkReportDescriptionValues(){
for (DescriptionValue descriptionValue : getWorkReport()
.getDescriptionValues()) {
if ((descriptionValue.getValue() == null)
|| (descriptionValue.getValue().isEmpty())) {
return descriptionValue;
}
}
return null;
}
@Override
public DescriptionValue validateWorkReportLineDescriptionValues(
WorkReportLine line) {
for (DescriptionValue descriptionValue : line.getDescriptionValues()) {
if ((descriptionValue.getValue() == null)
|| (descriptionValue.getValue().isEmpty())) {
return descriptionValue;
}
}
return null;
}
}