Skip to content

Commit

Permalink
aDD LIMIT
Browse files Browse the repository at this point in the history
  • Loading branch information
yamelsenih committed May 17, 2024
1 parent ab33f23 commit fa02628
Show file tree
Hide file tree
Showing 4 changed files with 233 additions and 23 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,10 @@ dependencies {
//implementation 'com.sun.xml.bind:jaxb-core:3.0.0-M4'
//implementation 'javax.activation:activation:1.1.1'
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.17.0'

implementation 'io.vavr:vavr:0.10.4'
// ADempiere Core
implementation "${baseGroupId}:base:${baseVersion}"
implementation "${baseGroupId}:adempiere-grpc-utils:1.3.8"
implementation "${baseGroupId}:adempiere-grpc-utils:1.3.9"

// Others
compileOnly 'org.apache.tomcat:annotations-api:6.0.53'
Expand Down
16 changes: 11 additions & 5 deletions src/main/java/org/spin/report_engine/format/PrintFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,15 @@ public QueryDefinition getQuery() {
query.append(", ");
}
if(item.isVirtualColumn()) {
columnName = item.getColumnSql();
columnName = "(" + item.getColumnSql() + ")";
query.append(columnName);
query.append(" AS ").append(item.getColumnName());
columns.add(PrintFormatColumn.newInstance(item).withColumnNameAlias(item.getColumnName()));
} else {
columnName = getQueryColumnName(item.getColumnName());
query.append(columnName);
columns.add(PrintFormatColumn.newInstance(item).withColumnNameAlias(columnName));
}
query.append("(").append(columnName).append(")");
query.append(" AS ").append(item.getColumnName());
// Process Display Value
if(item.getReferenceId() == DisplayType.TableDir
|| (item.getReferenceId() == DisplayType.Search && item.getReferenceId() == 0)) {
Expand All @@ -148,6 +151,7 @@ public QueryDefinition getQuery() {
}
query.append("(").append(columnName).append(")");
query.append(" AS ").append(getDisplayColumnName(item));
columns.add(PrintFormatColumn.newInstance(item).withColumnNameAlias(getDisplayColumnName(item)));
} else if(item.getReferenceId() == DisplayType.Table
|| (item.getReferenceId() == DisplayType.Search && item.getReferenceValueId() != 0)) {
addTableAlias();
Expand All @@ -166,6 +170,7 @@ public QueryDefinition getQuery() {
columnName = displayColumnValue.toString();
query.append("(").append(displayColumnValue).append(")");
query.append(" AS ").append(getDisplayColumnName(item));
columns.add(PrintFormatColumn.newInstance(item).withColumnNameAlias(getDisplayColumnName(item)));
// Add JOIN
if(item.isMandatory()) {
tableReferences.append(" INNER JOIN ");
Expand Down Expand Up @@ -203,6 +208,7 @@ public QueryDefinition getQuery() {
columnName = displayColumnValue.toString();
query.append("(").append(displayColumnValue).append(")");
query.append(" AS ").append(getDisplayColumnName(item));
columns.add(PrintFormatColumn.newInstance(item).withColumnNameAlias(getDisplayColumnName(item)));
// Add JOIN
if(item.isMandatory()) {
tableReferences.append(" INNER JOIN ");
Expand Down Expand Up @@ -242,6 +248,7 @@ public QueryDefinition getQuery() {
columnName = displayColumnValue.toString();
query.append("(").append(displayColumnValue).append(")");
query.append(" AS ").append(getDisplayColumnName(item));
columns.add(PrintFormatColumn.newInstance(item).withColumnNameAlias(getDisplayColumnName(item)));
// Add JOIN
if(item.isMandatory()) {
tableReferences.append(" INNER JOIN ");
Expand All @@ -264,7 +271,6 @@ public QueryDefinition getQuery() {
}
}
}
columns.add(PrintFormatColumn.newInstance(item).withColumnNameAlias(getDisplayColumnName(item)));
}
});
if(query.length() > 0) {
Expand All @@ -277,7 +283,7 @@ public QueryDefinition getQuery() {
// Return definition
return QueryDefinition.newInstance()
.withQuery(query.toString())
.withOrderBy(orderBy.toString())
// .withOrderBy(orderBy.toString())
.withColumns(columns);
}

Expand Down
221 changes: 211 additions & 10 deletions src/main/java/org/spin/report_engine/format/QueryDefinition.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@
package org.spin.report_engine.format;

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

import java.util.Optional;
import org.compiere.util.DisplayType;
import org.compiere.util.Util;
import org.spin.service.grpc.util.db.OperatorUtil;
import org.spin.service.grpc.util.db.ParameterUtil;
import org.spin.service.grpc.util.query.Filter;
import org.spin.service.grpc.util.value.ValueManager;

/**
* Query Representation
Expand All @@ -32,6 +37,9 @@ public class QueryDefinition {
private List<Object> parameters;
private List<PrintFormatColumn> columns;
private String whereClause;
private String completeQuery;
private int limit;
private int offset;

private QueryDefinition() {
conditions = new ArrayList<Filter>();
Expand Down Expand Up @@ -70,6 +78,12 @@ public QueryDefinition withGroupBy(String groupBy) {
return this;
}

public QueryDefinition withLimit(int limit, int offset) {
this.limit = limit;
this.offset = offset;
return this;
}

public List<Filter> getConditions() {
return conditions;
}
Expand Down Expand Up @@ -101,7 +115,16 @@ public QueryDefinition withWhereClause(String whereClause) {
return this;
}

public QueryDefinition getWhereClauseFromCriteria() {
public String getCompleteQuery() {
return completeQuery;
}

public QueryDefinition withCompleteQuery(String completeQuery) {
this.completeQuery = completeQuery;
return this;
}

public QueryDefinition buildQuery() {
// TODO: Add 1=1 to remove `if (whereClause.length() > 0)` and change stream with parallelStream
StringBuffer whereClause = new StringBuffer();
getConditions().stream()
Expand All @@ -110,18 +133,196 @@ public QueryDefinition getWhereClauseFromCriteria() {
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);
Optional<PrintFormatColumn> maybeColumn = getColumns()
.stream()
.filter(column -> column.getColumnName().equals(condition.getColumnName()))
.sorted(Comparator.comparing(PrintFormatColumn::getColumnNameAlias))
.findFirst();
if(maybeColumn.isPresent()) {
PrintFormatColumn column = maybeColumn.get();
condition.setColumnName(column.getColumnNameAlias());
String restriction = getRestrictionByOperator(condition, column.getReferenceId());
whereClause.append(restriction);
}
});
// Add Limit
if(limit <= 0) {
withLimit(100, 0);
}
if (whereClause.length() > 0) {
whereClause.append(" AND ");
}
whereClause.append("ROWNUM <= ").append(limit);
// Add offset
if (whereClause.length() > 0) {
whereClause.append(" AND ");
}
whereClause.append("ROWNUM >= ").append(offset);
//
withWhereClause(whereClause.toString());
StringBuffer completeQuery = new StringBuffer(getQuery());
if(!Util.isEmpty(getWhereClause())) {
completeQuery.append(" WHERE ").append(getWhereClause());
}
if(!Util.isEmpty(getGroupBy())) {
completeQuery.append(" GROUP BY ").append(getGroupBy());
}
if(!Util.isEmpty(getOrderBy())) {
completeQuery.append(" ORDER BY ").append(getOrderBy());
}
withCompleteQuery(completeQuery.toString());
return this;
}

/**
* Get sql restriction by operator
* @param condition
* @param displayType
* @param parameters
* @return
*/
private String getRestrictionByOperator(Filter condition, int displayType) {
String operatorValue = OperatorUtil.EQUAL;
if (!Util.isEmpty(condition.getOperator(), true)) {
operatorValue = condition.getOperator().toLowerCase();
}
String sqlOperator = OperatorUtil.convertOperator(condition.getOperator());

String columnName = condition.getColumnName();
String sqlValue = "";
StringBuilder additionalSQL = new StringBuilder();
// For IN or NOT IN
if (operatorValue.equals(OperatorUtil.IN) || operatorValue.equals(OperatorUtil.NOT_IN)) {
StringBuilder parameterValues = new StringBuilder();
final String baseColumnName = columnName;
StringBuilder column_name = new StringBuilder(columnName);

if (condition.getValues() != null) {
condition.getValues().forEach(currentValue -> {
boolean isString = DisplayType.isText(displayType) || currentValue instanceof String;

if (currentValue == null || (isString && Util.isEmpty((String) currentValue, true))) {
if (Util.isEmpty(additionalSQL.toString(), true)) {
additionalSQL.append("(SELECT " + baseColumnName + " WHERE " + baseColumnName + " IS NULL)");
}
if (isString) {
currentValue = "";
} else {
// does not add the null value to the filters, another restriction is
// added only for null values `additionalSQL`.
return;
}
}
if (parameterValues.length() > 0) {
parameterValues.append(", ");
}
String sqlInValue = "?";
if (isString) {
column_name.delete(0, column_name.length());
column_name.append("UPPER(").append(baseColumnName).append(")");
sqlInValue = "UPPER(?)";
}
parameterValues.append(sqlInValue);

Object valueToFilter = ParameterUtil.getValueToFilterRestriction(
displayType,
currentValue
);
parameters.add(valueToFilter);
});
}

columnName = column_name.toString();
if (!Util.isEmpty(parameterValues.toString(), true)) {
sqlValue = "(" + parameterValues.toString() + ")";
if (!Util.isEmpty(additionalSQL.toString(), true)) {
additionalSQL.insert(0, " OR " + columnName + sqlOperator);
}
}
} else if(operatorValue.equals(OperatorUtil.BETWEEN) || operatorValue.equals(OperatorUtil.NOT_BETWEEN)) {
// List<Object> values = condition.getValues();
Object valueStartToFilter = ParameterUtil.getValueToFilterRestriction(
displayType,
condition.getFromValue()
);
Object valueEndToFilter = ParameterUtil.getValueToFilterRestriction(
displayType,
condition.getToValue()
);

sqlValue = "";
if (valueStartToFilter == null) {
sqlValue = " ? ";
sqlOperator = OperatorUtil.convertOperator(OperatorUtil.LESS_EQUAL);
parameters.add(valueEndToFilter);
} else if (valueEndToFilter == null) {
sqlValue = " ? ";
sqlOperator = OperatorUtil.convertOperator(OperatorUtil.GREATER_EQUAL);
parameters.add(valueStartToFilter);
} else {
sqlValue = " ? AND ? ";
parameters.add(valueStartToFilter);
parameters.add(valueEndToFilter);
}
} else if(operatorValue.equals(OperatorUtil.LIKE) || operatorValue.equals(OperatorUtil.NOT_LIKE)) {
columnName = "UPPER(" + columnName + ")";
String valueToFilter = ValueManager.validateNull(
(String) condition.getValue()
);
// if (!Util.isEmpty(valueToFilter, true)) {
// if (!valueToFilter.startsWith("%")) {
// valueToFilter = "%" + valueToFilter;
// }
// if (!valueToFilter.endsWith("%")) {
// valueToFilter += "%";
// }
// }
// valueToFilter = "UPPPER(" + valueToFilter + ")";
sqlValue = "'%' || UPPER(?) || '%'";
parameters.add(valueToFilter);
} else if(operatorValue.equals(OperatorUtil.NULL) || operatorValue.equals(OperatorUtil.NOT_NULL)) {
;
} else if (operatorValue.equals(OperatorUtil.EQUAL) || operatorValue.equals(OperatorUtil.NOT_EQUAL)) {
Object parameterValue = condition.getValue();
sqlValue = " ? ";

boolean isString = DisplayType.isText(displayType);
boolean isEmptyString = isString && Util.isEmpty((String) parameterValue, true);
if (isString) {
if (isEmptyString) {
parameterValue = "";
} else {
columnName = "UPPER(" + columnName + ")";
sqlValue = "UPPER(?)";
}
}
if (parameterValue == null || isEmptyString) {
additionalSQL.append(" OR ")
.append(columnName)
.append(" IS NULL ")
;
}

Object valueToFilter = ParameterUtil.getValueToFilterRestriction(
displayType,
parameterValue
);
parameters.add(valueToFilter);
} else {
// Greater, Greater Equal, Less, Less Equal
sqlValue = " ? ";

Object valueToFilter = ParameterUtil.getValueToFilterRestriction(
displayType,
condition.getValue()
);
parameters.add(valueToFilter);
}

String rescriction = "(" + columnName + sqlOperator + sqlValue + additionalSQL.toString() + ")";

return rescriction;
}

@Override
public String toString() {
Expand Down
15 changes: 9 additions & 6 deletions src/main/java/org/spin/report_engine/service/ReportBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.compiere.util.Env;
import org.spin.report_engine.data.ReportInfo;
import org.spin.report_engine.format.PrintFormat;
import org.spin.report_engine.format.QueryDefinition;
import org.spin.service.grpc.util.query.Filter;

/**
Expand Down Expand Up @@ -96,17 +97,19 @@ public ReportBuilder withSummary(boolean isSummary) {
return this;
}

public ReportInfo run(int pageSize, String nextPageToken) {
public ReportInfo run(int limit, int offset) {
if(getPrintFormatId() <= 0) {
throw new AdempiereException("@FillMandatory@ @AD_PrintFormat_ID@");
}
withParameter("M_Product_ID", 147);
MPrintFormat printFormat = new MPrintFormat(Env.getCtx(), getPrintFormatId(), null);
PrintFormat format = PrintFormat.newInstance(printFormat);

DB.runResultSet(null, "", null, resulset -> {

QueryDefinition queryDefinition = format.getQuery().withConditions(conditions).withLimit(limit, offset).buildQuery();
DB.runResultSet(null, queryDefinition.getCompleteQuery(), queryDefinition.getParameters(), resulset -> {
while (resulset.next()) {
System.out.println(resulset.getObject("Line"));
}
});
System.out.println(format.getQuery());
return ReportInfo.newInstance();
}

Expand All @@ -117,6 +120,6 @@ public static void main(String[] args) {
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);
ReportBuilder.newInstance(50132).run(50, 0);
}
}

0 comments on commit fa02628

Please sign in to comment.