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
Line 
1import org.codehaus.groovy.grails.plugins.springsecurity.Secured
2
3@Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_TaskManager'])
4class TaskProcedureDetailedController extends BaseController {
5
6    def filterService
7    def authService
8    def taskProcedureService
9
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
15    @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_TaskManager', 'ROLE_TaskUser'])
16    def list = {
17        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
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 ]
27    }
28
29    @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_TaskManager', 'ROLE_TaskUser'])
30    def search = {
31        redirect(action:list)
32    }
33
34    @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_TaskManager', 'ROLE_TaskUser'])
35    def show = {
36
37        // In the case of an actionSubmit button, rewrite action name from 'index'.
38        if(params._action_Show)
39            params.action='show'
40
41        def taskProcedureInstance = TaskProcedure.get( params.id )
42
43        if(!taskProcedureInstance) {
44            flash.errorMessage = "TaskProcedure not found with id ${params.id}"
45            redirect(controller:'taskDetailed', action:'search')
46            return
47        }
48
49        redirect(controller:'taskDetailed',
50                        action:'show',
51                        id:taskProcedureInstance.linkedTask?.id,
52                        params:[showTab:"showProcedureTab"])
53    }
54
55    def delete = {
56        def taskProcedureInstance = TaskProcedure.get( params.id )
57        if(taskProcedureInstance) {
58            def taskInstance = taskProcedureInstance.linkedTask
59            try {
60                taskProcedureInstance.tasks.each {
61                    it.taskProcedure = null
62                }
63                taskProcedureInstance.delete(flush:true)
64                flash.message = "TaskProcedure ${params.id} deleted"
65                redirect(controller:'taskDetailed',
66                                action:'show',
67                                id:taskInstance.id,
68                                params:[showTab:"showProcedureTab"])
69            }
70            catch(org.springframework.dao.DataIntegrityViolationException e) {
71                flash.message = "TaskProcedure ${params.id} could not be deleted"
72                redirect(controller:'taskDetailed',
73                                action:'show',
74                                id:taskInstance.id,
75                                params:[showTab:"showProcedureTab"])
76            }
77        }
78        else {
79            flash.message = "TaskProcedure not found with id ${params.id}"
80            redirect(action:list)
81        }
82    }
83
84    def edit = {
85
86        // In the case of an actionSubmit button, rewrite action name from 'index'.
87        if(params._action_Edit)
88            params.action='edit'
89
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 = {
102        def result = taskProcedureService.update(params)
103
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        }
112
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        }
118
119        render(view:'edit', model:[taskProcedureInstance: result.taskProcedureInstance])
120    }
121
122    def create = {
123
124        if(!params.taskInstance?.id) {
125            flash.errorMessage = "Please select or create a task, then go to the Procedure tab."
126            redirect(controller:"taskDetailed", action:"search")
127            return
128        }
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]
141    }
142
143    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)
148
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
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."
160            redirect(controller:'taskDetailed',
161                            action:'show',
162                            id:taskProcedureInstance.linkedTask.id,
163                            params:[showTab:"showProcedureTab"])
164        }
165        else {
166            // Populate maintenanceAction errors for display.
167            taskProcedureInstance.maintenanceActions.each { it.validate() }
168            render(view:'create',model:[taskProcedureInstance:taskProcedureInstance])
169        }
170    }
171
172}
Note: See TracBrowser for help on using the repository browser.