br.com.jarch.apt.generate.PackageInfoCodeGenerate Maven / Gradle / Ivy
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.form.FieldForm;
import br.com.jarch.form.SearchFieldForm;
import br.com.jarch.form.XhtmlFieldForm;
import br.com.jarch.util.FileUtils;
import br.com.jarch.util.LogUtils;
import br.com.jarch.util.ProcessorUtils;
import br.com.jarch.util.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.util.ProcessorUtils.addCode;
import static br.com.jarch.util.ProcessorUtils.addLineBlank;
public final class PackageInfoCodeGenerate {
private final Element element;
private String nameSubPackage;
private JArchGenerateLogicCrud generateLogicCrud;
private JArchGenerateLogicMasterDetail generateLogicMasterDetail;
private JArchGenerateLogicMasterSubDetail generateLogicMasterSubDetail;
private File filePackageInfo;
private String namePackage;
private boolean importArchSearchFieldCombobox;
private boolean importArchSearchFieldLookup;
private boolean importCondition;
private final Set imports = new HashSet<>();
public PackageInfoCodeGenerate(Element element, JArchGenerateLogicCrud generateLogicCrud) {
this.element = element;
this.generateLogicCrud = generateLogicCrud;
configure(element, generateLogicCrud.nameSubPackage());
}
public PackageInfoCodeGenerate(Element element, JArchGenerateLogicMasterDetail generateLogicMasterDetail) {
this.element = element;
this.generateLogicMasterDetail = generateLogicMasterDetail;
configure(element, generateLogicMasterDetail.nameSubPackage());
}
public PackageInfoCodeGenerate(Element element, JArchGenerateLogicMasterSubDetail generateLogicMasterSubDetail) {
this.element = element;
this.generateLogicMasterSubDetail = generateLogicMasterSubDetail;
configure(element, generateLogicMasterSubDetail.nameSubPackage());
}
public void create() {
if (ProcessorUtils.isFileExistsClientOrWebOrWs(filePackageInfo))
return;
StringBuilder sbPackageInfo = new StringBuilder();
if (generateLogicCrud != null) {
addCode(sbPackageInfo, searchFields(generateLogicCrud.master().name(), ProcessorUtils.getListFieldForm(generateLogicCrud.master().fields())));
addCode(sbPackageInfo, columnsDataTable(generateLogicCrud.master().name(), ProcessorUtils.getListFieldForm(generateLogicCrud.master().fields())));
}
if (generateLogicMasterDetail != null) {
addCode(sbPackageInfo, searchFields(generateLogicMasterDetail.master().name(), ProcessorUtils.getListFieldForm(generateLogicMasterDetail.master().fields())));
addCode(sbPackageInfo, columnsDataTable(generateLogicMasterDetail.master().name(), ProcessorUtils.getListFieldForm(generateLogicMasterDetail.master().fields())));
Arrays
.stream(generateLogicMasterDetail.details())
.forEach(detail -> {
addCode(sbPackageInfo, searchFields(detail.name(), ProcessorUtils.getListFieldForm(detail.fields())));
addCode(sbPackageInfo, columnsDataTable(detail.name(), ProcessorUtils.getListFieldForm(detail.fields())));
});
}
if (generateLogicMasterSubDetail != null) {
addCode(sbPackageInfo, searchFields(generateLogicMasterSubDetail.master().name(), ProcessorUtils.getListFieldForm(generateLogicMasterSubDetail.master().fields())));
addCode(sbPackageInfo, columnsDataTable(generateLogicMasterSubDetail.master().name(), ProcessorUtils.getListFieldForm(generateLogicMasterSubDetail.master().fields())));
Arrays
.stream(generateLogicMasterSubDetail.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.annotation.JArchColumnDataTable;");
addCode(sbPackageInfo, "import br.com.jarch.annotation.JArchColumnsDataTable;");
addCode(sbPackageInfo, "import br.com.jarch.util.type.FieldType;");
addCode(sbPackageInfo, "import br.com.jarch.annotation.JArchSearchField;");
if (importArchSearchFieldCombobox) {
addCode(sbPackageInfo, "import br.com.jarch.annotation.JArchSearchFieldCombobox;");
}
if (importArchSearchFieldLookup) {
addCode(sbPackageInfo, "import br.com.jarch.annotation.JArchSearchFieldLookup;");
}
if (importCondition) {
addCode(sbPackageInfo, "import br.com.jarch.util.type.ConditionSearchType;");
}
addCode(sbPackageInfo, "import br.com.jarch.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 +=
"@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 = new File(ProcessorUtils.getPathSource(element, nameSubPackage));
File folder = ModuleType.WEB.getFolder(element, nameSubPackage);
if (!folder.exists()) {
folder.mkdir();
}
filePackageInfo = new File(folder.getAbsolutePath() + File.separator + "package-info.java");
// filePackageInfo = new File(ProcessorUtils.getPathSource(element, nameSubPackage) + "package-info.java");
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy