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 | |
---|
13 | |
---|
14 | |
---|
15 | |
---|
16 | /** CommunicationManager |
---|
17 | * |
---|
18 | * This class implements a REST client to fetch data from the Simple Assay Module (SAM). |
---|
19 | * The communicatino manager provides methods for accessing each resources. |
---|
20 | * Every REST resource corresponds to exactly one method in this class that makes |
---|
21 | * the communication with the resource available. |
---|
22 | * |
---|
23 | * For instance, the getSearchable() method calls the getMeasurements resource of the SAM |
---|
24 | * by passing arguments to it and returning the result of the calling that resource. |
---|
25 | */ |
---|
26 | |
---|
27 | |
---|
28 | class CommunicationManager implements CleanDataLayer { |
---|
29 | |
---|
30 | |
---|
31 | /** ServerULR contains a string that represents the URL of the |
---|
32 | * rest resources that this communication manager connects to. |
---|
33 | */ |
---|
34 | def static ServerURL = "http://nbx5.nugo.org:8182/ClinicalChemistry/rest"; |
---|
35 | //def static ServerURL = "http://localhost:8080/gscf/rest"; |
---|
36 | |
---|
37 | |
---|
38 | /* Methods implemented for CleanDataLayer */ |
---|
39 | |
---|
40 | |
---|
41 | /** |
---|
42 | * Get the names of all quantitative features that are available for a certain assay |
---|
43 | * @param assayID the module internal ID for the assay |
---|
44 | * @return |
---|
45 | */ |
---|
46 | public String[] getFeaturesQuantitative(long assayID) { |
---|
47 | return new String [20]; |
---|
48 | } |
---|
49 | |
---|
50 | |
---|
51 | |
---|
52 | /** |
---|
53 | * Get the data for a quantitative feature for a certain assay for a certain set of samples |
---|
54 | * @param feature |
---|
55 | * @param assayID |
---|
56 | * @param sampleIDs |
---|
57 | * @return Map |
---|
58 | */ |
---|
59 | public Map getDataQuantitative(String feature, long assayID, String[] sampleIDs) { |
---|
60 | return new HashMap(); |
---|
61 | } |
---|
62 | |
---|
63 | |
---|
64 | /** |
---|
65 | * Testing REST. Remove when connection to nbx5 is established. |
---|
66 | * |
---|
67 | * @return list of ClinicalFloatData |
---|
68 | */ |
---|
69 | public Object getFeatures() { |
---|
70 | // return request( "features" ) |
---|
71 | return getStudiesForKeyword("ldl") |
---|
72 | } |
---|
73 | |
---|
74 | |
---|
75 | /** |
---|
76 | * For a string for the searchable plugin. |
---|
77 | * This works for one keyeword, but protection should be built in using |
---|
78 | * the methods that searchable uses for building query strings. |
---|
79 | * |
---|
80 | * @return list of ClinicalFloatData |
---|
81 | */ |
---|
82 | private String getSearchable( keyword ) { |
---|
83 | return "?submit=Query&q=" + keyword |
---|
84 | } |
---|
85 | |
---|
86 | |
---|
87 | /** |
---|
88 | * Get all meassurements that contain a given keyword as feature. |
---|
89 | * |
---|
90 | * @param keyword, the keyword used |
---|
91 | * @return list of ClinicalFloatData |
---|
92 | */ |
---|
93 | public String getStudiesForKeyword( String keyword ) { |
---|
94 | def resource = "getMeasurementsForValue" |
---|
95 | request( resource + getSearchable(keyword) ) |
---|
96 | } |
---|
97 | |
---|
98 | |
---|
99 | /** |
---|
100 | * Get all meassurements that contain a given keyword as feature. |
---|
101 | * |
---|
102 | * @param keyword, the keyword used |
---|
103 | * @return list of ClinicalFloatData |
---|
104 | */ |
---|
105 | public Object getMeasurementsResource( String keyword ) { |
---|
106 | def url = new URL( ServerURL + "/" + getSearchable(keyword) ) |
---|
107 | return JSON.parse( url.newReader() ) |
---|
108 | } |
---|
109 | |
---|
110 | |
---|
111 | |
---|
112 | /** Send a request for the REST resource to the server and deliver the |
---|
113 | * resulting JSON object. (This is just a convenience method.) |
---|
114 | * |
---|
115 | * @param resource: the name of the resource including parameters |
---|
116 | * @return JSON object |
---|
117 | */ |
---|
118 | private Object request( String resource ) { |
---|
119 | def url = new URL( ServerURL + "/" + resource ); |
---|
120 | return JSON.parse( url.newReader() ); |
---|
121 | } |
---|
122 | |
---|
123 | |
---|
124 | |
---|
125 | /** Send a request for the REST resource to SAM and deliver the |
---|
126 | * results for the Query controller. |
---|
127 | * |
---|
128 | * @param compound a SAM compound, e.g., "ldl" or "weight" |
---|
129 | * @param value a SAM value of a measurement, e.g. "20" (without unit, please) |
---|
130 | * @param opperator a SAM operator, i.e., "", "=", "<", or ">" |
---|
131 | * @return List of matching studies |
---|
132 | */ |
---|
133 | public List<Study> getSAMStudies( String compound, String value, String opperator ) { |
---|
134 | return [] |
---|
135 | } |
---|
136 | |
---|
137 | |
---|
138 | |
---|
139 | |
---|
140 | |
---|
141 | |
---|
142 | |
---|
143 | |
---|
144 | /* To Do for querying |
---|
145 | public void getMeasurementsForValueResource() { |
---|
146 | } |
---|
147 | |
---|
148 | |
---|
149 | public void getMeasurementsForRangeResource() { |
---|
150 | } |
---|
151 | |
---|
152 | |
---|
153 | public void getDataSimple() { |
---|
154 | } |
---|
155 | */ |
---|
156 | |
---|
157 | |
---|
158 | |
---|
159 | |
---|
160 | |
---|
161 | } |
---|