First of all, before starting to write any code, one has to install the Oracle Primavera P6 Integration API. The official Oracle Primavera P6 Documentation will tell you how to do that. Having installed the P6 Integration API, the next step for any would be P6 programmer is to write code that will login to a P6 Java API Session so they can work their developer magic on P6.
No matter what else goes on in any application using the P6 Java API, they all have to create a session and log in, therefore, the class shown in this post should be useful. The P6 Java API can be run in local mode, where all the work is done on the same machine as the application, or it can be run in the remote mode where a web server does the heavy lifting and the application tells it what to do and gets the results back.
In this article, I’ll share the class I use to get logged in to Primavera P6 in either mode and consequently get started fairly quickly in any environment. It’s more of a helper class than anything else and as a result saves writing the same kind of code over and over again. The full code listing is at the end of this article, but first of all, I wanted to spend a little time just walking through it.
All those Imports
The class is called PrimaveraP6 to distinguish it from other Primavera tools such as Unifier where I would call the equivalent class PrimaveraUnifier. The class definition starts off with all the imports that are needed to create a P6 Java API session. Anyone new to programming with the Oracle Primavera P6 Java API will soon learn there are a lot of imports required to do some simple things.
In the case of PrimaveraP6, the Session and RMIURL classes are used to login and there are three exception classes that can be thrown by the login method. In fact nearly every Primavera P6 class seems to throw com.primavera.ServerException, com.primavera.integration.client.ClientExceptiPrimaveraon and com.primavera.integration.network.NetworkException.
There are two final imports as I make use of the Java logging classes. So here are all the imports used.
The way the class is instantiated defines whether it will attempt to use the Remote or Local mode of the P6 Java API session. There are 3 constructors. The first has no parameters and is for the Local mode. The other two are for Remote mode, where one requires both the hostname and port, whilst the other requires the hostname and uses the default port of 9099. These constructors set some private members that are the most common configuration.
Setting the right protocols
Once the PrimaveraP6 class has been instantiated there are various methods to make changes like using SSL. There aren’t a load of constructors to handle all these scenarios as most clients don’t use SSL or HTTP tunnelling and I like simple classes that set the most common settings by default.
The reason most clients do not use SSL with Primavera P6 appears to be because there are so few people who know how to set it up. Most forget to configure the database to use SSL. Maybe a post for another day.
There is a method (setSSL) to set the Remote mode to use SSL and another method (setStandard) to not use SSL. There’s even a method (isSSL) which reports if SSL is configured to be used or not.
In a similar fashion the setHTTPPort and unsetHTTPPort methods are used to set up or remove the configuration for HTTP tunnelling. (N.B. I have never used this and so it hasn’t been tested).
Login to Primavera P6
Finally the login method, which just calls login on the Session P6 Java API class. It returns an instance of the Session class which we can then use in our application. Typically I would use the class as in the following example, although the strings wouldn’t be hard-coded:
The class is pretty simple but can save a lot of time when trying to log in. It really deals with the more awkward aspects of setting up a connection. Using it means that it is very easy to create applications that can be configured at runtime to use either a Remote or a Local connection.
Here is a complete class and may your application development with the P6 Java API be a happy one.
Get Newsletter Updates
Subscribe to our newsletter to hear what we have been writing and hearing about.