ItEr38S05ValidacionEProbasFuncionaisItEr37S06: [Bug #119]. Fixing bug.
This commit is contained in:
parent
19a479e7ed
commit
fe4a8b0b82
3 changed files with 43 additions and 15 deletions
|
|
@ -20,12 +20,13 @@
|
|||
|
||||
package org.navalplanner.business.materials.entities;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
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;
|
||||
|
|
@ -38,6 +39,15 @@ import org.navalplanner.business.common.BaseEntity;
|
|||
*/
|
||||
public class MaterialCategory extends BaseEntity {
|
||||
|
||||
public static List<Material> getAllMaterialsFrom(
|
||||
Collection<? extends MaterialCategory> categories) {
|
||||
List<Material> result = new ArrayList<Material>();
|
||||
for (MaterialCategory each : categories) {
|
||||
result.addAll(each.getMaterials());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@NotEmpty
|
||||
private String name;
|
||||
|
||||
|
|
@ -104,16 +114,4 @@ public class MaterialCategory extends BaseEntity {
|
|||
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;
|
||||
// }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,7 +29,6 @@ import java.util.List;
|
|||
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.hibernate.validator.InvalidValue;
|
||||
import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
|
||||
import org.navalplanner.business.common.exceptions.ValidationException;
|
||||
import org.navalplanner.business.materials.entities.Material;
|
||||
import org.navalplanner.business.materials.entities.MaterialCategory;
|
||||
|
|
@ -326,6 +325,7 @@ public class MaterialsController extends
|
|||
}
|
||||
}
|
||||
}
|
||||
messagesForUser.showInvalidValues(validationException);
|
||||
}
|
||||
|
||||
private boolean locateAndSelectMaterialCategory(MaterialCategory materialCategory) {
|
||||
|
|
|
|||
|
|
@ -4,7 +4,9 @@ import static org.navalplanner.web.I18nHelper._;
|
|||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.commons.lang.Validate;
|
||||
|
|
@ -159,11 +161,39 @@ public class MaterialsModel implements IMaterialsModel {
|
|||
@Transactional
|
||||
public void confirmSave() throws ValidationException {
|
||||
final List<MaterialCategory> categories = materialCategories.asList();
|
||||
checkNoCodeRepeatedAtNewMaterials(categories);
|
||||
for (MaterialCategory each: categories) {
|
||||
categoryDAO.save(each);
|
||||
}
|
||||
}
|
||||
|
||||
private void checkNoCodeRepeatedAtNewMaterials(
|
||||
final List<MaterialCategory> categories) throws ValidationException {
|
||||
List<Material> allMaterials = MaterialCategory
|
||||
.getAllMaterialsFrom(categories);
|
||||
Map<String, Material> byCode = new HashMap<String, Material>();
|
||||
for (Material each : allMaterials) {
|
||||
if (byCode.containsKey(each.getCode())) {
|
||||
throw new ValidationException(sameCodeMessage(each, byCode
|
||||
.get(each.getCode())));
|
||||
}
|
||||
byCode.put(each.getCode(), each);
|
||||
}
|
||||
}
|
||||
|
||||
private String sameCodeMessage(Material first, Material second) {
|
||||
return _(
|
||||
"both {0} of category {1} and {2} of category {3} have the same code",
|
||||
asStringForUser(first), first.getCategory().getName(),
|
||||
asStringForUser(second), second.getCategory().getName());
|
||||
}
|
||||
|
||||
private String asStringForUser(Material material) {
|
||||
return String.format("{code: %s, description: %s}", material.getCode(),
|
||||
material
|
||||
.getDescription());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeMaterial(Material material) {
|
||||
material.getCategory().removeMaterial(material);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue