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

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

package br.com.jarch.faces.controller;

import br.com.jarch.core.annotation.JArchDynamicDetailShowDataController;
import br.com.jarch.core.annotation.JArchEventDataDetailStartEnviromentConsult;
import br.com.jarch.core.crud.search.ISearch;
import br.com.jarch.core.crud.service.IBaseService;
import br.com.jarch.core.exception.InsertException;
import br.com.jarch.core.model.*;
import br.com.jarch.core.model.type.ActionCrudType;
import br.com.jarch.core.util.BundleUtils;
import br.com.jarch.faces.util.JavaScriptUtils;
import br.com.jarch.util.LogUtils;
import br.com.jarch.util.ReflectionUtils;

import javax.annotation.PostConstruct;
import javax.enterprise.event.Event;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public abstract class BaseDataDetail implements IDataDetail, Serializable {

    @Inject
    private IBaseService service;

    @Inject
    private ISearch search;

    @Inject
    @JArchEventDataDetailStartEnviromentConsult
    private Event> eventStartConsult;

    private ColumnsList listColumnDataTable;

    private IAcessMenu acessMenu;

    private E entity;

    private E originalEntity;

    private Collection listBeforeChange;

    private IBaseEntity entityMaster;

    private ActionCrudType action;

    private boolean disableInsert;

    private boolean disableChange;

    private boolean disableConsult;

    private boolean disableDelete;

    private boolean required;

    private boolean blocked;

    private boolean modeShowData;

    private String classCollapse;

    private transient JArchDynamicDetailShowDataController annotationDynamic;

    private LazyDataModelPaginator dataModel;

    private transient Predicate filterDataModel;

    private transient Comparator orderDataModel;

    private transient Runnable processStartEnviroment;

    private List selections = new ArrayList<>();

    @PostConstruct
    private void init() {
        startEnviroment();
    }

    public E getEntity() {
        return entity;
    }

    public void setEntity(E entity) {
        this.entity = entity;
    }

    public Predicate getFilterDataModel() {
        return filterDataModel;
    }

    public void setFilterDataModel(Predicate filterDataModel) {
        this.filterDataModel = filterDataModel;
    }

    public Comparator getOrderDataModel() {
        return orderDataModel;
    }

    public void setOrderDataModel(Comparator orderDataModel) {
        this.orderDataModel = orderDataModel;
    }

    public IBaseService getService() {
        return service;
    }

    public ISearch getSearch() {
        return search;
    }

    public ActionCrudType getAction() {
        return action;
    }

    public void setAction(ActionCrudType action) {
        this.action = action;
    }

    public String getClassCollapse() {
        return classCollapse;
    }

    public void setClassCollapse(String classCollapse) {
        this.classCollapse = classCollapse;
    }

    @Override
    public List getListColumnDataTable() {
        if (listColumnDataTable == null) {
            LogUtils.warning("JARCH ERROR: BaseDataDetail " + this.getClass().getSimpleName() + " não configurado as colunas @JArchColumnDataTable");
            return new ArrayList<>();
        }

        return listColumnDataTable.stream().filter(IColumnList::isVisible).collect(Collectors.toList());
    }

    @Override
    public void setListColumnDataTable(ColumnsList listColumnDataTable) {
        this.listColumnDataTable = listColumnDataTable;
    }

    public List getSelections() {
        return selections;
    }

    public void setSelections(List selections) {
        this.selections = selections;
    }


    public boolean isRequired() {
        return required;
    }

    public Optional getColumnDataTable(String field) {
        return listColumnDataTable.stream().filter(c -> c.getField().equals(field)).findAny();
    }

    public LazyDataModelPaginator createDataModelFromMaster(IBaseEntity entityMaster) {
        this.entityMaster = entityMaster;

        if (entityMaster != null && dataModel == null || dataModel.getRowCount() == 0/* || !entityMaster.equals(this.entityMaster)*/) {
            configureDataModel();
        }

        return dataModel;
    }

//    public void createDataModelFromCollection(Collection listEntity) {
//        List listReturn = new ArrayList<>();
//
//        listReturn.addAll(listEntity);
//
//        if (predicateDataModel != null) {
//            listReturn = listReturn.stream().filter(predicateDataModel).collect(Collectors.toList());
//        }
//
//        if (orderDataModel != null) {
//            listReturn = listReturn.stream().sorted(orderDataModel).collect(Collectors.toList());
//        }
//
//        dataModel = new LazyDataModelPaginator<>(listReturn, search);
//    }

    public boolean isDisableConsult() {
        return disableConsult;
    }

    public void setDisableConsult(boolean disableConsult) {
        this.disableConsult = disableConsult;
    }

    public void consult(E entity) {
        try {
            if (isModeShowData()) {
                JavaScriptUtils.showMessageHeaderWarning(BundleUtils.messageBundle("label.atencao"), BundleUtils.messageBundle("message.dataDetailRegisterModeEdition"));
                return;
            }

            annotationDynamic = null;
            action = ActionCrudType.CONSULT;
            this.entity = entity;
            eventStartConsult.fire(this);
            modeShowData = true;

            if (processStartEnviroment != null)
                processStartEnviroment.run();

            collapseIn();
        } catch (Exception e) {
            LogUtils.generate(e);
            JavaScriptUtils.showMessageHeaderError(e);
        }
    }

    public void cancel() {
        updateDatatableTabBody();

        modeShowData = false;
        required = false;

        try {
            init();
        } catch (InsertException e) {
            LogUtils.generate(e);
        }

        collapse();
    }

    public boolean isBlockedDetail() {
        return blocked
                || (annotationDynamic != null && annotationDynamic.blockedDetail())
                || (!action.isInsert() && !action.isChange() && !action.isClone());
    }

    public void refresh() {
        startEnviroment();
        createDataModelFromMaster(entityMaster);
        updateDatatableTabBody();
    }

    public void clearDataModel() {
        dataModel = null;
    }

    public Runnable getProcessStartEnviroment() {
        return processStartEnviroment;
    }

    public void setProcessStartEnviroment(Runnable processStartEnviroment) {
        this.processStartEnviroment = processStartEnviroment;
    }

    public List getMenuActions() {
        return acessMenu
                .getMenuActions()
                .stream()
                .sorted(Comparator.comparing(ItemMenuArch::getOrder))
                .collect(Collectors.toList());
    }

    @Override
    public void createMenu(Field field) {
        acessMenu = new AcessMenu(field);
    }

    public JArchDynamicDetailShowDataController getAnnotationDynamic() {
        return annotationDynamic;
    }

    public String getDescriptionDynamic() {
        return annotationDynamic == null
                ? null
                : BundleUtils.messageBundle(annotationDynamic.labelButton());
    }

    public String getStyleClass() {
        return annotationDynamic == null ? null : annotationDynamic.styleClass().getStyleClass();
    }

    public void blocked() {
        this.blocked = true;
    }

    public void setBlocked(boolean blocked) {
        this.blocked = blocked;
    }

    public boolean isBlocked() {
        return blocked;
    }

    public void startEnviroment() {
        collapse();
        annotationDynamic = null;
        action = ActionCrudType.CONSULT;
        try {
            entity = service.getClassEntity().newInstance();
        } catch (Exception ex) {
            LogUtils.generate(ex);
        }
        eventStartConsult.fire(this);
        required = false;
    }

    private void configureDataModel() {
        List lista = new ArrayList<>();

        Field fieldCollection = ReflectionUtils.getFieldCollection(entityMaster.getClass(), this.entity.getClass());

        if (fieldCollection == null)
            return;

        Collection listaDetalhe = (Collection) ReflectionUtils.getValueByField(entityMaster, fieldCollection);

        if (listaDetalhe != null)
            lista.addAll(listaDetalhe);

        if (filterDataModel != null)
            lista = lista.stream().filter(filterDataModel).collect(Collectors.toList());

        if (orderDataModel != null)
            lista = lista.stream().sorted(orderDataModel).collect(Collectors.toList());

        dataModel = new br.com.jarch.faces.controller.LazyDataModelPaginator<>(lista, search);
    }

    private void showDataTab() {
        String tabName = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("tabName");
        JavaScriptUtils.execute("$('" + tabName + "').collapse('show');");
        collapseIn();
    }

    private void hideDataTab() {
        String tabName = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("tabName");
        JavaScriptUtils.execute("$('" + tabName + "').collapse('hide');");
        collapse();
    }

    public boolean isModeShowData() {
        return modeShowData;
    }

    private void updateDatatableTabBody() {
        JavaScriptUtils.execute("if (typeof updateDataTableTabBody == 'function') { updateDataTableTabBody(); }");
        JavaScriptUtils.execute("if (typeof updateDataTableDivDataDetailTab == 'function') { updateDataTableDivDataDetailTab(); }");
    }

    private void collapse() {
        classCollapse = "collapse";
    }

    private void collapseIn() {
        classCollapse = "collapse in";
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy