root/trunk/grails-app/controllers/dbnp/studycapturing/StudyController.groovy @ 1621

Revision 1621, 12.2 KB (checked in by robert@…, 3 years ago)

Fixed some bugs in simple wizard and study view page

  • Property svn:keywords set to Rev Author Date
Line 
1package dbnp.studycapturing
2
3import grails.converters.*
4import grails.plugins.springsecurity.Secured
5import org.dbnp.gdt.TemplateFieldType
6import org.dbnp.gdt.RelTime
7
8
9/**
10 * Controller class for studies
11 */
12class StudyController {
13    def authenticationService
14   
15    //static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
16
17    def index = {
18        redirect(action: "list", params: params)
19    }
20
21    /**
22     * Shows all studies where the user has access to
23     */
24    def list = {
25
26        def user = authenticationService.getLoggedInUser()
27        def max = Math.min(params.max ? params.int('max') : 10, 100)
28                def offset = params.offset ? params.int( 'offset' ) : 0
29        def studies = Study.giveReadableStudies( user, max, offset );
30                println "Studies: " + studies
31        [studyInstanceList: studies, studyInstanceTotal: Study.countReadableStudies( user ), loggedInUser: user]
32    }
33
34    /**
35     * Shows studies for which the logged in user is the owner
36     */
37    @Secured(['IS_AUTHENTICATED_REMEMBERED'])
38    def myStudies = {
39        def user = authenticationService.getLoggedInUser()
40        def max = Math.min(params.max ? params.int('max') : 10, 100)
41                def offset = params.offset ? params.int( 'offset' ) : 0
42               
43        def studies = Study.findAllByOwner(user, [max:max,offset: offset]);
44        render( view: "list", model: [studyInstanceList: studies, studyInstanceTotal: Study.countByOwner(user), loggedInUser: user] )
45    }
46
47    /**
48     * Shows a comparison of multiple studies using the show view
49     *
50     */
51    def list_extended = {
52                // If nothing has been selected, redirect the user
53                if( !params.id )
54                        redirect( action: 'list' )
55
56                // Check whether one id has been selected or multiple.
57                def ids = params.id
58                if( ids instanceof String )
59                        redirect( action: 'show', id: ids )
60
61                // Parse strings to a long
62                def long_ids = []
63                ids.each { long_ids.add( Long.parseLong( it ) ) }
64
65                println( long_ids )
66
67        def startTime = System.currentTimeMillis()
68                def c = Study.createCriteria()
69
70        def studyList = c {
71                        maxResults( Math.min(params.max ? params.int('max') : 10, 100) )
72                        'in'( "id", long_ids )
73                }
74        render(view:'show',model:[studyList: studyList, studyInstanceTotal: Study.count(), multipleStudies: ( studyList.size() > 1 ) ] )
75    }
76
77    /**
78     * Shows one or more studies
79     */
80    def show = {
81        def startTime = System.currentTimeMillis()
82
83        def studyInstance = Study.get( params.long( "id" ) )
84        if (!studyInstance) {
85            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'study.label', default: 'Study'), params.id])}"
86            redirect(action: "list")
87        }
88        else {
89            // Check whether the user may see this study
90            def loggedInUser = authenticationService.getLoggedInUser()
91            if( !studyInstance.canRead(loggedInUser) ) {
92                flash.message = "You have no access to this study"
93                redirect(action: "list")
94            }
95
96            // The study instance is packed into an array, to be able to
97            // use the same view for showing the study and comparing multiple
98            // studies
99            [studyList: [ studyInstance ], multipleStudies: false, loggedInUser: loggedInUser, facebookLikeUrl: studyInstance.getFieldValue('published') ? "/study/show/${studyInstance?.id}" : '' ]
100        }
101    }
102
103        /**
104     * Shows the subjects tab of one or more studies. Is called when opening the subjects-tab
105         * on the study overview screen.
106     */
107    def show_subjects = {
108                def studyList = readStudies( params.id );
109
110                if( !studyList )
111                        return
112
113                [studyList: studyList, studyInstanceTotal: Study.count(), multipleStudies: ( studyList.size() > 1 ), loggedInUser: authenticationService.getLoggedInUser() ]
114    }
115
116        /**
117     * Shows the events timeline tab of one or more studies. Is called when opening the events timeline-tab
118         * on the study overview screen.
119     */
120    def show_events_timeline = {
121                def studyList = readStudies( params.id );
122
123                if( !studyList )
124                        return
125
126                [studyList: studyList, studyInstanceTotal: Study.count(), multipleStudies: ( studyList.size() > 1 ), loggedInUser: authenticationService.getLoggedInUser() ]
127    }
128
129        /**
130     * Shows the events table tab of one or more studies. Is called when opening the events table-tab
131         * on the study overview screen.
132     */
133    def show_events_table = {
134                def studyList = readStudies( params.id );
135
136                if( !studyList )
137                        return
138
139                [studyList: studyList, studyInstanceTotal: Study.count(), multipleStudies: ( studyList.size() > 1 ), loggedInUser: authenticationService.getLoggedInUser() ]
140    }
141
142        /**
143     * Shows the assays tab of one or more studies. Is called when opening the assays tab
144         * on the study overview screen.
145     */
146    def show_assays = {
147                def studyList = readStudies( params.id );
148
149                if( !studyList )
150                        return
151
152                [studyList: studyList, studyInstanceTotal: Study.count(), multipleStudies: ( studyList.size() > 1 ), loggedInUser: authenticationService.getLoggedInUser() ]
153    }
154
155        /**
156     * Shows the samples tab of one or more studies. Is called when opening the samples-tab
157         * on the study overview screen.
158     */
159    def show_samples = {
160                def studyList = readStudies( params.id );
161
162                if( !studyList )
163                        return
164
165                [studyList: studyList, studyInstanceTotal: Study.count(), multipleStudies: ( studyList.size() > 1 ), loggedInUser: authenticationService.getLoggedInUser() ]
166    }
167
168        /**
169     * Shows the persons tab of one or more studies. Is called when opening the persons tab
170         * on the study overview screen.
171     */
172    def show_persons = {
173                def studyList = readStudies( params.id );
174
175                if( !studyList )
176                        return
177
178                [studyList: studyList, studyInstanceTotal: Study.count(), multipleStudies: ( studyList.size() > 1 ), loggedInUser: authenticationService.getLoggedInUser() ]
179    }
180
181        /**
182     * Shows the publications tab of one or more studies. Is called when opening the publications tab
183         * on the study overview screen.
184     */
185    def show_publications = {
186                def studyList = readStudies( params.id );
187
188                if( !studyList )
189                        return
190
191                [studyList: studyList, studyInstanceTotal: Study.count(), multipleStudies: ( studyList.size() > 1 ), loggedInUser: authenticationService.getLoggedInUser() ]
192    }
193
194        /**
195         * Creates the javascript for showing the timeline of one or more studies
196         */
197        def createTimelineBandsJs = {
198                def studyList = readStudies( params.id );
199
200                if( !studyList )
201                        return
202
203                [studyList: studyList, studyInstanceTotal: Study.count(), multipleStudies: ( studyList.size() > 1 ) ]
204        }
205
206    /**
207         * Reads one or more studies from the database and checks whether the logged
208         * in user is allowed to access them.
209         *
210         * Is used by several show_-methods
211         *
212         * @return List with Study objects or false if an error occurred.
213         */
214        private def readStudies( id ) {
215                // If nothing has been selected, redirect the user
216                if( !id || !( id instanceof String)) {
217            response.status = 500;
218            render 'No study selected';
219            return false
220                }
221
222                // Check whether one id has been selected or multiple.
223                def ids = URLDecoder.decode( id ).split( "," );
224
225                // Parse strings to a long
226                def long_ids = []
227                ids.each { long_ids.add( Long.parseLong( it ) ) }
228
229                def c = Study.createCriteria()
230
231        def studyList = c {
232                        maxResults( Math.min(params.max ? params.int('max') : 10, 100) )
233                        'in'( "id", long_ids )
234                }
235
236                // Check whether the user may see these studies
237                def studiesAllowed = []
238        def loggedInUser = authenticationService.getLoggedInUser()
239
240                studyList.each { studyInstance ->
241            if( studyInstance.canRead(loggedInUser) ) {
242                                studiesAllowed << studyInstance
243            }
244                }
245
246                // If the user is not allowed to see any of the studies, return 404
247                if( studiesAllowed.size() == 0 ) {
248            response.status = 404;
249            render 'Selected studies not found';
250            return false
251                }
252               
253                return studyList
254        }
255
256    def showByToken = {
257        def studyInstance = Study.findByStudyUUID(params.id)
258        if (!studyInstance) {
259            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'study.label', default: 'Study'), params.id])}"
260            redirect(action: "list")
261        }
262        else {
263            // Check whether the user may see this study
264            def loggedInUser = authenticationService.getLoggedInUser()
265            if( !studyInstance.canRead(loggedInUser) ) {
266                flash.message = "You have no access to this study"
267                redirect(action: "list")
268            }
269
270            redirect(action: "show", id: studyInstance.id)
271        }
272    }
273
274    /**
275     * Gives the events for one eventgroup in JSON format
276     *
277     */
278    def events = {
279        def eventGroupId = Integer.parseInt( params.id );
280        def studyId      = Integer.parseInt( params.study );
281        def eventGroup;
282
283        // eventGroupId == -1 means that the orphaned events should be given
284        if( eventGroupId == -1 ) {
285            def studyInstance = Study.get( studyId )
286           
287            if (studyInstance == null) {
288                flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'study.label', default: 'Study'), studyId])}"
289                redirect(action: "list");
290                return;
291            }
292
293            events = studyInstance.getOrphanEvents();
294        } else {
295            eventGroup = EventGroup.get(params.id)
296
297            if (eventGroup == null) {
298                flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'eventgroup.label', default: 'Eventgroup'), params.id])}"
299                redirect(action: "list");
300                return;
301            }
302            events = eventGroup?.events + eventGroup?.samplingEvents;
303        }
304
305        // This parameter should give the startdate of the study in milliseconds
306        // since 1-1-1970
307        long startDate  = Long.parseLong( params.startDate )
308
309        // Create JSON object
310        def json = [ 'dateTimeFormat': 'iso8601', events: [] ];
311
312        // Add all other events
313        for( event in events ) {
314            def parameters = []
315            for( templateField in event.giveTemplateFields() ) {
316                def value = event.getFieldValue( templateField.name );
317                                if( value ) {
318                                        if( templateField.type == TemplateFieldType.RELTIME )
319                                                value = new RelTime( value ).toString();
320
321                        def param = templateField.name + " = " + value;
322
323                                        if( templateField.unit )
324                                                param += templateField.unit;
325
326                    parameters << param ;
327                }
328            }
329
330                        def description = parameters.join( '<br />\n' );
331
332                        if( event instanceof SamplingEvent ) {
333                                 json.events << [
334                                        'start':    new Date( startDate + event.startTime * 1000 ),
335                                        'end':      new Date( startDate + event.startTime * 1000 ),
336                                        'durationEvent': false,
337                                        'title': event.template.name,
338                                        'description': description
339                                ]
340                        } else {
341                                 json.events << [
342                                        'start':    new Date( startDate + event.startTime * 1000 ),
343                                        'end':      new Date( startDate + event.endTime * 1000 ),
344                                        'durationEvent': true,
345                                        'title': event.template.name,
346                                        'description': description
347                                ]
348                               
349                        }
350        }
351        render json as JSON
352    }
353
354    def delete = {
355        def studyInstance = Study.get(params.id)
356        if (studyInstance) {
357            try {
358                studyInstance.delete(flush: true)
359                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'study.label', default: 'Study'), params.id])}"
360                redirect(action: "list")
361            }
362            catch (org.springframework.dao.DataIntegrityViolationException e) {
363                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'study.label', default: 'Study'), params.id])}"
364                redirect(action: "show", id: params.id)
365            }
366        }
367        else {
368            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'study.label', default: 'Study'), params.id])}"
369            redirect(action: "list")
370        }
371    }
372
373    /**
374     * Renders assay names and id's as JSON
375     */
376    def ajaxGetAssays = {
377
378        def study = Study.read(params.id)
379        render study?.assays?.collect{[name: it.name, id: it.id]} as JSON
380    }
381
382
383}
Note: See TracBrowser for help on using the browser.