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