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