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