Skip to content

Commit

Permalink
Add some stub values
Browse files Browse the repository at this point in the history
  • Loading branch information
yamelsenih committed May 17, 2024
1 parent c29f0ea commit ab33f23
Show file tree
Hide file tree
Showing 5 changed files with 438 additions and 21 deletions.
54 changes: 54 additions & 0 deletions src/main/java/org/spin/report_engine/format/ColumnReference.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
148 changes: 131 additions & 17 deletions src/main/java/org/spin/report_engine/format/PrintFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -111,32 +112,28 @@ 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() {
public QueryDefinition getQuery() {
clearTableAlias();
StringBuffer query = new StringBuffer();
StringBuffer orderBy = new StringBuffer();
StringBuffer tableReferences = new StringBuffer();
Language language = Language.getLoginLanguage();
List<PrintFormatColumn> columns = new ArrayList<PrintFormatColumn>();
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
Expand All @@ -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)) {
Expand All @@ -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
Expand All @@ -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) {
Expand All @@ -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 ++;
}
Expand Down
112 changes: 112 additions & 0 deletions src/main/java/org/spin/report_engine/format/PrintFormatColumn.java
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>. *
************************************************************************************/
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
+ "]";
}
}
Loading

0 comments on commit ab33f23

Please sign in to comment.