1 | /** |
---|
2 | * Api Controler |
---|
3 | * |
---|
4 | * API for third party applications to interact |
---|
5 | * with GSCF |
---|
6 | * |
---|
7 | * @author your email (+name?) |
---|
8 | * @since 20120328ma |
---|
9 | * |
---|
10 | * Revision information: |
---|
11 | * $Rev$ |
---|
12 | * $Author$ |
---|
13 | * $Date$ |
---|
14 | */ |
---|
15 | package api |
---|
16 | |
---|
17 | import grails.plugins.springsecurity.Secured |
---|
18 | import grails.converters.JSON |
---|
19 | import dbnp.studycapturing.Study |
---|
20 | import dbnp.authentication.SecUser |
---|
21 | import org.dbnp.gdt.TemplateFieldType |
---|
22 | |
---|
23 | class ApiController { |
---|
24 | def authenticationService |
---|
25 | def ApiService |
---|
26 | |
---|
27 | /** |
---|
28 | * index closure |
---|
29 | */ |
---|
30 | def index = { |
---|
31 | render(view:'index') |
---|
32 | } |
---|
33 | |
---|
34 | @Secured(['ROLE_CLIENT', 'ROLE_ADMIN']) |
---|
35 | def authenticate = { |
---|
36 | println "api::authenticate: ${params}" |
---|
37 | |
---|
38 | // see if we already have a token on file for this device id |
---|
39 | def token = Token.findByDeviceID(params.deviceID) |
---|
40 | |
---|
41 | // generate a new token if we don't have a token on file |
---|
42 | def result = [:] |
---|
43 | try { |
---|
44 | if (!token) { |
---|
45 | // generate a token for this device |
---|
46 | token = new Token( |
---|
47 | deviceID: params.deviceID, |
---|
48 | deviceToken: UUID.randomUUID().toString(), |
---|
49 | sequence: 0 |
---|
50 | ).save(failOnError: true) |
---|
51 | } |
---|
52 | |
---|
53 | result = ['token':token.deviceToken,'sequence':token.sequence] |
---|
54 | |
---|
55 | // set output headers |
---|
56 | response.status = 200 |
---|
57 | } catch (Exception e) { |
---|
58 | // caught an error |
---|
59 | response.status = 500 |
---|
60 | result = ['error':e.getMessage()] |
---|
61 | } |
---|
62 | |
---|
63 | response.contentType = 'application/json;charset=UTF-8' |
---|
64 | |
---|
65 | if (params.containsKey('callback')) { |
---|
66 | render "${params.callback}(${result as JSON})" |
---|
67 | } else { |
---|
68 | render result as JSON |
---|
69 | } |
---|
70 | } |
---|
71 | |
---|
72 | @Secured(['ROLE_CLIENT', 'ROLE_ADMIN']) |
---|
73 | def getStudies = { |
---|
74 | println "api::getStudies: ${params}" |
---|
75 | |
---|
76 | String deviceID = (params.containsKey('deviceID')) ? params.deviceID : '' |
---|
77 | String validation = (params.containsKey('validation')) ? params.validation : '' |
---|
78 | |
---|
79 | // check |
---|
80 | if (!apiService.validateRequest(deviceID,validation)) { |
---|
81 | response.sendError(401, 'Unauthorized') |
---|
82 | } else { |
---|
83 | def user = authenticationService.getLoggedInUser() |
---|
84 | def readableStudies = Study.giveReadableStudies(user) |
---|
85 | def studies = [] |
---|
86 | |
---|
87 | // iterate through studies and define resultset |
---|
88 | readableStudies.each { study -> |
---|
89 | // get result data |
---|
90 | studies[ studies.size() ] = [ |
---|
91 | 'token' : study.getToken(), |
---|
92 | 'title' : study.title, |
---|
93 | 'description' : study.description, |
---|
94 | 'subjects' : study.subjects.size(), |
---|
95 | 'species' : study.subjects.species.collect { it.name }.unique(), |
---|
96 | 'assays' : study.assays.collect { it.module.name }.unique(), |
---|
97 | 'events' : study.events.size(), |
---|
98 | 'uniqueEvents' : study.events.collect { it.toString() }.unique(), |
---|
99 | 'samplingEvents' : study.samplingEvents.size(), |
---|
100 | 'uniqueSamplingEvents' : study.samplingEvents.collect { it.toString() }.unique(), |
---|
101 | 'eventGroups' : study.eventGroups.size(), |
---|
102 | 'uniqueEventGroups' : study.eventGroups.collect { it.name }.unique(), |
---|
103 | 'samples' : study.samples.size() |
---|
104 | ] |
---|
105 | } |
---|
106 | |
---|
107 | |
---|
108 | def result = [ |
---|
109 | 'count' : studies.size(), |
---|
110 | 'studies' : studies |
---|
111 | ] |
---|
112 | |
---|
113 | // set output headers |
---|
114 | response.status = 200 |
---|
115 | response.contentType = 'application/json;charset=UTF-8' |
---|
116 | |
---|
117 | if (params.containsKey('callback')) { |
---|
118 | render "${params.callback}(${result as JSON})" |
---|
119 | } else { |
---|
120 | render result as JSON |
---|
121 | } |
---|
122 | } |
---|
123 | } |
---|
124 | |
---|
125 | @Secured(['ROLE_CLIENT', 'ROLE_ADMIN']) |
---|
126 | def getSubjectsForStudy = { |
---|
127 | println "api::getSubjectsForStudy: ${params}" |
---|
128 | |
---|
129 | String deviceID = (params.containsKey('deviceID')) ? params.deviceID : '' |
---|
130 | String validation = (params.containsKey('validation')) ? params.validation : '' |
---|
131 | String studyToken = (params.containsKey('studyToken')) ? params.studyToken : '' |
---|
132 | |
---|
133 | // fetch user and study |
---|
134 | def user = authenticationService.getLoggedInUser() |
---|
135 | def study = Study.findByStudyUUID(studyToken) |
---|
136 | |
---|
137 | // check |
---|
138 | if (!apiService.validateRequest(deviceID,validation)) { |
---|
139 | response.sendError(401, 'Unauthorized') |
---|
140 | } else if (!study) { |
---|
141 | response.sendError(400, 'No such study') |
---|
142 | } else if (!study.canRead(user)) { |
---|
143 | response.sendError(401, 'Unauthorized') |
---|
144 | } else { |
---|
145 | def subjects = [] |
---|
146 | |
---|
147 | // iterate through subjects |
---|
148 | study.subjects.each { |
---|
149 | def fields = it.giveFields() |
---|
150 | def subject = [:] |
---|
151 | |
---|
152 | // add subject id |
---|
153 | subject['id'] = it.id |
---|
154 | |
---|
155 | // add subject field values |
---|
156 | fields.each { field -> |
---|
157 | def value = it.getFieldValue( field.name ) |
---|
158 | |
---|
159 | if (value.hasProperty('name')) { |
---|
160 | subject[ field.name ] = value.name |
---|
161 | } else { |
---|
162 | subject[ field.name ] = value |
---|
163 | } |
---|
164 | } |
---|
165 | |
---|
166 | subjects[ subjects.size() ] = subject |
---|
167 | } |
---|
168 | |
---|
169 | // define result |
---|
170 | def result = [ |
---|
171 | 'count' : study.subjects.size(), |
---|
172 | 'subjects' : subjects |
---|
173 | ] |
---|
174 | |
---|
175 | // set output headers |
---|
176 | response.status = 200 |
---|
177 | response.contentType = 'application/json;charset=UTF-8' |
---|
178 | |
---|
179 | if (params.containsKey('callback')) { |
---|
180 | render "${params.callback}(${result as JSON})" |
---|
181 | } else { |
---|
182 | render result as JSON |
---|
183 | } |
---|
184 | } |
---|
185 | } |
---|
186 | |
---|
187 | |
---|
188 | @Secured(['ROLE_CLIENT', 'ROLE_ADMIN']) |
---|
189 | def getAssaysForStudy = { |
---|
190 | println "api::getAssaysForStudy: ${params}" |
---|
191 | |
---|
192 | String deviceID = (params.containsKey('deviceID')) ? params.deviceID : '' |
---|
193 | String validation = (params.containsKey('validation')) ? params.validation : '' |
---|
194 | String studyToken = (params.containsKey('studyToken')) ? params.studyToken : '' |
---|
195 | |
---|
196 | // fetch user and study |
---|
197 | def user = authenticationService.getLoggedInUser() |
---|
198 | def study = Study.findByStudyUUID(studyToken) |
---|
199 | |
---|
200 | // check |
---|
201 | if (!apiService.validateRequest(deviceID,validation)) { |
---|
202 | println "1" |
---|
203 | response.sendError(401, 'Unauthorized') |
---|
204 | } else if (!study) { |
---|
205 | println "2" |
---|
206 | response.sendError(400, 'No such study') |
---|
207 | } else if (!study.canRead(user)) { |
---|
208 | println "3" |
---|
209 | response.sendError(401, 'Unauthorized') |
---|
210 | } else { |
---|
211 | // define result |
---|
212 | def result = [ |
---|
213 | // 'count' : study.subjects.size(), |
---|
214 | // 'subjects' : subjects |
---|
215 | ] |
---|
216 | |
---|
217 | // set output headers |
---|
218 | response.status = 200 |
---|
219 | response.contentType = 'application/json;charset=UTF-8' |
---|
220 | |
---|
221 | if (params.containsKey('callback')) { |
---|
222 | render "${params.callback}(${result as JSON})" |
---|
223 | } else { |
---|
224 | render result as JSON |
---|
225 | } |
---|
226 | } |
---|
227 | } |
---|
228 | } |
---|