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

Revision 1680, 7.1 KB (checked in by robert@…, 3 years ago)

Solving problems with HTML tags around plain text output

  • Property svn:keywords set to Rev Author Date
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                // Check whether the autorsList is not too long. If it is, split it
36                if( publication.authorsList.size() > 255 ) {
37                        def postfix = " et al.";
38                        def split = publication.authorsList[ 0..255 - postfix.size()].lastIndexOf( ", " );
39                        publication.authorsList = publication.authorsList[ 0..split-1] + postfix;
40                }
41
42        def message;
43        def errors = '';
44
45        // validate term
46        if (publication.validate()) {
47            println "Publication validated correctly"
48            publication.save()
49            message = "Publication added to the system"
50        } else {
51            println "Publication validation failed"
52            println "errors:"
53            publication.errors.getAllErrors().each() {
54                println it
55            }
56            errors = publications.errors.getAllErrors().join( ', ' );
57            message = "Publication addition failed"
58        }
59
60        render( 'view': 'add', model:[ message: message, errors: errors ] );
61    }
62
63    def list = {
64        params.max = Math.min(params.max ? params.int('max') : 10, 100)
65        [publicationInstanceList: Publication.list(params), publicationInstanceTotal: Publication.count()]
66    }
67
68    def create = {
69        def publicationInstance = new Publication()
70        publicationInstance.properties = params
71        return [publicationInstance: publicationInstance]
72    }
73
74    def save = {
75        def publicationInstance = new Publication(params)
76        if (publicationInstance.save(flush: true)) {
77            flash.message = "${message(code: 'default.created.message', args: [message(code: 'publication.label', default: 'Publication'), publicationInstance.title])}"
78            redirect(action: "show", id: publicationInstance.id)
79        }
80        else {
81            render(view: "create", model: [publicationInstance: publicationInstance])
82        }
83    }
84
85    def show = {
86        def publicationInstance = Publication.get(params.id)
87        if (!publicationInstance) {
88            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'publication.label', default: 'Publication'), params.id])}"
89            redirect(action: "list")
90        }
91        else {
92            [publicationInstance: publicationInstance]
93        }
94    }
95
96    def edit = {
97        def publicationInstance = Publication.get(params.id)
98        if (!publicationInstance) {
99            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'publication.label', default: 'Publication'), params.id])}"
100            redirect(action: "list")
101        }
102        else {
103            return [publicationInstance: publicationInstance]
104        }
105    }
106
107    def update = {
108        def publicationInstance = Publication.get(params.id)
109        if (publicationInstance) {
110            if (params.version) {
111                def version = params.version.toLong()
112                if (publicationInstance.version > version) {
113                   
114                    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")
115                    render(view: "edit", model: [publicationInstance: publicationInstance])
116                    return
117                }
118            }
119            publicationInstance.properties = params
120            if (!publicationInstance.hasErrors() && publicationInstance.save(flush: true)) {
121                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'publication.label', default: 'Publication'), publicationInstance.title])}"
122                redirect(action: "show", id: publicationInstance.id)
123            }
124            else {
125                render(view: "edit", model: [publicationInstance: publicationInstance])
126            }
127        }
128        else {
129            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'publication.label', default: 'Publication'), params.id])}"
130            redirect(action: "list")
131        }
132    }
133
134    def delete = {
135        def publicationInstance = Publication.get(params.id)
136        if (publicationInstance) {
137            try {
138                def title = publicationInstance.title
139                publicationInstance.delete(flush: true)
140                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'publication.label', default: 'Publication'), title])}"
141                redirect(action: "list")
142            }
143            catch (org.springframework.dao.DataIntegrityViolationException e) {
144                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'publication.label', default: 'Publication'), params.id])}"
145                redirect(action: "show", id: params.id)
146            }
147        }
148        else {
149            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'publication.label', default: 'Publication'), params.id])}"
150            redirect(action: "list")
151        }
152    }
153
154    /**
155     * Searches for an ID in the current database, based on the pubMedID
156     * If the publication is not found in the database, it is added
157     */
158    def getID = {
159        // Find the ID
160        def pubMedID = params.get( 'publication-pubMedID' );
161        if( pubMedID ) {
162            def publication = Publication.findByPubMedID( pubMedID );
163            if( !publication ) {
164                publication = new Publication(
165                    title: params.get( 'publication-title' ),
166                    authorsList: params.get( 'publication-authorsList' ),
167                    pubMedID: params.get( 'publication-pubMedID' ),
168                    DOI: params.get( 'publication-doi' )
169                );
170                       
171                                // Check whether the autorsList is not too long. If it is, split it
172                                if( publication.authorsList.size() > 255 ) {
173                                        def postfix = " et al.";
174                                        def split = publication.authorsList[ 0..255 - postfix.size()].lastIndexOf( ", " );
175                                        publication.authorsList = publication.authorsList[ 0..split-1] + postfix;
176                                }
177                       
178                                publication.save(flush:true);
179            }
180
181            // Return the ID
182                        response.contentType = "text/plain"
183            render publication.id;
184        } else {
185            response.status = 500;
186                        response.contentType = "text/plain"
187            render "No pubMedID found in request";
188        }
189    }
190}
Note: See TracBrowser for help on using the browser.