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

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

package br.com.jarch.faces.controller;

import br.com.jarch.core.annotation.AnnotationUtils;
import br.com.jarch.core.annotation.JArchCrudDataStartEnviromentConsult;
import br.com.jarch.core.annotation.JArchEventDataLoadConsult;
import br.com.jarch.core.crud.repository.BaseRepository;
import br.com.jarch.core.crud.service.IBaseService;
import br.com.jarch.core.model.ColumnsList;
import br.com.jarch.core.model.IIdentity;
import br.com.jarch.faces.util.JsfUtils;
import br.com.jarch.util.ReflectionUtils;
import br.com.jarch.core.type.LoadCrudType;
import org.primefaces.model.StreamedContent;

import javax.annotation.PostConstruct;
import javax.enterprise.event.Event;
import javax.inject.Inject;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.function.Predicate;

public abstract class BaseDataController, R extends BaseRepository> extends br.com.jarch.faces.controller.BaseController implements IBaseDataController {

    @Inject
    private transient S service;

    @Inject
    private transient R repository;

    @Inject
    @JArchEventDataLoadConsult
    private Event eventLoadConsult;

    protected E entity;

    private boolean blockedMaster;

    private boolean blockedDetail;

    private boolean visibleMaster;

    private boolean visibleDetail;

    private List selections = new ArrayList<>();

    @PostConstruct
    private void init() {
        Arrays
                .stream(ReflectionUtils.getArrayFields(getClass(), true, true))
                .filter(field -> IDataDetail.class.isAssignableFrom(field.getType()))
                .forEach(field -> ((IDataDetail) ReflectionUtils.getValueByField(this, field)).createMenu(field));

        loadColumnDataTable();

        blockedMaster = true;
        blockedDetail = true;
        visibleMaster = true;
        visibleDetail = true;

        if (!CrudDataController.class.isAssignableFrom(this.getClass())) {
            configVariables();
            consultEnviroment(entity);
        }
    }

    @Override
    public S getService() {
        return service;
    }

    @Override
    public R getRepository() {
        return repository;
    }

    @Override
    public E getEntity() {
        return entity;
    }

    @Override
    public boolean isBlockedMaster() {
        return blockedMaster;
    }

    @Override
    public void setBlockedMaster(boolean blockedMaster) {
        this.blockedMaster = blockedMaster;
    }

    @Override
    public boolean isBlockedDetail() {
        return blockedDetail;
    }

    @Override
    public void setBlockedDetail(boolean blockedDetail) {
        this.blockedDetail = blockedDetail;
    }

    @Override
    public boolean isVisibleMaster() {
        return visibleMaster;
    }

    @Override
    public boolean isVisibleDetail() {
        return visibleDetail;
    }

    @Override
    public List getSelections() {
        return selections;
    }

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

    @Override
    public void processaMetodoActionMenuArch(String metodo) throws InvocationTargetException, IllegalAccessException {
        Objects.requireNonNull(ReflectionUtils.getMethod(this.getClass(), metodo)).invoke(this);
    }

    @Override
    public void processaMetodoActionMenuArch(String method, E row) throws InvocationTargetException, IllegalAccessException {
        ProcessMethod.execute(this, method, row);
    }

    @Override
    public boolean processRenderedEvaluation(Predicate evaluate, E row) {
        if (evaluate == null)
            return true;

        return evaluate.test(row);
    }

    @Override
    public boolean processDisabledEvaluation(Predicate evaluate, E row) {
        if (evaluate == null)
            return false;

        return evaluate.test(row);
    }

    @Override
    public StreamedContent processMethodMenuDownload(String method, E row) throws InvocationTargetException, IllegalAccessException {
        return ProcessMethod.generateStreamedContent(this, method, row);
    }

    private void loadColumnDataTable() {
        for (Field field : ReflectionUtils.getArrayFields(this, true, true)) {
            if (!IDataDetail.class.isAssignableFrom(field.getType()))
                continue;

            IDataDetail dataDetail = (IDataDetail) ReflectionUtils.getValueByField(this, field);
            dataDetail.setListColumnDataTable(ColumnsList.create(ReflectionUtils.getGenericClass(field, 0), getClass(), classEntity()));
        }
    }

    @Override
    public void consultEnviroment(E entity) {
        this.entity = entity;
        clearAllDataModelDetail();
        eventLoadConsult.fire(entity);
        AnnotationUtils.processMethodAnnotedIntoInstance(JArchCrudDataStartEnviromentConsult.class, this);
    }

    protected void clearAllDataModelDetail() {
        Arrays.stream(getClass().getDeclaredFields())
                .filter(f -> BaseDataDetail.class.isAssignableFrom(f.getType()))
                .forEach(f -> ((BaseDataDetail) ReflectionUtils.getValueByField(this, f)).clearDataModel());
    }

    private void configVariables() {
        TransferListToData transferListToData = new TransferListToData<>(classEntity());

        Long idEntity = transferListToData.getIdEntity();
        entity = transferListToData.getEntity();

        if (idEntity != null)
            entity = getService().loadCrud(idEntity, this.getClass().getSimpleName(), LoadCrudType.CONSULT);
        else if (entity != null && entity.getId() != null)
            entity = getService().loadCrud(entity.getId(), this.getClass().getSimpleName(), LoadCrudType.CONSULT);
    }

    @Override
    public void viewDiagram() {
        JsfUtils.redirect("../bpmn/diagram.jsf");
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy