From ab33f23ca0b23a0ace1211b820dd07144cc09ff0 Mon Sep 17 00:00:00 2001 From: yamelsenih Date: Thu, 16 May 2024 20:07:07 -0400 Subject: [PATCH] Add some stub values --- .../report_engine/format/ColumnReference.java | 54 +++++++ .../report_engine/format/PrintFormat.java | 148 ++++++++++++++++-- .../format/PrintFormatColumn.java | 112 +++++++++++++ .../report_engine/format/QueryDefinition.java | 130 +++++++++++++++ .../report_engine/service/ReportBuilder.java | 15 +- 5 files changed, 438 insertions(+), 21 deletions(-) create mode 100644 src/main/java/org/spin/report_engine/format/PrintFormatColumn.java create mode 100644 src/main/java/org/spin/report_engine/format/QueryDefinition.java diff --git a/src/main/java/org/spin/report_engine/format/ColumnReference.java b/src/main/java/org/spin/report_engine/format/ColumnReference.java index 1f07467..587d020 100644 --- a/src/main/java/org/spin/report_engine/format/ColumnReference.java +++ b/src/main/java/org/spin/report_engine/format/ColumnReference.java @@ -18,6 +18,9 @@ import java.sql.ResultSet; import java.sql.SQLException; import org.compiere.util.DB; +import org.compiere.util.DisplayType; +import org.compiere.util.Env; +import org.compiere.util.Language; /** * A stub class for reference of column to table @@ -84,6 +87,57 @@ public ColumnReference withIsTranslated(boolean isTranslated) { this.isTranslated = isTranslated; return this; } + + /** + * Get reference from language + * @param language + * @return + */ + public static ColumnReference getColumnReferenceList(Language language) { + ColumnReference columnReference = ColumnReference.newInstance() + .withDisplayColumn("Name") + .withKeyColumn("Value") + .withIsValueDisplayed(false) + .withTableName("AD_Ref_List"); + if(!Env.isBaseLanguage(language, "AD_Ref_List")) { + columnReference.withIsTranslated(true); + } + return columnReference; + } + + /** + * Get Column Reference Special + * @param referenceId + * @return + */ + public static ColumnReference getColumnReferenceSpecial(int referenceId) { + if (referenceId == DisplayType.Location) { + return ColumnReference.newInstance() + .withDisplayColumn("City||'.'") + .withKeyColumn("C_Location_ID") + .withIsValueDisplayed(false) + .withTableName("C_Location"); + } else if (referenceId == DisplayType.Account) { + return ColumnReference.newInstance() + .withDisplayColumn("Combination") + .withKeyColumn("C_ValidCombination_ID") + .withIsValueDisplayed(false) + .withTableName("C_ValidCombination"); + } else if (referenceId == DisplayType.Locator) { + return ColumnReference.newInstance() + .withDisplayColumn("Value") + .withKeyColumn("M_Locator_ID") + .withIsValueDisplayed(false) + .withTableName("M_Locator"); + } else if (referenceId == DisplayType.PAttribute) { + return ColumnReference.newInstance() + .withDisplayColumn("Description") + .withKeyColumn("M_AttributeSetInstance_ID") + .withIsValueDisplayed(false) + .withTableName("M_AttributeSetInstance"); + } + return null; + } /** * Get TableName and ColumnName for Reference Tables. diff --git a/src/main/java/org/spin/report_engine/format/PrintFormat.java b/src/main/java/org/spin/report_engine/format/PrintFormat.java index 507df94..9799624 100644 --- a/src/main/java/org/spin/report_engine/format/PrintFormat.java +++ b/src/main/java/org/spin/report_engine/format/PrintFormat.java @@ -29,6 +29,7 @@ import org.compiere.util.DisplayType; import org.compiere.util.Env; import org.compiere.util.Language; +import org.compiere.util.Util; /** * Print Format Representation @@ -111,32 +112,28 @@ public List getItems() { return items; } - private String getQueryColumnName(String columnName) { - return getTableName() + "." + columnName; - } - - private String getQueryReferenceColumnName(String columnName) { - return getTableAlias() + "." + columnName; - } - - public String getQuery() { + public QueryDefinition getQuery() { clearTableAlias(); StringBuffer query = new StringBuffer(); + StringBuffer orderBy = new StringBuffer(); StringBuffer tableReferences = new StringBuffer(); Language language = Language.getLoginLanguage(); + List columns = new ArrayList(); getItems().stream() .filter(item -> item.isActive() && item.isPrinted()) .sorted(Comparator.comparing(PrintFormatItem::getSequence)) .forEach(item -> { if(item.getColumnId() > 0) { + String columnName = null; if(query.length() > 0) { query.append(", "); } if(item.isVirtualColumn()) { - query.append(item.getColumnSql()); + columnName = item.getColumnSql(); } else { - query.append("(").append(getQueryColumnName(item.getColumnName())).append(")"); + columnName = getQueryColumnName(item.getColumnName()); } + query.append("(").append(columnName).append(")"); query.append(" AS ").append(item.getColumnName()); // Process Display Value if(item.getReferenceId() == DisplayType.TableDir @@ -145,12 +142,11 @@ public String getQuery() { query.append(", "); } if(item.isVirtualColumn()) { - String sqlDirect = MLookupFactory.getLookup_TableDirEmbed(language, item.getColumnName(), getTableName(), "(" + item.getColumnSql() + ")"); - query.append("(").append(sqlDirect).append(")"); + columnName = MLookupFactory.getLookup_TableDirEmbed(language, item.getColumnName(), getTableName(), "(" + item.getColumnSql() + ")"); } else { - String sqlDirect = MLookupFactory.getLookup_TableDirEmbed(language, item.getColumnName(), getTableName()); - query.append("(").append(sqlDirect).append(")"); + columnName = MLookupFactory.getLookup_TableDirEmbed(language, item.getColumnName(), getTableName()); } + query.append("(").append(columnName).append(")"); query.append(" AS ").append(getDisplayColumnName(item)); } else if(item.getReferenceId() == DisplayType.Table || (item.getReferenceId() == DisplayType.Search && item.getReferenceValueId() != 0)) { @@ -167,6 +163,44 @@ public String getQuery() { if(query.length() > 0) { query.append(", "); } + columnName = displayColumnValue.toString(); + query.append("(").append(displayColumnValue).append(")"); + query.append(" AS ").append(getDisplayColumnName(item)); + // Add JOIN + if(item.isMandatory()) { + tableReferences.append(" INNER JOIN "); + } else { + tableReferences.append(" LEFT OUTER JOIN "); + } + tableReferences.append(columnReference.getTableName()).append(" ").append(getTableAlias()).append(" ON (") + .append(getQueryReferenceColumnName(columnReference.getKeyColumn())).append("=").append(getQueryColumnName(item.getColumnName())).append(")"); + if(columnReference.isIsTranslated()) { + tableReferences.append(" LEFT OUTER JOIN ") + .append(columnReference.getTableName()).append("_Trl").append(" ").append(getTableAliasTrl()).append(" ON (") + .append(getQueryReferenceColumnNameTrl(columnReference.getKeyColumn())).append(" = ").append(getQueryColumnName(item.getColumnName())) + // Reference ID + .append(getLanguageCriteria(language)).append(")"); + } + } else if(item.getReferenceId() == DisplayType.List + || (item.getReferenceId() == DisplayType.Button && item.getReferenceValueId() != 0)) { + addTableAlias(); + ColumnReference columnReference = ColumnReference.getColumnReferenceList(language); + StringBuffer displayColumnValue = new StringBuffer(); + if(columnReference.isIsValueDisplayed()) { + displayColumnValue.append(getQueryReferenceColumnName("Value")); + } + if(displayColumnValue.length() > 0) { + displayColumnValue.append("|| '_' ||"); + } + if(columnReference.isIsTranslated()) { + displayColumnValue.append("COALESCE(").append(getQueryReferenceColumnNameTrl(columnReference.getDisplayColumn())).append(", ").append(getQueryReferenceColumnName(columnReference.getDisplayColumn())).append(")"); + } else { + displayColumnValue.append(getQueryReferenceColumnName(columnReference.getDisplayColumn())); + } + if(query.length() > 0) { + query.append(", "); + } + columnName = displayColumnValue.toString(); query.append("(").append(displayColumnValue).append(")"); query.append(" AS ").append(getDisplayColumnName(item)); // Add JOIN @@ -176,8 +210,61 @@ public String getQuery() { tableReferences.append(" LEFT OUTER JOIN "); } tableReferences.append(columnReference.getTableName()).append(" ").append(getTableAlias()).append(" ON (") - .append(getQueryColumnName(item.getColumnName())).append("=").append(getTableAlias()).append(".").append(columnReference.getKeyColumn()).append(")"); + .append(getQueryColumnName(item.getColumnName())).append(" = ").append(getTableAlias()).append(".").append(columnReference.getKeyColumn()) + // Reference ID + .append(" AND ").append(getTableAlias()).append(".").append("AD_Reference_ID").append(" = ").append(item.getReferenceValueId()).append(")"); + if(columnReference.isIsTranslated()) { + tableReferences.append(" LEFT OUTER JOIN ") + .append(columnReference.getTableName()).append("_Trl").append(" ").append(getTableAliasTrl()).append(" ON (") + .append(getQueryReferenceColumnNameTrl("AD_Ref_List_ID")).append(" = ").append(getQueryReferenceColumnName("AD_Ref_List_ID")) + // Reference ID + .append(getLanguageCriteria(language)).append(")"); + } + } else if (item.getReferenceId() == DisplayType.Location + || item.getReferenceId() == DisplayType.Account + || item.getReferenceId() == DisplayType.Locator + || item.getReferenceId() == DisplayType.PAttribute + ) { + ColumnReference columnReference = ColumnReference.getColumnReferenceSpecial(item.getReferenceId()); + if(columnReference != null) { + addTableAlias(); + StringBuffer displayColumnValue = new StringBuffer(); + if(columnReference.isIsValueDisplayed()) { + displayColumnValue.append(getQueryReferenceColumnName("Value")); + } + if(displayColumnValue.length() > 0) { + displayColumnValue.append("|| '_' ||"); + } + displayColumnValue.append(getQueryReferenceColumnName(columnReference.getDisplayColumn())); + if(query.length() > 0) { + query.append(", "); + } + columnName = displayColumnValue.toString(); + query.append("(").append(displayColumnValue).append(")"); + query.append(" AS ").append(getDisplayColumnName(item)); + // Add JOIN + if(item.isMandatory()) { + tableReferences.append(" INNER JOIN "); + } else { + tableReferences.append(" LEFT OUTER JOIN "); + } + tableReferences.append(columnReference.getTableName()).append(" ").append(getTableAlias()).append(" ON (") + .append(getQueryReferenceColumnName(columnReference.getKeyColumn())).append("=").append(getQueryColumnName(item.getColumnName())).append(")"); + } + } + // For Order By + if(item.isOrderBy()) { + if(!Util.isEmpty(columnName)) { + if(orderBy.length() > 0) { + orderBy.append(", "); + } + orderBy.append(columnName); + if(item.isDesc()) { + orderBy.append(" DESC"); + } + } } + columns.add(PrintFormatColumn.newInstance(item).withColumnNameAlias(getDisplayColumnName(item))); } }); if(query.length() > 0) { @@ -187,13 +274,40 @@ public String getQuery() { query.append(tableReferences); } } - return query.toString(); + // Return definition + return QueryDefinition.newInstance() + .withQuery(query.toString()) + .withOrderBy(orderBy.toString()) + .withColumns(columns); + } + + private String getQueryColumnName(String columnName) { + return getTableName() + "." + columnName; + } + + private String getQueryReferenceColumnName(String columnName) { + return getTableAlias() + "." + columnName; + } + + private String getQueryReferenceColumnNameTrl(String columnName) { + return getTableAliasTrl() + "." + columnName; + } + + private String getLanguageCriteria(Language language) { + if(!Env.isBaseLanguage(language, "AD_Ref_List")) { + return " AND " + getTableAliasTrl() + ".AD_Language = '" + language.getAD_Language() + "'"; + } + return ""; } private String getTableAlias() { return "t" + aliasNumber; } + private String getTableAliasTrl() { + return "tt" + aliasNumber; + } + private void addTableAlias() { aliasNumber ++; } diff --git a/src/main/java/org/spin/report_engine/format/PrintFormatColumn.java b/src/main/java/org/spin/report_engine/format/PrintFormatColumn.java new file mode 100644 index 0000000..4bae92b --- /dev/null +++ b/src/main/java/org/spin/report_engine/format/PrintFormatColumn.java @@ -0,0 +1,112 @@ +/************************************************************************************ + * Copyright (C) 2012-2018 E.R.P. Consultores y Asociados, C.A. * + * Contributor(s): Yamel Senih ysenih@erpya.com * + * This program is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, either version 2 of the License, or * + * (at your option) any later version. * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License * + * along with this program. If not, see . * + ************************************************************************************/ +package org.spin.report_engine.format; + +import org.compiere.util.Util; + +/** + * Print Format Line Representation + * @author Yamel Senih, ysenih@erpya.com, ERPCyA http://www.erpya.com + */ +public class PrintFormatColumn { + + private int printFormatItemId; + // Column attributes + private String columnName; + private String columnNameAlias; + private int columnId; + private int referenceId; + private int referenceValueId; + private boolean isKey; + private boolean isParent; + private boolean isMandatory; + private boolean isVirtualColumn; + private String columnSql; + + private PrintFormatColumn(PrintFormatItem printFormatItem) { + printFormatItemId = printFormatItem.getPrintFormatItemId(); + columnName = printFormatItem.getColumnName(); + columnId = printFormatItem.getColumnId(); + referenceId = printFormatItem.getReferenceId(); + referenceValueId = printFormatItem.getReferenceValueId(); + isKey = printFormatItem.isKey(); + isParent = printFormatItem.isParent(); + isMandatory = printFormatItem.isMandatory(); + columnSql = printFormatItem.getColumnSql(); + isVirtualColumn = !Util.isEmpty(printFormatItem.getColumnSql()); + } + + public static PrintFormatColumn newInstance(PrintFormatItem printFormatItem) { + return new PrintFormatColumn(printFormatItem); + } + + public int getPrintFormatItemId() { + return printFormatItemId; + } + + public String getColumnName() { + return columnName; + } + + public int getColumnId() { + return columnId; + } + + public int getReferenceId() { + return referenceId; + } + + public int getReferenceValueId() { + return referenceValueId; + } + + public boolean isKey() { + return isKey; + } + + public boolean isParent() { + return isParent; + } + + public boolean isVirtualColumn() { + return isVirtualColumn; + } + + public String getColumnSql() { + return columnSql; + } + + public boolean isMandatory() { + return isMandatory; + } + + public String getColumnNameAlias() { + return columnNameAlias; + } + + public PrintFormatColumn withColumnNameAlias(String columnNameAlias) { + this.columnNameAlias = columnNameAlias; + return this; + } + + @Override + public String toString() { + return "PrintFormatColumn [printFormatItemId=" + printFormatItemId + ", columnName=" + columnName + + ", columnNameAlias=" + columnNameAlias + ", columnId=" + columnId + ", referenceId=" + referenceId + + ", referenceValueId=" + referenceValueId + ", isKey=" + isKey + ", isParent=" + isParent + + ", isMandatory=" + isMandatory + ", isVirtualColumn=" + isVirtualColumn + ", columnSql=" + columnSql + + "]"; + } +} diff --git a/src/main/java/org/spin/report_engine/format/QueryDefinition.java b/src/main/java/org/spin/report_engine/format/QueryDefinition.java new file mode 100644 index 0000000..e4aba80 --- /dev/null +++ b/src/main/java/org/spin/report_engine/format/QueryDefinition.java @@ -0,0 +1,130 @@ +/************************************************************************************ + * Copyright (C) 2012-2018 E.R.P. Consultores y Asociados, C.A. * + * Contributor(s): Yamel Senih ysenih@erpya.com * + * This program is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, either version 2 of the License, or * + * (at your option) any later version. * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License * + * along with this program. If not, see . * + ************************************************************************************/ +package org.spin.report_engine.format; + +import java.util.ArrayList; +import java.util.List; + +import org.compiere.util.Util; +import org.spin.service.grpc.util.query.Filter; + +/** + * Query Representation + * @author Yamel Senih, ysenih@erpya.com, ERPCyA http://www.erpya.com + */ +public class QueryDefinition { + private String query; + private String groupBy; + private String orderBy; + private List conditions; + private List parameters; + private List columns; + private String whereClause; + + private QueryDefinition() { + conditions = new ArrayList(); + columns = new ArrayList(); + parameters = new ArrayList(); + } + + public static QueryDefinition newInstance() { + return new QueryDefinition(); + } + + public String getQuery() { + return query; + } + + public String getOrderBy() { + return orderBy; + } + + public QueryDefinition withQuery(String query) { + this.query = query; + return this; + } + + public QueryDefinition withOrderBy(String orderBy) { + this.orderBy = orderBy; + return this; + } + + public String getGroupBy() { + return groupBy; + } + + public QueryDefinition withGroupBy(String groupBy) { + this.groupBy = groupBy; + return this; + } + + public List getConditions() { + return conditions; + } + + public QueryDefinition withConditions(List conditions) { + this.conditions = conditions; + return this; + } + + public List getColumns() { + return columns; + } + + public QueryDefinition withColumns(List columns) { + this.columns = columns; + return this; + } + + public List getParameters() { + return parameters; + } + + public String getWhereClause() { + return whereClause; + } + + public QueryDefinition withWhereClause(String whereClause) { + this.whereClause = whereClause; + return this; + } + + public QueryDefinition getWhereClauseFromCriteria() { + // TODO: Add 1=1 to remove `if (whereClause.length() > 0)` and change stream with parallelStream + StringBuffer whereClause = new StringBuffer(); + getConditions().stream() + .filter(condition -> !Util.isEmpty(condition.getColumnName(), true)) + .forEach(condition -> { + if (whereClause.length() > 0) { + whereClause.append(" AND "); + } +// int displayTypeId = column.getAD_Reference_ID(); +// // set table alias to column name +// // TODO: Evaluate support to columnSQL +// String columnName = tableNameAlias + "." + column.getColumnName(); +// condition.setColumnName(columnName); +// String restriction = WhereClauseUtil.getRestrictionByOperator(condition, displayTypeId, parameters); + +// whereClause.append(restriction); + }); + withWhereClause(whereClause.toString()); + return this; + } + + @Override + public String toString() { + return "QueryDefinition [query=" + query + ", groupBy=" + groupBy + ", orderBy=" + orderBy + "]"; + } +} diff --git a/src/main/java/org/spin/report_engine/service/ReportBuilder.java b/src/main/java/org/spin/report_engine/service/ReportBuilder.java index d838e37..dcf227c 100644 --- a/src/main/java/org/spin/report_engine/service/ReportBuilder.java +++ b/src/main/java/org/spin/report_engine/service/ReportBuilder.java @@ -21,6 +21,7 @@ import org.adempiere.exceptions.AdempiereException; import org.compiere.print.MPrintFormat; +import org.compiere.util.DB; import org.compiere.util.Env; import org.spin.report_engine.data.ReportInfo; import org.spin.report_engine.format.PrintFormat; @@ -36,16 +37,16 @@ public class ReportBuilder { private int printFormatId; private int reportViewId; private boolean isSummary; - private List filters; + private List conditions; private ReportBuilder(int printFormatId) { this.printFormatId = printFormatId; - filters = new ArrayList(); + conditions = new ArrayList(); } public ReportBuilder withFilters(List filters) { - this.filters = filters; + this.conditions = filters; return this; } @@ -55,7 +56,7 @@ public ReportBuilder withParameter(String key, Object value) { condition.put(Filter.VALUES, value); Filter filter = new Filter(condition); filter.setColumnName(key); - filters.add(filter); + conditions.add(filter); return this; } @@ -101,6 +102,10 @@ public ReportInfo run(int pageSize, String nextPageToken) { } MPrintFormat printFormat = new MPrintFormat(Env.getCtx(), getPrintFormatId(), null); PrintFormat format = PrintFormat.newInstance(printFormat); + + DB.runResultSet(null, "", null, resulset -> { + + }); System.out.println(format.getQuery()); return ReportInfo.newInstance(); } @@ -110,6 +115,8 @@ public static void main(String[] args) { // Stocktake Line org.compiere.Adempiere.startup(true); Env.setContext(Env.getCtx(), "#AD_Client_ID", 11); + Env.setContext(Env.getCtx(), Env.LANGUAGE, "es_MX"); + Env.setContext(Env.getCtx(), "#AD_Role_ID", 102); ReportBuilder.newInstance(50132).run(50, null); } }