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

Last change on this file since 631 was 631, checked in by jahn, 11 years ago

Cleaned up CommunicationManager? and put it in uniform use and package
structure with the dbnp.rest used in the Simple Assay Module.

File size: 3.1 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
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.code, '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        * @param  externalStudyID
55        * @return as JSON object list of subject names
56        */
57        def getSubjects = {
58                List subjects = [] 
59                if( params.externalStudyID ) {
60                        def id = params.externalStudyID
61                        def study = Study.find( "from Study as s where s.code=?", [id])
62                        if(study) study.subjects.each { subjects.push it.name }
63                }
64                render subjects as JSON
65        }
66
67
68        /**
69        * REST resource for the Simple Assay Module.
70        * Provide a list of all assays for a given study
71        *
72        * @param  externalStudyID
73        * @return list of assays as JSON object
74        */
75        def getAssays = {
76                List assays = [] 
77                if( params.externalStudyID ) {
78                        def study = Study.find( "from Study as s where s.code=?", [params.externalStudyID])
79                        if(study) study.assays.each{ assay -> assays.push assay.externalAssayID }
80                }
81                render assays as JSON
82        }
83
84
85        /**
86        * REST resource for the Simple Assay Module.
87        * Provide all samples of a given Assay. The result is an enriched list with additional informatin on a sample.
88        *
89        * @param  assayID (externalAssayID of some Assay in GSCF)
90        * @return list of element of  Sample.name x Sample.material x Sample.subject.name x Sample.Event.name x Sample.Event.time
91        */
92        def getSamples = {
93                def items = []
94                if( params.externalAssayID ) {
95                        def id = Long.parseLong(params.externalAssayID)
96                        Assay.findAll().each{ println it }
97                        Assay.find( "from Assay as a where externalAssayID=?",[id]).getSamples().each { sample ->
98                                def item = [ 
99                                        'name'          : sample.name,
100                                        'material'      : sample.material.name,
101                                        'subject'       : sample.parentSubject.name,
102                                        'event'         : sample.parentEvent.template.name,
103                                        'startTime'     : sample.parentEvent.getDurationString()
104                                ] 
105                                items.push item
106                        }
107                }
108                render items as JSON
109        }
110
111}
Note: See TracBrowser for help on using the repository browser.