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

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

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

import br.com.jarch.apt.type.CodeType;
import br.com.jarch.apt.type.ModuleType;
import br.com.jarch.core.annotation.JArchGenerateCrud;

import javax.lang.model.element.Element;

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

public class EmbeddableAddressCodeGenerate extends BaseCodeGenerate {

    protected boolean required;

    public static void generate(Element element, JArchGenerateCrud generateCrud, boolean required) {
        generate(element, generateCrud.master().name(), generateCrud.nameSubPackage(), required);
    }

    public static void generate(Element element, String useCase, String nameSubPackage, boolean required) {
        var embeddableAddressCodeGenerate = new EmbeddableAddressCodeGenerate(element, useCase, nameSubPackage);
        embeddableAddressCodeGenerate.required = required;
        embeddableAddressCodeGenerate.generate();
    }

    EmbeddableAddressCodeGenerate(Element element, String useCase, String nameSubPackage) {
        super(element, useCase, nameSubPackage);
    }

    @Override
    public ModuleType getModule() {
        return ModuleType.CLIENT;
    }

    @Override
    public CodeType getType() {
        return CodeType.EMBEDDABLE;
    }

    @Override
    public String getContent() {
        StringBuilder sbListCode = new StringBuilder();
        addPackage(sbListCode);
        addImports(sbListCode);
        addBody(sbListCode);
        return sbListCode.toString();
    }

    private void addPackage(StringBuilder sbListCode) {
        addCode(sbListCode, "package " + getNamePackage() + ";");
        addLineBlank(sbListCode);
    }

    private void addImports(StringBuilder sbListCode) {
        addCode(sbListCode, "import java.io.Serializable;");
        addCode(sbListCode, "import javax.persistence.Column;");
        addCode(sbListCode, "import javax.persistence.Embeddable;");
        addCode(sbListCode, "import javax.validation.constraints.Size;");
        addCode(sbListCode, "import br.com.jarch.core.model.IAddress;");
        addCode(sbListCode, "import br.com.jarch.core.annotation.JArchValidCep;");
        addCode(sbListCode, "import br.com.jarch.core.annotation.JArchValidRequired;");
        addLineBlank(sbListCode);
    }

    private void addBody(StringBuilder sbListCode) {
        addCode(sbListCode, "@Embeddable");
        addCode(sbListCode, "public class " + getName() + " implements IAddress, Serializable {");
        addLineBlank(sbListCode);
        addLinesFields(sbListCode);
        addLinesGetsSets(sbListCode);
        addCode(sbListCode, "}");
    }

    private void addLinesFields(StringBuilder sbListCode) {
        addLineField(sbListCode, "publicPlace", 100, "nm_logradouro");
        addLineField(sbListCode, "complement", 50, "ds_complemento");
        addLineField(sbListCode, "district", 100, "nm_bairro");
        addLineField(sbListCode, "county", 100, "nm_municipio");
        addLineField(sbListCode, "state", 2, "sg_uf");
        addLineField(sbListCode, "zipCode", 8, "nr_cep");
    }

    private void addLineField(StringBuilder sbListCode, String fieldName, int size, String columnName) {
        addCode(sbListCode, "\t@Column(name = \"" + columnName + "\", length = " + size + ")");

        if (fieldName.equals("zipCode"))
            addCode(sbListCode, "\t@JArchValidCep");
        else
            addCode(sbListCode, "\t@Size(max = " + size + ")");

        if (required)
            addCode(sbListCode, "\t@JArchValidRequired(\"label." + fieldName + "\")");

        addCode(sbListCode, "\tprivate String " + fieldName + ";");
        addLineBlank(sbListCode);
    }

    private void addLinesGetsSets(StringBuilder sbListCode) {
        addLinesGetSet(sbListCode, "publicPlace");
        addLinesGetSet(sbListCode, "complement");
        addLinesGetSet(sbListCode, "district");
        addLinesGetSet(sbListCode, "county");
        addLinesGetSet(sbListCode, "state");
        addLinesGetSet(sbListCode, "zipCode");
    }

    private void addLinesGetSet(StringBuilder sbListCode, String fieldName) {
        var partialNameMethod = fieldName.substring(0, 1).toUpperCase().concat(fieldName.substring(1));
        addCode(sbListCode, "\tpublic String get" + partialNameMethod + "() {");
        addCode(sbListCode, "\t\treturn " + fieldName + ";");
        addCode(sbListCode, "\t}");
        addLineBlank(sbListCode);
        addCode(sbListCode, "\tpublic void set" + partialNameMethod + "(String " + fieldName + ") {");
        addCode(sbListCode, "\t\tthis." + fieldName + " = " + fieldName + ";");
        addCode(sbListCode, "\t}");
        addLineBlank(sbListCode);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy