Changeset 204


Ignore:
Timestamp:
Dec 1, 2009, 1:48:20 AM (14 years ago)
Author:
gav
Message:

Refactor taskService update() and enable optimistic version checking.

Location:
trunk/grails-app
Files:
4 edited

Legend:

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

    r196 r204  
    408408        }
    409409        else {
    410             if(result.taskInstance) {
    411                 render(view:'edit',model:[taskInstance:result.taskInstance])
    412             }
    413             else {
    414                 flash.message = "Task could not be updated."
    415                 redirect(action: 'search')
    416             }
    417         }
    418 
    419     }
    420 
    421 //     def update = {
    422 //         def taskInstance = Task.get( params.id )
    423 //         if(taskInstance) {
    424 //             if(params.version) {
    425 //                 def version = params.version.toLong()
    426 //                 if(taskInstance.version > version) {
    427 //
    428 //                     taskInstance.errors.rejectValue("version", "task.optimistic.locking.failure", "Another user has updated this Task while you were editing.")
    429 //                     render(view:'edit',model:[taskInstance:taskInstance])
    430 //                     return
    431 //                 }
    432 //             }
    433 //             taskInstance.properties = params
    434 //             if(!taskInstance.hasErrors() && taskInstance.save(flush: true)) {
    435 //                 flash.message = "Task ${params.id} updated"
    436 //                 redirect(action:show,id:taskInstance.id)
    437 //             }
    438 //             else {
    439 //                 render(view:'edit',model:[taskInstance:taskInstance])
    440 //             }
    441 //         }
    442 //         else {
    443 //             flash.message = "Task not found with id ${params.id}"
    444 //             redirect(action:edit,id:params.id)
    445 //         }
    446 //     }
     410            render(view:'edit',model:[taskInstance:result.taskInstance.refresh()])
     411        }
     412
     413    }
    447414
    448415    def create = {
  • trunk/grails-app/i18n/messages.properties

    r203 r204  
    7676default.paginate.prev=Previous
    7777default.paginate.next=Next
     78default.optimistic.locking.failure=Another user has updated this item while you were editing.
    7879
    7980# Rich UI plugin - Calendar
  • trunk/grails-app/services/TaskService.groovy

    r203 r204  
    172172                }
    173173
    174                 // All went well if we get to here.
     174                // If we get here all went well.
    175175                return result
    176176            }
     
    186186    * Updates an existing task.
    187187    * @param params The params to update for task with id of params.id.
    188     * @returns A map containing result.error=true (if any error) and result.taskInstance.
     188    * @returns A map containing result.error=true (if any error) and result.taskInstance (if available).
    189189    */
    190190    def update(params) {
    191191        Task.withTransaction { status ->
    192192            def result = [:]
    193             result.taskInstance = Task.get(params.id)
    194             if(result.taskInstance) {
    195 
    196                 // Optimistic locking check.
    197                 if(params.version) {
    198                     def version = params.version.toLong()
    199                     if(result.taskInstance.version > version) {
    200                         status.setRollbackOnly()
    201                         result.taskInstance.errors.rejectValue("version", "task.optimistic.locking.failure", "Another user has updated this Task while you were editing.")
    202                         result.error = true
    203                         return result
    204                     }
    205                 }
    206 
    207                 result.taskInstance.properties = params
    208 
    209                 if(result.taskInstance.save()) {
    210                     def taskModification = new TaskModification(person:personService.currentUser(),
    211                                                             taskModificationType: TaskModificationType.get(3),
    212                                                             task: result.taskInstance)
    213                     if(taskModification.save()) {
    214                         // All went well.
    215                         return result
    216                     }
    217                     else {
    218                         status.setRollbackOnly()
    219                         result.taskInstance.errors.rejectValue("taskModifications", "task.modifications.failedToSave")
    220                         result.error = true
    221                         return result
    222                     }
    223                 }
    224             }
    225             // Something failed.
    226             status.setRollbackOnly()
    227             result.error = true
     193
     194            def fail = { Object[] args ->
     195                status.setRollbackOnly()
     196                if(args.size() == 2) result.taskInstance.errors.rejectValue(args[0], args[1])
     197                result.error = true
     198                return result
     199            }
     200
     201            result.taskInstance = Task.get(params.id)
     202
     203            if(!result.taskInstance)
     204                return fail('task', "task.notFound")
     205
     206            // Optimistic locking check.
     207            if(params.version) {
     208                def version = params.version.toLong()
     209                if(result.taskInstance.version > version)
     210                    return fail("version", "default.optimistic.locking.failure")
     211            }
     212
     213            result.taskInstance.properties = params
     214
     215            if(result.taskInstance.hasErrors() || !result.taskInstance.save())
     216                return fail()
     217
     218            def taskModification = new TaskModification(person:personService.currentUser(),
     219                                                    taskModificationType: TaskModificationType.get(3),
     220                                                    task: result.taskInstance)
     221
     222            if(!taskModification.save())
     223                return fail("taskModifications", "task.modifications.failedToSave")
     224
     225            // If we get here all went well.
    228226            return result
    229227
  • trunk/grails-app/views/taskDetailed/edit.gsp

    r196 r204  
    2424            <g:form method="post" >
    2525                <input type="hidden" name="id" value="${taskInstance?.id}" />
     26                <input type="hidden" name="version" value="${taskInstance?.version}" />
    2627                <div class="dialog">
    2728                    <table>
Note: See TracChangeset for help on using the changeset viewer.