root/trunk/grails-app/controllers/RestController.groovy @ 549

Revision 549, 3.8 KB (checked in by jahn, 4 years ago)

Remvoed bug in getStudy(). If a non-valid id is passed as argument, we don't get an exception anymore.

Line 
1/**
2 * ModuleCommunicationController Controler
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
14 * @since       20100601
15 *
16 */
17
18import data.*
19import dbnp.studycapturing.Study
20import dbnp.studycapturing.Assay
21import grails.converters.*
22import org.codehaus.groovy.grails.web.json.*
23
24
25
26class RestController {
27
28
29        /* REST resources for Simple Assay Module (SAM) */
30
31
32        /**
33        * REST resource for the Simple Assay Module.
34        * Provide a list of all studies.
35        *
36        *
37        * Examlpe call of the getAssays REST resource: http://localhost:8080/gscf/rest/getAssays/json?externalStudyID=1
38        *
39        * @return as JSON object list of members externalStudyID, and title for all studies
40        */
41        def getStudies = {
42                List studies = []
43                Study.list().each { study ->
44                    studies.push( [ 'externalStudyID': study.externalStudyID, 'name':study.title ] )
45                }
46                render studies as JSON
47        }
48
49
50        /**
51        * REST resource for the Simple Assay Module.
52        * Provide a list of all subjects belonging to a study.
53        *
54        * Examlpe call of the getAssays REST resource: http://localhost:8080/gscf/rest/getSubjects/json?externalStudyID=1
55        *
56        * @param  externalStudyID
57        * @return as JSON object list of subject names
58        */
59        def getSubjects = {
60                List subjects = []
61                if( params.externalStudyID ) {
62                        def id = Long.parseLong(params.externalStudyID)
63                        def study = Study.find( "from Study as s where s.externalStudyID=?", [id])
64                        if(study) study.subjects.each { subjects.push it.name }
65                }
66                render subjects as JSON
67        }
68
69
70        /**
71        * REST resource for the Simple Assay Module.
72        * Provide a list of all assays for a given study
73        *
74        * Example for calling this resource: http://localhost:8080/gscf/rest/getAssays/json?externalStudyID=2
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 id = Long.parseLong(params.externalStudyID)
83                        def study = Study.find( "from Study as s where s.externalStudyID=?", [id])
84                        if(study) study.assays.each{ assay -> assays.push assay.externalAssayID }
85                }
86                render assays as JSON
87        }
88
89
90        /**
91        * REST resource for the Simple Assay Module.
92        * Provide all samples of a given Assay. The result is an enriched list with additional informatin on a sample.
93        *
94        * Example for calling this resource: http://localhost:8080/gscf/rest/getAssays/json?externalStudyID=2
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                        Assay.find( "from Assay as a where externalAssayID=?",[id]).getSamples().each { sample ->
104                                def item = [
105                                        'name'            : sample.name,
106                                        'material'        : sample.material.name,
107                                        'subject'         : sample.parentSubject.name,
108                                        //'event'         : sample.parentEvent.name,  // get the freaking name
109                                        'startTime'       : sample.parentEvent.startTime
110                                ]
111                                items.push item
112                        }
113                }
114                render items as JSON
115        }
116
117}
Note: See TracBrowser for help on using the browser.