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 data.* |
---|
19 | import dbnp.studycapturing.Study |
---|
20 | import dbnp.studycapturing.Assay |
---|
21 | import grails.converters.* |
---|
22 | import org.codehaus.groovy.grails.web.json.* |
---|
23 | |
---|
24 | |
---|
25 | |
---|
26 | class RestController { |
---|
27 | |
---|
28 | |
---|
29 | |
---|
30 | /**************************************************/ |
---|
31 | /** Rest resources for Simple Assay Module (SAM) **/ |
---|
32 | /**************************************************/ |
---|
33 | |
---|
34 | |
---|
35 | |
---|
36 | /** |
---|
37 | * REST resource for the Simple Assay Module. |
---|
38 | * Provide a list of all studies. |
---|
39 | * |
---|
40 | * |
---|
41 | * Examlpe call of the getAssays REST resource: http://localhost:8080/gscf/rest/getAssays/json?externalStudyID=1 |
---|
42 | * |
---|
43 | * @return as JSON object list of members externalStudyID, and title for all studies |
---|
44 | */ |
---|
45 | def getStudies = { |
---|
46 | List studies = [] |
---|
47 | Study.list().each { study -> |
---|
48 | studies.push( [ 'externalStudyID': study.code, 'name':study.title ] ) |
---|
49 | } |
---|
50 | render studies as JSON |
---|
51 | } |
---|
52 | |
---|
53 | |
---|
54 | /** |
---|
55 | * REST resource for the Simple Assay Module. |
---|
56 | * Provide a list of all subjects belonging to a study. |
---|
57 | * |
---|
58 | * @param externalStudyID |
---|
59 | * @return as JSON object list of subject names |
---|
60 | */ |
---|
61 | def getSubjects = { |
---|
62 | List subjects = [] |
---|
63 | if( params.externalStudyID ) { |
---|
64 | def id = params.externalStudyID |
---|
65 | def study = Study.find( "from Study as s where s.code=?", [id]) |
---|
66 | if(study) study.subjects.each { subjects.push it.name } |
---|
67 | } |
---|
68 | render subjects as JSON |
---|
69 | } |
---|
70 | |
---|
71 | |
---|
72 | /** |
---|
73 | * REST resource for the Simple Assay Module. |
---|
74 | * Provide a list of all assays for a given study |
---|
75 | * |
---|
76 | * @param externalStudyID |
---|
77 | * @return list of assays as JSON object |
---|
78 | */ |
---|
79 | def getAssays = { |
---|
80 | List assays = [] |
---|
81 | if( params.externalStudyID ) { |
---|
82 | def study = Study.find( "from Study as s where s.code=?", [params.externalStudyID]) |
---|
83 | if(study) study.assays.each{ assay -> |
---|
84 | def map = ['name':assay.name, 'externalAssayID':assay.externalAssayID] |
---|
85 | assays.push( map ) |
---|
86 | } |
---|
87 | } |
---|
88 | render assays as JSON |
---|
89 | } |
---|
90 | |
---|
91 | |
---|
92 | /** |
---|
93 | * REST resource for the Simple Assay Module. |
---|
94 | * Provide all samples of a given Assay. The result is an enriched list with additional informatin on a sample. |
---|
95 | * |
---|
96 | * @param assayID (externalAssayID of some Assay in GSCF) |
---|
97 | * @return list of element of Sample.name x Sample.material x Sample.subject.name x Sample.Event.name x Sample.Event.time |
---|
98 | */ |
---|
99 | def getSamples = { |
---|
100 | def items = [] |
---|
101 | if( params.externalAssayID ) { |
---|
102 | def id = Long.parseLong(params.externalAssayID) |
---|
103 | def assay = Assay.find( "from Assay as a where externalAssayID=?",[id]) |
---|
104 | assay.getSamples().each { sample -> |
---|
105 | def item = [ |
---|
106 | 'name' : sample.name, |
---|
107 | 'material' : sample.material.name, |
---|
108 | 'subject' : sample.parentSubject.name, |
---|
109 | 'event' : sample.parentEvent.template.name, |
---|
110 | 'startTime' : sample.parentEvent.getStartTimeString(), |
---|
111 | 'externalSampleId': sample.externalSampleId |
---|
112 | ] |
---|
113 | items.push item |
---|
114 | } |
---|
115 | } |
---|
116 | render items as JSON |
---|
117 | } |
---|
118 | |
---|
119 | |
---|
120 | |
---|
121 | |
---|
122 | |
---|
123 | /****************************/ |
---|
124 | /** Rest resources for DSP **/ |
---|
125 | /****************************/ |
---|
126 | |
---|
127 | |
---|
128 | /* still not complete!! */ |
---|
129 | |
---|
130 | /** |
---|
131 | * REST resource for DSP. |
---|
132 | * call: gscf/rest/isUser/?username=username&password=password |
---|
133 | * |
---|
134 | * @param String username |
---|
135 | * @param String password |
---|
136 | * @return bool |
---|
137 | */ |
---|
138 | def isUser= { |
---|
139 | def isUser = isVerifiedUser( params ) |
---|
140 | render isUser as JSON |
---|
141 | } |
---|
142 | |
---|
143 | |
---|
144 | /* still not complete!! */ |
---|
145 | /** |
---|
146 | * REST resource for DSP. |
---|
147 | * call: gscf/rest/listStudies/?username=username&password=password |
---|
148 | * |
---|
149 | * @param String username |
---|
150 | * @param String password |
---|
151 | * @return list of studies |
---|
152 | */ |
---|
153 | def listStudies = { |
---|
154 | |
---|
155 | if( !isVerifiedUser( params ) ) { |
---|
156 | render [:] as JSON |
---|
157 | return |
---|
158 | } |
---|
159 | |
---|
160 | List studies = [] |
---|
161 | |
---|
162 | // add code for filtering studies that belong to given user |
---|
163 | // (use Study.findAll( ... ) |
---|
164 | // ... |
---|
165 | Study.list().each { |
---|
166 | def map = ["study_token":it.code, "name":it.name] |
---|
167 | studies.add( map ) |
---|
168 | } |
---|
169 | |
---|
170 | render studies as JSON |
---|
171 | } |
---|
172 | |
---|
173 | |
---|
174 | |
---|
175 | /* still not complete!! */ |
---|
176 | /** |
---|
177 | * REST resource for DSP. |
---|
178 | * call: gscf/rest/getStudy/?username=username&password=password&study_token=studytoken |
---|
179 | * |
---|
180 | * @param String username |
---|
181 | * @param String password |
---|
182 | * @param String study_token |
---|
183 | * @return list of studies |
---|
184 | */ |
---|
185 | def getStudy = { |
---|
186 | |
---|
187 | if( !isVerifiedUser( params ) ) { |
---|
188 | render [:] as JSON |
---|
189 | return |
---|
190 | } |
---|
191 | |
---|
192 | List studyResult = [:] |
---|
193 | def code = params.study_token |
---|
194 | |
---|
195 | def query = "from Study as s where s.code = ?" |
---|
196 | def study = Study.find( query, code ) |
---|
197 | studyResult = [ 'study_token' : study.code, 'name' : study.name ] |
---|
198 | /* still not complete!! |
---|
199 | Add features |
---|
200 | ... study_token:âGHyJeR#gâ, |
---|
201 | ... created: â20/06/2010 22:34:52â, |
---|
202 | ... meta: [ |
---|
203 | ... greenhouse_id: âGH010938.AB.5â, |
---|
204 | ... greenhouse_type: âlean-to, detached, and ridge and gutter connectedâ ] |
---|
205 | */ |
---|
206 | |
---|
207 | render studyResult as JSON |
---|
208 | } |
---|
209 | |
---|
210 | |
---|
211 | |
---|
212 | |
---|
213 | /* still not complete!! */ |
---|
214 | /** |
---|
215 | * REST resource for DSP. |
---|
216 | * call: gscf/rest/listStudySamples/?username=username&password=password&study_token=studytoken |
---|
217 | * |
---|
218 | * @param String user name |
---|
219 | * @param String password |
---|
220 | * @param String a valid GSCF Study.code |
---|
221 | * @return List of pairs; each pair is a map with keys sample_token and name and values Study.code and Sample.name. |
---|
222 | */ |
---|
223 | def listStudySamples = { |
---|
224 | |
---|
225 | if( !isVerifiedUser( params ) ) { |
---|
226 | render [:] as JSON |
---|
227 | return |
---|
228 | } |
---|
229 | |
---|
230 | List samples = [:] |
---|
231 | def code = params.study_token |
---|
232 | def query = "from Samples as s where s.study = ?" |
---|
233 | def study = Study.find( query, code ) |
---|
234 | if(study) study.samples.each { sample -> |
---|
235 | def map = [ sample_token:code, name:sample.name ] |
---|
236 | samples.add( map ) |
---|
237 | } |
---|
238 | |
---|
239 | render samples as JSON |
---|
240 | } |
---|
241 | |
---|
242 | |
---|
243 | |
---|
244 | /* still not complete!! */ |
---|
245 | /** |
---|
246 | * REST resource for DSP. |
---|
247 | * call: getStudySample/?username=me&password=123&study_token=GHyJeR#g&sample_name=âAHVJwRâ) |
---|
248 | * |
---|
249 | * @param String username |
---|
250 | * @param String password |
---|
251 | * @param String study_token |
---|
252 | * @param String sample_name |
---|
253 | * @return list of studies |
---|
254 | */ |
---|
255 | def getStudySample = { |
---|
256 | |
---|
257 | if( !isVerifiedUser( params ) ) { |
---|
258 | render [:] as JSON |
---|
259 | return |
---|
260 | } |
---|
261 | |
---|
262 | List sample = [:] |
---|
263 | def code = params.study_token |
---|
264 | def name = params.sample_name |
---|
265 | |
---|
266 | def query = "from Sample as s where s.name = ? AND s.parentStudy " |
---|
267 | def study = Sample.find( query, name ) |
---|
268 | sample = [ 'study_token' : sample.code, 'name' : sample.name ] |
---|
269 | // samples will have unique identifier strings |
---|
270 | /* still not complete!! |
---|
271 | Add features |
---|
272 | [ |
---|
273 | study_token:âGHyJeR#gâ, |
---|
274 | sample_token:âAHVJwRâ, |
---|
275 | name: âSample SMPL002â, |
---|
276 | created: â25/06/2010 09:14:32â, |
---|
277 | meta: [ subject: âSUB000294-34942.Aâ, subject_bmi: â29.3â, ... study_token:âGHyJeR#gâ, |
---|
278 | ... created: â20/06/2010 22:34:52â, greenhouse_id: âGH010938.AB.5â, |
---|
279 | greenhouse_type: âlean-to, detached, and ridge and gutter connectedâ |
---|
280 | ] |
---|
281 | */ |
---|
282 | render sample as JSON |
---|
283 | } |
---|
284 | |
---|
285 | |
---|
286 | |
---|
287 | |
---|
288 | /* still not complete!! */ |
---|
289 | /** Convenience method for isUser and listStudies. |
---|
290 | * Verify user and password. |
---|
291 | * @param params object with two map keys: (1) 'username', (2) 'password' |
---|
292 | * @param String password |
---|
293 | * @return bool |
---|
294 | */ |
---|
295 | private isVerifiedUser( params ) { |
---|
296 | def isVerified = false |
---|
297 | def user = params?.username |
---|
298 | def pass = params?.password |
---|
299 | |
---|
300 | if( user && pass ) { |
---|
301 | // insert code for verification of user and |
---|
302 | // ... |
---|
303 | isVerified = true |
---|
304 | } |
---|
305 | return isVerified |
---|
306 | } |
---|
307 | |
---|
308 | |
---|
309 | |
---|
310 | /* this is just for testing! */ |
---|
311 | def test = { |
---|
312 | render( dbnp.rest.common.CommunicationManager.getQueryResultWithOperator("Insulin",">",200) ) |
---|
313 | } |
---|
314 | } |
---|