source: trunk/grails-app/controllers/TaskRecurringScheduleDetailedController.groovy @ 205

Last change on this file since 205 was 203, checked in by gav, 15 years ago

Move task recurring schedule creation to a service.

File size: 5.7 KB
Line 
1import org.codehaus.groovy.grails.plugins.springsecurity.Secured
2import org.codehaus.groovy.runtime.TimeCategory
3import java.text.SimpleDateFormat
4
5class TaskRecurringScheduleDetailedController extends BaseController {
6
7    def dateUtilService
8    def taskRecurringScheduleService
9
10    def index = { redirect(action:list,params:params) }
11
12    // the delete, save and update actions only accept POST requests
13    static allowedMethods = [delete:'POST', save:'POST', update:'POST']
14
15    def list = {
16        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
17        [ taskRecurringScheduleInstanceList: TaskRecurringSchedule.list( params ), taskRecurringScheduleInstanceTotal: TaskRecurringSchedule.count() ]
18    }
19
20    def show = {
21        def taskRecurringScheduleInstance = TaskRecurringSchedule.get( params.id )
22
23        if(!taskRecurringScheduleInstance) {
24            flash.message = "Recurring Schedule not found with id ${params.id}"
25            redirect(action:list)
26        }
27        else { return [ taskRecurringScheduleInstance : taskRecurringScheduleInstance ] }
28    }
29
30    def delete = {
31        def taskRecurringScheduleInstance = TaskRecurringSchedule.get( params.id )
32        if(taskRecurringScheduleInstance) {
33            try {
34                taskRecurringScheduleInstance.delete(flush: true)
35                flash.message = "Recurring Schedule ${params.id} deleted"
36                redirect(action:list)
37            }
38            catch(org.springframework.dao.DataIntegrityViolationException e) {
39                flash.message = "Recurring Schedule ${params.id} could not be deleted"
40                redirect(action:show,id:params.id)
41            }
42        }
43        else {
44            flash.message = "Recurring Schedule not found with id ${params.id}"
45            redirect(action:list)
46        }
47    }
48
49    def edit = {
50        def taskRecurringScheduleInstance = TaskRecurringSchedule.get( params.id )
51
52        if(!taskRecurringScheduleInstance) {
53            flash.message = "Recurring Schedule not found with id ${params.id}"
54            redirect(action:list)
55        }
56        else {
57            return [ taskRecurringScheduleInstance : taskRecurringScheduleInstance]
58        }
59    }
60
61    def update = {
62        TaskRecurringSchedule.withTransaction { status ->
63
64            def taskRecurringScheduleInstance = TaskRecurringSchedule.get( params.id )
65            if(taskRecurringScheduleInstance) {
66
67                if(params.version) {
68                    def version = params.version.toLong()
69                    if(taskRecurringScheduleInstance.version > version) {
70                        taskRecurringScheduleInstance.errors.rejectValue("version", "taskRecurringSchedule.optimistic.locking.failure", "Another user has updated this Recurring Schedule while you were editing.")
71                        render(view:'edit',model:[taskRecurringScheduleInstance:taskRecurringScheduleInstance])
72                        return
73                    }
74                }
75
76                Date originalDate = taskRecurringScheduleInstance.nextTargetStartDate
77                taskRecurringScheduleInstance.properties = params  // Domain object is now 'dirty'.
78                Date newDate = taskRecurringScheduleInstance.nextTargetStartDate
79
80                // If user changes nextTargetStartDate then ensure it is in the future, otherwise it's ok to keep the original date.
81                if(originalDate.getTime() != newDate.getTime())
82                {
83                    if(newDate < dateUtilService.getToday()) 
84                    {
85                        status.setRollbackOnly()  // Only allow the transaction to Rollback, preventing flush due to 'dirty'.
86                        taskRecurringScheduleInstance.errors.rejectValue("nextTargetStartDate", "taskRecurring.nextTargetStartDate.NotInTheFuture")
87                        render(view:'edit',model:[taskRecurringScheduleInstance:taskRecurringScheduleInstance])
88                        return
89                    }
90                }
91
92                taskRecurringScheduleInstance.setNextGenerationDate()
93                taskRecurringScheduleInstance.setNextTargetCompletionDate()
94
95                if(!taskRecurringScheduleInstance.hasErrors() && taskRecurringScheduleInstance.save(flush: true))
96                {
97                    flash.message = "Recurring Schedule ${params.id} updated"
98                    redirect(action:show,id:taskRecurringScheduleInstance.id)
99                }
100                else 
101                {
102                    render(view:'edit',model:[taskRecurringScheduleInstance:taskRecurringScheduleInstance])
103                }
104            }
105            else 
106            {
107                flash.message = "Recurring Schedule not found with id ${params.id}"
108                redirect(action:edit,id:params.id)
109            }
110
111        } // end withTransaction
112    } // end update()
113
114    def create = {
115        if(!params.task?.id || !Task.exists(params.task?.id)) {
116            flash.message = "Please select a task, then Create a Recurring Schedule for it."
117            redirect(controller:"taskDetailed", action:"search")
118            return
119        }
120        def taskRecurringScheduleInstance = new TaskRecurringSchedule()
121        taskRecurringScheduleInstance.properties = params
122        return [taskRecurringScheduleInstance: taskRecurringScheduleInstance]
123    } // end create()
124
125    def save = {
126        def result = taskRecurringScheduleService.create(params)
127
128        if(!result.error) {
129            flash.message = "Recurring Schedule ${result.taskRecurringScheduleInstance.id} created."
130            redirect(action:show, id: result.taskRecurringScheduleInstance.id)
131        }
132        else {
133            render(view:'create',model:[taskRecurringScheduleInstance: result.taskRecurringScheduleInstance])
134        }
135    } // end save()
136
137}
Note: See TracBrowser for help on using the repository browser.