Skip to main content
Version: v11.9.2

How to Design a workflow in Camunda BPM and integrate into a WaveMaker app


Overview

In this article you will learn how to add workflow into a WaveMaker application by integrating with a opensource BPM product - Camunda. We will use the flexibility of the code generated by WaveMaker and call rules modelled in Camunda.

For demonstration purpose, we will use Employee Leave Management application. Employee will apply for the leave using UI and the request will go to the backend where Camunda business rules engine is integrated. Based on leave balance, Camunda REST API will return the list of approvers to process the request.

STEP 1 – CAMUNDA SETUP

  1. About Camunda:

Camunda BPM is a light-weight, open-source platform for Business Process Management. Camunda is implemented in Java and supports BPMN for workflow and process automation, CMMN for Case Management and DMN for Business Decision Management.

  1. Getting Started With Camunda https://docs.camunda.org/get-started/quick-start/

  2. Download and Install Camunda Note: For Integrating WM online server, Please install Camunda services on cloud such as AWS/Azure.

  3. Create BPMN Process for Apply Leave Request Below is the process flow create in Camunda. lftr_sel

  4. Create DMN(Decision Management) table for setting up rules Below is the decision table created using the Camunda DMN.

    lftr_sel

  5. Create a REST End-point for Camunda WorkFlow Engine You can create the Camunda REST endpoint url. Refer to this Camunda documentation Example: POST /decision-definition/key/{key}

Request:

    POST http://<camunda-host>/engine-rest/decision-definition/key/approve_leave/evaluate`
HTTP Headers: Content-Type="application/json"
Request Payload:
{"variables":{"applied":{"value":3,"type":"Integer"},"leftBalance":{"value":10,"type":"Integer"}}
}

Response:

Response Payload: This will return the list of all Approvers needed for a leave request.

    [
{
"secondApprover": {
"type": "String",
"value": "Director",
"valueInfo": {}
},
"firstApprover": {
"type": "String",
"value": "Manager",
"valueInfo": {}
}
}
]

STEP 2 – Build Leave Management WaveMaker App

In leave management application, employee will apply for the leaves entering start date & end date. Note: Initial leave balance for all the employees will be 10. Depending on the applied leave by employee Camunda workflow will process it and request will go to the respected person i.e. Manager, Director or HR.

Create the UI in WaveMaker for Leave Management application:

lftr_sel

STEP 3 – Integrate Camunda REST APIs for Business Rules Evaluation

WaveMaker generates REST API needed to perform Create, Read, Update, Delete (CRUD) operations, when you import a database. This REST API allows customisation via addition of pre & post processing hooks. To learn in more detail about this feature, refer to this page.

Import the REST endpoint url

Import the Camunda REST Web Service from WaveMaker’s Web Service section. Note: Please make sure to add Content-Type value as application/json in Header Params.

lftr_sel

Implement the Pre-processing service before invoking Database API

        @Override
public LeaveRequest create(LeaveRequest leaveRequest) {
String camundaResponse = invokeRestService(leaveRequest);

//Write the actual invocation method..
private String invokeRestService(LeaveRequest leaveRequest){
String serviceId = "restService"; //serviceId which we need to invoke
String operationId = "invoke"; //default operation provided
Date dateBefore = leaveRequest.getStartDate();
Date dateAfter = leaveRequest.getEndDate();
int appliedDays = daysBetween(dateBefore, dateAfter);
int leaveBalance = leaveRequest.getBalanceLeave();

HttpRequestData httpRequestData = new HttpRequestData();
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Content-Type", "application/json");
httpRequestData.setHttpHeaders(httpHeaders);

try{

JSONObject leftBal = new JSONObject();
leftBal.put("value", leaveBalance);
leftBal.put("type", "Integer");

JSONObject applied = new JSONObject();
applied.put("value", appliedDays);
applied.put("type", "Integer");

JSONObject variables = new JSONObject();
variables.put("applied", applied);
variables.put("leftBalance", leftBal);

JSONObject main = new JSONObject();
main.put("variables", variables);

httpRequestData.setRequestBody(new ByteArrayInputStream(main.toString().getBytes(StandardCharsets.UTF8)));

HttpResponseDetails httpResponseDetails = restRuntimeService.executeRestCall(serviceId,operationId, httpRequestData);
return IOUtils.toString(httpResponseDetails.getBody(), "UTF-8");

}catch(Exception ex){
throw new WMRuntimeException("Some error encountered in Camunda invoke: "+ ex, ex);
}

STEP 4 – Database Design for the WM app

The following is the database design in WaveMaker for Leave Management Application. The model has employee details and his leave balance along with leave requests and history. Model has "user" table which has different role like HR, Manager and Director who are responsbile for leave request approval.

lftr_sel

SUMMARY

In this article you have learnt step by step approach on Camunda BPM workflow integration with WaveMaker generated prebuilt CRUD operations REST APIs. The following are the highlights:

  1. How to invoke external workflow engine like Camunda exposed REST APIs by importing into WaveMaker WebServices interface.

  2. Leveraging WaveMaker generated prebuilt Database APIs and intercepting in pre-processing hooks to call Camunda REST APIs

  3. Externalizing Business Rules onto Camunda decision flows so that any change in business rules WavaMaker app is not affected.

  4. Able to demonstrate a live application like Employee Leave Management, where Camunda returns list of leave approvers while the actual UI interaction is built in WavaMaker. WaveMaker can trigger automatice emails to the list of leave approvers.

See Also

How to send emails using Java Service
How to access REST APIs from Java Service
How to schedule a Java Service
How to accomplish Pre-Post Processing for a DB Service APIs