Changeset 207


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

Refactor taskRecurringSchedule update() to a service method.

Location:
trunk/grails-app
Files:
3 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 = {
  • trunk/grails-app/i18n/messages.properties

    r204 r207  
    3131task.modifications.failedToSave=Could not complete operation, as task modification record failed to save.
    3232tast.taskRecurringSchedule.alreadyExists=This task already has a recurring schedule.
     33
     34taskRecurringSchedule.notFound=Could not complete operation, recurring schedule not found.
    3335
    3436task.assignedPersons=Assigned Persons
  • trunk/grails-app/services/TaskRecurringScheduleService.groovy

    r203 r207  
    8181                return fail()
    8282
    83             // All went well if we get to here.
     83             // If we get here all went well.
    8484            return result
    8585
     
    8787    } // end create()
    8888
     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.validate()
     118
     119            Date originalDate = result.taskRecurringScheduleInstance.nextTargetStartDate
     120            result.taskRecurringScheduleInstance.properties = params
     121            Date newDate = result.taskRecurringScheduleInstance.nextTargetStartDate
     122
     123            // If user changes nextTargetStartDate then ensure it is in the future, otherwise it's ok to keep the original date.
     124            if(originalDate.getTime() != newDate.getTime())
     125            {
     126                if(newDate < dateUtilService.getToday())
     127                    return fail("nextTargetStartDate", "taskRecurring.nextTargetStartDate.NotInTheFuture")
     128            }
     129
     130            result.taskRecurringScheduleInstance.setNextGenerationDate()
     131            result.taskRecurringScheduleInstance.setNextTargetCompletionDate()
     132
     133            if(result.taskRecurringScheduleInstance.hasErrors() || !result.taskRecurringScheduleInstance.save())
     134                return fail()
     135
     136            // If we get here all went well.
     137            return result
     138
     139        } //end withTransaction
     140    }  // end update()
     141
    89142} // end of class
Note: See TracChangeset for help on using the changeset viewer.