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

Add optimistic locking checks and transactional save for TaskProcedure.

File:
1 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
Note: See TracChangeset for help on using the changeset viewer.