br.com.jarch.apt.generate.PackageInfoCodeGenerate 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.SearchFieldForm;
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.apt.type.ModuleType;
import javax.lang.model.element.Element;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
import static br.com.jarch.apt.util.ProcessorUtils.addCode;
import static br.com.jarch.apt.util.ProcessorUtils.addLineBlank;
public final class PackageInfoCodeGenerate {
private final Element element;
private String nameSubPackage;
private final JArchGenerateCrud generateCrud;
private File filePackageInfo;
private String namePackage;
private boolean importArchSearchFieldCombobox;
private boolean importArchSearchFieldLookup;
private boolean importCondition;
private final Set imports = new HashSet<>();
PackageInfoCodeGenerate(Element element, JArchGenerateCrud generateCrud) {
this.element = element;
this.generateCrud = generateCrud;
configure(element, generateCrud.nameSubPackage());
}
public static void generate(Element element, JArchGenerateCrud generateCrud) {
new PackageInfoCodeGenerate(element, generateCrud).generate();
}
private void generate() {
if (ProcessorUtils.isFileExistsClientOrWebOrWs(filePackageInfo))
return;
StringBuilder sbPackageInfo = new StringBuilder();
addCode(sbPackageInfo, searchFields(generateCrud.master().name(), ProcessorUtils.getListFieldForm(generateCrud.master().fields())));
addCode(sbPackageInfo, columnsDataTable(generateCrud.master().name(), ProcessorUtils.getListFieldForm(generateCrud.master().fields())));
Arrays
.stream(generateCrud.master().details())
.forEach(detail -> {
addCode(sbPackageInfo, searchFields(detail.name(), ProcessorUtils.getListFieldForm(detail.fields())));
addCode(sbPackageInfo, columnsDataTable(detail.name(), ProcessorUtils.getListFieldForm(detail.fields())));
Arrays
.stream(detail.subDetails())
.forEach(subDetail -> {
addCode(sbPackageInfo, searchFields(subDetail.name(), ProcessorUtils.getListFieldForm(subDetail.fields())));
addCode(sbPackageInfo, columnsDataTable(subDetail.name(), ProcessorUtils.getListFieldForm(subDetail.fields())));
});
});
addLineBlank(sbPackageInfo);
addCode(sbPackageInfo, "package " + namePackage + ";");
addLineBlank(sbPackageInfo);
addCode(sbPackageInfo, "import br.com.jarch.core.annotation.JArchColumnDataTable;");
addCode(sbPackageInfo, "import br.com.jarch.core.annotation.JArchColumnsDataTable;");
addCode(sbPackageInfo, "import br.com.jarch.core.type.FieldType;");
addCode(sbPackageInfo, "import br.com.jarch.core.annotation.JArchSearchField;");
if (importArchSearchFieldCombobox)
addCode(sbPackageInfo, "import br.com.jarch.core.annotation.JArchSearchFieldCombobox;");
if (importArchSearchFieldLookup)
addCode(sbPackageInfo, "import br.com.jarch.core.annotation.JArchSearchFieldLookup;");
if (importCondition)
addCode(sbPackageInfo, "import br.com.jarch.core.type.ConditionSearchType;");
addCode(sbPackageInfo, "import br.com.jarch.core.annotation.JArchSearchTab;");
imports.forEach(i -> addCode(sbPackageInfo, i));
try {
FileUtils.save(filePackageInfo, sbPackageInfo.toString().replace("\n\n", "\n"));
ProcessorUtils.messageNote("JARCH Created ==> " + filePackageInfo.getAbsoluteFile(), element);
} catch (IOException e) {
LogUtils.generate(e);
}
}
private String searchFields(String name, List fieldForms) {
String nameEntity = ProcessorUtils.getNomeEntity(name);
StringBuilder searchFields = new StringBuilder();
StringBuilder searchComboFields = new StringBuilder();
StringBuilder searchLookFields = new StringBuilder();
for (FieldForm campoForm : new ArrayList<>(fieldForms)) {
for (SearchFieldForm search : campoForm.getSearch().stream().sorted(Comparator.comparing(SearchFieldForm::getRow).thenComparing(SearchFieldForm::getColumn)).collect(Collectors.toList())) {
imports.add("import " + namePackage.replace(".web.", ".client.") + "." + nameEntity + ";");
if (campoForm.getTipo().isEnum()) {
importArchSearchFieldCombobox = true;
searchComboFields.append(
"\t\t@JArchSearchFieldCombobox(field = \"" + campoForm.getAtributo() + "\", " +
"label = \"" + campoForm.getBundleCodigoJava() + "\", " +
"fieldItems = \"" + campoForm.getAtributo() + "s\", " +
"fieldShow = \"description\", " +
"row = " + search.getRow() + ", " +
"column = " + search.getColumn() + ", " +
"span = " + (search.getSpan() == 0 ? campoForm.getTipo().getSpan() : search.getSpan()) + "), \n");
} else if (campoForm.getTipo().isEntity()) {
importArchSearchFieldLookup = true;
String entityLookup = campoForm.getAtributoPrimeiroLetraMaiusculo();
searchLookFields.append(
"\t\t@JArchSearchFieldLookup(field = \"" + campoForm.getAtributo() + "\", " +
"label = \"" + campoForm.getBundleCodigoJava() + "\", " +
"classEntityLookup = " + entityLookup + "Entity.class, " +
"row = " + search.getRow() + ", " +
"column = " + (search.getColumn()) + ", " +
"span = " + (search.getSpan() == 0 ? campoForm.getTipo().getSpan() : search.getSpan()) + "), \n");
imports.add("import " + namePackage.replace(nameSubPackage.toLowerCase(), entityLookup.toLowerCase()) + "." + entityLookup + "Entity;");
} else {
int quantityFieldsBeetwen = campoForm.getTipo().isNumber() || campoForm.getTipo().isDateTime() ? 2 : 1;
for (int fieldActual = 0; fieldActual < quantityFieldsBeetwen; fieldActual++) {
boolean first = fieldActual == 0;
String condition = "";
if (quantityFieldsBeetwen > 1) {
if (!campoForm.getSearch().isEmpty() && first)
condition = "condition = ConditionSearchType.LARGER_EQUAL, ";
else if (!campoForm.getSearch().isEmpty() && !first)
condition = "condition = ConditionSearchType.LESS_EQUAL, ";
}
if (!condition.isEmpty())
importCondition = true;
searchFields.append(
"\t\t@JArchSearchField(field = \"" + campoForm.getAtributo() + "\", " +
"label = \"" + campoForm.getBundleCodigoJava() + "\", " +
"type = FieldType." + campoForm.getTipo() + ", " +
condition +
"row = " + search.getRow() + ", " +
"column = " + (search.getColumn() + fieldActual) + ", " +
"span = " + (search.getSpan() == 0 ? campoForm.getTipo().getSpan() : search.getSpan()) + "), \n");
}
}
}
}
String sectionFieldsSearchs = String.format("" +
"//##################################################################################################################\n" +
"//### %s - Configuration of search fields and result fields\n" +
"//##################################################################################################################",
nameEntity);
sectionFieldsSearchs += "\n@JArchSearchTab(order = 1, classEntity = " + nameEntity + ".class";
if (!searchFields.toString().isEmpty())
sectionFieldsSearchs += ",\n\tsearchFields = {\n" + searchFields + "\t}";
if (!searchComboFields.toString().isEmpty())
sectionFieldsSearchs += ",\n\tsearchFieldComboboxes = {\n" + searchComboFields + "\t}";
if (!searchLookFields.toString().isEmpty())
sectionFieldsSearchs += ",\n\tsearchFieldLookups = {\n" + searchLookFields + "\t}";
sectionFieldsSearchs += ")";
return sectionFieldsSearchs;
}
private String columnsDataTable(String name, List fieldForms) {
String nameEntity = ProcessorUtils.getNomeEntity(name);
StringBuilder result = new StringBuilder("@JArchColumnsDataTable(classEntity = " + nameEntity + ".class");
boolean hasFieldColumn = false;
for (FieldForm campoForm : fieldForms) {
for (XhtmlFieldForm xhtml : campoForm.getXhtml()) {
if (xhtml.isShowDataTableList()) {
String nomeAtributo = campoForm.getAtributo();
if (!result.toString().contains("JArchColumnDataTable(field"))
result.append(",\n\tcolumns = {\n");
result.append(
"\t\t@JArchColumnDataTable(field =\"" + nomeAtributo + (campoForm.getTipo().isEnum() ? ".description" : "") + "\", " +
"title = \"" + campoForm.getBundleCodigoJava() + "\", " +
"width = " + (xhtml.getWidth() == 0 ? campoForm.getTipo().getWidthXhtml() : xhtml.getWidth()) + ", " +
"type = FieldType." + campoForm.getTipo().name().replace("ENTITY", "NAME") + "),\n");
hasFieldColumn = true;
}
}
}
if (result.toString().contains("JArchColumnDataTable(field"))
result.append("\t}");
result.append(")");
if (hasFieldColumn)
return result.toString();
else
return "";
}
private void configure(Element element, String nameSubPackage) {
namePackage = ModuleType.WEB.getNamePackage(element, nameSubPackage);
this.nameSubPackage = nameSubPackage;
File folder = ModuleType.WEB.getFolder(element, nameSubPackage);
if (!folder.exists()) {
folder.mkdir();
}
filePackageInfo = new File(folder.getAbsolutePath() + File.separator + "package-info.java");
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy