X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Ffts%2Fservice%2Fthreedbeacons%2FTDBeaconsFTSRestClient.java;h=9ca42e5bbc702cd8768a63da52ffedd8d8e3373b;hb=eb4fa398cc61118453e33187199438a5f8c397ba;hp=e6d45e5a07de9d65b5967413ba111d1421229175;hpb=f640efee12150d1ae174c64c936f4630716cb7c4;p=jalview.git diff --git a/src/jalview/fts/service/threedbeacons/TDBeaconsFTSRestClient.java b/src/jalview/fts/service/threedbeacons/TDBeaconsFTSRestClient.java index e6d45e5..9ca42e5 100644 --- a/src/jalview/fts/service/threedbeacons/TDBeaconsFTSRestClient.java +++ b/src/jalview/fts/service/threedbeacons/TDBeaconsFTSRestClient.java @@ -21,25 +21,41 @@ 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 final String DEFAULT_THREEDBEACONS_DOMAIN = "https://wwwdev.ebi.ac.uk/pdbe/pdbe-kb/3dbeacons/api/uniprot/summary/"; + private static FTSRestClientI instance = null; - + protected TDBeaconsFTSRestClient() - { + { + } + public static void createMockTDBeaconsFSRestClient(String workingQuery, String jsonResponse) { + instance = new TDBeaconsFTSRestClient(); + ((TDBeaconsFTSRestClient) instance).setMock(workingQuery,jsonResponse); } + private String mockQuery = null; + private String mockResponse = null; + protected void setMock(String workingQuery,String jsonResponse) { + mockQuery=workingQuery; + mockResponse = jsonResponse; + } + private boolean isMocked() + { + return mockQuery != null && mockResponse!=null; + } @SuppressWarnings("unchecked") @Override public FTSRestResponse executeRequest(FTSRestRequest tdbRestRequest) @@ -47,16 +63,7 @@ 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(); - //int offSet = pdbRestRequest.getOffSet(); - - String query = tdbRestRequest.getFieldToSearchBy() - + tdbRestRequest.getSearchTerm(); + String query = tdbRestRequest.getSearchTerm(); Client client; Class clientResponseClass; if (Platform.isJS()) @@ -68,6 +75,7 @@ public class TDBeaconsFTSRestClient extends FTSRestClient else /** * Java only + * * @j2sIgnore */ { @@ -76,29 +84,31 @@ public class TDBeaconsFTSRestClient extends FTSRestClient } WebResource webResource; webResource = client.resource(DEFAULT_THREEDBEACONS_DOMAIN) - .queryParam("ac", query); -// .queryParam("rows", String.valueOf(responseSize)) -// .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 - .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 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() ? (mockQuery.equals(query) ? 200 : 404) : clientResponse.getStatus(); switch (responseStatus) { - // if succesful + // if success case 200: if (Platform.isJS()) { @@ -106,17 +116,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(); @@ -137,53 +150,70 @@ public class TDBeaconsFTSRestClient extends FTSRestClient throw e; } } - + } - - public String setSearchTerm() { + + /** + * 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) null, tdbRestRequest); } - + @SuppressWarnings("unchecked") - public static FTSRestResponse parseTDBeaconsJsonResponse(String tdbJsonResponseString, - Map jsonObj, FTSRestRequest tdbRestRequest) + public static FTSRestResponse parseTDBeaconsJsonResponse( + String tdbJsonResponseString, Map jsonObj, + FTSRestRequest tdbRestRequest) { FTSRestResponse searchResult = new FTSRestResponse(); - //searchResult.setNumberOfItemsFound(1); List result = null; - + try { if (jsonObj == null) { - jsonObj = (Map) JSONUtils.parse(tdbJsonResponseString); + jsonObj = (Map) JSONUtils + .parse(tdbJsonResponseString); } - //Map tdbResponse = (Map) jsonObj.get("structures"); - //String seqLength = tdbEntry.get("sequence_length").toString(); - //int seqLength = Integer.valueOf(tdbEntry.get("sequence_length").toString()); - - int seqLength = (int) ((Map) jsonObj.get("uniprot_entry")).get("sequence_length"); - result = new ArrayList<>(); + + 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) + // jsonObj.get("uniprot_entry")).get("sequence_length"); + List structures = (List) jsonObj.get("structures"); + result = new ArrayList<>(); + int numFound = 0; for (Iterator strucIter = structures.iterator(); strucIter .hasNext();) { - Map structure = (Map) strucIter.next(); + Map structure = (Map) strucIter + .next(); result.add(getFTSData(structure, tdbRestRequest)); numFound++; } - searchResult.setNumberOfItemsFound(numFound); - searchResult.setSearchSummary(result); - searchResult.setSequenceLength(seqLength); - + + searchResult.setNumberOfItemsFound(numFound); + searchResult.setSearchSummary(result); + } catch (ParseException e) { e.printStackTrace(); @@ -191,28 +221,45 @@ public class TDBeaconsFTSRestClient extends FTSRestClient return searchResult; } -private static FTSData getFTSData(Map tdbJsonStructure, + private static FTSData getFTSData(Map tdbJsonStructure, FTSRestRequest tdbRequest) { - // TODO Auto-generated method stub + // TODO: consider reusing PDBFTSRestClient.getFTSData ? + String primaryKey = null; Object[] summaryRowData; + + SequenceI associatedSequence; + Collection 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); 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 { @@ -225,14 +272,14 @@ private static FTSData getFTSData(Map tdbJsonStructure, : fieldData; } catch (Exception e) { - e.printStackTrace(); - System.out.println("offending value:" + fieldData); + // e.printStackTrace(); + System.out.println("offending value:" + fieldData + fieldData); } } } final String primaryKey1 = primaryKey; final Object[] summaryRowData1 = summaryRowData; - + return new FTSData() { @@ -247,7 +294,7 @@ private static FTSData getFTSData(Map tdbJsonStructure, { return primaryKey1; } - + /** * Returns a string representation of this object; */ @@ -263,7 +310,7 @@ private static FTSData getFTSData(Map tdbJsonStructure, } return summaryFieldValues.toString(); } - + /** * Returns hash code value for this object */ @@ -281,19 +328,20 @@ private static FTSData getFTSData(Map tdbJsonStructure, }; } -// private static FTSData getFTSData(Map doc, -// FTSRestRequest tdbRestRequest) -// { -// String primaryKey = null; -// -// Object[] summaryRowData; -// -// Collection displayFields = tdbRestRequest.getWantedFields(); -// int colCounter = 0; -// summaryRowData = new Object[displayFields.size() + 1]; -// -// return null; -// } + // private static FTSData getFTSData(Map doc, + // FTSRestRequest tdbRestRequest) + // { + // String primaryKey = null; + // + // Object[] summaryRowData; + // + // Collection displayFields = + // tdbRestRequest.getWantedFields(); + // int colCounter = 0; + // summaryRowData = new Object[displayFields.size() + 1]; + // + // return null; + // } private String parseJsonExceptionString(String jsonErrorString) { @@ -304,10 +352,9 @@ private static FTSData getFTSData(Map tdbJsonStructure, @Override public String getColumnDataConfigFileName() { - // TODO Auto-generated method stub - return null; + return "/fts/tdbeacons_data_columns.txt"; } - + public static FTSRestClientI getInstance() { if (instance == null) @@ -316,7 +363,7 @@ private static FTSData getFTSData(Map tdbJsonStructure, } return instance; } - + private Collection allDefaultDisplayedStructureDataColumns; public Collection getAllDefaultDisplayedStructureDataColumns() @@ -330,5 +377,26 @@ private static FTSData getFTSData(Map 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; + } }