Advanced Adapter Usage and Mashup

Overview

Now that basic usage of different types of adapters has been covered, it is important to remember that adapters can be combined to make a procedure that uses different adapters to generate one processed result. You can combine several sources (different HTTP servers, SQL, etc).

In theory, from the client side, you could make several requests successively, one depending on the other.
However, writing this logic on the server side could be faster and cleaner.

Jump to

JavaScript adapter API

Calling a JavaScript adapter procedure from a JavaScript adapter

When calling a JavaScript adapter procedure from another JavaScript adapter use the MFP.Server.invokeProcedure(invocationData) API. This API enables to invoke a procedure on any of your JavaScript adapters. MFP.Server.invokeProcedure(invocationData) returns the result object retrieved from the called procedure.

Calling a JavaScript adapter procedure from a Java adapter

When calling a JavaScript adapter procedure from a Java adapter use both the executeAdapterRequest API and the createJavascriptAdapterRequest API that creates an HttpUriRequest to pass as a parameter to the executeAdapterRequest call.

Data mashup example

The following example shows how to mash up data from 2 data sources, a database table and Fixer.io (exchange rate and currency conversion service), And to return the data stream to the application as a single object.

In this example we will use 2 adapters:

SQL Adapter:

Extract a list of currencies from a currencies database table.

The result contains the list of currencies. Each currency will have an id, symbol and name. For example: {3, EUR, Euro}

This adapter will also have a procedure that calls the HTTP adapter passing 2 parameters - a base currency and a target currency to retrieve the updated exchange-rate.

HTTP Adapter:

Connect to the Fixer.io service.

Extract an updated exchange-rate for the requested currencies that are retrieved as parameters via the SQL adapter.

Afterward, the mashed-up data is returned to the application for display.

The provided sample in this tutorial demonstrates the implementation of this scenario using 3 different mashup types.
In each one of them the names of the adapters are slightly different.
Here is a list of the mashup types and the corresponding adapter names:

Scenario

SQL Adapter name

HTTP Adapter name

JavaScript adapter → JavaScript adapter

SQLAdapterJS

HTTPAdapterJS

Java adapter → JavaScript adapter

SQLAdapterJava

HTTPAdapterJS

Java adapter → Java adapter

SQLAdapterJava

HTTPAdapterJava

Mashup Sample Flow

1. Create a procedure / adapter call that create a request to a back-end endpoint for the requested currencies and retrieves the corresponding data:

2. Create a procedure that fetches the currencies records from the database and returns a resultSet / JSONArray to the application:

(SQLAdapterJS adapter)

vargetCurrenciesListStatement="SELECT id, symbol, name FROM currencies;";functiongetCurrenciesList(){varlist=MFP.Server.invokeSQLStatement({preparedStatement:getCurrenciesListStatement,parameters:[]});returnlist.resultSet;}

(SQLAdapterJava adapter)

@GET@Produces(MediaType.APPLICATION_JSON)@Path("/getCurrenciesList")publicJSONArraygetCurrenciesList()throwsSQLException,IOException{JSONArrayjsonArr=newJSONArray();Connectionconn=getSQLConnection();PreparedStatementgetAllCities=conn.prepareStatement("select id, symbol, name from currencies");ResultSetrs=getAllCities.executeQuery();while(rs.next()){JSONObjectjsonObj=newJSONObject();jsonObj.put("id",rs.getString("id"));jsonObj.put("symbol",rs.getString("symbol"));jsonObj.put("name",rs.getString("name"));jsonArr.add(jsonObj);}rs.close();conn.close();returnjsonArr;}

3. Create a procedure that calls the HTTPAdapter procedure (which we created in step 1) with the base-currency and the target-currency: