1 | import grails.util.Environment |
---|
2 | |
---|
3 | /** |
---|
4 | * Provides a service class for the Task domain class. |
---|
5 | * |
---|
6 | */ |
---|
7 | class TaskService { |
---|
8 | |
---|
9 | boolean transactional = false |
---|
10 | |
---|
11 | def authService |
---|
12 | def dateUtilService |
---|
13 | def assignedGroupService |
---|
14 | def assignedPersonService |
---|
15 | |
---|
16 | /** |
---|
17 | * Determines and returns a possible parent list for a task. |
---|
18 | * @todo Create and use another method that limits the results to say the latest 20 or 100 tasks? |
---|
19 | * @param taskInstance The task to use when determining the possible parent list. |
---|
20 | * @returns A list of the possible parents. |
---|
21 | */ |
---|
22 | def possibleParentList(taskInstance) { |
---|
23 | def criteria = taskInstance.createCriteria() |
---|
24 | def possibleParentList = criteria { |
---|
25 | and { |
---|
26 | notEqual('trash', true) |
---|
27 | notEqual('id', taskInstance.id) |
---|
28 | taskInstance.subTasks.each() { notEqual('id', it.id) } |
---|
29 | } |
---|
30 | } |
---|
31 | } |
---|
32 | |
---|
33 | /** |
---|
34 | * Determines and returns a list of possible task types for scheduled tasks. |
---|
35 | * @returns A list of the possible task types. |
---|
36 | */ |
---|
37 | def getScheduledTaskTypes() { |
---|
38 | def criteria = TaskType.createCriteria() |
---|
39 | def scheduledTaskTypes = criteria { |
---|
40 | and { |
---|
41 | eq('isActive', true) |
---|
42 | gt('id', 2L) |
---|
43 | } |
---|
44 | } |
---|
45 | } |
---|
46 | |
---|
47 | /** |
---|
48 | * Determines and returns a list of possible task priorites for Scheduled tasks. |
---|
49 | * @returns A list of the possible task priorites. |
---|
50 | */ |
---|
51 | def getScheduledTaskPriorities() { |
---|
52 | def criteria = TaskPriority.createCriteria() |
---|
53 | def scheduledTaskPriorities = [:] |
---|
54 | scheduledTaskPriorities.list = criteria { |
---|
55 | and { |
---|
56 | eq('isActive', true) |
---|
57 | gt('id', 1L) |
---|
58 | } |
---|
59 | } |
---|
60 | scheduledTaskPriorities.default = scheduledTaskPriorities.list.find { it.id == 4L } // 1-Normal. |
---|
61 | return scheduledTaskPriorities |
---|
62 | } |
---|
63 | |
---|
64 | /** |
---|
65 | * Determines and returns a list of possible task priorites for Unscheduled tasks. |
---|
66 | * @returns A map containing a list of the possible task priorites and the default priority. |
---|
67 | */ |
---|
68 | def getUnscheduledTaskPriorities() { |
---|
69 | def criteria = TaskPriority.createCriteria() |
---|
70 | def unscheduledTaskPriorities = [:] |
---|
71 | unscheduledTaskPriorities.list = criteria { |
---|
72 | and { |
---|
73 | eq('isActive', true) |
---|
74 | lt('id', 5L) |
---|
75 | ne('id', 1L) |
---|
76 | } |
---|
77 | } |
---|
78 | unscheduledTaskPriorities.default = unscheduledTaskPriorities.list.find { it.id == 3L } // 2-High. |
---|
79 | return unscheduledTaskPriorities |
---|
80 | } |
---|
81 | |
---|
82 | /** |
---|
83 | * Creates a new task with the given params. |
---|
84 | * @param params The params to use when creating the new task. |
---|
85 | * @returns A map containing result.error (if any error) and result.taskInstance. |
---|
86 | */ |
---|
87 | def save(params) { |
---|
88 | Task.withTransaction { status -> |
---|
89 | def result = [:] |
---|
90 | |
---|
91 | def fail = { Map m -> |
---|
92 | status.setRollbackOnly() |
---|
93 | if(result.taskInstance && m.field) |
---|
94 | result.taskInstance.errors.rejectValue(m.field, m.code) |
---|
95 | result.error = [ code: m.code, args: ["Task", params.id] ] |
---|
96 | return result |
---|
97 | } |
---|
98 | |
---|
99 | // Default status to "not started" if not supplied. |
---|
100 | params.taskStatus = params.taskStatus ?: TaskStatus.get(1) |
---|
101 | |
---|
102 | // Set budgetStatus. |
---|
103 | if(params.taskType?.id?.toLong() == 1 || params.taskType?.id?.toLong() == 2) // Immediate Callout or Unsheduled Breakin. |
---|
104 | params.taskBudgetStatus = params.taskBudgetStatus ?: TaskBudgetStatus.get(1) // Unplanned. |
---|
105 | else |
---|
106 | params.taskBudgetStatus = params.taskBudgetStatus ?: TaskBudgetStatus.get(2) // Planned. |
---|
107 | |
---|
108 | def taskInstance = new Task(params) |
---|
109 | result.taskInstance = taskInstance |
---|
110 | |
---|
111 | if(result.taskInstance.parentTask?.trash) |
---|
112 | return fail(field:"parentTask", code:"task.operationNotPermittedOnTaskInTrash") |
---|
113 | |
---|
114 | if(result.taskInstance.hasErrors() || !result.taskInstance.save()) |
---|
115 | return fail(code:"default.create.failure") |
---|
116 | |
---|
117 | def taskModification = new TaskModification(person: authService.currentUser, |
---|
118 | taskModificationType: TaskModificationType.get(1), |
---|
119 | task: taskInstance) |
---|
120 | |
---|
121 | if(taskModification.hasErrors() || !taskModification.save()) |
---|
122 | return fail(field:"taskModifications", code:"task.modifications.failedToSave") |
---|
123 | |
---|
124 | //Add the assignedGroups, provided by a new ArrayList(task.assignedGroups) |
---|
125 | if(params.assignedGroups) { |
---|
126 | def assignedGroupsResult |
---|
127 | def assignedGroupParams = [:] |
---|
128 | params.assignedGroups.each() { |
---|
129 | |
---|
130 | assignedGroupParams = [personGroup: it.personGroup, |
---|
131 | task: taskInstance, |
---|
132 | estimatedHour: it.estimatedHour, |
---|
133 | estimatedMinute: it.estimatedMinute] |
---|
134 | |
---|
135 | assignedGroupsResult = assignedGroupService.save(assignedGroupParams) |
---|
136 | |
---|
137 | if(assignedGroupsResult.error) |
---|
138 | return fail(field:"assignedGroups", code:"task.assignedGroups.failedToSave") |
---|
139 | |
---|
140 | } |
---|
141 | } |
---|
142 | |
---|
143 | //Add the assignedPersons, provided by a new ArrayList(task.assignedPersons) |
---|
144 | if(params.assignedPersons) { |
---|
145 | def assignedPersonsResult |
---|
146 | def assignedPersonsParams = [:] |
---|
147 | params.assignedPersons.each() { |
---|
148 | |
---|
149 | assignedPersonsParams = [person: it.person, |
---|
150 | task: taskInstance, |
---|
151 | estimatedHour: it.estimatedHour, |
---|
152 | estimatedMinute: it.estimatedMinute] |
---|
153 | |
---|
154 | assignedPersonsResult = assignedPersonService.save(assignedPersonsParams) |
---|
155 | |
---|
156 | if(assignedPersonsResult.error) |
---|
157 | return fail(field:"assignedPersons", code:"task.assignedPersons.failedToSave") |
---|
158 | |
---|
159 | } |
---|
160 | } |
---|
161 | |
---|
162 | // Success. |
---|
163 | return result |
---|
164 | |
---|
165 | } //end withTransaction |
---|
166 | } // end save() |
---|
167 | |
---|
168 | /** |
---|
169 | * Creates a subTask copying sane attributes from the parentTask unless otherwise specified in params. |
---|
170 | * The targetStartDate and targetCompletionDate default to today since that is the sane thing to do. |
---|
171 | * The taskProcedure is only assigned to the sub task if supplied in params. |
---|
172 | * The assignedPersons and assignedGroups are only added to the sub task if supplied in params. |
---|
173 | * Collections in params must be supplied as new ArrayList's. |
---|
174 | * This method is not intended to be a copyTask method. |
---|
175 | * There should be no reason to copy tasks, recurrence can be used to create similar tasks. |
---|
176 | * @param parentTask The parent task to get attributes from, also set as the parent. |
---|
177 | * @param params Overrides the parent task values if specified. |
---|
178 | * @returns A map containing result.error=true (if any error) and result.taskInstance. |
---|
179 | */ |
---|
180 | def createSubTask(parentTask, params = [:]) { |
---|
181 | |
---|
182 | def result = [:] |
---|
183 | |
---|
184 | //Make our new Task a subTask and set the required properites. |
---|
185 | def p = [:] |
---|
186 | p.parentTask = parentTask |
---|
187 | p.description = params.description ?: parentTask.description |
---|
188 | p.comment = params.comment ?: parentTask.comment |
---|
189 | p.targetStartDate = params.targetStartDate ?: dateUtilService.today |
---|
190 | p.targetCompletionDate = params.targetCompletionDate ?: dateUtilService.today |
---|
191 | |
---|
192 | p.taskGroup = params.taskGroup ?: parentTask.taskGroup |
---|
193 | p.taskStatus = TaskStatus.get(1) // A new subTask must always be "Not Started". |
---|
194 | p.taskPriority = parentTask.taskPriority |
---|
195 | p.taskType = params.taskType ?: parentTask.taskType |
---|
196 | p.leadPerson = params.leadPerson ?: parentTask.leadPerson |
---|
197 | p.primaryAsset = params.primaryAsset ?: parentTask.primaryAsset |
---|
198 | p.associatedAssets = params.associatedAssets ?: new ArrayList(parentTask.associatedAssets) // Collection. |
---|
199 | |
---|
200 | // Supplied by recurring tasks. |
---|
201 | if(params.taskProcedure) p.taskProcedure = params.taskProcedure |
---|
202 | if(params.assignedGroups) p.assignedGroups = params.assignedGroups // Collection. |
---|
203 | if(params.assignedPersons) p.assignedPersons = params.assignedPersons // Collection. |
---|
204 | |
---|
205 | // trash: A new subTask must always have trash=false, which is already the domain class default. |
---|
206 | |
---|
207 | // These would be considered copying, hence not done. |
---|
208 | // taskRecurringSchedule, entries, taskModifications, subTasks, inventoryMovements. |
---|
209 | |
---|
210 | // Create the sub task and return the result. |
---|
211 | result = save(p) |
---|
212 | |
---|
213 | // Approve. |
---|
214 | if(!result.error && parentTask.approved) { |
---|
215 | p = [:] |
---|
216 | p.id = result.taskInstance.id |
---|
217 | approve(p) |
---|
218 | } |
---|
219 | |
---|
220 | // Success. |
---|
221 | return result |
---|
222 | |
---|
223 | } // end createSubTask() |
---|
224 | |
---|
225 | /** |
---|
226 | * In production tasks are NEVER deleted, only the trash flag is set! |
---|
227 | * However during testing it may be required to delete a task and that |
---|
228 | * is why this method exists. |
---|
229 | */ |
---|
230 | def delete(params) { |
---|
231 | Task.withTransaction { status -> |
---|
232 | def result = [:] |
---|
233 | |
---|
234 | def fail = { Map m -> |
---|
235 | status.setRollbackOnly() |
---|
236 | if(result.taskInstance && m.field) |
---|
237 | result.taskInstance.errors.rejectValue(m.field, m.code) |
---|
238 | result.error = [ code: m.code, args: ["Task", params.id] ] |
---|
239 | return result |
---|
240 | } |
---|
241 | |
---|
242 | if(Environment.current == Environment.PRODUCTION) |
---|
243 | return fail(code:"task.delete.failure.production") |
---|
244 | |
---|
245 | result.taskInstance = Task.get(params.id) |
---|
246 | |
---|
247 | if(!result.taskInstance) |
---|
248 | return fail(code:"default.not.found") |
---|
249 | |
---|
250 | // Handle taskModifications. |
---|
251 | def taskModifications = TaskModification.findAllByTask(result.taskInstance) |
---|
252 | taskModifications.each() { |
---|
253 | result.taskInstance.removeFromTaskModifications(it) |
---|
254 | it.delete() |
---|
255 | } |
---|
256 | |
---|
257 | // Handle assignedPersons. |
---|
258 | def taskAssignedPersons = AssignedPerson.findAllByTask(result.taskInstance) |
---|
259 | taskAssignedPersons.each() { |
---|
260 | result.taskInstance.removeFromAssignedPersons(it) |
---|
261 | it.delete() |
---|
262 | } |
---|
263 | |
---|
264 | // Handle assignedGroups. |
---|
265 | def taskAssignedGroups = AssignedGroup.findAllByTask(result.taskInstance) |
---|
266 | taskAssignedGroups.each() { |
---|
267 | result.taskInstance.removeFromAssignedGroups(it) |
---|
268 | it.delete() |
---|
269 | } |
---|
270 | |
---|
271 | if(result.error) |
---|
272 | return result |
---|
273 | |
---|
274 | try { |
---|
275 | result.taskInstance.delete(flush:true) |
---|
276 | return result //Success. |
---|
277 | } |
---|
278 | catch(org.springframework.dao.DataIntegrityViolationException e) { |
---|
279 | return fail(code:"default.delete.failure") |
---|
280 | } |
---|
281 | |
---|
282 | } // end withTransaction |
---|
283 | } // delete() |
---|
284 | |
---|
285 | /** |
---|
286 | * Creates a new task entry. |
---|
287 | * @param params The params to use when creating the new entry. |
---|
288 | * @returns A map containing result.error=true (if any error), result.entryInstance and result.taskId. |
---|
289 | */ |
---|
290 | def saveEntry(params) { |
---|
291 | Task.withTransaction { status -> |
---|
292 | def result = [:] |
---|
293 | |
---|
294 | def fail = { Map m -> |
---|
295 | status.setRollbackOnly() |
---|
296 | if(result.taskInstance && m.field) |
---|
297 | result.taskInstance.errors.rejectValue(m.field, m.code) |
---|
298 | result.error = [ code: m.code, args: ["Entry", params.id] ] |
---|
299 | return result |
---|
300 | } |
---|
301 | |
---|
302 | result.entryInstance = new Entry(params) |
---|
303 | result.entryInstance.enteredBy = authService.currentUser |
---|
304 | |
---|
305 | def taskInstance |
---|
306 | if(result.entryInstance.task?.id) { |
---|
307 | result.taskId = result.entryInstance.task.id |
---|
308 | taskInstance = Task.lock(result.entryInstance.task.id) |
---|
309 | } |
---|
310 | |
---|
311 | if(!taskInstance) |
---|
312 | return fail(field:"task", code:"task.notFound") |
---|
313 | |
---|
314 | if(result.entryInstance.hasErrors() || !result.entryInstance.save()) |
---|
315 | return fail(code:"default.create.failure") |
---|
316 | |
---|
317 | if(taskInstance.taskStatus.id == 3) |
---|
318 | return fail(field:"task", code:"task.operationNotPermittedOnCompleteTask") |
---|
319 | |
---|
320 | // If task status is "Not Started" and entry type is "Work Done" and time has been booked. |
---|
321 | // Then we create the started modification and set task status. |
---|
322 | if(taskInstance.taskStatus.id == 1 && result.entryInstance.entryType.id == 3 |
---|
323 | && (result.entryInstance.durationHour + result.entryInstance.durationMinute > 0)) { |
---|
324 | |
---|
325 | // Create the "Started" task modification, this provides the "Actual Started Date". |
---|
326 | def taskModification = new TaskModification(person: authService.currentUser, |
---|
327 | taskModificationType: TaskModificationType.read(2), |
---|
328 | task: taskInstance) |
---|
329 | |
---|
330 | if(taskModification.hasErrors() || !taskModification.save()) |
---|
331 | return fail(field:"task", code:"task.modifications.failedToSave") |
---|
332 | |
---|
333 | // Set task status to "In Progress". |
---|
334 | taskInstance.taskStatus = TaskStatus.read(2) |
---|
335 | |
---|
336 | if(taskInstance.hasErrors() || !taskInstance.save()) |
---|
337 | return fail(field:"task", code:"task.failedToSave") |
---|
338 | } |
---|
339 | |
---|
340 | // Success. |
---|
341 | return result |
---|
342 | |
---|
343 | } // end withTransaction |
---|
344 | } // end saveEntry() |
---|
345 | |
---|
346 | /** |
---|
347 | * Updates an existing task. |
---|
348 | * @param params The params to update for task with id of params.id. |
---|
349 | * @returns A map containing result.error (if any error) and result.taskInstance (if available). |
---|
350 | */ |
---|
351 | def update(params) { |
---|
352 | Task.withTransaction { status -> |
---|
353 | def result = [:] |
---|
354 | |
---|
355 | def fail = { Map m -> |
---|
356 | status.setRollbackOnly() |
---|
357 | if(result.taskInstance && m.field) |
---|
358 | result.taskInstance.errors.rejectValue(m.field, m.code) |
---|
359 | result.error = [ code: m.code, args: ["Task", params.id] ] |
---|
360 | return result |
---|
361 | } |
---|
362 | |
---|
363 | result.taskInstance = Task.get(params.id) |
---|
364 | |
---|
365 | if(!result.taskInstance) |
---|
366 | return fail('id', "task.notFound") |
---|
367 | |
---|
368 | // Optimistic locking check. |
---|
369 | if(params.version) { |
---|
370 | if(result.taskInstance.version > params.version.toLong()) |
---|
371 | return fail(field:"version", code:"default.optimistic.locking.failure") |
---|
372 | } |
---|
373 | |
---|
374 | result.taskInstance.properties = params |
---|
375 | |
---|
376 | if(result.taskInstance.hasErrors() || !result.taskInstance.save()) |
---|
377 | return fail(code:"default.update.failure") |
---|
378 | |
---|
379 | def taskModification = new TaskModification(person:authService.currentUser, |
---|
380 | taskModificationType: TaskModificationType.get(3), |
---|
381 | task: result.taskInstance) |
---|
382 | |
---|
383 | if(taskModification.hasErrors() || !taskModification.save()) |
---|
384 | return fail(code:"task.modifications.failedToSave") |
---|
385 | |
---|
386 | // Success. |
---|
387 | return result |
---|
388 | |
---|
389 | } //end withTransaction |
---|
390 | } // end update() |
---|
391 | |
---|
392 | /** |
---|
393 | * Completes an existing task. |
---|
394 | * @param params The params for task with id of params.id. |
---|
395 | * @returns A map containing result.error (if any error) and result.taskInstance (if available). |
---|
396 | */ |
---|
397 | def complete(params) { |
---|
398 | Task.withTransaction { status -> |
---|
399 | def result = [:] |
---|
400 | |
---|
401 | def fail = { Map m -> |
---|
402 | status.setRollbackOnly() |
---|
403 | if(result.taskInstance && m.field) |
---|
404 | result.taskInstance.errors.rejectValue(m.field, m.code) |
---|
405 | result.error = [ code: m.code, args: ["Task", params.id] ] |
---|
406 | return result |
---|
407 | } |
---|
408 | |
---|
409 | result.taskInstance = Task.get(params.id) |
---|
410 | |
---|
411 | if(!result.taskInstance) |
---|
412 | return fail(code:"default.not.found") |
---|
413 | |
---|
414 | // Optimistic locking check. |
---|
415 | if(params.version) { |
---|
416 | if(result.taskInstance.version > params.version.toLong()) |
---|
417 | return fail(field:"version", code:"default.optimistic.locking.failure") |
---|
418 | } |
---|
419 | |
---|
420 | result.taskInstance.taskStatus = TaskStatus.get(3) |
---|
421 | result.taskInstance.attentionFlag = false |
---|
422 | result.taskInstance.taskRecurringSchedule?.enabled = false |
---|
423 | |
---|
424 | if(result.taskInstance.hasErrors() || !result.taskInstance.save()) |
---|
425 | return fail(code:"default.update.failure") |
---|
426 | |
---|
427 | def taskModification = new TaskModification(person:authService.currentUser, |
---|
428 | taskModificationType: TaskModificationType.get(4), |
---|
429 | task: result.taskInstance) |
---|
430 | |
---|
431 | |
---|
432 | if(taskModification.hasErrors() || !taskModification.save()) |
---|
433 | return fail(code:"task.modifications.failedToSave") |
---|
434 | |
---|
435 | // Success. |
---|
436 | return result |
---|
437 | |
---|
438 | } //end withTransaction |
---|
439 | } // end complete() |
---|
440 | |
---|
441 | /** |
---|
442 | * Sets the attentionFlag on an existing task. |
---|
443 | * @param params The params for task with id of params.id. |
---|
444 | * @returns A map containing result.error (if any error) and result.taskInstance (if available). |
---|
445 | */ |
---|
446 | def setAttentionFlag(params) { |
---|
447 | Task.withTransaction { status -> |
---|
448 | def result = [:] |
---|
449 | |
---|
450 | def fail = { Map m -> |
---|
451 | status.setRollbackOnly() |
---|
452 | if(result.taskInstance && m.field) |
---|
453 | result.taskInstance.errors.rejectValue(m.field, m.code) |
---|
454 | result.error = [ code: m.code, args: ["Task", params.id] ] |
---|
455 | return result |
---|
456 | } |
---|
457 | |
---|
458 | result.taskInstance = Task.get(params.id) |
---|
459 | |
---|
460 | if(!result.taskInstance) |
---|
461 | return fail(code:"default.not.found") |
---|
462 | |
---|
463 | // Optimistic locking check. |
---|
464 | if(params.version) { |
---|
465 | if(result.taskInstance.version > params.version.toLong()) |
---|
466 | return fail(field:"version", code:"default.optimistic.locking.failure") |
---|
467 | } |
---|
468 | |
---|
469 | result.taskInstance.attentionFlag = true |
---|
470 | |
---|
471 | if(result.taskInstance.hasErrors() || !result.taskInstance.save()) |
---|
472 | return fail(code:"default.update.failure") |
---|
473 | |
---|
474 | def taskModification = new TaskModification(person:authService.currentUser, |
---|
475 | taskModificationType: TaskModificationType.get(12), |
---|
476 | task: result.taskInstance) |
---|
477 | |
---|
478 | if(taskModification.hasErrors() || !taskModification.save()) |
---|
479 | return fail(code:"task.modifications.failedToSave") |
---|
480 | |
---|
481 | // Success. |
---|
482 | return result |
---|
483 | |
---|
484 | } //end withTransaction |
---|
485 | } // end flag() |
---|
486 | |
---|
487 | /** |
---|
488 | * Clears the attentionFlag on an existing task. |
---|
489 | * @param params The params for task with id of params.id. |
---|
490 | * @returns A map containing result.error (if any error) and result.taskInstance (if available). |
---|
491 | */ |
---|
492 | def clearAttentionFlag(params) { |
---|
493 | Task.withTransaction { status -> |
---|
494 | def result = [:] |
---|
495 | |
---|
496 | def fail = { Map m -> |
---|
497 | status.setRollbackOnly() |
---|
498 | if(result.taskInstance && m.field) |
---|
499 | result.taskInstance.errors.rejectValue(m.field, m.code) |
---|
500 | result.error = [ code: m.code, args: ["Task", params.id] ] |
---|
501 | return result |
---|
502 | } |
---|
503 | |
---|
504 | result.taskInstance = Task.get(params.id) |
---|
505 | |
---|
506 | if(!result.taskInstance) |
---|
507 | return fail(code:"default.not.found") |
---|
508 | |
---|
509 | // Optimistic locking check. |
---|
510 | if(params.version) { |
---|
511 | if(result.taskInstance.version > params.version.toLong()) |
---|
512 | return fail(field:"version", code:"default.optimistic.locking.failure") |
---|
513 | } |
---|
514 | |
---|
515 | result.taskInstance.attentionFlag = false |
---|
516 | |
---|
517 | if(result.taskInstance.hasErrors() || !result.taskInstance.save()) |
---|
518 | return fail(code:"default.update.failure") |
---|
519 | |
---|
520 | def taskModification = new TaskModification(person:authService.currentUser, |
---|
521 | taskModificationType: TaskModificationType.get(13), |
---|
522 | task: result.taskInstance) |
---|
523 | |
---|
524 | if(taskModification.hasErrors() || !taskModification.save()) |
---|
525 | return fail(code:"task.modifications.failedToSave") |
---|
526 | |
---|
527 | // Success. |
---|
528 | return result |
---|
529 | |
---|
530 | } //end withTransaction |
---|
531 | } // end clearFlag() |
---|
532 | |
---|
533 | /** |
---|
534 | * Reopens an existing task. |
---|
535 | * @param params The params for task with id of params.id. |
---|
536 | * @returns A map containing result.error (if any error) and result.taskInstance (if available). |
---|
537 | */ |
---|
538 | def reopen(params) { |
---|
539 | Task.withTransaction { status -> |
---|
540 | def result = [:] |
---|
541 | |
---|
542 | def fail = { Map m -> |
---|
543 | status.setRollbackOnly() |
---|
544 | if(result.taskInstance && m.field) |
---|
545 | result.taskInstance.errors.rejectValue(m.field, m.code) |
---|
546 | result.error = [ code: m.code, args: ["Task", params.id] ] |
---|
547 | return result |
---|
548 | } |
---|
549 | |
---|
550 | result.taskInstance = Task.get(params.id) |
---|
551 | |
---|
552 | if(!result.taskInstance) |
---|
553 | return fail(code:"default.not.found") |
---|
554 | |
---|
555 | // Optimistic locking check. |
---|
556 | if(params.version) { |
---|
557 | if(result.taskInstance.version > params.version.toLong()) |
---|
558 | return fail(field:"version", code:"default.optimistic.locking.failure") |
---|
559 | } |
---|
560 | |
---|
561 | def isInProgress = false |
---|
562 | result.taskInstance.entries.each() { |
---|
563 | if(it.entryType.id == 3 && (it.durationHour + it.durationMinute > 0) ) |
---|
564 | isInProgress = true |
---|
565 | } |
---|
566 | |
---|
567 | if(isInProgress) |
---|
568 | result.taskInstance.taskStatus = TaskStatus.read(2) // In Progress |
---|
569 | else |
---|
570 | result.taskInstance.taskStatus = TaskStatus.read(1) // Not Started |
---|
571 | |
---|
572 | if(result.taskInstance.hasErrors() || !result.taskInstance.save()) |
---|
573 | return fail(code:"default.update.failure") |
---|
574 | |
---|
575 | def taskModification = new TaskModification(person:authService.currentUser, |
---|
576 | taskModificationType: TaskModificationType.get(5), |
---|
577 | task: result.taskInstance) |
---|
578 | |
---|
579 | if(taskModification.hasErrors() || !taskModification.save()) |
---|
580 | return fail(code:"task.modifications.failedToSave") |
---|
581 | |
---|
582 | // Success. |
---|
583 | return result |
---|
584 | |
---|
585 | } //end withTransaction |
---|
586 | } // end reopen() |
---|
587 | |
---|
588 | /** |
---|
589 | * Move a task to the trash. |
---|
590 | * @param params The params for task with id of params.id. |
---|
591 | * @returns A map containing result.error (if any error) and result.taskInstance (if available). |
---|
592 | */ |
---|
593 | def trash(params) { |
---|
594 | Task.withTransaction { status -> |
---|
595 | def result = [:] |
---|
596 | |
---|
597 | def fail = { Map m -> |
---|
598 | status.setRollbackOnly() |
---|
599 | if(result.taskInstance && m.field) |
---|
600 | result.taskInstance.errors.rejectValue(m.field, m.code) |
---|
601 | result.error = [ code: m.code, args: ["Task", params.id] ] |
---|
602 | return result |
---|
603 | } |
---|
604 | |
---|
605 | result.taskInstance = Task.get(params.id) |
---|
606 | |
---|
607 | if(!result.taskInstance) |
---|
608 | return fail(code:"default.not.found") |
---|
609 | |
---|
610 | // Optimistic locking check. |
---|
611 | if(params.version) { |
---|
612 | if(result.taskInstance.version > params.version.toLong()) |
---|
613 | return fail(field:"version", code:"default.optimistic.locking.failure") |
---|
614 | } |
---|
615 | |
---|
616 | result.taskInstance.trash = true |
---|
617 | result.taskInstance.attentionFlag = false |
---|
618 | result.taskInstance.taskRecurringSchedule?.enabled = false |
---|
619 | |
---|
620 | if(result.taskInstance.hasErrors() || !result.taskInstance.save()) |
---|
621 | return fail(code:"default.update.failure") |
---|
622 | |
---|
623 | def taskModification = new TaskModification(person:authService.currentUser, |
---|
624 | taskModificationType: TaskModificationType.get(6), |
---|
625 | task: result.taskInstance) |
---|
626 | |
---|
627 | if(taskModification.hasErrors() || !taskModification.save()) |
---|
628 | return fail(code:"task.modifications.failedToSave") |
---|
629 | |
---|
630 | // Success. |
---|
631 | return result |
---|
632 | |
---|
633 | } //end withTransaction |
---|
634 | } // end trash() |
---|
635 | |
---|
636 | /** |
---|
637 | * Restore a task from the trash. |
---|
638 | * @param params The params for task with id of params.id. |
---|
639 | * @returns A map containing result.error (if any error) and result.taskInstance (if available). |
---|
640 | */ |
---|
641 | def restore(params) { |
---|
642 | Task.withTransaction { status -> |
---|
643 | def result = [:] |
---|
644 | |
---|
645 | def fail = { Map m -> |
---|
646 | status.setRollbackOnly() |
---|
647 | if(result.taskInstance && m.field) |
---|
648 | result.taskInstance.errors.rejectValue(m.field, m.code) |
---|
649 | result.error = [ code: m.code, args: ["Task", params.id] ] |
---|
650 | return result |
---|
651 | } |
---|
652 | |
---|
653 | result.taskInstance = Task.get(params.id) |
---|
654 | |
---|
655 | if(!result.taskInstance) |
---|
656 | return fail(code:"default.not.found") |
---|
657 | |
---|
658 | // Optimistic locking check. |
---|
659 | if(params.version) { |
---|
660 | if(result.taskInstance.version > params.version.toLong()) |
---|
661 | return fail(field:"version", code:"default.optimistic.locking.failure") |
---|
662 | } |
---|
663 | |
---|
664 | result.taskInstance.trash = false |
---|
665 | |
---|
666 | if(result.taskInstance.hasErrors() || !result.taskInstance.save()) |
---|
667 | return fail(code:"default.update.failure") |
---|
668 | |
---|
669 | def taskModification = new TaskModification(person:authService.currentUser, |
---|
670 | taskModificationType: TaskModificationType.get(7), |
---|
671 | task: result.taskInstance) |
---|
672 | |
---|
673 | if(taskModification.hasErrors() || !taskModification.save()) |
---|
674 | return fail(code:"task.modifications.failedToSave") |
---|
675 | |
---|
676 | // Success. |
---|
677 | return result |
---|
678 | |
---|
679 | } //end withTransaction |
---|
680 | } // end restore() |
---|
681 | |
---|
682 | /** |
---|
683 | * Approve a task. |
---|
684 | * @param params The params for task with id of params.id. |
---|
685 | * @returns A map containing result.error (if any error) and result.taskInstance (if available). |
---|
686 | */ |
---|
687 | def approve(params) { |
---|
688 | Task.withTransaction { status -> |
---|
689 | def result = [:] |
---|
690 | |
---|
691 | def fail = { Map m -> |
---|
692 | status.setRollbackOnly() |
---|
693 | if(result.taskInstance && m.field) |
---|
694 | result.taskInstance.errors.rejectValue(m.field, m.code) |
---|
695 | result.error = [ code: m.code, args: ["Task", params.id] ] |
---|
696 | return result |
---|
697 | } |
---|
698 | |
---|
699 | result.taskInstance = Task.get(params.id) |
---|
700 | |
---|
701 | if(!result.taskInstance) |
---|
702 | return fail(code:"default.not.found") |
---|
703 | |
---|
704 | // Optimistic locking check. |
---|
705 | if(params.version) { |
---|
706 | if(result.taskInstance.version > params.version.toLong()) |
---|
707 | return fail(field:"version", code:"default.optimistic.locking.failure") |
---|
708 | } |
---|
709 | |
---|
710 | result.taskInstance.approved = true |
---|
711 | |
---|
712 | if(result.taskInstance.hasErrors() || !result.taskInstance.save()) |
---|
713 | return fail(code:"default.update.failure") |
---|
714 | |
---|
715 | def taskModification = new TaskModification(person:authService.currentUser, |
---|
716 | taskModificationType: TaskModificationType.get(8), |
---|
717 | task: result.taskInstance) |
---|
718 | |
---|
719 | if(taskModification.hasErrors() || !taskModification.save()) |
---|
720 | return fail(code:"task.modifications.failedToSave") |
---|
721 | |
---|
722 | // Success. |
---|
723 | return result |
---|
724 | |
---|
725 | } //end withTransaction |
---|
726 | } // end approve() |
---|
727 | |
---|
728 | /** |
---|
729 | * Remove a previously given approval from a task. |
---|
730 | * @param params The params for task with id of params.id. |
---|
731 | * @returns A map containing result.error (if any error) and result.taskInstance (if available). |
---|
732 | */ |
---|
733 | def renegeApproval(params) { |
---|
734 | Task.withTransaction { status -> |
---|
735 | def result = [:] |
---|
736 | |
---|
737 | def fail = { Map m -> |
---|
738 | status.setRollbackOnly() |
---|
739 | if(result.taskInstance && m.field) |
---|
740 | result.taskInstance.errors.rejectValue(m.field, m.code) |
---|
741 | result.error = [ code: m.code, args: ["Task", params.id] ] |
---|
742 | return result |
---|
743 | } |
---|
744 | |
---|
745 | result.taskInstance = Task.get(params.id) |
---|
746 | |
---|
747 | if(!result.taskInstance) |
---|
748 | return fail(code:"default.not.found") |
---|
749 | |
---|
750 | // Optimistic locking check. |
---|
751 | if(params.version) { |
---|
752 | if(result.taskInstance.version > params.version.toLong()) |
---|
753 | return fail(field:"version", code:"default.optimistic.locking.failure") |
---|
754 | } |
---|
755 | |
---|
756 | result.taskInstance.approved = false |
---|
757 | |
---|
758 | if(result.taskInstance.hasErrors() || !result.taskInstance.save()) |
---|
759 | return fail(code:"default.update.failure") |
---|
760 | |
---|
761 | def taskModification = new TaskModification(person:authService.currentUser, |
---|
762 | taskModificationType: TaskModificationType.get(9), |
---|
763 | task: result.taskInstance) |
---|
764 | |
---|
765 | if(taskModification.hasErrors() || !taskModification.save()) |
---|
766 | return fail(code:"task.modifications.failedToSave") |
---|
767 | |
---|
768 | // Success. |
---|
769 | return result |
---|
770 | |
---|
771 | } //end withTransaction |
---|
772 | } // end renegeApproval() |
---|
773 | |
---|
774 | /** |
---|
775 | * Creates a new unscheduled breakin task with the given params. |
---|
776 | * @param params The params to use when creating the new task. |
---|
777 | * @returns A map containing result.error (if any error) and result.taskInstance. |
---|
778 | */ |
---|
779 | def saveUnscheduled(params) { |
---|
780 | Task.withTransaction { status -> |
---|
781 | def result = [:] |
---|
782 | |
---|
783 | def fail = { Map m -> |
---|
784 | status.setRollbackOnly() |
---|
785 | if(result.taskInstance && m.field) |
---|
786 | result.taskInstance.errors.rejectValue(m.field, m.code) |
---|
787 | result.error = [ code: m.code, args: ["Task", params.id] ] |
---|
788 | return result |
---|
789 | } |
---|
790 | |
---|
791 | // If not supplied. |
---|
792 | if(!params.taskStatus) |
---|
793 | params.taskStatus = TaskStatus.get(1) // Not Started. |
---|
794 | |
---|
795 | result.taskInstance = new Task(params) |
---|
796 | |
---|
797 | // Always for an unscheduled breakin.. |
---|
798 | result.taskInstance.taskType = TaskType.get(2) // Unscheduled Breakin. |
---|
799 | result.taskInstance.taskBudgetStatus = TaskBudgetStatus.get(1) // Unplanned. |
---|
800 | |
---|
801 | if(result.taskInstance.hasErrors() || !result.taskInstance.save()) |
---|
802 | fail(code:"default.create.failure") |
---|
803 | |
---|
804 | if(!result.error) { |
---|
805 | def taskModification = new TaskModification(person: authService.currentUser, |
---|
806 | taskModificationType: TaskModificationType.get(1), // Created. |
---|
807 | task: result.taskInstance) |
---|
808 | |
---|
809 | if(taskModification.hasErrors() || !taskModification.save()) |
---|
810 | fail(field:"taskModifications", code:"task.modifications.failedToSave") |
---|
811 | } |
---|
812 | |
---|
813 | // Success. |
---|
814 | return result |
---|
815 | |
---|
816 | } //end withTransaction |
---|
817 | } // end saveUnscheduled() |
---|
818 | |
---|
819 | /** |
---|
820 | * Creates a new immediate callout task with the given params. |
---|
821 | * @param params The params to use when creating the new task. |
---|
822 | * @returns A map containing result.error (if any error) and result.taskInstance. |
---|
823 | */ |
---|
824 | def saveImmediateCallout(params) { |
---|
825 | Task.withTransaction { status -> |
---|
826 | def result = [:] |
---|
827 | |
---|
828 | def fail = { Map m -> |
---|
829 | status.setRollbackOnly() |
---|
830 | if(result.taskInstance && m.field) |
---|
831 | result.taskInstance.errors.rejectValue(m.field, m.code) |
---|
832 | result.error = [ code: m.code, args: ["Task", params.id] ] |
---|
833 | return result |
---|
834 | } |
---|
835 | |
---|
836 | // If not supplied. |
---|
837 | if(!params.taskStatus) |
---|
838 | params.taskStatus = TaskStatus.get(1) // Not Started. |
---|
839 | |
---|
840 | result.taskInstance = new Task(params) |
---|
841 | |
---|
842 | // Always for an immediate callout. |
---|
843 | result.taskInstance.taskType = TaskType.get(1) // Immediate Callout. |
---|
844 | result.taskInstance.taskBudgetStatus = TaskBudgetStatus.get(1) // Unplanned. |
---|
845 | result.taskInstance.taskPriority = TaskPriority.get(1) // Immediate. |
---|
846 | result.taskInstance.taskGroup = TaskGroup.get(1) // Engineering Activites. |
---|
847 | result.taskInstance.approved = true |
---|
848 | result.taskInstance.leadPerson = authService.currentUser |
---|
849 | result.taskInstance.targetCompletionDate = result.taskInstance.targetStartDate |
---|
850 | |
---|
851 | if(result.taskInstance.hasErrors() || !result.taskInstance.save()) |
---|
852 | fail(code:"default.create.failure") |
---|
853 | |
---|
854 | if(!result.error) { |
---|
855 | def taskModification = new TaskModification(person: authService.currentUser, |
---|
856 | taskModificationType: TaskModificationType.get(1), // Created. |
---|
857 | task: result.taskInstance) |
---|
858 | |
---|
859 | if(taskModification.hasErrors() || !taskModification.save()) |
---|
860 | fail(field:"taskModifications", code:"task.modifications.failedToSave") |
---|
861 | } |
---|
862 | |
---|
863 | def productionReference |
---|
864 | if(params.entryFault.productionReference.id.isLong()) |
---|
865 | productionReference = ProductionReference.get(params.entryFault.productionReference.id.toLong()) |
---|
866 | |
---|
867 | def faultParams = [task: result.taskInstance, |
---|
868 | entryType: EntryType.get(1), |
---|
869 | comment: params.entryFault.comment, |
---|
870 | dateDone: result.taskInstance.targetStartDate, |
---|
871 | productionReference: productionReference, |
---|
872 | durationHour: params.entryFault.durationHour, |
---|
873 | durationMinute: params.entryFault.durationMinute] |
---|
874 | def faultResult = saveEntry(faultParams) |
---|
875 | result.entryFaultInstance = faultResult.entryInstance |
---|
876 | |
---|
877 | def causeParams = [task: result.taskInstance, |
---|
878 | entryType: EntryType.get(2), |
---|
879 | dateDone: result.taskInstance.targetStartDate, |
---|
880 | comment: params.entryCause.comment] |
---|
881 | def causeResult = saveEntry(causeParams) |
---|
882 | result.entryCauseInstance = causeResult.entryInstance |
---|
883 | |
---|
884 | def workDoneParams = [task: result.taskInstance, |
---|
885 | entryType: EntryType.get(3), |
---|
886 | comment: params.entryWorkDone.comment, |
---|
887 | dateDone: result.taskInstance.targetStartDate, |
---|
888 | durationHour: params.entryWorkDone.durationHour, |
---|
889 | durationMinute: params.entryWorkDone.durationMinute] |
---|
890 | def workDoneResult = saveEntry(workDoneParams) |
---|
891 | result.entryWorkDoneInstance = workDoneResult.entryInstance |
---|
892 | |
---|
893 | if(result.error) |
---|
894 | return result |
---|
895 | |
---|
896 | if(causeResult.error) |
---|
897 | return fail(code: "default.create.failure") |
---|
898 | |
---|
899 | if(faultResult.error) |
---|
900 | return fail(code: "default.create.failure") |
---|
901 | |
---|
902 | if(workDoneResult.error) |
---|
903 | return fail(code: "default.create.failure") |
---|
904 | |
---|
905 | // Success. |
---|
906 | return result |
---|
907 | |
---|
908 | } //end withTransaction |
---|
909 | } // end saveImmediateCallout() |
---|
910 | |
---|
911 | } // end TaskService |
---|