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