JAL-3829 attempt to fix my changes to follow the Jira issue
[jalview.git] / src / jalview / fts / service / threedbeacons / TDBeaconsFTSRestClient.java
index 1ee7f37..7c06f05 100644 (file)
@@ -6,6 +6,7 @@ import java.util.Collection;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.Objects;
 
 import javax.ws.rs.core.MediaType;
 
@@ -35,6 +36,10 @@ public class TDBeaconsFTSRestClient extends FTSRestClient
   
   private static FTSRestClientI instance = null;
   
+  protected TDBeaconsFTSRestClient()
+  {   
+  }
+  
   @SuppressWarnings("unchecked")
   @Override
   public FTSRestResponse executeRequest(FTSRestRequest tdbRestRequest)
@@ -42,13 +47,6 @@ public class TDBeaconsFTSRestClient extends FTSRestClient
   {
     try
     {
-      // Removed wantedFields, sortParam & facetPivot from PDBFTSRClient
-      String wantedFields = getDataColumnsFieldsAsCommaDelimitedString(
-              tdbRestRequest.getWantedFields());
-      int responseSize = (tdbRestRequest.getResponseSize() == 0)
-              ? getDefaultResponsePageSize()
-              : tdbRestRequest.getResponseSize();
-      
       String query = tdbRestRequest.getFieldToSearchBy()
               + tdbRestRequest.getSearchTerm();
       Client client;
@@ -70,10 +68,9 @@ public class TDBeaconsFTSRestClient extends FTSRestClient
       }
       WebResource webResource;
       webResource = client.resource(DEFAULT_THREEDBEACONS_DOMAIN)
-              .queryParam("P",query);
+              .path(query);
       URI uri = webResource.getURI();
-        
-      System.out.println(uri);
+      System.out.println(uri.toString());
         
       // Execute the REST request
       ClientResponse clientResponse = webResource
@@ -84,13 +81,11 @@ public class TDBeaconsFTSRestClient extends FTSRestClient
       Map<String, Object> jsonObj = null;
       String responseString = null;
   
-      System.out.println("query >>>>>>> " + tdbRestRequest.toString());
-        
       // Check the response status and report exception if one occurs
       int responseStatus = clientResponse.getStatus();
       switch (responseStatus)
       {
-      // if succesful
+      // if success
       case 200:
         if (Platform.isJS())
         {
@@ -132,6 +127,10 @@ public class TDBeaconsFTSRestClient extends FTSRestClient
       
   }
   
+  public String setSearchTerm(String term) {
+    return term;
+  }
+  
   public static FTSRestResponse parseTDBeaconsJsonResponse(
           String tdbJsonResponseString, FTSRestRequest tdbRestRequest)
   {
@@ -144,7 +143,6 @@ public class TDBeaconsFTSRestClient extends FTSRestClient
           Map<String, Object> jsonObj, FTSRestRequest tdbRestRequest)
   {
     FTSRestResponse searchResult = new FTSRestResponse();
-    //searchResult.setNumberOfItemsFound(1);
     List<FTSData> result = null;
     
     try
@@ -153,35 +151,31 @@ public class TDBeaconsFTSRestClient extends FTSRestClient
       {
         jsonObj = (Map<String, Object>) JSONUtils.parse(tdbJsonResponseString);
       }
-      Map<String, Object> tdbEntry = (Map<String, Object>) jsonObj.get("uniprot_entry");
-      //String seqLength = tdbEntry.get("sequence_length").toString();
-      int seqLength = Integer.valueOf(tdbEntry.get("sequence_length").toString());
       
-      // tdbStructures is the list of found structures
-//      Map<String, Object> tdbStructures = (Map<String, Object>) jsonObj.get("structures");
-      Object[] structures = (Object[]) jsonObj.get("structures");
-      String accessName = ((Map<String, Object>) jsonObj.get("uniprot_entry"))
-              .get("ac").toString();
-      // this is to be modified
-      int numFound = structures.length;
-//      if (numFound > 0)
-//      {
-//        
-//        result = new ArrayList<>();
-//        List<String> models;
-//        //= ((Map<String, Object>) jsonObj.get("structures"))
-//                //.get("model_identifier").toString();
-//        for (Iterator<String> modelsIter = models.iterator(); modelsIter.hasNext();)
-//                .hasNext();)
-//        {
-//          Map<String, Object> doc = (Map<String, Object>) docIter.next();
-//          result.add(getFTSData(doc, tdbRestRequest));
-//        }
-        searchResult.setNumberOfItemsFound(numFound); 
-        // searchResult.setAccessionName(accessName); ?usefulness?       
-//        searchResult.setSearchSummary(result);
-//        
-//      }
+      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");
+      List<Object> structures = (List<Object>) jsonObj.get("structures");
+      result = new ArrayList<>();
+      
+      int numFound = 0;
+      for (Iterator<Object> strucIter = structures.iterator(); strucIter.hasNext();)
+      {
+        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.setSearchSummary(result);
+      searchResult.setSequenceLength(seqLength);
+        
     } catch (ParseException e)
     {
       e.printStackTrace();
@@ -189,6 +183,99 @@ public class TDBeaconsFTSRestClient extends FTSRestClient
     return searchResult;
   }
 
+private static FTSData getFTSData(Map<String, Object> tdbJsonStructure,
+          FTSRestRequest tdbRequest)
+  {
+    // TODO Auto-generated method stub
+    String primaryKey = null;
+    Object[] summaryRowData;
+    Collection<FTSDataColumnI> displayFields = tdbRequest.getWantedFields();
+    int colCounter = 0;
+    summaryRowData = new Object[displayFields.size()];
+    
+    for (FTSDataColumnI field : displayFields) {
+      System.out.println("Field " + field);
+      String fieldData = (tdbJsonStructure.get(field.getCode()) == null) ? " " 
+              : tdbJsonStructure.get(field.getCode()).toString();
+      System.out.println("Field Data : " + fieldData);
+      if (field.isPrimaryKeyColumn())
+      {
+        primaryKey = fieldData;
+        summaryRowData[colCounter++] = primaryKey;
+      }
+      else if (fieldData == null || fieldData.isEmpty())
+      {
+        summaryRowData[colCounter++] = null;
+      }
+      else 
+      {
+        try
+        {
+          summaryRowData[colCounter++] = (field.getDataType()
+                  .getDataTypeClass() == Integer.class)
+                          ? Integer.valueOf(fieldData)
+                          : (field.getDataType()
+                                  .getDataTypeClass() == Double.class)
+                                          ? Double.valueOf(fieldData)
+                                          : fieldData;
+        } catch (Exception e)
+        {
+          //e.printStackTrace();
+          System.out.println("offending value:" + fieldData);
+        }
+      }
+    }
+    final String primaryKey1 = primaryKey;
+    final Object[] summaryRowData1 = summaryRowData;
+    
+    return new FTSData()
+    {
+
+      @Override
+      public Object[] getSummaryData()
+      {
+        return summaryRowData1;
+      }
+
+      @Override
+      public Object getPrimaryKey()
+      {
+        return primaryKey1;
+      }
+      
+      /**
+       * Returns a string representation of this object;
+       */
+      @Override
+      public String toString()
+      {
+        StringBuilder summaryFieldValues = new StringBuilder();
+        for (Object summaryField : summaryRowData1)
+        {
+          summaryFieldValues.append(
+                  summaryField == null ? " " : summaryField.toString())
+                  .append("\t");
+        }
+        return summaryFieldValues.toString();
+      }
+      
+      /**
+       * Returns hash code value for this object
+       */
+      @Override
+      public int hashCode()
+      {
+        return Objects.hash(primaryKey1, this.toString());
+      }
+
+      @Override
+      public boolean equals(Object that)
+      {
+        return this.toString().equals(that.toString());
+      }
+    };
+  }
+
 //  private static FTSData getFTSData(Map<String, Object> doc,
 //          FTSRestRequest tdbRestRequest)
 //  {
@@ -212,8 +299,7 @@ public class TDBeaconsFTSRestClient extends FTSRestClient
   @Override
   public String getColumnDataConfigFileName()
   {
-    // TODO Auto-generated method stub
-    return null;
+    return "/fts/tdbeacons_data_columns.txt";
   }
   
   public static FTSRestClientI getInstance()