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

Revision 941, 6.6 KB (checked in by business@…, 4 years ago)

added a showByToken action to the study controller

  • Property svn:keywords set to Date Author Rev
Line 
1package dbnp.studycapturing
2
3import grails.converters.*
4
5/**
6 * Controller class for studies
7 */
8class StudyController {
9
10    //static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
11
12    def index = {
13        redirect(action: "list", params: params)
14    }
15
16    def list = {
17        params.max = Math.min(params.max ? params.int('max') : 10, 100)
18        [studyInstanceList: Study.list(params), studyInstanceTotal: Study.count()]
19    }
20
21    /**
22     * Shows a comparison of multiple studies using the show view
23     *
24     */
25    def list_extended = {
26        def startTime = System.currentTimeMillis()
27        params.max = Math.min(params.max ? params.int('max') : 10, 100)
28       
29        def studyList = Study.list(params)
30        render(view:'show',model:[studyList: studyList, studyInstanceTotal: Study.count(), multipleStudies: ( studyList.size() > 1 ) ] )
31    }
32
33    /**
34     * Shows one or more studies
35     */
36    def show = {
37        def startTime = System.currentTimeMillis()
38
39        def studyInstance = Study.get( params.long( "id" ) )
40        if (!studyInstance) {
41            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'study.label', default: 'Study'), params.id])}"
42            redirect(action: "list")
43        }
44        else {
45            // The study instance is packed into an array, to be able to
46            // use the same view for showing the study and comparing multiple
47            // studies
48            [studyList: [ studyInstance ], multipleStudies: false ]
49        }
50    }
51
52        def showByToken = {
53        def studyInstance = Study.findByCode(params.id)
54        if (!studyInstance) {
55            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'study.label', default: 'Study'), params.id])}"
56            redirect(action: "list")
57        }
58        else {
59            redirect(action: "show", id: studyInstance.id)
60        }
61    }
62
63    /**
64     * Gives the events for one eventgroup in JSON format
65     *
66     */
67    def events = {
68        def eventGroupId = Integer.parseInt( params.id );
69        def studyId      = Integer.parseInt( params.study );
70        def eventGroup;
71
72        // eventGroupId == -1 means that the orphaned events should be given
73        if( eventGroupId == -1 ) {
74            def studyInstance = Study.get( studyId )
75           
76            if (studyInstance == null) {
77                flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'study.label', default: 'Study'), studyId])}"
78                redirect(action: "list");
79                return;
80            }
81
82            events = studyInstance.getOrphanEvents();
83        } else {
84            eventGroup = EventGroup.get(params.id)
85
86            if (eventGroup == null) {
87                flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'eventgroup.label', default: 'Eventgroup'), params.id])}"
88                redirect(action: "list");
89                return;
90            }
91            events = eventGroup?.events;
92        }
93
94        // This parameter should give the startdate of the study in milliseconds
95        // since 1-1-1970
96        long startDate  = Long.parseLong( params.startDate )
97
98        // Create JSON object
99        def json = [ 'dateTimeFormat': 'iso8601', events: [] ];
100
101        // Add all other events
102        for( event in events ) {
103            def parameters = []
104            for( templateField in event.giveTemplateFields() ) {
105                def value = event.getFieldValue( templateField.name );
106                if( value ) {
107                    parameters << templateField.name + " = " + value;
108                }
109            }
110
111             json.events << [
112                'start':    new Date( startDate + event.startTime * 1000 ),
113                'end':      new Date( startDate + event.endTime * 1000 ),
114                'durationEvent': !event.isSamplingEvent(),
115                'title': event.template.name + " (" + parameters.join( ', ' ) + ")",
116                'description': parameters
117            ]
118        }
119        render json as JSON
120    }
121
122    /*def edit = {
123        def studyInstance = Study.get(params.id)
124        if (!studyInstance) {
125            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'study.label', default: 'Study'), params.id])}"
126            redirect(action: "list")
127        }
128        else {
129            return [studyInstance: studyInstance]
130        }
131    }
132
133    def update = {
134        def studyInstance = Study.get(params.id)
135        if (studyInstance) {
136            if (params.version) {
137                def version = params.version.toLong()
138                if (studyInstance.version > version) {
139                   
140                    studyInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'study.label', default: 'Study')] as Object[], "Another user has updated this Study while you were editing")
141                    render(view: "edit", model: [studyInstance: studyInstance])
142                    return
143                }
144            }
145            studyInstance.properties = params
146            if (!studyInstance.hasErrors() && studyInstance.save(flush: true)) {
147                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'study.label', default: 'Study'), studyInstance.id])}"
148                redirect(action: "show", id: studyInstance.id)
149            }
150            else {
151                render(view: "edit", model: [studyInstance: studyInstance])
152            }
153        }
154        else {
155            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'study.label', default: 'Study'), params.id])}"
156            redirect(action: "list")
157        }
158    }
159
160    def delete = {
161        def studyInstance = Study.get(params.id)
162        if (studyInstance) {
163            try {
164                studyInstance.delete(flush: true)
165                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'study.label', default: 'Study'), params.id])}"
166                redirect(action: "list")
167            }
168            catch (org.springframework.dao.DataIntegrityViolationException e) {
169                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'study.label', default: 'Study'), params.id])}"
170                redirect(action: "show", id: params.id)
171            }
172        }
173        else {
174            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'study.label', default: 'Study'), params.id])}"
175            redirect(action: "list")
176        }
177    }*/
178}
Note: See TracBrowser for help on using the browser.