AVIO Consulting

BPM 12c – BPM Object Scripting Using Groovy

Jan 29, 2015 | BPM

Oracle BPM 12c has a new feature included that allows for BPM scripting using the Groovy language. This was missing in BPM 11g and is very similar to the feature that was available in BPM 10g that allowed for BPM scripting using its own scripting language – PBL (Process Business Language). There are two primary places you can use Groovy in 12c:

  1. BPM process Script Task.
  2. BPM Business Object method.

Since the Oracle A-team has covered the introduction to Groovy and the use case for using Groovy in the BPM process Script Task beautifully in this blog – Oracle BPM 12c just got Groovy – A Webcenter Content Transformation Example, I will hop onto the next one, the BPM Business Object method. It will be a hard act to follow but I shall try.

In 12c you have the ability to add methods (or functions) to your BPM Business Objects. It is these methods that are written in Groovy. You do not want to stuff your business logic into this code though. You want to leverage BPM process models, data associations, XSL transformation, business rules, web services, and DB to execute your business logic. This is stated by the Oracle A-team as well in their blog; “If you can easily code, then it is easy to write code to do everything. But this goes against what BPM is all about. We must always first look to leverage the powerful middleware infrastructure that the Oracle BPM execution engine sits on, before we look to solve our implementation challenges with low level code.” However, there are still a number of valid use cases and advantages for creating BPM Business Object methods.

The use cases for using these methods are:

  1. Adding default values: A method can be created to add default values to the Business Object’s data elements that are null or empty.
  2. Normalizing values: A method or method(s) can be created to normalize the Business Object’s data elements, for example, US state abbreviations need to be uppercase or all dates need to be a certain format.
  3. Performing complex calculations: A method or method(s) can be created that can perform complex math calculations or date calculations or string manipulations that would be extremely hard to create and then maintain in XSLT.

The advantages of using these methods are:

  1. Reusability: These methods can be called from and reused in all BPM processes where that Business Object is used.
  2. Maintainability: These methods hold functionality that is specific to the Business Object and does not necessarily affect or guide the Business logic. Having that separation from the Business logic and having it contained in its own area under the Business Object promotes long term maintainability.

In the example below, we will cover the performing complex calculations use case. We will create a BPM Business Object method that calculates the difference between two date-time values and returns a “how much time do I have” Duration code. Here are some examples of Duration code:

PT5M – five minutes

P1D – a day

P1M – a month

P1Y1DT1H1S – one year, one day, one hour, and one second

I did the same use case in my previous blog in 11g using a BPEL process with a Java embedding component. This time around we will do it in 12c using a BPM Business Object method.

  1. In JDeveloper 12c, create a BPM project and the appropriate Business Object(s). Expand the Business Components section and open a Business Object to create the method. I have not used the proper naming conventions, “AustinPowers” (Groovy, get it?), “BOOrder2”, “d1”, “d2” etc., as this is a sample project, but please make sure you use better names than I did.

     

  2. In the Business Object, under the Methods section, click on the + icon and give the new method an appropriate name. 

    methods

     

  3. Click on the Edit icon to change the signature on the method. Here we can add/remove/edit input arguments (unlimited) and output arguments (only 1). The Business Object’s data elements are readily available in the method and they do not need to be arguments to/from the method. In this example, we have no input arguments and a String output argument.

     

    return type string

     

  4. Click on the Select Imports button to add the appropriate libraries that are needed to write the Groovy code. Click on the + icon and start typing the Library name. You can reference the available libraries under the Scripting Catalog section, as shown below. In this example, we need two libraries: java.text.SimpleDateFormat and java.util.Date.

    select imports

     

  5. Now write the Groovy code in the method’s code editor window. For this example, copy/paste the following code. As stated earlier, we are calculating the difference between two date-time elements from this Business Object and returning a Duration code as a String.
    try {

    SimpleDateFormat format = new SimpleDateFormat(“MM/dd/yyyy HH:mm:ss”);
    String diffDatePeriod = “P”;

    Date d1 = format.parse(this.orderDateStart);
    Date d2 = format.parse(this.orderDateEnd);

    long diff = d2.getTime() – d1.getTime();
    long diffSeconds = ((int)diff / 1000) % 60;
    long diffMinutes = ((int)diff / (60 * 1000)) % 60;
    long diffHours = ((int)diff / (60 * 60 * 1000)) % 24;
    long diffDays = ((int)diff / (24 * 60 * 60 * 1000));

    diffDatePeriod = diffDatePeriod + diffDays + “DT”;
    diffDatePeriod = diffDatePeriod + diffHours + “H”;
    diffDatePeriod = diffDatePeriod + diffMinutes + “M”;
    diffDatePeriod = diffDatePeriod + diffSeconds + “S”;

    System.out.println(“BOOrder2.dateDiffAdDuration()::diffDatePeriod: ” + diffDatePeriod);
    return diffDatePeriod;

    } catch (Exception e) {
    System.out.println(“Exception on BOOrder2.dateDiffAdDuration(): ” + e.getMessage());
    }

     

    Strign data diff as duration

     

  6. The BPM Business Object method is complete. It should look similar to as shown below.

     

    BOOrder2

     

  7. Now, we need a way to call this method. Create a BPM process that uses the Business Object that has the method we just created. In this example, the Business Object is also the input argument to the BPM process. Create a Script Task in this BPM process. Right-click the Script Task and click on Go To Script.

     

    go to script

     

  8. This will open the Groovy scripting tab. Make a call to the BPM Business Object method. In this example, since the method returns a String, assign the returned string value to a variable, as shown below.

    scripting

     

  9. Deploy the composite and test it from your BPM process. Provide the two date-time values that will be used in the calculation as part of the input arguments to the BPM process. In this example, the difference is 1 minute and 30 seconds between the two dates.

     

    strings

     

  10.  Once the Groovy code is executed, the Duration code P0DT0H1M30S, which translates to 1 minute and 30 seconds is printed in the logs, as shown below.

     

    string key

     

  11.  Later, we also used this Duration code as an SLA value for a Catch Timer Event in the BPM process. The SLA was set on the Catch Timer Event and then fired after 1 minute and 30 seconds.

     

    catch event 1

     

Since this is a method inside the Business Object, it can be reused in any other BPM process that uses this Business Object. This BPM scripting feature in Oracle BPM 12c, added via Groovy, is a very powerful and handy tool. Even though it is not always the best option, there are a few use cases where using it has some clear advantages over other techniques, as in the use case shown above. Please feel free to leave me your questions and comments.