Added the possibility of having OR conditions between filters with the
same key
This commit is contained in:
parent
16da114f8b
commit
3657057e48
|
@ -2,9 +2,12 @@ package org.gcube.accounting.analytics.persistence.postgresql;
|
|||
|
||||
import java.sql.SQLException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.gcube.accounting.analytics.Filter;
|
||||
|
@ -31,7 +34,7 @@ public class Query extends PostgreSQLQuery {
|
|||
private Set<String> requestedTableField;
|
||||
|
||||
protected TemporalConstraint temporalConstraint;
|
||||
protected Set<Filter> filters;
|
||||
protected Map<String, Collection<String>> filters;
|
||||
protected Set<String> contexts;
|
||||
|
||||
private String tableFieldToRequest;
|
||||
|
@ -40,24 +43,6 @@ public class Query extends PostgreSQLQuery {
|
|||
|
||||
private String recordId;
|
||||
|
||||
protected String orConditionKey;
|
||||
protected Set<String> orConditionValues;
|
||||
|
||||
public void setOrConditionKey(String orConditionKey) {
|
||||
this.orConditionKey = orConditionKey;
|
||||
}
|
||||
|
||||
public void setOrConditionValues(Collection<String> orConditionValues) {
|
||||
this.orConditionValues = new HashSet<>(orConditionValues);
|
||||
}
|
||||
|
||||
public void addOrConditionValue(String conditionValue) {
|
||||
if(this.orConditionValues == null) {
|
||||
this.orConditionValues = new HashSet<>();
|
||||
}
|
||||
this.orConditionValues.add(conditionValue);
|
||||
}
|
||||
|
||||
public Query(Class<? extends AggregatedRecord<?, ?>> clz) throws Exception {
|
||||
this.clz = clz;
|
||||
this.recordToDBFields = RecordToDBMapping.getRecordToDBFields(clz);
|
||||
|
@ -71,8 +56,20 @@ public class Query extends PostgreSQLQuery {
|
|||
this.temporalConstraint = temporalConstraint;
|
||||
}
|
||||
|
||||
public void setFilters(Set<Filter> filters) {
|
||||
this.filters = filters;
|
||||
public void setFilters(Collection<Filter> filters) {
|
||||
this.filters = new HashMap<>();
|
||||
if(filters!=null && filters.size()>0) {
|
||||
for(Filter filter : filters) {
|
||||
String key = filter.getKey();
|
||||
String value = filter.getValue();
|
||||
Collection<String> list = this.filters.get(key);
|
||||
if(list == null) {
|
||||
list = new ArrayList<>();
|
||||
this.filters.put(key, list);
|
||||
}
|
||||
list.add(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void setContexts(Set<String> contexts) {
|
||||
|
@ -120,28 +117,26 @@ public class Query extends PostgreSQLQuery {
|
|||
return date;
|
||||
}
|
||||
|
||||
//TODO need to check duplicated keys and put all values in OR
|
||||
protected void addFilters() throws SQLException {
|
||||
if(filters!=null && filters.size()>0) {
|
||||
// The first filter if the time_bucket
|
||||
stringBuffer.append(" AND ");
|
||||
stringBuffer.append("(");
|
||||
boolean first = true;
|
||||
for(Filter filter : filters) {
|
||||
for(String key : filters.keySet()) {
|
||||
if(first) {
|
||||
first = false;
|
||||
}else {
|
||||
stringBuffer.append(" AND ");
|
||||
}
|
||||
appendTableField(filter.getKey());
|
||||
stringBuffer.append("=");
|
||||
appendValue(filter.getValue());
|
||||
addOrConditions(key, filters.get(key));
|
||||
}
|
||||
stringBuffer.append(")");
|
||||
}
|
||||
}
|
||||
|
||||
protected void addOrConditions(String key, Set<String> values) throws SQLException {
|
||||
protected void addOrConditions(String key, Collection<String> values) throws SQLException {
|
||||
if(values!=null && values.size()>0) {
|
||||
// The first filter if the time_bucket
|
||||
stringBuffer.append(" AND (");
|
||||
stringBuffer.append("(");
|
||||
boolean first = true;
|
||||
for(String value : values) {
|
||||
if(first) {
|
||||
|
@ -157,15 +152,10 @@ public class Query extends PostgreSQLQuery {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
protected void addContextFilter() throws SQLException {
|
||||
addOrConditions(UsageRecord.SCOPE, contexts);
|
||||
}
|
||||
|
||||
protected void addOrConditionFilter() throws SQLException {
|
||||
addOrConditions(orConditionKey, orConditionValues);
|
||||
}
|
||||
|
||||
protected void addEmittedFields(boolean addNested) throws Exception {
|
||||
Set<String> aggregatedField = clz.newInstance().getAggregatedFields();
|
||||
for(String fieldName : aggregatedField) {
|
||||
|
@ -357,7 +347,9 @@ public class Query extends PostgreSQLQuery {
|
|||
|
||||
addTemporalConstraintToQuery();
|
||||
|
||||
stringBuffer.append(" AND ");
|
||||
addFilters();
|
||||
stringBuffer.append(" AND ");
|
||||
addContextFilter();
|
||||
|
||||
addDateGroupBy(false);
|
||||
|
@ -384,7 +376,9 @@ public class Query extends PostgreSQLQuery {
|
|||
|
||||
addTemporalConstraintToQuery();
|
||||
|
||||
stringBuffer.append(" AND ");
|
||||
addFilters();
|
||||
stringBuffer.append(" AND ");
|
||||
addContextFilter();
|
||||
|
||||
addGroupAndOrderByForOrderByField();
|
||||
|
|
Loading…
Reference in New Issue