source: trunk/grails-app/controllers/dbnp/studycapturing/StudyController.groovy @ 836

Last change on this file since 836 was 536, checked in by roberth, 13 years ago

Samples are now shown in the study overview page. Also some tests are added and the bootstrap is updated to show some sample fields with a template

  • Property svn:keywords set to Date Author Rev
File size: 6.2 KB
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    /**
53     * Gives the events for one eventgroup in JSON format
54     *
55     */
56    def events = {
57        def eventGroupId = Integer.parseInt( params.id );
58        def studyId      = Integer.parseInt( params.study );
59        def eventGroup;
60
61        // eventGroupId == -1 means that the orphaned events should be given
62        if( eventGroupId == -1 ) {
63            def studyInstance = Study.get( studyId )
64           
65            if (studyInstance == null) {
66                flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'study.label', default: 'Study'), studyId])}"
67                redirect(action: "list");
68                return;
69            }
70
71            events = studyInstance.getOrphanEvents();
72        } else {
73            eventGroup = EventGroup.get(params.id)
74
75            if (eventGroup == null) {
76                flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'eventgroup.label', default: 'Eventgroup'), params.id])}"
77                redirect(action: "list");
78                return;
79            }
80            events = eventGroup?.events;
81        }
82
83        // This parameter should give the startdate of the study in milliseconds
84        // since 1-1-1970
85        long startDate  = Long.parseLong( params.startDate )
86
87        // Create JSON object
88        def json = [ 'dateTimeFormat': 'iso8601', events: [] ];
89
90        // Add all other events
91        for( event in events ) {
92            def parameters = []
93            for( templateField in event.giveTemplateFields() ) {
94                def value = event.getFieldValue( templateField.name );
95                if( value ) {
96                    parameters << templateField.name + " = " + value;
97                }
98            }
99
100             json.events << [
101                'start':    new Date( startDate + event.startTime * 1000 ),
102                'end':      new Date( startDate + event.endTime * 1000 ),
103                'durationEvent': !event.isSamplingEvent(),
104                'title': event.template.name + " (" + parameters.join( ', ' ) + ")",
105                'description': parameters
106            ]
107        }
108        render json as JSON
109    }
110
111    /*def edit = {
112        def studyInstance = Study.get(params.id)
113        if (!studyInstance) {
114            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'study.label', default: 'Study'), params.id])}"
115            redirect(action: "list")
116        }
117        else {
118            return [studyInstance: studyInstance]
119        }
120    }
121
122    def update = {
123        def studyInstance = Study.get(params.id)
124        if (studyInstance) {
125            if (params.version) {
126                def version = params.version.toLong()
127                if (studyInstance.version > version) {
128                   
129                    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")
130                    render(view: "edit", model: [studyInstance: studyInstance])
131                    return
132                }
133            }
134            studyInstance.properties = params
135            if (!studyInstance.hasErrors() && studyInstance.save(flush: true)) {
136                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'study.label', default: 'Study'), studyInstance.id])}"
137                redirect(action: "show", id: studyInstance.id)
138            }
139            else {
140                render(view: "edit", model: [studyInstance: studyInstance])
141            }
142        }
143        else {
144            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'study.label', default: 'Study'), params.id])}"
145            redirect(action: "list")
146        }
147    }
148
149    def delete = {
150        def studyInstance = Study.get(params.id)
151        if (studyInstance) {
152            try {
153                studyInstance.delete(flush: true)
154                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'study.label', default: 'Study'), params.id])}"
155                redirect(action: "list")
156            }
157            catch (org.springframework.dao.DataIntegrityViolationException e) {
158                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'study.label', default: 'Study'), params.id])}"
159                redirect(action: "show", id: params.id)
160            }
161        }
162        else {
163            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'study.label', default: 'Study'), params.id])}"
164            redirect(action: "list")
165        }
166    }*/
167}
Note: See TracBrowser for help on using the repository browser.