Skip to content

Commit

Permalink
feat: Filter parameter with multiple values.
Browse files Browse the repository at this point in the history
  • Loading branch information
EdwinBetanc0urt committed May 4, 2023
1 parent e8765b3 commit 4302538
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 18 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"java.compile.nullAnalysis.mode": "interactive"
"java.compile.nullAnalysis.mode": "interactive",
"java.configuration.updateBuildConfiguration": "disabled"
}
66 changes: 49 additions & 17 deletions src/main/java/org/spin/eca50/controller/ChartBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -95,11 +96,13 @@ private static ChartQueryDefinition getDataSourceQuery(int chartDatasourceId, bo
List<Object> parameters = new ArrayList<Object>();
StringBuffer sql = new StringBuffer("SELECT " + value + ", " + category + ", " + series);
sql.append(" FROM ").append(fromClause);
StringBuffer whereClause = new StringBuffer();
if (!Util.isEmpty(where)) {
whereClause.append(where);

// where clause
StringBuffer whereClause = new StringBuffer(" WHERE 1=1 ");
if (!Util.isEmpty(where, true)) {
whereClause.append(" AND (").append(where).append(")");
}

Timestamp currentDate = Env.getContextAsDate(Env.getCtx(), "#Date");
Timestamp startDate = null;
Timestamp endDate = null;
Expand All @@ -114,27 +117,33 @@ private static ChartQueryDefinition getDataSourceQuery(int chartDatasourceId, bo
}

if (startDate != null && endDate != null) {
if(whereClause.length() > 0) {
whereClause.append(" AND ");
}
whereClause.append(category).append(" >= ? ").append("AND ").append(category).append(" <= ? ");
whereClause.append(" AND ").append(category).append(" >= ? ")
.append("AND ").append(category).append(" <= ? ")
;
parameters.add(startDate);
parameters.add(endDate);
}
// Add custom parameters
if(customParameters != null && customParameters.size() > 0) {
if (customParameters != null && customParameters.size() > 0) {
customParameters.entrySet().forEach(parameter -> {
if(whereClause.length() > 0) {
whereClause.append(" AND ");
whereClause.append(" AND ")
.append(parameter.getKey())
;

// add value(s)
Object currentValue = parameter.getValue();
if (currentValue instanceof Collection) {
// is multiple values to IN or BETWEEN operators
addCollectionParameters(currentValue, parameters);
} else {
parameters.add(currentValue);
}
whereClause.append(parameter.getKey());
parameters.add(parameter.getValue());
});
}

// Add where clause
if(whereClause.length() > 0) {
sql.append(" WHERE ").append(whereClause);
}
sql.append(whereClause);

MRole role = MRole.getDefault(Env.getCtx(), false);
sql = new StringBuffer(role.addAccessSQL(sql.toString(), null, true, false));

Expand Down Expand Up @@ -202,7 +211,30 @@ public static ChartValue getChartData(int chartId, Map<String, Object> customPar
//
return metrics;
}



/**
* When filter value is a collection (List, ArrayList)
* @param objectColelction
* @param parameters
*/
@SuppressWarnings("unchecked")
public static void addCollectionParameters(Object objectColelction, List<Object> parameters) {
if (objectColelction instanceof Collection) {
try {
Collection<Object> collection = (Collection<Object>) objectColelction;
// for-each loop
for (Object rangeValue : collection) {
parameters.add(rangeValue);
}
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
}


public static void main(String[] args) {
org.compiere.Adempiere.startup(true);
Env.setContext(Env.getCtx(), "#Date", new Timestamp(System.currentTimeMillis()));
Expand Down

0 comments on commit 4302538

Please sign in to comment.