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