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

br.com.jarch.apt.generate.DataControllerCodeGenerate 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.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.HashSet;
import java.util.Set;

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

public final class DataControllerCodeGenerate {

    private Element element;
    private File fileDataController;
    private File fileDataAction;
    private File fileDadosController;
    private String namePackage;
    private String nomeSemCaracterEspecial;
    private String nomeSemCaracterEspecialMinusculo;
    private Set dataDetails = new HashSet<>();
    private Set binarys = new HashSet<>();

    public DataControllerCodeGenerate(Element element, String nameMaster, String nameSubPackage) {
        this.element = element;
        nomeSemCaracterEspecial = ProcessorUtils.getNameWithoutCharEspecialCapitalize(nameMaster);
        nomeSemCaracterEspecialMinusculo = ProcessorUtils.getNameWithoutCharEspecialStartLowerCase(nameMaster);

        File folder = new File(ProcessorUtils.getPathSource(element, nameSubPackage).replace("-client", "-web").replace(File.separator + "client" + File.separator, File.separator + "web" + File.separator));

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

        fileDataController = new File(folder.getAbsolutePath(), nomeSemCaracterEspecial + "DataController.java");
        fileDataAction = new File(folder.getAbsolutePath(), nomeSemCaracterEspecial + "DataAction.java");
        fileDadosController = new File(folder.getAbsolutePath(), "Dados" + nomeSemCaracterEspecial + "Controller.java");
        namePackage = ProcessorUtils.getPackage(element, nameSubPackage);
        namePackage = namePackage.replace(".client.", ".web.");
    }

    public void createCrud(JArchGenerateLogicCrud generateLogicCrud) {
        if (ProcessorUtils.isFileExistsClientOrWebOrWs(fileDataController)
                || ProcessorUtils.isFileExistsClientOrWebOrWs(fileDataAction)
                || ProcessorUtils.isFileExistsClientOrWebOrWs(fileDadosController)) {
            return;
        }

        Arrays.stream(generateLogicCrud.master().fields()).filter(field -> FieldType.BINARY.equals(field.type())).forEach(field -> binarys.add(new BynaryFile(generateLogicCrud.master().name(), field.fieldName(), true)));

        create();
    }

    public void createMasterDetail(JArchGenerateLogicMasterDetail generateLogicMasterDetail) {
        if (ProcessorUtils.isFileExistsClientOrWebOrWs(fileDataController)) {
            return;
        }

        Arrays.stream(generateLogicMasterDetail.details()).forEach(d -> dataDetails.add(ProcessorUtils.getNameWithoutCharEspecialCapitalize(d.name())));

        Arrays
                .stream(generateLogicMasterDetail.master().fields())
                .filter(field -> FieldType.BINARY.equals(field.type()))
                .forEach(field -> binarys.add(new BynaryFile(generateLogicMasterDetail.master().name(), field.fieldName(), true)));

        Arrays
                .stream(generateLogicMasterDetail.details())
                .forEach(detail -> Arrays.stream(detail.fields())
                        .filter(field -> FieldType.BINARY.equals(field.type()))
                        .forEach(field -> binarys.add(new BynaryFile(detail.name(), field.fieldName(), false))));

        create();
    }

    public void createMasterDetailSubDetail(JArchGenerateLogicMasterSubDetail generateLogicMasterSubDetail) {
        if (ProcessorUtils.isFileExistsClientOrWebOrWs(fileDataController)) {
            return;
        }

        Arrays
                .stream(generateLogicMasterSubDetail.details())
                .forEach(d -> {
                    dataDetails.add(ProcessorUtils.getNameWithoutCharEspecialCapitalize(d.name()));
                    Arrays.stream(d.subDetails()).forEach(sd -> dataDetails.add(ProcessorUtils.getNameWithoutCharEspecialCapitalize(sd.name())));
                });


        Arrays
                .stream(generateLogicMasterSubDetail.master().fields())
                .filter(field -> FieldType.BINARY.equals(field.type()))
                .forEach(field -> binarys.add(new BynaryFile(generateLogicMasterSubDetail.master().name(), field.fieldName(), true)));

        Arrays
                .stream(generateLogicMasterSubDetail.details())
                .forEach(detail -> {
                            Arrays
                                    .stream(detail.fields())
                                    .filter(field -> FieldType.BINARY.equals(field.type()))
                                    .forEach(field -> binarys.add(new BynaryFile(detail.name(), field.fieldName(), false)));
                            Arrays
                                    .stream(detail.subDetails())
                                    .forEach(subDetail -> Arrays
                                            .stream(subDetail.fields())
                                            .filter(field -> FieldType.BINARY.equals(field.type()))
                                            .forEach(field -> binarys.add(new BynaryFile(subDetail.name(), field.fieldName(), false))));
                        }
                );

        create();
    }

    private void create() {
        StringBuilder sbListController = new StringBuilder();

        addCode(sbListController, "package " + namePackage + ";");
        addLineBlank(sbListController);
        addCode(sbListController, "import br.com.jarch.annotation.JArchDataController;");
        addCode(sbListController, "import br.com.jarch.faces.controller.CrudDataController;");

        if (!dataDetails.isEmpty()) {
            addCode(sbListController, "import br.com.jarch.faces.controller.CrudDataDetail;");
            addCode(sbListController, "import javax.inject.Inject;");
        }

        if (!binarys.isEmpty()) {
            addCode(sbListController, "import org.primefaces.event.FileUploadEvent;");
            addCode(sbListController, "import org.primefaces.model.file.UploadedFile;");
        }


        if (namePackage.contains(".web.")) {
            addCode(sbListController, "import " + namePackage.replace(".web.", ".client.") + "." + nomeSemCaracterEspecial + "Entity;");
            addCode(sbListController, "import " + namePackage.replace(".web.", ".client.") + "." + nomeSemCaracterEspecial + "Service;");
            addCode(sbListController, "import " + namePackage.replace(".web.", ".client.") + "." + nomeSemCaracterEspecial + "Repository;");

            dataDetails
                    .stream()
                    .forEach(d -> addCode(sbListController, "import " + namePackage.replace(".web.", ".client.") + "." + d + "Entity;"));
        }

        addLineBlank(sbListController);
        addCode(sbListController, "@JArchDataController");
        addCode(sbListController, "public class " + nomeSemCaracterEspecial + "DataController extends CrudDataController<" + nomeSemCaracterEspecial + "Entity, " + nomeSemCaracterEspecial + "Service, " + nomeSemCaracterEspecial + "Repository> {");
        addLineBlank(sbListController);

        dataDetails.forEach(d -> addCode(sbListController, "" +
                "\t@Inject \n" +
                "\tprivate CrudDataDetail<" + d + "Entity> dataDetail" + d + ";\n"
        ));

        binarys.forEach(b -> addCode(sbListController, b.getFieldInject()));

        addCode(sbListController, "\t@Override");
        addCode(sbListController, "\tpublic String getPageList() {");
        addCode(sbListController, "\t\treturn \"" + nomeSemCaracterEspecialMinusculo + "List.jsf\";");
        addCode(sbListController, "\t}");

        addLineBlank(sbListController);

        dataDetails.forEach(d -> addCode(sbListController, "" +
                "\tpublic CrudDataDetail<" + d + "Entity> getDataDetail" + d + "() {\n" +
                "\t\treturn dataDetail" + d + ";\n" +
                "\t}\n"
        ));

        binarys.forEach(b -> addCode(sbListController, b.getMethodGetterSetter()));

        addCode(sbListController, "}");

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

class BynaryFile {

    private String name;
    private String field;
    private boolean master;

    BynaryFile(String name, String field, boolean master) {
        this.name = name;
        this.field = field;
        this.master = master;
    }

    String getFieldInject() {
        return "\tprivate UploadedFile " + getFieldNameFile() + ";\n";
    }

    String getMethodGetterSetter() {
        return "" +
                "\tpublic UploadedFile " + getMethodGetterFile() + "() {\n" +
                "\t\treturn " + getFieldNameFile() + ";\n" +
                "\t}\n" +
                "\n" +
                "\tpublic void " + getMethodSetterFile() + "(UploadedFile " + getFieldNameFile() + ") {\n" +
                "\t\tthis." + getFieldNameFile() + " = " + getFieldNameFile() + ";\n" +
//                "\t\t" + getSetterEntity() + "\n" +
                "\t}\n" +
                "\n" +
                "\tpublic void fileUploadListener" + ProcessorUtils.getNameWithoutCharEspecialCapitalize(name) + field.substring(0, 1).toUpperCase().concat(field.substring(1)) + "(FileUploadEvent event) {\n" +
                "\t\t" + getSetterEntity() + "\n" +
                "\t}\n";
    }

    private String getSetterEntity() {
        return (
                master
                        ? "getEntity().set" + field.substring(0, 1).toUpperCase().concat(field.substring(1))
                        : "dataDetail" + ProcessorUtils.getNameWithoutCharEspecialCapitalize(name) + ".getEntity().set" + field.substring(0, 1).toUpperCase().concat(field.substring(1))
        )
                + "(event.getFile().getContent());";
    }


    private String getFieldNameFile() {
        return "file" + ProcessorUtils.getNameWithoutCharEspecialCapitalize(name) + field.substring(0, 1).toUpperCase() + field.substring(1);
    }

    private String getMethodGetterFile() {
        return "getFile" + ProcessorUtils.getNameWithoutCharEspecialCapitalize(name) + field.substring(0, 1).toUpperCase() + field.substring(1);
    }

    private String getMethodSetterFile() {
        return "setFile" + ProcessorUtils.getNameWithoutCharEspecialCapitalize(name) + field.substring(0, 1).toUpperCase() + field.substring(1);
    }

}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy