1 | /** |
---|
2 | * Service class that encapsulates the business logic for Task searches. |
---|
3 | */ |
---|
4 | class TaskSearchService { |
---|
5 | |
---|
6 | boolean transactional = false |
---|
7 | |
---|
8 | def authService |
---|
9 | def dateUtilService |
---|
10 | def messageSource |
---|
11 | |
---|
12 | def paramsMax = 100000 |
---|
13 | |
---|
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) { |
---|
114 | params.max = Math.min(params?.max?.toInteger() ?: 10, paramsMax) |
---|
115 | params.offset = params?.offset?.toInteger() ?: 0 |
---|
116 | params.sort = params?.sort ?: "attentionFlag" |
---|
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) { |
---|
124 | ge("targetStartDate", dateUtilService.today+dayAdjustment) |
---|
125 | lt("targetStartDate", dateUtilService.tomorrow+dayAdjustment) |
---|
126 | eq("trash", false) |
---|
127 | } |
---|
128 | } |
---|
129 | |
---|
130 | def getMyTodays(params, dayAdjustment=0) { |
---|
131 | params.max = Math.min(params?.max?.toInteger() ?: 10, paramsMax) |
---|
132 | params.offset = params?.offset?.toInteger() ?: 0 |
---|
133 | params.sort = params?.sort ?: "attentionFlag" |
---|
134 | params.order = params?.order ?: "desc" |
---|
135 | |
---|
136 | def currentUser = authService.currentUser |
---|
137 | |
---|
138 | def taskInstanceList = Task.createCriteria().list( |
---|
139 | max: params.max, |
---|
140 | offset: params.offset, |
---|
141 | sort: params.sort, |
---|
142 | order: params.order) { |
---|
143 | ge("targetStartDate", dateUtilService.today+dayAdjustment) |
---|
144 | lt("targetStartDate", dateUtilService.tomorrow+dayAdjustment) |
---|
145 | eq("trash", false) |
---|
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 |
---|
160 | |
---|
161 | def getPastWeek(params) { |
---|
162 | params.max = Math.min(params?.max?.toInteger() ?: 10, paramsMax) |
---|
163 | params.offset = params?.offset?.toInteger() ?: 0 |
---|
164 | params.sort = params?.sort ?: "attentionFlag" |
---|
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) { |
---|
172 | ge("targetStartDate", dateUtilService.oneWeekAgo) |
---|
173 | lt("targetStartDate", dateUtilService.tomorrow) |
---|
174 | eq("trash", false) |
---|
175 | } |
---|
176 | } |
---|
177 | |
---|
178 | def getMyPastWeek(params) { |
---|
179 | params.max = Math.min(params?.max?.toInteger() ?: 10, paramsMax) |
---|
180 | params.offset = params?.offset?.toInteger() ?: 0 |
---|
181 | params.sort = params?.sort ?: "attentionFlag" |
---|
182 | params.order = params?.order ?: "desc" |
---|
183 | |
---|
184 | def currentUser = authService.currentUser |
---|
185 | |
---|
186 | def taskInstanceList = Task.createCriteria().list( |
---|
187 | max: params.max, |
---|
188 | offset: params.offset, |
---|
189 | sort: params.sort, |
---|
190 | order: params.order) { |
---|
191 | ge("targetStartDate", dateUtilService.oneWeekAgo) |
---|
192 | lt("targetStartDate", dateUtilService.tomorrow) |
---|
193 | eq("trash", false) |
---|
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 |
---|
208 | |
---|
209 | def getBudgetPlanned(params) { |
---|
210 | params.max = Math.min(params?.max?.toInteger() ?: 10, paramsMax) |
---|
211 | params.offset = params?.offset?.toInteger() ?: 0 |
---|
212 | params.sort = params?.sort ?: "targetStartDate" |
---|
213 | params.order = params?.order ?: "asc" |
---|
214 | |
---|
215 | def taskInstanceList = Task.createCriteria().list( |
---|
216 | max: params.max, |
---|
217 | offset: params.offset, |
---|
218 | sort: params.sort, |
---|
219 | order: params.order) { |
---|
220 | eq("taskBudgetStatus", TaskBudgetStatus.read(2)) |
---|
221 | ge("targetStartDate", dateUtilService.oneWeekAgo) |
---|
222 | lt("targetStartDate", dateUtilService.tomorrow) |
---|
223 | eq("trash", false) |
---|
224 | } |
---|
225 | } |
---|
226 | |
---|
227 | def getBudgetUnplanned(params) { |
---|
228 | params.max = Math.min(params?.max?.toInteger() ?: 10, paramsMax) |
---|
229 | params.offset = params?.offset?.toInteger() ?: 0 |
---|
230 | params.sort = params?.sort ?: "targetStartDate" |
---|
231 | params.order = params?.order ?: "asc" |
---|
232 | |
---|
233 | def taskInstanceList = Task.createCriteria().list( |
---|
234 | max: params.max, |
---|
235 | offset: params.offset, |
---|
236 | sort: params.sort, |
---|
237 | order: params.order) { |
---|
238 | eq("taskBudgetStatus", TaskBudgetStatus.read(1)) |
---|
239 | ge("targetStartDate", dateUtilService.oneWeekAgo) |
---|
240 | lt("targetStartDate", dateUtilService.tomorrow) |
---|
241 | eq("trash", false) |
---|
242 | } |
---|
243 | } |
---|
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 | } |
---|
261 | } |
---|