@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 initializeMaterialCategories() { final List<MaterialCategory> categories = categoryDAO.getAllRootMaterialCategories(); for (MaterialCategory materialCategory: categories) { initializeMaterials(materialCategory.getMaterials()); materialCategories.addToRoot(materialCategory); addCategories(materialCategory, materialCategory.getSubcategories()); storeOldCodes(materialCategory); } }
@Override @Transactional public void confirmSave() throws ValidationException { final List<MaterialCategory> categories = materialCategories.asList(); checkNoCodeRepeatedAtNewMaterials(categories); Integer numberOfDigits = getNumberOfDigitsCode(); generateMaterialCodesIfIsNecessary(categories, numberOfDigits); for (MaterialCategory each : categories) { categoryDAO.save(each); } }