JAL-3829 mocking for PDBFTS for related query to UniprotFTS mock.
[jalview.git] / src / jalview / fts / service / threedbeacons / TDBeaconsFTSRestClient.java
index 9c6f45a..73939c3 100644 (file)
@@ -21,25 +21,26 @@ import jalview.datamodel.SequenceI;
 import jalview.fts.api.FTSData;
 import jalview.fts.api.FTSDataColumnI;
 import jalview.fts.api.FTSRestClientI;
+import jalview.fts.api.StructureFTSRestClientI;
 import jalview.fts.core.FTSRestClient;
 import jalview.fts.core.FTSRestRequest;
 import jalview.fts.core.FTSRestResponse;
+import jalview.fts.core.FTSDataColumnPreferences.PreferenceSource;
 import jalview.fts.service.pdb.PDBFTSRestClient;
 import jalview.util.JSONUtils;
 import jalview.util.MessageManager;
 import jalview.util.Platform;
 
 public class TDBeaconsFTSRestClient extends FTSRestClient
+        implements StructureFTSRestClientI
 {
-  private static final String DEFAULT_THREEDBEACONS_DOMAIN = 
-          "https://wwwdev.ebi.ac.uk/pdbe/pdbe-kb/3dbeacons-hub-api/uniprot/";
-  
-  private static FTSRestClientI instance = null;
-  
+  private static final String DEFAULT_THREEDBEACONS_DOMAIN = "https://wwwdev.ebi.ac.uk/pdbe/pdbe-kb/3dbeacons/api/uniprot/summary/";
+
+  public static FTSRestClientI instance = null;
+
   protected TDBeaconsFTSRestClient()
-  {   
+  {
   }
-  
   @SuppressWarnings("unchecked")
   @Override
   public FTSRestResponse executeRequest(FTSRestRequest tdbRestRequest)
@@ -47,8 +48,7 @@ public class TDBeaconsFTSRestClient extends FTSRestClient
   {
     try
     {
-      String query = tdbRestRequest.getFieldToSearchBy()
-              + tdbRestRequest.getSearchTerm();
+      String query = tdbRestRequest.getSearchTerm();
       Client client;
       Class<ClientResponse> clientResponseClass;
       if (Platform.isJS())
@@ -60,6 +60,7 @@ public class TDBeaconsFTSRestClient extends FTSRestClient
       else
       /**
        * Java only
+       * 
        * @j2sIgnore
        */
       {
@@ -71,18 +72,25 @@ public class TDBeaconsFTSRestClient extends FTSRestClient
               .path(query);
       URI uri = webResource.getURI();
       System.out.println(uri.toString());
-        
+
       // Execute the REST request
-      ClientResponse clientResponse = webResource
-              .accept(MediaType.APPLICATION_JSON).get(clientResponseClass);
-  
+      ClientResponse clientResponse;
+      if (isMocked()) { 
+        clientResponse = null;
+      }
+      else
+      {
+        clientResponse = webResource.accept(MediaType.APPLICATION_JSON)
+                .get(clientResponseClass);
+      }
+
       // Get the JSON string from the response object or directly from the
       // client (JavaScript)
       Map<String, Object> jsonObj = null;
       String responseString = null;
-  
+
       // Check the response status and report exception if one occurs
-      int responseStatus = clientResponse.getStatus();
+      int responseStatus = isMocked() ? (mockQuery.equals(query) ? 200 : 404) : clientResponse.getStatus();
       switch (responseStatus)
       {
       // if success
@@ -93,17 +101,20 @@ public class TDBeaconsFTSRestClient extends FTSRestClient
         }
         else
         {
-          responseString = clientResponse.getEntity(String.class);
+          responseString = isMocked() ? mockResponse: clientResponse.getEntity(String.class);
         }
         break;
       case 400:
         throw new Exception(parseJsonExceptionString(responseString));
+      case 404:
+        return emptyTDBeaconsJsonResponse();
       default:
         throw new Exception(
                 getMessageByHTTPStatusCode(responseStatus, "3DBeacons"));
       }
       // Process the response and return the result to the caller.
-      return parseTDBeaconsJsonResponse(responseString, jsonObj, tdbRestRequest);
+      return parseTDBeaconsJsonResponse(responseString, jsonObj,
+              tdbRestRequest);
     } catch (Exception e)
     {
       String exceptionMsg = e.getMessage();
@@ -124,58 +135,70 @@ public class TDBeaconsFTSRestClient extends FTSRestClient
         throw e;
       }
     }
-      
+
   }
-  
-  public String setSearchTerm(String term) {
+
+  /**
+   * returns response for when the 3D-Beacons service doesn't have a record for
+   * the given query - in 2.11.2 this triggers a failover to the PDBe FTS 
+   * 
+   * @return null
+   */
+  private FTSRestResponse emptyTDBeaconsJsonResponse()
+  {
+    return null;
+  }
+
+  public String setSearchTerm(String term)
+  {
     return term;
   }
-  
+
   public static FTSRestResponse parseTDBeaconsJsonResponse(
           String tdbJsonResponseString, FTSRestRequest tdbRestRequest)
   {
     return parseTDBeaconsJsonResponse(tdbJsonResponseString,
             (Map<String, Object>) null, tdbRestRequest);
   }
-  
+
   @SuppressWarnings("unchecked")
-  public static FTSRestResponse parseTDBeaconsJsonResponse(String tdbJsonResponseString,
-          Map<String, Object> jsonObj, FTSRestRequest tdbRestRequest)
+  public static FTSRestResponse parseTDBeaconsJsonResponse(
+          String tdbJsonResponseString, Map<String, Object> jsonObj,
+          FTSRestRequest tdbRestRequest)
   {
     FTSRestResponse searchResult = new FTSRestResponse();
     List<FTSData> result = null;
-    
+
     try
     {
       if (jsonObj == null)
       {
-        jsonObj = (Map<String, Object>) JSONUtils.parse(tdbJsonResponseString);
+        jsonObj = (Map<String, Object>) JSONUtils
+                .parse(tdbJsonResponseString);
       }
-      
-      Object uniprot_entry = jsonObj.get("uniprot_entry"); 
-      Long seqLength = (Long) ((Map<String, Object>) jsonObj.get("uniprot_entry")).get("sequence_length");
-      //System.out.println(uniprot_entry);
-      //System.out.println(jsonObj);
-      //System.out.println("seqLenght :" + seqLength);
-      
-      //Map<String, Object> tdbResponse = (Map<String, Object>) jsonObj.get("structures");
+
+      Object uniprot_entry = jsonObj.get("uniprot_entry");
+      // TODO: decide if anything from uniprot_entry needs to be reported via
+      // the FTSRestResponse object
+      // Arnaud added seqLength = (Long) ((Map<String, Object>)
+      // jsonObj.get("uniprot_entry")).get("sequence_length");
+
       List<Object> structures = (List<Object>) jsonObj.get("structures");
       result = new ArrayList<>();
-      
+
       int numFound = 0;
-      for (Iterator<Object> strucIter = structures.iterator(); strucIter.hasNext();)
+      for (Iterator<Object> strucIter = structures.iterator(); strucIter
+              .hasNext();)
       {
-        Map<String, Object> structure = (Map<String, Object>) strucIter.next();
+        Map<String, Object> structure = (Map<String, Object>) strucIter
+                .next();
         result.add(getFTSData(structure, tdbRestRequest));
         numFound++;
-        //System.out.println(structure);
       }
-      
-      //System.out.println("1 : " + structures.get(1));
-      searchResult.setNumberOfItemsFound(numFound); 
+
+      searchResult.setNumberOfItemsFound(numFound);
       searchResult.setSearchSummary(result);
-      searchResult.setSequenceLength(seqLength);
-        
+
     } catch (ParseException e)
     {
       e.printStackTrace();
@@ -183,30 +206,45 @@ public class TDBeaconsFTSRestClient extends FTSRestClient
     return searchResult;
   }
 
-private static FTSData getFTSData(Map<String, Object> tdbJsonStructure,
+  private static FTSData getFTSData(Map<String, Object> tdbJsonStructure,
           FTSRestRequest tdbRequest)
   {
-    // TODO Auto-generated method stub
+    // TODO: consider reusing PDBFTSRestClient.getFTSData ?
+
     String primaryKey = null;
     Object[] summaryRowData;
+
+    SequenceI associatedSequence;
+
     Collection<FTSDataColumnI> displayFields = tdbRequest.getWantedFields();
+    SequenceI associatedSeq = tdbRequest.getAssociatedSequence();
     int colCounter = 0;
-    summaryRowData = new Object[displayFields.size()];
-    
-    for (FTSDataColumnI field : displayFields) {
-      String fieldData = (tdbJsonStructure.get(field.getCode()) == null) ? " " 
+    summaryRowData = new Object[(associatedSeq != null)
+                                ? displayFields.size() + 1
+                                : displayFields.size()];
+                        if (associatedSeq != null)
+                        {
+                          associatedSequence = associatedSeq;
+                          summaryRowData[0] = associatedSequence;
+                          colCounter = 1;
+                        }
+
+    for (FTSDataColumnI field : displayFields)
+    {
+      String fieldData = (tdbJsonStructure.get(field.getCode()) == null)
+              ? " "
               : tdbJsonStructure.get(field.getCode()).toString();
-      System.out.println("Field : " + field + "  Data : " + fieldData);
+      // System.out.println("Field : " + field + " Data : " + fieldData);
       if (field.isPrimaryKeyColumn())
       {
         primaryKey = fieldData;
         summaryRowData[colCounter++] = primaryKey;
       }
-      else if (fieldData == null || fieldData.isEmpty())
+      else if (fieldData == null || fieldData.trim().isEmpty())
       {
         summaryRowData[colCounter++] = null;
       }
-      else 
+      else
       {
         try
         {
@@ -219,14 +257,14 @@ private static FTSData getFTSData(Map<String, Object> tdbJsonStructure,
                                           : fieldData;
         } catch (Exception e)
         {
-          //e.printStackTrace();
+          // e.printStackTrace();
           System.out.println("offending value:" + fieldData + fieldData);
         }
       }
     }
     final String primaryKey1 = primaryKey;
     final Object[] summaryRowData1 = summaryRowData;
-    
+
     return new FTSData()
     {
 
@@ -241,7 +279,7 @@ private static FTSData getFTSData(Map<String, Object> tdbJsonStructure,
       {
         return primaryKey1;
       }
-      
+
       /**
        * Returns a string representation of this object;
        */
@@ -257,7 +295,7 @@ private static FTSData getFTSData(Map<String, Object> tdbJsonStructure,
         }
         return summaryFieldValues.toString();
       }
-      
+
       /**
        * Returns hash code value for this object
        */
@@ -275,19 +313,20 @@ private static FTSData getFTSData(Map<String, Object> tdbJsonStructure,
     };
   }
 
-//  private static FTSData getFTSData(Map<String, Object> doc,
-//          FTSRestRequest tdbRestRequest)
-//  {
-//    String primaryKey = null;
-//
-//    Object[] summaryRowData;
-//
-//    Collection<FTSDataColumnI> displayFields = tdbRestRequest.getWantedFields();
-//    int colCounter = 0;
-//    summaryRowData = new Object[displayFields.size() + 1];
-// 
-//    return null;
-//  }
+  // private static FTSData getFTSData(Map<String, Object> doc,
+  // FTSRestRequest tdbRestRequest)
+  // {
+  // String primaryKey = null;
+  //
+  // Object[] summaryRowData;
+  //
+  // Collection<FTSDataColumnI> displayFields =
+  // tdbRestRequest.getWantedFields();
+  // int colCounter = 0;
+  // summaryRowData = new Object[displayFields.size() + 1];
+  //
+  // return null;
+  // }
 
   private String parseJsonExceptionString(String jsonErrorString)
   {
@@ -300,7 +339,7 @@ private static FTSData getFTSData(Map<String, Object> tdbJsonStructure,
   {
     return "/fts/tdbeacons_data_columns.txt";
   }
-  
+
   public static FTSRestClientI getInstance()
   {
     if (instance == null)
@@ -309,7 +348,7 @@ private static FTSData getFTSData(Map<String, Object> tdbJsonStructure,
     }
     return instance;
   }
-  
+
   private Collection<FTSDataColumnI> allDefaultDisplayedStructureDataColumns;
 
   public Collection<FTSDataColumnI> getAllDefaultDisplayedStructureDataColumns()
@@ -323,5 +362,26 @@ private static FTSData getFTSData(Map<String, Object> tdbJsonStructure,
     }
     return allDefaultDisplayedStructureDataColumns;
   }
-  
+
+  @Override
+  public String[] getPreferencesColumnsFor(PreferenceSource source)
+  {
+    String[] columnNames = null;
+    switch (source)
+    {
+    case SEARCH_SUMMARY:
+      columnNames = new String[] { "", "Display", "Group" };
+      break;
+    case STRUCTURE_CHOOSER:
+      columnNames = new String[] { "", "Display", "Group" };
+      break;
+    case PREFERENCES:
+      columnNames = new String[] { "3DB Beacons Field", "Show in search summary",
+          "Show in structure summary" };
+      break;
+    default:
+      break;
+    }
+    return columnNames;
+  }
 }