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
RevLine 
[131]1import org.codehaus.groovy.grails.plugins.springsecurity.Secured
[136]2import org.codehaus.groovy.runtime.TimeCategory
[137]3import java.text.SimpleDateFormat
[131]4
5class TaskRecurringScheduleDetailedController extends BaseController {
[157]6
7    def dateUtilService
[203]8    def taskRecurringScheduleService
[157]9
[131]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) {
[157]24            flash.message = "Recurring Schedule not found with id ${params.id}"
[131]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 {
[199]34                taskRecurringScheduleInstance.delete(flush: true)
[157]35                flash.message = "Recurring Schedule ${params.id} deleted"
[131]36                redirect(action:list)
37            }
38            catch(org.springframework.dao.DataIntegrityViolationException e) {
[157]39                flash.message = "Recurring Schedule ${params.id} could not be deleted"
[131]40                redirect(action:show,id:params.id)
41            }
42        }
43        else {
[157]44            flash.message = "Recurring Schedule not found with id ${params.id}"
[131]45            redirect(action:list)
46        }
47    }
48
49    def edit = {
50        def taskRecurringScheduleInstance = TaskRecurringSchedule.get( params.id )
51
52        if(!taskRecurringScheduleInstance) {
[157]53            flash.message = "Recurring Schedule not found with id ${params.id}"
[131]54            redirect(action:list)
55        }
56        else {
[137]57            return [ taskRecurringScheduleInstance : taskRecurringScheduleInstance]
[131]58        }
59    }
60
61    def update = {
[157]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
[199]76                Date originalDate = taskRecurringScheduleInstance.nextTargetStartDate
[157]77                taskRecurringScheduleInstance.properties = params  // Domain object is now 'dirty'.
[199]78                Date newDate = taskRecurringScheduleInstance.nextTargetStartDate
[157]79
[199]80                // If user changes nextTargetStartDate then ensure it is in the future, otherwise it's ok to keep the original date.
[157]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'.
[199]86                        taskRecurringScheduleInstance.errors.rejectValue("nextTargetStartDate", "taskRecurring.nextTargetStartDate.NotInTheFuture")
[157]87                        render(view:'edit',model:[taskRecurringScheduleInstance:taskRecurringScheduleInstance])
88                        return
89                    }
90                }
91
[199]92                taskRecurringScheduleInstance.setNextGenerationDate()
[157]93                taskRecurringScheduleInstance.setNextTargetCompletionDate()
94
[199]95                if(!taskRecurringScheduleInstance.hasErrors() && taskRecurringScheduleInstance.save(flush: true))
[157]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
[131]114    def create = {
[203]115        if(!params.task?.id || !Task.exists(params.task?.id)) {
116            flash.message = "Please select a task, then Create a Recurring Schedule for it."
[199]117            redirect(controller:"taskDetailed", action:"search")
[203]118            return
[134]119        }
[203]120        def taskRecurringScheduleInstance = new TaskRecurringSchedule()
121        taskRecurringScheduleInstance.properties = params
122        return [taskRecurringScheduleInstance: taskRecurringScheduleInstance]
[137]123    } // end create()
[131]124
125    def save = {
[203]126        def result = taskRecurringScheduleService.create(params)
[134]127
[203]128        if(!result.error) {
129            flash.message = "Recurring Schedule ${result.taskRecurringScheduleInstance.id} created."
130            redirect(action:show, id: result.taskRecurringScheduleInstance.id)
[157]131        }
132        else {
[203]133            render(view:'create',model:[taskRecurringScheduleInstance: result.taskRecurringScheduleInstance])
[157]134        }
[137]135    } // end save()
[136]136
[131]137}
Note: See TracBrowser for help on using the repository browser.