import java.util.Iterator;
import java.util.List;
import java.util.Map;
+import java.util.Objects;
import javax.ws.rs.core.MediaType;
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;
-
+ /**
+ * production server URI
+ */
+ private static String TDB_PROD_API="https://www.ebi.ac.uk/pdbe/pdbe-kb/3dbeacons/api/uniprot/summary/";
+ /**
+ * dev server URI
+ */
+ private static String TDB_DEV_API="https://wwwdev.ebi.ac.uk/pdbe/pdbe-kb/3dbeacons/api/uniprot/summary/";
+ private static String DEFAULT_THREEDBEACONS_DOMAIN = TDB_PROD_API;
+
+ public static FTSRestClientI instance = null;
+
+ protected TDBeaconsFTSRestClient()
+ {
+ }
@SuppressWarnings("unchecked")
@Override
public FTSRestResponse executeRequest(FTSRestRequest tdbRestRequest)
{
try
{
- // Removed wantedFields, sortParam & facetPivot from PDBFTSRClient
-
- int responseSize = (tdbRestRequest.getResponseSize() == 0)
- ? getDefaultResponsePageSize()
- : tdbRestRequest.getResponseSize();
-
- String query = tdbRestRequest.getFieldToSearchBy()
- + tdbRestRequest.getSearchTerm();
+ String query = tdbRestRequest.getSearchTerm();
Client client;
Class<ClientResponse> clientResponseClass;
if (Platform.isJS())
else
/**
* Java only
+ *
* @j2sIgnore
*/
{
client = Client.create(new DefaultClientConfig());
clientResponseClass = ClientResponse.class;
}
+
WebResource webResource;
- webResource = client.resource(DEFAULT_THREEDBEACONS_DOMAIN);
+ webResource = client.resource(DEFAULT_THREEDBEACONS_DOMAIN+query);
+
URI uri = webResource.getURI();
-
- System.out.println(uri);
-
+ 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;
-
- System.out.println("query >>>>>>> " + tdbRestRequest.toString());
-
+
// Check the response status and report exception if one occurs
- int responseStatus = clientResponse.getStatus();
+ int responseStatus = isMocked() ? (mockQueries.containsKey(query) ? 200 : 404) : clientResponse.getStatus();
switch (responseStatus)
{
- // if succesful
+ // if success
case 200:
if (Platform.isJS())
{
}
else
{
- responseString = clientResponse.getEntity(String.class);
+ responseString = isMocked() ? mockQueries.get(query): 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();
- if (exceptionMsg.contains("SocketException"))
- {
- // No internet connection
- throw new Exception(MessageManager.getString(
- "exception.unable_to_detect_internet_connection"));
- }
- else if (exceptionMsg.contains("UnknownHostException"))
- {
- // The server is unreachable
- throw new Exception(MessageManager.formatMessage(
- "exception.fts_server_unreachable", "3DB Hub"));
- }
- else
+ if (exceptionMsg != null)
{
- throw e;
+ if (exceptionMsg.contains("SocketException"))
+ {
+ // No internet connection
+ throw new Exception(MessageManager.getString(
+ "exception.unable_to_detect_internet_connection"));
+ }
+ else if (exceptionMsg.contains("UnknownHostException"))
+ {
+ // The server is unreachable
+ throw new Exception(MessageManager.formatMessage(
+ "exception.fts_server_unreachable", "3DB Hub"));
+ }
}
- }
+ throw e;
+ }
+
+ }
+
+ /**
+ * 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();
- //searchResult.setNumberOfItemsFound(1);
List<FTSData> result = null;
-
+
try
{
if (jsonObj == null)
{
- jsonObj = (Map<String, Object>) JSONUtils.parse(tdbJsonResponseString);
+ 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");
+ // 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();)
+ {
+ Map<String, Object> structure = (Map<String, Object>) strucIter
+ .next();
+ result.add(getFTSData(structure, tdbRestRequest));
+ numFound++;
+ }
+
+ searchResult.setNumberOfItemsFound(numFound);
+ searchResult.setSearchSummary(result);
+
} catch (ParseException e)
{
e.printStackTrace();
return searchResult;
}
-// 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> tdbJsonStructure,
+ FTSRestRequest tdbRequest)
+ {
+ // 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[(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);
+ if (field.isPrimaryKeyColumn())
+ {
+ primaryKey = fieldData;
+ summaryRowData[colCounter++] = primaryKey;
+ }
+ else if (fieldData == null || fieldData.trim().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 + 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)
+ // {
+ // 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)
{
@Override
public String getColumnDataConfigFileName()
{
- // TODO Auto-generated method stub
- return null;
+ return "/fts/tdbeacons_data_columns.txt";
}
-
+
public static FTSRestClientI getInstance()
{
if (instance == null)
}
return instance;
}
-
+
private Collection<FTSDataColumnI> allDefaultDisplayedStructureDataColumns;
public Collection<FTSDataColumnI> getAllDefaultDisplayedStructureDataColumns()
}
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;
+ }
}