1 | import org.codehaus.groovy.grails.plugins.springsecurity.Secured |
---|
2 | import org.codehaus.groovy.grails.commons.ConfigurationHolder |
---|
3 | import org.apache.commons.lang.WordUtils |
---|
4 | |
---|
5 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager']) |
---|
6 | class AssetDetailedController extends BaseController { |
---|
7 | |
---|
8 | def assetCsvService |
---|
9 | def filterService |
---|
10 | def exportService |
---|
11 | def assetService |
---|
12 | def assetTreeService |
---|
13 | def taskService |
---|
14 | |
---|
15 | // the delete, save and update actions only accept POST requests |
---|
16 | static allowedMethods = [delete:'POST', save:'POST', update:'POST', saveCopy:'POST'] |
---|
17 | |
---|
18 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager', 'ROLE_AssetUser']) |
---|
19 | def index = { redirect(action:search,params:params) } |
---|
20 | |
---|
21 | /** |
---|
22 | * Set session.assetSearchParamsMax |
---|
23 | */ |
---|
24 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager', 'ROLE_AssetUser']) |
---|
25 | def setSearchParamsMax = { |
---|
26 | def max = 1000 |
---|
27 | if(params.newMax?.isInteger()) { |
---|
28 | def i = params.newMax.toInteger() |
---|
29 | if(i > 0 && i <= max) |
---|
30 | session.assetSearchParamsMax = params.newMax |
---|
31 | if(i > max) |
---|
32 | session.assetSearchParamsMax = max |
---|
33 | } |
---|
34 | forward(action: 'search', params: params) |
---|
35 | } |
---|
36 | |
---|
37 | /** |
---|
38 | * Build and return the asset tree response for the AJAX request. |
---|
39 | */ |
---|
40 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager', 'ROLE_AssetUser']) |
---|
41 | def assetTree = { |
---|
42 | def s = assetTreeService.buildAssetTree(params, session) |
---|
43 | render s |
---|
44 | } |
---|
45 | |
---|
46 | /** |
---|
47 | * Build and return the compact asset tree response. |
---|
48 | */ |
---|
49 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager', 'ROLE_AssetUser']) |
---|
50 | def assetTreeCompact = { |
---|
51 | def assetInstance = Asset.read(params.id) |
---|
52 | if(!assetInstance) { |
---|
53 | render g.message(code: 'default.not.found', args: ['Asset',params.id]) |
---|
54 | return |
---|
55 | } |
---|
56 | |
---|
57 | render(template:"/shared/assetTreeCompact", model:['assetInstance':assetInstance]) |
---|
58 | } |
---|
59 | |
---|
60 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager', 'ROLE_AssetUser']) |
---|
61 | def exportAssetTreeHtml = { |
---|
62 | response.contentType = ConfigurationHolder.config.grails.mime.types["csv"] |
---|
63 | response.setHeader("Content-disposition", "attachment; filename=AssetTree.html") |
---|
64 | def s = assetTreeService.buildAssetTree(params, session) |
---|
65 | render s |
---|
66 | } |
---|
67 | |
---|
68 | /** |
---|
69 | * Save the asset tree status in the current http session. |
---|
70 | */ |
---|
71 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager', 'ROLE_AssetUser']) |
---|
72 | def saveAssetTreeStatus = { |
---|
73 | session.assetTreeVisibleBranches = params.assetTreeVisibleBranches |
---|
74 | } |
---|
75 | |
---|
76 | /** |
---|
77 | * Disaply the import view. |
---|
78 | */ |
---|
79 | def importAssetTree = { |
---|
80 | } |
---|
81 | |
---|
82 | /** |
---|
83 | * Handle the import save. |
---|
84 | */ |
---|
85 | def importAssetTreeSave = { |
---|
86 | def result = assetCsvService.importAssetTree(request) |
---|
87 | |
---|
88 | if(!result.error) { |
---|
89 | flash.message = g.message(code: "asset.tree.import.success") |
---|
90 | redirect(action:search) |
---|
91 | return |
---|
92 | } |
---|
93 | |
---|
94 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
95 | redirect(action: importAssetTree) |
---|
96 | } |
---|
97 | |
---|
98 | /** |
---|
99 | * Export a csv template. |
---|
100 | * NOTE: IE has a 'validating' bug in dev mode that causes the export to take a long time! |
---|
101 | * This does not appear to be a problem once deployed to Tomcat. |
---|
102 | */ |
---|
103 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager', 'ROLE_AssetUser']) |
---|
104 | def exportAssetTreeTemplate = { |
---|
105 | response.contentType = ConfigurationHolder.config.grails.mime.types["csv"] |
---|
106 | response.setHeader("Content-disposition", "attachment; filename=AssetTreeTemplate.csv") |
---|
107 | def s = assetCsvService.buildAssetTreeTemplate() |
---|
108 | render s |
---|
109 | } |
---|
110 | |
---|
111 | /** |
---|
112 | * Export a csv test file. |
---|
113 | */ |
---|
114 | def exportAssetTreeTest = { |
---|
115 | response.contentType = ConfigurationHolder.config.grails.mime.types["csv"] |
---|
116 | response.setHeader("Content-disposition", "attachment; filename=AssetTreeTestFile.csv") |
---|
117 | def s = assetCsvService.buildAssetTreeTest() |
---|
118 | render s |
---|
119 | } |
---|
120 | |
---|
121 | /** |
---|
122 | * Export the entire asset tree as a csv file. |
---|
123 | */ |
---|
124 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager', 'ROLE_AssetUser']) |
---|
125 | def exportAssetTree = { |
---|
126 | |
---|
127 | def assetList = Asset.list() |
---|
128 | |
---|
129 | response.contentType = ConfigurationHolder.config.grails.mime.types["csv"] |
---|
130 | response.setHeader("Content-disposition", "attachment; filename=AssetTree.csv") |
---|
131 | def s = assetCsvService.buildAssetTree(assetList) |
---|
132 | render s |
---|
133 | } |
---|
134 | |
---|
135 | /** |
---|
136 | * Search action. |
---|
137 | */ |
---|
138 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager', 'ROLE_AssetUser']) |
---|
139 | def search = { |
---|
140 | |
---|
141 | if(session.assetSearchParamsMax) |
---|
142 | params.max = session.assetSearchParamsMax |
---|
143 | |
---|
144 | params.max = Math.min( params.max ? params.max.toInteger() : 10, 1000) |
---|
145 | |
---|
146 | def assetInstanceList = [] |
---|
147 | def assetInstanceTotal |
---|
148 | def filterParams = [:] |
---|
149 | |
---|
150 | // Quick Search: |
---|
151 | if(!params.filter) { |
---|
152 | assetInstanceList = Asset.list( params ) |
---|
153 | assetInstanceTotal = Asset.count() |
---|
154 | filterParams.quickSearch = params.quickSearch |
---|
155 | } |
---|
156 | else { |
---|
157 | // filterPane: |
---|
158 | assetInstanceList = filterService.filter( params, Asset ) |
---|
159 | assetInstanceTotal = filterService.count( params, Asset ) |
---|
160 | filterParams = com.zeddware.grails.plugins.filterpane.FilterUtils.extractFilterParams(params) |
---|
161 | } |
---|
162 | |
---|
163 | // export plugin: |
---|
164 | if(params?.format && params.format != "html") { |
---|
165 | |
---|
166 | def dateFmt = { date -> |
---|
167 | formatDate(format: "EEE, dd-MMM-yyyy", date: date) |
---|
168 | } |
---|
169 | |
---|
170 | // def fmtAsset = { m -> |
---|
171 | // def r = '' |
---|
172 | // def assetInstance = Asset.findByName(m) |
---|
173 | // |
---|
174 | // r += assetInstance |
---|
175 | // r += ", " |
---|
176 | // |
---|
177 | // def lastSubAsset = assetInstance.subAssets.size() - 1 |
---|
178 | // assetInstance.subAssets.eachWithIndex() { obj, i -> |
---|
179 | // r += "\"" + obj + "\"" |
---|
180 | // if( i < lastSubAsset ) |
---|
181 | // r += ", " |
---|
182 | // } |
---|
183 | // return r |
---|
184 | // } |
---|
185 | |
---|
186 | // def fmtSubAsset = { m -> |
---|
187 | // def r = '' |
---|
188 | // m.each() { |
---|
189 | // def machine = Machine.findByName(it) |
---|
190 | // def assemblies = machine.assemblies |
---|
191 | // r += machine.name |
---|
192 | // r += " " |
---|
193 | // r += assemblies |
---|
194 | // r += " " |
---|
195 | // } |
---|
196 | // return r |
---|
197 | // } |
---|
198 | |
---|
199 | String title = "Asset List." |
---|
200 | |
---|
201 | response.contentType = ConfigurationHolder.config.grails.mime.types[params.format] |
---|
202 | response.setHeader("Content-disposition", "attachment; filename=Assets.${params.extension}") |
---|
203 | List fields = ["section.site", |
---|
204 | "section", |
---|
205 | "name", |
---|
206 | "description"] |
---|
207 | Map labels = ["section.site": "Site", |
---|
208 | "section": "Section", |
---|
209 | "name": "Asset", |
---|
210 | "description": "Description"] |
---|
211 | // Map labels |
---|
212 | // Map formatters = ["subAsset.name": fmtSubAsset] |
---|
213 | Map formatters = [:] |
---|
214 | Map parameters = [title: title, separator: ","] |
---|
215 | |
---|
216 | exportService.export(params.format, |
---|
217 | response.outputStream, |
---|
218 | assetInstanceList.sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }, |
---|
219 | fields, |
---|
220 | labels, |
---|
221 | formatters, |
---|
222 | parameters) |
---|
223 | } |
---|
224 | |
---|
225 | // Add some basic params to filterParams. |
---|
226 | filterParams.max = params.max |
---|
227 | filterParams.offset = params.offset?.toInteger() ?: 0 |
---|
228 | filterParams.sort = params.sort ?: "id" |
---|
229 | filterParams.order = params.order ?: "desc" |
---|
230 | |
---|
231 | return[ assetInstanceList: assetInstanceList, |
---|
232 | assetInstanceTotal: assetInstanceTotal, |
---|
233 | filterParams: filterParams ] |
---|
234 | |
---|
235 | } // end search() |
---|
236 | |
---|
237 | /** |
---|
238 | * Show action. |
---|
239 | */ |
---|
240 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager', 'ROLE_AssetUser']) |
---|
241 | def show = { |
---|
242 | |
---|
243 | // In the case of an actionSubmit button, rewrite action name from 'index'. |
---|
244 | if(params._action_Show) |
---|
245 | params.action='show' |
---|
246 | |
---|
247 | def assetInstance = Asset.get( params.id ) |
---|
248 | |
---|
249 | if(!assetInstance) { |
---|
250 | flash.message = "Asset not found with id ${params.id}" |
---|
251 | redirect(action:search) |
---|
252 | return |
---|
253 | } |
---|
254 | |
---|
255 | return [ assetInstance : assetInstance, |
---|
256 | parentPMs: taskService.getParentPMs(assetInstance)] |
---|
257 | } |
---|
258 | |
---|
259 | /** |
---|
260 | * Delete action. |
---|
261 | */ |
---|
262 | def delete = { |
---|
263 | def result = assetService.delete(params) |
---|
264 | |
---|
265 | if(!result.error) { |
---|
266 | flash.message = g.message(code: "default.delete.success", args: ["Asset", params.id]) |
---|
267 | redirect(action:search) |
---|
268 | return |
---|
269 | } |
---|
270 | |
---|
271 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
272 | |
---|
273 | if(result.error.code == "default.not.found") { |
---|
274 | redirect(action:search) |
---|
275 | return |
---|
276 | } |
---|
277 | |
---|
278 | redirect(action:show, id: params.id) |
---|
279 | } |
---|
280 | |
---|
281 | /** |
---|
282 | * Edit action. |
---|
283 | */ |
---|
284 | def edit = { |
---|
285 | |
---|
286 | // In the case of an actionSubmit button, rewrite action name from 'index'. |
---|
287 | if(params._action_Edit) |
---|
288 | params.action='edit' |
---|
289 | |
---|
290 | def assetInstance = Asset.get( params.id ) |
---|
291 | |
---|
292 | if(!assetInstance) { |
---|
293 | flash.message = "Asset not found with id ${params.id}" |
---|
294 | redirect(action:search) |
---|
295 | return |
---|
296 | } |
---|
297 | |
---|
298 | return [ assetInstance : assetInstance, |
---|
299 | possibleAssetSubItems: assetService.possibleAssetSubItems(), |
---|
300 | parentPMs: taskService.getParentPMs(assetInstance) ] |
---|
301 | |
---|
302 | } |
---|
303 | |
---|
304 | /** |
---|
305 | * Update action. |
---|
306 | */ |
---|
307 | def update = { |
---|
308 | def assetInstance = Asset.get( params.id ) |
---|
309 | |
---|
310 | if(!assetInstance) { |
---|
311 | flash.message = "Asset not found with id ${params.id}" |
---|
312 | redirect(action:search) |
---|
313 | return |
---|
314 | } |
---|
315 | |
---|
316 | if(params.version) { |
---|
317 | def version = params.version.toLong() |
---|
318 | if(assetInstance.version > version) { |
---|
319 | |
---|
320 | assetInstance.errors.rejectValue("version", "default.optimistic.locking.failure") |
---|
321 | render(view:'edit',model:[assetInstance:assetInstance, |
---|
322 | possibleAssetSubItems: assetService.possibleAssetSubItems(), |
---|
323 | parentPMs: taskService.getParentPMs(assetInstance)] ) |
---|
324 | return |
---|
325 | } |
---|
326 | } |
---|
327 | |
---|
328 | assetInstance.properties = params |
---|
329 | |
---|
330 | use(WordUtils) { |
---|
331 | assetInstance.name = assetInstance.name.capitalize() |
---|
332 | assetInstance.description = assetInstance.description.capitalize() |
---|
333 | } |
---|
334 | |
---|
335 | assetInstance.setAssetSubItemsFromCheckBoxList(params.assetSubItems) |
---|
336 | |
---|
337 | if(!assetInstance.hasErrors() && assetInstance.save(flush: true)) { |
---|
338 | flash.message = "Asset '${assetInstance.name}' updated" |
---|
339 | redirect(action:show,id:assetInstance.id) |
---|
340 | } |
---|
341 | else { |
---|
342 | render(view:'edit',model:[assetInstance:assetInstance, |
---|
343 | possibleAssetSubItems: assetService.possibleAssetSubItems(), |
---|
344 | parentPMs: taskService.getParentPMs(assetInstance)] ) |
---|
345 | } |
---|
346 | |
---|
347 | } |
---|
348 | |
---|
349 | /** |
---|
350 | * Create action. |
---|
351 | */ |
---|
352 | def create = { |
---|
353 | def result = assetService.create(params) |
---|
354 | |
---|
355 | if(!result.error) |
---|
356 | return [assetInstance: result.assetInstance] |
---|
357 | |
---|
358 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
359 | redirect(action: search) |
---|
360 | } |
---|
361 | |
---|
362 | /** |
---|
363 | * Copy action. |
---|
364 | */ |
---|
365 | def copy = { |
---|
366 | def result = assetService.copy(params) |
---|
367 | |
---|
368 | if(!result.error) |
---|
369 | return [assetInstance: result.assetInstance, assetToCopy: result.assetToCopy] |
---|
370 | |
---|
371 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
372 | redirect(action: search) |
---|
373 | } |
---|
374 | |
---|
375 | /** |
---|
376 | * Save action. |
---|
377 | */ |
---|
378 | def save = { |
---|
379 | def result = assetService.save(params) |
---|
380 | |
---|
381 | if(!result.error) { |
---|
382 | flash.message = g.message(code: "default.create.success", args: ["Asset", "'${result.assetInstance.name}'"]) |
---|
383 | redirect(action:show, id: result.assetInstance.id) |
---|
384 | return |
---|
385 | } |
---|
386 | |
---|
387 | render(view:'create', model:[assetInstance: result.assetInstance]) |
---|
388 | } |
---|
389 | |
---|
390 | /** |
---|
391 | * Copy save action. |
---|
392 | */ |
---|
393 | def saveCopy = { |
---|
394 | def result = assetService.saveCopy(params) |
---|
395 | |
---|
396 | if(!result.error) { |
---|
397 | flash.message = g.message(code: "default.create.success", args: ["Asset", result.assetInstance.id]) |
---|
398 | redirect(action:show, id: result.assetInstance.id) |
---|
399 | return |
---|
400 | } |
---|
401 | |
---|
402 | if(result.error.code == "default.not.found") { |
---|
403 | flash.message = g.message(code: result.error.code, args: ["Asset", params.assetToCopy?.id]) |
---|
404 | redirect(action: search) |
---|
405 | return |
---|
406 | } |
---|
407 | |
---|
408 | flash.errorMessage = g.message(code: result.error.code, args: ["Asset"]) |
---|
409 | |
---|
410 | render(view:'copy', model:[assetInstance: result.assetInstance, assetToCopy: result.assetToCopy]) |
---|
411 | } |
---|
412 | |
---|
413 | } // end class |
---|