1 | class TaskService { |
---|
2 | |
---|
3 | boolean transactional = false |
---|
4 | |
---|
5 | def dateUtilService |
---|
6 | def personService |
---|
7 | |
---|
8 | def create(params) { |
---|
9 | Task.withTransaction { status -> |
---|
10 | def result = [:] |
---|
11 | // Default status to "not started" if not supplied. |
---|
12 | params.taskStatus = params.taskStatus ?: TaskStatus.get(1) |
---|
13 | def taskInstance = new Task(params) |
---|
14 | result.taskInstance = taskInstance |
---|
15 | |
---|
16 | if(taskInstance.save()) { |
---|
17 | def taskModification = new TaskModification(person: personService.currentUser(), |
---|
18 | taskModificationType: TaskModificationType.get(1), |
---|
19 | task: taskInstance) |
---|
20 | |
---|
21 | if(!taskModification.save()) { |
---|
22 | status.setRollbackOnly() |
---|
23 | taskInstance.errors.rejectValue("taskModifications", "task.modifications.failedToSave") |
---|
24 | result.error = true |
---|
25 | return result |
---|
26 | } |
---|
27 | |
---|
28 | return result |
---|
29 | } |
---|
30 | else { |
---|
31 | result.error = true |
---|
32 | return result |
---|
33 | } |
---|
34 | |
---|
35 | } //end withTransaction |
---|
36 | } // end create() |
---|
37 | |
---|
38 | def start() { |
---|
39 | //TaskModificationType.get(2) |
---|
40 | } // end start() |
---|
41 | |
---|
42 | def update(params) { |
---|
43 | Task.withTransaction { status -> |
---|
44 | def result = [:] |
---|
45 | result.taskInstance = Task.get(params.id) |
---|
46 | if(result.taskInstance) { |
---|
47 | |
---|
48 | // Optimistic locking check. |
---|
49 | if(params.version) { |
---|
50 | def version = params.version.toLong() |
---|
51 | if(result.taskInstance.version > version) { |
---|
52 | status.setRollbackOnly() |
---|
53 | result.taskInstance.errors.rejectValue("version", "task.optimistic.locking.failure", "Another user has updated this Task while you were editing.") |
---|
54 | result.error = true |
---|
55 | return result |
---|
56 | } |
---|
57 | } |
---|
58 | |
---|
59 | result.taskInstance.properties = params |
---|
60 | |
---|
61 | if(result.taskInstance.save()) { |
---|
62 | def taskModification = new TaskModification(person:personService.currentUser(), |
---|
63 | taskModificationType: TaskModificationType.get(3), |
---|
64 | task: result.taskInstance) |
---|
65 | if(taskModification.save()) { |
---|
66 | // All went well. |
---|
67 | return result |
---|
68 | } |
---|
69 | else { |
---|
70 | status.setRollbackOnly() |
---|
71 | result.taskInstance.errors.rejectValue("taskModifications", "task.modifications.failedToSave") |
---|
72 | result.error = true |
---|
73 | return result |
---|
74 | } |
---|
75 | } |
---|
76 | } |
---|
77 | // Something failed. |
---|
78 | status.setRollbackOnly() |
---|
79 | result.error = true |
---|
80 | return result |
---|
81 | |
---|
82 | } //end withTransaction |
---|
83 | } // end update() |
---|
84 | |
---|
85 | def complete(params) { |
---|
86 | Task.withTransaction { status -> |
---|
87 | def result = [:] |
---|
88 | result.taskInstance = Task.get(params.id) |
---|
89 | if(result.taskInstance) { |
---|
90 | |
---|
91 | // Optimistic locking check. |
---|
92 | if(params.version) { |
---|
93 | def version = params.version.toLong() |
---|
94 | if(result.taskInstance.version > version) { |
---|
95 | status.setRollbackOnly() |
---|
96 | result.taskInstance.errors.rejectValue("version", "task.optimistic.locking.failure", "Another user has updated this Task while you were editing.") |
---|
97 | result.error = true |
---|
98 | return result |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | result.taskInstance.taskStatus = TaskStatus.get(3) |
---|
103 | |
---|
104 | if(result.taskInstance.save()) { |
---|
105 | def taskModification = new TaskModification(person:personService.currentUser(), |
---|
106 | taskModificationType: TaskModificationType.get(4), |
---|
107 | task: result.taskInstance) |
---|
108 | if(taskModification.save()) { |
---|
109 | // All went well. |
---|
110 | return result |
---|
111 | } |
---|
112 | else { |
---|
113 | status.setRollbackOnly() |
---|
114 | result.taskInstance.errors.rejectValue("taskModifications", "task.modifications.failedToSave") |
---|
115 | result.error = true |
---|
116 | return result |
---|
117 | } |
---|
118 | } |
---|
119 | } |
---|
120 | // Something failed. |
---|
121 | status.setRollbackOnly() |
---|
122 | result.error = true |
---|
123 | return result |
---|
124 | |
---|
125 | } //end withTransaction |
---|
126 | } // end complete() |
---|
127 | |
---|
128 | def reopen(params) { |
---|
129 | Task.withTransaction { status -> |
---|
130 | def result = [:] |
---|
131 | result.taskInstance = Task.get(params.id) |
---|
132 | if(result.taskInstance) { |
---|
133 | |
---|
134 | // Optimistic locking check. |
---|
135 | if(params.version) { |
---|
136 | def version = params.version.toLong() |
---|
137 | if(result.taskInstance.version > version) { |
---|
138 | status.setRollbackOnly() |
---|
139 | result.taskInstance.errors.rejectValue("version", "task.optimistic.locking.failure", "Another user has updated this Task while you were editing.") |
---|
140 | result.error = true |
---|
141 | return result |
---|
142 | } |
---|
143 | } |
---|
144 | |
---|
145 | result.taskInstance.taskStatus = TaskStatus.get(2) |
---|
146 | |
---|
147 | if(result.taskInstance.save()) { |
---|
148 | def taskModification = new TaskModification(person:personService.currentUser(), |
---|
149 | taskModificationType: TaskModificationType.get(5), |
---|
150 | task: result.taskInstance) |
---|
151 | if(taskModification.save()) { |
---|
152 | // All went well. |
---|
153 | return result |
---|
154 | } |
---|
155 | else { |
---|
156 | status.setRollbackOnly() |
---|
157 | result.taskInstance.errors.rejectValue("taskModifications", "task.modifications.failedToSave") |
---|
158 | result.error = true |
---|
159 | return result |
---|
160 | } |
---|
161 | } |
---|
162 | } |
---|
163 | // Something failed. |
---|
164 | status.setRollbackOnly() |
---|
165 | result.error = true |
---|
166 | return result |
---|
167 | |
---|
168 | } //end withTransaction |
---|
169 | } // end reopen() |
---|
170 | |
---|
171 | def trash(params) { |
---|
172 | Task.withTransaction { status -> |
---|
173 | def result = [:] |
---|
174 | result.taskInstance = Task.get(params.id) |
---|
175 | if(result.taskInstance) { |
---|
176 | |
---|
177 | // Optimistic locking check. |
---|
178 | if(params.version) { |
---|
179 | def version = params.version.toLong() |
---|
180 | if(result.taskInstance.version > version) { |
---|
181 | status.setRollbackOnly() |
---|
182 | result.taskInstance.errors.rejectValue("version", "task.optimistic.locking.failure", "Another user has updated this Task while you were editing.") |
---|
183 | result.error = true |
---|
184 | return result |
---|
185 | } |
---|
186 | } |
---|
187 | |
---|
188 | result.taskInstance.trash = true |
---|
189 | |
---|
190 | if(result.taskInstance.save()) { |
---|
191 | def taskModification = new TaskModification(person:personService.currentUser(), |
---|
192 | taskModificationType: TaskModificationType.get(6), |
---|
193 | task: result.taskInstance) |
---|
194 | if(taskModification.save()) { |
---|
195 | // All went well. |
---|
196 | return result |
---|
197 | } |
---|
198 | else { |
---|
199 | status.setRollbackOnly() |
---|
200 | result.taskInstance.errors.rejectValue("taskModifications", "task.modifications.failedToSave") |
---|
201 | result.error = true |
---|
202 | return result |
---|
203 | } |
---|
204 | } |
---|
205 | } |
---|
206 | // Something failed. |
---|
207 | status.setRollbackOnly() |
---|
208 | result.error = true |
---|
209 | return result |
---|
210 | |
---|
211 | } //end withTransaction |
---|
212 | } // end trash() |
---|
213 | |
---|
214 | def restore(params) { |
---|
215 | Task.withTransaction { status -> |
---|
216 | def result = [:] |
---|
217 | result.taskInstance = Task.get(params.id) |
---|
218 | if(result.taskInstance) { |
---|
219 | |
---|
220 | // Optimistic locking check. |
---|
221 | if(params.version) { |
---|
222 | def version = params.version.toLong() |
---|
223 | if(result.taskInstance.version > version) { |
---|
224 | status.setRollbackOnly() |
---|
225 | result.taskInstance.errors.rejectValue("version", "task.optimistic.locking.failure", "Another user has updated this Task while you were editing.") |
---|
226 | result.error = true |
---|
227 | return result |
---|
228 | } |
---|
229 | } |
---|
230 | |
---|
231 | result.taskInstance.trash = false |
---|
232 | |
---|
233 | if(result.taskInstance.save()) { |
---|
234 | def taskModification = new TaskModification(person:personService.currentUser(), |
---|
235 | taskModificationType: TaskModificationType.get(7), |
---|
236 | task: result.taskInstance) |
---|
237 | if(taskModification.save()) { |
---|
238 | // All went well. |
---|
239 | return result |
---|
240 | } |
---|
241 | else { |
---|
242 | status.setRollbackOnly() |
---|
243 | result.taskInstance.errors.rejectValue("taskModifications", "task.modifications.failedToSave") |
---|
244 | result.error = true |
---|
245 | return result |
---|
246 | } |
---|
247 | } |
---|
248 | } |
---|
249 | // Something failed. |
---|
250 | status.setRollbackOnly() |
---|
251 | result.error = true |
---|
252 | return result |
---|
253 | |
---|
254 | } //end withTransaction |
---|
255 | } // end restore() |
---|
256 | |
---|
257 | def approve(params) { |
---|
258 | Task.withTransaction { status -> |
---|
259 | def result = [:] |
---|
260 | result.taskInstance = Task.get(params.id) |
---|
261 | if(result.taskInstance) { |
---|
262 | |
---|
263 | // Optimistic locking check. |
---|
264 | if(params.version) { |
---|
265 | def version = params.version.toLong() |
---|
266 | if(result.taskInstance.version > version) { |
---|
267 | status.setRollbackOnly() |
---|
268 | result.taskInstance.errors.rejectValue("version", "task.optimistic.locking.failure", "Another user has updated this Task while you were editing.") |
---|
269 | result.error = true |
---|
270 | return result |
---|
271 | } |
---|
272 | } |
---|
273 | |
---|
274 | result.taskInstance.approved = true |
---|
275 | |
---|
276 | if(result.taskInstance.save()) { |
---|
277 | def taskModification = new TaskModification(person:personService.currentUser(), |
---|
278 | taskModificationType: TaskModificationType.get(8), |
---|
279 | task: result.taskInstance) |
---|
280 | if(taskModification.save()) { |
---|
281 | // All went well. |
---|
282 | return result |
---|
283 | } |
---|
284 | else { |
---|
285 | status.setRollbackOnly() |
---|
286 | result.taskInstance.errors.rejectValue("taskModifications", "task.modifications.failedToSave") |
---|
287 | result.error = true |
---|
288 | return result |
---|
289 | } |
---|
290 | } |
---|
291 | } |
---|
292 | // Something failed. |
---|
293 | status.setRollbackOnly() |
---|
294 | result.error = true |
---|
295 | return result |
---|
296 | |
---|
297 | } //end withTransaction |
---|
298 | } // end approve() |
---|
299 | |
---|
300 | def renegeApproval(params) { |
---|
301 | Task.withTransaction { status -> |
---|
302 | def result = [:] |
---|
303 | result.taskInstance = Task.get(params.id) |
---|
304 | if(result.taskInstance) { |
---|
305 | |
---|
306 | // Optimistic locking check. |
---|
307 | if(params.version) { |
---|
308 | def version = params.version.toLong() |
---|
309 | if(result.taskInstance.version > version) { |
---|
310 | status.setRollbackOnly() |
---|
311 | result.taskInstance.errors.rejectValue("version", "task.optimistic.locking.failure", "Another user has updated this Task while you were editing.") |
---|
312 | result.error = true |
---|
313 | return result |
---|
314 | } |
---|
315 | } |
---|
316 | |
---|
317 | result.taskInstance.approved = false |
---|
318 | |
---|
319 | if(result.taskInstance.save()) { |
---|
320 | def taskModification = new TaskModification(person:personService.currentUser(), |
---|
321 | taskModificationType: TaskModificationType.get(9), |
---|
322 | task: result.taskInstance) |
---|
323 | if(taskModification.save()) { |
---|
324 | // All went well. |
---|
325 | return result |
---|
326 | } |
---|
327 | else { |
---|
328 | status.setRollbackOnly() |
---|
329 | result.taskInstance.errors.rejectValue("taskModifications", "task.modifications.failedToSave") |
---|
330 | result.error = true |
---|
331 | return result |
---|
332 | } |
---|
333 | } |
---|
334 | } |
---|
335 | // Something failed. |
---|
336 | status.setRollbackOnly() |
---|
337 | result.error = true |
---|
338 | return result |
---|
339 | |
---|
340 | } //end withTransaction |
---|
341 | } // end renegeApproval() |
---|
342 | |
---|
343 | } // end TaskService |
---|