1 package jalview.fts.service.threedbeacons;
4 import java.util.ArrayList;
5 import java.util.Collection;
6 import java.util.Iterator;
9 import java.util.Objects;
11 import javax.ws.rs.core.MediaType;
13 import org.json.simple.parser.ParseException;
15 import com.sun.jersey.api.client.Client;
16 import com.sun.jersey.api.client.ClientResponse;
17 import com.sun.jersey.api.client.WebResource;
18 import com.sun.jersey.api.client.config.DefaultClientConfig;
20 import jalview.datamodel.SequenceI;
21 import jalview.fts.api.FTSData;
22 import jalview.fts.api.FTSDataColumnI;
23 import jalview.fts.api.FTSRestClientI;
24 import jalview.fts.core.FTSRestClient;
25 import jalview.fts.core.FTSRestRequest;
26 import jalview.fts.core.FTSRestResponse;
27 import jalview.fts.service.pdb.PDBFTSRestClient;
28 import jalview.util.JSONUtils;
29 import jalview.util.MessageManager;
30 import jalview.util.Platform;
32 public class TDBeaconsFTSRestClient extends FTSRestClient
34 private static final String DEFAULT_THREEDBEACONS_DOMAIN =
35 "https://wwwdev.ebi.ac.uk/pdbe/pdbe-kb/3dbeacons-hub-api/uniprot/";
37 private static FTSRestClientI instance = null;
39 protected TDBeaconsFTSRestClient()
43 @SuppressWarnings("unchecked")
45 public FTSRestResponse executeRequest(FTSRestRequest tdbRestRequest)
50 String query = tdbRestRequest.getFieldToSearchBy()
51 + tdbRestRequest.getSearchTerm();
53 Class<ClientResponse> clientResponseClass;
57 client = (Client) (Object) new jalview.javascript.web.Client();
58 clientResponseClass = (Class<ClientResponse>) (Object) jalview.javascript.web.ClientResponse.class;
66 client = Client.create(new DefaultClientConfig());
67 clientResponseClass = ClientResponse.class;
69 WebResource webResource;
70 webResource = client.resource(DEFAULT_THREEDBEACONS_DOMAIN)
72 URI uri = webResource.getURI();
73 System.out.println(uri.toString());
75 // Execute the REST request
76 ClientResponse clientResponse = webResource
77 .accept(MediaType.APPLICATION_JSON).get(clientResponseClass);
79 // Get the JSON string from the response object or directly from the
80 // client (JavaScript)
81 Map<String, Object> jsonObj = null;
82 String responseString = null;
84 // Check the response status and report exception if one occurs
85 int responseStatus = clientResponse.getStatus();
86 switch (responseStatus)
92 jsonObj = clientResponse.getEntity(Map.class);
96 responseString = clientResponse.getEntity(String.class);
100 throw new Exception(parseJsonExceptionString(responseString));
103 getMessageByHTTPStatusCode(responseStatus, "3DBeacons"));
105 // Process the response and return the result to the caller.
106 return parseTDBeaconsJsonResponse(responseString, jsonObj, tdbRestRequest);
107 } catch (Exception e)
109 String exceptionMsg = e.getMessage();
110 if (exceptionMsg.contains("SocketException"))
112 // No internet connection
113 throw new Exception(MessageManager.getString(
114 "exception.unable_to_detect_internet_connection"));
116 else if (exceptionMsg.contains("UnknownHostException"))
118 // The server is unreachable
119 throw new Exception(MessageManager.formatMessage(
120 "exception.fts_server_unreachable", "3DB Hub"));
130 public String setSearchTerm(String term) {
134 public static FTSRestResponse parseTDBeaconsJsonResponse(
135 String tdbJsonResponseString, FTSRestRequest tdbRestRequest)
137 return parseTDBeaconsJsonResponse(tdbJsonResponseString,
138 (Map<String, Object>) null, tdbRestRequest);
141 @SuppressWarnings("unchecked")
142 public static FTSRestResponse parseTDBeaconsJsonResponse(String tdbJsonResponseString,
143 Map<String, Object> jsonObj, FTSRestRequest tdbRestRequest)
145 FTSRestResponse searchResult = new FTSRestResponse();
146 //searchResult.setNumberOfItemsFound(1);
147 List<FTSData> result = null;
153 jsonObj = (Map<String, Object>) JSONUtils.parse(tdbJsonResponseString);
155 System.out.println(jsonObj);
156 //Map<String, Object> tdbResponse = (Map<String, Object>) jsonObj.get("structures");
158 //System.out.println((Map<String, Object>) jsonObj.get("uniprot_entry"));
159 Long seqLength = (Long) ((Map<String, Object>) jsonObj.get("uniprot_entry")).get("sequence_length");
160 System.out.println(seqLength);
161 result = new ArrayList<>();
162 List<Object> structures = (List<Object>) jsonObj.get("structures");
163 //System.out.println(jsonObj.get("structures"));
164 //System.out.println(structures);
165 System.out.println("ZERO : " + structures.get(0));
166 System.out.println("1 : " + structures.get(1));
168 for (Iterator<Object> strucIter = structures.iterator(); strucIter
171 Map<String, Object> structure = (Map<String, Object>) strucIter.next();
172 result.add(getFTSData(structure, tdbRestRequest));
174 //System.out.println(structure);
176 //System.out.println(structures);
177 System.out.println("1 : " + structures.get(1));
178 searchResult.setNumberOfItemsFound(numFound);
179 searchResult.setSearchSummary(result);
180 searchResult.setSequenceLength(seqLength);
182 } catch (ParseException e)
189 private static FTSData getFTSData(Map<String, Object> tdbJsonStructure,
190 FTSRestRequest tdbRequest)
192 // TODO Auto-generated method stub
193 String primaryKey = null;
194 Object[] summaryRowData;
195 Collection<FTSDataColumnI> displayFields = tdbRequest.getWantedFields();
197 summaryRowData = new Object[displayFields.size()];
198 for (FTSDataColumnI field : displayFields) {
199 String fieldData = (tdbJsonStructure.get(field.getCode()) == null) ? " "
200 : tdbJsonStructure.get(field.getCode()).toString();
201 if (field.isPrimaryKeyColumn())
203 primaryKey = fieldData;
204 summaryRowData[colCounter++] = primaryKey;
206 else if (fieldData == null || fieldData.isEmpty())
208 summaryRowData[colCounter++] = null;
214 summaryRowData[colCounter++] = (field.getDataType()
215 .getDataTypeClass() == Integer.class)
216 ? Integer.valueOf(fieldData)
217 : (field.getDataType()
218 .getDataTypeClass() == Double.class)
219 ? Double.valueOf(fieldData)
221 } catch (Exception e)
224 System.out.println("offending value:" + fieldData);
228 final String primaryKey1 = primaryKey;
229 final Object[] summaryRowData1 = summaryRowData;
235 public Object[] getSummaryData()
237 return summaryRowData1;
241 public Object getPrimaryKey()
247 * Returns a string representation of this object;
250 public String toString()
252 StringBuilder summaryFieldValues = new StringBuilder();
253 for (Object summaryField : summaryRowData1)
255 summaryFieldValues.append(
256 summaryField == null ? " " : summaryField.toString())
259 return summaryFieldValues.toString();
263 * Returns hash code value for this object
266 public int hashCode()
268 return Objects.hash(primaryKey1, this.toString());
272 public boolean equals(Object that)
274 return this.toString().equals(that.toString());
279 // private static FTSData getFTSData(Map<String, Object> doc,
280 // FTSRestRequest tdbRestRequest)
282 // String primaryKey = null;
284 // Object[] summaryRowData;
286 // Collection<FTSDataColumnI> displayFields = tdbRestRequest.getWantedFields();
287 // int colCounter = 0;
288 // summaryRowData = new Object[displayFields.size() + 1];
293 private String parseJsonExceptionString(String jsonErrorString)
295 // TODO Auto-generated method stub
300 public String getColumnDataConfigFileName()
302 return "/fts/tdbeacons_data_columns.txt";
305 public static FTSRestClientI getInstance()
307 if (instance == null)
309 instance = new TDBeaconsFTSRestClient();
314 private Collection<FTSDataColumnI> allDefaultDisplayedStructureDataColumns;
316 public Collection<FTSDataColumnI> getAllDefaultDisplayedStructureDataColumns()
318 if (allDefaultDisplayedStructureDataColumns == null
319 || allDefaultDisplayedStructureDataColumns.isEmpty())
321 allDefaultDisplayedStructureDataColumns = new ArrayList<>();
322 allDefaultDisplayedStructureDataColumns
323 .addAll(super.getAllDefaultDisplayedFTSDataColumns());
325 return allDefaultDisplayedStructureDataColumns;