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