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

Last change on this file since 226 was 219, checked in by gav, 15 years ago

Make some dates more groovy.

File size: 5.1 KB
RevLine 
[199]1class TaskRecurringScheduleService {
2
3    boolean transactional = false
4
5    def dateUtilService
6    def taskService
7
[203]8    /**
9    * Generate all enabled recurring tasks.
10    */
[199]11    def generateAll() {
12
13        def taskRecurringScheduleList = TaskRecurringSchedule.findAllByEnabled(true)
14
15        taskRecurringScheduleList.each() {
16
[219]17            if ( dateUtilService.tomorrow > it.nextGenerationDate) {
[199]18                    def p = [:]
19                    p.targetStartDate = it.nextTargetStartDate
20                    p.targetCompletionDate = it.nextTargetCompletionDate
[203]21                    if(it.task.taskProcedure) p.taskProcedure = it.task.taskProcedure
[199]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 {
[203]31                        log.error "Sub task generation for recurring schedule ${it.id} failed."
32                        log.error result.taskInstance.errors
[199]33                    }
34            }
35
36        }
37    }
38
[203]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
[219]75            if(result.taskRecurringScheduleInstance.nextTargetStartDate < dateUtilService.today)
[213]76                return fail("nextTargetStartDate", "taskRecurringSchedule.nextTargetStartDate.mayNotBePast")
[203]77
78            taskInstance.taskRecurringSchedule = result.taskRecurringScheduleInstance
79
80            if(!result.taskRecurringScheduleInstance.save() || !taskInstance.save())
81                return fail()
82
[207]83             // If we get here all went well.
[203]84            return result
85
86        } //end withTransaction
87    } // end create()
88
[207]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.properties = params
118
[219]119            if(result.taskRecurringScheduleInstance.nextTargetStartDate < dateUtilService.today)
[213]120                return fail("nextTargetStartDate", "taskRecurringSchedule.nextTargetStartDate.mayNotBePast")
[207]121
122            result.taskRecurringScheduleInstance.setNextGenerationDate()
123            result.taskRecurringScheduleInstance.setNextTargetCompletionDate()
124
125            if(result.taskRecurringScheduleInstance.hasErrors() || !result.taskRecurringScheduleInstance.save())
126                return fail()
127
128            // If we get here all went well.
129            return result
130
131        } //end withTransaction
132    }  // end update()
133
[199]134} // end of class
Note: See TracBrowser for help on using the repository browser.