Index: trunk/grails-app/services/TaskSearchService.groovy
===================================================================
--- trunk/grails-app/services/TaskSearchService.groovy	(revision 476)
+++ trunk/grails-app/services/TaskSearchService.groovy	(revision 479)
@@ -1,2 +1,4 @@
+import grails.orm.PagedResultList
+
 /**
 * Service class that encapsulates the business logic for Task searches.
@@ -111,4 +113,9 @@
     } // getQuickSearch
 
+    /**
+    * Get all tasks that are not in the trash, by default today's tasks.
+    * @param params The request params.
+    * @param dayAdjustment The number of days to adjust from today, defaults to 0.
+    */
     def getTodays(params, dayAdjustment=0) {
         params.max = Math.min(params?.max?.toInteger() ?: 10, paramsMax)
@@ -122,11 +129,64 @@
             sort: params.sort, 
             order: params.order) {
-                ge("targetStartDate", dateUtilService.today+dayAdjustment)
                 lt("targetStartDate", dateUtilService.tomorrow+dayAdjustment)
-                eq("trash", false)
-            }
-    }
-
+                ge("targetCompletionDate", dateUtilService.today+dayAdjustment)
+                eq("trash", false)
+            }
+    }
+
+    /**
+    * Get current user's tasks, by default today's tasks.
+    * "Approved tasks where I am the lead or have been assigned"
+    * @param params The request params.
+    * @param dayAdjustment The number of days to adjust from today, defaults to 0.
+    */
     def getMyTodays(params, dayAdjustment=0) {
+        def paginateParams = [:]
+        paginateParams.max = Math.min(params?.max?.toInteger() ?: 10, paramsMax)
+        paginateParams.offset = params?.offset?.toInteger() ?: 0
+
+        def sort = "task." + (params?.sort ?: "attentionFlag")
+        def order = params?.order == "asc" ? "asc" : "desc"
+        def orderBy = " order by " + sort + ' ' + order
+
+        def namedParams = [:]
+        namedParams.currentUser = authService.currentUser
+        namedParams.startOfDay = dateUtilService.today+dayAdjustment
+        namedParams.startOfNextDay = dateUtilService.tomorrow+dayAdjustment
+
+        def baseQuery = "from Task as task \
+                                        left join task.assignedPersons as assignedPersonOfTask \
+                                        left join assignedPersonOfTask.person as assignedPerson \
+                                        left join task.assignedGroups as assignedGroupOfTask \
+                                        left join assignedGroupOfTask.personGroup as personGroup \
+                                        left join personGroup.persons as assignedPersonViaGroup \
+                                        where (task.trash = false \
+                                                    and task.approved = true \
+                                                    and ( \
+                                                            task.targetStartDate < :startOfNextDay \
+                                                            and task.targetCompletionDate >= :startOfDay \
+                                                        ) \
+                                                    and ( \
+                                                            task.leadPerson = :currentUser \
+                                                            or assignedPerson = :currentUser \
+                                                            or assignedPersonViaGroup = :currentUser \
+                                                            ) \
+                                                    )"
+
+        def searchQuery = "select distinct task " + baseQuery + orderBy
+        def list = Task.executeQuery(searchQuery, namedParams, paginateParams)
+
+        def countQuery = "select count(distinct task) as taskCount " + baseQuery
+        def totalCount = Task.executeQuery(countQuery, namedParams)[0].toInteger()
+
+        def taskInstanceList = new PagedResultList(list, totalCount)
+        return taskInstanceList
+    } // getMyTodays
+
+    /**
+    * Get all tasks that are not in the trash, during the past week.
+    * @param params The request params.
+    */
+    def getPastWeek(params) {
         params.max = Math.min(params?.max?.toInteger() ?: 10, paramsMax)
         params.offset = params?.offset?.toInteger() ?: 0
@@ -134,6 +194,4 @@
         params.order = params?.order ?: "desc"
 
-        def currentUser = authService.currentUser
-
         def taskInstanceList = Task.createCriteria().list(
             max: params.max,
@@ -141,70 +199,62 @@
             sort: params.sort, 
             order: params.order) {
-                ge("targetStartDate", dateUtilService.today+dayAdjustment)
-                lt("targetStartDate", dateUtilService.tomorrow+dayAdjustment)
-                eq("trash", false)
-                eq("approved", true)
-                or {
-                    eq("leadPerson", currentUser)
-                    assignedPersons {
-                        eq("person", currentUser)
-                    }
-                    assignedGroups {
-                        currentUser.personGroups.each() {
-                            eq("personGroup", it)
-                        }
-                    } // assignedGroups
-                } // or
-            } // criteria
+                lt("targetStartDate", dateUtilService.tomorrow)
+                ge("targetCompletionDate", dateUtilService.oneWeekAgo)
+                eq("trash", false)
+            }
+    }
+
+    /**
+    * Get current user's tasks in the past week.
+    * "Approved tasks where I am the lead or have been assigned"
+    * @param params The request params.
+    */
+    def getMyPastWeek(params) {
+        def paginateParams = [:]
+        paginateParams.max = Math.min(params?.max?.toInteger() ?: 10, paramsMax)
+        paginateParams.offset = params?.offset?.toInteger() ?: 0
+
+        def sort = "task." + (params?.sort ?: "attentionFlag")
+        def order = params?.order == "asc" ? "asc" : "desc"
+        def orderBy = " order by " + sort + ' ' + order
+
+        def namedParams = [:]
+        namedParams.currentUser = authService.currentUser
+        namedParams.startOfDay = dateUtilService.oneWeekAgo
+        namedParams.startOfNextDay = dateUtilService.tomorrow
+
+        def baseQuery = "from Task as task \
+                                        left join task.assignedPersons as assignedPersonOfTask \
+                                        left join assignedPersonOfTask.person as assignedPerson \
+                                        left join task.assignedGroups as assignedGroupOfTask \
+                                        left join assignedGroupOfTask.personGroup as personGroup \
+                                        left join personGroup.persons as assignedPersonViaGroup \
+                                        where (task.trash = false \
+                                                    and task.approved = true \
+                                                    and ( \
+                                                            task.targetStartDate < :startOfNextDay \
+                                                            and task.targetCompletionDate >= :startOfDay \
+                                                        ) \
+                                                    and ( \
+                                                            task.leadPerson = :currentUser \
+                                                            or assignedPerson = :currentUser \
+                                                            or assignedPersonViaGroup = :currentUser \
+                                                            ) \
+                                                    )"
+
+        def searchQuery = "select distinct task " + baseQuery + orderBy
+        def list = Task.executeQuery(searchQuery, namedParams, paginateParams)
+
+        def countQuery = "select count(distinct task) as taskCount " + baseQuery
+        def totalCount = Task.executeQuery(countQuery, namedParams)[0].toInteger()
+
+        def taskInstanceList = new PagedResultList(list, totalCount)
+        return taskInstanceList
     } // getMyTodays
 
-    def getPastWeek(params) {
-        params.max = Math.min(params?.max?.toInteger() ?: 10, paramsMax)
-        params.offset = params?.offset?.toInteger() ?: 0
-        params.sort = params?.sort ?: "attentionFlag"
-        params.order = params?.order ?: "desc"
-
-        def taskInstanceList = Task.createCriteria().list(
-            max: params.max,
-            offset: params.offset,
-            sort: params.sort, 
-            order: params.order) {
-                ge("targetStartDate", dateUtilService.oneWeekAgo)
-                lt("targetStartDate", dateUtilService.tomorrow)
-                eq("trash", false)
-            }
-    }
-
-    def getMyPastWeek(params) {
-        params.max = Math.min(params?.max?.toInteger() ?: 10, paramsMax)
-        params.offset = params?.offset?.toInteger() ?: 0
-        params.sort = params?.sort ?: "attentionFlag"
-        params.order = params?.order ?: "desc"
-
-        def currentUser = authService.currentUser
-
-        def taskInstanceList = Task.createCriteria().list(
-            max: params.max,
-            offset: params.offset,
-            sort: params.sort, 
-            order: params.order) {
-                ge("targetStartDate", dateUtilService.oneWeekAgo)
-                lt("targetStartDate", dateUtilService.tomorrow)
-                eq("trash", false)
-                eq("approved", true)
-                or {
-                    eq("leadPerson", currentUser)
-                    assignedPersons {
-                        eq("person", currentUser)
-                    }
-                    assignedGroups {
-                        currentUser.personGroups.each() {
-                            eq("personGroup", it)
-                        }
-                    } // assignedGroups
-                } // or
-            } // criteria
-    } // getMyPastWeek
-
+    /**
+    * "Tasks with budget status of Planned, in the past week."
+    * @param params The request params.
+    */
     def getBudgetPlanned(params) {
         params.max = Math.min(params?.max?.toInteger() ?: 10, paramsMax)
@@ -219,10 +269,14 @@
             order: params.order) {
                 eq("taskBudgetStatus", TaskBudgetStatus.read(2))
-                ge("targetStartDate", dateUtilService.oneWeekAgo)
                 lt("targetStartDate", dateUtilService.tomorrow)
-                eq("trash", false)
-            }
-    }
-
+                ge("targetCompletionDate", dateUtilService.oneWeekAgo)
+                eq("trash", false)
+            }
+    }
+
+    /**
+    * "Tasks with budget status of Unplanned, in the past week."
+    * @param params The request params.
+    */
     def getBudgetUnplanned(params) {
         params.max = Math.min(params?.max?.toInteger() ?: 10, paramsMax)
@@ -237,10 +291,14 @@
             order: params.order) {
                 eq("taskBudgetStatus", TaskBudgetStatus.read(1))
-                ge("targetStartDate", dateUtilService.oneWeekAgo)
                 lt("targetStartDate", dateUtilService.tomorrow)
-                eq("trash", false)
-            }
-    }
-
+                ge("targetCompletionDate", dateUtilService.oneWeekAgo)
+                eq("trash", false)
+            }
+    }
+
+    /**
+    * "Tasks in the past week and two weeks ahead."
+    * @param params The request params.
+    */
     def getPlannersRange(params) {
         params.max = Math.min(params?.max?.toInteger() ?: 10, paramsMax)
