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

Revision 496, 5.5 KB (checked in by duh, 4 years ago)

- set svn keyword expansion

  • Property svn:keywords set to Date Author Rev
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    /**
19     * Shows a form to add a new publication by searching pubmed
20     */
21    def add = {}
22
23    /**
24     * Adds publication selected from pubmed
25     */
26    def createFromPubmed = {
27        // instantiate term with parameters
28        def publication = new Publication(
29            title: params.get( 'publication-title' ),
30            authorsList: params.get( 'publication-authorsList' ),
31            pubMedID: params.get( 'publication-pubMedID' ),
32            DOI: params.get( 'publication-doi' )
33        )
34
35        def message;
36        def errors = '';
37
38        // validate term
39        if (publication.validate()) {
40            println "Publication validated correctly"
41            publication.save()
42            message = "Publication added to the system"
43        } else {
44            println "Publication validation failed"
45            println "errors:"
46            publication.errors.getAllErrors().each() {
47                println it
48            }
49            errors = publications.errors.getAllErrors.join( ', ' );
50            message = "Publication addition failed"
51        }
52
53        render( 'view': 'add', model:[ message: message, errors: errors ] );
54    }
55
56    def list = {
57        params.max = Math.min(params.max ? params.int('max') : 10, 100)
58        [publicationInstanceList: Publication.list(params), publicationInstanceTotal: Publication.count()]
59    }
60
61    def create = {
62        def publicationInstance = new Publication()
63        publicationInstance.properties = params
64        return [publicationInstance: publicationInstance]
65    }
66
67    def save = {
68        def publicationInstance = new Publication(params)
69        if (publicationInstance.save(flush: true)) {
70            flash.message = "${message(code: 'default.created.message', args: [message(code: 'publication.label', default: 'Publication'), publicationInstance.title])}"
71            redirect(action: "show", id: publicationInstance.id)
72        }
73        else {
74            render(view: "create", model: [publicationInstance: publicationInstance])
75        }
76    }
77
78    def show = {
79        def publicationInstance = Publication.get(params.id)
80        if (!publicationInstance) {
81            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'publication.label', default: 'Publication'), params.id])}"
82            redirect(action: "list")
83        }
84        else {
85            [publicationInstance: publicationInstance]
86        }
87    }
88
89    def edit = {
90        def publicationInstance = Publication.get(params.id)
91        if (!publicationInstance) {
92            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'publication.label', default: 'Publication'), params.id])}"
93            redirect(action: "list")
94        }
95        else {
96            return [publicationInstance: publicationInstance]
97        }
98    }
99
100    def update = {
101        def publicationInstance = Publication.get(params.id)
102        if (publicationInstance) {
103            if (params.version) {
104                def version = params.version.toLong()
105                if (publicationInstance.version > version) {
106                   
107                    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")
108                    render(view: "edit", model: [publicationInstance: publicationInstance])
109                    return
110                }
111            }
112            publicationInstance.properties = params
113            if (!publicationInstance.hasErrors() && publicationInstance.save(flush: true)) {
114                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'publication.label', default: 'Publication'), publicationInstance.title])}"
115                redirect(action: "show", id: publicationInstance.id)
116            }
117            else {
118                render(view: "edit", model: [publicationInstance: publicationInstance])
119            }
120        }
121        else {
122            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'publication.label', default: 'Publication'), params.id])}"
123            redirect(action: "list")
124        }
125    }
126
127    def delete = {
128        def publicationInstance = Publication.get(params.id)
129        if (publicationInstance) {
130            try {
131                def title = publicationInstance.title
132                publicationInstance.delete(flush: true)
133                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'publication.label', default: 'Publication'), title])}"
134                redirect(action: "list")
135            }
136            catch (org.springframework.dao.DataIntegrityViolationException e) {
137                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'publication.label', default: 'Publication'), params.id])}"
138                redirect(action: "show", id: params.id)
139            }
140        }
141        else {
142            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'publication.label', default: 'Publication'), params.id])}"
143            redirect(action: "list")
144        }
145    }
146}
Note: See TracBrowser for help on using the browser.