gunboxer gunboxer - 1 year ago 100
Groovy Question

Groovy domain class find with only provided filter


I need to perform Domain class filtering in Groovy for provided filter fields.
Here is code sample:

User.findAll(name:, age: filter.age, department: filter.department)

Is there exists some syntax sugar to help me to validate if for example is not provided e.g. null or empty - do not filter by this field. Thanks.

Answer Source

Abs has right. Here is an example with if-statements for not provided filter fields:

User.createCriteria().list {
  if (       eq("name",
  if (filter.age)        eq("age", filter.age)
  if (filter.department) eq("department", filter.department)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download