Question

I am trying to add some user defined filters to a query. The filters are properties in html list elements and what I want to do is, if "all" is not selected, add the criteria to the query. What I've tried so far is

Criteria crit = Person.createCriteria().
addAccountCondition(crit)
addPrimarySkillCondition(crit)
addSkillsetCondition(crit)
addDateCondition(crit)
crit.addOrder(Order.asc("account.name"))

crit.list()

private void addAccountCondition(Criteria query){
String acct = params?.accts?.value?.toString()

if (!acct.equals("all") && acct != null){
    query.add(Restrictions.eq("account.name", acct))
// old version
    //          query = query.where {
//              account {
//                  eq "name", acct
//                  order "name", "asc"
//              }
//          }
    println "acct is ${acct}"
    println "Applying account condition. The query is ${query.toString()}"
}
}

But, with this code, I'm trying to cast a groovy Criteria into a Hibernate criteria. I know how to make a groovy query with all of these, but not how to make them dynamically

No correct solution

OTHER TIPS

It is a little hard to tell from your code sample what all is going on but something like this may help...

def results = Person.withCriteria {
    def acct = params.accts?.value
    if(acct) {
        account {
            eq 'name', acct
        }
    }
}

If you really want to pass the closure around to several methods like addPrimarySkillCondition, addDateCondition, etc, you can do that with some special delegate handling on the closure. To show you the specifics I would need to see what is in those methods.

I hope that helps.

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