br.com.jarch.apt.generate.JpaConverterCodeGenerate 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 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 JpaConverterCodeGenerate extends BaseCodeGenerate {
public static void generate(Element element, String useCase, String nameSubPackage) {
new JpaConverterCodeGenerate(element, useCase, nameSubPackage).generate();
}
JpaConverterCodeGenerate(Element element, String useCase, String nameSubPackage) {
super(element, useCase, nameSubPackage);
}
@Override
public ModuleType getModule() {
return ModuleType.CLIENT;
}
@Override
public CodeType getType() {
return CodeType.JPA_CONVERTER;
}
@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 javax.persistence.AttributeConverter;");
addCode(sbListCode, "import javax.persistence.Converter;");
addLineBlank(sbListCode);
}
private void addBody(StringBuilder sbListCode) {
addCode(sbListCode, "@Converter(autoApply = true)");
addCode(sbListCode, "public class " + getName() + " implements AttributeConverter<" + getNameType() + ", String> {");
addLineBlank(sbListCode);
addCode(sbListCode, "\t@Override");
addCode(sbListCode, "\tpublic String convertToDatabaseColumn(" + getNameType() + " valueEnum) {");
addCode(sbListCode, "\t\treturn valueEnum == null ? null : valueEnum.getAbbreviation();");
addCode(sbListCode, "\t}");
addLineBlank(sbListCode);
addCode(sbListCode, "\t@Override");
addCode(sbListCode, "\tpublic " + getNameType() + " convertToEntityAttribute(String valueDataBase) {");
addCode(sbListCode, "\t\treturn valueDataBase == null ? null : " + getNameType() + ".abbreviationToEnum(valueDataBase);");
addCode(sbListCode, "\t}");
addCode(sbListCode, "}");
}
private String getNameType() {
return getName().replace(getType().getSuffixName(), "Type");
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy