JAL-1720 JAL-2002 improvement for 'uniprot coverage' filter using facet pivot query
[jalview.git] / src / jalview / ws / dbsources / PDBRestClient.java
index da50606..2c05acd 100644 (file)
@@ -1,5 +1,26 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *  
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
 package jalview.ws.dbsources;
 
+import jalview.util.MessageManager;
 import jalview.ws.uimodel.PDBRestRequest;
 import jalview.ws.uimodel.PDBRestResponse;
 import jalview.ws.uimodel.PDBRestResponse.PDBResponseSummary;
@@ -21,7 +42,6 @@ import com.sun.jersey.api.client.ClientResponse;
 import com.sun.jersey.api.client.WebResource;
 import com.sun.jersey.api.client.config.ClientConfig;
 import com.sun.jersey.api.client.config.DefaultClientConfig;
-import com.sun.jersey.api.json.JSONConfiguration;
 
 /**
  * A rest client for querying the Search endpoing of the PDB REST API
@@ -49,8 +69,6 @@ public class PDBRestClient
     try
     {
       ClientConfig clientConfig = new DefaultClientConfig();
-      clientConfig.getFeatures().put(
-              JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
       Client client = Client.create(clientConfig);
 
       String wantedFields = getPDBDocFieldsAsCommaDelimitedString(pdbRestRequest
@@ -61,20 +79,39 @@ public class PDBRestClient
               .getFieldToSortBy().trim().isEmpty()) ? "" : (pdbRestRequest
               .getFieldToSortBy() + (pdbRestRequest.isAscending() ? " asc"
               : " desc"));
-
+      String facetPivot = (pdbRestRequest.getFacetPivot() == null || pdbRestRequest
+              .getFacetPivot().isEmpty()) ? "" : pdbRestRequest
+              .getFacetPivot();
+      String facetPivotMinCount = String.valueOf(pdbRestRequest
+              .getFacetPivotMinCount());
+      
       // Build request parameters for the REST Request
-      WebResource webResource = client.resource(PDB_SEARCH_ENDPOINT)
-              .queryParam("wt", "json").queryParam("fl", wantedFields)
-              .queryParam("rows", String.valueOf(responseSize))
-              .queryParam("q", pdbRestRequest.getQuery())
-              .queryParam("sort", sortParam);
-
+      WebResource webResource = null;
+      if (pdbRestRequest.isFacet())
+      {
+        webResource = client.resource(PDB_SEARCH_ENDPOINT)
+                .queryParam("wt", "json").queryParam("fl", wantedFields)
+                .queryParam("rows", String.valueOf(responseSize))
+                .queryParam("q", pdbRestRequest.getQuery())
+                .queryParam("sort", sortParam).queryParam("facet", "true")
+                .queryParam("facet.pivot", facetPivot)
+                .queryParam("facet.pivot.mincount", facetPivotMinCount);
+      }
+      else
+      {
+        webResource = client.resource(PDB_SEARCH_ENDPOINT)
+                .queryParam("wt", "json").queryParam("fl", wantedFields)
+                .queryParam("rows", String.valueOf(responseSize))
+                .queryParam("q", pdbRestRequest.getQuery())
+                .queryParam("sort", sortParam);
+      }
       // Execute the REST request
       ClientResponse clientResponse = webResource.accept(
               MediaType.APPLICATION_JSON).get(ClientResponse.class);
 
       // Get the JSON string from the response object
       String responseString = clientResponse.getEntity(String.class);
+      System.out.println("query >>>>>>> " + pdbRestRequest.toString());
 
       // Check the response status and report exception if one occurs
       if (clientResponse.getStatus() != 200)
@@ -105,16 +142,17 @@ public class PDBRestClient
       String exceptionMsg = e.getMessage();
       if (exceptionMsg.contains("SocketException"))
       {
-        throw new Exception(
-                "Jalview is unable to detect an internet connection");
         // No internet connection
+        throw new Exception(
+                MessageManager
+                        .getString("exception.unable_to_detect_internet_connection"));
       }
       else if (exceptionMsg.contains("UnknownHostException"))
       {
-        throw new Exception(
-                "Jalview couldn't reach the host server @ www.ebi.ac.uk"
-                        + "\nPlease ensure that you are connected to the internet.");
         // The server 'www.ebi.ac.uk' is unreachable
+        throw new Exception(
+                MessageManager
+                        .getString("exception.pdb_server_unreachable"));
       }
       else
       {
@@ -129,11 +167,12 @@ public class PDBRestClient
     switch (code)
     {
     case 410:
-      message = "PDB Rest Service no longer exists!";
+      message = MessageManager
+              .getString("exception.pdb_rest_service_no_longer_available");
       break;
     case 403:
     case 404:
-      message = "The requested resource could not be found";
+      message = MessageManager.getString("exception.resource_not_be_found");
       break;
     case 408:
     case 409:
@@ -143,7 +182,7 @@ public class PDBRestClient
     case 503:
     case 504:
     case 505:
-      message = "There seems to be an error from the PDB Rest API server.";
+      message = MessageManager.getString("exception.pdb_server_error");
       break;
 
     default:
@@ -271,27 +310,27 @@ public class PDBRestClient
    * table. The PDB Id serves as a unique identifier for a given row in the
    * summary table
    * 
-   * @param wantedFeilds
+   * @param wantedFields
    *          the available table columns in no particular order
    * @return the pdb id field column index
    */
   public static int getPDBIdColumIndex(
-          Collection<PDBDocField> wantedFeilds, boolean hasRefSeq)
+          Collection<PDBDocField> wantedFields, boolean hasRefSeq)
   {
 
     // If a reference sequence is attached then start counting from 1 else
     // start from zero
-    int pdbFeildIndexCounter = hasRefSeq ? 1 : 0;
+    int pdbFieldIndexCounter = hasRefSeq ? 1 : 0;
 
-    for (PDBDocField feild : wantedFeilds)
+    for (PDBDocField field : wantedFields)
     {
-      if (feild.equals(PDBDocField.PDB_ID))
+      if (field.equals(PDBDocField.PDB_ID))
       {
         break; // Once PDB Id index is determined exit iteration
       }
-      ++pdbFeildIndexCounter;
+      ++pdbFieldIndexCounter;
     }
-    return pdbFeildIndexCounter;
+    return pdbFieldIndexCounter;
   }
 
   /**
@@ -395,9 +434,10 @@ public class PDBRestClient
       return code;
     }
 
+    @Override
     public String toString()
     {
       return name;
     }
   }
-}
\ No newline at end of file
+}