Friday, May 6, 2016

Service Task Example in WSO2 Business Process Server

This tutorial illustrates how to model a business process with Service Tasks. Here as an example I will describe a sample bonus payment process.

Steps of the use-case


1. Obtaining the customer information with their salary and working period
2. Adding (working period * random number) to the salary of the employee
3. Manager approval (approve/reject) the bonus amount

The above components can be matched with the components in activiti pallet section as below.

Start Event : Filling the details about the employee such as employee id, name, salary and the working period.
Service Task : use to automate the calculation mention in step 2
User Task : is used to approve or reject the bonus amount for the given employee

Prerequisites


  • Java
  • WSO2 BPS
  • Eclipse activiti-designer plugin

Implementing the java class  to be used in Service Task


First create a maven project in eclipse and apply the following dependency to the pom.xml file.
(The jar file should be copied into <BPS_HOME>/repository/components/lib folder before starting the BPS Server)

<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-engine</artifactId>
<version>5.19.0</version>
</dependency>

Java class implementation.

package org.wso2.bps.serviceTask;

import java.util.Random;

import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;

/**
 * Service task to calculate Bonus for employees
 *
 */
public class App implements JavaDelegate {
public void execute(DelegateExecution execution) throws Exception {
int salary = Integer.parseInt((String) execution.getVariable("employeeSalary"));
int numOfWorkingDays = Integer.parseInt((String) execution.getVariable("workingPeriod")); 
Random randomGenerator = new Random();
int value = randomGenerator.nextInt(10);
int result = salary + (numOfWorkingDays * value);
execution.setVariable("result", result);
}
}

Modeling the process utilizing eclipse activiti-designer tool


First create an activiti project in eclipse (See the steps below)

  1. Go to File -> New -> Other - > Activiti and select Activiti Project.
  2. Then enter activiti project name and click Finish.
  3. Right click on the created project and select New -> Other -> Activiti Diagram and click Next.
  4. Give a name for the process and click Finish.


Design the user-case using the pallet components dragging and dropping to the canvas layer in the tool as showing in the following figure.

Configuration of each components


Configure properties tab of the start event as below.

Form tab - Define the variables to be initialized in the start event


Main config tab -  Define the initiator of the process




 Configure properties tab of the service task as below.

Main config tab - the relevant java class that contains the logic should be specified here.  You should have to create the java class before configure the main config of the service task. (I will explain this class implementation later.)



Configure the properties tab of the user task as below.

Form tab
(Here the result variable coming from the Service task)


Main config tab - Assign a candidate group for the user task. For the claiming purpose you should have to create a user and assign the candidate group role to him and login with the newly created user to approve the bonus amount when process execution comes to the user task.



Save the project and right click on the activiti project in package-explorer in eclipse and click Create Deployment Artifacts.

Now you can see that a .bar file is generated inside of the deployment folder in the project.


Deploying and testing the bonus payment process using WSO2 BPS 


Start the BPS Server
Log in the management console and navigate to Home>Manage>Add>BPMN. Upload the .bar file to deploy it as seen below. 



Create a new user called kermit and assign him to the bonusApproval role.







Login to the BPMN-explorer using admin/admin credentials (default admin user credentials) and start the bonus payment Process.






Now login to the BPMN-explorer using kermit/kermit credentials to approve the bonus payment.




No comments:

Post a Comment