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

Revision 1794, 6.9 KB (checked in by work@…, 3 years ago)

- added support for fuzzymatching (resolves #418)
- removed debug lines
- gdt up to 0.0.38

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