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

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

The newest version!
package br.com.jarch.apt.generate;

import br.com.jarch.core.annotation.JArchGenerateCrud;
import br.com.jarch.core.form.FieldForm;
import br.com.jarch.core.form.XhtmlFieldForm;
import br.com.jarch.util.FileUtils;
import br.com.jarch.util.LogUtils;
import br.com.jarch.apt.util.ProcessorUtils;
import br.com.jarch.core.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.Objects;
import java.util.stream.Collectors;

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

public final class DataXhtmlCodeGenerate {

    final String ENTITY = "entity";
    final String DATA_DETAIL = "dataDetail";
    final String DOT_ENTITY = ".entity";

    private final Element element;
    private final JArchGenerateCrud generateLogicCrud;
    private File fileDataXhtml;
    private File fileDadosXhtml;
    private String nameMaster;
    private String nameMasterSemCaracterEspecial;
    private String nameMasterSemCaracterEspecialMinusculo;
    private String nameController;

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

    public static void generate(Element element, JArchGenerateCrud generateLogicCrud) {
        new DataXhtmlCodeGenerate(element, generateLogicCrud).create();
    }

    private 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/templateData.xhtml\">");
        addCode(dataXhtml, "\t<ui:define name=\"panelBodyTemplateDados\">");
        addCode(dataXhtml, "\t\t<h:form id=\"frm" + nameMasterSemCaracterEspecial + "\">");
        addCode(dataXhtml, "\t\t\t<e:panelTitle");
        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:panelDataMaster id=\"ddm" + nameMasterSemCaracterEspecial + "\"");
        addCode(dataXhtml, "\t\t\t\ttitle=\"#{e:bundle('label." + nameMasterSemCaracterEspecialMinusculo + "')}\"");
        addCode(dataXhtml, "\t\t\t\tactionData=\"#{" + nameMasterSemCaracterEspecialMinusculo + "DataController}\">");

        addCode(dataXhtml, generateXhtmlFromFieldsMaster(nameMaster, ProcessorUtils.getListFieldForm(generateLogicCrud.master().fields()), ENTITY, ""));

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

        if (generateLogicCrud.master().details().length > 0) {
            addDivDataDetail(dataXhtml);

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

            Arrays
                    .stream(generateLogicCrud.master().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=\"#{e: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=\"#{e: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:panelDataDetail>");
        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:panelDataDetail 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 = Objects.equals(name, nameMaster);

        StringBuilder result = new StringBuilder();

        int maiorLinha = formFields
                .stream()
                .mapToInt(f -> f.getXhtml().stream().mapToInt(XhtmlFieldForm::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 id='pnlgrd" + linha + "' 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);

            if (linha != maiorLinha)
                result.append("\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>