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
RevLine 
[202]1/**
2* Provides a service class for the Task domain class.
[196]3*
4*/
[137]5class TaskService {
6
[180]7    boolean transactional = false
[137]8
[291]9    def authService
[251]10    def assignedGroupService
11    def assignedPersonService
[137]12
[202]13    /**
[203]14    * Determines and returns a possible parent list for a task.
[245]15    * @todo Create and use another method that limits the results to say the latest 20 or 100 tasks?
[202]16    * @param taskInstance The task to use when determining the possible parent list.
[196]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
[202]30    /**
[433]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    /**
[196]80    * Creates a new task with the given params.
[202]81    * @param params The params to use when creating the new task.
[418]82    * @returns A map containing result.error (if any error) and result.taskInstance.
[196]83    */
[394]84    def save(params) {
[180]85        Task.withTransaction { status ->
86            def result = [:]
[418]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
[181]96            // Default status to "not started" if not supplied.
97            params.taskStatus = params.taskStatus ?: TaskStatus.get(1)
[252]98
99            // Set budgetStatus.
[418]100            if(params.taskType?.id?.toLong() == 1 || params.taskType?.id?.toLong() == 2) // Immediate Callout or Unsheduled Breakin.
[252]101                params.taskBudgetStatus = params.taskBudgetStatus ?: TaskBudgetStatus.get(1) // Unplanned.
102            else
103                params.taskBudgetStatus = params.taskBudgetStatus ?: TaskBudgetStatus.get(2) // Planned.
104
[180]105            def taskInstance = new Task(params)
106            result.taskInstance = taskInstance
107
[418]108            if(result.taskInstance.parentTask?.trash)
109                return fail(field:"parentTask", code:"task.operationNotPermittedOnTaskInTrash")
[196]110
[418]111            if(result.taskInstance.hasErrors() || !result.taskInstance.save())
112                return fail(code:"default.create.failure")
[180]113
[418]114            def taskModification = new TaskModification(person: authService.currentUser,
115                                                taskModificationType: TaskModificationType.get(1),
116                                                task: taskInstance)
[180]117
[418]118            if(taskModification.hasErrors() || !taskModification.save())
119                return fail(field:"taskModifications", code:"task.modifications.failedToSave")
[243]120
[418]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() {
[251]126
[418]127                    assignedGroupParams = [personGroup: it.personGroup,
128                                                                task: taskInstance,
129                                                                estimatedHour: it.estimatedHour,
130                                                                estimatedMinute: it.estimatedMinute]
[251]131
[418]132                    assignedGroupsResult = assignedGroupService.save(assignedGroupParams)
[251]133
[418]134                    if(assignedGroupsResult.error)
135                        return fail(field:"assignedGroups", code:"task.assignedGroups.failedToSave")
136
[243]137                }
[418]138            }
[243]139
[418]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() {
[243]145
[418]146                    assignedPersonsParams = [person: it.person,
147                                                                task: taskInstance,
148                                                                estimatedHour: it.estimatedHour,
149                                                                estimatedMinute: it.estimatedMinute]
[251]150
[418]151                    assignedPersonsResult = assignedPersonService.save(assignedPersonsParams)
[251]152
[418]153                    if(assignedPersonsResult.error)
154                        return fail(field:"assignedPersons", code:"task.assignedPersons.failedToSave")
[251]155
[243]156                }
[180]157            }
158
[418]159            // Success.
160            return result
161
[180]162        } //end withTransaction
[394]163    } // end save()
[180]164
[202]165    /**
[245]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.
[196]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
[245]185        p.targetStartDate = params.targetStartDate ?: parentTask.targetStartDate
186        p.targetCompletionDate = params.targetCompletionDate ?: parentTask.targetCompletionDate
[478]187        p.scheduled = params.scheduled ?:  parentTask.scheduled
[196]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
[245]195        p.associatedAssets = params.associatedAssets ?: new ArrayList(parentTask.associatedAssets) // Collection.
[196]196
[245]197        // Supplied by recurring tasks.
[202]198        if(params.taskProcedure) p.taskProcedure = params.taskProcedure
[245]199        if(params.assignedGroups) p.assignedGroups = params.assignedGroups // Collection.
200        if(params.assignedPersons) p.assignedPersons = params.assignedPersons // Collection.
[202]201
[245]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.
[394]208        result = save(p)
[196]209
[478]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
[196]220    } // end createSubTask()
221
[202]222    /**
[196]223    * Creates a new task entry.
[202]224    * @param params The params to use when creating the new entry.
[196]225    * @returns A map containing result.error=true (if any error), result.entryInstance and result.taskId.
226    */
[394]227    def saveEntry(params) {
[186]228        Task.withTransaction { status ->
229            def result = [:]
[395]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
[186]239            result.entryInstance = new Entry(params)
[291]240            result.entryInstance.enteredBy = authService.currentUser
[180]241
[395]242            def taskInstance
243            if(result.entryInstance.task.id) {
[186]244                result.taskId = result.entryInstance.task.id
[395]245                taskInstance = Task.lock(result.entryInstance.task.id)
246            }
[186]247
[395]248            if(!taskInstance)
249                return fail(field:"task", code:"task.notFound")
[186]250
[395]251            if(result.entryInstance.hasErrors() || !result.entryInstance.save())
252                return fail(code:"default.create.failure")
[186]253
[395]254            if(taskInstance.taskStatus.id == 3)
255                return fail(field:"task", code:"task.operationNotPermittedOnCompleteTask")
[186]256
[395]257            // If task status is "Not Started" and entry type is "Work Done" then we create the started modification and set the status.
[418]258            if(taskInstance.taskStatus.id == 1 && result.entryInstance.entryType.id == 3) {
[186]259
[395]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)
[186]264
[395]265                if(taskModification.hasErrors() || !taskModification.save())
266                    return fail(field:"task", code:"task.modifications.failedToSave")
[186]267
[395]268                // Set task status to "In Progress".
269                taskInstance.taskStatus = TaskStatus.get(2)
[186]270
[395]271                if(taskInstance.hasErrors() || !taskInstance.save())
272                    return fail(field:"task", code:"task.failedToSave")
[186]273            }
274
[395]275            // Success.
276            return result
277
[482]278        } // end withTransaction
[394]279    } // end saveEntry()
[186]280
[202]281    /**
282    * Updates an existing task.
283    * @param params The params to update for task with id of params.id.
[418]284    * @returns A map containing result.error (if any error) and result.taskInstance (if available).
[202]285    */
[180]286    def update(params) {
287        Task.withTransaction { status ->
288            def result = [:]
[204]289
[418]290            def fail = { Map m ->
[204]291                status.setRollbackOnly()
[418]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] ]
[204]295                return result
296            }
297
[180]298            result.taskInstance = Task.get(params.id)
299
[204]300            if(!result.taskInstance)
[206]301                return fail('id', "task.notFound")
[180]302
[204]303            // Optimistic locking check.
304            if(params.version) {
[418]305                if(result.taskInstance.version > params.version.toLong())
306                    return fail(field:"version", code:"default.optimistic.locking.failure")
[204]307            }
[180]308
[204]309            result.taskInstance.properties = params
310
311            if(result.taskInstance.hasErrors() || !result.taskInstance.save())
[418]312                return fail(code:"default.update.failure")
[204]313
[291]314            def taskModification = new TaskModification(person:authService.currentUser,
[204]315                                                    taskModificationType: TaskModificationType.get(3),
316                                                    task: result.taskInstance)
317
[418]318            if(taskModification.hasErrors() || !taskModification.save())
319                return fail(code:"task.modifications.failedToSave")
[204]320
[418]321            // Success.
[180]322            return result
323
324        } //end withTransaction
325    }  // end update()
326
[202]327    /**
328    * Completes an existing task.
329    * @param params The params for task with id of params.id.
[418]330    * @returns A map containing result.error (if any error) and result.taskInstance (if available).
[202]331    */
[181]332    def complete(params) {
333        Task.withTransaction { status ->
334            def result = [:]
[418]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
[181]344            result.taskInstance = Task.get(params.id)
345
[418]346            if(!result.taskInstance)
347                return fail(code:"default.not.found")
[181]348
[418]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            }
[181]354
[418]355            result.taskInstance.taskStatus = TaskStatus.get(3)
356            result.taskInstance.attentionFlag = false
357            result.taskInstance.taskRecurringSchedule?.enabled = false
[201]358
[418]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.
[181]371            return result
372
373        } //end withTransaction
[180]374    }  // end complete()
375
[202]376    /**
[418]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    /**
[202]469    * Reopens an existing task.
470    * @param params The params for task with id of params.id.
[418]471    * @returns A map containing result.error (if any error) and result.taskInstance (if available).
[202]472    */
[181]473    def reopen(params) {
474        Task.withTransaction { status ->
475            def result = [:]
[418]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
[181]485            result.taskInstance = Task.get(params.id)
486
[418]487            if(!result.taskInstance)
488                return fail(code:"default.not.found")
[181]489
[418]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            }
[181]495
[418]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.
[181]509            return result
510
511        } //end withTransaction
[180]512    }  // end reopen()
513
[202]514    /**
515    * Move a task to the trash.
516    * @param params The params for task with id of params.id.
[418]517    * @returns A map containing result.error (if any error) and result.taskInstance (if available).
[202]518    */
[181]519    def trash(params) {
520        Task.withTransaction { status ->
521            def result = [:]
[418]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
[181]531            result.taskInstance = Task.get(params.id)
532
[418]533            if(!result.taskInstance)
534                return fail(code:"default.not.found")
[181]535
[418]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            }
[181]541
[418]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.
[181]557            return result
558
559        } //end withTransaction
[180]560    }  // end trash()
561
[202]562    /**
563    * Restore a task from the trash.
564    * @param params The params for task with id of params.id.
[418]565    * @returns A map containing result.error (if any error) and result.taskInstance (if available).
[202]566    */
[181]567    def restore(params) {
568        Task.withTransaction { status ->
569            def result = [:]
[418]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
[181]579            result.taskInstance = Task.get(params.id)
580
[418]581            if(!result.taskInstance)
582                return fail(code:"default.not.found")
[181]583
[418]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            }
[181]589
[418]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.
[181]603            return result
604
605        } //end withTransaction
[180]606    }  // end restore()
607
[202]608    /**
609    * Approve a task.
610    * @param params The params for task with id of params.id.
[418]611    * @returns A map containing result.error (if any error) and result.taskInstance (if available).
[202]612    */
[181]613    def approve(params) {
614        Task.withTransaction { status ->
615            def result = [:]
[418]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
[181]625            result.taskInstance = Task.get(params.id)
626
[418]627            if(!result.taskInstance)
628                return fail(code:"default.not.found")
[181]629
[418]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            }
[181]635
[418]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.
[181]649            return result
650
651        } //end withTransaction
[180]652    }  // end approve()
653
[202]654    /**
655    * Remove a previously given approval from a task.
656    * @param params The params for task with id of params.id.
[418]657    * @returns A map containing result.error (if any error) and result.taskInstance (if available).
[202]658    */
[181]659    def renegeApproval(params) {
660        Task.withTransaction { status ->
661            def result = [:]
[418]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
[181]671            result.taskInstance = Task.get(params.id)
672
[418]673            if(!result.taskInstance)
674                return fail(code:"default.not.found")
[181]675
[418]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            }
[181]681
[418]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.
[181]695            return result
696
697        } //end withTransaction
[180]698    }  // end renegeApproval()
699
[395]700    /**
[433]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    /**
[418]746    * Creates a new immediate callout task with the given params.
[395]747    * @param params The params to use when creating the new task.
[418]748    * @returns A map containing result.error (if any error) and result.taskInstance.
[395]749    */
[418]750    def saveImmediateCallout(params) {
[395]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
[418]768            // Always for an immediate callout.
769            result.taskInstance.taskType = TaskType.get(1) // Immediate Callout.
[395]770            result.taskInstance.taskBudgetStatus = TaskBudgetStatus.get(1) // Unplanned.
[433]771            result.taskInstance.taskPriority = TaskPriority.get(1) // Immediate.
[395]772            result.taskInstance.taskGroup = TaskGroup.get(1) // Engineering Activites.
773            result.taskInstance.approved = true
774            result.taskInstance.leadPerson = authService.currentUser
[432]775            result.taskInstance.targetCompletionDate = result.taskInstance.targetStartDate
[395]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
[431]789            def productionReference
790            if(params.entryFault.productionReference.id.isLong())
791                productionReference = ProductionReference.get(params.entryFault.productionReference.id.toLong())
792
[395]793            def faultParams = [task: result.taskInstance,
794                                            entryType: EntryType.get(1),
795                                            comment: params.entryFault.comment,
[432]796                                            dateDone: result.taskInstance.targetStartDate,
[431]797                                            productionReference: productionReference,
[395]798                                            durationHour: params.entryFault.durationHour,
799                                            durationMinute: params.entryFault.durationMinute]
800            def faultResult = saveEntry(faultParams)
801            result.entryFaultInstance = faultResult.entryInstance
802
[418]803            def causeParams = [task: result.taskInstance,
804                                            entryType: EntryType.get(2),
[432]805                                            dateDone: result.taskInstance.targetStartDate,
[418]806                                            comment: params.entryCause.comment]
807            def causeResult = saveEntry(causeParams)
808            result.entryCauseInstance = causeResult.entryInstance
809
[395]810            def workDoneParams = [task: result.taskInstance,
[418]811                                                    entryType: EntryType.get(3),
[395]812                                                    comment: params.entryWorkDone.comment,
[432]813                                            dateDone: result.taskInstance.targetStartDate,
[395]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
[418]822            if(causeResult.error)
823                return fail(code: "default.create.failure")
824
[395]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
[418]835    } // end saveImmediateCallout()
[395]836
[180]837} // end TaskService
Note: See TracBrowser for help on using the repository browser.