Advanced adapter usage and mashup
Now that basic usage of different types of adapters has been covered in previous tutorials, 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, one could make several requests successively, one depending on the other.
However, writing this logic on the server-side could be faster and cleaner.
At the heart of this mashup scenario is the
WL.Server.invokeProcedure(invocationData) API. Almost identical to its client-side counterpart, it enables you to invoke a procedure on any of your adapters.
invocationData parameter has the same format as the client-side API.
However, while the client-side
invokeProcedure uses a success handler, the server-side one returns the result object itself.
Data Mashup Sample
The following example shows how to mash up data from two data sources and to return the data stream to the application as a single
Data mashup can be implemented in the same way for any number of data sources and across different adapter types.
As an example, mash up data from the following sources:
- 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.
- Connect to the Yahoo! Weather Service.
- Extract an updated weather forecast for each of the cities that are retrieved via SQL.
Afterward, the mashed-up data is returned to the application for display.
- Create an HTTP adapter and name it HTTPWeather. This adapter connects to Yahoo! Weather RSS feed at http://weather.yahooapis.com/forecastrss.
The adapter has a single procedure called
The HTTP request has the following parameters:
w– Where in the world ID, a city ID used by Yahoo!
u– units. Can be "c" for Celsius or "f" for Fahrenheit.
- Create the
getYahooWeatherfunction in the HTTPWeather-impl.js file and use it to retrieve and return the weather data by using the Yahoo! Weather API:
- Create a SQL adapter and name it SQLAdapter. This adapter contains:
getCitiesWeather– a public procedure, which is declared in the adapter XML file. It is called from the application and returns the mashed-up data.
getCitiesList– an internal (private) function, which is not declared in the adapter XML file. It is called from the adapter through server API to get the cities list from the SQL server.
getCityWeather– an internal (private) function, which is not declared in the adapter XML file. It calls the
getYahooWeatherprocedure from a different adapter (HTTP) with the Yahoo! Weather ID as a parameter.
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.
- Create a
getCitiesListfunction in the SQLAdapter-impl.js file and use it to retrieve and return the city list from the SQL database.
- Create a
getCityWeatherfunction in the SQLAdapter-impl.js file and use it to retrieve and return the weather data from the HTTPWeather adapter:
- In the SQL adapter, extract a city identifier from the city list that was retrieved earlier and use it to call the
getYahooWeatherprocedure of the HTTPWeather adapter.
- Repeat for each city, attaching the received data to the city object.
- Return the mashed-up data to the application, which you can process in client-side code.
Click to download the Studio project.