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.
Calling a JavaScript adapter procedure from a JavaScript adapter
When calling a JavaScript adapter procedure from another JavaScript adapter use the WL.Server.invokeProcedure(invocationData) API.
This API enables to invoke a procedure on any of your adapters. WL.Server.invokeProcedure(invocationData) returns the result object
retrieved from the called procedure.
The invocationData API format is: WL.Server.invokeProcedure({adapter: [Adapter Name], procedure: [Procedure Name], parameters: [Parameters seperated by a comma]})
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.
The following example shows how to mash up data from 2 data sources, a database table and Yahoo! Weather Service, And to return the data stream to the application as a single object.
In this example we will use 2 adapters:
Cities Adapter:
Extract a list of cities from a “weather” database table.
The result contains the list of several cities around the world, their Yahoo! Weather identifier and some description.
Weather Adapter:
Connect to the Yahoo! Weather Service.
Extract an updated weather forecast for each of the cities that are retrieved via the Cities 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
cities Adapter
Weather Adapter
JavaScript -> JavaScript adapters
getCitiesListJS
getCityWeatherJS
Java adapter -> JavaScript adapter
getCitiesListJavaToJS
getCityWeatherJS
Java adapter -> Java adapter
getCitiesListJava
getCityWeatherJava
Mashup Sample Flow
Create a procedure / adapter call that create a request to Yahoo! Weather Service for each city and retrieves the corresponding data:
Create an SQL query and fetch the cities records from the database:
(getCitiesListJS adapter)
vargetCitiesListStatement=WL.Server.createSQLStatement("select city, identifier, summary from weather;");functiongetCitiesList(){returnWL.Server.invokeSQLStatement({preparedStatement:getCitiesListStatement,parameters:[]});}
PreparedStatementgetAllCities=getSQLConnection().prepareStatement("select city, identifier, summary from weather");ResultSetrs=getAllCities.executeQuery();
Loop through the cities records and fetch the weather info for each city from Yahoo! Weather Service:
Iterating through the retrieved rss feed to fetch only the weather description, put this values in a resultSet / JSONArray object
and return it to the application:
{...JSONObjectrss=(JSONObject)jsonWeather.get("rss");JSONObjectchannel=(JSONObject)rss.get("channel");JSONObjectitem=(JSONObject)channel.get("item");StringcityWeatherSummary=(String)item.get("description");/* Putting the current city results into a JSONObject */JSONObjectjsonObj=newJSONObject();jsonObj.put("city",rs.getString("city"));jsonObj.put("identifier",rs.getString("identifier"));jsonObj.put("summary",rs.getString("summary"));jsonObj.put("weather",cityWeatherSummary);jsonArr.add(jsonObj);}conn.close();returnjsonArr.toString();
An example of city list in SQL is provided with the attached sample, under server/mobilefirstTraining.sql.
Remember that SQL adapters require a JDBC connector driver, which must be downloaded separately by the developer and added to the server/lib folder of the project.
Sample application
Click to download the MobileFirst project.
This sample is a hybrid project and includes also the server code (adapters).
▲
Inclusive terminology note: The Mobile First Platform team is making changes to support
the IBM® initiative to replace racially biased and other discriminatory language in our code and content
with more inclusive language. While IBM values the use of inclusive language, terms that are outside of
IBM's direct influence are sometimes required for the sake of maintaining user understanding. As other
industry leaders join IBM in embracing the use of inclusive language, IBM will continue to update the
documentation to reflect those changes.