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