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 | def startDate = params.startDate |
---|
76 | |
---|
77 | if (!eventGroup) { |
---|
78 | flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'eventgroup.label', default: 'Eventgroup'), params.id])}" |
---|
79 | redirect(action: "list") |
---|
80 | } |
---|
81 | else { |
---|
82 | |
---|
83 | // Create JSON object |
---|
84 | def json = [ 'dateTimeFormat': 'iso8601', events: [] ]; |
---|
85 | |
---|
86 | // Add the start of the study as event |
---|
87 | /* |
---|
88 | json.events << [ |
---|
89 | 'start': startDate, |
---|
90 | 'durationEvent': false, |
---|
91 | 'title': "Start date study", |
---|
92 | 'color': 'red' |
---|
93 | ] |
---|
94 | */ |
---|
95 | |
---|
96 | // Add all other events |
---|
97 | for( event in eventGroup.events ) { |
---|
98 | def parameters = [] |
---|
99 | for( templateField in event.giveTemplateFields() ) { |
---|
100 | def value = event.getFieldValue( templateField.name ); |
---|
101 | if( value ) { |
---|
102 | parameters << templateField.name + " = " + value; |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | json.events << [ |
---|
107 | 'start': event.startTime, |
---|
108 | 'end': event.endTime, |
---|
109 | 'durationEvent': !event.isSamplingEvent(), |
---|
110 | 'title': event.template.name + " (" + parameters.join( ', ' ) + ")", |
---|
111 | 'description': parameters |
---|
112 | ] |
---|
113 | } |
---|
114 | render json as JSON |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | /*def edit = { |
---|
119 | def studyInstance = Study.get(params.id) |
---|
120 | if (!studyInstance) { |
---|
121 | flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'study.label', default: 'Study'), params.id])}" |
---|
122 | redirect(action: "list") |
---|
123 | } |
---|
124 | else { |
---|
125 | return [studyInstance: studyInstance] |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | def update = { |
---|
130 | def studyInstance = Study.get(params.id) |
---|
131 | if (studyInstance) { |
---|
132 | if (params.version) { |
---|
133 | def version = params.version.toLong() |
---|
134 | if (studyInstance.version > version) { |
---|
135 | |
---|
136 | 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") |
---|
137 | render(view: "edit", model: [studyInstance: studyInstance]) |
---|
138 | return |
---|
139 | } |
---|
140 | } |
---|
141 | studyInstance.properties = params |
---|
142 | if (!studyInstance.hasErrors() && studyInstance.save(flush: true)) { |
---|
143 | flash.message = "${message(code: 'default.updated.message', args: [message(code: 'study.label', default: 'Study'), studyInstance.id])}" |
---|
144 | redirect(action: "show", id: studyInstance.id) |
---|
145 | } |
---|
146 | else { |
---|
147 | render(view: "edit", model: [studyInstance: studyInstance]) |
---|
148 | } |
---|
149 | } |
---|
150 | else { |
---|
151 | flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'study.label', default: 'Study'), params.id])}" |
---|
152 | redirect(action: "list") |
---|
153 | } |
---|
154 | } |
---|
155 | |
---|
156 | def delete = { |
---|
157 | def studyInstance = Study.get(params.id) |
---|
158 | if (studyInstance) { |
---|
159 | try { |
---|
160 | studyInstance.delete(flush: true) |
---|
161 | flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'study.label', default: 'Study'), params.id])}" |
---|
162 | redirect(action: "list") |
---|
163 | } |
---|
164 | catch (org.springframework.dao.DataIntegrityViolationException e) { |
---|
165 | flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'study.label', default: 'Study'), params.id])}" |
---|
166 | redirect(action: "show", id: params.id) |
---|
167 | } |
---|
168 | } |
---|
169 | else { |
---|
170 | flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'study.label', default: 'Study'), params.id])}" |
---|
171 | redirect(action: "list") |
---|
172 | } |
---|
173 | }*/ |
---|
174 | } |
---|