Skip to Content

Hi,

In my last blog (here) I introduced the Mobile OData Service Generator, a feature in the Mobile Development Kit plugin for SAP Web IDE that generates a full fledged OData service for you based on a CSDL.

Well, I hope you are eager to hear how it works so we will jump directly into the action:

You need a place to store the data of your solution and to make it more easily to understand let’s assume you want to build a mobile canteen menu application for your peers.

In order to use the new Mobile OData Service Generator you need to active your Mobile Development Kit feature in the SAP WebIDE full-stack edition. (this video explains how it works)

Now you  can create a new project by choosing File > Project from Template. In the list of project templates, select Category “All categories” as a filter and select the “Mobile OData Service Project”.

For demonstration purposes we go the easiest way possible – choosing inMemory, so you can just type in a project name like “MyBackEnd” in the first step of the wizard dialog popping up.

You need to provide some more information about your service. Do you want to generate a service for CF or for NEO? Do you want to generate the persistence for SAP HANA, SAP ASE or Postgress? For our little getting started guide, we use InMemory with BasicAuth on NEO like below:

 

After finishing the project wizard, you’ll see a new project in your workspace like this:

This is the empty project structure of your service. Next step is to right click the project folder and create New > OData CSDL Document and fill out the form as follows:

These information are being used to generate the Java classes that will make your service.

In addition an file will be created that represents your OData Service: com.sap.mobile.backend.canteen.csdl.xml

If you don’t know what CSDL is all about, you should NOT read this. Keep focused on the task before you and save that link for later.

Just double click on the csdl.xml file to open a graphical editor that let’s you design your service.

I created a service like this:

This created a CSDL as follows:

<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="4.0" xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx">
<edmx:DataServices>
<Schema Namespace="com.sap.mobile.backend.canteen" Alias="canteen" xmlns="http://docs.oasis-open.org/odata/ns/edm">

<EntityType Name="Canteen">
  <Key>
    <PropertyRef Name="CanteenID"/>
  </Key>
  <Property Name="CanteenID" Type="Edm.Int64" Nullable="false"/>
  <Property Name="Location" Type="Edm.String" Nullable="false" MaxLength="50"/>
  <Property Name="Name" Type="Edm.String" Nullable="false"/>
  <NavigationProperty Name="LunchMenu" Type="canteen.Menu" Nullable="false"/>
</EntityType>

<EntityType Name="Menu">
  <Key>
    <PropertyRef Name="MenuID"/>
  </Key>
  <Property Name="DateOfLunch" Type="Edm.Date" Nullable="false"/>
  <Property Name="Description" Type="Edm.String" Nullable="false" MaxLength="250"/>
  <Property Name="MenuID" Type="Edm.Int64" Nullable="false"/>
  <Property Name="Name" Type="Edm.String" Nullable="false" MaxLength="50"/>
  <Property Name="kcal" Type="Edm.Int32" Nullable="true"/>
  <Property Name="veggie" Type="Edm.Binary" Nullable="true"/>
</EntityType>

<EntityContainer Name="CanteenService">
  <EntitySet Name="CanteenSet" EntityType="canteen.Canteen">
    <NavigationPropertyBinding Path="LunchMenu" Target="MenuSet"/>
  </EntitySet>

  <EntitySet Name="MenuSet" EntityType="canteen.Menu"/>
</EntityContainer>

</Schema>
</edmx:DataServices>
</edmx:Edmx>

Right-click on the csdl.xml file and click “Generate Mobile OData Server” to start generation of an Java project that represents the service. After a while the “srv” folder should look like this:

The next step would be to compile the Java project, but before you can do that you need an SAP Cloud Foundry trial, then go back to SAP WebIDE preferences and add your CF account details as seen below.

Please click the “Install Builder” button.

Don’t forget to “Save” the changes in this dialog at the bottom of the screen!

Now we can compile the Java Project and we will get a odata-service-0.1.0.war file like this:

This concludes the service generation and the scope of the tool. Everything that follows is standard SAP Cloud Platform behavior.

Now we will export the war and bring it into our SAP Cloud Platform Cockpit and run it as a Java Application in our trial landscape.

So in your Cockpit, choose Java Applications, then “Deploy Application” button to upload our .war file.

I used these parameters:

 

After deploying the war, start the app.

Once the service started, you can assign the “ServiceUser” role to yourself and open the service in your browser under https://odataservicecanteed0XXXXXXtrial.hanatrial.ondemand.com/odata-service-0.1.0, where XXXXXX is your trial account name.

What you see is your freshly created service, which fully supports all OData operations (CRUD) as well as OData features like, filter, select. It even serves the data as OData v2 and v4 simultaneously, just like the client demands it!

 

/odata-service-0.1.0/CanteenSet

 

{"@odata.context":"https://odataservicecanteed055161trial.hanatrial.ondemand.com/odata-service-0.1.0/$metadata#CanteenSet","@odata.type":"#Collection(com.sap.mobile.backend.canteen.Canteen)","value":[]}

 

If you send along a header like this DataServiceVersion:2.0 you’ll get a result like this

{
  "d": {
    "results": []
  }
}

I admit that it looks quite complicated, but if you look at the steps I performed for achieving this, there was not a single line of code involved and I used a graphical modelling approach to create an OData service.

Next step would be to create either an mobile app with SAP Mobile Cards, SAP Cloud Platform SDK for iOS or SAP Cloud Platform SDK for Android on top of this OData service.

And the OData service generator can even do more, which I did not have covered so far:

  • Generate an delta token enabled OData Service
  • Provides hooks to inject your own logic into the service to make it even more versatile

But these topics will be covered by other blogs in the future. More information can be found in our docs.

I hope you enjoyed our newest addition to the SAP Cloud Platform Mobile Service feature set.

Have Fun,

Martin

 

To report this post you need to login first.

4 Comments

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

  1. Dominique Pierre

    Hi Martin,

    Is this feature available in productive environments? We do have an account with the Mobile Services service and the MDK feature enabled but I can’t see the Mobile OData Service Project template. I can see it in my trial account.

    Cheers,

    Pierre

    (0) 
        1. Martin Lacasse

          Bonjour Dominique,

          Yes we implement the default create, update and delete.  You can add code extensions to the generated code to add your own logic to create and updates (e.g. some business rules) or to implement actions/function imports.

          I would refer you to the official docs on how to extend the generated java code:

          https://help.sap.com/viewer/977416d43cd74bdc958289038749100e/Latest/en-US/dc55f74f6e064dd38ddc51f6ef79d11e.html

          Also, we allow delta enabling the service, without any custom code required.

          thanks,

          Martin

          (1) 

Leave a Reply