source: trunk/grails-app/controllers/TaskProcedureDetailedController.groovy @ 954

Last change on this file since 954 was 913, checked in by gav, 14 years ago

Svn merge -r875:r911 branches/features/grailsUpgrade/ into trunk/.

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 assetService
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        def taskProcedureInstanceList
20        def taskProcedureInstanceTotal
21        def filterParams = com.zeddware.grails.plugins.filterpane.FilterUtils.extractFilterParams(params)
22
23        if(params.filter) {
24            taskProcedureInstanceList = filterService.filter( params, TaskProcedure )
25            taskProcedureInstanceTotal = filterService.count( params, TaskProcedure )
26        }
27        else {
28            taskProcedureInstanceList = TaskProcedure.list(params)
29            taskProcedureInstanceTotal = TaskProcedure.count()
30        }
31
32        // filterPane:
33        return[ taskProcedureInstanceList: taskProcedureInstanceList,
34            taskProcedureInstanceTotal: taskProcedureInstanceTotal,
35            filterParams: filterParams,
36            params: params ]
37    }
38
39    @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_TaskManager', 'ROLE_TaskUser'])
40    def search = {
41        redirect(action:list)
42    }
43
44    @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_TaskManager', 'ROLE_TaskUser'])
45    def show = {
46
47        // In the case of an actionSubmit button, rewrite action name from 'index'.
48        if(params._action_Show)
49            params.action='show'
50
51        def taskProcedureInstance = TaskProcedure.get( params.id )
52
53        if(!taskProcedureInstance) {
54            flash.errorMessage = "TaskProcedure not found with id ${params.id}"
55            redirect(action:'list')
56            return
57        }
58
59        def taskProcedureRevision
60        if(params.revision?.toInteger())
61             taskProcedureRevision = taskProcedureInstance.getRevision( params.revision )
62        else
63             taskProcedureRevision = taskProcedureInstance.latestRevision
64
65        if(!taskProcedureRevision) {
66            flash.errorMessage = "TaskProcedure ${params.id} revision ${params.revision} not found"
67            redirect(action:'list')
68            return
69        }
70
71        return [taskProcedureRevision: taskProcedureRevision]
72
73    }
74
75    def edit = {
76
77        // In the case of an actionSubmit button, rewrite action name from 'index'.
78        //if(params._action_Edit)
79            params.action='edit'
80
81        def taskProcedureInstance = TaskProcedure.get( params.id )
82
83        if(!taskProcedureInstance) {
84            flash.errorMessage = "TaskProcedure not found with id ${params.id}"
85            redirect(action:list)
86        }
87        else {
88            def assemblies = assetService.getAssemblies(taskProcedureInstance.linkedTask.primaryAsset)
89            return [ taskProcedureInstance : taskProcedureInstance,
90                            assemblies: assemblies ]
91        }
92    }
93
94    def update = {
95        def result = taskProcedureService.update(params)
96
97        if(!result.error) {
98            flash.message = g.message(code: "default.update.success", args: ["TaskProcedure", params.id])
99            redirect(action:'show', id:result.taskProcedureInstance.id)
100            return
101        }
102
103        if(result.error.code == "default.not.found") {
104            flash.errorMessage = g.message(code: result.error.code, args: result.error.args)
105            redirect(action:list)
106            return
107        }
108
109        if(result.error.code == "default.optimistic.locking.failure") {
110            flash.errorMessage = g.message(code: result.error.code, args: result.error.args)
111            redirect(action:'show', id:result.taskProcedureInstance.id)
112            return
113        }
114
115        def assemblies = assetService.getAssemblies(result.taskProcedureInstance.linkedTask.primaryAsset)
116        render(view:'edit', model:[taskProcedureInstance: result.taskProcedureInstance,
117                                                assemblies: assemblies])
118    }
119
120    def create = {
121
122        if(!params.taskInstance?.id) {
123            flash.errorMessage = "Please select or create a task, then go to the Procedure tab."
124            redirect(controller:"taskDetailed", action:"search")
125            return
126        }
127
128        params.linkedTask = Task.get(params.taskInstance.id)
129
130        // Task already has a taskProcedure.
131        if(params.linkedTask.taskProcedureRevision) {
132            flash.errorMessage = g.message(code: 'default.optimistic.locking.failure')
133            redirect(controller:'taskDetailed',
134                            action:'show',
135                            id:params.linkedTask.id,
136                            params:[showTab:"showProcedureTab"])
137            return
138        }
139
140        // Task does not have a primaryAsset.
141        if(!params.linkedTask?.primaryAsset) {
142            flash.errorMessage = "Please set a Primary Asset first, then go to the Procedure tab."
143            redirect(controller:"taskDetailed", action:"show", id:params.linkedTask?.id)
144            return
145        }
146
147        def taskProcedureInstance = new TaskProcedure()
148        taskProcedureInstance.properties = params
149
150        // Populate a couple of maintenanceActions for the first assembly.
151        def assemblies = assetService.getAssemblies(params.linkedTask.primaryAsset)
152        if(assemblies) {
153            def assembly = assemblies[0]
154            def ma = taskProcedureInstance.maintenanceActionLazyList.get(0)
155            ma.assetSubItem = assembly
156            ma.procedureStepNumber = 10
157            ma = taskProcedureInstance.maintenanceActionLazyList.get(1)
158            ma.assetSubItem = assembly
159            ma.procedureStepNumber = 20
160        }
161
162        return ['taskProcedureInstance':taskProcedureInstance, 'assemblies': assemblies]
163    }
164
165    def save = {
166        def result = taskProcedureService.save(params)
167
168        if(!result.error) {
169            flash.message = g.message(code: "default.create.success", args: ["TaskProcedure", result.taskProcedureInstance.id])
170            redirect(action:'show', id:result.taskProcedureInstance.id)
171            return
172        }
173
174        // Task already has a taskProcedure.
175        if(result.error.code == "default.optimistic.locking.failure") {
176            flash.errorMessage = g.message(code: result.error.code, args: result.error.args)
177            redirect(controller:'taskDetailed',
178                            action:'show',
179                            id:params.linkedTask.id,
180                            params:[showTab:"showProcedureTab"])
181            return
182        }
183
184        def assemblies = assetService.getAssemblies(result.taskProcedureInstance.linkedTask.primaryAsset)
185        render(view:'create', model:[taskProcedureInstance: result.taskProcedureInstance,
186                                                    assemblies: assemblies])
187    }
188
189}
Note: See TracBrowser for help on using the repository browser.