Dynamically creating a request based on parameters passed to the controller

In my task management application, users should be able to filter based on the tasks: assignedTo, priority, statusand / ordueDate

I'm not sure how to create a dynamic query so that it builds the query based on the available parameters.

For instance:

If I have a url like: task/index?assignedTo=1&status=2

I can build a query based on only these two parameters. The method I'm used to is

Task.findAllByAssignedToAndStatus(
   User.get(params.assignedTo),
   TaskStatus.get(params.status)
)

I obviously don’t want to implement the DRY method, writing out each findAllByrequest for every possible combination of URL parameters.

Is there a good way to do this in the grail?

+5
source share
5 answers

, createCriteria :

0 , "" , where, eq() .

:

else
    {       
        def assignedTo = Integer.parseInt(taskParams.assignedTo)
        def priority = Integer.parseInt(taskParams.priority)
        def status = Integer.parseInt(taskParams.status)

        tasks = Task.createCriteria().list {            

            eq("project", Project.get(taskParams.PId))

            if(assignedTo > 0)
                eq("assignedTo", User.get(assignedTo))

            if(priority > 0)
                eq("priority", TaskPriority.get(priority))

            if(status > 0)
                eq("status", TaskStatus.get(status))                
        }           
    }
    return tasks
}
+5

. , namedQueries .

:

class Employee {

    String firstname
    String lastname
    Integer age

    static constraints = {
    }

    static namedQueries = {
        filteronFirstname { String inFirstname ->
            if (inFirstname&& inFirstname?.size() > 0) {
                ilike 'firstname', "%${inFirstname}%"
            }
        }

        filteronLastname { String inLastname ->
            if (inLastname && inLastname?.size() > 0) {
                ilike 'lastname', "%${inLastname}%"
            }
        }

        filteronAgeOlderThen { String ageOlderThen ->
            if (age && age ?.size() > 0) {
                gt 'age', ageOlderThen as Integer
            }
        }

    }
}

, 1 , , , , .

Employee.filteronFirstname("John").
filterOnLastname("Doe").
filteronAgeOlderThen("10").
list(params)
+7

I would consider the Searchable plugin for this ( http://grails.org/plugin/searchable ). It is easy to specify which properties Taskshould be searchable.

+1
source

Take a look at the free interfaces, you can use them to add or remove (filter) each parameter.

http://en.wikipedia.org/wiki/Fluent_interface

0
source

My solution for dynamic Where query from URL:

if (params.query) {
    def classLoader = getClass().getClassLoader();
    def query = new GroovyShell(classLoader).evaluate(
            "import myapp.Visit; Visit.where {$params.query}")
    visits = query.list(params)
} 

This takes its limitations from such a URL:

visit/index?query=client.status=='suspect';atHome==false
0
source

All Articles