source: trunk/grails-app/controllers/PictureDetailedController.groovy @ 182

Last change on this file since 182 was 182, checked in by gav, 14 years ago

Add support for inventory item Pictures and Images.
Add new PersonService, refactor CreateDataService and TaskService to suite.

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