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

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

Small fix to prevent recurring schedule generating errors before base data has been created.

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