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