Created
August 16, 2015 03:39
-
-
Save msrivastav13/151abb8da4bfba0412c2 to your computer and use it in GitHub Desktop.
Extends the DataSource.Connection class to enable Salesforce to sync the external system’s metadata schema and to handle queries and searches of the external data.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Extends the DataSource.Connection class to enable | |
* Salesforce to sync the external system’s metadata schema | |
* and to handle queries and searches of the external data. | |
**/ | |
global class CurrencyDataSourceConnection extends DataSource.Connection { | |
private DataSource.ConnectionParams connectionInfo; | |
/** | |
* Constructor for DriveDataSourceConnection. | |
**/ | |
global CurrencyDataSourceConnection(DataSource.ConnectionParams connectionInfo) { | |
this.connectionInfo = connectionInfo; | |
} | |
/** | |
* Called when the administrator clicks “Validate and Sync” | |
* in the user interface for the external data source. | |
**/ | |
override global List<DataSource.Table> sync() { | |
List<DataSource.Table> tables =new List<DataSource.Table>(); | |
List<DataSource.Column> columns; | |
columns = new List<DataSource.Column>(); | |
columns.add(DataSource.Column.number('rate', 18,6)); | |
columns.add(DataSource.Column.text('from',10)); | |
columns.add(DataSource.Column.text('to',10)); | |
columns.add(DataSource.Column.text('ExternalId',10)); | |
columns.add(DataSource.Column.url('DisplayUrl',10)); | |
tables.add(DataSource.Table.get('Currency','ExternalId',columns)); | |
return tables; | |
} | |
/** | |
* Called to query and get results from the external | |
* system for SOQL queries, list views, and detail pages | |
* for an external object that’s associated with the | |
* external data source. | |
* | |
* The queryContext argument represents the query to run | |
* against a table in the external system. | |
* | |
* Returns a list of rows as the query results. | |
**/ | |
override global DataSource.TableResult query(DataSource.QueryContext c){ | |
DataSource.Filter filter = c.tableSelection.filter; | |
String url; | |
if (filter != null) { | |
String cName = filter.columnName; | |
if (cName != null && cName.equals('To')) | |
url = 'http://api.exchangeratelab.com/api/single/'+filter.columnValue+'?apikey='+Label.API_Key; | |
else | |
url = 'http://api.exchangeratelab.com/api/current?apikey='+Label.API_Key; | |
} else { | |
url = 'http://api.exchangeratelab.com/api/current?apikey='+Label.API_Key; | |
} | |
/** | |
* Filters, sorts, and applies limit and offset clauses. | |
**/ | |
List<Map<String, Object>> rows = DataSource.QueryUtils.process(c, getData(url)); | |
return DataSource.TableResult.get(true, null,c.tableSelection.tableSelected, rows); | |
} | |
/** | |
* Called to do a full text search and get results from | |
* the external system for SOSL queries and Salesforce | |
* global searches. | |
* | |
* The searchContext argument represents the query to run | |
* against a table in the external system. | |
* | |
* Returns results for each table that the searchContext | |
* requested to be searched. | |
* This is to be implemented and not shown in basic Project | |
**/ | |
override global List<DataSource.TableResult> search(DataSource.SearchContext c){ | |
List<DataSource.TableResult> results =new List<DataSource.TableResult>(); | |
for (Integer i =0; i< c.tableSelections.size();i++){ | |
String entity = c.tableSelections[i].tableSelected; | |
String url = ''; | |
results.add(DataSource.TableResult.get( | |
true, null, entity, getData(url))); | |
} | |
return results; | |
} | |
/** | |
* Helper method to parse the data. | |
* The url argument is the URL of the external system. | |
* Returns a list of rows from the external system. | |
**/ | |
public List<Map<String, Object>> getData(String url){ | |
HttpResponse response = getResponse(url); | |
List<Map<String, Object>> rows = | |
new List<Map<String, Object>>(); | |
Map<String, Object> m = ( | |
Map<String, Object>)JSON.deserializeUntyped( | |
response.getBody()); | |
system.debug(m); | |
String baseCurrency=(String)m.get('baseCurrency');//Get the Base Currency | |
/** | |
* Checks errors. | |
**/ | |
Map<String, Object> error = | |
(Map<String, Object>)m.get('error'); | |
if (error!=null){ | |
List<Object> errorsList = | |
(List<Object>)error.get('errors'); | |
Map<String, Object> errors = | |
(Map<String, Object>)errorsList[0]; | |
String ms = (String)errors.get('message'); | |
throw new DataSource.OAuthTokenExpiredException(ms); | |
} | |
List<Object> fileItems=(List<Object>)m.get('rates'); | |
if (fileItems != null){ | |
for (Integer i=0; i< fileItems.size(); i++){ | |
Map<String, Object> item = | |
(Map<String, Object>)fileItems[i]; | |
rows.add(createRow(item,baseCurrency)); | |
} | |
} else { | |
rows.add(createRow(m,baseCurrency)); | |
} | |
return rows; | |
} | |
/** | |
* Helper method to populate the External ID and Display | |
* URL fields on external object records based on the ‘id’ | |
* value that’s sent by the external system. | |
* | |
* The item argument maps to the data that | |
* represents a row. | |
* | |
* Returns an updated map with the External ID and | |
* Display URL values. | |
**/ | |
public Map<String, Object> createRow(Map<String, Object> item,String baseCurrency){ | |
Map<String, Object> row = new Map<String, Object>(); | |
for ( String key : item.keySet() ){ | |
if (key == 'to') { | |
row.put('ExternalId', item.get(key)); | |
row.put('DisplayUrl', item.get(key)); | |
row.put('to', item.get(key)); | |
} | |
else{ | |
row.put(key, item.get(key)); | |
} | |
} | |
row.put('from',baseCurrency); | |
return row; | |
} | |
/** | |
* Helper method to make the HTTP GET call. | |
* The url argument is the URL of the external system. | |
* Returns the response from the external system. | |
**/ | |
public HttpResponse getResponse(String url) { | |
Http httpProtocol = new Http(); | |
HttpRequest request = new HttpRequest(); | |
request.setEndPoint(url); | |
request.setMethod('GET'); | |
HttpResponse response = httpProtocol.send(request); | |
return response; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment