source: branches/features/taskProcedureRework/grails-app/controllers/TaskProcedureDetailedController.groovy @ 774

Last change on this file since 774 was 774, checked in by gav, 13 years ago

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

File size: 6.6 KB
RevLine 
[133]1import org.codehaus.groovy.grails.plugins.springsecurity.Secured
2
[298]3@Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_TaskManager'])
[133]4class TaskProcedureDetailedController extends BaseController {
[147]5
[155]6    def filterService
[772]7    def authService
[774]8    def taskProcedureService
[155]9
[133]10    def index = { redirect(action:list,params:params) }
11
12    // the delete, save and update actions only accept POST requests
13    static allowedMethods = [delete:'POST', save:'POST', update:'POST']
14
[298]15    @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_TaskManager', 'ROLE_TaskUser'])
[133]16    def list = {
17        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
[147]18
19        if(!params.filter)
20        { return [taskProcedureInstanceList: TaskProcedure.list(params), taskProcedureInstanceTotal: TaskProcedure.count()] }
21
22        // filterPane:
23        return[ taskProcedureInstanceList: filterService.filter( params, TaskProcedure ),
24            taskProcedureInstanceTotal: filterService.count( params, TaskProcedure ),
25            filterParams: com.zeddware.grails.plugins.filterpane.FilterUtils.extractFilterParams(params),
26            params:params ]
[133]27    }
[147]28
[298]29    @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_TaskManager', 'ROLE_TaskUser'])
[134]30    def search = {
31        redirect(action:list)
32    }
[133]33
[298]34    @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_TaskManager', 'ROLE_TaskUser'])
[133]35    def show = {
[147]36
37        // In the case of an actionSubmit button, rewrite action name from 'index'.
38        if(params._action_Show)
[375]39            params.action='show'
[147]40
[133]41        def taskProcedureInstance = TaskProcedure.get( params.id )
42
43        if(!taskProcedureInstance) {
[770]44            flash.errorMessage = "TaskProcedure not found with id ${params.id}"
45            redirect(controller:'taskDetailed', action:'search')
46            return
[133]47        }
[770]48
49        redirect(controller:'taskDetailed',
50                        action:'show',
51                        id:taskProcedureInstance.linkedTask?.id,
52                        params:[showTab:"showProcedureTab"])
[133]53    }
54
55    def delete = {
56        def taskProcedureInstance = TaskProcedure.get( params.id )
57        if(taskProcedureInstance) {
[770]58            def taskInstance = taskProcedureInstance.linkedTask
[133]59            try {
[758]60                taskProcedureInstance.tasks.each {
61                    it.taskProcedure = null
62                }
[365]63                taskProcedureInstance.delete(flush:true)
[133]64                flash.message = "TaskProcedure ${params.id} deleted"
[770]65                redirect(controller:'taskDetailed',
66                                action:'show',
67                                id:taskInstance.id,
68                                params:[showTab:"showProcedureTab"])
[133]69            }
70            catch(org.springframework.dao.DataIntegrityViolationException e) {
71                flash.message = "TaskProcedure ${params.id} could not be deleted"
[770]72                redirect(controller:'taskDetailed',
73                                action:'show',
74                                id:taskInstance.id,
75                                params:[showTab:"showProcedureTab"])
[133]76            }
77        }
78        else {
79            flash.message = "TaskProcedure not found with id ${params.id}"
80            redirect(action:list)
81        }
82    }
83
84    def edit = {
[147]85
86        // In the case of an actionSubmit button, rewrite action name from 'index'.
87        if(params._action_Edit)
[375]88            params.action='edit'
[147]89
[133]90        def taskProcedureInstance = TaskProcedure.get( params.id )
91
92        if(!taskProcedureInstance) {
93            flash.message = "TaskProcedure not found with id ${params.id}"
94            redirect(action:list)
95        }
96        else {
97            return [ taskProcedureInstance : taskProcedureInstance ]
98        }
99    }
100
101    def update = {
[774]102        def result = taskProcedureService.update(params)
[147]103
[774]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        }
[760]112
[774]113        if(result.error.code == "default.not.found") {
114            flash.message = g.message(code: result.error.code, args: result.error.args)
[365]115            redirect(action:list)
[774]116            return
[133]117        }
[774]118
119        render(view:'edit', model:[taskProcedureInstance: result.taskProcedureInstance])
[133]120    }
121
122    def create = {
[147]123
[770]124        if(!params.taskInstance?.id) {
125            flash.errorMessage = "Please select or create a task, then go to the Procedure tab."
[155]126            redirect(controller:"taskDetailed", action:"search")
[770]127            return
[147]128        }
[770]129
130        params.linkedTask = Task.get(params.taskInstance.id)
131
132        if(!params.linkedTask?.primaryAsset) {
133            flash.errorMessage = "Please set a Primary Asset first, then go to the Procedure tab."
134            redirect(controller:"taskDetailed", action:"show", id:params.linkedTask?.id)
135            return
136        }
137
138        def taskProcedureInstance = new TaskProcedure()
139        taskProcedureInstance.properties = params
140        return ['taskProcedureInstance':taskProcedureInstance]
[133]141    }
142
143    def save = {
144        def taskProcedureInstance = new TaskProcedure(params)
[772]145        taskProcedureInstance.createdBy = authService.currentUser
146        taskProcedureInstance.lastUpdatedBy = authService.currentUser
[762]147        def taskInstance = Task.get(params.linkedTask.id)
[147]148
[770]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)
154        }
155
[178]156        if(!taskProcedureInstance.hasErrors() && taskProcedureInstance.save(flush: true)) {
[770]157             // Also sets: taskInstance.taskProcedure = taskProcedureInstance
158            taskProcedureInstance.addToTasks(taskInstance)
[147]159            flash.message = "TaskProcedure ${taskProcedureInstance.id} created."
[770]160            redirect(controller:'taskDetailed',
161                            action:'show',
162                            id:taskProcedureInstance.linkedTask.id,
163                            params:[showTab:"showProcedureTab"])
[133]164        }
165        else {
[770]166            // Populate maintenanceAction errors for display.
167            taskProcedureInstance.maintenanceActions.each { it.validate() }
168            render(view:'create',model:[taskProcedureInstance:taskProcedureInstance])
[133]169        }
170    }
[770]171
[133]172}
Note: See TracBrowser for help on using the repository browser.