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