All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.com.jarch.faces.controller.CrudListController Maven / Gradle / Ivy

package br.com.jarch.faces.controller;

import br.com.jarch.core.ConstantCore;
import br.com.jarch.core.annotation.*;
import br.com.jarch.core.cdi.GlobalInformation;
import br.com.jarch.core.crud.repository.CrudRepository;
import br.com.jarch.core.crud.service.ICrudService;
import br.com.jarch.core.model.IColumnList;
import br.com.jarch.core.model.ICrudEntity;
import br.com.jarch.core.model.IIdentity;
import br.com.jarch.core.model.ItemMenuSelected;
import br.com.jarch.core.model.type.ActionCrudType;
import br.com.jarch.faces.util.JavaScriptUtils;
import br.com.jarch.util.NumberUtils;
import br.com.jarch.util.ReflectionUtils;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collection;

@JArchCrudList
public abstract class CrudListController, R extends CrudRepository> extends BaseListController implements ICrudListController {

    private ItemMenuSelected itemMenuSelected;

    @Override
    public ItemMenuSelected getItemMenuSelected() {
        return itemMenuSelected;
    }

    @Override
    public void setItemMenuSelected(ItemMenuSelected itemMenuSelected) {
        this.itemMenuSelected = itemMenuSelected;
    }

    @Override
    public String getWidgetVar() {
        return getClass().getSimpleName();
    }

    @Override
    public boolean isExclusionLogic() {
        return getService().isExclusionLogic();
    }

    @Override
    public void callInsert() {
        try {
            AnnotationUtils.processMethodAnnotedIntoInstance(JArchCrudListBeforeCallInsert.class, this);
            redirectPageDataInsert();
        } catch (Exception ex) {
            JavaScriptUtils.showMessageHeaderError(ex);
        }
    }

    @Override
    public void callClone(Long id) {
        AnnotationUtils.processMethodAnnotedIntoInstance(JArchCrudListBeforeCallClone.class, this, id);
        redirectPageDataClone(id);
    }

    @Override
    public void callClone(E identity) {
        callClone(identity.getId());
    }

    @Override
    public void callChange(Long id) {
        AnnotationUtils.processMethodAnnotedIntoInstance(JArchCrudListBeforeCallChange.class, this, id);
        redirectPageDataChange(id);
    }

    @Override
    public void callChange(E identity) {
        callChange(identity.getId());
    }

    @Override
    public void callDelete(Long id) {
        AnnotationUtils.processMethodAnnotedIntoInstance(JArchCrudListBeforeCallDelete.class, this, id);
        redirectPageDataDelete(id);
    }

    @Override
    public void callDelete(E identity) {
        callDelete(identity.getId());
    }

    @Override
    public void callActionDynamic(Long idEntity, String idDynamic) {
        AnnotationUtils.processMethodAnnotedIntoInstance(JArchCrudListBeforeCallDynamic.class, this, idDynamic, idEntity);
        redirectPageDataDynamic(idEntity, idDynamic, null);
    }

    @Override
    public void callActionDynamic(IIdentity identity, String idDynamic) {
        callActionDynamic(identity.getId(), idDynamic);
    }

    @Override
    public void callActionDynamic(Long idEntity, String idDynamic, String labelMenuDynamic) {
        AnnotationUtils.processMethodAnnotedIntoInstance(JArchCrudListBeforeCallDynamic.class, this, idDynamic, idEntity);
        redirectPageDataDynamic(idEntity, idDynamic, labelMenuDynamic);
    }

    @Override
    public void callActionDynamic(IIdentity identity, String idDynamic, String labelMenuDynamic) {
        callActionDynamic(identity.getId(), idDynamic, labelMenuDynamic);
    }

    @Override
    public void processMethodDynamic(String method, String idDynamic, IIdentity rowTable, String labelMenu) {
        processMethodDynamic(method, idDynamic, rowTable, labelMenu, false);
    }

    @Override
    public void processMethodDynamicItemMenuSelected() {
        itemMenuSelected = GlobalInformation.getInstance().get(ConstantCore.JARCH_CRUD_LIST_CONTROLLER_ITEM_MENU_SELECTED);
        processMethodDynamic(itemMenuSelected.getMethod(), itemMenuSelected.getIdDynamic(), itemMenuSelected.getRowTable(),
            itemMenuSelected.getLabelMenu(), itemMenuSelected.isShowMessageSuccess());
    }

    @Override
    public void processMethodDynamic(String method, String idDynamic, IIdentity rowTable, String labelMenu,
                                     boolean showMessageSuccess) {
        if (method.equals("callActionDynamic")) {
            try {
                callActionDynamic(rowTable, idDynamic, labelMenu);
            } catch (Exception ex) {
                JavaScriptUtils.showMessageHeaderError(ex);
            }
        } else {
            getService().setIdDynamic(idDynamic);
            try {
                ReflectionUtils.executeMethod(this, method, rowTable);
                if (showMessageSuccess) {
                    JavaScriptUtils.showMessageBodySuccessNoRedirect();
                } else {
                    JavaScriptUtils.hideMessageProcess(true);
                }
            } catch (Exception ex) {
                JavaScriptUtils.hideMessageProcess(true);
                JavaScriptUtils.showMessageHeaderError(ex);
            } finally {
                getService().setIdDynamic(null);
            }
        }
    }

    @Override
    public void processMethodDynamicConfirmation(boolean confirmation, String headerConfirmation,
                                                 String messageConfirmation, String method, String idDynamic,
                                                 IIdentity rowTable, String labelMenu, boolean showMessageSuccess) {

        itemMenuSelected = new ItemMenuSelected();
        itemMenuSelected.setConfirmation(confirmation);
        itemMenuSelected.setHeaderConfirmation(headerConfirmation);
        itemMenuSelected.setMessageConfirmation(messageConfirmation);
        itemMenuSelected.setMethod(method);
        itemMenuSelected.setIdDynamic(idDynamic);
        itemMenuSelected.setRowTable(rowTable);
        itemMenuSelected.setLabelMenu(labelMenu);
        itemMenuSelected.setShowMessageSuccess(showMessageSuccess);

        GlobalInformation.getInstance().set(ConstantCore.JARCH_CRUD_LIST_CONTROLLER_ITEM_MENU_SELECTED, itemMenuSelected);

        if (confirmation) {
            JavaScriptUtils.execute("PF('widgetVarConfirmation').show()");
            return;
        }

        if (method.equals("callActionDynamic")) {
            callActionDynamic(rowTable, idDynamic, labelMenu);
        } else {
            getService().setIdDynamic(idDynamic);
            try {
                ReflectionUtils.executeMethod(this, method, rowTable);
                if (showMessageSuccess) {
                    JavaScriptUtils.showMessageBodySuccessNoRedirect();
                } else {
                    JavaScriptUtils.hideMessageProcess(true);
                }
            } catch (Exception ex) {
                JavaScriptUtils.hideMessageProcess(true);
                JavaScriptUtils.showMessageHeaderError(ex);
            } finally {
                getService().setIdDynamic(null);
            }
        }
    }

    private void redirectPageDataInsert() {
        redirectPageData(ActionCrudType.INSERT, null, null, null, null);
    }

    private void redirectPageDataClone(Long id) {
        redirectPageData(ActionCrudType.CLONE, null, id, null, null);
    }

    private void redirectPageDataChange(Long id) {
        redirectPageData(ActionCrudType.CHANGE, null, id, null, null);
    }

    private void redirectPageDataDelete(Long id) {
        redirectPageData(ActionCrudType.DELETE, null, id, null, null);
    }

    private void redirectPageDataDynamic(Long idEntity, String idDynamic, String labelMenuDynamic) {
        redirectPageData(ActionCrudType.DYNAMIC, null, idEntity, idDynamic, labelMenuDynamic);
    }

    @Override
    public String totalizer(IColumnList columnList){
        String valorRetorno = "";

        if (getDataModel() != null && columnList != null && columnList.isNumber()){

            Collection lista = getDataModel()
                    .getPaginator()
                    .getList();

            double total = 0d;

            for (Object entity: lista){
                Object valor = ReflectionUtils.getValueByName(entity, columnList.getField().replace(".abs()", ""));
                if (BigDecimal.class.isAssignableFrom(valor.getClass()))
                    total += ((BigDecimal) valor).doubleValue();
                else if (BigInteger.class.isAssignableFrom(valor.getClass()))
                    total += ((BigInteger) valor).doubleValue();
                else if (Integer.class.isAssignableFrom(valor.getClass()))
                    total += ((Integer) valor).doubleValue();
                else
                    total += ((Long) valor).doubleValue();
            }

            if (BigDecimal.class.isAssignableFrom(columnList.getType().getClassType())) {
                valorRetorno = NumberUtils.formatMoney(total);
            } else {
                valorRetorno = NumberUtils.formatInteger(total);
            }
        }

        return valorRetorno;
    }

    @Override
    public boolean isExistColumnsTotalizer() {
        return getListColumnDataTable().stream().anyMatch(IColumnList::isNumber);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy