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 // Removed wantedFields, sortParam & facetPivot from PDBFTSRClient
51 String wantedFields = getDataColumnsFieldsAsCommaDelimitedString(
52 tdbRestRequest.getWantedFields());
53 int responseSize = (tdbRestRequest.getResponseSize() == 0)
54 ? getDefaultResponsePageSize()
55 : tdbRestRequest.getResponseSize();
56 //int offSet = pdbRestRequest.getOffSet();
58 String query = tdbRestRequest.getFieldToSearchBy()
59 + tdbRestRequest.getSearchTerm();
61 Class<ClientResponse> clientResponseClass;
65 client = (Client) (Object) new jalview.javascript.web.Client();
66 clientResponseClass = (Class<ClientResponse>) (Object) jalview.javascript.web.ClientResponse.class;
74 client = Client.create(new DefaultClientConfig());
75 clientResponseClass = ClientResponse.class;
77 WebResource webResource;
78 webResource = client.resource(DEFAULT_THREEDBEACONS_DOMAIN)
79 .queryParam("ac", query);
80 // .queryParam("rows", String.valueOf(responseSize))
81 // .queryParam("P",query);
82 URI uri = webResource.getURI();
84 System.out.println(uri);
86 // Execute the REST request
87 ClientResponse clientResponse = webResource
88 .accept(MediaType.APPLICATION_JSON).get(clientResponseClass);
90 // Get the JSON string from the response object or directly from the
91 // client (JavaScript)
92 Map<String, Object> jsonObj = null;
93 String responseString = null;
95 System.out.println("query >>>>>>> " + tdbRestRequest.toString());
97 // Check the response status and report exception if one occurs
98 int responseStatus = clientResponse.getStatus();
99 switch (responseStatus)
105 jsonObj = clientResponse.getEntity(Map.class);
109 responseString = clientResponse.getEntity(String.class);
113 throw new Exception(parseJsonExceptionString(responseString));
116 getMessageByHTTPStatusCode(responseStatus, "3DBeacons"));
118 // Process the response and return the result to the caller.
119 return parseTDBeaconsJsonResponse(responseString, jsonObj, tdbRestRequest);
120 } catch (Exception e)
122 String exceptionMsg = e.getMessage();
123 if (exceptionMsg.contains("SocketException"))
125 // No internet connection
126 throw new Exception(MessageManager.getString(
127 "exception.unable_to_detect_internet_connection"));
129 else if (exceptionMsg.contains("UnknownHostException"))
131 // The server is unreachable
132 throw new Exception(MessageManager.formatMessage(
133 "exception.fts_server_unreachable", "3DB Hub"));
143 public String setSearchTerm() {
147 public static FTSRestResponse parseTDBeaconsJsonResponse(
148 String tdbJsonResponseString, FTSRestRequest tdbRestRequest)
150 return parseTDBeaconsJsonResponse(tdbJsonResponseString,
151 (Map<String, Object>) null, tdbRestRequest);
154 @SuppressWarnings("unchecked")
155 public static FTSRestResponse parseTDBeaconsJsonResponse(String tdbJsonResponseString,
156 Map<String, Object> jsonObj, FTSRestRequest tdbRestRequest)
158 FTSRestResponse searchResult = new FTSRestResponse();
159 //searchResult.setNumberOfItemsFound(1);
160 List<FTSData> result = null;
166 jsonObj = (Map<String, Object>) JSONUtils.parse(tdbJsonResponseString);
168 //Map<String, Object> tdbResponse = (Map<String, Object>) jsonObj.get("structures");
169 //String seqLength = tdbEntry.get("sequence_length").toString();
170 //int seqLength = Integer.valueOf(tdbEntry.get("sequence_length").toString());
172 int seqLength = (int) ((Map<String, Object>) jsonObj.get("uniprot_entry")).get("sequence_length");
173 result = new ArrayList<>();
174 List<Object> structures = (List<Object>) jsonObj.get("structures");
176 for (Iterator<Object> strucIter = structures.iterator(); strucIter
179 Map<String, Object> structure = (Map<String, Object>) strucIter.next();
180 result.add(getFTSData(structure, tdbRestRequest));
183 searchResult.setNumberOfItemsFound(numFound);
184 searchResult.setSearchSummary(result);
185 searchResult.setSequenceLength(seqLength);
187 } catch (ParseException e)
194 private static FTSData getFTSData(Map<String, Object> tdbJsonStructure,
195 FTSRestRequest tdbRequest)
197 // TODO Auto-generated method stub
198 String primaryKey = null;
199 Object[] summaryRowData;
200 Collection<FTSDataColumnI> displayFields = tdbRequest.getWantedFields();
202 summaryRowData = new Object[displayFields.size()];
203 for (FTSDataColumnI field : displayFields) {
204 String fieldData = (tdbJsonStructure.get(field.getCode()) == null) ? " "
205 : tdbJsonStructure.get(field.getCode()).toString();
206 if (field.isPrimaryKeyColumn())
208 primaryKey = fieldData;
209 summaryRowData[colCounter++] = primaryKey;
211 else if (fieldData == null || fieldData.isEmpty())
213 summaryRowData[colCounter++] = null;
219 summaryRowData[colCounter++] = (field.getDataType()
220 .getDataTypeClass() == Integer.class)
221 ? Integer.valueOf(fieldData)
222 : (field.getDataType()
223 .getDataTypeClass() == Double.class)
224 ? Double.valueOf(fieldData)
226 } catch (Exception e)
229 System.out.println("offending value:" + fieldData);
233 final String primaryKey1 = primaryKey;
234 final Object[] summaryRowData1 = summaryRowData;
240 public Object[] getSummaryData()
242 return summaryRowData1;
246 public Object getPrimaryKey()
252 * Returns a string representation of this object;
255 public String toString()
257 StringBuilder summaryFieldValues = new StringBuilder();
258 for (Object summaryField : summaryRowData1)
260 summaryFieldValues.append(
261 summaryField == null ? " " : summaryField.toString())
264 return summaryFieldValues.toString();
268 * Returns hash code value for this object
271 public int hashCode()
273 return Objects.hash(primaryKey1, this.toString());
277 public boolean equals(Object that)
279 return this.toString().equals(that.toString());
284 // private static FTSData getFTSData(Map<String, Object> doc,
285 // FTSRestRequest tdbRestRequest)
287 // String primaryKey = null;
289 // Object[] summaryRowData;
291 // Collection<FTSDataColumnI> displayFields = tdbRestRequest.getWantedFields();
292 // int colCounter = 0;
293 // summaryRowData = new Object[displayFields.size() + 1];
298 private String parseJsonExceptionString(String jsonErrorString)
300 // TODO Auto-generated method stub
305 public String getColumnDataConfigFileName()
307 // TODO Auto-generated method stub
311 public static FTSRestClientI getInstance()
313 if (instance == null)
315 instance = new TDBeaconsFTSRestClient();
320 private Collection<FTSDataColumnI> allDefaultDisplayedStructureDataColumns;
322 public Collection<FTSDataColumnI> getAllDefaultDisplayedStructureDataColumns()
324 if (allDefaultDisplayedStructureDataColumns == null
325 || allDefaultDisplayedStructureDataColumns.isEmpty())
327 allDefaultDisplayedStructureDataColumns = new ArrayList<>();
328 allDefaultDisplayedStructureDataColumns
329 .addAll(super.getAllDefaultDisplayedFTSDataColumns());
331 return allDefaultDisplayedStructureDataColumns;