1 | package dbnp.rest |
---|
2 | |
---|
3 | import java.util.Map |
---|
4 | import java.util.List |
---|
5 | import java.util.HashMap |
---|
6 | import grails.converters.JSON |
---|
7 | import org.codehaus.groovy.grails.web.json.* |
---|
8 | import dbnp.studycapturing.TemplateFieldListItem |
---|
9 | import dbnp.studycapturing.Template |
---|
10 | import dbnp.data.CleanDataLayer |
---|
11 | import dbnp.studycapturing.Study |
---|
12 | import 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 | |
---|
29 | class 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/sam" |
---|
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 | /* Methods for accessing URLs in SAM */ |
---|
147 | |
---|
148 | |
---|
149 | /** |
---|
150 | * Get the URL for importing an assay from SAM. |
---|
151 | * This is not a REST method! It only creates a rest resource and returns it's url. |
---|
152 | * |
---|
153 | * @params assay |
---|
154 | * @return URL |
---|
155 | */ |
---|
156 | public Object getAssayImportURL( assay ) { |
---|
157 | return new URL( ServerURL + '/importer/pages/assay?externalAssayID=' + assay.externalAssayID ) |
---|
158 | } |
---|
159 | |
---|
160 | |
---|
161 | /** |
---|
162 | * Get the URL for showing an assay in SAM. |
---|
163 | * This is not a REST method! It only creates a rest resource and returns it's url. |
---|
164 | * |
---|
165 | * @params assay |
---|
166 | * @return URL |
---|
167 | */ |
---|
168 | public Object getAssayShowURL( assay ) { |
---|
169 | return new URL( ServerURL + '/simpleAssay/show/assay?externalAssayID=' + assay.externalAssayID ) |
---|
170 | } |
---|
171 | |
---|
172 | |
---|
173 | /** |
---|
174 | * Get the URL for editing an assay in SAM. |
---|
175 | * This is not a REST method! It only creates a rest resource and returns it's url. |
---|
176 | * |
---|
177 | * @params assay |
---|
178 | * @return URL |
---|
179 | */ |
---|
180 | public Object getAssayEditURL( assay ) { |
---|
181 | return new URL( ServerURL + '/simpleAssay/edit/assay?externalAssayID=' + assay.externalAssayID ) |
---|
182 | } |
---|
183 | |
---|
184 | |
---|
185 | /** |
---|
186 | * Get the URL for showing a measurement in SAM. |
---|
187 | * This is not a REST method! It only creates a rest resource and returns it's url. |
---|
188 | * |
---|
189 | * @params URL |
---|
190 | * @return list of ClinicalFloatData |
---|
191 | */ |
---|
192 | public Object getMeasurementTypesURL() { |
---|
193 | return new URL( ServerURL + '/simpleAssayMeasurementType/list/nil?externalAssayID=' + assay.externalAssayID ) |
---|
194 | } |
---|
195 | |
---|
196 | |
---|
197 | } |
---|