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