source: trunk/grails-app/services/TaskRecurringScheduleService.groovy @ 387

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

Implement copying of assigned groups and persons to auto generated sub tasks.
As per ticket #55.

File size: 5.3 KB
Line 
1class TaskRecurringScheduleService {
2
3    boolean transactional = false
4
5    def dateUtilService
6    def taskService
7
8    /**
9    * Generate all enabled recurring tasks.
10    */
11    def generateAll() {
12
13        def taskRecurringScheduleList = TaskRecurringSchedule.findAllByEnabled(true)
14
15        taskRecurringScheduleList.each() {
16
17            if ( dateUtilService.tomorrow > it.nextGenerationDate) {
18                    def p = [:]
19
20                    // Build the required params.
21                    p.targetStartDate = it.nextTargetStartDate
22                    p.targetCompletionDate = it.nextTargetCompletionDate
23                    if(it.task.taskProcedure) p.taskProcedure = it.task.taskProcedure
24                    if(it.task.assignedGroups) p.assignedGroups = new ArrayList(it.task.assignedGroups)
25                    if(it.task.assignedPersons) p.assignedPersons = new ArrayList(it.task.assignedPersons)
26
27                    def result = taskService.createSubTask(it.task, p)
28                    if( !result.error ) {
29                        it.lastGeneratedSubTask = result.taskInstance
30                        it.subTasksGenerated++
31                        it.setNextTargetStartDate()
32                        it.setNextGenerationDate()
33                        it.setNextTargetCompletionDate()
34                    }
35                    else {
36                        log.error "Sub task generation for recurring schedule ${it.id} failed."
37                        log.error result.taskInstance.errors
38                    }
39            }
40
41        }
42    }
43
44    /**
45    * Creates a new recurring schedule for a task with the given params.
46    * @param params The params to use when creating the new recurring schedule.
47    * @returns A map containing result.error=true (if any error) and result.taskRecurringScheduleInstance and result.taskId.
48    */
49    def create(params) {
50        TaskRecurringSchedule.withTransaction { status ->
51            def result = [:]
52
53            def fail = { Object[] args ->
54                status.setRollbackOnly()
55                if(args.size() == 2) result.taskRecurringScheduleInstance.errors.rejectValue(args[0], args[1])
56                result.error = true
57                return result
58            }
59
60            result.taskRecurringScheduleInstance = new TaskRecurringSchedule(params)
61            result.taskId = result.taskRecurringScheduleInstance.task.id
62
63            if(!result.taskRecurringScheduleInstance.validate())
64                return fail()
65
66            def taskInstance = Task.lock(result.taskId)
67
68            if(!taskInstance)
69                return fail('task', "task.notFound")
70
71            if(taskInstance.taskRecurringSchedule)
72                return fail('task', "tast.taskRecurringSchedule.alreadyExists")
73
74            if(taskInstance.taskStatus.id == 3)
75                return fail('task', "task.operationNotPermittedOnCompleteTask")
76
77            if(taskInstance.trash)
78                return fail('task', "task.operationNotPermittedOnTaskInTrash")
79
80            if(result.taskRecurringScheduleInstance.nextTargetStartDate < dateUtilService.today)
81                return fail("nextTargetStartDate", "taskRecurringSchedule.nextTargetStartDate.mayNotBePast")
82
83            taskInstance.taskRecurringSchedule = result.taskRecurringScheduleInstance
84
85            if(!result.taskRecurringScheduleInstance.save() || !taskInstance.save())
86                return fail()
87
88             // If we get here all went well.
89            return result
90
91        } //end withTransaction
92    } // end create()
93
94    /**
95    * Updates an existing recurring schedule.
96    * @param params The params to update for recurring schedule with id of params.id.
97    * @returns A map containing result.error=true (if any error) and result.taskRecurringScheduleInstance (if available).
98    */
99    def update(params) {
100        TaskRecurringSchedule.withTransaction { status ->
101            def result = [:]
102
103            def fail = { Object[] args ->
104                status.setRollbackOnly()
105                if(args.size() == 2) result.taskRecurringScheduleInstance.errors.rejectValue(args[0], args[1])
106                result.error = true
107                return result
108            }
109
110            result.taskRecurringScheduleInstance = TaskRecurringSchedule.get(params.id)
111
112            if(!result.taskRecurringScheduleInstance)
113                return fail('id', "taskRecurringSchedule.notFound")
114
115            // Optimistic locking check.
116            if(params.version) {
117                def version = params.version.toLong()
118                if(result.taskRecurringScheduleInstance.version > version)
119                    return fail("version", "default.optimistic.locking.failure")
120            }
121
122            result.taskRecurringScheduleInstance.properties = params
123
124            if(result.taskRecurringScheduleInstance.nextTargetStartDate < dateUtilService.today)
125                return fail("nextTargetStartDate", "taskRecurringSchedule.nextTargetStartDate.mayNotBePast")
126
127            result.taskRecurringScheduleInstance.setNextGenerationDate()
128            result.taskRecurringScheduleInstance.setNextTargetCompletionDate()
129
130            if(result.taskRecurringScheduleInstance.hasErrors() || !result.taskRecurringScheduleInstance.save())
131                return fail()
132
133            // If we get here all went well.
134            return result
135
136        } //end withTransaction
137    }  // end update()
138
139} // end of class
Note: See TracBrowser for help on using the repository browser.