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 | * Search action. |
---|
111 | */ |
---|
112 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager', 'ROLE_AssetUser']) |
---|
113 | def search = { |
---|
114 | |
---|
115 | if(session.assetSearchParamsMax) |
---|
116 | params.max = session.assetSearchParamsMax |
---|
117 | |
---|
118 | params.max = Math.min( params.max ? params.max.toInteger() : 10, 1000) |
---|
119 | |
---|
120 | def assetInstanceList = [] |
---|
121 | def assetInstanceTotal |
---|
122 | def filterParams = [:] |
---|
123 | |
---|
124 | // Quick Search: |
---|
125 | if(!params.filter) { |
---|
126 | assetInstanceList = Asset.list( params ) |
---|
127 | assetInstanceTotal = Asset.count() |
---|
128 | filterParams.quickSearch = params.quickSearch |
---|
129 | } |
---|
130 | else { |
---|
131 | // filterPane: |
---|
132 | assetInstanceList = filterService.filter( params, Asset ) |
---|
133 | assetInstanceTotal = filterService.count( params, Asset ) |
---|
134 | filterParams = com.zeddware.grails.plugins.filterpane.FilterUtils.extractFilterParams(params) |
---|
135 | } |
---|
136 | |
---|
137 | // export plugin: |
---|
138 | if(params?.format && params.format != "html") { |
---|
139 | |
---|
140 | def dateFmt = { date -> |
---|
141 | formatDate(format: "EEE, dd-MMM-yyyy", date: date) |
---|
142 | } |
---|
143 | |
---|
144 | // def fmtAsset = { m -> |
---|
145 | // def r = '' |
---|
146 | // def assetInstance = Asset.findByName(m) |
---|
147 | // |
---|
148 | // r += assetInstance |
---|
149 | // r += ", " |
---|
150 | // |
---|
151 | // def lastSubAsset = assetInstance.subAssets.size() - 1 |
---|
152 | // assetInstance.subAssets.eachWithIndex() { obj, i -> |
---|
153 | // r += "\"" + obj + "\"" |
---|
154 | // if( i < lastSubAsset ) |
---|
155 | // r += ", " |
---|
156 | // } |
---|
157 | // return r |
---|
158 | // } |
---|
159 | |
---|
160 | // def fmtSubAsset = { m -> |
---|
161 | // def r = '' |
---|
162 | // m.each() { |
---|
163 | // def machine = Machine.findByName(it) |
---|
164 | // def assemblies = machine.assemblies |
---|
165 | // r += machine.name |
---|
166 | // r += " " |
---|
167 | // r += assemblies |
---|
168 | // r += " " |
---|
169 | // } |
---|
170 | // return r |
---|
171 | // } |
---|
172 | |
---|
173 | String title = "Asset List." |
---|
174 | |
---|
175 | response.contentType = ConfigurationHolder.config.grails.mime.types[params.format] |
---|
176 | response.setHeader("Content-disposition", "attachment; filename=assets.${params.extension}") |
---|
177 | List fields = ["section.site", |
---|
178 | "section", |
---|
179 | "name", |
---|
180 | "description"] |
---|
181 | Map labels = ["section.site": "Site", |
---|
182 | "section": "Section", |
---|
183 | "name": "Asset", |
---|
184 | "description": "Description"] |
---|
185 | // Map labels |
---|
186 | // Map formatters = ["subAsset.name": fmtSubAsset] |
---|
187 | Map formatters = [:] |
---|
188 | Map parameters = [title: title, separator: ","] |
---|
189 | |
---|
190 | exportService.export(params.format, |
---|
191 | response.outputStream, |
---|
192 | assetInstanceList.sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }, |
---|
193 | fields, |
---|
194 | labels, |
---|
195 | formatters, |
---|
196 | parameters) |
---|
197 | } |
---|
198 | |
---|
199 | // Add some basic params to filterParams. |
---|
200 | filterParams.max = params.max |
---|
201 | filterParams.offset = params.offset?.toInteger() ?: 0 |
---|
202 | filterParams.sort = params.sort ?: "id" |
---|
203 | filterParams.order = params.order ?: "desc" |
---|
204 | |
---|
205 | return[ assetInstanceList: assetInstanceList, |
---|
206 | assetInstanceTotal: assetInstanceTotal, |
---|
207 | filterParams: filterParams ] |
---|
208 | |
---|
209 | } // end search() |
---|
210 | |
---|
211 | /** |
---|
212 | * Show action. |
---|
213 | */ |
---|
214 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager', 'ROLE_AssetUser']) |
---|
215 | def show = { |
---|
216 | |
---|
217 | // In the case of an actionSubmit button, rewrite action name from 'index'. |
---|
218 | if(params._action_Show) |
---|
219 | { params.action='show' } |
---|
220 | |
---|
221 | def assetInstance = Asset.get( params.id ) |
---|
222 | |
---|
223 | if(!assetInstance) { |
---|
224 | flash.message = "Asset not found with id ${params.id}" |
---|
225 | redirect(action:search) |
---|
226 | } |
---|
227 | else { return [ assetInstance : assetInstance ] } |
---|
228 | } |
---|
229 | |
---|
230 | /** |
---|
231 | * Delete action. |
---|
232 | */ |
---|
233 | def delete = { |
---|
234 | def result = assetService.delete(params) |
---|
235 | |
---|
236 | if(!result.error) { |
---|
237 | flash.message = g.message(code: "default.delete.success", args: ["Asset", params.id]) |
---|
238 | redirect(action:search) |
---|
239 | return |
---|
240 | } |
---|
241 | |
---|
242 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
243 | |
---|
244 | if(result.error.code == "default.not.found") { |
---|
245 | redirect(action:search) |
---|
246 | return |
---|
247 | } |
---|
248 | |
---|
249 | redirect(action:show, id: params.id) |
---|
250 | } |
---|
251 | |
---|
252 | /** |
---|
253 | * Edit action. |
---|
254 | */ |
---|
255 | def edit = { |
---|
256 | |
---|
257 | // In the case of an actionSubmit button, rewrite action name from 'index'. |
---|
258 | if(params._action_Edit) |
---|
259 | { params.action='edit' } |
---|
260 | |
---|
261 | def assetInstance = Asset.get( params.id ) |
---|
262 | |
---|
263 | if(!assetInstance) { |
---|
264 | flash.message = "Asset not found with id ${params.id}" |
---|
265 | redirect(action:search) |
---|
266 | } |
---|
267 | else { |
---|
268 | return [ assetInstance : assetInstance, possibleAssetSubItems: assetService.possibleAssetSubItems() ] |
---|
269 | } |
---|
270 | } |
---|
271 | |
---|
272 | /** |
---|
273 | * Update action. |
---|
274 | */ |
---|
275 | def update = { |
---|
276 | def assetInstance = Asset.get( params.id ) |
---|
277 | if(assetInstance) { |
---|
278 | if(params.version) { |
---|
279 | def version = params.version.toLong() |
---|
280 | if(assetInstance.version > version) { |
---|
281 | |
---|
282 | assetInstance.errors.rejectValue("version", "asset.optimistic.locking.failure", "Another user has updated this Asset while you were editing.") |
---|
283 | render(view:'edit',model:[assetInstance:assetInstance, possibleAssetSubItems: assetService.possibleAssetSubItems()]) |
---|
284 | return |
---|
285 | } |
---|
286 | } |
---|
287 | |
---|
288 | assetInstance.properties = params |
---|
289 | |
---|
290 | assetInstance.setAssetSubItemsFromCheckBoxList(params.assetSubItems) |
---|
291 | |
---|
292 | if(!assetInstance.hasErrors() && assetInstance.save(flush: true)) { |
---|
293 | flash.message = "Asset ${params.id} updated" |
---|
294 | redirect(action:show,id:assetInstance.id) |
---|
295 | } |
---|
296 | else { |
---|
297 | render(view:'edit',model:[assetInstance:assetInstance, possibleAssetSubItems: assetService.possibleAssetSubItems()]) |
---|
298 | } |
---|
299 | } |
---|
300 | else { |
---|
301 | flash.message = "Asset not found with id ${params.id}" |
---|
302 | redirect(action:search) |
---|
303 | } |
---|
304 | } |
---|
305 | |
---|
306 | /** |
---|
307 | * Create action. |
---|
308 | */ |
---|
309 | def create = { |
---|
310 | def result = assetService.create(params) |
---|
311 | |
---|
312 | if(!result.error) |
---|
313 | return [assetInstance: result.assetInstance] |
---|
314 | |
---|
315 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
316 | redirect(action: search) |
---|
317 | } |
---|
318 | |
---|
319 | /** |
---|
320 | * Copy action. |
---|
321 | */ |
---|
322 | def copy = { |
---|
323 | def result = assetService.copy(params) |
---|
324 | |
---|
325 | if(!result.error) |
---|
326 | return [assetInstance: result.assetInstance, assetToCopy: result.assetToCopy] |
---|
327 | |
---|
328 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
329 | redirect(action: search) |
---|
330 | } |
---|
331 | |
---|
332 | /** |
---|
333 | * Save action. |
---|
334 | */ |
---|
335 | def save = { |
---|
336 | def result = assetService.save(params) |
---|
337 | |
---|
338 | if(!result.error) { |
---|
339 | flash.message = g.message(code: "default.create.success", args: ["Asset", result.assetInstance.id]) |
---|
340 | redirect(action:show, id: result.assetInstance.id) |
---|
341 | return |
---|
342 | } |
---|
343 | |
---|
344 | render(view:'create', model:[assetInstance: result.assetInstance]) |
---|
345 | } |
---|
346 | |
---|
347 | /** |
---|
348 | * Copy save action. |
---|
349 | */ |
---|
350 | def saveCopy = { |
---|
351 | def result = assetService.saveCopy(params) |
---|
352 | |
---|
353 | if(!result.error) { |
---|
354 | flash.message = g.message(code: "default.create.success", args: ["Asset", result.assetInstance.id]) |
---|
355 | redirect(action:show, id: result.assetInstance.id) |
---|
356 | return |
---|
357 | } |
---|
358 | |
---|
359 | if(result.error.code == "default.not.found") { |
---|
360 | flash.message = g.message(code: result.error.code, args: ["Asset", params.assetToCopy?.id]) |
---|
361 | redirect(action: search) |
---|
362 | return |
---|
363 | } |
---|
364 | |
---|
365 | if(result.error.code != "default.create.failure") { |
---|
366 | flash.errorMessage = g.message(code: result.error.code, args: ["Asset"]) |
---|
367 | } |
---|
368 | |
---|
369 | render(view:'copy', model:[assetInstance: result.assetInstance, assetToCopy: result.assetToCopy]) |
---|
370 | } |
---|
371 | |
---|
372 | } // end class |
---|