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

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

Exclude tasks in trash from subTask lists.
Add create subTask functionality.
Move possibleParentList() to TaskService.

File size: 19.6 KB
Line 
1/*
2* Provides a service for the Task domain class.
3*
4*/
5class TaskService {
6
7    boolean transactional = false
8
9    def dateUtilService
10    def personService
11
12    /*
13    * Determines and returns a possible parent list
14    * @taskInstance The task to use when determining the possible parent list.
15    * @returns A list of the possible parents.
16    */
17    def possibleParentList(taskInstance) {
18        def criteria = taskInstance.createCriteria()
19        def possibleParentList = criteria {
20            and {
21                notEqual('trash', true)
22                notEqual('id', taskInstance.id)
23                taskInstance.subTasks.each() { notEqual('id', it.id) }
24                }
25        }
26    }
27
28    /*
29    * Creates a new task with the given params.
30    * @params The params to use when creating the new task.
31    * @returns A map containing result.error=true (if any error) and result.taskInstance.
32    */
33    def create(params) {
34        Task.withTransaction { status ->
35            def result = [:]
36            // Default status to "not started" if not supplied.
37            params.taskStatus = params.taskStatus ?: TaskStatus.get(1)
38            def taskInstance = new Task(params)
39            result.taskInstance = taskInstance
40
41            if(result.taskInstance.parentTask?.trash) {
42                status.setRollbackOnly()
43                result.taskInstance.errors.rejectValue("parentTask", "task.operationNotPermittedOnTaskInTrash")
44                result.error = true
45                return result
46            }
47
48            if(taskInstance.save()) {
49                def taskModification = new TaskModification(person: personService.currentUser(),
50                                                        taskModificationType: TaskModificationType.get(1),
51                                                        task: taskInstance)
52
53                if(!taskModification.save()) {
54                    status.setRollbackOnly()
55                    taskInstance.errors.rejectValue("taskModifications", "task.modifications.failedToSave")
56                    result.error = true
57                    return result
58                }
59
60                // If we get here all went well.
61                return result
62            }
63            else {
64                result.error = true
65                return result
66            }
67
68        } //end withTransaction
69    } // end create()
70
71    /*
72    * Creates a subTask copying attributes from the parentTask unless otherwise specified.
73    * @param parentTask The parent task to get attributes from, also set as the parent.
74    * @param params Overrides the parent task values if specified.
75    * @returns A map containing result.error=true (if any error) and result.taskInstance.
76    */
77    def createSubTask(parentTask, params = [:]) {
78
79        def result = [:]
80
81        //Make our new Task a subTask and set the required properites.
82        def p = [:]
83        p.parentTask = parentTask
84        p.description = params.description ?: parentTask.description
85        p.comment = params.comment ?: parentTask.comment
86
87        p.taskGroup = params.taskGroup ?: parentTask.taskGroup
88        p.taskStatus = TaskStatus.get(1) // A new subTask must always be "Not Started".
89        p.taskPriority = parentTask.taskPriority
90        p.taskType = params.taskType ?: parentTask.taskType
91        p.leadPerson = params.leadPerson ?: parentTask.leadPerson
92        p.primaryAsset = params.primaryAsset ?: parentTask.primaryAsset
93
94        p.targetStartDate = params.targetStartDate ?: parentTask.targetStartDate
95        p.targetCompletionDate = params.targetCompletionDate ?: parentTask.targetCompletionDate
96
97                    //Set the assignedPersons
98//                     taskInstance.assignedPersons.each() {
99//
100//                     def assignedPerson = new AssignedPerson(person: it.person,
101//                                                                                             task: subTaskInstance,
102//                                                                                             estimatedHour: it.estimatedHour,
103//                                                                                             estimatedMinute: it.estimatedMinute).save()
104//                     }
105
106        result = create(p)
107
108    } // end createSubTask()
109
110    /*
111    * Creates a new task entry.
112    * @params The params to use when creating the new entry.
113    * @returns A map containing result.error=true (if any error), result.entryInstance and result.taskId.
114    */
115    def createEntry(params) {
116        Task.withTransaction { status ->
117            def result = [:]
118            result.entryInstance = new Entry(params)
119            result.entryInstance.enteredBy = personService.currentUser()
120
121            if(result.entryInstance.validate()) {
122                def taskInstance = Task.lock(result.entryInstance.task.id)
123                result.taskId = result.entryInstance.task.id
124
125                if(!taskInstance) {
126                    status.setRollbackOnly()
127                    result.entryInstance.errors.rejectValue('task', "entry.task.notFound")
128                    result.error = true
129                    return result
130                }
131
132                if(taskInstance.taskStatus.id == 3) {
133                    status.setRollbackOnly()
134                    result.entryInstance.errors.rejectValue('task', "task.operationNotPermittedOnCompleteTask")
135                    result.error = true
136                    return result
137                }
138
139                // If task status is "Not Started" and entry type is "Work Done" then we create the started modification and set the status.
140                if(taskInstance.taskStatus.id == 1 && result.entryInstance.entryType.id == 2) {
141
142                    // Create the "Started" task modification, this provides the "Actual started date".
143                    def taskModification = new TaskModification(person: personService.currentUser(),
144                                                            taskModificationType: TaskModificationType.get(2),
145                                                            task: taskInstance)
146
147                    if(!taskModification.save()) {
148                        status.setRollbackOnly()
149                        taskInstance.errors.rejectValue("task", "entry.task.failedToSaveTaskModification")
150                        result.error = true
151                        return result
152                    }
153
154                    // Set task status to "In progress".
155                    taskInstance.taskStatus = TaskStatus.get(2)
156
157                    if(!taskInstance.save()) {
158                        status.setRollbackOnly()
159                        result.entryInstance.errors.rejectValue("task", "entry.task.failedToSave")
160                        result.error = true
161                        return result
162                    }
163                }
164
165                if(!result.entryInstance.save()) {
166                    status.setRollbackOnly()
167                    result.error = true
168                    return result
169                }
170
171                // All went well if we get to here.
172                return result
173            }
174            else {
175                result.error = true
176                return result
177            }
178
179        } //end withTransaction
180    } // end create()
181
182    def update(params) {
183        Task.withTransaction { status ->
184            def result = [:]
185            result.taskInstance = Task.get(params.id)
186            if(result.taskInstance) {
187
188                // Optimistic locking check.
189                if(params.version) {
190                    def version = params.version.toLong()
191                    if(result.taskInstance.version > version) {
192                        status.setRollbackOnly()
193                        result.taskInstance.errors.rejectValue("version", "task.optimistic.locking.failure", "Another user has updated this Task while you were editing.")
194                        result.error = true
195                        return result
196                    }
197                }
198
199                result.taskInstance.properties = params
200
201                if(result.taskInstance.save()) {
202                    def taskModification = new TaskModification(person:personService.currentUser(),
203                                                            taskModificationType: TaskModificationType.get(3),
204                                                            task: result.taskInstance)
205                    if(taskModification.save()) {
206                        // All went well.
207                        return result
208                    }
209                    else {
210                        status.setRollbackOnly()
211                        result.taskInstance.errors.rejectValue("taskModifications", "task.modifications.failedToSave")
212                        result.error = true
213                        return result
214                    }
215                }
216            }
217            // Something failed.
218            status.setRollbackOnly()
219            result.error = true
220            return result
221
222        } //end withTransaction
223    }  // end update()
224
225    def complete(params) {
226        Task.withTransaction { status ->
227            def result = [:]
228            result.taskInstance = Task.get(params.id)
229            if(result.taskInstance) {
230
231                // Optimistic locking check.
232                if(params.version) {
233                    def version = params.version.toLong()
234                    if(result.taskInstance.version > version) {
235                        status.setRollbackOnly()
236                        result.taskInstance.errors.rejectValue("version", "task.optimistic.locking.failure", "Another user has updated this Task while you were editing.")
237                        result.error = true
238                        return result
239                    }
240                }
241
242                result.taskInstance.taskStatus = TaskStatus.get(3)
243
244                if(result.taskInstance.save()) {
245                    def taskModification = new TaskModification(person:personService.currentUser(),
246                                                            taskModificationType: TaskModificationType.get(4),
247                                                            task: result.taskInstance)
248                    if(taskModification.save()) {
249                        // All went well.
250                        return result
251                    }
252                    else {
253                        status.setRollbackOnly()
254                        result.taskInstance.errors.rejectValue("taskModifications", "task.modifications.failedToSave")
255                        result.error = true
256                        return result
257                    }
258                }
259            }
260            // Something failed.
261            status.setRollbackOnly()
262            result.error = true
263            return result
264
265        } //end withTransaction
266    }  // end complete()
267
268    def reopen(params) {
269        Task.withTransaction { status ->
270            def result = [:]
271            result.taskInstance = Task.get(params.id)
272            if(result.taskInstance) {
273
274                // Optimistic locking check.
275                if(params.version) {
276                    def version = params.version.toLong()
277                    if(result.taskInstance.version > version) {
278                        status.setRollbackOnly()
279                        result.taskInstance.errors.rejectValue("version", "task.optimistic.locking.failure", "Another user has updated this Task while you were editing.")
280                        result.error = true
281                        return result
282                    }
283                }
284
285                result.taskInstance.taskStatus = TaskStatus.get(2)
286
287                if(result.taskInstance.save()) {
288                    def taskModification = new TaskModification(person:personService.currentUser(),
289                                                            taskModificationType: TaskModificationType.get(5),
290                                                            task: result.taskInstance)
291                    if(taskModification.save()) {
292                        // All went well.
293                        return result
294                    }
295                    else {
296                        status.setRollbackOnly()
297                        result.taskInstance.errors.rejectValue("taskModifications", "task.modifications.failedToSave")
298                        result.error = true
299                        return result
300                    }
301                }
302            }
303            // Something failed.
304            status.setRollbackOnly()
305            result.error = true
306            return result
307
308        } //end withTransaction
309    }  // end reopen()
310
311    def trash(params) {
312        Task.withTransaction { status ->
313            def result = [:]
314            result.taskInstance = Task.get(params.id)
315            if(result.taskInstance) {
316
317                // Optimistic locking check.
318                if(params.version) {
319                    def version = params.version.toLong()
320                    if(result.taskInstance.version > version) {
321                        status.setRollbackOnly()
322                        result.taskInstance.errors.rejectValue("version", "task.optimistic.locking.failure", "Another user has updated this Task while you were editing.")
323                        result.error = true
324                        return result
325                    }
326                }
327
328                result.taskInstance.trash = true
329
330                if(result.taskInstance.save()) {
331                    def taskModification = new TaskModification(person:personService.currentUser(),
332                                                            taskModificationType: TaskModificationType.get(6),
333                                                            task: result.taskInstance)
334                    if(taskModification.save()) {
335                        // All went well.
336                        return result
337                    }
338                    else {
339                        status.setRollbackOnly()
340                        result.taskInstance.errors.rejectValue("taskModifications", "task.modifications.failedToSave")
341                        result.error = true
342                        return result
343                    }
344                }
345            }
346            // Something failed.
347            status.setRollbackOnly()
348            result.error = true
349            return result
350
351        } //end withTransaction
352    }  // end trash()
353
354    def restore(params) {
355        Task.withTransaction { status ->
356            def result = [:]
357            result.taskInstance = Task.get(params.id)
358            if(result.taskInstance) {
359
360                // Optimistic locking check.
361                if(params.version) {
362                    def version = params.version.toLong()
363                    if(result.taskInstance.version > version) {
364                        status.setRollbackOnly()
365                        result.taskInstance.errors.rejectValue("version", "task.optimistic.locking.failure", "Another user has updated this Task while you were editing.")
366                        result.error = true
367                        return result
368                    }
369                }
370
371                result.taskInstance.trash = false
372
373                if(result.taskInstance.save()) {
374                    def taskModification = new TaskModification(person:personService.currentUser(),
375                                                            taskModificationType: TaskModificationType.get(7),
376                                                            task: result.taskInstance)
377                    if(taskModification.save()) {
378                        // All went well.
379                        return result
380                    }
381                    else {
382                        status.setRollbackOnly()
383                        result.taskInstance.errors.rejectValue("taskModifications", "task.modifications.failedToSave")
384                        result.error = true
385                        return result
386                    }
387                }
388            }
389            // Something failed.
390            status.setRollbackOnly()
391            result.error = true
392            return result
393
394        } //end withTransaction
395    }  // end restore()
396
397    def approve(params) {
398        Task.withTransaction { status ->
399            def result = [:]
400            result.taskInstance = Task.get(params.id)
401            if(result.taskInstance) {
402
403                // Optimistic locking check.
404                if(params.version) {
405                    def version = params.version.toLong()
406                    if(result.taskInstance.version > version) {
407                        status.setRollbackOnly()
408                        result.taskInstance.errors.rejectValue("version", "task.optimistic.locking.failure", "Another user has updated this Task while you were editing.")
409                        result.error = true
410                        return result
411                    }
412                }
413
414                result.taskInstance.approved = true
415
416                if(result.taskInstance.save()) {
417                    def taskModification = new TaskModification(person:personService.currentUser(),
418                                                            taskModificationType: TaskModificationType.get(8),
419                                                            task: result.taskInstance)
420                    if(taskModification.save()) {
421                        // All went well.
422                        return result
423                    }
424                    else {
425                        status.setRollbackOnly()
426                        result.taskInstance.errors.rejectValue("taskModifications", "task.modifications.failedToSave")
427                        result.error = true
428                        return result
429                    }
430                }
431            }
432            // Something failed.
433            status.setRollbackOnly()
434            result.error = true
435            return result
436
437        } //end withTransaction
438    }  // end approve()
439
440    def renegeApproval(params) {
441        Task.withTransaction { status ->
442            def result = [:]
443            result.taskInstance = Task.get(params.id)
444            if(result.taskInstance) {
445
446                // Optimistic locking check.
447                if(params.version) {
448                    def version = params.version.toLong()
449                    if(result.taskInstance.version > version) {
450                        status.setRollbackOnly()
451                        result.taskInstance.errors.rejectValue("version", "task.optimistic.locking.failure", "Another user has updated this Task while you were editing.")
452                        result.error = true
453                        return result
454                    }
455                }
456
457                result.taskInstance.approved = false
458
459                if(result.taskInstance.save()) {
460                    def taskModification = new TaskModification(person:personService.currentUser(),
461                                                            taskModificationType: TaskModificationType.get(9),
462                                                            task: result.taskInstance)
463                    if(taskModification.save()) {
464                        // All went well.
465                        return result
466                    }
467                    else {
468                        status.setRollbackOnly()
469                        result.taskInstance.errors.rejectValue("taskModifications", "task.modifications.failedToSave")
470                        result.error = true
471                        return result
472                    }
473                }
474            }
475            // Something failed.
476            status.setRollbackOnly()
477            result.error = true
478            return result
479
480        } //end withTransaction
481    }  // end renegeApproval()
482
483} // end TaskService
Note: See TracBrowser for help on using the repository browser.