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 assetSubItemInstance = AssetSubItem.get( params.id ) |
---|
95 | if(assetSubItemInstance) { |
---|
96 | try { |
---|
97 | assetSubItemInstance.delete(flush:true) |
---|
98 | flash.message = "AssetSubItem ${params.id} deleted" |
---|
99 | redirect(action: "search") |
---|
100 | } |
---|
101 | catch(org.springframework.dao.DataIntegrityViolationException e) { |
---|
102 | flash.errorMessage = "AssetSubItem ${params.id} could not be deleted" |
---|
103 | redirect(action:show,id:params.id) |
---|
104 | } |
---|
105 | } |
---|
106 | else { |
---|
107 | flash.message = "AssetSubItem not found with id ${params.id}" |
---|
108 | redirect(action: "search") |
---|
109 | } |
---|
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 | } |
---|
119 | else { |
---|
120 | return [ assetSubItemInstance : assetSubItemInstance ] |
---|
121 | } |
---|
122 | } |
---|
123 | |
---|
124 | def update = { |
---|
125 | def assetSubItemInstance = AssetSubItem.get( params.id ) |
---|
126 | if(assetSubItemInstance) { |
---|
127 | if(params.version) { |
---|
128 | def version = params.version.toLong() |
---|
129 | if(assetSubItemInstance.version > version) { |
---|
130 | |
---|
131 | assetSubItemInstance.errors.rejectValue("version", "assetSubItem.optimistic.locking.failure", "Another user has updated this AssetSubItem while you were editing.") |
---|
132 | render(view:'edit',model:[assetSubItemInstance:assetSubItemInstance]) |
---|
133 | return |
---|
134 | } |
---|
135 | } |
---|
136 | |
---|
137 | assetSubItemInstance.properties = params |
---|
138 | |
---|
139 | // if(!assetSubItemInstance.asset && !assetSubItemInstance.parentItem) { |
---|
140 | // flash.message = "Either an asset or parentItem is required." |
---|
141 | // render(view:'edit',model:[assetSubItemInstance:assetSubItemInstance]) |
---|
142 | // return |
---|
143 | // } |
---|
144 | // |
---|
145 | // if(assetSubItemInstance.asset && assetSubItemInstance.parentItem) { |
---|
146 | // flash.message = "Both an asset and parentItem may not be specified at the same time." |
---|
147 | // render(view:'edit',model:[parentItem:parentItem]) |
---|
148 | // return |
---|
149 | // } |
---|
150 | |
---|
151 | if(!assetSubItemInstance.hasErrors() && assetSubItemInstance.save(flush: true)) { |
---|
152 | flash.message = "AssetSubItem ${params.id} updated" |
---|
153 | |
---|
154 | if(assetSubItemInstance.assets) |
---|
155 | redirect(controller: "assetDetailed", action:"show", id: assetSubItemInstance.assets.toArray()[0].id) |
---|
156 | else { |
---|
157 | //Get the top parentItem. |
---|
158 | def parentItem = assetSubItemInstance.parentItem |
---|
159 | def i = 0 |
---|
160 | while(parentItem?.parentItem) { |
---|
161 | parentItem = parentItem.parentItem |
---|
162 | // Protect against infinite recurrsion. |
---|
163 | i++ |
---|
164 | if(i > 100) |
---|
165 | break |
---|
166 | } |
---|
167 | |
---|
168 | if(parentItem?.assets) |
---|
169 | redirect(controller: "assetDetailed", action:"show", id: parentItem.assets.toArray()[0].id) |
---|
170 | else |
---|
171 | redirect(action:"show", id: assetSubItemInstance.id) // End stop if all else fails. |
---|
172 | } |
---|
173 | } |
---|
174 | else { |
---|
175 | render(view:'edit',model:[assetSubItemInstance:assetSubItemInstance]) |
---|
176 | } |
---|
177 | } |
---|
178 | else { |
---|
179 | flash.message = "AssetSubItem not found with id ${params.id}" |
---|
180 | redirect(action: "search") |
---|
181 | } |
---|
182 | } |
---|
183 | |
---|
184 | def create = { |
---|
185 | def result = assetSubItemService.create(params) |
---|
186 | |
---|
187 | if(!result.error) |
---|
188 | return [assetSubItemInstance: result.assetSubItemInstance, assetInstance: result.assetInstance] |
---|
189 | |
---|
190 | flash.message = g.message(code: result.error.code, args: result.error.args) |
---|
191 | redirect(controller: 'assetDetailed', action: 'search') |
---|
192 | } |
---|
193 | |
---|
194 | def save = { |
---|
195 | def result = assetSubItemService.save(params) |
---|
196 | |
---|
197 | if(!result.error) { |
---|
198 | // Success. |
---|
199 | flash.message = g.message(code: "default.create.success", args: ["AssetSubItem", result.assetSubItemInstance.name]) |
---|
200 | |
---|
201 | /// @todo: This just grabs the first parent in an unsorted list and needs improving. |
---|
202 | if(result.assetSubItemInstance.assets) |
---|
203 | redirect(controller: "assetDetailed", action:"show", id: result.assetSubItemInstance.assets.toArray()[0].id) |
---|
204 | else { |
---|
205 | //Get the top parentItem. |
---|
206 | def parentItem = result.assetSubItemInstance.parentItem |
---|
207 | def i = 0 |
---|
208 | while(parentItem?.parentItem) { |
---|
209 | parentItem = parentItem.parentItem |
---|
210 | // Protect against infinite recurrsion. |
---|
211 | i++ |
---|
212 | if(i > 100) |
---|
213 | break |
---|
214 | } |
---|
215 | |
---|
216 | if(parentItem?.assets) |
---|
217 | redirect(controller: "assetDetailed", action:"show", id: parentItem.assets.toArray()[0].id) |
---|
218 | else |
---|
219 | redirect(action:"show", id: result.assetSubItemInstance.id) // End stop if all else fails. |
---|
220 | } |
---|
221 | return |
---|
222 | } |
---|
223 | |
---|
224 | if(result.error.code == "assetSubItem.asset.not.found") |
---|
225 | flash.errorMessage = g.message(code: "default.create.success") |
---|
226 | |
---|
227 | render(view:'create', model:[assetSubItemInstance: result.assetSubItemInstance, assetInstance: result.assetInstance]) |
---|
228 | } |
---|
229 | |
---|
230 | } // end class |
---|