ccdc525b3187efbad87c2e85e41aebe2a12ec628
[jalview.git] / src / jalview / fts / service / threedbeacons / TDBeaconsFTSRestClient.java
1 package jalview.fts.service.threedbeacons;
2
3 import java.net.URI;
4 import java.util.ArrayList;
5 import java.util.Collection;
6 import java.util.Iterator;
7 import java.util.List;
8 import java.util.Map;
9 import java.util.Objects;
10
11 import javax.ws.rs.core.MediaType;
12
13 import org.json.simple.parser.ParseException;
14
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;
19
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.api.StructureFTSRestClientI;
25 import jalview.fts.core.FTSRestClient;
26 import jalview.fts.core.FTSRestRequest;
27 import jalview.fts.core.FTSRestResponse;
28 import jalview.fts.core.FTSDataColumnPreferences.PreferenceSource;
29 import jalview.fts.service.pdb.PDBFTSRestClient;
30 import jalview.util.JSONUtils;
31 import jalview.util.MessageManager;
32 import jalview.util.Platform;
33
34 public class TDBeaconsFTSRestClient extends FTSRestClient
35         implements StructureFTSRestClientI
36 {
37   /**
38    * production server URI
39    */
40   private static String TDB_PROD_API="https://www.ebi.ac.uk/pdbe/pdbe-kb/3dbeacons/api/uniprot/summary/";
41   /**
42    * dev server URI
43    */
44   private static String TDB_DEV_API="https://wwwdev.ebi.ac.uk/pdbe/pdbe-kb/3dbeacons/api/uniprot/summary/";
45   private static String DEFAULT_THREEDBEACONS_DOMAIN = TDB_PROD_API; 
46
47   public static FTSRestClientI instance = null;
48
49   protected TDBeaconsFTSRestClient()
50   {
51   }
52   @SuppressWarnings("unchecked")
53   @Override
54   public FTSRestResponse executeRequest(FTSRestRequest tdbRestRequest)
55           throws Exception
56   {
57     try
58     {
59       String query = tdbRestRequest.getSearchTerm();
60       Client client;
61       Class<ClientResponse> clientResponseClass;
62       if (Platform.isJS())
63       {
64         // JavaScript only
65         client = (Client) (Object) new jalview.javascript.web.Client();
66         clientResponseClass = (Class<ClientResponse>) (Object) jalview.javascript.web.ClientResponse.class;
67       }
68       else
69       /**
70        * Java only
71        * 
72        * @j2sIgnore
73        */
74       {
75         client = Client.create(new DefaultClientConfig());
76         clientResponseClass = ClientResponse.class;
77       }
78
79       WebResource webResource;
80       webResource = client.resource(DEFAULT_THREEDBEACONS_DOMAIN+query);
81
82       URI uri = webResource.getURI();
83       System.out.println(uri.toString());
84
85       // Execute the REST request
86       ClientResponse clientResponse;
87       if (isMocked()) { 
88         clientResponse = null;
89       }
90       else
91       {
92         clientResponse = webResource.accept(MediaType.APPLICATION_JSON)
93                 .get(clientResponseClass);
94       }
95
96       // Get the JSON string from the response object or directly from the
97       // client (JavaScript)
98       Map<String, Object> jsonObj = null;
99       String responseString = null;
100
101       // Check the response status and report exception if one occurs
102       int responseStatus = isMocked() ? (mockQueries.containsKey(query) ? 200 : 404) : clientResponse.getStatus();
103       switch (responseStatus)
104       {
105       // if success
106       case 200:
107         if (Platform.isJS())
108         {
109           jsonObj = clientResponse.getEntity(Map.class);
110         }
111         else
112         {
113           responseString = isMocked() ? mockQueries.get(query): clientResponse.getEntity(String.class);
114         }
115         break;
116       case 400:
117         throw new Exception(parseJsonExceptionString(responseString));
118       case 404:
119         return emptyTDBeaconsJsonResponse();
120       default:
121         throw new Exception(
122                 getMessageByHTTPStatusCode(responseStatus, "3DBeacons"));
123       }
124       // Process the response and return the result to the caller.
125       return parseTDBeaconsJsonResponse(responseString, jsonObj,
126               tdbRestRequest);
127     } catch (Exception e)
128     {
129       String exceptionMsg = e.getMessage();
130       if (exceptionMsg != null)
131       {
132         if (exceptionMsg.contains("SocketException"))
133         {
134           // No internet connection
135           throw new Exception(MessageManager.getString(
136                   "exception.unable_to_detect_internet_connection"));
137         }
138         else if (exceptionMsg.contains("UnknownHostException"))
139         {
140           // The server is unreachable
141           throw new Exception(MessageManager.formatMessage(
142                   "exception.fts_server_unreachable", "3DB Hub"));
143         }
144       }
145       throw e;
146       
147     }
148
149   }
150
151   /**
152    * returns response for when the 3D-Beacons service doesn't have a record for
153    * the given query - in 2.11.2 this triggers a failover to the PDBe FTS 
154    * 
155    * @return null
156    */
157   private FTSRestResponse emptyTDBeaconsJsonResponse()
158   {
159     return null;
160   }
161
162   public String setSearchTerm(String term)
163   {
164     return term;
165   }
166
167   public static FTSRestResponse parseTDBeaconsJsonResponse(
168           String tdbJsonResponseString, FTSRestRequest tdbRestRequest)
169   {
170     return parseTDBeaconsJsonResponse(tdbJsonResponseString,
171             (Map<String, Object>) null, tdbRestRequest);
172   }
173
174   @SuppressWarnings("unchecked")
175   public static FTSRestResponse parseTDBeaconsJsonResponse(
176           String tdbJsonResponseString, Map<String, Object> jsonObj,
177           FTSRestRequest tdbRestRequest)
178   {
179     FTSRestResponse searchResult = new FTSRestResponse();
180     List<FTSData> result = null;
181
182     try
183     {
184       if (jsonObj == null)
185       {
186         jsonObj = (Map<String, Object>) JSONUtils
187                 .parse(tdbJsonResponseString);
188       }
189
190       Object uniprot_entry = jsonObj.get("uniprot_entry");
191       // TODO: decide if anything from uniprot_entry needs to be reported via
192       // the FTSRestResponse object
193       // Arnaud added seqLength = (Long) ((Map<String, Object>)
194       // jsonObj.get("uniprot_entry")).get("sequence_length");
195
196       List<Object> structures = (List<Object>) jsonObj.get("structures");
197       result = new ArrayList<>();
198
199       int numFound = 0;
200       for (Iterator<Object> strucIter = structures.iterator(); strucIter
201               .hasNext();)
202       {
203         Map<String, Object> structure = (Map<String, Object>) strucIter
204                 .next();
205         result.add(getFTSData(structure, tdbRestRequest));
206         numFound++;
207       }
208
209       searchResult.setNumberOfItemsFound(numFound);
210       searchResult.setSearchSummary(result);
211
212     } catch (ParseException e)
213     {
214       e.printStackTrace();
215     }
216     return searchResult;
217   }
218
219   private static FTSData getFTSData(Map<String, Object> tdbJsonStructure,
220           FTSRestRequest tdbRequest)
221   {
222     String primaryKey = null;
223     Object[] summaryRowData;
224
225     SequenceI associatedSequence;
226
227     Collection<FTSDataColumnI> displayFields = tdbRequest.getWantedFields();
228     SequenceI associatedSeq = tdbRequest.getAssociatedSequence();
229     int colCounter = 0;
230     summaryRowData = new Object[(associatedSeq != null)
231                                 ? displayFields.size() + 1
232                                 : displayFields.size()];
233                         if (associatedSeq != null)
234                         {
235                           associatedSequence = associatedSeq;
236                           summaryRowData[0] = associatedSequence;
237                           colCounter = 1;
238                         }
239
240     for (FTSDataColumnI field : displayFields)
241     {
242       String fieldData = (tdbJsonStructure.get(field.getCode()) == null)
243               ? " "
244               : tdbJsonStructure.get(field.getCode()).toString();
245       // System.out.println("Field : " + field + " Data : " + fieldData);
246       if (field.isPrimaryKeyColumn())
247       {
248         primaryKey = fieldData;
249         summaryRowData[colCounter++] = primaryKey;
250       }
251       else if (fieldData == null || fieldData.trim().isEmpty())
252       {
253         summaryRowData[colCounter++] = null;
254       }
255       else
256       {
257         try
258         {
259           summaryRowData[colCounter++] = (field.getDataType()
260                   .getDataTypeClass() == Integer.class)
261                           ? Integer.valueOf(fieldData)
262                           : (field.getDataType()
263                                   .getDataTypeClass() == Double.class)
264                                           ? Double.valueOf(fieldData)
265                                           : fieldData;
266         } catch (Exception e)
267         {
268           // e.printStackTrace();
269           System.out.println("offending value:" + fieldData + fieldData);
270         }
271       }
272     }
273     final String primaryKey1 = primaryKey;
274     final Object[] summaryRowData1 = summaryRowData;
275
276     return new TDB_FTSData(primaryKey, tdbJsonStructure, summaryRowData1);
277   }
278
279   // private static FTSData getFTSData(Map<String, Object> doc,
280   // FTSRestRequest tdbRestRequest)
281   // {
282   // String primaryKey = null;
283   //
284   // Object[] summaryRowData;
285   //
286   // Collection<FTSDataColumnI> displayFields =
287   // tdbRestRequest.getWantedFields();
288   // int colCounter = 0;
289   // summaryRowData = new Object[displayFields.size() + 1];
290   //
291   // return null;
292   // }
293
294   private String parseJsonExceptionString(String jsonErrorString)
295   {
296     // TODO Auto-generated method stub
297     return null;
298   }
299
300   @Override
301   public String getColumnDataConfigFileName()
302   {
303     return "/fts/tdbeacons_data_columns.txt";
304   }
305
306   public static FTSRestClientI getInstance()
307   {
308     if (instance == null)
309     {
310       instance = new TDBeaconsFTSRestClient();
311     }
312     return instance;
313   }
314
315   private Collection<FTSDataColumnI> allDefaultDisplayedStructureDataColumns;
316
317   public Collection<FTSDataColumnI> getAllDefaultDisplayedStructureDataColumns()
318   {
319     if (allDefaultDisplayedStructureDataColumns == null
320             || allDefaultDisplayedStructureDataColumns.isEmpty())
321     {
322       allDefaultDisplayedStructureDataColumns = new ArrayList<>();
323       allDefaultDisplayedStructureDataColumns
324               .addAll(super.getAllDefaultDisplayedFTSDataColumns());
325     }
326     return allDefaultDisplayedStructureDataColumns;
327   }
328
329   @Override
330   public String[] getPreferencesColumnsFor(PreferenceSource source)
331   {
332     String[] columnNames = null;
333     switch (source)
334     {
335     case SEARCH_SUMMARY:
336       columnNames = new String[] { "", "Display", "Group" };
337       break;
338     case STRUCTURE_CHOOSER:
339       columnNames = new String[] { "", "Display", "Group" };
340       break;
341     case PREFERENCES:
342       columnNames = new String[] { "3DB Beacons Field", "Show in search summary",
343           "Show in structure summary" };
344       break;
345     default:
346       break;
347     }
348     return columnNames;
349   }
350 }