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

Revision 1565, 7.4 KB (checked in by robert@…, 3 years ago)

Fixed #260 (study wizard in safari) and fixed a bug when adding a publication with a lot of authors (>255 characters)

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