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

br.com.jarch.apt.generate.DataXhtmlCodeGenerate Maven / Gradle / Ivy

There is a newer version: 24.9.0
Show newest version
package br.com.jarch.apt.generate;

import br.com.jarch.annotation.JArchGenerateLogicCrud;
import br.com.jarch.annotation.JArchGenerateLogicMasterDetail;
import br.com.jarch.annotation.JArchGenerateLogicMasterSubDetail;
import br.com.jarch.form.FieldForm;
import br.com.jarch.util.FileUtils;
import br.com.jarch.util.LogUtils;
import br.com.jarch.util.ProcessorUtils;
import br.com.jarch.util.type.FieldType;

import javax.lang.model.element.Element;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import static br.com.jarch.util.ProcessorUtils.addCode;

public final class DataXhtmlCodeGenerate {

    final String ENTITY = "entity";
    final String DATA_DETAIL = "dataDetail";
    final String DOT_ENTITY = ".entity";
    private Element element;
    private JArchGenerateLogicCrud generateLogicCrud;
    private JArchGenerateLogicMasterDetail generateLogicMasterDetail;
    private JArchGenerateLogicMasterSubDetail generateLogicMasterSubDetail;
    private File fileDataXhtml;
    private File fileDadosXhtml;
    private String nameMaster;
    private String nameMasterSemCaracterEspecial;
    private String nameMasterSemCaracterEspecialMinusculo;
    private String nameController;

    public DataXhtmlCodeGenerate(Element element, JArchGenerateLogicCrud generateLogicCrud) {
        this.element = element;
        this.generateLogicCrud = generateLogicCrud;
        configure(element, generateLogicCrud.master().name(), generateLogicCrud.nameSubPackage());
    }

    public DataXhtmlCodeGenerate(Element element, JArchGenerateLogicMasterDetail generateLogicMasterDetail) {
        this.element = element;
        this.generateLogicMasterDetail = generateLogicMasterDetail;
        configure(element, generateLogicMasterDetail.master().name(), generateLogicMasterDetail.nameSubPackage());
    }

    public DataXhtmlCodeGenerate(Element element, JArchGenerateLogicMasterSubDetail generateLogicMasterSubDetail) {
        this.element = element;
        this.generateLogicMasterSubDetail = generateLogicMasterSubDetail;
        configure(element, generateLogicMasterSubDetail.master().name(), generateLogicMasterSubDetail.nameSubPackage());
    }

    public void create() {
        if (ProcessorUtils.isFileExistsClientOrWebOrWs(fileDataXhtml) || ProcessorUtils.isFileExistsClientOrWebOrWs(fileDadosXhtml)) {
            return;
        }

        StringBuilder dataXhtml = new StringBuilder();
        addCode(dataXhtml, "");
        addCode(dataXhtml, "");
        addCode(dataXhtml, "\t");
        addCode(dataXhtml, "\t\t");
        addCode(dataXhtml, "\t</h:head>");
        addCode(dataXhtml, "<ui:composition template=\"/paginas/templates/templateDadosV2.xhtml\">");
        addCode(dataXhtml, "\t<ui:define name=\"panelBodyTemplateDados\">");
        addCode(dataXhtml, "\t\t<h:form id=\"frm" + nameMasterSemCaracterEspecial + "\">");
        addCode(dataXhtml, "\t\t\t<e:divTitle");
        addCode(dataXhtml, "\t\t\t\ttitle=\"#{e:bundle('label." + nameMasterSemCaracterEspecialMinusculo + "')} - #{" + nameMasterSemCaracterEspecialMinusculo + "DataController.labelAction}\"");
        addCode(dataXhtml, "\t\t\t\tdescription=\"#{e:bundle('label.manter')} #{e:bundle('label." + nameMasterSemCaracterEspecialMinusculo + "')}\"/>");
        addCode(dataXhtml, "\t\t\t<e:divDataMaster id=\"ddm" + nameMasterSemCaracterEspecial + "\"");
        addCode(dataXhtml, "\t\t\t\ttitle=\"#{e:bundle('label." + nameMasterSemCaracterEspecialMinusculo + "')}\"");
        addCode(dataXhtml, "\t\t\t\tactionData=\"#{" + nameMasterSemCaracterEspecialMinusculo + "DataController}\">");

        if (generateLogicCrud != null) {
            addCode(dataXhtml, generateXhtmlFromFieldsMaster(nameMaster, ProcessorUtils.getListFieldForm(generateLogicCrud.master().fields()), ENTITY, ""));
        }

        if (generateLogicMasterDetail != null) {
            addCode(dataXhtml, generateXhtmlFromFieldsMaster(nameMaster, ProcessorUtils.getListFieldForm(generateLogicMasterDetail.master().fields()), ENTITY, ""));
        }

        if (generateLogicMasterSubDetail != null) {
            addCode(dataXhtml, generateXhtmlFromFieldsMaster(nameMaster, ProcessorUtils.getListFieldForm(generateLogicMasterSubDetail.master().fields()), ENTITY, ""));
        }

        addCode(dataXhtml, "\t\t\t</e:divDataMaster>");

        if (generateLogicMasterDetail != null) {
            addDivDataDetail(dataXhtml);

            addCode(dataXhtml, "\t\t\t\t\t<a:tabView>");

            Arrays
                    .stream(generateLogicMasterDetail.details())
                    .forEach(d -> {
                        String nameSemCaracterEspecial = ProcessorUtils.getNameWithoutCharEspecialCapitalize(d.name());
                        String nameSemCaracterEspecialMinusculo = ProcessorUtils.getNameWithoutCharEspecialStartLowerCase(d.name());

                        String entity = DATA_DETAIL + nameSemCaracterEspecial + DOT_ENTITY;
                        String dataDetail = DATA_DETAIL + nameSemCaracterEspecial;
                        String actionDataDetail = nameController + "." + dataDetail;

                        addCode(dataXhtml, "" +
                                "\t\t\t\t\t\t<a:tab id=\"tab" + nameSemCaracterEspecial + "\">\n" +
                                "\t\t\t\t\t\t\t<f:facet name=\"title\">\n" +
                                "\t\t\t\t\t\t\t\t<e:tabTitle title=\"#{e:bundle('label." + nameSemCaracterEspecialMinusculo + "')}\" entityParent=\"#{" + nameController + DOT_ENTITY + "}\" dataDetail=\"#{" + actionDataDetail + "}\" idTab=\"tab" + nameSemCaracterEspecial + "\" update=\"#{a:component('tabBody" + nameSemCaracterEspecial + "')}\" hideInsert=\"#{" + nameController + ".blockedDetail}\"/>\n" +
                                "\t\t\t\t\t\t\t</f:facet>\n" +
                                "\t\t\t\t\t\t\t<e:tabBody id=\"tabBody" + nameSemCaracterEspecial + "\" actionData=\"#{" + nameController + "}\" dataDetail=\"#{" + actionDataDetail + "}\" idTab=\"tab" + nameSemCaracterEspecial + "\">");

                        addCode(dataXhtml, generateXhtmlFromFieldsDetail(d.name(), ProcessorUtils.getListFieldForm(d.fields()), entity, dataDetail));

                        addCode(dataXhtml, "\t\t\t\t\t\t\t</e:tabBody>");
                        addCode(dataXhtml, "\t\t\t\t\t\t</a:tab>");
                    });

            addCode(dataXhtml, "\t\t\t\t\t</a:tabView>");

            addCloseDivDataDetail(dataXhtml);
        }

        if (generateLogicMasterSubDetail != null) {
            addDivDataDetail(dataXhtml);

            addCode(dataXhtml, "\t\t\t\t\t<a:tabView>");

            Arrays
                    .stream(generateLogicMasterSubDetail.details())
                    .forEach(detail -> {
                        String nameSemCaracterEspecial = ProcessorUtils.getNameWithoutCharEspecialCapitalize(detail.name());
                        String nameSemCaracterEspecialMinusculo = ProcessorUtils.getNameWithoutCharEspecialStartLowerCase(detail.name());

                        String entity = DATA_DETAIL + nameSemCaracterEspecial + DOT_ENTITY;
                        String dataDetail = DATA_DETAIL + nameSemCaracterEspecial;
                        String actionDataDetail = nameController + "." + dataDetail;

                        addCode(dataXhtml, "" +
                                "\t\t\t\t\t\t<a:tab id=\"tab" + nameSemCaracterEspecial + "\">\n" +
                                "\t\t\t\t\t\t\t<f:facet name=\"title\">\n" +
                                "\t\t\t\t\t\t\t\t<e:tabTitle title=\"#{e:bundle('label." + nameSemCaracterEspecialMinusculo + "')}\" entityParent=\"#{" + nameController + DOT_ENTITY + "}\" dataDetail=\"#{" + actionDataDetail + "}\" idTab=\"tab" + nameSemCaracterEspecial + "\" update=\"#{a:component('tabBody" + nameSemCaracterEspecial + "')}\" hideInsert=\"#{" + nameController + ".blockedDetail}\"/>\n" +
                                "\t\t\t\t\t\t\t</f:facet>\n" +
                                "\t\t\t\t\t\t\t<e:tabBody id=\"tabBody" + nameSemCaracterEspecial + "\" actionData=\"#{" + nameController + "}\" dataDetail=\"#{" + actionDataDetail + "}\" idTab=\"tab" + nameSemCaracterEspecial + "\">");

                        addCode(dataXhtml, generateXhtmlFromFieldsDetail(detail.name(), ProcessorUtils.getListFieldForm(detail.fields()), entity, dataDetail));

                        Arrays
                                .stream(detail.subDetails())
                                .forEach(subDetail -> {
                                            String nameSubDetalheSemCaracterEspecial = ProcessorUtils.getNameWithoutCharEspecialCapitalize(subDetail.name());
                                            String nameSubDetalheSemCaracterEspecialMinusculo = ProcessorUtils.getNameWithoutCharEspecialStartLowerCase(subDetail.name());

                                            String entitySubDetalhe = DATA_DETAIL + nameSubDetalheSemCaracterEspecial + DOT_ENTITY;
                                            String dataDetailSubDetalhe = DATA_DETAIL + nameSubDetalheSemCaracterEspecial;
                                            String actionDataDetailSubDetalhe = nameController + "." + dataDetailSubDetalhe;

                                            addCode(dataXhtml, "" +
                                                    "\t\t\t\t\t\t\t\t<a:panelGrid columns=\"1\">\n" +
                                                    "\t\t\t\t\t\t\t\t\t<a:tabView>\n" +
                                                    "\t\t\t\t\t\t\t\t\t\t<a:tab id=\"tab" + nameSubDetalheSemCaracterEspecial + "\">\n" +
                                                    "\t\t\t\t\t\t\t\t\t\t\t<f:facet name=\"title\">\n" +
                                                    "\t\t\t\t\t\t\t\t\t\t\t\t<e:tabTitle title=\"#{e:bundle('label." + nameSubDetalheSemCaracterEspecialMinusculo + "')}\" entityParent=\"#{" + actionDataDetail + DOT_ENTITY + "}\" dataDetail=\"#{" + actionDataDetailSubDetalhe + "}\" idTab=\"tab" + nameSubDetalheSemCaracterEspecial + "\" update=\"#{a:component('tabBody" + nameSubDetalheSemCaracterEspecial + "')}\" hideInsert=\"#{" + nameController + ".blockedDetail}\"/>\n" +
                                                    "\t\t\t\t\t\t\t\t\t\t\t</f:facet>\n" +
                                                    "\t\t\t\t\t\t\t\t\t\t\t<e:tabBody id=\"tabBody" + nameSubDetalheSemCaracterEspecial + "\" actionData=\"#{" + nameController + "}\" dataDetail=\"#{" + actionDataDetailSubDetalhe + "}\" idTab=\"tab" + nameSubDetalheSemCaracterEspecial + "\" dataDetailParent=\"#{" + actionDataDetail + "}\">");

                                            addCode(dataXhtml, generateXhtmlFromFieldsSubDetail(subDetail.name(), ProcessorUtils.getListFieldForm(subDetail.fields()), entitySubDetalhe, dataDetailSubDetalhe));

                                            addCode(dataXhtml, "\t\t\t\t\t\t\t\t\t\t\t</e:tabBody>");
                                            addCode(dataXhtml, "\t\t\t\t\t\t\t\t\t\t</a:tab>");
                                            addCode(dataXhtml, "\t\t\t\t\t\t\t\t\t</a:tabView>");
                                            addCode(dataXhtml, "\t\t\t\t\t\t\t\t</a:panelGrid>");
                                        }
                                );

                        addCode(dataXhtml, "\t\t\t\t\t\t\t</e:tabBody>");
                        addCode(dataXhtml, "\t\t\t\t\t\t</a:tab>");
                    });

            addCode(dataXhtml, "\t\t\t\t\t</a:tabView>");
            addCloseDivDataDetail(dataXhtml);
        }

        addCode(dataXhtml, "\t\t</h:form>");
        addCode(dataXhtml, "\t</ui:define>");
        addCode(dataXhtml, "</ui:composition>");
        addCode(dataXhtml, "</html>");

        try {
            FileUtils.save(fileDataXhtml, dataXhtml.toString());
            ProcessorUtils.messageNote("JARCH Created ==> " + fileDataXhtml.getAbsoluteFile(), element);
        } catch (IOException e) {
            LogUtils.generate(e);
        }
    }

    private void addCloseDivDataDetail(StringBuilder sbListaXhtml) {
        addCode(sbListaXhtml, "\t\t\t\t</e:divDataDetail>");
        addCode(sbListaXhtml, "\t\t\t</f:validateBean>");
    }

    private void addDivDataDetail(StringBuilder sbListaXhtml) {
        addCode(sbListaXhtml, "\t\t\t<f:validateBean disabled=\"true\">");
        addCode(sbListaXhtml, "\t\t\t\t<e:divDataDetail title=\"#{e:bundle('label.manutencaoDetalhe')}\">");
    }

    private String generateXhtmlFromFieldsMaster(String name, List<FieldForm> formFields, String entity, String dataDetail) {
        return generateXhtmlFromFields(TabXhtmlType.MASTER, name, formFields, entity, dataDetail);
    }

    private String generateXhtmlFromFieldsDetail(String name, List<FieldForm> formFields, String entity, String dataDetail) {
        return generateXhtmlFromFields(TabXhtmlType.DETAIL, name, formFields, entity, dataDetail);
    }

    private String generateXhtmlFromFieldsSubDetail(String name, List<FieldForm> formFields, String entity, String dataDetail) {
        return generateXhtmlFromFields(TabXhtmlType.SUBDETAIL, name, formFields, entity, dataDetail);
    }

    private String generateXhtmlFromFields(TabXhtmlType tabXhtmlType, String name, List<FieldForm> formFields, String entity, String dataDetail) {
        boolean master = name == nameMaster;

        StringBuilder result = new StringBuilder();

        int maiorLinha = formFields
                .stream()
                .mapToInt(f -> f.getXhtml().stream().mapToInt(l -> l.getRowDataXhtml()).max().orElse(0))
                .max()
                .orElse(0);

        for (int linha = 1; linha <= maiorLinha; linha++) {

            int linhaComparacao = linha;

            List<FieldForm> formFieldsOrderByRowColumn = formFields
                    .stream()
                    .filter(f -> !f.getXhtml().isEmpty())
                    .filter(f -> f.getXhtml().stream().allMatch(l -> l.getRowDataXhtml() == linhaComparacao))
                    .sorted(Comparator.comparing((FieldForm f) -> f.getXhtml().get(0).getColumnDataXhtml()))
                    .collect(Collectors.toList());

            if (formFieldsOrderByRowColumn.isEmpty()) {
                continue;
            }

            String tabsDentro = tabXhtmlType.tabs();

            String stylePanelGrid = "";

            if (formFieldsOrderByRowColumn.stream().anyMatch(f -> f.getTipo() == FieldType.ADDRESS)) {
                stylePanelGrid = " style=\"width: 100%\"";
            }

            result.append(tabsDentro + "<a:panelGrid columns=\"" + formFieldsOrderByRowColumn.size() + "\"" + stylePanelGrid + ">\n");

            String fechandoTagPanelGrid = tabsDentro + "</a:panelGrid>";

            tabsDentro += "\t";

            for (FieldForm campoForm : formFieldsOrderByRowColumn) {
                result.append(tabsDentro);

                result.append("<h:panelGroup>\n");

                if (!FieldType.ENTITY.equals(campoForm.getTipo())
                        && !FieldType.MONEY.equals(campoForm.getTipo())
                        && !campoForm.getTipo().isAddress()) {
                    result.append(tabsDentro + "\t<a:outputLabel value=\"" + campoForm.getBundleJsf() + "\" for=\"" + campoForm.getGenerateId(nameMaster, dataDetail) + "\" ");

                    if (campoForm.getTipo().isNumber()) {
                        result.append("styleClass=\"outputlabel-inputnumber\"");
                    }

                    result.append("/>\n");

                    result.append(tabsDentro + "\t<br/>\n");
                }

                result.append(tabsDentro + "\t");

                result.append(campoForm.getTagXhtml(master, nameController, nameMasterSemCaracterEspecial, dataDetail, entity, tabsDentro));

                result.append(tabsDentro + "</h:panelGroup>\n");
            }

            result.append(fechandoTagPanelGrid + "\n");
        }

        return result.toString();
    }

    private void configure(Element element, String nameMaster, String nameSubPackage) {
        this.nameMaster = nameMaster;

        nameMasterSemCaracterEspecial = ProcessorUtils.getNameWithoutCharEspecialCapitalize(nameMaster);
        nameMasterSemCaracterEspecialMinusculo = ProcessorUtils.getNameWithoutCharEspecialStartLowerCase(nameMaster);

        nameController = nameMasterSemCaracterEspecialMinusculo + "DataController";

        File folder = new File(ProcessorUtils.getPathPage(element, nameSubPackage));

        if (!folder.exists()) {
            folder.mkdir();
        }

        fileDataXhtml = new File(ProcessorUtils.getPathPage(element, nameSubPackage) + nameMasterSemCaracterEspecialMinusculo + "Data.xhtml");
        fileDadosXhtml = new File(ProcessorUtils.getPathPage(element, nameSubPackage) + "dados" + nameMasterSemCaracterEspecial + ".xhtml");
    }
}</code></pre>    <br/>
    <br/>
<div class='clear'></div>
</main>
</div>
<br/><br/>
    <div class="align-center">© 2015 - 2024 <a href="/legal-notice.php">Weber Informatics LLC</a> | <a href="/data-protection.php">Privacy Policy</a></div>
<br/><br/><br/><br/><br/><br/>
</body>
</html>