Changeset 775


Ignore:
Timestamp:
Feb 3, 2011, 5:12:40 AM (14 years ago)
Author:
gav
Message:

Add optimistic locking checks and transactional save for TaskProcedure.

Location:
branches/features/taskProcedureRework/grails-app
Files:
2 edited

Legend:

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

    r774 r775  
    55
    66    def filterService
    7     def authService
    87    def taskProcedureService
    98
     
    6968            }
    7069            catch(org.springframework.dao.DataIntegrityViolationException e) {
    71                 flash.message = "TaskProcedure ${params.id} could not be deleted"
     70                flash.errorMessage = "TaskProcedure ${params.id} could not be deleted"
    7271                redirect(controller:'taskDetailed',
    7372                                action:'show',
     
    7776        }
    7877        else {
    79             flash.message = "TaskProcedure not found with id ${params.id}"
     78            flash.errorMessage = "TaskProcedure not found with id ${params.id}"
    8079            redirect(action:list)
    8180        }
     
    9190
    9291        if(!taskProcedureInstance) {
    93             flash.message = "TaskProcedure not found with id ${params.id}"
     92            flash.errorMessage = "TaskProcedure not found with id ${params.id}"
    9493            redirect(action:list)
    9594        }
     
    112111
    113112        if(result.error.code == "default.not.found") {
    114             flash.message = g.message(code: result.error.code, args: result.error.args)
     113            flash.errorMessage = g.message(code: result.error.code, args: result.error.args)
    115114            redirect(action:list)
     115            return
     116        }
     117
     118        if(result.error.code == "default.optimistic.locking.failure") {
     119            flash.errorMessage = g.message(code: result.error.code, args: result.error.args)
     120            redirect(controller:'taskDetailed',
     121                            action:'show',
     122                            id:result.taskProcedureInstance.linkedTask.id,
     123                            params:[showTab:"showProcedureTab"])
    116124            return
    117125        }
     
    130138        params.linkedTask = Task.get(params.taskInstance.id)
    131139
     140        // Task already has a taskProcedure.
     141        if(params.linkedTask.taskProcedure) {
     142            flash.errorMessage = g.message(code: 'default.optimistic.locking.failure')
     143            redirect(controller:'taskDetailed',
     144                            action:'show',
     145                            id:params.linkedTask.id,
     146                            params:[showTab:"showProcedureTab"])
     147            return
     148        }
     149
     150        // Task does not have a primaryAsset.
    132151        if(!params.linkedTask?.primaryAsset) {
    133152            flash.errorMessage = "Please set a Primary Asset first, then go to the Procedure tab."
     
    142161
    143162    def save = {
    144         def taskProcedureInstance = new TaskProcedure(params)
    145         taskProcedureInstance.createdBy = authService.currentUser
    146         taskProcedureInstance.lastUpdatedBy = authService.currentUser
    147         def taskInstance = Task.get(params.linkedTask.id)
     163        def result = taskProcedureService.save(params)
    148164
    149         // Gaps in the html index's can be created by adding 2 items and removing the first one.
    150         // This creates a gap at the missing index where LazyList will return a null.
    151         def nullMaintenanceActions = taskProcedureInstance.maintenanceActions.findAll {!it}
    152         if (nullMaintenanceActions) {
    153             taskProcedureInstance.maintenanceActions.removeAll(nullMaintenanceActions)
     165        if(!result.error) {
     166            flash.message = g.message(code: "default.create.success", args: ["TaskProcedure", result.taskProcedureInstance.id])
     167            redirect(controller:'taskDetailed',
     168                            action:'show',
     169                            id:result.taskProcedureInstance.linkedTask.id,
     170                            params:[showTab:"showProcedureTab"])
     171            return
    154172        }
    155173
    156         if(!taskProcedureInstance.hasErrors() && taskProcedureInstance.save(flush: true)) {
    157              // Also sets: taskInstance.taskProcedure = taskProcedureInstance
    158             taskProcedureInstance.addToTasks(taskInstance)
    159             flash.message = "TaskProcedure ${taskProcedureInstance.id} created."
     174        if(result.error.code == "default.optimistic.locking.failure") {
     175            flash.errorMessage = g.message(code: result.error.code, args: result.error.args)
    160176            redirect(controller:'taskDetailed',
    161177                            action:'show',
    162                             id:taskProcedureInstance.linkedTask.id,
     178                            id:result.taskProcedureInstance.linkedTask.id,
    163179                            params:[showTab:"showProcedureTab"])
     180            return
    164181        }
    165         else {
    166             // Populate maintenanceAction errors for display.
    167             taskProcedureInstance.maintenanceActions.each { it.validate() }
    168             render(view:'create',model:[taskProcedureInstance:taskProcedureInstance])
    169         }
     182
     183        render(view:'create', model:[taskProcedureInstance: result.taskProcedureInstance])
    170184    }
    171185
  • branches/features/taskProcedureRework/grails-app/services/TaskProcedureService.groovy

    r774 r775  
    2222                    result.taskProcedureInstance.errors.rejectValue(m.field, m.code)
    2323                result.error = [ code: m.code, args: ["TaskProcedure", params.id] ]
     24                // Fetch to prevent lazy initialization error.
     25                result.taskProcedureInstance?.linkedTask.primaryAsset
     26                result.taskProcedureInstance?.createdBy
    2427                return result
    2528            }
     
    6366                // Populate maintenanceAction errors for display.
    6467                result.taskProcedureInstance.maintenanceActions.each { it.validate() }
    65                 // Fetch to prevent lazy initialization error.
    66                 result.taskProcedureInstance.linkedTask.primaryAsset
    67                 result.taskProcedureInstance.createdBy
    6868                return fail(code:"default.update.failure")
    6969            }
     
    7575    }  // end update()
    7676
     77    /**
     78    * Creates a new taskProcedure with the given params.
     79    * @param params The params to use when creating the new taskProcedure.
     80    * @returns A map containing result.error (if any error) and result.taskProcedure.
     81    */
     82    def save(params) {
     83        def result = [:]
     84        TaskProcedure.withTransaction { status ->
     85            def fail = { Map m ->
     86                status.setRollbackOnly()
     87                if(result.taskProcedureInstance && m.field)
     88                    result.taskProcedureInstance.errors.rejectValue(m.field, m.code)
     89                result.error = [ code: m.code, args: ["TaskProcedure", params.id] ]
     90                // Fetch to prevent lazy initialization error.
     91                result.taskProcedureInstance.linkedTask.primaryAsset
     92                return result
     93            }
     94
     95            result.taskProcedureInstance = new TaskProcedure(params)
     96
     97            // Optimistic locking check on linkedTask.
     98            if(result.taskProcedureInstance.linkedTask.taskProcedure)
     99                    return fail(field:"version", code:"default.optimistic.locking.failure")
     100
     101            result.taskProcedureInstance.createdBy = authService.currentUser
     102            result.taskProcedureInstance.lastUpdatedBy = authService.currentUser
     103
     104            // Gaps in the html index's can be created by adding 2 items and removing the first one.
     105            // This creates a gap at the missing index where LazyList will return a null.
     106            def nullMaintenanceActions = result.taskProcedureInstance.maintenanceActions.findAll {!it}
     107            if (nullMaintenanceActions) {
     108                result.taskProcedureInstance.maintenanceActions.removeAll(nullMaintenanceActions)
     109            }
     110
     111            // Also sets: taskInstance.taskProcedure = taskProcedureInstance
     112            result.taskProcedureInstance.addToTasks(result.taskProcedureInstance.linkedTask)
     113
     114            if(result.taskProcedureInstance.hasErrors() || !result.taskProcedureInstance.save()) {
     115                // Populate maintenanceAction errors for display.
     116                result.taskProcedureInstance.maintenanceActions.each { it.validate() }
     117                return fail(code:"default.create.failure")
     118            }
     119
     120        } //end withTransaction
     121
     122        result.taskProcedureInstance.lastUpdated = new Date() // Required to trigger version increment to 1.
     123
     124        // success
     125        return result
     126    }  // end save()
     127
    77128} // end class
Note: See TracChangeset for help on using the changeset viewer.