This  blog is about Integration Gateway (IGW) in SAP Mobile Platform 3.0 (SMP).

Based on the component Integration Gateway in SMP, you can provide OData services that expose data that is fetched from different data sources – like Database, JPA, webservices.

This means that IGW acts as middleware and supports data integration:

1. It fetches the data from the connected backend(s).

  This data has a certain structure; e.g. in case of database, there are tables and columns.

2. This data is then mapped to the OData model

3. At the end, it is provided as OData service

The point is:

the framework is able to automatically (or with the help of custom processors) handle some supported data sources: JDBC, JPA, SOAP, ODC, REST

But what, if you have your data in a different source?

For this purpose, since SMP SP09 a new data source has been introduced: the “Custom Code”

This is a kind of generic user exit, it gives the user the freedom to provide any data, that might be fetched from any source.

As the name indicates, a script file containing custom code is used to write freestyle coding to provide any data.

The positive side: now any data source can be used to be exposed as OData service

The negative side: it has to be coded manually

The following tutorial shows how to do that.

It shows it in a very simplified way, based on hard-coded data, in order to show the essentials.

The full source code can be found in the Appendix and also attached to this blog.

A quick start section for IGW-experts can also be found in the Appendix.

Prerequisites

  • SAP Mobile Platform 3.0 SP09 (SMP) installation.
  • Eclipse with SAP Mobile Platform Tools installed
    check this blog for help
  • Basic knowledge about SMP and Integration Gateway
    Check this blog for some introduction

Overview

1. Design the OData model

  1.1 Create Project

  1.2 Create OData Model

  1.3. Bind data source

2 Implement the custom code

  2.1. getFeed(message)

  2.2. getEntry(message)

3. Test the OData service

  3.1. Deploy

  3.2. Run

4. Summary

5. Links

6. Appendix

  6.1. Configure the SMP server in Eclipse

  6.2. Full source code

  6.3. Quick Start for experts

1. Design the OData model

1.1. Create Project

In Eclipse, choose File -> New -> Other -> SAP Mobile Platform -> SAP Mobile Platform OData Implementation Project

Enter a “Project name” and enter a “Model name” for a “Blank OData Model”

The OData Model Editor is displayed.

1.2. Create OData Model

Create an Entity Type “Customer”, along with the properties and Entity Set as depicted below:

To keep it simple, we don’t change the data type for the properties, so the default, Edm.String is used.

1.3. Bind Data Source

Select the .odatasrv file which is located in the folder src.main.resources.model

Open the context menu and choose “Select Data Source”

In the wizard, choose “Custom Code”

As you can see, all the single operations are disabled, which means that one script file will cover all operations.

On the next page, select your preferred language for the script and press Finish

A file is created and some code is generated into it.

The generated code consists of 5 methods – one for each operation:

getFeed(message)

getEntry(message)


createEntry(message)

updateEntry(message)

deleteEntry(message)

These methods have also generated sample implementation.

A few words about it:

One prominent use case for the “Custom Code” data source is to support “Stored Procedures” of a database.

The generated sample code covers this use case.

The ScriptUtil class (available since SP09) provides several helper methods that are tailored to support this use case.

In our current tutorial we won’t talk about this use case.

2. Implement the custom code

The methods in the generated script file are equivalent to the OData calls.

We’ll see in detail what this means.

We start with deleting all the content of each generated method.

2.1. getFeed(message)

This method is invoked, if a QUERY operation is executed on the Odata service.

Example:

https://localhost:8083/gateway/odata/SAP/<MYSERVICE>/CustomerSet

As of the OData specification, this URL which points to the Entity Set is supposed to return a list of entities.

The IGW framework receives the OData request and for this kind of request if calls the getFeed() method of our script.

Our task is to provide this list of entities,

So now, in our tutorial, we have to proceed as follows:

1. create at least one entity to carry the sample data

2. create a list that carries this entity

3. set this list as responseBody to the message object that is passed to the getFeed() method

Let’s get our hands dirty and implement these steps as simple as possible

1. Create entity with sample data

The point is: in which format do we have to provide the data?

Actually, according to our OData model, the data is always a value of a property:

propertyName – propertyValue

Therefore, the suitable format is a map.

So in our implementation, we create a java.util.Map and we fill it with entries which carry the data corresponding to our Entity Type “Customer”.

The key of the map-entry is the property name.

The value for this key is provided by us.

For the property “ID” we create a map-entry and specify a dummy value, e.g. “1”

For the property “name” we create a map-entry and specify a dummy value, e.g. “MyCoolCompany”

The code snippet:

      var customerMap = new HashMap();

    customerMap.put(“ID”, “1”);

    customerMap.put(“Name”, “MyCoolCompany”);

Such a map represents one entry of data in the backend – it represents one Entity in the context of OData

This means that in a productive implementation we would have a bunch of maps.

For our simple tutorial it’s enough to have only one entry in the list.

2. Create a list

The list is an instance of java.util.List which is filled with all maps.

      var list = new ArrayList();

    list.add(customerMap);

3. Set the response

The getFeed() method is called by the framework and all the required interchange information is encapsulated in the message object that is passed in the method signature.

So what we have to do with the ArrayList is to set it as body to the message object and return it:

    message.setBody(list);

      return message;

2.2. getEntry(message)

This method is invoked whenever, if a single READ operation is executed on the OData service.

Example for such URL:

https://localhost:8083/gateway/odata/SAP/<MYSERVICE>/CustomerSet(‘1’)

For this call, the OData service has to return one customer-entity.

So the implementation is very much the same like in the getFeed() method:

We create the data in a map and return it (no List required):

      var customerMap = new HashMap();

    customerMap.put(“ID”, “1”);

    customerMap.put(“Name”, “MyCoolCompany”);         

    message.setBody(customerMap);

      return message;

3. Test the OData service

3.1. Deploy

In Eclipse, select your project and from the context menu choose “Generate and Deploy Integration Content” and confirm the subsequent dialog

Note:

Eclipse needs to know the details of the server, so if you haven’t entered the server details in the preference page, check the Appendix section

3.2. Run

After successful deployment, open the SMP cockpit in a browser https://localhost:8083/Admin/  and navigate to the “OData Services” tab

The deployed service should be listed there.

Note:

We don’t need to assign any destination to our service, because in our example we don’t connect to a backend.

However, it could be possible to create and assign a destination and access it from the code.

Click the hyperlink “Open Service Document” in order to open your new OData service.

The service document is opened in a new browser tab.

To test the getFeed() method:

https://localhost:8083/gateway/odata/SAP/<YOUR_SERVICE>/CustomerSet

To test the getEntry() method:

https://localhost:8083/gateway/odata/SAP/<YOUR_SERVICE>/CustomerSet(‘1’)

4. Summary

The component Integration Gateway in SMP 3.0 supports providing any data as OData service.

For this purpose, the “Custom Code” has to be chosen as “data source” in Eclipse.

In a script file, the relevant code to provide the desired data has to be implemented manually.

In this tutorial we’ve shown how the 2 methods for read operations have to be implemented.

In the next tutorial we’ll show how the write operations are implemented.

5. Links

Installing SMP toolkit:

http://scn.sap.com/community/developer-center/mobility-platform/blog/2014/08/22/how-to-install-sap-mobile-platform-tools-for-integration-gateway-in-eclipse-kepler

Tutorial for Integration Gateway:

http://scn.sap.com/community/developer-center/mobility-platform/blog/2014/06/10/creating-an-odata-service-based-on-sap-gateway-soap-jdbc-and-jpa-data-sources-ba

Understanding Custom Code data source part 2: write operations

The series of tutorials about the REST data source. Many implementation details are also relevant for the “Custom Code” data source.

Integration Gateway: REST data source, overview of Blogs

6. Appendix

Configure the SMP server in Eclipse

Open the preference page at Window -> Preferences -> SAP Mobile Platform Tools -> Server

Enter the server-url and the user credentials.
Press “Test Connection” button, the result should be an info popup

The full source code


Below sample code contains the full script, including the not-implemented methods


function getFeed(message){
    importPackage(java.util);
    var customerMap = new HashMap();
    customerMap.put("ID", "1");
    customerMap.put("Name", "MyCoolCompany");
    var list = new ArrayList();
    list.add(customerMap);
    message.setBody(list);
    return message;
}
function getEntry(message){
    importPackage(java.util);
    // create the map carrying the dummy data
    var customerMap = new HashMap();
    customerMap.put("ID", "1");
    customerMap.put("Name", "MyCoolCompany");
    //set the list as the body.
    message.setBody(customerMap);
    return message;
}
function createEntry(message) {
    return message;
}
function updateEntry(message) {
    return message;
}
function deleteEntry(message){
    return message;
}












Quick Start for experts


If you’re already familiar with IGW scripting:

  • getFeed() and getEntry() methods are invoked for QUERY and READ operations, respectively
  • The message object provides access to headers and body
  • The format for delivering the data to IGW:
    getFeed() : response body is an instance of java.util.List containing java.util.Map
    getEntry() : response body is an instance of java.util.Map



To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply