[476] | 1 | /** |
---|
| 2 | * Service class that encapsulates the business logic for Task searches. |
---|
| 3 | */ |
---|
[143] | 4 | class TaskSearchService { |
---|
| 5 | |
---|
| 6 | boolean transactional = false |
---|
| 7 | |
---|
[476] | 8 | def authService |
---|
[143] | 9 | def dateUtilService |
---|
[476] | 10 | def messageSource |
---|
[143] | 11 | |
---|
[476] | 12 | def paramsMax = 100000 |
---|
[260] | 13 | |
---|
[476] | 14 | /** |
---|
| 15 | * Selects and returns the correct search results based on the supplied quickSearch. |
---|
| 16 | * @param params The request params, may contain params.quickSearch string to specify the search. |
---|
| 17 | * @param locale The locale to use when generating result.message. |
---|
| 18 | */ |
---|
| 19 | def getQuickSearch(params, locale) { |
---|
| 20 | def result = [:] |
---|
| 21 | def currentUser = authService.currentUser |
---|
| 22 | result.quickSearch = params.quickSearch ?: "searchPlannersRange" |
---|
| 23 | |
---|
| 24 | def getMessage = { Map m -> |
---|
| 25 | messageSource.getMessage(m.code, m.args == null ? null : m.args.toArray(), locale) |
---|
| 26 | } |
---|
| 27 | |
---|
| 28 | switch (result.quickSearch) { |
---|
| 29 | case "myTodays": |
---|
| 30 | result.taskInstanceList = getMyTodays(params) |
---|
| 31 | if(result.taskInstanceList.totalCount > 0) |
---|
| 32 | result.message = getMessage(code:"task.search.text.my.todays", args:[currentUser]) |
---|
| 33 | else |
---|
| 34 | result.message = getMessage(code:"task.search.text.my.todays.none.found", args:[currentUser]) |
---|
| 35 | break |
---|
| 36 | case "myYesterdays": |
---|
| 37 | result.taskInstanceList = getMyTodays(params, -1) |
---|
| 38 | if(result.taskInstanceList.totalCount > 0) |
---|
| 39 | result.message = getMessage(code:"task.search.text.my.yesterdays", args:[currentUser]) |
---|
| 40 | else |
---|
| 41 | result.message = getMessage(code:"task.search.text.my.yesterdays.none.found", args:[currentUser]) |
---|
| 42 | break |
---|
| 43 | case "myTomorrows": |
---|
| 44 | result.taskInstanceList = getMyTodays(params, 1) |
---|
| 45 | if(result.taskInstanceList.totalCount > 0) |
---|
| 46 | result.message = getMessage(code:"task.search.text.my.tomorrows", args:[currentUser]) |
---|
| 47 | else |
---|
| 48 | result.message = getMessage(code:"task.search.text.my.tomorrows.none.found", args:[currentUser]) |
---|
| 49 | break |
---|
| 50 | case "myPastWeek": |
---|
| 51 | result.taskInstanceList = getMyPastWeek(params) |
---|
| 52 | if(result.taskInstanceList.totalCount > 0) |
---|
| 53 | result.message = getMessage(code:"task.search.text.my.past.week", args:[currentUser]) |
---|
| 54 | else |
---|
| 55 | result.message = getMessage(code:"task.search.text.my.past.week.none.found", args:[currentUser]) |
---|
| 56 | break |
---|
| 57 | case "todays": |
---|
| 58 | result.taskInstanceList = getTodays(params) |
---|
| 59 | if(result.taskInstanceList.totalCount > 0) |
---|
| 60 | result.message = getMessage(code:"task.search.text.todays") |
---|
| 61 | else |
---|
| 62 | result.message = getMessage(code:"task.search.text.todays.none.found") |
---|
| 63 | break |
---|
| 64 | case "yesterdays": |
---|
| 65 | result.taskInstanceList = getTodays(params, -1) |
---|
| 66 | if(result.taskInstanceList.totalCount > 0) |
---|
| 67 | result.message = getMessage(code:"task.search.text.yesterdays") |
---|
| 68 | else |
---|
| 69 | result.message = getMessage(code:"task.search.text.yesterdays.none.found") |
---|
| 70 | break |
---|
| 71 | case "tomorrows": |
---|
| 72 | result.taskInstanceList = getTodays(params, 1) |
---|
| 73 | if(result.taskInstanceList.totalCount > 0) |
---|
| 74 | result.message = getMessage(code:"task.search.text.tomorrows") |
---|
| 75 | else |
---|
| 76 | result.message = getMessage(code:"task.search.text.tomorrows.none.found") |
---|
| 77 | break |
---|
| 78 | case "pastWeek": |
---|
| 79 | result.taskInstanceList = getPastWeek(params) |
---|
| 80 | if(result.taskInstanceList.totalCount > 0) |
---|
| 81 | result.message = getMessage(code:"task.search.text.past.week") |
---|
| 82 | else |
---|
| 83 | result.message = getMessage(code:"task.search.text.past.week.none.found") |
---|
| 84 | break |
---|
| 85 | case "budgetUnplanned": |
---|
| 86 | result.taskInstanceList = getBudgetUnplanned(params) |
---|
| 87 | if(result.taskInstanceList.totalCount > 0) |
---|
| 88 | result.message = getMessage(code:"task.search.text.budget.unplanned") |
---|
| 89 | else |
---|
| 90 | result.message = getMessage(code:"task.search.text.budget.unplanned.none.found") |
---|
| 91 | break |
---|
| 92 | case "budgetPlanned": |
---|
| 93 | result.taskInstanceList = getBudgetPlanned(params) |
---|
| 94 | if(result.taskInstanceList.totalCount > 0) |
---|
| 95 | result.message = getMessage(code:"task.search.text.budget.planned") |
---|
| 96 | else |
---|
| 97 | result.message = getMessage(code:"task.search.text.budget.planned.none.found") |
---|
| 98 | break |
---|
| 99 | default: |
---|
| 100 | result.taskInstanceList = getPlannersRange(params) |
---|
| 101 | if(result.taskInstanceList.totalCount > 0) |
---|
| 102 | result.message = getMessage(code:"task.search.text.planners.range") |
---|
| 103 | else |
---|
| 104 | result.message = getMessage(code:"task.search.text.planners.range.none.found") |
---|
| 105 | break |
---|
| 106 | } // switch. |
---|
| 107 | |
---|
| 108 | // Success. |
---|
| 109 | return result |
---|
| 110 | |
---|
| 111 | } // getQuickSearch |
---|
| 112 | |
---|
| 113 | def getTodays(params, dayAdjustment=0) { |
---|
[260] | 114 | params.max = Math.min(params?.max?.toInteger() ?: 10, paramsMax) |
---|
[143] | 115 | params.offset = params?.offset?.toInteger() ?: 0 |
---|
[419] | 116 | params.sort = params?.sort ?: "attentionFlag" |
---|
[143] | 117 | params.order = params?.order ?: "desc" |
---|
| 118 | |
---|
| 119 | def taskInstanceList = Task.createCriteria().list( |
---|
| 120 | max: params.max, |
---|
| 121 | offset: params.offset, |
---|
| 122 | sort: params.sort, |
---|
| 123 | order: params.order) { |
---|
[476] | 124 | ge("targetStartDate", dateUtilService.today+dayAdjustment) |
---|
| 125 | lt("targetStartDate", dateUtilService.tomorrow+dayAdjustment) |
---|
[181] | 126 | eq("trash", false) |
---|
[143] | 127 | } |
---|
| 128 | } |
---|
| 129 | |
---|
[476] | 130 | def getMyTodays(params, dayAdjustment=0) { |
---|
[260] | 131 | params.max = Math.min(params?.max?.toInteger() ?: 10, paramsMax) |
---|
[143] | 132 | params.offset = params?.offset?.toInteger() ?: 0 |
---|
[419] | 133 | params.sort = params?.sort ?: "attentionFlag" |
---|
[143] | 134 | params.order = params?.order ?: "desc" |
---|
| 135 | |
---|
[476] | 136 | def currentUser = authService.currentUser |
---|
| 137 | |
---|
[143] | 138 | def taskInstanceList = Task.createCriteria().list( |
---|
| 139 | max: params.max, |
---|
| 140 | offset: params.offset, |
---|
| 141 | sort: params.sort, |
---|
| 142 | order: params.order) { |
---|
[476] | 143 | ge("targetStartDate", dateUtilService.today+dayAdjustment) |
---|
| 144 | lt("targetStartDate", dateUtilService.tomorrow+dayAdjustment) |
---|
[181] | 145 | eq("trash", false) |
---|
[476] | 146 | eq("approved", true) |
---|
| 147 | or { |
---|
| 148 | eq("leadPerson", currentUser) |
---|
| 149 | assignedPersons { |
---|
| 150 | eq("person", currentUser) |
---|
| 151 | } |
---|
| 152 | assignedGroups { |
---|
| 153 | currentUser.personGroups.each() { |
---|
| 154 | eq("personGroup", it) |
---|
| 155 | } |
---|
| 156 | } // assignedGroups |
---|
| 157 | } // or |
---|
| 158 | } // criteria |
---|
| 159 | } // getMyTodays |
---|
[144] | 160 | |
---|
[476] | 161 | def getPastWeek(params) { |
---|
[260] | 162 | params.max = Math.min(params?.max?.toInteger() ?: 10, paramsMax) |
---|
[144] | 163 | params.offset = params?.offset?.toInteger() ?: 0 |
---|
[419] | 164 | params.sort = params?.sort ?: "attentionFlag" |
---|
[144] | 165 | params.order = params?.order ?: "desc" |
---|
| 166 | |
---|
| 167 | def taskInstanceList = Task.createCriteria().list( |
---|
| 168 | max: params.max, |
---|
| 169 | offset: params.offset, |
---|
| 170 | sort: params.sort, |
---|
| 171 | order: params.order) { |
---|
[219] | 172 | ge("targetStartDate", dateUtilService.oneWeekAgo) |
---|
[476] | 173 | lt("targetStartDate", dateUtilService.tomorrow) |
---|
[181] | 174 | eq("trash", false) |
---|
[144] | 175 | } |
---|
| 176 | } |
---|
| 177 | |
---|
[476] | 178 | def getMyPastWeek(params) { |
---|
[260] | 179 | params.max = Math.min(params?.max?.toInteger() ?: 10, paramsMax) |
---|
[144] | 180 | params.offset = params?.offset?.toInteger() ?: 0 |
---|
[419] | 181 | params.sort = params?.sort ?: "attentionFlag" |
---|
[144] | 182 | params.order = params?.order ?: "desc" |
---|
| 183 | |
---|
[476] | 184 | def currentUser = authService.currentUser |
---|
| 185 | |
---|
[144] | 186 | def taskInstanceList = Task.createCriteria().list( |
---|
| 187 | max: params.max, |
---|
| 188 | offset: params.offset, |
---|
| 189 | sort: params.sort, |
---|
| 190 | order: params.order) { |
---|
[219] | 191 | ge("targetStartDate", dateUtilService.oneWeekAgo) |
---|
[476] | 192 | lt("targetStartDate", dateUtilService.tomorrow) |
---|
[181] | 193 | eq("trash", false) |
---|
[476] | 194 | eq("approved", true) |
---|
| 195 | or { |
---|
| 196 | eq("leadPerson", currentUser) |
---|
| 197 | assignedPersons { |
---|
| 198 | eq("person", currentUser) |
---|
| 199 | } |
---|
| 200 | assignedGroups { |
---|
| 201 | currentUser.personGroups.each() { |
---|
| 202 | eq("personGroup", it) |
---|
| 203 | } |
---|
| 204 | } // assignedGroups |
---|
| 205 | } // or |
---|
| 206 | } // criteria |
---|
| 207 | } // getMyPastWeek |
---|
[165] | 208 | |
---|
| 209 | def getBudgetPlanned(params) { |
---|
[260] | 210 | params.max = Math.min(params?.max?.toInteger() ?: 10, paramsMax) |
---|
[165] | 211 | params.offset = params?.offset?.toInteger() ?: 0 |
---|
[476] | 212 | params.sort = params?.sort ?: "targetStartDate" |
---|
| 213 | params.order = params?.order ?: "asc" |
---|
[165] | 214 | |
---|
| 215 | def taskInstanceList = Task.createCriteria().list( |
---|
| 216 | max: params.max, |
---|
| 217 | offset: params.offset, |
---|
| 218 | sort: params.sort, |
---|
| 219 | order: params.order) { |
---|
[476] | 220 | eq("taskBudgetStatus", TaskBudgetStatus.read(2)) |
---|
[219] | 221 | ge("targetStartDate", dateUtilService.oneWeekAgo) |
---|
[252] | 222 | lt("targetStartDate", dateUtilService.tomorrow) |
---|
[181] | 223 | eq("trash", false) |
---|
[165] | 224 | } |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | def getBudgetUnplanned(params) { |
---|
[260] | 228 | params.max = Math.min(params?.max?.toInteger() ?: 10, paramsMax) |
---|
[165] | 229 | params.offset = params?.offset?.toInteger() ?: 0 |
---|
[476] | 230 | params.sort = params?.sort ?: "targetStartDate" |
---|
| 231 | params.order = params?.order ?: "asc" |
---|
[165] | 232 | |
---|
| 233 | def taskInstanceList = Task.createCriteria().list( |
---|
| 234 | max: params.max, |
---|
| 235 | offset: params.offset, |
---|
| 236 | sort: params.sort, |
---|
| 237 | order: params.order) { |
---|
[476] | 238 | eq("taskBudgetStatus", TaskBudgetStatus.read(1)) |
---|
[219] | 239 | ge("targetStartDate", dateUtilService.oneWeekAgo) |
---|
[476] | 240 | lt("targetStartDate", dateUtilService.tomorrow) |
---|
[181] | 241 | eq("trash", false) |
---|
[165] | 242 | } |
---|
| 243 | } |
---|
[476] | 244 | |
---|
| 245 | def getPlannersRange(params) { |
---|
| 246 | params.max = Math.min(params?.max?.toInteger() ?: 10, paramsMax) |
---|
| 247 | params.offset = params?.offset?.toInteger() ?: 0 |
---|
| 248 | params.sort = params?.sort ?: "targetStartDate" |
---|
| 249 | params.order = params?.order ?: "asc" |
---|
| 250 | |
---|
| 251 | def taskInstanceList = Task.createCriteria().list( |
---|
| 252 | max: params.max, |
---|
| 253 | offset: params.offset, |
---|
| 254 | sort: params.sort, |
---|
| 255 | order: params.order) { |
---|
| 256 | ge("targetStartDate", dateUtilService.oneWeekAgo) |
---|
| 257 | lt("targetStartDate", dateUtilService.today + 15) |
---|
| 258 | eq("trash", false) |
---|
| 259 | } |
---|
| 260 | } |
---|
[143] | 261 | } |
---|