| 1 | import grails.orm.PagedResultList |
|---|
| 2 | |
|---|
| 3 | /** |
|---|
| 4 | * Service class that encapsulates the business logic for Task searches. |
|---|
| 5 | */ |
|---|
| 6 | class TaskSearchService { |
|---|
| 7 | |
|---|
| 8 | boolean transactional = false |
|---|
| 9 | |
|---|
| 10 | def authService |
|---|
| 11 | def dateUtilService |
|---|
| 12 | def messageSource |
|---|
| 13 | |
|---|
| 14 | def paramsMax = 100000 |
|---|
| 15 | |
|---|
| 16 | /** |
|---|
| 17 | * Selects and returns the correct search results based on the supplied quickSearch. |
|---|
| 18 | * @param params The request params, may contain params.quickSearch string to specify the search. |
|---|
| 19 | * @param locale The locale to use when generating result.message. |
|---|
| 20 | */ |
|---|
| 21 | def getQuickSearch(params, locale) { |
|---|
| 22 | def result = [:] |
|---|
| 23 | def currentUser = authService.currentUser |
|---|
| 24 | result.quickSearch = params.quickSearch ?: "searchPlannersRange" |
|---|
| 25 | |
|---|
| 26 | def getMessage = { Map m -> |
|---|
| 27 | messageSource.getMessage(m.code, m.args == null ? null : m.args.toArray(), locale) |
|---|
| 28 | } |
|---|
| 29 | |
|---|
| 30 | switch (result.quickSearch) { |
|---|
| 31 | case "myTodays": |
|---|
| 32 | result.taskInstanceList = getMyTodays(params) |
|---|
| 33 | if(result.taskInstanceList.totalCount > 0) |
|---|
| 34 | result.message = getMessage(code:"task.search.text.my.todays", args:[currentUser]) |
|---|
| 35 | else |
|---|
| 36 | result.message = getMessage(code:"task.search.text.my.todays.none.found", args:[currentUser]) |
|---|
| 37 | break |
|---|
| 38 | case "myYesterdays": |
|---|
| 39 | result.taskInstanceList = getMyTodays(params, -1) |
|---|
| 40 | if(result.taskInstanceList.totalCount > 0) |
|---|
| 41 | result.message = getMessage(code:"task.search.text.my.yesterdays", args:[currentUser]) |
|---|
| 42 | else |
|---|
| 43 | result.message = getMessage(code:"task.search.text.my.yesterdays.none.found", args:[currentUser]) |
|---|
| 44 | break |
|---|
| 45 | case "myTomorrows": |
|---|
| 46 | result.taskInstanceList = getMyTodays(params, 1) |
|---|
| 47 | if(result.taskInstanceList.totalCount > 0) |
|---|
| 48 | result.message = getMessage(code:"task.search.text.my.tomorrows", args:[currentUser]) |
|---|
| 49 | else |
|---|
| 50 | result.message = getMessage(code:"task.search.text.my.tomorrows.none.found", args:[currentUser]) |
|---|
| 51 | break |
|---|
| 52 | case "myPastWeek": |
|---|
| 53 | result.taskInstanceList = getMyPastWeek(params) |
|---|
| 54 | if(result.taskInstanceList.totalCount > 0) |
|---|
| 55 | result.message = getMessage(code:"task.search.text.my.past.week", args:[currentUser]) |
|---|
| 56 | else |
|---|
| 57 | result.message = getMessage(code:"task.search.text.my.past.week.none.found", args:[currentUser]) |
|---|
| 58 | break |
|---|
| 59 | case "todays": |
|---|
| 60 | result.taskInstanceList = getTodays(params) |
|---|
| 61 | if(result.taskInstanceList.totalCount > 0) |
|---|
| 62 | result.message = getMessage(code:"task.search.text.todays") |
|---|
| 63 | else |
|---|
| 64 | result.message = getMessage(code:"task.search.text.todays.none.found") |
|---|
| 65 | break |
|---|
| 66 | case "yesterdays": |
|---|
| 67 | result.taskInstanceList = getTodays(params, -1) |
|---|
| 68 | if(result.taskInstanceList.totalCount > 0) |
|---|
| 69 | result.message = getMessage(code:"task.search.text.yesterdays") |
|---|
| 70 | else |
|---|
| 71 | result.message = getMessage(code:"task.search.text.yesterdays.none.found") |
|---|
| 72 | break |
|---|
| 73 | case "tomorrows": |
|---|
| 74 | result.taskInstanceList = getTodays(params, 1) |
|---|
| 75 | if(result.taskInstanceList.totalCount > 0) |
|---|
| 76 | result.message = getMessage(code:"task.search.text.tomorrows") |
|---|
| 77 | else |
|---|
| 78 | result.message = getMessage(code:"task.search.text.tomorrows.none.found") |
|---|
| 79 | break |
|---|
| 80 | case "pastWeek": |
|---|
| 81 | result.taskInstanceList = getPastWeek(params) |
|---|
| 82 | if(result.taskInstanceList.totalCount > 0) |
|---|
| 83 | result.message = getMessage(code:"task.search.text.past.week") |
|---|
| 84 | else |
|---|
| 85 | result.message = getMessage(code:"task.search.text.past.week.none.found") |
|---|
| 86 | break |
|---|
| 87 | case "budgetUnplanned": |
|---|
| 88 | result.taskInstanceList = getBudgetUnplanned(params) |
|---|
| 89 | if(result.taskInstanceList.totalCount > 0) |
|---|
| 90 | result.message = getMessage(code:"task.search.text.budget.unplanned") |
|---|
| 91 | else |
|---|
| 92 | result.message = getMessage(code:"task.search.text.budget.unplanned.none.found") |
|---|
| 93 | break |
|---|
| 94 | case "budgetPlanned": |
|---|
| 95 | result.taskInstanceList = getBudgetPlanned(params) |
|---|
| 96 | if(result.taskInstanceList.totalCount > 0) |
|---|
| 97 | result.message = getMessage(code:"task.search.text.budget.planned") |
|---|
| 98 | else |
|---|
| 99 | result.message = getMessage(code:"task.search.text.budget.planned.none.found") |
|---|
| 100 | break |
|---|
| 101 | default: |
|---|
| 102 | result.taskInstanceList = getPlannersRange(params) |
|---|
| 103 | if(result.taskInstanceList.totalCount > 0) |
|---|
| 104 | result.message = getMessage(code:"task.search.text.planners.range") |
|---|
| 105 | else |
|---|
| 106 | result.message = getMessage(code:"task.search.text.planners.range.none.found") |
|---|
| 107 | break |
|---|
| 108 | } // switch. |
|---|
| 109 | |
|---|
| 110 | // Success. |
|---|
| 111 | return result |
|---|
| 112 | |
|---|
| 113 | } // getQuickSearch |
|---|
| 114 | |
|---|
| 115 | /** |
|---|
| 116 | * Get all tasks that are not in the trash, by default today's tasks. |
|---|
| 117 | * @param params The request params. |
|---|
| 118 | * @param dayAdjustment The number of days to adjust from today, defaults to 0. |
|---|
| 119 | */ |
|---|
| 120 | def getTodays(params, dayAdjustment=0) { |
|---|
| 121 | params.max = Math.min(params?.max?.toInteger() ?: 10, paramsMax) |
|---|
| 122 | params.offset = params?.offset?.toInteger() ?: 0 |
|---|
| 123 | params.sort = params?.sort ?: "attentionFlag" |
|---|
| 124 | params.order = params?.order ?: "desc" |
|---|
| 125 | |
|---|
| 126 | def taskInstanceList = Task.createCriteria().list( |
|---|
| 127 | max: params.max, |
|---|
| 128 | offset: params.offset, |
|---|
| 129 | sort: params.sort, |
|---|
| 130 | order: params.order) { |
|---|
| 131 | lt("targetStartDate", dateUtilService.tomorrow+dayAdjustment) |
|---|
| 132 | ge("targetCompletionDate", dateUtilService.today+dayAdjustment) |
|---|
| 133 | eq("trash", false) |
|---|
| 134 | } |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | /** |
|---|
| 138 | * Get current user's tasks, by default today's tasks. |
|---|
| 139 | * "Approved tasks where I am the lead or have been assigned" |
|---|
| 140 | * @param params The request params. |
|---|
| 141 | * @param dayAdjustment The number of days to adjust from today, defaults to 0. |
|---|
| 142 | */ |
|---|
| 143 | def getMyTodays(params, dayAdjustment=0) { |
|---|
| 144 | def paginateParams = [:] |
|---|
| 145 | paginateParams.max = Math.min(params?.max?.toInteger() ?: 10, paramsMax) |
|---|
| 146 | paginateParams.offset = params?.offset?.toInteger() ?: 0 |
|---|
| 147 | |
|---|
| 148 | def sort = "task." + (params?.sort ?: "attentionFlag") |
|---|
| 149 | def order = params?.order == "asc" ? "asc" : "desc" |
|---|
| 150 | def orderBy = " order by " + sort + ' ' + order |
|---|
| 151 | |
|---|
| 152 | def namedParams = [:] |
|---|
| 153 | namedParams.currentUser = authService.currentUser |
|---|
| 154 | namedParams.startOfDay = dateUtilService.today+dayAdjustment |
|---|
| 155 | namedParams.startOfNextDay = dateUtilService.tomorrow+dayAdjustment |
|---|
| 156 | |
|---|
| 157 | def baseQuery = "from Task as task \ |
|---|
| 158 | left join task.assignedPersons as assignedPersonOfTask \ |
|---|
| 159 | left join assignedPersonOfTask.person as assignedPerson \ |
|---|
| 160 | left join task.assignedGroups as assignedGroupOfTask \ |
|---|
| 161 | left join assignedGroupOfTask.personGroup as personGroup \ |
|---|
| 162 | left join personGroup.persons as assignedPersonViaGroup \ |
|---|
| 163 | where (task.trash = false \ |
|---|
| 164 | and task.approved = true \ |
|---|
| 165 | and ( \ |
|---|
| 166 | task.targetStartDate < :startOfNextDay \ |
|---|
| 167 | and task.targetCompletionDate >= :startOfDay \ |
|---|
| 168 | ) \ |
|---|
| 169 | and ( \ |
|---|
| 170 | task.leadPerson = :currentUser \ |
|---|
| 171 | or assignedPerson = :currentUser \ |
|---|
| 172 | or assignedPersonViaGroup = :currentUser \ |
|---|
| 173 | ) \ |
|---|
| 174 | )" |
|---|
| 175 | |
|---|
| 176 | def searchQuery = "select distinct task " + baseQuery + orderBy |
|---|
| 177 | def list = Task.executeQuery(searchQuery, namedParams, paginateParams) |
|---|
| 178 | |
|---|
| 179 | def countQuery = "select count(distinct task) as taskCount " + baseQuery |
|---|
| 180 | def totalCount = Task.executeQuery(countQuery, namedParams)[0].toInteger() |
|---|
| 181 | |
|---|
| 182 | def taskInstanceList = new PagedResultList(list, totalCount) |
|---|
| 183 | return taskInstanceList |
|---|
| 184 | } // getMyTodays |
|---|
| 185 | |
|---|
| 186 | /** |
|---|
| 187 | * Get all tasks that are not in the trash, during the past week. |
|---|
| 188 | * @param params The request params. |
|---|
| 189 | */ |
|---|
| 190 | def getPastWeek(params) { |
|---|
| 191 | params.max = Math.min(params?.max?.toInteger() ?: 10, paramsMax) |
|---|
| 192 | params.offset = params?.offset?.toInteger() ?: 0 |
|---|
| 193 | params.sort = params?.sort ?: "attentionFlag" |
|---|
| 194 | params.order = params?.order ?: "desc" |
|---|
| 195 | |
|---|
| 196 | def taskInstanceList = Task.createCriteria().list( |
|---|
| 197 | max: params.max, |
|---|
| 198 | offset: params.offset, |
|---|
| 199 | sort: params.sort, |
|---|
| 200 | order: params.order) { |
|---|
| 201 | lt("targetStartDate", dateUtilService.tomorrow) |
|---|
| 202 | ge("targetCompletionDate", dateUtilService.oneWeekAgo) |
|---|
| 203 | eq("trash", false) |
|---|
| 204 | } |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | /** |
|---|
| 208 | * Get current user's tasks in the past week. |
|---|
| 209 | * "Approved tasks where I am the lead or have been assigned" |
|---|
| 210 | * @param params The request params. |
|---|
| 211 | */ |
|---|
| 212 | def getMyPastWeek(params) { |
|---|
| 213 | def paginateParams = [:] |
|---|
| 214 | paginateParams.max = Math.min(params?.max?.toInteger() ?: 10, paramsMax) |
|---|
| 215 | paginateParams.offset = params?.offset?.toInteger() ?: 0 |
|---|
| 216 | |
|---|
| 217 | def sort = "task." + (params?.sort ?: "attentionFlag") |
|---|
| 218 | def order = params?.order == "asc" ? "asc" : "desc" |
|---|
| 219 | def orderBy = " order by " + sort + ' ' + order |
|---|
| 220 | |
|---|
| 221 | def namedParams = [:] |
|---|
| 222 | namedParams.currentUser = authService.currentUser |
|---|
| 223 | namedParams.startOfDay = dateUtilService.oneWeekAgo |
|---|
| 224 | namedParams.startOfNextDay = dateUtilService.tomorrow |
|---|
| 225 | |
|---|
| 226 | def baseQuery = "from Task as task \ |
|---|
| 227 | left join task.assignedPersons as assignedPersonOfTask \ |
|---|
| 228 | left join assignedPersonOfTask.person as assignedPerson \ |
|---|
| 229 | left join task.assignedGroups as assignedGroupOfTask \ |
|---|
| 230 | left join assignedGroupOfTask.personGroup as personGroup \ |
|---|
| 231 | left join personGroup.persons as assignedPersonViaGroup \ |
|---|
| 232 | where (task.trash = false \ |
|---|
| 233 | and task.approved = true \ |
|---|
| 234 | and ( \ |
|---|
| 235 | task.targetStartDate < :startOfNextDay \ |
|---|
| 236 | and task.targetCompletionDate >= :startOfDay \ |
|---|
| 237 | ) \ |
|---|
| 238 | and ( \ |
|---|
| 239 | task.leadPerson = :currentUser \ |
|---|
| 240 | or assignedPerson = :currentUser \ |
|---|
| 241 | or assignedPersonViaGroup = :currentUser \ |
|---|
| 242 | ) \ |
|---|
| 243 | )" |
|---|
| 244 | |
|---|
| 245 | def searchQuery = "select distinct task " + baseQuery + orderBy |
|---|
| 246 | def list = Task.executeQuery(searchQuery, namedParams, paginateParams) |
|---|
| 247 | |
|---|
| 248 | def countQuery = "select count(distinct task) as taskCount " + baseQuery |
|---|
| 249 | def totalCount = Task.executeQuery(countQuery, namedParams)[0].toInteger() |
|---|
| 250 | |
|---|
| 251 | def taskInstanceList = new PagedResultList(list, totalCount) |
|---|
| 252 | return taskInstanceList |
|---|
| 253 | } // getMyTodays |
|---|
| 254 | |
|---|
| 255 | /** |
|---|
| 256 | * "Tasks with budget status of Planned, in the past week." |
|---|
| 257 | * @param params The request params. |
|---|
| 258 | */ |
|---|
| 259 | def getBudgetPlanned(params) { |
|---|
| 260 | params.max = Math.min(params?.max?.toInteger() ?: 10, paramsMax) |
|---|
| 261 | params.offset = params?.offset?.toInteger() ?: 0 |
|---|
| 262 | params.sort = params?.sort ?: "targetStartDate" |
|---|
| 263 | params.order = params?.order ?: "asc" |
|---|
| 264 | |
|---|
| 265 | def taskInstanceList = Task.createCriteria().list( |
|---|
| 266 | max: params.max, |
|---|
| 267 | offset: params.offset, |
|---|
| 268 | sort: params.sort, |
|---|
| 269 | order: params.order) { |
|---|
| 270 | eq("taskBudgetStatus", TaskBudgetStatus.read(2)) |
|---|
| 271 | lt("targetStartDate", dateUtilService.tomorrow) |
|---|
| 272 | ge("targetCompletionDate", dateUtilService.oneWeekAgo) |
|---|
| 273 | eq("trash", false) |
|---|
| 274 | } |
|---|
| 275 | } |
|---|
| 276 | |
|---|
| 277 | /** |
|---|
| 278 | * "Tasks with budget status of Unplanned, in the past week." |
|---|
| 279 | * @param params The request params. |
|---|
| 280 | */ |
|---|
| 281 | def getBudgetUnplanned(params) { |
|---|
| 282 | params.max = Math.min(params?.max?.toInteger() ?: 10, paramsMax) |
|---|
| 283 | params.offset = params?.offset?.toInteger() ?: 0 |
|---|
| 284 | params.sort = params?.sort ?: "targetStartDate" |
|---|
| 285 | params.order = params?.order ?: "asc" |
|---|
| 286 | |
|---|
| 287 | def taskInstanceList = Task.createCriteria().list( |
|---|
| 288 | max: params.max, |
|---|
| 289 | offset: params.offset, |
|---|
| 290 | sort: params.sort, |
|---|
| 291 | order: params.order) { |
|---|
| 292 | eq("taskBudgetStatus", TaskBudgetStatus.read(1)) |
|---|
| 293 | lt("targetStartDate", dateUtilService.tomorrow) |
|---|
| 294 | ge("targetCompletionDate", dateUtilService.oneWeekAgo) |
|---|
| 295 | eq("trash", false) |
|---|
| 296 | } |
|---|
| 297 | } |
|---|
| 298 | |
|---|
| 299 | /** |
|---|
| 300 | * "Tasks in the past week and two weeks ahead." |
|---|
| 301 | * @param params The request params. |
|---|
| 302 | */ |
|---|
| 303 | def getPlannersRange(params) { |
|---|
| 304 | params.max = Math.min(params?.max?.toInteger() ?: 10, paramsMax) |
|---|
| 305 | params.offset = params?.offset?.toInteger() ?: 0 |
|---|
| 306 | params.sort = params?.sort ?: "targetStartDate" |
|---|
| 307 | params.order = params?.order ?: "asc" |
|---|
| 308 | |
|---|
| 309 | def taskInstanceList = Task.createCriteria().list( |
|---|
| 310 | max: params.max, |
|---|
| 311 | offset: params.offset, |
|---|
| 312 | sort: params.sort, |
|---|
| 313 | order: params.order) { |
|---|
| 314 | ge("targetStartDate", dateUtilService.oneWeekAgo) |
|---|
| 315 | lt("targetStartDate", dateUtilService.today + 15) |
|---|
| 316 | eq("trash", false) |
|---|
| 317 | } |
|---|
| 318 | } |
|---|
| 319 | } |
|---|