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

Refactor taskService update() and enable optimistic version checking.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.