1 | package dbnp.studycapturing |
---|
2 | |
---|
3 | import grails.converters.* |
---|
4 | |
---|
5 | /** |
---|
6 | * Controller class for studies |
---|
7 | */ |
---|
8 | class 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 | /*def create = { |
---|
34 | def studyInstance = new Study() |
---|
35 | studyInstance.properties = params |
---|
36 | return [studyInstance: studyInstance] |
---|
37 | } |
---|
38 | |
---|
39 | def save = { |
---|
40 | def studyInstance = new Study(params) |
---|
41 | if (studyInstance.save(flush: true)) { |
---|
42 | flash.message = "${message(code: 'default.created.message', args: [message(code: 'study.label', default: 'Study'), studyInstance.id])}" |
---|
43 | redirect(action: "show", id: studyInstance.id) |
---|
44 | } |
---|
45 | else { |
---|
46 | render(view: "create", model: [studyInstance: studyInstance]) |
---|
47 | } |
---|
48 | }*/ |
---|
49 | |
---|
50 | /** |
---|
51 | * Shows one or more studies |
---|
52 | */ |
---|
53 | def show = { |
---|
54 | def startTime = System.currentTimeMillis() |
---|
55 | |
---|
56 | def studyInstance = Study.get( params.long( "id" ) ) |
---|
57 | if (!studyInstance) { |
---|
58 | flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'study.label', default: 'Study'), params.id])}" |
---|
59 | redirect(action: "list") |
---|
60 | } |
---|
61 | else { |
---|
62 | // The study instance is packed into an array, to be able to |
---|
63 | // use the same view for showing the study and comparing multiple |
---|
64 | // studies |
---|
65 | [studyList: [ studyInstance ], multipleStudies: false ] |
---|
66 | } |
---|
67 | } |
---|
68 | |
---|
69 | /** |
---|
70 | * Gives the events for one eventgroup in JSON format |
---|
71 | * |
---|
72 | */ |
---|
73 | def events = { |
---|
74 | def eventGroup = EventGroup.get(params.id) |
---|
75 | |
---|
76 | // This parameter should give the startdate of the study in milliseconds |
---|
77 | // since 1-1-1970 |
---|
78 | long startDate = Long.parseLong( params.startDate ) |
---|
79 | |
---|
80 | if (!eventGroup) { |
---|
81 | flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'eventgroup.label', default: 'Eventgroup'), params.id])}" |
---|
82 | redirect(action: "list") |
---|
83 | } |
---|
84 | else { |
---|
85 | |
---|
86 | // Create JSON object |
---|
87 | def json = [ 'dateTimeFormat': 'iso8601', events: [] ]; |
---|
88 | |
---|
89 | // Add the start of the study as event |
---|
90 | /* |
---|
91 | json.events << [ |
---|
92 | 'start': startDate, |
---|
93 | 'durationEvent': false, |
---|
94 | 'title': "Start date study", |
---|
95 | 'color': 'red' |
---|
96 | ] |
---|
97 | */ |
---|
98 | |
---|
99 | // Add all other events |
---|
100 | for( event in eventGroup.events ) { |
---|
101 | def parameters = [] |
---|
102 | for( templateField in event.giveTemplateFields() ) { |
---|
103 | def value = event.getFieldValue( templateField.name ); |
---|
104 | if( value ) { |
---|
105 | parameters << templateField.name + " = " + value; |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | json.events << [ |
---|
110 | 'start': new Date( startDate + event.startTime * 1000 ), |
---|
111 | 'end': new Date( startDate + event.endTime * 1000 ), |
---|
112 | 'durationEvent': !event.isSamplingEvent(), |
---|
113 | 'title': event.template.name + " (" + parameters.join( ', ' ) + ")", |
---|
114 | 'description': parameters |
---|
115 | ] |
---|
116 | } |
---|
117 | render json as JSON |
---|
118 | } |
---|
119 | } |
---|
120 | |
---|
121 | /*def edit = { |
---|
122 | def studyInstance = Study.get(params.id) |
---|
123 | if (!studyInstance) { |
---|
124 | flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'study.label', default: 'Study'), params.id])}" |
---|
125 | redirect(action: "list") |
---|
126 | } |
---|
127 | else { |
---|
128 | return [studyInstance: studyInstance] |
---|
129 | } |
---|
130 | } |
---|
131 | |
---|
132 | def update = { |
---|
133 | def studyInstance = Study.get(params.id) |
---|
134 | if (studyInstance) { |
---|
135 | if (params.version) { |
---|
136 | def version = params.version.toLong() |
---|
137 | if (studyInstance.version > version) { |
---|
138 | |
---|
139 | 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") |
---|
140 | render(view: "edit", model: [studyInstance: studyInstance]) |
---|
141 | return |
---|
142 | } |
---|
143 | } |
---|
144 | studyInstance.properties = params |
---|
145 | if (!studyInstance.hasErrors() && studyInstance.save(flush: true)) { |
---|
146 | flash.message = "${message(code: 'default.updated.message', args: [message(code: 'study.label', default: 'Study'), studyInstance.id])}" |
---|
147 | redirect(action: "show", id: studyInstance.id) |
---|
148 | } |
---|
149 | else { |
---|
150 | render(view: "edit", model: [studyInstance: studyInstance]) |
---|
151 | } |
---|
152 | } |
---|
153 | else { |
---|
154 | flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'study.label', default: 'Study'), params.id])}" |
---|
155 | redirect(action: "list") |
---|
156 | } |
---|
157 | } |
---|
158 | |
---|
159 | def delete = { |
---|
160 | def studyInstance = Study.get(params.id) |
---|
161 | if (studyInstance) { |
---|
162 | try { |
---|
163 | studyInstance.delete(flush: true) |
---|
164 | flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'study.label', default: 'Study'), params.id])}" |
---|
165 | redirect(action: "list") |
---|
166 | } |
---|
167 | catch (org.springframework.dao.DataIntegrityViolationException e) { |
---|
168 | flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'study.label', default: 'Study'), params.id])}" |
---|
169 | redirect(action: "show", id: params.id) |
---|
170 | } |
---|
171 | } |
---|
172 | else { |
---|
173 | flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'study.label', default: 'Study'), params.id])}" |
---|
174 | redirect(action: "list") |
---|
175 | } |
---|
176 | }*/ |
---|
177 | } |
---|