source: trunk/grails-app/controllers/RestController.groovy @ 835

Last change on this file since 835 was 835, checked in by keesvb, 12 years ago

added metabolomics assay examples, cleaned up RestController?, changed assay show link for SAM

File size: 5.6 KB
Line 
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
18import dbnp.studycapturing.Study
19import dbnp.studycapturing.Assay
20import grails.converters.*
21import nl.metabolomicscentre.dsp.http.BasicAuthentication
22
23
24class RestController {
25
26
27
28       /**************************************************/
29      /** Rest resources for Simple Assay Module (SAM) **/
30     /**************************************************/
31
32        def authService
33        def beforeInterceptor = [action:this.&auth,except:["isUser"]]
34        def credentials
35        def requestUser
36
37        /**
38         * Authorization closure, which is run before executing any of the REST resource actions
39         * It fetches a username/password combination from basic HTTP authentication and checks whether
40         * that is an active (nimble) account
41         * @return
42         */
43        private def auth() {
44            credentials = BasicAuthentication.credentialsFromRequest(request)
45                requestUser = authService.authUser(credentials.u,credentials.p)
46                if(!requestUser) {
47                    response.sendError(403)
48                return false
49            }
50                else {
51                        return true
52                }
53        }
54
55        /**
56        * REST resource for data modules.
57        * Username and password should be supplied via HTTP Basic Authentication.
58        * Determines whether the given user/password combination is a valid GSCF account.
59        *
60        * @return bool True when user/password is a valid GSCF account, false otherwise.
61        */
62        def isUser= {
63                boolean isUser
64                def reqUser = authService.authUser(credentials.u,credentials.p)
65                if (reqUser) {
66                        isUser = true
67                }
68                else {
69                        isUser = false
70                }
71                render isUser as JSON
72        }
73
74        /**
75        * REST resource for data modules.
76        * Username and password should be supplied via HTTP Basic Authentication.
77        * Provide a list of all studies owned by the supplied user.
78        *
79        * @return JSON object list containing 'externalStudyID', and 'name' (title) for each study
80        */
81        def getStudies = {
82                List studies = [] 
83                Study.findAllByOwner(requestUser).each { study ->
84                        studies.push( [ 'externalStudyID': study.code, 'name':study.title ] )
85                }
86                render studies as JSON
87        }
88
89
90        /**
91        * REST resource for data modules.
92        * Username and password should be supplied via HTTP Basic Authentication.
93        * Provide a list of all subjects belonging to a study.
94        *
95        * @param externalStudyID String The external study id (code) of the target GSCF Study object
96        * @return JSON object list of subject names
97        */
98        def getSubjects = {
99                List subjects = [] 
100                if( params.externalStudyID ) {
101                        def id = params.externalStudyID
102                        def study = Study.find( "from Study as s where s.code=?", [id])
103                        if(study) study.subjects.each { subjects.push it.name }
104                }
105                render subjects as JSON
106        }
107
108
109        /**
110        * REST resource for data modules.
111        * Username and password should be supplied via HTTP Basic Authentication.
112        * Provide a list of all assays for a given study
113        *
114        * Example call of the getAssays REST resource: http://localhost:8080/gscf/rest/getAssays?externalStudyID=PPSH&moduleURL=http://localhost:8182/sam
115        *
116        * @param externalStudyID String The external study id (code) of the target GSCF Study object
117        * @param moduleURL String The base URL of the calling dbNP module
118        * @return list of assays in the study as JSON object list, filtered to only contain assays for the specified module, with 'externalAssayID' and 'name' for each assay
119        */
120        def getAssays = {
121                List assays = [] 
122                if( params.externalStudyID ) {
123                        def study = Study.find( "from Study as s where s.code=?", [params.externalStudyID])
124                        if(study && study.owner == requestUser) study.assays.each{ assay ->
125                                if (assay.module.url.equals(params.moduleURL)) {
126                                def map = ['name':assay.name, 'externalAssayID':assay.externalAssayID]
127                                        assays.push( map )
128                                }
129                        }
130                }
131                render assays as JSON
132        }
133
134
135        /**
136        * REST resource for data modules.
137        * Username and password should be supplied via HTTP Basic Authentication.
138        * Provide all samples of a given Assay. The result is an enriched list with additional information for each sample.
139        *
140        * @param externalAssayID String (externalAssayID of some Assay in GSCF)
141        * @return As a JSON object list, for each sample in that assay:
142        * @return 'name' (Sample name, which is unique)
143        * @return 'material' (Sample material)
144        * @return 'subject' (The name of the subject from which the sample was taken)
145        * @return 'event' (the name of the template of the SamplingEvent describing the sampling)
146        * @return 'startTime' (the time the sample was taken relative to the start of the study, as a string)
147        */
148        def getSamples = {
149                def items = []
150                if( params.externalAssayID ) {
151                        def assay = Assay.find( "from Assay as a where externalAssayID=?",[params.externalAssayID])
152                        assay.getSamples().each { sample ->
153                                def item = [ 
154                                        'name'                : sample.name,
155                                        'material'            : sample.material.name,
156                                        'subject'             : sample.parentSubject.name,
157                                        'event'               : sample.parentEvent.template.name,
158                                        'startTime'           : sample.parentEvent.getStartTimeString()
159                                ]
160                                items.push item
161                        }
162                }
163                render items as JSON
164        }
165
166
167    /* this is just for testing! */
168    /*def test = {
169                render( dbnp.rest.common.CommunicationManager.getQueryResultWithOperator("Insulin",">",200) )
170    }*/
171}
Note: See TracBrowser for help on using the repository browser.