1 | import org.codehaus.groovy.grails.plugins.springsecurity.Secured |
---|
2 | import org.codehaus.groovy.grails.commons.ConfigurationHolder |
---|
3 | import org.apache.commons.lang.WordUtils |
---|
4 | |
---|
5 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager']) |
---|
6 | class AssetSubItemDetailedController extends BaseController { |
---|
7 | |
---|
8 | def filterService |
---|
9 | def exportService |
---|
10 | def assetSubItemService |
---|
11 | |
---|
12 | // the delete, save and update actions only accept POST requests |
---|
13 | static allowedMethods = [delete:'POST', save:'POST', update:'POST'] |
---|
14 | |
---|
15 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager', 'ROLE_AssetUser']) |
---|
16 | def index = { redirect(action:search,params:params) } |
---|
17 | |
---|
18 | /** |
---|
19 | * Set session.assetSubItemSearchParamsMax |
---|
20 | */ |
---|
21 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager', 'ROLE_AssetUser']) |
---|
22 | def setSearchParamsMax = { |
---|
23 | def max = 1000 |
---|
24 | if(params.newMax.isInteger()) { |
---|
25 | def i = params.newMax.toInteger() |
---|
26 | if(i > 0 && i <= max) |
---|
27 | session.assetSubItemSearchParamsMax = params.newMax |
---|
28 | if(i > max) |
---|
29 | session.assetSubItemSearchParamsMax = max |
---|
30 | } |
---|
31 | forward(action: 'search', params: params) |
---|
32 | } |
---|
33 | |
---|
34 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager', 'ROLE_AssetUser']) |
---|
35 | def search = { |
---|
36 | |
---|
37 | if(session.assetSubItemSearchParamsMax) |
---|
38 | params.max = session.assetSubItemSearchParamsMax |
---|
39 | |
---|
40 | params.max = Math.min( params.max ? params.max.toInteger() : 10, 1000) |
---|
41 | |
---|
42 | def assetSubItemInstanceList = [] |
---|
43 | def assetSubItemInstanceTotal |
---|
44 | def filterParams = [:] |
---|
45 | |
---|
46 | // Quick Search: |
---|
47 | if(!params.filter) { |
---|
48 | assetSubItemInstanceList = AssetSubItem.list( params ) |
---|
49 | assetSubItemInstanceTotal = AssetSubItem.count() |
---|
50 | filterParams.quickSearch = params.quickSearch |
---|
51 | } |
---|
52 | else { |
---|
53 | // filterPane: |
---|
54 | assetSubItemInstanceList = filterService.filter( params, AssetSubItem ) |
---|
55 | assetSubItemInstanceTotal = filterService.count( params, AssetSubItem ) |
---|
56 | filterParams = com.zeddware.grails.plugins.filterpane.FilterUtils.extractFilterParams(params) |
---|
57 | } |
---|
58 | |
---|
59 | // export plugin: |
---|
60 | if(params?.format && params.format != "html") { |
---|
61 | |
---|
62 | def dateFmt = { date -> |
---|
63 | formatDate(format: "EEE, dd-MMM-yyyy", date: date) |
---|
64 | } |
---|
65 | |
---|
66 | String title = "AssetSubItem List." |
---|
67 | |
---|
68 | response.contentType = ConfigurationHolder.config.grails.mime.types[params.format] |
---|
69 | response.setHeader("Content-disposition", "attachment; filename=assets.${params.extension}") |
---|
70 | List fields = ["name", |
---|
71 | "description"] |
---|
72 | Map labels = ["name": "AssetSubItem", |
---|
73 | "description": "Description"] |
---|
74 | |
---|
75 | Map formatters = [:] |
---|
76 | Map parameters = [title: title, separator: ","] |
---|
77 | |
---|
78 | exportService.export(params.format, |
---|
79 | response.outputStream, |
---|
80 | assetSubItemInstanceList.sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }, |
---|
81 | fields, |
---|
82 | labels, |
---|
83 | formatters, |
---|
84 | parameters) |
---|
85 | } |
---|
86 | |
---|
87 | // Add some basic params to filterParams. |
---|
88 | filterParams.max = params.max |
---|
89 | filterParams.offset = params.offset?.toInteger() ?: 0 |
---|
90 | filterParams.sort = params.sort ?: "id" |
---|
91 | filterParams.order = params.order ?: "desc" |
---|
92 | |
---|
93 | return[ assetSubItemInstanceList: assetSubItemInstanceList, |
---|
94 | assetSubItemInstanceTotal: assetSubItemInstanceTotal, |
---|
95 | filterParams: filterParams ] |
---|
96 | |
---|
97 | } // end search() |
---|
98 | |
---|
99 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager', 'ROLE_AssetUser']) |
---|
100 | def show = { |
---|
101 | |
---|
102 | // In the case of an actionSubmit button, rewrite action name from 'index'. |
---|
103 | if(params._action_Show) |
---|
104 | params.action='show' |
---|
105 | |
---|
106 | def assetSubItemInstance = AssetSubItem.get( params.id ) |
---|
107 | |
---|
108 | if(!assetSubItemInstance) { |
---|
109 | flash.message = "AssetSubItem not found with id ${params.id}" |
---|
110 | redirect(action: "search") |
---|
111 | } |
---|
112 | else { return [ assetSubItemInstance : assetSubItemInstance ] } |
---|
113 | } |
---|
114 | |
---|
115 | def delete = { |
---|
116 | def result = assetSubItemService.delete(params) |
---|
117 | |
---|
118 | if(!result.error) { |
---|
119 | flash.message = g.message(code: "default.delete.success", args: ["AssetSubItem", params.id]) |
---|
120 | redirect(action: search) |
---|
121 | return |
---|
122 | } |
---|
123 | |
---|
124 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
125 | |
---|
126 | if(result.error.code == "default.not.found") { |
---|
127 | redirect(action: search) |
---|
128 | return |
---|
129 | } |
---|
130 | |
---|
131 | redirect(action:show, id: params.id) |
---|
132 | } |
---|
133 | |
---|
134 | def edit = { |
---|
135 | |
---|
136 | // In the case of an actionSubmit button, rewrite action name from 'index'. |
---|
137 | if(params._action_Edit) |
---|
138 | params.action='edit' |
---|
139 | |
---|
140 | def assetSubItemInstance = AssetSubItem.get( params.id ) |
---|
141 | |
---|
142 | if(!assetSubItemInstance) { |
---|
143 | flash.message = "AssetSubItem not found with id ${params.id}" |
---|
144 | redirect(action: "search") |
---|
145 | return |
---|
146 | } |
---|
147 | |
---|
148 | def possibleParentItems = assetSubItemService.possibleParentItems(assetSubItemInstance) |
---|
149 | |
---|
150 | return [ assetSubItemInstance : assetSubItemInstance, possibleParentItems: possibleParentItems ] |
---|
151 | } |
---|
152 | |
---|
153 | def update = { |
---|
154 | def assetSubItemInstance = AssetSubItem.get( params.id ) |
---|
155 | if(assetSubItemInstance) { |
---|
156 | if(params.version) { |
---|
157 | def version = params.version.toLong() |
---|
158 | if(assetSubItemInstance.version > version) { |
---|
159 | |
---|
160 | assetSubItemInstance.errors.rejectValue("version", "assetSubItem.optimistic.locking.failure", "Another user has updated this AssetSubItem while you were editing.") |
---|
161 | render(view:'edit',model:[assetSubItemInstance:assetSubItemInstance]) |
---|
162 | return |
---|
163 | } |
---|
164 | } |
---|
165 | |
---|
166 | assetSubItemInstance.properties = params |
---|
167 | |
---|
168 | use(WordUtils) { |
---|
169 | assetSubItemInstance.name = assetSubItemInstance.name.capitalize() |
---|
170 | assetSubItemInstance.description = assetSubItemInstance.description.capitalize() |
---|
171 | } |
---|
172 | |
---|
173 | if(assetSubItemInstance.assets) { |
---|
174 | assetSubItemInstance.parentItem = null |
---|
175 | } |
---|
176 | |
---|
177 | if(!assetSubItemInstance.hasErrors() && assetSubItemInstance.save(flush: true)) { |
---|
178 | flash.message = "Sub Item '${assetSubItemInstance.name}' updated" |
---|
179 | |
---|
180 | if(assetSubItemInstance.assets) |
---|
181 | redirect(controller: "assetDetailed", action:"show", id: assetSubItemInstance.assets.toArray()[0].id) |
---|
182 | else { |
---|
183 | //Get the top parentItem. |
---|
184 | def parentItem = assetSubItemInstance.parentItem |
---|
185 | def i = 0 |
---|
186 | while(parentItem?.parentItem) { |
---|
187 | parentItem = parentItem.parentItem |
---|
188 | // Protect against infinite recurrsion. |
---|
189 | i++ |
---|
190 | if(i > 100) |
---|
191 | break |
---|
192 | } |
---|
193 | |
---|
194 | if(parentItem?.assets) |
---|
195 | redirect(controller: "assetDetailed", action:"show", id: parentItem.assets.toArray()[0].id) |
---|
196 | else |
---|
197 | redirect(action:"show", id: assetSubItemInstance.id) // End stop if all else fails. |
---|
198 | } |
---|
199 | } |
---|
200 | else { |
---|
201 | render(view:'edit',model:[assetSubItemInstance:assetSubItemInstance]) |
---|
202 | } |
---|
203 | } |
---|
204 | else { |
---|
205 | flash.message = "AssetSubItem not found with id ${params.id}" |
---|
206 | redirect(action: "search") |
---|
207 | } |
---|
208 | } |
---|
209 | |
---|
210 | def create = { |
---|
211 | def result = assetSubItemService.create(params) |
---|
212 | |
---|
213 | if(!result.error) |
---|
214 | return [assetSubItemInstance: result.assetSubItemInstance, assetInstance: result.assetInstance] |
---|
215 | |
---|
216 | flash.message = g.message(code: result.error.code, args: result.error.args) |
---|
217 | redirect(controller: 'assetDetailed', action: 'search') |
---|
218 | } |
---|
219 | |
---|
220 | def save = { |
---|
221 | def result = assetSubItemService.save(params) |
---|
222 | |
---|
223 | if(!result.error) { |
---|
224 | // Success. |
---|
225 | flash.message = g.message(code: "default.create.success", args: ["Sub Item", "'${result.assetSubItemInstance.name}'"]) |
---|
226 | |
---|
227 | /// @todo: This just grabs the first parent in an unsorted list and needs improving. |
---|
228 | if(result.assetSubItemInstance.assets) |
---|
229 | redirect(controller: "assetDetailed", action:"show", id: result.assetSubItemInstance.assets.toArray()[0].id) |
---|
230 | else { |
---|
231 | //Get the top parentItem. |
---|
232 | def parentItem = result.assetSubItemInstance.parentItem |
---|
233 | def i = 0 |
---|
234 | while(parentItem?.parentItem) { |
---|
235 | parentItem = parentItem.parentItem |
---|
236 | // Protect against infinite recurrsion. |
---|
237 | i++ |
---|
238 | if(i > 100) |
---|
239 | break |
---|
240 | } |
---|
241 | |
---|
242 | if(parentItem?.assets) |
---|
243 | redirect(controller: "assetDetailed", action:"show", id: parentItem.assets.toArray()[0].id) |
---|
244 | else |
---|
245 | redirect(action:"show", id: result.assetSubItemInstance.id) // End stop if all else fails. |
---|
246 | } |
---|
247 | return |
---|
248 | } |
---|
249 | |
---|
250 | if(result.error.code == "assetSubItem.asset.not.found") |
---|
251 | flash.errorMessage = g.message(code: "default.create.success") |
---|
252 | |
---|
253 | render(view:'create', model:[assetSubItemInstance: result.assetSubItemInstance, assetInstance: result.assetInstance]) |
---|
254 | } |
---|
255 | |
---|
256 | } // end class |
---|