Ignore:
Timestamp:
Dec 1, 2009, 4:51:32 AM (15 years ago)
Author:
gav
Message:

Refactor taskRecurringSchedule update() to a service method.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/grails-app/controllers/TaskRecurringScheduleDetailedController.groovy

    r203 r207  
    6060
    6161    def update = {
    62         TaskRecurringSchedule.withTransaction { status ->
    6362
    64             def taskRecurringScheduleInstance = TaskRecurringSchedule.get( params.id )
    65             if(taskRecurringScheduleInstance) {
     63        if(!TaskRecurringSchedule.exists(params.id)) {
     64            flash.message = "Recurring Schedule not found with id ${params.id}"
     65            redirect(action: 'list')
     66        }
    6667
    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                 }
     68        def result = taskRecurringScheduleService.update(params)
    7569
    76                 Date originalDate = taskRecurringScheduleInstance.nextTargetStartDate
    77                 taskRecurringScheduleInstance.properties = params  // Domain object is now 'dirty'.
    78                 Date newDate = taskRecurringScheduleInstance.nextTargetStartDate
     70        if(!result.error) {
     71                flash.message = "Recurring Schedule ${params.id} updated"
     72                redirect(action: 'show', id: result.taskRecurringScheduleInstance.id)
     73        }
     74        else {
     75            render(view:'edit',model:[taskRecurringScheduleInstance: result.taskRecurringScheduleInstance.attach()])
     76        }
    7977
    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()
     78    }
    11379
    11480    def create = {
Note: See TracChangeset for help on using the changeset viewer.