Ignore:
Timestamp:
Feb 3, 2011, 2:50:45 AM (14 years ago)
Author:
gav
Message:

Use transactional service for TaskProcedure update to prevent maintenanceActions leaking into database when procedureStepNumber fails validation.

Location:
branches/features/taskProcedureRework/grails-app
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • branches/features/taskProcedureRework/grails-app/controllers/TaskProcedureDetailedController.groovy

    r772 r774  
    66    def filterService
    77    def authService
     8    def taskProcedureService
    89
    910    def index = { redirect(action:list,params:params) }
     
    99100
    100101    def update = {
    101         def taskProcedureInstance = TaskProcedure.get( params.id )
    102         if(taskProcedureInstance) {
    103             if(params.version) {
    104                 def version = params.version.toLong()
    105                 if(taskProcedureInstance.version > version) {
     102        def result = taskProcedureService.update(params)
    106103
    107                     taskProcedureInstance.errors.rejectValue("version", "default.optimistic.locking.failure")
    108                     render(view:'edit',model:[taskProcedureInstance:taskProcedureInstance])
    109                     return
    110                 }
    111             }
    112             taskProcedureInstance.properties = params
    113             taskProcedureInstance.lastUpdatedBy = authService.currentUser
    114             taskProcedureInstance.lastUpdated = new Date() // Required to trigger version increment.
     104        if(!result.error) {
     105            flash.message = g.message(code: "default.update.success", args: ["TaskProcedure", params.id])
     106            redirect(controller:'taskDetailed',
     107                            action:'show',
     108                            id:result.taskProcedureInstance.linkedTask.id,
     109                            params:[showTab:"showProcedureTab"])
     110            return
     111        }
    115112
    116             // Gaps in the html index's can be created by adding 2 items and removing the first one.
    117             // This creates a gap at the missing index where LazyList will return a null.
    118             def nullMaintenanceActions = taskProcedureInstance.maintenanceActions.findAll {!it}
    119             if (nullMaintenanceActions) {
    120                 taskProcedureInstance.maintenanceActions.removeAll(nullMaintenanceActions)
    121             }
     113        if(result.error.code == "default.not.found") {
     114            flash.message = g.message(code: result.error.code, args: result.error.args)
     115            redirect(action:list)
     116            return
     117        }
    122118
    123             // Save for restoration if validation fails.
    124             def savedMaintenanceActions = new ArrayList(taskProcedureInstance.maintenanceActions)
    125 
    126             // Remove toBeDeleted before validation.
    127             def toBeDeleted = taskProcedureInstance.maintenanceActions.findAll {it.toBeDeleted}
    128             if (toBeDeleted) {
    129                 taskProcedureInstance.maintenanceActions.removeAll(toBeDeleted)
    130             }
    131 
    132             if(!taskProcedureInstance.hasErrors() && taskProcedureInstance.save(flush: true)) {
    133                 flash.message = "TaskProcedure ${params.id} updated"
    134                 redirect(controller:'taskDetailed',
    135                                 action:'show',
    136                                 id:taskProcedureInstance.linkedTask.id,
    137                                 params:[showTab:"showProcedureTab"])
    138             }
    139             else {
    140                 // Restore the saved items, some of which contain toBeDeleted flags but
    141                 // have not been deleted yet since validation failed.
    142                 // The toBeDeleted items are hidden in the view.
    143                 taskProcedureInstance.maintenanceActions = savedMaintenanceActions
    144                 // Populate maintenanceAction errors for display.
    145                 taskProcedureInstance.maintenanceActions.each { it.validate() }
    146                 render(view:'edit',model:[taskProcedureInstance:taskProcedureInstance])
    147             }
    148         }
    149         else {
    150             flash.message = "TaskProcedure not found with id ${params.id}"
    151             redirect(action:list)
    152         }
     119        render(view:'edit', model:[taskProcedureInstance: result.taskProcedureInstance])
    153120    }
    154121
Note: See TracChangeset for help on using the changeset viewer.