This blog discussed how you can use an Activiti datamodel to interact with customer data that is stored in a database that is external to activiti. A process will often require information from an external data source. In previous blogs we have looked at integrating Flowable to camel in order to set and retrieve information that is stored outside of the process engine. There are many circumstances however where organisations do not support a services oriented architecture but use a database with a centralised store for enterprise information. The use of a datamodel that is directly linked to an external database through a datasource allows us to achieve CRUD operations on centralised enterprise information like a customers details.
The blog follows the following steps:
- Create a customers table in a database. This will act as our entity information for the datasource.
- Configure an activiti datasource to connect to the database customer entity.
- Configure a datamodel which maps the customers properties into the datamodel.
- Create a Workflow definition that creates a new customer record in the database and later reads and updates it.
- You will need an Activiti Process Services (Activiti 6) instance to be installed and running.
- Have an existing mysql Database Schema that we can create a customer table in.
- The mysql JDBC driver is installed in the Activiti App lib directory. This is required to connect the activiti datasource to the mysql database that holds the customer table.
Create Customer Entity
To create the customer entity table log into mysql and run the following mysql table create:
CREATE TABLE IF NOT EXISTS customers ( customer_id INT AUTO_INCREMENT, name VARCHAR(255) NOT NULL, age TINYINT NOT NULL, PRIMARY KEY (customer_id) ) ENGINE=INNODB;
Run a sql describe command to ensure that table has been created correctly:
mysql> desc customers; +-------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------------+--------------+------+-----+---------+----------------+ | customer_id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(255) | NO | | NULL | | | age | tinyint(4) | NO | | NULL | | +-------------+--------------+------+-----+---------+----------------+
Create Customer Data Model
Prior to creating the data model we need to create a Data Source that connects to the external database. The Data Source dialog is located in the Tenants menu within the Identity Management App of the activiti-app webapp.
Create Data Source
- Navigate to the Tenants admin page and click on the Data Source menu in the Left Navigation.
- Click on the + button under the datasource list. This brings up the edit Datasource dialog.
- Add a value for the name, in our case we called it customers to match the table we are going to reference.
- Add a value for the database url and driver. Our database is called seedbpm and is a mysql db so uses the jdbc driver for mysql.
- The username and password should be a user that can connect to the db and have both read and update permission on the customers table.
- Click the Test connection button to verify your config works. This is pretty handy as it allows you to troubleshoot if there are any issues in your data source connection. For example, does the user connecting have permission to do so from the activiti server from IP from which it will send the requests. Is the JDBC Driver on the classpath of the activiti server etc. A diagram of this setup is shown below:
Create Data Model
- Navigate back to the App Designer app and click on the Data Models top menu.
- Click on the Create Data Model button on the top right. This opens up Create Data Model dialog.
- Select Database as the Data Source and the Customers data source from the drop down menu.
- Click the Import button. This imports any tables that are in the DB pointed at from the Data Source chosen. It also maps the table fields to the data model entity created.
- As you can see from the diagram for the customers data model this means all table columns are now available to the data model.
Now that your Data Model has been configured we can use it inside our workflow definitions.
Using the Customer DataModel in a workflow
We have created the following workflow process using the App Designer in activiti. The process collects customer information using a form and passes this into a Store Entity Task to create the customer row in the mapped entity database. A Store Entity allows you to both create and update the entity in the database. It allows you to map in static values, form values or process variables to the entity which are then written to the database table. Once the Create Customer Row Store Entity task has run next task is the Verify Customer Details. This is a user task that displays the values set in the Database and re-uses the start form to allow the user to update the customer details. This means that the state of the customer data is not stored in the workflow. The user task reads from the customer database table but the customer details could be updated by another enterprise process independently of the running workflow by another enterprise application. By mapping to a read-only copy of the database values the workflow will always show up to date values for the customer. For any changes to the customer details it can pass the updated values changed in the form to the next Store Entity task. The final step in the workflow is therefore the Update Customer Details Store Entity task which updates any changes make by the customer to the database table. The various setup for this process definition is shown below:
Running the Example
We have published the process definition into an actviti application called Test. The diagram below demonstrates us running the Customer Onboard workflow with both a create and update to customer details.
This blog has demonstrated how you can connect to a Database that contains information that is shared outside of the workflow process. This allows for a fast and easily understood method to integrate workflows to a system of record within the broader enterprise. Once a datamodel is mapped to an Entity it can be used by process designers and business analysts to work with a broad range of enterprise data. Activiti also supports setting up data sources for information in Alfresco or to a custom data source such as apache chemistry or sharepoint.