Added the possibility of having OR conditions between filters with the

same key
This commit is contained in:
Luca Frosini 2021-10-29 19:02:49 +02:00
parent 16da114f8b
commit 3657057e48
1 changed files with 29 additions and 35 deletions

View File

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