root/trunk/grails-app/controllers/dbnp/studycapturing/PublicationController.groovy @ 437

Revision 437, 4.4 KB (checked in by roberth, 4 years ago)

Added publication controller and views

Line 
1package dbnp.studycapturing
2
3/**
4 * Publications controller
5 *
6 * @author      Robert Horlings
7 * @since       20100519
8 * @package     studycapturing
9 */
10class PublicationController {
11
12    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
13
14    def index = {
15        redirect(action: "list", params: params)
16    }
17
18    def list = {
19        params.max = Math.min(params.max ? params.int('max') : 10, 100)
20        [publicationInstanceList: Publication.list(params), publicationInstanceTotal: Publication.count()]
21    }
22
23    def create = {
24        def publicationInstance = new Publication()
25        publicationInstance.properties = params
26        return [publicationInstance: publicationInstance]
27    }
28
29    def save = {
30        def publicationInstance = new Publication(params)
31        if (publicationInstance.save(flush: true)) {
32            flash.message = "${message(code: 'default.created.message', args: [message(code: 'publication.label', default: 'Publication'), publicationInstance.title])}"
33            redirect(action: "show", id: publicationInstance.id)
34        }
35        else {
36            render(view: "create", model: [publicationInstance: publicationInstance])
37        }
38    }
39
40    def show = {
41        def publicationInstance = Publication.get(params.id)
42        if (!publicationInstance) {
43            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'publication.label', default: 'Publication'), params.id])}"
44            redirect(action: "list")
45        }
46        else {
47            [publicationInstance: publicationInstance]
48        }
49    }
50
51    def edit = {
52        def publicationInstance = Publication.get(params.id)
53        if (!publicationInstance) {
54            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'publication.label', default: 'Publication'), params.id])}"
55            redirect(action: "list")
56        }
57        else {
58            return [publicationInstance: publicationInstance]
59        }
60    }
61
62    def update = {
63        def publicationInstance = Publication.get(params.id)
64        if (publicationInstance) {
65            if (params.version) {
66                def version = params.version.toLong()
67                if (publicationInstance.version > version) {
68                   
69                    publicationInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'publication.label', default: 'Publication')] as Object[], "Another user has updated this Publication while you were editing")
70                    render(view: "edit", model: [publicationInstance: publicationInstance])
71                    return
72                }
73            }
74            publicationInstance.properties = params
75            if (!publicationInstance.hasErrors() && publicationInstance.save(flush: true)) {
76                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'publication.label', default: 'Publication'), publicationInstance.title])}"
77                redirect(action: "show", id: publicationInstance.id)
78            }
79            else {
80                render(view: "edit", model: [publicationInstance: publicationInstance])
81            }
82        }
83        else {
84            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'publication.label', default: 'Publication'), params.id])}"
85            redirect(action: "list")
86        }
87    }
88
89    def delete = {
90        def publicationInstance = Publication.get(params.id)
91        if (publicationInstance) {
92            try {
93                def title = publicationInstance.title
94                publicationInstance.delete(flush: true)
95                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'publication.label', default: 'Publication'), title])}"
96                redirect(action: "list")
97            }
98            catch (org.springframework.dao.DataIntegrityViolationException e) {
99                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'publication.label', default: 'Publication'), params.id])}"
100                redirect(action: "show", id: params.id)
101            }
102        }
103        else {
104            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'publication.label', default: 'Publication'), params.id])}"
105            redirect(action: "list")
106        }
107    }
108}
Note: See TracBrowser for help on using the browser.