How to integrate BOS engine in a Grails application

I often see people in the forum trying to integrate BOS engine in an MVC framework such as spring MVC, Zend framework or even Grails. In this article, we will explore this kind of integration using the Grails application as an example.

We will look at how to replace the instantiation form of a process using Grails, then continue the process in Bonita User Experience. An example usecase could be when you want an interface that presents a form where visitors can request a phone call from the Bonita Sales team. In this article, we’ll use the process Buy a mini provided with Bonita Studio.

The sourcecode can be downloaded at github [1]

Before looking more closely at code, let’s check how we will set up the architecture of our integration. Basically, BOS will run on its own server and the Grails webapp will access it remotely. In this kind of integration, two types of access are possible: via EJB or the REST API. To make things simple, we will use the REST API (which is exposed by the studio by default).

First, let’s deal with BOS. We’ll use the engine integrated in the studio and the example process: buy a mini. Download BOS [2] and open the example “Buy a mini”.

Start the process in Studio and switch to Bonita User Experience.

Let’s jump now to your favourite IDE and create a new grails application (init tag).

The first thing to do is to make bonita-client a dependency. Grails dependency configuration takes place in grails-app/conf/BuildConfig.groovy.
So, now let’s add bonita-client in the dependency section. As some libraries needed by BOS are already present in Grails, you must ignore log4j-jdk4 and scannotation using the excludes directive.
Your dependencies section should look like the code below:
[cc]
dependencies {
runtime(“org.ow2.bonita:bonita-client:5.6.3”) {
excludes “scannotation”, “slf4j-jdk14”
}
}
[/cc]
Now that this dependency is installed, let’s create our web application.
To do this, start by creating a controller. Let’s name it ProcessController:
[cc]
grails create-controller process
[/cc]

Let’s now create a method to list deployed processes.

[cc]
def list_processes() {
def callbackHandler = new SimpleCallbackHandler(‘Guest’, ‘’)
LoginContext loginContext = new LoginContext(‘BonitaStore’, callbackHandler)
loginContext.login()

    QueryDefinitionAPI queryDefinitionAPI = AccessorUtil.getQueryDefinitionAPI()
    def processList = queryDefinitionAPI.getLightProcesses()*.getUUID()

    loginContext.logout()

    [processList: processList]
}

[/cc]

The corresponding view (process/list_processes.gsp) will display this processes in a list :

[cc]
<g:each in=“${processList}” var=“processUUID”>


  • ${processUUID}


</g:each>
[/cc]

As we are using JaaS to transmit the requester to the API, we’ll have to create a JaaS configuration file. This file will be placed at the root of the grails project.

[cc]
BonitaStore {
org.ow2.bonita.identity.auth.BonitaRESTLoginModule required restUser=“restuser” restPassword=“restbpm”;
};
[/cc]

Then, let’s try our project. You’ll have to configure bonita-client by setting this JVM arguments :
[cc]
-Djava.security.auth.login.config=“jaas.cfg”
-Dorg.ow2.bonita.api-type=“REST”
-Dorg.ow2.bonita.rest-server-address=“http://localhost:9090/bonita-server-rest/
[/cc]

In starting the web application, the deployed process Buy a mini should be displayed

This state is accessible from the git tag list_processes

Right now, it would be nice to have the possibility to start the process by clicking on the item buy a mini.

To do this, in list_processes.gsp, replace the name of process by a link to the controller start buy a mini.

[cc]
<g:each in=“${processList}” var=“processUUID”>


  • <g:link action=“start_Buy_A_Mini” params=“${[process:processUUID]}”>${processUUID}</g:link>


</g:each>
[/cc]

The corresponding method will only pass the processUUID to the instantiation form

[cc]
def start_Buy_A_Mini() {
[processUUID: params.process]
}
[/cc]

And the form will look like the following:

[cc]
<g:form action=“instantiate_process”>

<g:hiddenField name=“processDefinitionUUID” value=“${processUUID}” />


Select a car
<g:select name=“car” from=“${[‘MINI Convertible’, ‘MINI Hardtop’, ‘MINI Clubman’]}” value=“MINI Hardtop” />


<g:submitButton name=“Validate” />


</g:form>
[/cc]

When clicking on the submit button, the form will send the model to the controller named instantiate_process.

[cc]
def instantiate_process() {
def callbackHandler = new SimpleCallbackHandler(‘admin’, ‘’)
LoginContext loginContext = new LoginContext(‘BonitaStore’, callbackHandler)
loginContext.login()
RuntimeAPI runtimeAPI = AccessorUtil.getRuntimeAPI()
def processUUID = new ProcessDefinitionUUID(params.processDefinitionUUID)
Map<String, Object> parameters = new HashMap()
parameters.put(‘car’, params.car)
runtimeAPI.instantiateProcess(processUUID, parameters)
loginContext.logout()
redirect(action: ‘list_processes’)
}
[/cc]

Save and refresh the web page. The instantiation cycle should now be completed. Now click on Buy a mini in the list to start the process when you validate the form.

In the Bonita User Experience, you should now see the started process

The full code is accessible in github from the tag start_process

Links

  1. https://github.com/bouquetf/BOS_in_Grails
  2. http://www.bonitasoft.com/products/BPM_downloads