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.
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.
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).
In a next blog, I’ll probably show an example on how to run a Query… but I think that for this, I might use Restlet as it is easier (at least, with my few days of experience, that’s my current conclusion).
Have fun and let me know if you think it could be done better, if it helped you or if you need more information.
Rammstein is the key to most of our dev problems 😉
Nice post, clear and complete...
Hello Greg,
Very informative blog.
I have a question. Can I pull data from HANA Views into Success Factors? If so, how? Can you give me some pointers?
Hey,
Can you please give me more details ? My understanding is that you want to read data in a HANA database and display it in a tile in SuccessFactors ?
Cheers
Greg
Hello Greg,
Thanks for your prompt response. You got it right. Currently I am pulling data from HANA database through calculation views and exposing them through Odata Service to be consumed in Fiori (Analytical App).
I would like to know if I can consume data from HANA (Analytical/calculation views) and display it in a tile in Success factors. Since success factors is cloud based how can we achieve this ? Any inputs are highly appreciated.
Thank you
Hello,
Sorry for the late reply, I was on leave for a few days... Anyway, the way we currently achieve this is by creating an UI5 application on HCP and consume the data via oData. We created a generic component in SuccessFactors and put the link to this application within an iFrame. Since there is SSO between SFSF and HCP, it works quite well.
If you're certified with SuccessFactors, I think you're allowed to create native extensions but I have no experience yet with this.
Cheers
Greg
Thank you Greg.
Hi Greg,
Olingo is definitely the way to go for this sort of thing, I find the hardest part the conversion to and from pojos, I'm sure there are ways to do this more easily using annotations but I haven't yet found this to be easier - would be interested if you had any view on that.
Although you may have looked at it, I'd also look closely at using OAuth2SAMLBearerAssertion to connect yourself to SuccessFactors - SAP HANA Cloud Platform
You should be able to create the destination connection in exactly the same way no matter what auth you are using - so if using basic to test on local development, or OAuth2 SAML Bearer when deployed to HCP.
Check out the example SAP HANA Cloud Platform - which is pretty cool. Perhaps one of the best reasons to use the Java Web profile over the Tomcat7 one is the more fully featured connectivity features.
Nice blog post.
Cheers,
Chris
Hi Greg.
Great post! This is exactly what I was looking for.
I tried testing your application but I get an error with the connectivity import. How can I include this SAP library?
Hello Tiago,
Thanks 🙂 I think you might have overlooked to include the libraries from SAP in Eclipse. If this is the case, you need to follow this guide https://tools.hana.ondemand.com/#cloud
If it does not work, I am happy to help.
Cheers
Greg
Hi Greg.
The project was not compiling with the SDK. I included it manually and it worked. Thank you for your help. 🙂
Hello Greg,
I come from an SAP HCM world and very new to JAVA. I am also trying to achieve access of data from SF to HCP. Your article has given me confidence.
I was trying to import the code which i extracted from the GIT. However, com.sap.core.connectivity.api.configuration.ConnectivityConfiguration and com.sap.core.connectivity.api.configuration.DestinationConfiguration is not found. Is there any specific library i need to import or all the cloud tools installation should have ensured it. I am using neo-java-web-sdk-1.78.16.
Regards,
Tarun Mishra
Hello Tarun,
Thanks for the kind words... This sample will only work with Tomcat, I think you downloaded the Java Web version...
Note that I think you did great by downloading the Java Web version as I think it can handle better the connectivity to SuccessFactors.
Give me one day and I'll publish a sample of code working on Java Web.
Cheers
Greg
Thank you so much for your support.. it would really help!!
Thank you very much Greg, a very useful post.
Have you published the Java Web/Java EE Version 6 Web Profile version?
I'm looking forward to seeing his next post.
Regards.
Juan C.Orta