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

Revision 1921, 7.0 KB (checked in by t.w.abma@…, 3 years ago)

- added filter to BaseFilters?
- added security annotation to all controllers where needed (except for Home, (Advanced) Search and Publications)
- added tags to top navigation menus showing only menu(items) dependent on whether a user is logged in or not

  • Property svn:keywords set to Rev Author Date
Line 
1package dbnp.studycapturing
2
3import grails.plugins.springsecurity.Secured
4/**
5 * Publications controller
6 *
7 * @author      Robert Horlings
8 * @since       20100519
9 * @package     studycapturing
10 */
11
12class PublicationController {
13
14    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
15
16    def index = {
17        redirect(action: "list", params: params)
18    }
19
20    /**
21     * Shows a form to add a new publication by searching pubmed
22     */
23    def add = {}
24
25    /**
26     * Adds publication selected from pubmed
27     */
28    def createFromPubmed = {
29        // instantiate term with parameters
30        def publication = new Publication(
31            title: params.get( 'publication-title' ),
32            authorsList: params.get( 'publication-authorsList' ),
33            pubMedID: params.get( 'publication-pubMedID' ),
34            DOI: params.get( 'publication-doi' )
35        );
36       
37                // Check whether the autorsList is not too long. If it is, split it
38                if( publication.authorsList.size() > 255 ) {
39                        def postfix = " et al.";
40                        def split = publication.authorsList[ 0..255 - postfix.size()].lastIndexOf( ", " );
41                        publication.authorsList = publication.authorsList[ 0..split-1] + postfix;
42                }
43
44        def message;
45        def errors = '';
46
47        // validate term
48        if (publication.validate()) {
49            publication.save()
50            message = "Publication added to the system"
51        } else {
52            errors = publications.errors.getAllErrors().join( ', ' );
53            message = "Publication addition failed"
54        }
55
56        render( 'view': 'add', model:[ message: message, errors: errors ] );
57    }
58
59    def list = {
60        params.max = Math.min(params.max ? params.int('max') : 10, 100)
61        [publicationInstanceList: Publication.list(params), publicationInstanceTotal: Publication.count()]
62    }
63
64    @Secured(['IS_AUTHENTICATED_REMEMBERED'])
65    def create = {
66        def publicationInstance = new Publication()
67        publicationInstance.properties = params
68        return [publicationInstance: publicationInstance]
69    }
70
71    def save = {
72        def publicationInstance = new Publication(params)
73        if (publicationInstance.save(flush: true)) {
74            flash.message = "${message(code: 'default.created.message', args: [message(code: 'publication.label', default: 'Publication'), publicationInstance.title])}"
75            redirect(action: "show", id: publicationInstance.id)
76        }
77        else {
78            render(view: "create", model: [publicationInstance: publicationInstance])
79        }
80    }
81
82    def show = {
83        def publicationInstance = Publication.get(params.id)
84        if (!publicationInstance) {
85            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'publication.label', default: 'Publication'), params.id])}"
86            redirect(action: "list")
87        }
88        else {
89            [publicationInstance: publicationInstance]
90        }
91    }
92
93    def edit = {
94        def publicationInstance = Publication.get(params.id)
95        if (!publicationInstance) {
96            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'publication.label', default: 'Publication'), params.id])}"
97            redirect(action: "list")
98        }
99        else {
100            return [publicationInstance: publicationInstance]
101        }
102    }
103
104    def update = {
105        def publicationInstance = Publication.get(params.id)
106        if (publicationInstance) {
107            if (params.version) {
108                def version = params.version.toLong()
109                if (publicationInstance.version > version) {
110                   
111                    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")
112                    render(view: "edit", model: [publicationInstance: publicationInstance])
113                    return
114                }
115            }
116            publicationInstance.properties = params
117            if (!publicationInstance.hasErrors() && publicationInstance.save(flush: true)) {
118                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'publication.label', default: 'Publication'), publicationInstance.title])}"
119                redirect(action: "show", id: publicationInstance.id)
120            }
121            else {
122                render(view: "edit", model: [publicationInstance: publicationInstance])
123            }
124        }
125        else {
126            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'publication.label', default: 'Publication'), params.id])}"
127            redirect(action: "list")
128        }
129    }
130
131    def delete = {
132        def publicationInstance = Publication.get(params.id)
133        if (publicationInstance) {
134            try {
135                def title = publicationInstance.title
136                publicationInstance.delete(flush: true)
137                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'publication.label', default: 'Publication'), title])}"
138                redirect(action: "list")
139            }
140            catch (org.springframework.dao.DataIntegrityViolationException e) {
141                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'publication.label', default: 'Publication'), params.id])}"
142                redirect(action: "show", id: params.id)
143            }
144        }
145        else {
146            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'publication.label', default: 'Publication'), params.id])}"
147            redirect(action: "list")
148        }
149    }
150 
151    /**
152     * Searches for an ID in the current database, based on the pubMedID
153     * If the publication is not found in the database, it is added
154     */
155    def getID = {
156        // Find the ID
157        def pubMedID = params.get( 'publication-pubMedID' );
158        if( pubMedID ) {
159            def publication = Publication.findByPubMedID( pubMedID );
160            if( !publication ) {
161                publication = new Publication(
162                    title: params.get( 'publication-title' ),
163                    authorsList: params.get( 'publication-authorsList' ),
164                    pubMedID: params.get( 'publication-pubMedID' ),
165                    DOI: params.get( 'publication-doi' )
166                );
167                       
168                                // Check whether the autorsList is not too long. If it is, split it
169                                if( publication.authorsList.size() > 255 ) {
170                                        def postfix = " et al.";
171                                        def split = publication.authorsList[ 0..255 - postfix.size()].lastIndexOf( ", " );
172                                        publication.authorsList = publication.authorsList[ 0..split-1] + postfix;
173                                }
174                       
175                                publication.save(flush:true);
176            }
177
178            // Return the ID
179                        response.contentType = "text/plain"
180            render publication.id;
181        } else {
182            response.status = 500;
183                        response.contentType = "text/plain"
184            render "No pubMedID found in request";
185        }
186    }
187}
Note: See TracBrowser for help on using the browser.