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