diff --git a/src/main/java/org/spin/report_engine/format/QueryDefinition.java b/src/main/java/org/spin/report_engine/format/QueryDefinition.java index 57a83fc..b78cd1c 100644 --- a/src/main/java/org/spin/report_engine/format/QueryDefinition.java +++ b/src/main/java/org/spin/report_engine/format/QueryDefinition.java @@ -40,7 +40,7 @@ public class QueryDefinition { private String whereClause; private String completeQuery; private String completeQueryCount; - private int limit; + private int limit; private int offset; private int instanceId; public static final int NO_LIMIT = -1; @@ -97,7 +97,7 @@ public QueryDefinition withLimit(int limit, int offset) { this.offset = offset; return this; } - + public List getConditions() { return conditions; } @@ -130,7 +130,7 @@ public List getParameters() { } public String getWhereClause() { - return whereClause; + return this.whereClause; } public QueryDefinition withWhereClause(String whereClause) { @@ -157,6 +157,11 @@ public QueryDefinition withCompleteQuery(String completeQuery) { } public QueryDefinition buildQuery() { + // Add Query columns + StringBuffer completeQuery = new StringBuffer(getQuery()); + StringBuffer completeQueryWithoutLimit = new StringBuffer(getQuery()); + + // Add Where restriction // TODO: Add 1=1 to remove `if (whereClause.length() > 0)` and change stream with parallelStream StringBuffer whereClause = new StringBuffer(); getConditions().stream() @@ -177,46 +182,53 @@ public QueryDefinition buildQuery() { whereClause.append(restriction); } }); - // No Limit for Counter - StringBuffer completeQueryWithoutLimit = new StringBuffer(getQuery()); - if(!Util.isEmpty(getWhereClause())) { - completeQueryWithoutLimit.append(" WHERE ").append(whereClause); - } - if(!Util.isEmpty(getGroupBy())) { - completeQueryWithoutLimit.append(" GROUP BY ").append(getGroupBy()); - } - if(!Util.isEmpty(getOrderBy())) { - completeQueryWithoutLimit.append(" ORDER BY ").append(getOrderBy()); + withWhereClause(whereClause.toString()); + if(!Util.isEmpty(getWhereClause(), true)) { + completeQuery.append(" WHERE ").append(getWhereClause()); + completeQueryWithoutLimit.append(" WHERE ").append(getWhereClause()); } - withCompleteQueryCount(completeQueryWithoutLimit.toString()); - // Add Limit - if(limit != NO_LIMIT) { - if(limit == 0) { + + // Add Limit records + if(this.limit != NO_LIMIT) { + if(this.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 "); + + StringBuffer limitClause = new StringBuffer() + // TODO: Implement with use https://github.com/adempiere/adempiere/pull/4142 + // .append(" LIMIT ") + // .append(this.limit) + // .append(" OFFSET ") + // .append(this.offset) + // .append(completeQueryCount) + ; + + if(!Util.isEmpty(getWhereClause(), true)) { + limitClause.insert(0, " AND "); + } else { + limitClause.insert(0, " WHERE "); } - whereClause.append("ROWNUM >= ").append(offset); - } - // - withWhereClause(whereClause.toString()); - StringBuffer completeQuery = new StringBuffer(getQuery()); - if(!Util.isEmpty(getWhereClause())) { - completeQuery.append(" WHERE ").append(getWhereClause()); + limitClause.append(" ROWNUM <= ").append(this.limit); + limitClause.append(" AND ROWNUM >= ").append(this.offset); + + completeQuery.append(limitClause.toString()); } - if(!Util.isEmpty(getGroupBy())) { + + // Add Group By + if(!Util.isEmpty(getGroupBy(), true)) { completeQuery.append(" GROUP BY ").append(getGroupBy()); + completeQueryWithoutLimit.append(" GROUP BY ").append(getGroupBy()); } - if(!Util.isEmpty(getOrderBy())) { + + // Add Order By + if(!Util.isEmpty(getOrderBy(), true)) { completeQuery.append(" ORDER BY ").append(getOrderBy()); + completeQueryWithoutLimit.append(" ORDER BY ").append(getOrderBy()); } + + withCompleteQueryCount(completeQueryWithoutLimit.toString()); withCompleteQuery(completeQuery.toString()); + return this; } 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 5cba080..bb114ba 100644 --- a/src/main/java/org/spin/report_engine/service/ReportBuilder.java +++ b/src/main/java/org/spin/report_engine/service/ReportBuilder.java @@ -76,7 +76,11 @@ public class ReportBuilder { private ReportBuilder() { conditions = new ArrayList(); } - + + public List getFilters() { + return this.conditions; + } + public ReportBuilder withFilters(List filters) { this.conditions = filters; return this; @@ -88,7 +92,7 @@ public ReportBuilder withParameter(String key, Object value) { condition.put(Filter.VALUES, value); Filter filter = new Filter(condition); filter.setColumnName(key); - conditions.add(filter); + this.conditions.add(filter); return this; } @@ -172,9 +176,19 @@ private ReportInfo get(String transactionName) { Language language = Language.getLoginLanguage(); MPrintFormat printFormat = new MPrintFormat(Env.getCtx(), getPrintFormatId(), null); PrintFormat format = PrintFormat.newInstance(printFormat); - QueryDefinition queryDefinition = format.getQuery().withConditions(conditions).withInstanceId(getInstanceId()).withLimit(limit, offset).buildQuery(); + QueryDefinition queryDefinition = format.getQuery() + .withConditions(conditions) + .withInstanceId(getInstanceId()) + .withLimit(limit, offset) + .buildQuery() + ; int count = CountUtil.countRecords(queryDefinition.getCompleteQueryCount(), format.getTableName(), queryDefinition.getParameters()); - ReportInfo reportInfo = ReportInfo.newInstance(format, queryDefinition).withReportViewId(getReportViewId()).withInstanceId(getInstanceId()).withRecordCount(count).withSummary(isSummary()); + ReportInfo reportInfo = ReportInfo.newInstance(format, queryDefinition) + .withReportViewId(getReportViewId()) + .withInstanceId(getInstanceId()) + .withRecordCount(count) + .withSummary(isSummary()) + ; DB.runResultSet(transactionName, queryDefinition.getCompleteQuery(), queryDefinition.getParameters(), resulset -> { while (resulset.next()) { format.getItems().forEach(item -> { @@ -263,7 +277,9 @@ private MPInstance generateProcessInstance() { processInstance.setName(process.get_Translation(I_AD_Process.COLUMNNAME_Name)); processInstance.setRecord_ID(getRecordId()); processInstance.saveEx(); - withInstanceId(processInstance.getAD_PInstance_ID()); + if (!Util.isEmpty(process.getClassname(), true)) { + withInstanceId(processInstance.getAD_PInstance_ID()); + } // Add Parameters AtomicInteger sequence = new AtomicInteger(0); conditions.forEach(filter -> { diff --git a/src/main/java/org/spin/report_engine/service/Service.java b/src/main/java/org/spin/report_engine/service/Service.java index 460a33c..7f42b76 100644 --- a/src/main/java/org/spin/report_engine/service/Service.java +++ b/src/main/java/org/spin/report_engine/service/Service.java @@ -41,6 +41,7 @@ import org.spin.report_engine.format.QueryDefinition; import org.spin.service.grpc.authentication.SessionManager; import org.spin.service.grpc.util.db.LimitUtil; +import org.spin.service.grpc.util.query.Filter; import org.spin.service.grpc.util.query.FilterManager; import org.spin.service.grpc.util.value.ValueManager; @@ -143,8 +144,10 @@ public static Report.Builder getReport(GetReportRequest request) { int offset = (pageNumber - 1) * limit; ReportBuilder reportBuilder = ReportBuilder.newInstance().withReportId(request.getReportId()); if(!Util.isEmpty(request.getFilters())) { - reportBuilder.withFilters(FilterManager.newInstance(request.getFilters()) - .getConditions()); + List conditionsList = FilterManager.newInstance(request.getFilters()) + .getConditions() + ; + reportBuilder.withFilters(conditionsList); } reportBuilder.withPrintFormatId(request.getPrintFormatId()).withReportViewId(request.getReportViewId()); reportBuilder.withSummary(request.getIsSummary());