Question

if (params.filters) {
                def o = JSON.parse(params.filters);
                def groupOp = o.groupOp
                def fields = o.rules.field
                def values = o.rules.data
                def op = o.rules.op
                println fields
                println values

                if(groupOp == "AND") {
                    fields.eachWithIndex {a, i ->
                        println op[i]
                        if(op[i].equals( "eq")) {
                            and{    eq(fields[i], values[i])}
                        }
                        if(op[i].equals("ne")) {
                            and{    ne(fields[i], values[i])}
                        }
                        if(op[i].equals("ge")) {
                            def valu = Double.valueOf( values[i]);
                            and{    ge(fields[i], valu)}
                        }
                    }
                }
                if(groupOp == "OR") {
                    fields.eachWithIndex {a, i ->
                        println op[i]
                        if(op[i].equals( "eq")) {
                            println 'eq';
                            or{ eq(fields[i], values[i])}
                        }
                        if(op[i].equals("ne")) {
                            println 'ne';
                            or{ ne(fields[i], values[i])}
                        }
                        if(op[i].equals("ge")) {
                            def valu = Double.valueOf( values[i]);
                            or{ ge(fields[i], valu)}
                        }
                    }
                }
            }

where params.filters is following JSON text.

    {
   "groupOp":"OR",
   "rules":[
      {
         "field":"foo1",
         "op":"le",
         "data":"9.5"
      },
      {
         "field":"foo2",
         "op":"eq",
         "data":"12345-123"
      },
      {
         "field":"foo3",
         "op":"cn",
         "data":"IDM"
      }
   ]
}

This data is coming from JQuery data grid.

Is there a better way of doing this? In the code I have just listed only 3 operators, but in real I have 14 operations.

Was it helpful?

Solution

You can use String as Criteria operation, like:

A.withCriteria {
  'eq' (id, 1)
}

so you might come to something like

A.withCriteria {
    (groupOp) {
        for (???) {
            (op[i]) (fields[i], parsedVals[i])
        }
    }
}

Anyway you'll need to sanitize the web-submitted query for only allowed subset of operations. You don't want to receive end execute arbitrary sqlRestriction, right? :D So the code is going to be more complex then this anyway.

Note: wrapping and{} or or {} around single statement has no point, you need to put it around whole block of if-s.

OTHER TIPS

I suggest that you have a look at the source code of the FilterPane plugin. Its service does essentially what you are doing and may give you some ideas for enhancements.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top