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

Last change on this file since 207 was 207, checked in by gav, 10 years ago

Refactor taskRecurringSchedule update() to a service method.

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