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

br.com.jarch.faces.parameter.BaseParameterController Maven / Gradle / Ivy

package br.com.jarch.faces.parameter;

import br.com.jarch.core.crud.parameter.BaseParameterService;
import br.com.jarch.core.crud.parameter.IParameter;
import br.com.jarch.core.crud.parameter.JArchParameterFilterSystem;
import br.com.jarch.core.crud.parameter.Parameter;
import br.com.jarch.core.model.ProgressInformation;
import br.com.jarch.core.util.BundleUtils;
import br.com.jarch.faces.parameter.JArchParametersViewShow.FieldSet;
import br.com.jarch.faces.parameter.JArchParametersViewShow.Menu;
import br.com.jarch.faces.parameter.JArchParametersViewShow.Tab;
import br.com.jarch.faces.util.JavaScriptUtils;
import br.com.jarch.util.CollectionUtils;
import br.com.jarch.util.LogUtils;
import br.com.jarch.util.StringUtils;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

import javax.annotation.PostConstruct;
import javax.enterprise.inject.Instance;
import javax.inject.Inject;
import java.io.ByteArrayInputStream;
import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public abstract class BaseParameterController> implements Serializable {

    @Inject
    private transient S service;

    @Inject
    @JArchParameterFilterSystem
    private Instance> filterSystems;

    private ProgressInformation progress;

    private String menu = "";
    private String tab = "";

    private List menus = new ArrayList<>();
    private List tabs = new ArrayList<>();
    private List fieldSets = new ArrayList<>();

    private Map>>> parameterViews;
    private String menuLast;
    private String tabLast;
    private String fieldSetLast;
    private List paramsLast;

    @PostConstruct
    private void init() {
        resetValues();
        configMenus();
    }

    private void resetValues() {
        parameterViews = new HashMap<>();
        menuLast = "";
        tabLast = "";
        fieldSetLast = "";
        paramsLast = new ArrayList<>();
    }

    public String getMenu() {
        return menu;
    }

    private void configMenus() {
        menus = allMenus();
    }

    public void setMenu(String menu) {
        if (!this.menu.equals(menu)) {
            tabs.clear();
            fieldSets.clear();
        }

        this.menu = menu;
    }

    public List getMenus() {
        return menus;
    }

    public List configTabList(String menu) {
        if (tabs.isEmpty() || !this.menu.equals(menu)) {
            configTabs(menu);
        }

        return tabs;
    }

    private void configTabs(String menu) {
        this.menu = menu;

        tabs = allMenusStream()
                .filter(t -> getLabel(t.labelMenu()).equals(menu))
                .flatMap(t -> Arrays.stream(t.tabs()))
                .map(t -> getLabel(t.labelTab()))
                .collect(Collectors.toList());
    }

    public List configFieldSetList(String menu, String tab) {
        if (StringUtils.isNullOrEmpty(menu))
            menu = getLabel("label.geral");

        if (StringUtils.isNullOrEmpty(tab))
            tab = getLabel("label.geral");

        if (StringUtils.isNullOrEmpty(this.menu))
            this.menu = getLabel("label.geral");

        if (StringUtils.isNullOrEmpty(this.tab))
            this.tab = getLabel("label.geral");

        if (CollectionUtils.isNullOrEmpty(fieldSets)
                || !this.menu.equals(menu)
                || !this.tab.equals(tab))
            configFieldSets(menu, tab);

        return fieldSets;
    }

    private static String getLabel(String key) {
        if (StringUtils.isNullOrEmpty(key))
            return BundleUtils.messageBundle("label.geral");
        else
            return BundleUtils.messageBundle(key);
    }

    private void configFieldSets(String menu, String tab) {
        this.menu = menu;
        this.tab = tab;

        fieldSets = allFieldSetsStream(menu, tab)
                .map(fs -> getLabel(fs.labelFieldSet()))
                .collect(Collectors.toList());
    }

    public List parameterList(String menu, String tab, String fieldSet) {
        return searchAllBy(menu, tab, fieldSet);
    }

    private int counterParameters(Object obj) {
        int count = 0;
        if (obj instanceof Map) {
            Map map = (Map) obj;
            for (Object value : map.values()) {
                count += counterParameters(value);
            }
        } else {
            return ((List)obj).size();
        }

        return count;
    }

    public void save() {
        String lastMenu = null;
        String lastTab = null;
        String lastFieldSet = null;
        String lastDescription = null;

        if (parameterViews.isEmpty()) {
            getProgress().status("Carregando Parâmetros...");
            loadAllParameters();
        }

        getProgress().size(counterParameters(parameterViews));

        try {
            for (var menu : parameterViews.keySet()) {
                lastMenu = menu;

                for (var tab : parameterViews.get(menu).keySet()) {
                    lastTab = tab;

                    for (var fieldSet : parameterViews.get(menu).get(tab).keySet()) {
                        lastFieldSet = fieldSet;

                        for (ParameterView parameterView :  parameterViews
                                .get(menu)
                                .get(tab)
                                .get(fieldSet)) {

                            lastDescription = parameterView.getParameter().description();

                            getProgress().increment();
                            getProgress().status(menu + " -> " + tab + " -> " + fieldSet + " -> " + lastDescription);

                            service.save(parameterView.getParameter());
                        }
                    }
                }
            }

            JavaScriptUtils.showMessageHeaderSuccess(BundleUtils.messageBundle("label.parametros"),
                    BundleUtils.messageBundle("message.procedimentoRealizadoSucesso"));
        } catch (Exception ex) {
            String message = menuTabFieldSetDescription(lastMenu, lastTab, lastFieldSet, lastDescription);

            message += ex.getMessage();

            JavaScriptUtils.showMessageHeaderError(message);
        } finally {
            getProgress().reset();
        }

    }

    public String menuTabFieldSetDescription(String menu, String tab, String fieldSet, String description) {
        String message = isNullOrBlankOrGeral(menu) ? "" : "-> " + menu;
        message += isNullOrBlankOrGeral(tab) ? "" : " -> " + tab;
        message += isNullOrBlankOrGeral(fieldSet) ? "" : " -> " + fieldSet;
        message += description == null ? "" : " -> " + description;

        if (!StringUtils.isNullOrEmpty(message))
            message += ": ";

        if (message.startsWith("-> "))
            message = message.substring(3);
        return message;
    }

    private boolean isNullOrBlankOrGeral(String menuAtual) {
        return StringUtils.isNullOrEmpty(menuAtual) || menuAtual.equals(getLabel("label.geral"));
    }

    public void handleFileUpload(FileUploadEvent event) {
        String idJsf = event.getComponent().getClientId();
        var parameterUpload = getByIdJsf(idJsf);

        if (parameterUpload.isPresent()) {
            parameterUpload.get().getParameter().setValueBinary(event.getFile().getContent());
            parameterUpload.get().getParameter().setValueOtherInformation(event.getFile().getFileName());
        }
    }

    private List searchAllBy(String menu, String tab, String fieldSet) {
        if (StringUtils.isNullOrEmpty(menu))
            menu = getLabel("label.geral");

        if (StringUtils.isNullOrEmpty(tab))
            tab = getLabel("label.geral");

        if (StringUtils.isNullOrEmpty(fieldSet))
            fieldSet = getLabel("label.geral");

        if (!paramsLast.isEmpty()
                && menuLast != null
                && menuLast.equals(menu)
                && tabLast != null
                && tabLast.equals(tab)
                && fieldSetLast != null
                && fieldSetLast.equals(fieldSet)) {
            return paramsLast;
        }

        loadAllParameters();

        if (parameterViews.containsKey(menu)
                && parameterViews.get(menu).containsKey(tab)
                && parameterViews.get(menu).get(tab).containsKey(fieldSet))
            paramsLast = parameterViews.get(menu).get(tab).get(fieldSet);
        else
            paramsLast = Collections.emptyList();

        menuLast = menu;
        tabLast = tab;
        fieldSetLast = fieldSet;

        return paramsLast;
    }

    private void loadAllParameters() {
        if (!parameterViews.isEmpty())
            return;

        parameterViews = new HashMap<>();

        for (String menu : allMenus()) {

            LogUtils.generate(menu);

            for (String tab : allTabs(menu)) {

                LogUtils.generate(menu + " -> " + tab);

                for (String fieldSet : allFieldSets(menu, tab)) {

                    LogUtils.generate(menu + " -> " + tab + " -> " + fieldSet);

                    parameterViews.computeIfAbsent(menu, l -> new HashMap<>());
                    parameterViews.get(menu).computeIfAbsent(tab, l -> new HashMap<>());
                    parameterViews.get(menu).get(tab).computeIfAbsent(fieldSet, l -> new ArrayList<>());

                    List filterMenus = allMenusStream()
                            .filter(c -> getLabel(c.labelMenu()).equals(menu))
                            .collect(Collectors.toList());

                    List filterTabs = filterMenus
                            .stream()
                            .flatMap(m -> Arrays.stream(m.tabs()))
                            .filter(t -> getLabel(t.labelTab()).equals(tab))
                            .collect(Collectors.toList());

                    List
filterFieldSets = filterTabs .stream() .flatMap(t -> Arrays.stream(t.fieldSets())) .filter(fs -> getLabel(fs.labelFieldSet()).equals(fieldSet)) .collect(Collectors.toList()); List>> subTypes = filterFieldSets .stream() .flatMap(fs -> Arrays.stream(fs.parameters())) .collect(Collectors.toList()); for (Class> classParameter : subTypes) { try { Parameter parameter = classParameter.newInstance(); service.configBeanFromEntity(parameter); if (filterSystems.isResolvable() && !filterSystems.get().contains(parameter.system())) continue; JArchParametersViewConfig.ViewConfig annotationParameterView = allParameterView() .filter(p -> p.parameter().equals(parameter.getClass())) .findAny() .orElse(null); parameterViews .get(menu) .get(tab) .get(fieldSet) .add(new ParameterView(menu, tab, fieldSet, parameter, annotationParameterView)); } catch (Exception ex) { LogUtils.generate(ex); } } } } } } public List allFieldSets(String menu, String tab) { if (StringUtils.isNullOrEmpty(menu)) menu = getLabel("label.geral"); if (StringUtils.isNullOrEmpty(tab)) tab = getLabel("label.geral"); var result = allFieldSetsStream(menu, tab) .map(fs -> getLabel(fs.labelFieldSet())).collect(Collectors.toList()); return result; } public List allTabs(String menu) { if (StringUtils.isNullOrEmpty(menu)) menu = getLabel(null); return allTabsStream(menu) .map(t -> getLabel(t.labelTab())) .collect(Collectors.toList()); } private List allMenus() { return allMenusStream() .map(m -> getLabel(m.labelMenu())) .collect(Collectors.toList()); } private Optional getByIdJsf(String idJsf) { for (String category : menus) { for (String tab : configTabList(category)) { for (String fieldSet : configFieldSetList(menu, tab)) { if (parameterViews.get(category) == null || parameterViews.get(category).get(tab) == null || parameterViews.get(category).get(tab).get(fieldSet) == null) continue; var result = parameterViews .get(category) .get(tab) .get(fieldSet) .stream() .filter(p -> idJsf.endsWith(p.getIdJsf())) .findAny(); if (result.isPresent()) return result; } } } return Optional.empty(); } private Stream allMenusStream() { List packagesAnnotation = Arrays .stream(getClass().getClassLoader().getDefinedPackages()) .filter(p -> p.isAnnotationPresent(JArchParametersViewShow.class)) .collect(Collectors.toList()); return packagesAnnotation .stream() .flatMap(p -> Arrays.stream(p.getAnnotationsByType(JArchParametersViewShow.class))) .flatMap(p -> Arrays.stream(p.menus())); } private Stream allTabsStream(String menu) { return allMenusStream() .filter(m -> getLabel(m.labelMenu()).equals(menu)) .flatMap(m -> Arrays.stream(m.tabs())); } private Stream
allFieldSetsStream(String menu, String tab) { return allTabsStream(menu) .filter(t -> getLabel(t.labelTab()).equals(tab)) .flatMap(t -> Arrays.stream(t.fieldSets())); } private Stream allParameterView() { List packagesAnnotation = Arrays .stream(getClass().getClassLoader().getDefinedPackages()) .filter(p -> p.isAnnotationPresent(JArchParametersViewConfig.class)) .collect(Collectors.toList()); Stream parameterConfigViewsStream = packagesAnnotation .stream() .flatMap(p -> Arrays.stream(p.getAnnotationsByType(JArchParametersViewConfig.class))); return parameterConfigViewsStream.flatMap(p -> Arrays.stream(p.viewsConfig())); } public StreamedContent download(byte[] binary, String nameFile) { return DefaultStreamedContent .builder() .name(nameFile) .stream(() -> new ByteArrayInputStream(binary)) .build(); } public String valueToolTip(IParameter parameter) { if (parameter == null) return ""; String description = parameter.getDescription() == null ? parameter.description() : parameter.getDescription(); String explanation = parameter.getExplanation() == null ? parameter.explanation() : parameter.getExplanation(); return explanation.startsWith("???") ? description : explanation; } public String headerToolTip(String menu, String tab, String fieldSet) { var result = menu; result += StringUtils.isNullOrEmpty(tab) || "Geral".equals(tab) ? "" : " / " + tab; result += StringUtils.isNullOrEmpty(fieldSet) || "Geral".equals(fieldSet) ? "" : " / " + fieldSet; return result; } public ProgressInformation getProgress() { return ProgressInformation.getInstance(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy