1 | import org.codehaus.groovy.grails.plugins.springsecurity.Secured |
---|
2 | |
---|
3 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_TaskManager']) |
---|
4 | class 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 | 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.errorMessage = "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.errorMessage = "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.errorMessage = "TaskProcedure not found with id ${params.id}" |
---|
94 | redirect(action:list) |
---|
95 | } |
---|
96 | else { |
---|
97 | def assemblies = assetService.getAssemblies(taskProcedureInstance.linkedTask.primaryAsset) |
---|
98 | return [ taskProcedureInstance : taskProcedureInstance, |
---|
99 | assemblies: assemblies ] |
---|
100 | } |
---|
101 | } |
---|
102 | |
---|
103 | def update = { |
---|
104 | def result = taskProcedureService.update(params) |
---|
105 | |
---|
106 | if(!result.error) { |
---|
107 | flash.message = g.message(code: "default.update.success", args: ["TaskProcedure", params.id]) |
---|
108 | redirect(controller:'taskDetailed', |
---|
109 | action:'show', |
---|
110 | id:result.taskProcedureInstance.linkedTask.id, |
---|
111 | params:[showTab:"showProcedureTab"]) |
---|
112 | return |
---|
113 | } |
---|
114 | |
---|
115 | if(result.error.code == "default.not.found") { |
---|
116 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
117 | redirect(action:list) |
---|
118 | return |
---|
119 | } |
---|
120 | |
---|
121 | if(result.error.code == "default.optimistic.locking.failure") { |
---|
122 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
123 | redirect(controller:'taskDetailed', |
---|
124 | action:'show', |
---|
125 | id:result.taskProcedureInstance.linkedTask.id, |
---|
126 | params:[showTab:"showProcedureTab"]) |
---|
127 | return |
---|
128 | } |
---|
129 | |
---|
130 | def assemblies = assetService.getAssemblies(result.taskProcedureInstance.linkedTask.primaryAsset) |
---|
131 | render(view:'edit', model:[taskProcedureInstance: result.taskProcedureInstance, |
---|
132 | assemblies: assemblies]) |
---|
133 | } |
---|
134 | |
---|
135 | def create = { |
---|
136 | |
---|
137 | if(!params.taskInstance?.id) { |
---|
138 | flash.errorMessage = "Please select or create a task, then go to the Procedure tab." |
---|
139 | redirect(controller:"taskDetailed", action:"search") |
---|
140 | return |
---|
141 | } |
---|
142 | |
---|
143 | params.linkedTask = Task.get(params.taskInstance.id) |
---|
144 | |
---|
145 | // Task already has a taskProcedure. |
---|
146 | if(params.linkedTask.taskProcedure) { |
---|
147 | flash.errorMessage = g.message(code: 'default.optimistic.locking.failure') |
---|
148 | redirect(controller:'taskDetailed', |
---|
149 | action:'show', |
---|
150 | id:params.linkedTask.id, |
---|
151 | params:[showTab:"showProcedureTab"]) |
---|
152 | return |
---|
153 | } |
---|
154 | |
---|
155 | // Task does not have a primaryAsset. |
---|
156 | if(!params.linkedTask?.primaryAsset) { |
---|
157 | flash.errorMessage = "Please set a Primary Asset first, then go to the Procedure tab." |
---|
158 | redirect(controller:"taskDetailed", action:"show", id:params.linkedTask?.id) |
---|
159 | return |
---|
160 | } |
---|
161 | |
---|
162 | def taskProcedureInstance = new TaskProcedure() |
---|
163 | taskProcedureInstance.properties = params |
---|
164 | |
---|
165 | // Populate a maintenanceAction for each assembly. |
---|
166 | def assemblies = assetService.getAssemblies(params.linkedTask.primaryAsset) |
---|
167 | assemblies.eachWithIndex { assembly, i -> |
---|
168 | def ma = taskProcedureInstance.maintenanceActionLazyList.get(i) |
---|
169 | ma.assetSubItem = assembly |
---|
170 | ma.procedureStepNumber = (i+1)*10 |
---|
171 | } |
---|
172 | |
---|
173 | return ['taskProcedureInstance':taskProcedureInstance, 'assemblies': assemblies] |
---|
174 | } |
---|
175 | |
---|
176 | def save = { |
---|
177 | def result = taskProcedureService.save(params) |
---|
178 | |
---|
179 | if(!result.error) { |
---|
180 | flash.message = g.message(code: "default.create.success", args: ["TaskProcedure", result.taskProcedureInstance.id]) |
---|
181 | redirect(controller:'taskDetailed', |
---|
182 | action:'show', |
---|
183 | id:result.taskProcedureInstance.linkedTask.id, |
---|
184 | params:[showTab:"showProcedureTab"]) |
---|
185 | return |
---|
186 | } |
---|
187 | |
---|
188 | if(result.error.code == "default.optimistic.locking.failure") { |
---|
189 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
190 | redirect(controller:'taskDetailed', |
---|
191 | action:'show', |
---|
192 | id:result.taskProcedureInstance.linkedTask.id, |
---|
193 | params:[showTab:"showProcedureTab"]) |
---|
194 | return |
---|
195 | } |
---|
196 | |
---|
197 | def assemblies = assetService.getAssemblies(result.taskProcedureInstance.linkedTask.primaryAsset) |
---|
198 | render(view:'create', model:[taskProcedureInstance: result.taskProcedureInstance, |
---|
199 | assemblies: assemblies]) |
---|
200 | } |
---|
201 | |
---|
202 | } |
---|