Connect to SuccessFactors oData from Hana Cloud Platform

I’ve been working in the SAP HR industry as a developer and an architect for the last 15 years and I must say that I am really impressed by the speed of all the changes that SAP has brought within the last 2 years. For developers, a really amazing journey is just beginning.

A couple of weeks ago, I started playing around in SuccessFactors to see how easy it would be possible to build some extensions, it turned out it would not be that easy… I learned a lot over the first few weeks, including:

  • The architecture of SAP HANA Cloud Platform is something all developers must understand.
  • oData is a very powerful protocol that I have learned to love and I must say that the exposed service from SuccessFactors is really well done.
  • The basics of Java can still take me out of my comfort zone.

To get comfortable with all of these technologies, I decided to work on a small project this week that would just allow me to connect from HCP to SuccessFactors and get information from the metadata. For this little project, I worked with my friend Jonathan Jouret, and all I can say is that it was not a walk in the park (especially for novices like us in Java!).

For all the Java gurus, please don’t throw me rocks for the code I published. While everything here might seem very obvious, I think for a SAP developer, it might be interesting.

oData libraries

As newbies in Java, we googled “oData Java” and found out that there were a few libraries out there that we could use. So Jon and I decided to take one each and try to make them work.

I picked Restlet (apparently a standard in the industry) and Jon picked oData4J from Google. After a couple of hours, we managed to get some data from SuccessFactors using both libraries but we failed in getting some SuccessFactors specific annotations.

In the following example, we were able to get most of the oData standard information, but we were also interested in getting the “Label” (here below: Address) and the oData “sf:” attributes. On Restlet, I tried to inherit a few classes and get my way through the source of the XML, but I quickly gave up.

Connect to SuccessFactors oData from Hana Cloud Platform

I asked help from Krassi, who told me that SAP was using Olingo (from Apache) as a library. We went back to our computers with the motivation to get the data we wanted, and while listening to Rammstein to give us a rhythm, we managed to get everything we wanted after just a few hours.

I tried to load these libraries with Maven but although my code was building, at runtime I had an error so I just put the libraries in the build path.

Some code

In the snippet, you will find some code we used that we hope can help you get started.

Eclipse Configuration
We started with an Eclipse that contains all HCP tools, etc., and to make our life easier, we created a Dynamic Web Application. We chose this because we wanted to call a webpage and return a JSON with the info we needed. An important note is that the Tomcat Server should be configured with the link and credential from SuccessFactors.

Connect to SuccessFactors oData from Hana Cloud Platform

In this example, I created the destination (under connectivity tab) “sap_hcmcloud_core_odata” and put the URL and the credentials. Note that with this destination name is standard and will make your life way easier when you’ll deploy your application.

Getting connectivity information from the context
SAP is making our life easy by providing us with some nice libraries. I used the following libraries:

import com.sap.core.connectivity.api.configuration.ConnectivityConfiguration;
import com.sap.core.connectivity.api.configuration.DestinationConfiguration;
The following code is used to get the destination configuration:

Context context = new InitialContext();
// Get the connectivity settings from the context
ConnectivityConfiguration configuration = (ConnectivityConfiguration) context.lookup(“java:comp/env/connectivityConfiguration”);
this.destConfiguration = configuration.getConfiguration(“sap_hcmcloud_core_odata”);// Create user/password token + Authentication
String userPassword = destConfiguration.getProperty(“User”) + “:” + destConfiguration.getProperty(“Password”);
this.userPassword = “Basic ” + new sun.misc.BASE64Encoder().encode(userPassword.getBytes());

Getting the metadata
So, once we got all the information we needed to connect to SuccessFactors, we just had to call the service and have an EDM object created.

// Construct the URL for the metadata
String urlMetadata = destConfiguration.getProperty(“URL”) + SEPARATOR + METADATA;
URL url = new URL(urlMetadata);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod(HTTP_METHOD_GET);
connection.setRequestProperty(“Authorization”, this.userPassword);
connection.setRequestProperty(HTTP_HEADER_ACCEPT, APPLICATION_XML);
connection.connect();
checkStatus(connection);
// Get the metadata from the server
Edm edm = EntityProvider.readMetadata(connection.getInputStream(), false);
connection.disconnect(); // get some memory back
The metadata is loaded in the edm object… from there a lot of methods are available to get necessary information from the metadata.

And the rest of the code
Our challenge was to get some labels and <SF:> elements, and we managed to do that with the following code. Some of these information are “hidden” deep in the objects.

You can have a look at the files attached to this blog and more specifically at the method “loadAnnotations” and “loadSFAttributes”. I am happy to provide more explanation if necessary.

Here is the link to the github.

HANA Cloud Platform
The code can be published on HANA Cloud Platform and works if the proper destination is set up… I havent tried yet with the SSO but I am sure it should work (I’ll give it a try in a couple of days).