source: trunk/src/groovy/dbnp/rest/CommunicationManager.groovy @ 608

Last change on this file since 608 was 608, checked in by jahn, 10 years ago

Added methods supplying SAM urls.

File size: 5.6 KB
Line 
1package dbnp.rest
2
3import java.util.Map 
4import java.util.List
5import java.util.HashMap
6import grails.converters.JSON
7import org.codehaus.groovy.grails.web.json.*
8import dbnp.studycapturing.TemplateFieldListItem
9import dbnp.studycapturing.Template
10import dbnp.data.CleanDataLayer
11import dbnp.studycapturing.Study 
12import dbnp.studycapturing.Assay
13
14
15
16
17/**  CommunicationManager
18 *
19 *   This class implements a REST client to fetch data from the Simple Assay Module (SAM).
20 *   The communicatino manager provides methods for accessing each resources.
21 *   Every REST resource corresponds to exactly one method in this class that makes
22 *   the communication with the resource available.
23 *
24 *   For instance, the getSearchable() method calls the getMeasurements resource of the SAM
25 *   by passing arguments to it and returning the result of the calling that resource.
26 */
27
28
29class CommunicationManager implements CleanDataLayer {
30
31   
32    /** ServerULR contains a string that represents the URL of the
33     *  rest resources that this communication manager connects to.
34     */ 
35
36    def static ServerURL = "http://localhost:8182/ClinicalChemistry"
37    //def static ServerURL = "http://nbx5.nugo.org:8182/ClinicalChemistry"
38    def static RestServerURL = ServerURL + "/rest"
39
40
41    /* Methods implemented for CleanDataLayer */
42
43
44
45
46    /**
47     * Get the names of all quantitative features that are available for a certain assay
48     * @param assayID the module internal ID for the assay
49     * @return
50     */
51    public String[] getFeaturesQuantitative(long assayID) {
52         return new String [20]
53    }
54   
55
56
57    /**
58     * Get the data for a quantitative feature for a certain assay for a certain set of samples
59     * @param feature
60     * @param assayID
61     * @param sampleIDs
62     * @return Map
63     */
64    public Map getDataQuantitative(String feature, long assayID, String[] sampleIDs) {
65         return new HashMap() 
66    }
67
68
69    /**
70     * Testing REST. Remove when connection to nbx5 is established.
71     *
72     * @return list of ClinicalFloatData
73     */
74    public Object getFeatures() {
75    //    return  request( "features" )
76        return  getStudiesForKeyword("ldl")
77    }
78
79
80    /**
81     * For a string for the searchable plugin.
82     * This works for one keyeword, but protection should be built in using
83     * the methods that searchable uses for building query strings.
84     *
85     * @return list of ClinicalFloatData
86     */
87    private String getSearchable( keyword ) {
88        return  "?submit=Query&q=" + keyword
89    }
90
91
92    /**
93     * Get all meassurements that contain a given keyword as feature.
94     *
95     * @param  keyword, the keyword used
96     * @return list of ClinicalFloatData
97     */
98    public String getStudiesForKeyword( String keyword ) {
99        def resource = "getMeasurementsForValue"
100        request( resource + getSearchable(keyword) )
101    }
102
103
104    /**
105     * Get all meassurements that contain a given keyword as feature.
106     *
107     * @param  keyword, the keyword used
108     * @return list of ClinicalFloatData
109     */
110    public Object getMeasurementsResource( String keyword ) {
111        def url = new URL( RestServerURL + "/" + getSearchable(keyword) )
112        return  JSON.parse( url.newReader() )
113    }
114
115
116
117    /** Send a request for the REST resource to the server and deliver the
118     *  resulting JSON object. (This is just a convenience method.)
119     *
120     *  @param resource: the name of the resource including parameters
121     *  @return JSON object
122     */
123    private Object request( String resource ) { 
124        def url = new URL( RestServerURL + "/" + resource )
125        return  JSON.parse( url.newReader() )
126    }
127
128
129
130    /** Send a request for the REST resource to SAM and deliver the
131     *  results for the Query controller.
132     *
133     *  @param  compound        a SAM compound, e.g., "ldl" or "weight"
134     *  @param  value           a SAM value of a measurement, e.g. "20" (without unit, please)
135     *  @param  opperator       a SAM operator, i.e., "", "=", "<", or ">"
136     *  @return List of matching studies
137     */
138    public List<Study> getSAMStudies( String compound, String value, String opperator ) {
139         return [] 
140    }
141
142
143
144
145    /**
146     * Get the URL for importing an assay within the GSCF.
147     * This is not a REST method! It only creates a rest resource and returns it's url.
148     *
149     * @params assay
150     * @return list of ClinicalFloatData
151     */
152    public Object getAssayImportURL( assay ) {
153        return new URL( ServerURL + '/importer/test?externalAssayID=' + assay.externalAssayID )
154    }
155
156
157
158
159    /* Methods for acessing URLs in SAM */
160
161
162    /**
163     * Get the URL for importing an assay within the GSCF.
164     * This is not a REST method! It only creates a rest resource and returns it's url.
165     *
166     * @params assay
167     * @return URL
168     */
169    public Object getAssayShowURL( assay ) {
170        return new URL( ServerURL + '/simpleAssay/show/assay?externalAssayID=' + assay.externalAssayID )
171    }
172
173
174    /**
175     * Get the URL for importing an assay within the GSCF.
176     * This is not a REST method! It only creates a rest resource and returns it's url.
177     *
178     * @params assay
179     * @return URL
180     */
181    public Object getAssayEditURL( assay ) {
182        return new URL( ServerURL + '/simpleAssay/edit/assay?externalAssayID=' + assay.externalAssayID )
183    }
184
185
186    /**
187     * Get the URL for importing an assay within the GSCF.
188     * This is not a REST method! It only creates a rest resource and returns it's url.
189     *
190     * @params URL
191     * @return list of ClinicalFloatData
192     */
193    public Object getMeasurementTypesURL() {
194        return new URL( ServerURL + '/simpleAssayMeasurementType/list/nil?externalAssayID=' + assay.externalAssayID )
195    }
196
197
198}
Note: See TracBrowser for help on using the repository browser.