private boolean equalsMaterialCategory(MaterialCategory obj1, MaterialCategory obj2) { String name1 = StringUtils.deleteWhitespace(obj1.getName() .toLowerCase()); String name2 = StringUtils.deleteWhitespace(obj2.getName() .toLowerCase()); return name1.equals(name2); }
@Override @Transactional public void confirmRemoveMaterialCategory(MaterialCategory materialCategory) { // Remove from list of material categories materialCategories.remove(materialCategory); // Remove from its parent final MaterialCategory parent = materialCategory.getParent(); if (parent != null) { materialCategory.getParent().removeSubcategory(materialCategory); } final Long idMaterialCategory = materialCategory.getId(); // It's not a yet-to-save element if (idMaterialCategory != null) { // It has a parent, in this case is enough with saving parent (all-delete-orphan) if (parent != null) { categoryDAO.save(materialCategory.getParent()); } else { // It was a root element, should be deleted from DB try { categoryDAO.remove(idMaterialCategory); } catch (InstanceNotFoundException e) { throw new RuntimeException(); } } reloadMaterialCategories(); } }
private void generateMaterialCodesIfIsNecessary(List<MaterialCategory> categories, Integer numberOfDigits) { for (MaterialCategory category: categories) { if (category.isCodeAutogenerated()) { category.generateMaterialCodes(numberOfDigits); } } }
@Override @Transactional(readOnly = true) public Collection<? extends Material> getMaterials() { List<Material> result = new ArrayList<Material>(); for (MaterialCategory each: materialCategories.asList()) { result.addAll(each.getMaterials()); } return result; }
private void storeOldCodes(MaterialCategory materialCategory) { // it stores temporaly the autogenerated code. if (materialCategory.isCodeAutogenerated()) { oldCodes.put(materialCategory, materialCategory.getCode()); for (Material child : materialCategory.getMaterials()) { oldMaterialCodes.put(child, child.getCode()); } } }
private void initializeMaterialCategories() { final List<MaterialCategory> categories = categoryDAO.getAllRootMaterialCategories(); for (MaterialCategory materialCategory: categories) { initializeMaterials(materialCategory.getMaterials()); materialCategories.addToRoot(materialCategory); addCategories(materialCategory, materialCategory.getSubcategories()); storeOldCodes(materialCategory); } }
private void addCategories(MaterialCategory materialCategory, Set<MaterialCategory> categories) { for (MaterialCategory category: categories) { initializeMaterials(category.getMaterials()); materialCategories.add(materialCategory, category); storeOldCodes(category); final Set<MaterialCategory> subcategories = category.getSubcategories(); if (subcategories != null) { addCategories(category, subcategories); } } }
@Override @Transactional(readOnly=true) public List<Material> getMaterials(MaterialCategory materialCategory) { List<Material> result = new ArrayList<Material>(); result.addAll(materialCategory.getMaterials()); return result; }
@Override @Transactional(readOnly=true) public void addMaterialCategory(MaterialCategory parent, String categoryName) throws ValidationException { Validate.notNull(categoryName); Boolean generateCode = configurationDAO.getConfiguration() .getGenerateCodeForMaterialCategories(); MaterialCategory child = MaterialCategory.createUnvalidated("", _(categoryName)); if (generateCode) { setCurrentMaterialCategory(child); setDefaultCode(); } child.setCodeAutogenerated(generateCode); final MaterialCategory materialCategory = findMaterialCategory(child); if (materialCategory != null) { throw new ValidationException(invalidValue( _("{0} already exists", materialCategory.getName()), "name", materialCategory.getName(), materialCategory)); } child.setParent(parent); if (parent == null) { materialCategories.addToRoot(child); } else { materialCategories.add(parent, child); } }
private void checkNoCodeRepeatedAtNewMaterials( final List<MaterialCategory> categories) throws ValidationException { List<Material> allMaterials = MaterialCategory .getAllMaterialsWithoutAutogeneratedCodeFrom(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); } }
@Override public void addMaterialToMaterialCategory(MaterialCategory materialCategory) { Material material = Material.create(""); material.setCategory(materialCategory); materialCategory.addMaterial(material); }