Skip to content

Commit

Permalink
Add generic load for lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
yamelsenih committed May 16, 2024
1 parent b8b8c33 commit c29f0ea
Show file tree
Hide file tree
Showing 5 changed files with 513 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/main/java/org/spin/report_engine/data/Cell.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class Cell {
private String color;
private String style;
private Object value;
private String displayValue;

private Cell() {

Expand Down Expand Up @@ -61,6 +62,15 @@ public Cell withValue(Object value) {
return this;
}

public String getDisplayValue() {
return displayValue;
}

public Cell withDisplayValue(String displayValue) {
this.displayValue = displayValue;
return this;
}

@Override
public String toString() {
return "Cell [color=" + color + ", style=" + style + ", value=" + value + "]";
Expand Down
128 changes: 128 additions & 0 deletions src/main/java/org/spin/report_engine/format/ColumnReference.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/************************************************************************************
* 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 <https://www.gnu.org/licenses/>. *
************************************************************************************/
package org.spin.report_engine.format;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.compiere.util.DB;

/**
* A stub class for reference of column to table
* @author Yamel Senih, ysenih@erpya.com, ERPCyA http://www.erpya.com
*
*/
public class ColumnReference {
/** Table Name */
private String tableName;
/** Key Column */
private String keyColumn;
/** Display Column */
private String displayColumn;
/** Displayed */
private boolean isValueDisplayed = false;
/** Translated */
private boolean isTranslated = false;

public static ColumnReference newInstance() {
return new ColumnReference();
}

public String getTableName() {
return tableName;
}

public String getKeyColumn() {
return keyColumn;
}

public String getDisplayColumn() {
return displayColumn;
}

public boolean isIsValueDisplayed() {
return isValueDisplayed;
}

public boolean isIsTranslated() {
return isTranslated;
}

public ColumnReference withTableName(String tableName) {
this.tableName = tableName;
return this;
}

public ColumnReference withKeyColumn(String keyColumn) {
this.keyColumn = keyColumn;
return this;
}

public ColumnReference withDisplayColumn(String displayColumn) {
this.displayColumn = displayColumn;
return this;
}

public ColumnReference withIsValueDisplayed(boolean isValueDisplayed) {
this.isValueDisplayed = isValueDisplayed;
return this;
}

public ColumnReference withIsTranslated(boolean isTranslated) {
this.isTranslated = isTranslated;
return this;
}

/**
* Get TableName and ColumnName for Reference Tables.
* @param referenceValueId reference value
* @return 0=TableName, 1=KeyColumn, 2=DisplayColumn
*/
public static ColumnReference getColumnReference (int referenceValueId) {
if (referenceValueId <= 0)
throw new IllegalArgumentException("AD_Reference_Value_ID <= 0");
//
ColumnReference columnReference = ColumnReference.newInstance();
//
String SQL = "SELECT t.TableName, ck.ColumnName AS KeyColumn," // 1..2
+ " cd.ColumnName AS DisplayColumn, rt.IsValueDisplayed, cd.IsTranslated "
+ "FROM AD_Ref_Table rt"
+ " INNER JOIN AD_Table t ON (rt.AD_Table_ID = t.AD_Table_ID)"
+ " INNER JOIN AD_Column ck ON (rt.AD_Key = ck.AD_Column_ID)"
+ " INNER JOIN AD_Column cd ON (rt.AD_Display = cd.AD_Column_ID) "
+ "WHERE rt.AD_Reference_ID=?" // 1
+ " AND rt.IsActive = 'Y' AND t.IsActive = 'Y'";
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = DB.prepareStatement(SQL, null);
pstmt.setInt (1, referenceValueId);
rs = pstmt.executeQuery();
if (rs.next()) {
columnReference.withTableName(rs.getString("TableName"))
.withKeyColumn(rs.getString("KeyColumn"))
.withDisplayColumn(rs.getString("DisplayColumn"))
.withIsValueDisplayed("Y".equals(rs.getString("IsValueDisplayed")))
.withIsTranslated("Y".equals(rs.getString("IsTranslated")));
}
} catch (SQLException ex) {
// None
} finally {
DB.close(rs, pstmt);
rs = null; pstmt = null;
}
return columnReference;
} // getTableReference
}
107 changes: 107 additions & 0 deletions src/main/java/org/spin/report_engine/format/PrintFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,20 @@
package org.spin.report_engine.format;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import org.adempiere.core.domains.models.I_AD_PrintFormatItem;
import org.adempiere.core.domains.models.I_AD_ReportView;
import org.compiere.model.MLookupFactory;
import org.compiere.model.MReportView;
import org.compiere.model.MTable;
import org.compiere.model.Query;
import org.compiere.print.MPrintFormat;
import org.compiere.print.MPrintFormatItem;
import org.compiere.util.DisplayType;
import org.compiere.util.Env;
import org.compiere.util.Language;

/**
* Print Format Representation
Expand All @@ -36,9 +41,11 @@ public class PrintFormat {
private int printFormatId;
private ReportView reportView;
private int tableId;
private String tableName;
private boolean isSummary;
private List<ReportView> reportViews;
private List<PrintFormatItem> items;
private int aliasNumber;

private PrintFormat(MPrintFormat printFormat) {
name = printFormat.getName();
Expand All @@ -48,6 +55,7 @@ private PrintFormat(MPrintFormat printFormat) {
reportView = ReportView.newInstance(new MReportView(Env.getCtx(), printFormat.getAD_ReportView_ID(), null));
}
tableId = printFormat.getAD_Table_ID();
tableName = MTable.getTableName(printFormat.getCtx(), printFormat.getAD_Table_ID());
isSummary = printFormat.isSummary();
// Get Views
reportViews = new ArrayList<ReportView>();
Expand Down Expand Up @@ -87,6 +95,10 @@ public int getTableId() {
return tableId;
}

public String getTableName() {
return tableName;
}

public boolean isSummary() {
return isSummary;
}
Expand All @@ -98,6 +110,101 @@ public List<ReportView> getReportViews() {
public List<PrintFormatItem> getItems() {
return items;
}

private String getQueryColumnName(String columnName) {
return getTableName() + "." + columnName;
}

private String getQueryReferenceColumnName(String columnName) {
return getTableAlias() + "." + columnName;
}

public String getQuery() {
clearTableAlias();
StringBuffer query = new StringBuffer();
StringBuffer tableReferences = new StringBuffer();
Language language = Language.getLoginLanguage();
getItems().stream()
.filter(item -> item.isActive() && item.isPrinted())
.sorted(Comparator.comparing(PrintFormatItem::getSequence))
.forEach(item -> {
if(item.getColumnId() > 0) {
if(query.length() > 0) {
query.append(", ");
}
if(item.isVirtualColumn()) {
query.append(item.getColumnSql());
} else {
query.append("(").append(getQueryColumnName(item.getColumnName())).append(")");
}
query.append(" AS ").append(item.getColumnName());
// Process Display Value
if(item.getReferenceId() == DisplayType.TableDir
|| (item.getReferenceId() == DisplayType.Search && item.getReferenceId() == 0)) {
if(query.length() > 0) {
query.append(", ");
}
if(item.isVirtualColumn()) {
String sqlDirect = MLookupFactory.getLookup_TableDirEmbed(language, item.getColumnName(), getTableName(), "(" + item.getColumnSql() + ")");
query.append("(").append(sqlDirect).append(")");
} else {
String sqlDirect = MLookupFactory.getLookup_TableDirEmbed(language, item.getColumnName(), getTableName());
query.append("(").append(sqlDirect).append(")");
}
query.append(" AS ").append(getDisplayColumnName(item));
} else if(item.getReferenceId() == DisplayType.Table
|| (item.getReferenceId() == DisplayType.Search && item.getReferenceValueId() != 0)) {
addTableAlias();
ColumnReference columnReference = ColumnReference.getColumnReference(item.getReferenceValueId());
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(", ");
}
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(getQueryColumnName(item.getColumnName())).append("=").append(getTableAlias()).append(".").append(columnReference.getKeyColumn()).append(")");
}
}
});
if(query.length() > 0) {
query.insert(0, "SELECT ");
query.append(" FROM ").append(getTableName());
if(tableReferences.length() > 0) {
query.append(tableReferences);
}
}
return query.toString();
}

private String getTableAlias() {
return "t" + aliasNumber;
}

private void addTableAlias() {
aliasNumber ++;
}

private void clearTableAlias() {
aliasNumber = 0;
}

private String getDisplayColumnName(PrintFormatItem item) {
return item.getColumnName() + "_" + item.getPrintFormatItemId() + "_DisplayValue";
}

@Override
public String toString() {
Expand Down
Loading

0 comments on commit c29f0ea

Please sign in to comment.