ItEr35S14CUAdministracionMateriaisItEr34S14: Check constraints and show WrongValueExceptions if error happens after saving
This commit is contained in:
parent
ff45f1cc41
commit
c93b7d52f5
4 changed files with 101 additions and 38 deletions
|
|
@ -34,6 +34,7 @@ import org.navalplanner.business.common.BaseEntity;
|
|||
*/
|
||||
public class Material extends BaseEntity {
|
||||
|
||||
@NotNull
|
||||
@NotEmpty
|
||||
private String code;
|
||||
|
||||
|
|
|
|||
|
|
@ -24,9 +24,10 @@ import java.util.Collections;
|
|||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.commons.collections.Unmodifiable;
|
||||
import org.apache.commons.lang.Validate;
|
||||
import org.hibernate.validator.AssertTrue;
|
||||
import org.hibernate.validator.NotEmpty;
|
||||
import org.hibernate.validator.Valid;
|
||||
import org.navalplanner.business.common.BaseEntity;
|
||||
|
||||
/**
|
||||
|
|
@ -42,8 +43,10 @@ public class MaterialCategory extends BaseEntity {
|
|||
|
||||
private MaterialCategory parent = null;
|
||||
|
||||
@Valid
|
||||
private Set<MaterialCategory> subcategories = new HashSet<MaterialCategory>();
|
||||
|
||||
@Valid
|
||||
private Set<Material> materials = new HashSet<Material>();
|
||||
|
||||
// Default constructor, needed by Hibernate
|
||||
|
|
@ -97,7 +100,21 @@ public class MaterialCategory extends BaseEntity {
|
|||
materials.add(material);
|
||||
material.setCategory(this);
|
||||
}
|
||||
|
||||
public void removeMaterial(Material material) {
|
||||
materials.remove(material);
|
||||
}
|
||||
|
||||
@AssertTrue(message="material code must be unique within a category")
|
||||
public boolean checkConstraintNonRepeatedMaterialCodes() {
|
||||
Set<String> materialCodes = new HashSet<String>();
|
||||
for (Material each: materials) {
|
||||
final String code = each.getCode();
|
||||
if (materialCodes.contains(code)) {
|
||||
return false;
|
||||
}
|
||||
materialCodes.add(code);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@ import static org.navalplanner.web.I18nHelper._;
|
|||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
|
@ -31,6 +32,7 @@ import org.hibernate.validator.InvalidValue;
|
|||
import org.navalplanner.business.common.exceptions.ValidationException;
|
||||
import org.navalplanner.business.materials.entities.Material;
|
||||
import org.navalplanner.business.materials.entities.MaterialCategory;
|
||||
import org.navalplanner.web.common.ConstraintChecker;
|
||||
import org.navalplanner.web.common.IMessagesForUser;
|
||||
import org.navalplanner.web.common.Level;
|
||||
import org.navalplanner.web.common.MessagesForUser;
|
||||
|
|
@ -211,6 +213,13 @@ public class MaterialsController extends
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds which element in categoryTree has the same name as {@link MaterialCategory},
|
||||
* and returns name {@link Textbox} component
|
||||
*
|
||||
* @param materialCategory
|
||||
* @return
|
||||
*/
|
||||
private Component findInMaterialCategoryTree(MaterialCategory materialCategory) {
|
||||
final Treechildren children = categoriesTree.getTreechildren();
|
||||
for(Treeitem each: (Collection<Treeitem>) children.getItems()) {
|
||||
|
|
@ -241,21 +250,64 @@ public class MaterialsController extends
|
|||
}
|
||||
|
||||
public void saveAndExit() {
|
||||
save();
|
||||
messagesForUser.showMessage(Level.INFO, _("Materials saved"));
|
||||
}
|
||||
|
||||
private void save() {
|
||||
validate();
|
||||
try {
|
||||
materialsModel.confirmSave();
|
||||
} catch (ValidationException e) {
|
||||
messagesForUser.showInvalidValues(e);
|
||||
if (save()) {
|
||||
messagesForUser.showMessage(Level.INFO, _("Materials saved"));
|
||||
}
|
||||
}
|
||||
|
||||
private void validate() {
|
||||
ConstraintChecker.
|
||||
private boolean save() {
|
||||
try {
|
||||
materialsModel.confirmSave();
|
||||
return true;
|
||||
} catch (ValidationException e) {
|
||||
showInvalidValues(e);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void showInvalidValues(ValidationException validationException) {
|
||||
final InvalidValue[] invalidValues = validationException.getInvalidValues();
|
||||
for (InvalidValue each: invalidValues) {
|
||||
if (each.getBean() instanceof Material) {
|
||||
final Material material = (Material) each.getBean();
|
||||
showConstraintErrorsFor(material.getCategory());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void showConstraintErrorsFor(MaterialCategory materialCategory) {
|
||||
Treeitem treeitem = findTreeItemByMaterialCategory(categoriesTree.getRoot(), materialCategory);
|
||||
if (treeitem != null) {
|
||||
treeitem.setSelected(true);
|
||||
|
||||
// Load materials for category
|
||||
final List<Material> materials = getMaterials(materialCategory);
|
||||
gridMaterials.setModel(new SimpleListModel(materials));
|
||||
gridMaterials.renderAll();
|
||||
|
||||
// Show errors
|
||||
ConstraintChecker.isValid(gridMaterials);
|
||||
}
|
||||
}
|
||||
|
||||
private Treeitem findTreeItemByMaterialCategory(Component node, MaterialCategory materialCategory) {
|
||||
if (node instanceof Treeitem) {
|
||||
final Treeitem treeitem = (Treeitem) node;
|
||||
final MaterialCategory _materialCategory = (MaterialCategory) treeitem.getValue();
|
||||
if (_materialCategory.equals(materialCategory)) {
|
||||
return treeitem;
|
||||
}
|
||||
}
|
||||
for (Iterator i = node.getChildren().iterator(); i.hasNext(); ) {
|
||||
Object obj = i.next();
|
||||
if (obj instanceof Component) {
|
||||
Treeitem treeitem = findTreeItemByMaterialCategory((Component) obj, materialCategory);
|
||||
if (treeitem != null) {
|
||||
return treeitem;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void saveAndContinue() {
|
||||
|
|
@ -279,10 +331,13 @@ public class MaterialsController extends
|
|||
private List<Material> getMaterials(Treeitem treeitem) {
|
||||
final List<Material> result = new ArrayList<Material>();
|
||||
if (treeitem != null) {
|
||||
final MaterialCategory materialCategory = (MaterialCategory) treeitem.getValue();
|
||||
result.addAll(materialsModel.getMaterials(materialCategory));
|
||||
result.addAll(getMaterials((MaterialCategory) treeitem.getValue()));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private List<Material> getMaterials(MaterialCategory materialCategory) {
|
||||
return materialsModel.getMaterials(materialCategory);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -48,7 +48,7 @@
|
|||
<button label="${i18n:_('Add')}"
|
||||
onClick="materialsController.addMaterialCategory()" />
|
||||
<tree id="categoriesTree"
|
||||
width="280px" rows="10" vflex="true" multiple="true"
|
||||
width="280px" rows="10" vflex="true" multiple="false"
|
||||
model="@{materialsController.materialCategories}"
|
||||
treeitemRenderer="@{materialsController.materialCategoryRenderer}"
|
||||
onSelect="materialsController.refreshMaterials()">
|
||||
|
|
@ -72,30 +72,20 @@
|
|||
fixedLayout="true"
|
||||
model="@{materialsController.materials}">
|
||||
<columns>
|
||||
<column
|
||||
label="${i18n:_('Code')}" />
|
||||
<column
|
||||
label="${i18n:_('Description')}" />
|
||||
<column
|
||||
label="${i18n:_('Unit price')}" />
|
||||
<column
|
||||
label="${i18n:_('Unit type')}" />
|
||||
<column
|
||||
label="${i18n:_('Disabled')}" />
|
||||
<column label="${i18n:_('Code')}" />
|
||||
<column label="${i18n:_('Description')}" />
|
||||
<column label="${i18n:_('Unit price')}" />
|
||||
<column label="${i18n:_('Unit type')}" />
|
||||
<column label="${i18n:_('Disabled')}" />
|
||||
</columns>
|
||||
<rows>
|
||||
<row
|
||||
self="@{each='material'}" value="@{material}">
|
||||
<textbox
|
||||
value="@{material.code}" constraint="no empty:${i18n:_('cannot be null or empty')}" />
|
||||
<textbox
|
||||
value="@{material.description}" />
|
||||
<intbox
|
||||
value="@{material.defaultUnitPrice}" constraint="no empty:${i18n:_('cannot be null or empty')}" />
|
||||
<textbox
|
||||
value="@{material.unitType}" constraint="no empty:${i18n:_('cannot be null or empty')}" />
|
||||
<checkbox
|
||||
checked="@{material.disabled}" />
|
||||
<row self="@{each='material'}" value="@{material}">
|
||||
<textbox id="code" value="@{material.code}"
|
||||
constraint="no empty:${i18n:_('cannot be null or empty')}" />
|
||||
<textbox value="@{material.description}" />
|
||||
<intbox value="@{material.defaultUnitPrice}" />
|
||||
<textbox value="@{material.unitType}" />
|
||||
<checkbox checked="@{material.disabled}" />
|
||||
</row>
|
||||
</rows>
|
||||
</grid>
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue