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

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

Add optimistic locking checks and transactional save for TaskProcedure.

File size: 7.0 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 taskProcedureService
8
9    def index = { redirect(action:list,params:params) }
10
11    // the delete, save and update actions only accept POST requests
12    static allowedMethods = [delete:'POST', save:'POST', update:'POST']
13
14    @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_TaskManager', 'ROLE_TaskUser'])
15    def list = {
16        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
17
18        if(!params.filter)
19        { return [taskProcedureInstanceList: TaskProcedure.list(params), taskProcedureInstanceTotal: TaskProcedure.count()] }
20
21        // filterPane:
22        return[ taskProcedureInstanceList: filterService.filter( params, TaskProcedure ),
23            taskProcedureInstanceTotal: filterService.count( params, TaskProcedure ),
24            filterParams: com.zeddware.grails.plugins.filterpane.FilterUtils.extractFilterParams(params),
25            params:params ]
26    }
27
28    @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_TaskManager', 'ROLE_TaskUser'])
29    def search = {
30        redirect(action:list)
31    }
32
33    @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_TaskManager', 'ROLE_TaskUser'])
34    def show = {
35
36        // In the case of an actionSubmit button, rewrite action name from 'index'.
37        if(params._action_Show)
38            params.action='show'
39
40        def taskProcedureInstance = TaskProcedure.get( params.id )
41
42        if(!taskProcedureInstance) {
43            flash.errorMessage = "TaskProcedure not found with id ${params.id}"
44            redirect(controller:'taskDetailed', action:'search')
45            return
46        }
47
48        redirect(controller:'taskDetailed',
49                        action:'show',
50                        id:taskProcedureInstance.linkedTask?.id,
51                        params:[showTab:"showProcedureTab"])
52    }
53
54    def delete = {
55        def taskProcedureInstance = TaskProcedure.get( params.id )
56        if(taskProcedureInstance) {
57            def taskInstance = taskProcedureInstance.linkedTask
58            try {
59                taskProcedureInstance.tasks.each {
60                    it.taskProcedure = null
61                }
62                taskProcedureInstance.delete(flush:true)
63                flash.message = "TaskProcedure ${params.id} deleted"
64                redirect(controller:'taskDetailed',
65                                action:'show',
66                                id:taskInstance.id,
67                                params:[showTab:"showProcedureTab"])
68            }
69            catch(org.springframework.dao.DataIntegrityViolationException e) {
70                flash.errorMessage = "TaskProcedure ${params.id} could not be deleted"
71                redirect(controller:'taskDetailed',
72                                action:'show',
73                                id:taskInstance.id,
74                                params:[showTab:"showProcedureTab"])
75            }
76        }
77        else {
78            flash.errorMessage = "TaskProcedure not found with id ${params.id}"
79            redirect(action:list)
80        }
81    }
82
83    def edit = {
84
85        // In the case of an actionSubmit button, rewrite action name from 'index'.
86        if(params._action_Edit)
87            params.action='edit'
88
89        def taskProcedureInstance = TaskProcedure.get( params.id )
90
91        if(!taskProcedureInstance) {
92            flash.errorMessage = "TaskProcedure not found with id ${params.id}"
93            redirect(action:list)
94        }
95        else {
96            return [ taskProcedureInstance : taskProcedureInstance ]
97        }
98    }
99
100    def update = {
101        def result = taskProcedureService.update(params)
102
103        if(!result.error) {
104            flash.message = g.message(code: "default.update.success", args: ["TaskProcedure", params.id])
105            redirect(controller:'taskDetailed',
106                            action:'show',
107                            id:result.taskProcedureInstance.linkedTask.id,
108                            params:[showTab:"showProcedureTab"])
109            return
110        }
111
112        if(result.error.code == "default.not.found") {
113            flash.errorMessage = g.message(code: result.error.code, args: result.error.args)
114            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"])
124            return
125        }
126
127        render(view:'edit', model:[taskProcedureInstance: result.taskProcedureInstance])
128    }
129
130    def create = {
131
132        if(!params.taskInstance?.id) {
133            flash.errorMessage = "Please select or create a task, then go to the Procedure tab."
134            redirect(controller:"taskDetailed", action:"search")
135            return
136        }
137
138        params.linkedTask = Task.get(params.taskInstance.id)
139
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.
151        if(!params.linkedTask?.primaryAsset) {
152            flash.errorMessage = "Please set a Primary Asset first, then go to the Procedure tab."
153            redirect(controller:"taskDetailed", action:"show", id:params.linkedTask?.id)
154            return
155        }
156
157        def taskProcedureInstance = new TaskProcedure()
158        taskProcedureInstance.properties = params
159        return ['taskProcedureInstance':taskProcedureInstance]
160    }
161
162    def save = {
163        def result = taskProcedureService.save(params)
164
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
172        }
173
174        if(result.error.code == "default.optimistic.locking.failure") {
175            flash.errorMessage = g.message(code: result.error.code, args: result.error.args)
176            redirect(controller:'taskDetailed',
177                            action:'show',
178                            id:result.taskProcedureInstance.linkedTask.id,
179                            params:[showTab:"showProcedureTab"])
180            return
181        }
182
183        render(view:'create', model:[taskProcedureInstance: result.taskProcedureInstance])
184    }
185
186}
Note: See TracBrowser for help on using the repository browser.