1 | import org.codehaus.groovy.grails.plugins.springsecurity.Secured |
---|
2 | |
---|
3 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
4 | class PictureDetailedController extends BaseController { |
---|
5 | |
---|
6 | def index = { redirect(action:list, params:params) } |
---|
7 | |
---|
8 | // the delete, save and update actions only accept POST requests |
---|
9 | static allowedMethods = [delete:'POST', save:'POST', update:'POST'] |
---|
10 | |
---|
11 | def list = { |
---|
12 | prepareList() |
---|
13 | [ list: Picture.list(params), paginateCount: Picture.count() ] |
---|
14 | } |
---|
15 | |
---|
16 | void prepareList() { |
---|
17 | if (!params.max) { |
---|
18 | params.max = 10 |
---|
19 | } |
---|
20 | if (!params.order) { |
---|
21 | params.order = "desc" |
---|
22 | } |
---|
23 | if (!params.sort) { |
---|
24 | params.sort = "id" |
---|
25 | } |
---|
26 | } |
---|
27 | |
---|
28 | def show = { |
---|
29 | // In the case of an actionSubmit button, rewrite action name from 'index'. |
---|
30 | if(params._action_Show) |
---|
31 | params.action='show' |
---|
32 | |
---|
33 | def picture = Picture.get( params.id ) |
---|
34 | |
---|
35 | if(!picture) { |
---|
36 | flash.message = "Picture not found." |
---|
37 | redirect(action:list) |
---|
38 | } |
---|
39 | else { [ picture : picture ] } |
---|
40 | } |
---|
41 | |
---|
42 | def delete = { |
---|
43 | def picture = Picture.get(params.id) |
---|
44 | def inventoryItem = InventoryItem.get(picture?.inventoryItem?.id) |
---|
45 | |
---|
46 | if (inventoryItem && picture) { |
---|
47 | Picture.withTransaction { status -> |
---|
48 | inventoryItem.picture = null |
---|
49 | inventoryItem.save() |
---|
50 | picture.delete() |
---|
51 | } |
---|
52 | flash.message = "Picture ${params.id} deleted" |
---|
53 | redirect(controller: "inventoryItemDetailed", action: show, id: inventoryItem.id) |
---|
54 | |
---|
55 | } |
---|
56 | else { |
---|
57 | if(picture) { |
---|
58 | flash.message = "Removing orphan picture ${picture.id}." |
---|
59 | picture.delete(flush:true) |
---|
60 | redirect(action: list) |
---|
61 | } |
---|
62 | else { |
---|
63 | flash.message = "Picture not found" |
---|
64 | redirect(action: list) |
---|
65 | } |
---|
66 | } |
---|
67 | } |
---|
68 | |
---|
69 | def edit = { |
---|
70 | // In the case of an actionSubmit button, rewrite action name from 'index'. |
---|
71 | if(params._action_Edit) |
---|
72 | params.action='edit' |
---|
73 | |
---|
74 | def picture = Picture.get(params.id) |
---|
75 | if (!picture) { |
---|
76 | flash.message = "Picture not found" |
---|
77 | redirect(action: list) |
---|
78 | } |
---|
79 | else { |
---|
80 | [ picture : picture ] |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | def update = { |
---|
85 | def picture = Picture.get(params.id) |
---|
86 | def inventoryItem = picture?.inventoryItem |
---|
87 | if (inventoryItem && picture) { |
---|
88 | |
---|
89 | if(params.version) { |
---|
90 | def version = params.version.toLong() |
---|
91 | if(picture.version > version) { |
---|
92 | picture.errors.rejectValue("version", "default.optimistic.locking.failure") |
---|
93 | render(view:'edit',model:[picture:picture]) |
---|
94 | return |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|
98 | picture.properties = params |
---|
99 | if (!picture.hasErrors()) { |
---|
100 | def imaging = new Imaging() |
---|
101 | def images = imaging.updateAll(picture) |
---|
102 | boolean result = false |
---|
103 | Picture.withTransaction{ status -> |
---|
104 | result = picture.save() |
---|
105 | } |
---|
106 | if (result) { |
---|
107 | flash.message = "Picture ${params.id} updated" |
---|
108 | redirect(action: show, id: picture.id) |
---|
109 | } |
---|
110 | else { |
---|
111 | render(view: 'edit', model: [ picture: picture ]) |
---|
112 | } |
---|
113 | } |
---|
114 | else { |
---|
115 | render(view: 'edit', model: [ picture: picture ]) |
---|
116 | } |
---|
117 | } |
---|
118 | else { |
---|
119 | flash.message = "Picture not updated, picture or inventory item not found." |
---|
120 | redirect(action: list) |
---|
121 | } |
---|
122 | } |
---|
123 | |
---|
124 | def create = { |
---|
125 | if(!params.inventoryItem?.id) { |
---|
126 | flash.message = "Please select an inventory item and then 'Add Picture'." |
---|
127 | redirect(controller: "inventoryItemDetailed", action: 'search') |
---|
128 | return |
---|
129 | } |
---|
130 | def picture = new Picture() |
---|
131 | picture.properties = params |
---|
132 | [ picture: picture ] |
---|
133 | } |
---|
134 | |
---|
135 | def save = { |
---|
136 | def inventoryItem = InventoryItem.get(params.inventoryItem.id) |
---|
137 | if (inventoryItem) { |
---|
138 | if(inventoryItem.picture) { |
---|
139 | flash.message = "Inventory item already has a picture, please delete the old picture first." |
---|
140 | redirect(controller: 'inventoryItemDetailed', action: 'show', id: inventoryItem.id) |
---|
141 | return |
---|
142 | } |
---|
143 | def picture = new Picture(params) |
---|
144 | def multiPartFile = request.getFile('file') |
---|
145 | def imaging = new Imaging() |
---|
146 | def images = null |
---|
147 | if (multiPartFile && !multiPartFile.isEmpty()) { |
---|
148 | if (multiPartFile.getSize() > Image.MAX_SIZE) { |
---|
149 | picture.errors.rejectValue('file', 'picture.file.maxSize.exceeded', |
---|
150 | [ 'file', 'Picture', Image.MAX_SIZE ] as Object[], |
---|
151 | 'Property [{0}] of class [{1}] image file is too big (maximum [{2}] bytes)') |
---|
152 | } |
---|
153 | else { |
---|
154 | try { |
---|
155 | images = imaging.createAll(inventoryItem, picture, multiPartFile.inputStream) |
---|
156 | } |
---|
157 | catch(Exception ex) { |
---|
158 | log.error("picture save", ex) |
---|
159 | picture.errors.rejectValue('file', 'picture.file.unrecognised', |
---|
160 | [ 'file', 'Picture', multiPartFile.originalFilename ] as Object[], |
---|
161 | 'Property [{0}] of class [{1}] image file [{2}]: type not recognised') |
---|
162 | } |
---|
163 | } |
---|
164 | } |
---|
165 | else { |
---|
166 | picture.errors.rejectValue('file', 'picture.file.missing', |
---|
167 | ['file', 'Picture', ''] as Object[], |
---|
168 | 'Property [{0}] of class [{1}] no file specified') |
---|
169 | } |
---|
170 | if (images && !picture.hasErrors()) { |
---|
171 | boolean result = false |
---|
172 | Picture.withTransaction { status -> |
---|
173 | images.each { image -> |
---|
174 | picture.addToImages(image) |
---|
175 | } |
---|
176 | result = picture.save() |
---|
177 | inventoryItem.picture = picture |
---|
178 | inventoryItem.save() |
---|
179 | } |
---|
180 | if (result) { |
---|
181 | flash.message = "Picture ${picture.id} created" |
---|
182 | redirect(controller: 'inventoryItemDetailed', action: 'show', id: inventoryItem.id) |
---|
183 | } |
---|
184 | else { |
---|
185 | render(view: 'create', model: [ picture: picture ]) |
---|
186 | } |
---|
187 | } |
---|
188 | else { |
---|
189 | render(view: 'create', model: [ picture: picture ]) |
---|
190 | } |
---|
191 | } |
---|
192 | else { |
---|
193 | flash.message = "Picture not created, inventory item not found." |
---|
194 | redirect(action: list) |
---|
195 | } |
---|
196 | } |
---|
197 | |
---|
198 | def view = { |
---|
199 | def picture = Picture.get(params.id) |
---|
200 | def size = params.size ? Integer.parseInt(params.size) : Image.Small |
---|
201 | def image = picture ? Image.findByPictureAndSize(picture, size) : null |
---|
202 | if (picture && image) { |
---|
203 | response.setContentType(image.contentType) |
---|
204 | response.setContentLength(image.data.size()) |
---|
205 | response.setHeader('filename', image.filename()) |
---|
206 | OutputStream out = response.outputStream |
---|
207 | out.write(image.data) |
---|
208 | out.close() |
---|
209 | } |
---|
210 | else { |
---|
211 | response.sendError(404) |
---|
212 | } |
---|
213 | } |
---|
214 | |
---|
215 | } |
---|