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