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

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

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

import br.com.jarch.core.annotation.JArchGenerateCrud;
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.HashSet;
import java.util.Set;

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

public final class DataControllerCodeGenerate {

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

    private DataControllerCodeGenerate(Element element, JArchGenerateCrud generateCrud) {
        this.element = element;
        this.generateCrud = generateCrud;
        nomeSemCaracterEspecial = ProcessorUtils.getNameWithoutCharEspecialCapitalize(generateCrud.master().name());
        nomeSemCaracterEspecialMinusculo = ProcessorUtils.getNameWithoutCharEspecialStartLowerCase(generateCrud.master().name());

        File folder = new File(ProcessorUtils.getPathSource(element, generateCrud.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");
        namePackage = ProcessorUtils.getPackage(element, generateCrud.nameSubPackage()).replace(".client.", ".web.");
    }

    public static void generate(Element element, JArchGenerateCrud generateCrud) {
        new DataControllerCodeGenerate(element, generateCrud).generate();
    }

    private void generate() {
        if (ProcessorUtils.isFileExistsClientOrWebOrWs(fileDataController))
            return;

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


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

        Arrays
                .stream(generateCrud.master().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.core.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
                    .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 final String name;
    private final String field;
    private final 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}\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