Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
bjoernw
Employee
Employee

In this blog I would like to introduce you to the custom scripting capabilities that were introduced with the Integration Gateway in SAP Mobile Platform 3.0 SP04. If you are interested in seeing which other new features were introduced, I can highly recommend you to read my colleague mustafa.saglam blog: What's New in Integration Gateway in SAP Mobile Platform 3.0 SP04.

A little while ago I wrote a blog/tutorial about creating an OData service based on SAP Gateway, SOAP, JDBC and JPA data sources. As you could see in that blog, there was only a mapping capability available for SOAP data sources. For ODC, JDBC and JPA data sources this meant that the names of the entity sets and their properties had to correspond exactly to the fields of the data sources. Sometimes this resulted in rather ugly looking entity sets, as you had to keep the case lettering identical to the source and keep the technical field names of the source. You can see it in this example:

However, as of the latest release of SAP Mobile Platform, it is possible to create powerful enhancements using the JavaScript and Groovy scripting languages. After assigning a data source to an entity set, there some user exits are provided where you can modify the requests before they are sent to the data source, and modify the responses as they are returned from the data source. Some examples of what you can do with this are delta token handling, tombstone support, SOAP authentication, or data source mapping.


In the following, I'd like to show you how you can map an entity set to a JDBC data source using JavaScript. Taking the example from the image above, the result will look as follows, after having performed a data mapping:

Setting up the SAP Mobile Platform Tools

Along with the new service pack, a new set of OData modeling tools called SAP Mobile Platform Tools were released. You can find more information about this in my colleague amit.nigam2 blog: SAP Gateway Developer Tools – Bridging the Past, Present and Future.


The SAP Mobile Platform tools are based on Eclipse Kepler and support the new features of the SAP Mobile Platform 3.0 SP04. But of course they are downwards compatible and you may use them to work on your SP03 projects.


So, if you haven't done so already, please download Eclipse Kepler and install the SAP Mobile Platform Tools by following these instructions.

Implementing the Data Mapping

1. Create a new project

Do a right click in the Project Explorer view and select: New > Other > SAP Mobile Platform > SAP Mobile Platform OData Implementation Project

Provide a project name and select the target runtime SAP Mobile Platform 3.0 SP4.

Provide a model name, select Blank OData Model and finish the wizard.

2. Modeling the entity set and assigning the data source

Please model your entity set as follows:

Perform a right click on the JDBCmapping.odatasrv file and select Select Data Source. In the following wizard, assign the Data Source JDBC and finish.

3. Defining the Custom Code

Expand the nodes under the odatasrv-file, do a right click on Data Source JDBC and select Define Custom Code.

You may keep the suggested file name, and select the script type JavaScript.

Now Eclipse will open a JavaScript file with some sample coding. The script contains four JavaScript functions; processRequestData, processRequestSQL, processResponseData, processResponseResult:

4. Implementing the mapping

In order to do the mapping, we will need to implement the processRequestData function. Luckily, it comes with sample coding to perform such a mapping. The coding should look like this:


function processRequestData(message) {
  //Import statements
  importPackage(com.sap.gateway.ip.core.customdev.util);
  importPackage(java.util);
  importPackage(org.apache.olingo.odata2.api.edm);
  importPackage(com.sap.gateway.core.ip.component.commons);
  importPackage(org.apache.olingo.odata2.api.uri);
  importPackage (com.sap.gateway.ip.core.customdev.logging);
  importPackage(com.sap.gateway.ip.core.customdev.api);
  //Create the table map and populate with EntitySet name and DB Table name
  var tableMap = new HashMap();
  tableMap.put("LocationsSet","COMPANYLOCATIONS");
  //Create the entity map and the two property maps
  var entitiesMap = new HashMap();
  var companyLocationsMap = new HashMap();
  //Property map
  companyLocationsMap.put("LocationID","LOC_ID");
  companyLocationsMap.put("LocationName","DESCRIPTION");
  //Entities map populated with property maps against relevant EntitySets
  entitiesMap.put("LocationsSet", companyLocationsMap);
  //Set the header in the message object with the two maps created
  message.setHeader(ODataCamelExchangeHeaders.JDBC_TABLE_MAPPING.toString(), tableMap);
  message.setHeader(ODataCamelExchangeHeaders.JDBC_PROP_MAPPING.toString(), entitiesMap);
  //Logger this will log error to the console - useful for finding errors
  log.logErrors(LogMessage.TechnicalError, message.getBody());
  return message;
}


This is all you need to do to perform the mapping. Now you only need to deploy your project to the SAP Mobile Platform server, assign the JDBC destination to your entity set, and then you are done.

5. Testing the service

When you call your service, you will find that the mapping is in place, and it is returning the data from your database:

Thanks for reading, I hope you will find this helpful.

Kind regards

Björn

4 Comments