* @param optionalUserFilters
* @throws JSONException
*/
public static void applyOptionalFilters(Query query, JSONObject optionalUserFilters) throws JSONException{
String[] fields = JSONObject.getNames(optionalUserFilters);
ExpressionNode leftExpression = query.getWhereClauseStructure();
for(int i=0; i<fields.length; i++){
String fieldName = fields[i];
JSONArray valuesArray = optionalUserFilters.getJSONArray(fieldName);
//if the filter has some value
if(valuesArray.length()>0){
String[] values = new String[1];
values[0] =fieldName;
Operand leftOperand = new Operand(values,fieldName, AbstractStatement.OPERAND_TYPE_FIELD, values,values);
values = new String[valuesArray.length()];
for(int j=0; j<valuesArray.length(); j++){
values[j] = valuesArray.getString(j);
}
Operand rightOperand = new Operand(values,fieldName, AbstractStatement.OPERAND_TYPE_STATIC, values, values);
String operator = "NOT EQUALS TO";
if(valuesArray.length()>0){
operator="IN";
}
query.addWhereField("OptionalFilter"+i, "OptionalFilter"+i, false, leftOperand, operator, rightOperand, "AND");
ExpressionNode filterNode = new ExpressionNode("NO_NODE_OP","$F{OptionalFilter"+i+"}");
//build the where clause tree
if(leftExpression==null){
leftExpression = filterNode;
}else{
ExpressionNode operationNode = new ExpressionNode("NODE_OP", "AND");
operationNode.addChild(leftExpression);
operationNode.addChild(filterNode);
leftExpression = operationNode;
}
}
}
query.setWhereClauseStructure(leftExpression);