Exemplo n.º 1
0
    @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);
        }
    }
Exemplo n.º 2
0
    @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();
        }
    }
Exemplo n.º 3
0
 @Override
 @Transactional(readOnly = true)
 public MutableTreeModel<MaterialCategory> getMaterialCategories() {
     if (materialCategories.isEmpty()) {
         initializeMaterialCategories();
     }
     return materialCategories;
 }
Exemplo n.º 4
0
 @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;
 }
Exemplo n.º 5
0
 private MaterialCategory findMaterialCategory(
         final MaterialCategory category) {
     for (MaterialCategory mc : materialCategories.asList()) {
         if (equalsMaterialCategory(mc, category)) {
             return mc;
         }
     }
     return null;
 }
Exemplo n.º 6
0
 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);
     }
 }
Exemplo n.º 7
0
 @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);
     }
 }
Exemplo n.º 8
0
 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);
         }
     }
 }
Exemplo n.º 9
0
 @Override
 @Transactional(readOnly=true)
 public void reloadMaterialCategories() {
     materialCategories = MutableTreeModel.create(MaterialCategory.class);
     initializeMaterialCategories();
 }
Exemplo n.º 10
0
@Service
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
@OnConcurrentModification(goToPage = "/materials/materials.zul")
public class MaterialsModel extends IntegrationEntityModel implements
        IMaterialsModel {

    @Autowired
    IMaterialCategoryDAO categoryDAO;

    @Autowired
    IMaterialDAO materialDAO;

    @Autowired
    IUnitTypeDAO unitTypeDAO;

    @Autowired
    IConfigurationDAO configurationDAO;

    @Autowired
    IMaterialAssignmentDAO materialAssignmentDAO;

    MutableTreeModel<MaterialCategory> materialCategories = MutableTreeModel
            .create(MaterialCategory.class);

    private List<UnitType> unitTypes = new ArrayList<UnitType>();

    private MaterialCategory currentMaterialCategory;

    private Map<MaterialCategory, String> oldCodes = new HashMap<MaterialCategory, String>();

    private Map<Material, String> oldMaterialCodes = new HashMap<Material, String>();

    @Override
    @Transactional(readOnly = true)
    public MutableTreeModel<MaterialCategory> getMaterialCategories() {
        if (materialCategories.isEmpty()) {
            initializeMaterialCategories();
        }
        return materialCategories;
    }

    @Override
    @Transactional(readOnly=true)
    public void reloadMaterialCategories() {
        materialCategories = MutableTreeModel.create(MaterialCategory.class);
        initializeMaterialCategories();
    }

    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 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 initializeMaterials(Set<Material> materials) {
        for (Material each: materials) {
            each.getDescription();
            if (each.getUnitType() != null) {
                each.getUnitType().getMeasure();
            }
        }
    }

    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 MaterialCategory findMaterialCategory(
            final MaterialCategory category) {
        for (MaterialCategory mc : materialCategories.asList()) {
            if (equalsMaterialCategory(mc, category)) {
                return mc;
            }
        }
        return null;
    }

    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();
        }
    }

    @Override
    public void addMaterialToMaterialCategory(MaterialCategory materialCategory) {
        Material material = Material.create("");
        material.setCategory(materialCategory);
        materialCategory.addMaterial(material);
    }

    @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);
        }
    }

    private void generateMaterialCodesIfIsNecessary(List<MaterialCategory> categories,
            Integer numberOfDigits) {
        for (MaterialCategory category: categories) {
            if (category.isCodeAutogenerated()) {
                category.generateMaterialCodes(numberOfDigits);
            }
        }
    }

    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);
        }
    }

    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);
    }

    @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;
    }

    @Override
    @Transactional(readOnly = true)
    public void loadUnitTypes() {
        List<UnitType> result = new ArrayList<UnitType>();
        for (UnitType each : unitTypeDAO.findAll()) {
            each.getMeasure();
            result.add(each);
        }
        this.unitTypes = result;
    }

    public List<UnitType> getUnitTypes() {
        return this.unitTypes;
    }

    @Override
    @Transactional(readOnly = true)
    public boolean canRemoveMaterial(Material material) {
        if(material.isNewObject()) {
            return true;
        }
        return materialAssignmentDAO.getByMaterial(material).size() == 0;
    }

    @Override
    protected void restoreOldCodes() {
        getCurrentEntity().setCode(oldCodes.get(getCurrentEntity()));
        for (Material child : ((MaterialCategory) getCurrentEntity())
                .getMaterials()) {
            if (!child.isNewObject()) {
                child.setCode(oldMaterialCodes.get(child));
            }
        }
    }

    public void setCodeAutogenerated(boolean codeAutogenerated,
            MaterialCategory materialCategory)
            throws ConcurrentModificationException {
        setCurrentMaterialCategory(materialCategory);
        setCodeAutogenerated(codeAutogenerated);
    }

    @Override
    public Boolean isGenerateCodeOld() {
        if (getCurrentEntity() != null) {
            return oldCodes.get(getCurrentEntity()) != null;
        }
        return false;
    }

    public EntityNameEnum getEntityName() {
        return EntityNameEnum.MATERIAL_CATEGORY;
    }

    public Set<IntegrationEntity> getChildren() {
        return new HashSet<IntegrationEntity>();
    }

    public IntegrationEntity getCurrentEntity() {
        return this.currentMaterialCategory;
    }

    public void setCurrentMaterialCategory(MaterialCategory materialCategory) {
        this.currentMaterialCategory = materialCategory;
    }

}