1 | /** |
---|
2 | * RestControler |
---|
3 | * |
---|
4 | * This controler provides a REST service. |
---|
5 | * The names of the RESET resources are the same as the names of this |
---|
6 | * controller's actions. E.g., the resources called getStudies simply |
---|
7 | * corresponds to the action getStudies. Some of the resources are parameterized. |
---|
8 | * The parameters are passed as parameters in the url and are available in the |
---|
9 | * params respecting Grails' conventions. In this file, we adher to the javadoc |
---|
10 | * convention for describing parameters ("@param"), but actually we mean |
---|
11 | * key-value pairs in the params object of each Grails action we comment on. |
---|
12 | * |
---|
13 | * @author Jahn-Takeshi Saito |
---|
14 | * @since 20100601 |
---|
15 | * |
---|
16 | */ |
---|
17 | |
---|
18 | import dbnp.studycapturing.Study |
---|
19 | import dbnp.studycapturing.Assay |
---|
20 | import grails.converters.* |
---|
21 | import nl.metabolomicscentre.dsp.http.BasicAuthentication |
---|
22 | |
---|
23 | |
---|
24 | class RestController { |
---|
25 | |
---|
26 | |
---|
27 | |
---|
28 | /**************************************************/ |
---|
29 | /** Rest resources for Simple Assay Module (SAM) **/ |
---|
30 | /**************************************************/ |
---|
31 | |
---|
32 | def authService |
---|
33 | def beforeInterceptor = [action:this.&auth] |
---|
34 | def credentials |
---|
35 | def requestUser |
---|
36 | // defined as a regular method so its private |
---|
37 | |
---|
38 | /** |
---|
39 | * Authorization closure, which is run before executing any of the REST resource actions |
---|
40 | * It fetches a username/password combination from basic HTTP authentication and checks whether |
---|
41 | * that is an active (nimble) account |
---|
42 | * @return |
---|
43 | */ |
---|
44 | def auth() { |
---|
45 | credentials = BasicAuthentication.credentialsFromRequest(request) |
---|
46 | requestUser = authService.authUser(credentials.u,credentials.p) |
---|
47 | if(!requestUser) { |
---|
48 | response.sendError(403) |
---|
49 | return false |
---|
50 | } |
---|
51 | else { |
---|
52 | return true |
---|
53 | } |
---|
54 | } |
---|
55 | |
---|
56 | |
---|
57 | |
---|
58 | /** |
---|
59 | * REST resource for the Simple Assay Module. |
---|
60 | * Provide a list of all studies. |
---|
61 | * |
---|
62 | * |
---|
63 | * @return as JSON object list of members externalStudyID, and title for all studies |
---|
64 | */ |
---|
65 | def getStudies = { |
---|
66 | List studies = [] |
---|
67 | Study.findAllByOwner(requestUser).each { study -> |
---|
68 | studies.push( [ 'externalStudyID': study.code, 'name':study.title ] ) |
---|
69 | } |
---|
70 | render studies as JSON |
---|
71 | } |
---|
72 | |
---|
73 | |
---|
74 | /** |
---|
75 | * REST resource for the Simple Assay Module. |
---|
76 | * Provide a list of all subjects belonging to a study. |
---|
77 | * |
---|
78 | * @param externalStudyID |
---|
79 | * @return as JSON object list of subject names |
---|
80 | */ |
---|
81 | def getSubjects = { |
---|
82 | List subjects = [] |
---|
83 | if( params.externalStudyID ) { |
---|
84 | def id = params.externalStudyID |
---|
85 | def study = Study.find( "from Study as s where s.code=?", [id]) |
---|
86 | if(study) study.subjects.each { subjects.push it.name } |
---|
87 | } |
---|
88 | render subjects as JSON |
---|
89 | } |
---|
90 | |
---|
91 | |
---|
92 | /** |
---|
93 | * REST resource for the Simple Assay Module. |
---|
94 | * Provide a list of all assays for a given study |
---|
95 | * |
---|
96 | * Example call of the getAssays REST resource: http://localhost:8080/gscf/rest/getAssays?externalStudyID=PPSH&moduleURL=http://localhost:8182/sam |
---|
97 | * |
---|
98 | * @param externalStudyID The external study id (code) of the target GSCF Study object |
---|
99 | * @param moduleURL The base URL of the calling dbNP module |
---|
100 | * @return list of assays in the study as JSON object, filtered to only contain assays for the specified module |
---|
101 | */ |
---|
102 | def getAssays = { |
---|
103 | List assays = [] |
---|
104 | if( params.externalStudyID ) { |
---|
105 | def study = Study.find( "from Study as s where s.code=?", [params.externalStudyID]) |
---|
106 | if(study && study.owner == requestUser) study.assays.each{ assay -> |
---|
107 | if (assay.module.url.equals(params.moduleURL)) { |
---|
108 | def map = ['name':assay.name, 'externalAssayID':assay.externalAssayID] |
---|
109 | assays.push( map ) |
---|
110 | } |
---|
111 | } |
---|
112 | } |
---|
113 | render assays as JSON |
---|
114 | } |
---|
115 | |
---|
116 | |
---|
117 | /** |
---|
118 | * REST resource for the Simple Assay Module. |
---|
119 | * Provide all samples of a given Assay. The result is an enriched list with additional informatin on a sample. |
---|
120 | * |
---|
121 | * @param assayID (externalAssayID of some Assay in GSCF) |
---|
122 | * @return list of element of Sample.name x Sample.material x Sample.subject.name x Sample.Event.name x Sample.Event.time |
---|
123 | */ |
---|
124 | def getSamples = { |
---|
125 | def items = [] |
---|
126 | if( params.externalAssayID ) { |
---|
127 | def assay = Assay.find( "from Assay as a where externalAssayID=?",[params.externalAssayID]) |
---|
128 | assay.getSamples().each { sample -> |
---|
129 | def item = [ |
---|
130 | 'name' : sample.name, |
---|
131 | 'material' : sample.material.name, |
---|
132 | 'subject' : sample.parentSubject.name, |
---|
133 | 'event' : sample.parentEvent.template.name, |
---|
134 | 'startTime' : sample.parentEvent.getStartTimeString(), |
---|
135 | 'externalSampleId': sample.externalSampleId |
---|
136 | ] |
---|
137 | items.push item |
---|
138 | } |
---|
139 | } |
---|
140 | render items as JSON |
---|
141 | } |
---|
142 | |
---|
143 | |
---|
144 | |
---|
145 | |
---|
146 | |
---|
147 | /****************************/ |
---|
148 | /** Rest resources for DSP **/ |
---|
149 | /****************************/ |
---|
150 | |
---|
151 | |
---|
152 | /* still not complete!! */ |
---|
153 | |
---|
154 | /** |
---|
155 | * REST resource for DSP. |
---|
156 | * call: gscf/rest/isUser/?username=username&password=password |
---|
157 | * |
---|
158 | * @param String username |
---|
159 | * @param String password |
---|
160 | * @return bool |
---|
161 | */ |
---|
162 | def isUser= { |
---|
163 | def isUser = isVerifiedUser( params ) |
---|
164 | render isUser as JSON |
---|
165 | } |
---|
166 | |
---|
167 | |
---|
168 | /* still not complete!! */ |
---|
169 | /** |
---|
170 | * REST resource for DSP. |
---|
171 | * call: gscf/rest/listStudies/?username=username&password=password |
---|
172 | * |
---|
173 | * @param String username |
---|
174 | * @param String password |
---|
175 | * @return list of studies |
---|
176 | */ |
---|
177 | def listStudies = { |
---|
178 | |
---|
179 | if( !isVerifiedUser( params ) ) { |
---|
180 | render [:] as JSON |
---|
181 | return |
---|
182 | } |
---|
183 | |
---|
184 | List studies = [] |
---|
185 | |
---|
186 | // add code for filtering studies that belong to given user |
---|
187 | // (use Study.findAll( ... ) |
---|
188 | // ... |
---|
189 | Study.list().each { |
---|
190 | def map = ["study_token":it.code, "name":it.name] |
---|
191 | studies.add( map ) |
---|
192 | } |
---|
193 | |
---|
194 | render studies as JSON |
---|
195 | } |
---|
196 | |
---|
197 | |
---|
198 | |
---|
199 | /* still not complete!! */ |
---|
200 | /** |
---|
201 | * REST resource for DSP. |
---|
202 | * call: gscf/rest/getStudy/?username=username&password=password&study_token=studytoken |
---|
203 | * |
---|
204 | * @param String username |
---|
205 | * @param String password |
---|
206 | * @param String study_token |
---|
207 | * @return list of studies |
---|
208 | */ |
---|
209 | def getStudy = { |
---|
210 | |
---|
211 | if( !isVerifiedUser( params ) ) { |
---|
212 | render [:] as JSON |
---|
213 | return |
---|
214 | } |
---|
215 | |
---|
216 | List studyResult = [:] |
---|
217 | def code = params.study_token |
---|
218 | |
---|
219 | def query = "from Study as s where s.code = ?" |
---|
220 | def study = Study.find( query, code ) |
---|
221 | studyResult = [ 'study_token' : study.code, 'name' : study.name ] |
---|
222 | /* still not complete!! |
---|
223 | Add features |
---|
224 | ... study_token:âGHyJeR#gâ, |
---|
225 | ... created: â20/06/2010 22:34:52â, |
---|
226 | ... meta: [ |
---|
227 | ... greenhouse_id: âGH010938.AB.5â, |
---|
228 | ... greenhouse_type: âlean-to, detached, and ridge and gutter connectedâ ] |
---|
229 | */ |
---|
230 | |
---|
231 | render studyResult as JSON |
---|
232 | } |
---|
233 | |
---|
234 | |
---|
235 | |
---|
236 | |
---|
237 | /* still not complete!! */ |
---|
238 | /** |
---|
239 | * REST resource for DSP. |
---|
240 | * call: gscf/rest/listStudySamples/?username=username&password=password&study_token=studytoken |
---|
241 | * |
---|
242 | * @param String user name |
---|
243 | * @param String password |
---|
244 | * @param String a valid GSCF Study.code |
---|
245 | * @return List of pairs; each pair is a map with keys sample_token and name and values Study.code and Sample.name. |
---|
246 | */ |
---|
247 | def listStudySamples = { |
---|
248 | |
---|
249 | if( !isVerifiedUser( params ) ) { |
---|
250 | render [:] as JSON |
---|
251 | return |
---|
252 | } |
---|
253 | |
---|
254 | List samples = [:] |
---|
255 | def code = params.study_token |
---|
256 | def query = "from Samples as s where s.study = ?" |
---|
257 | def study = Study.find( query, code ) |
---|
258 | if(study) study.samples.each { sample -> |
---|
259 | def map = [ sample_token:code, name:sample.name ] |
---|
260 | samples.add( map ) |
---|
261 | } |
---|
262 | |
---|
263 | render samples as JSON |
---|
264 | } |
---|
265 | |
---|
266 | |
---|
267 | |
---|
268 | /* still not complete!! */ |
---|
269 | /** |
---|
270 | * REST resource for DSP. |
---|
271 | * call: getStudySample/?username=me&password=123&study_token=GHyJeR#g&sample_name=âAHVJwRâ) |
---|
272 | * |
---|
273 | * @param String username |
---|
274 | * @param String password |
---|
275 | * @param String study_token |
---|
276 | * @param String sample_name |
---|
277 | * @return list of studies |
---|
278 | */ |
---|
279 | def getStudySample = { |
---|
280 | |
---|
281 | if( !isVerifiedUser( params ) ) { |
---|
282 | render [:] as JSON |
---|
283 | return |
---|
284 | } |
---|
285 | |
---|
286 | List sample = [:] |
---|
287 | def code = params.study_token |
---|
288 | def name = params.sample_name |
---|
289 | |
---|
290 | def query = "from Sample as s where s.name = ? AND s.parentStudy " |
---|
291 | def study = Sample.find( query, name ) |
---|
292 | sample = [ 'study_token' : sample.code, 'name' : sample.name ] |
---|
293 | // samples will have unique identifier strings |
---|
294 | /* still not complete!! |
---|
295 | Add features |
---|
296 | [ |
---|
297 | study_token:âGHyJeR#gâ, |
---|
298 | sample_token:âAHVJwRâ, |
---|
299 | name: âSample SMPL002â, |
---|
300 | created: â25/06/2010 09:14:32â, |
---|
301 | meta: [ subject: âSUB000294-34942.Aâ, subject_bmi: â29.3â, ... study_token:âGHyJeR#gâ, |
---|
302 | ... created: â20/06/2010 22:34:52â, greenhouse_id: âGH010938.AB.5â, |
---|
303 | greenhouse_type: âlean-to, detached, and ridge and gutter connectedâ |
---|
304 | ] |
---|
305 | */ |
---|
306 | render sample as JSON |
---|
307 | } |
---|
308 | |
---|
309 | |
---|
310 | |
---|
311 | |
---|
312 | /* still not complete!! */ |
---|
313 | /** Convenience method for isUser and listStudies. |
---|
314 | * Verify user and password. |
---|
315 | * @param params object with two map keys: (1) 'username', (2) 'password' |
---|
316 | * @param String password |
---|
317 | * @return bool |
---|
318 | */ |
---|
319 | private isVerifiedUser( params ) { |
---|
320 | def isVerified = false |
---|
321 | def user = params?.username |
---|
322 | def pass = params?.password |
---|
323 | |
---|
324 | if( user && pass ) { |
---|
325 | // insert code for verification of user and |
---|
326 | // ... |
---|
327 | isVerified = true |
---|
328 | } |
---|
329 | return isVerified |
---|
330 | } |
---|
331 | |
---|
332 | |
---|
333 | |
---|
334 | /* this is just for testing! */ |
---|
335 | def test = { |
---|
336 | render( dbnp.rest.common.CommunicationManager.getQueryResultWithOperator("Insulin",">",200) ) |
---|
337 | } |
---|
338 | } |
---|