Skip to Content

 

Blog Series:ODATA Protocol implementation for IOT Applications on SAP HCP to be precise – A JAVA Web Application using Apache Olingo API

 

This is a series of blog on Olingo OData implementation and divided into parts based on the topics, which I came across, while implementing the Olingo 2.0 APIs, First part consists of setting up of project and implementation of the Olingo OData APIs.

 

Part 1:  Setup of OData APIs through Olingo 2.0 API.

Part 2:  Entity class overview in JPA based WEB applications

Part 3:  Adding Custom annotation and labels to metadata.

Part 4:  JPA mapping or redefining the columns.

Part 5:  Debugging and Error Handling implementation in Olingo 2.0 OData Services.

Part 6:  Options to provide OData/REST services through JPAContext.

Part 7: Olingo OData 2.0 limitations

 

1. Introduction

 

This is series of documents on Apache Olingo OData2 services for HANA cloud platform (i.e. XSA and Cloud foundry) and JPA based web application. More details on creating JPA based application will be covered below. Apache Olingo is a Java library that implements the Open Data Protocol (OData). Apache Olingo serves client and server aspects of OData. It currently supports OData 2.0 for JPA Entities. Blog also provides different ways to customize pre/post processes for different type Requests on OData Services to various use cases, custom annotation, JPA mapping, entity mapping, debugging.

 

 

 

2. Prerequisite Activities

2.1 Create and build JPA Project

Web Project is already developed with JPA Entity to consume HANA Cloud Platform Database (HANA or MaxDB). Project should have persistence.xml, resource.xml and class files for JPA entities as shown in below picture.

/wp-content/uploads/2016/10/1_1032319.png

2.2 Get dependent Library:

Add Following maven dependencies in project pom.xml to include OData Olingo and Apache CXF Jars

/wp-content/uploads/2016/10/2_1032329.png

3. Configuration and Implementation

3.1 Configure persistence.xml in JPA model

 

Open persistence.xml file from <ProjectName>/Java Resources/src/META-IN and Add the following yellow-marked <property> line to the persistence.xml file

 

property name=“eclipselink.target-database” value=“org.eclipse.persistence.platform.database.HANAPlatform”

/wp-content/uploads/2016/10/3_1032330.png

 

 

3.1.1 Configure resource.xml in JPA model

 

In resource.xml describes in cloud foundry, when application is pushed it will read the resource file about the HDI container to connect to Database, Here we have defined the provider, which can work with TOMEE-1.7 version or with tomcat8 (but we need to include the jar cxf-rt-frontend-jaxrs 2.7.8 version in pom.xml). Here service=sampleTest is the persistent unit name which has to define in web.xml, and JtaManaged is false, so that the JTA transaction will be maintained by Entity Manager factory.

 

/wp-content/uploads/2016/10/4_1032331.png

 

3.2 Create Required Java Classes

 

3.2.1 Create Factory class for Managing Connection with Database using Server Context (eg. JpaEntityManagerFactory.java)

/wp-content/uploads/2016/10/5_1032336.png

3.2.2 Create new Java class by extending ODataJPAServiceFactory to provide means for initializing Entity Data Model (EDM) Provider and OData JPA                     Processors.

/wp-content/uploads/2016/10/6_1032337.png

 

3.3 Configure web descriptor file (web.xml)

 

Configure the web application as shown below by adding the following servlet configuration to web.xml. The Service factory (e.g. SampleCustomOdataJpaServiceFactory) which was implemented is configured in the web.xml of the OData Application as one of the init parameters.

 

 

/wp-content/uploads/2016/10/7_1032344.png

4. Testing OData Service

 

Publish and start web application (Contain JPA Entity) on Cloud foundry, XSA or local web server and Open the application URL and append recourse path provided in web.xml for accessing OData Service,

 

/wp-content/uploads/2016/10/8_1032345.png

 

Test Other OData operations using following tutorial http://www.odata.org/getting-started/basic-tutorial/

As well as you can check the ODATA APIs using Advance rest client or Postman (Both are Chrome extension). You can test all the method like GET, PUT, POST, DELETE etc. using Advance rest Client.

 

/wp-content/uploads/2016/10/9_1032351.png

 

 

5. Custom OData Processing

 

Most of the time in web project we need to provide custom pre and post processing with default OData processing like

  • Adding additional filters or select filter to restrict usages of OData GET Request
  • Manipulating results of GET Request
  • Triggering some functionality in case of new Entity creation
  • Filled non-provided data for Entity.
  • Redefining metadata like renaming of any columns or any renaming any entity class name.

 

 

5.1 Create Custom JPA Processor for OData Request

Extend default implement class org.apache.olingo.odata2.jpa.processor.core.ODataJPAProcessorDefault for OData Processing handling with your own Custom Processor Class like e.g. Class CustomoDataJPAProcessor in following screen shot and override method for different operations on case by case basis

    

5.1.1. Override readEntitySet Method for extending GET request to get entitySet as return – The instance variable jpaProcessor can be used to process the OData request. The jpaProcessor returns the JPA entities after processing the OData request.

/wp-content/uploads/2016/10/10_1032352.png

 

5.1.2. Override createEntity method for extending POST request to create new record for entity –  create private method to manipulate entity record and add any additional processing, You can see other default method in default JPA Processor class ODataJPAProcessorDefault, 

/wp-content/uploads/2016/10/11_1032359.png

5.1.3 Use CustomODataServiceFactory for extending your service factory class instead of using default ODataJPAServiceFactory

Change extension for class created in step 3.2.

/wp-content/uploads/2016/10/13_1032360.png

 

 

References:

What is the Open Data Protocol (OData)? http://www.odata.org/

What is JPA (JAVA Persistence API)? http://wiki.eclipse.org/EclipseLink/FAQ/JPA

Why does SAP prefer OData as connectivity?

These two SCN blogs motivates the reasons why SAP prefers to use OData as connectivity:

http://scn.sap.com/community/open-source/blog/2013/08/12/sap-odata-library-java-contributed-to-open-source

http://scn.sap.com/people/ralf.handl/blog/2011/06/09/sap-netweaver-gateway-speaks-odata-with-sap-annotations

 

 

 

Hope this blog covers the topic of setting up and implementation of OData services using Apache olingo API. In next part of my blog I will be explaining about the Entity class overview in JPA based web application.

I have checked in my code to github.

 Few of my other blog post about the Logging in cloud foundry using jolokia apis

Logging in HCP Cloud foundry with Java and Tomee using slf4j, logback, Jolokia

CDS extension feature in HANA

To report this post you need to login first.

4 Comments

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

  1. Austin Kloske

    Thank you for this blog series!

    Have you or are you willing to share the project .war file or source code via github?

    If not, is the project this blog was initiated with available on Github?

    The JAVA applications I’ve attempted to use thus far, do not contain both persistence.xml and resources.xml as mentioned as a pre-requisite. 

    Either would be extremely helpful! Thank you in advance – Austin

    (0) 
    1. Rajnish Tiwari Post author

       

      Hi Austin,
      Thanks for your positive feedback !!

      In the above blog, I have mentioned the code for persistence.xml refer section 3.1 Configure persistence.xml in JPA model, for resource.xml refer section 3.1.1 Configure resource.xml in JPA model.

      This is what you need to add code snippet in persistence.xml and resource.xml files as mentioned in project structure.

      Will inform, once the code is uploaded in github.

      Thanks,
      Rajnish

      (0) 
  2. Austin Kloske

    Rajnish,

    I want to clone the project that you initiated the tutorial with (section 2.1) so that I can follow along and deploy this tutorial on my HCP trial account. Is the project you used as a Pre-requisite in section 2.1 available? If not, do you have know of another github based JPA  Project that is compatible with this tutorial?

     

    Thank you in advance!

    (0) 

Leave a Reply