source: trunk/grails-app/services/TaskService.groovy @ 482

Last change on this file since 482 was 482, checked in by gav, 14 years ago

Refactor entry creation to provide correct error messages when trying to create a new entry on a complete task.

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