Create Listener Class in Activiti Workflow of Alfresco

In recent articles about JBPM workflows we had seen in one of the article regarding how to create listener class for JBPM workflow which is part of Alfresco. To get more details on importance of Actions class and how to create action class in JBPM workflow please refer this post.

Here we will see how to create similar action class in Activiti this is really important because in all new implementations clients are asking for Activiti rather than JBPM. Let’s get into implementation of this.

Step1:

Create your custom process definition file

<?xml version="1.0" encoding="UTF-8" ?>

<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"

   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:activiti="http://activiti.org/bpmn"

   xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC"

   xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema"

   expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://alfresco.org">

 

    <process isExecutable="true" id="activitiReview" name="Review And Approve Activiti Processssssss">

 

        <startEvent id="start"

            activiti:formKey="wf:submitReviewTask" />

 

        <sequenceFlow id='flow1'

            sourceRef='start'

            targetRef='reviewTask' />

 

        <userTask id="reviewTask" name="Review Task"

            activiti:formKey="wf:activitiReviewTask">

           <extensionElements>

               <activiti:taskListener event="create" class="com.test.CustomListener"></activiti:taskListener>

               <activiti:taskListener event="create" class="org.alfresco.repo.workflow.activiti.tasklistener.ScriptTaskListener">

                  <activiti:field name="script">

                     <activiti:string>

                        if (typeof bpm_workflowDueDate != 'undefined') task.dueDate = bpm_workflowDueDate

                        if (typeof bpm_workflowPriority != 'undefined') task.priority = bpm_workflowPriority;      
                                                            
                     </activiti:string>

                  </activiti:field>

               </activiti:taskListener>

               <activiti:taskListener event="complete" class="org.alfresco.repo.workflow.activiti.tasklistener.ScriptTaskListener">

                  <activiti:field name="script">

                     <activiti:string>

                                execution.setVariable('wf_reviewOutcome', task.getVariable('wf_reviewOutcome'));

                     </activiti:string>

                  </activiti:field>

               </activiti:taskListener>

           </extensionElements>

            <humanPerformer>

                <resourceAssignmentExpression>

                    <formalExpression>${bpm_assignee.properties.userName}</formalExpression>

                </resourceAssignmentExpression>

            </humanPerformer>

        </userTask>

 

        <sequenceFlow id='flow2'

        sourceRef='reviewTask'

            targetRef='reviewDecision' />

 

        <exclusiveGateway  id="reviewDecision" name="Review Decision" />

 

        <sequenceFlow id='flow3' sourceRef='reviewDecision' targetRef='approved' >

            <conditionExpression xsi:type="tFormalExpression">${wf_reviewOutcome == 'Approve'}</conditionExpression>

        </sequenceFlow>

 

        <sequenceFlow id='flow4'

        sourceRef='reviewDecision'

            targetRef='rejected' />

 

       <userTask id="approved" name="Document Approved"

            activiti:formKey="wf:approvedTask" >

            <documentation>    The document was reviewed and approved.

            </documentation>

            <extensionElements>

              <activiti:taskListener event="create" class="com.test.CustomListener"></activiti:taskListener>

               <activiti:taskListener event="create" class="org.alfresco.repo.workflow.activiti.tasklistener.ScriptTaskListener">

                  <activiti:field name="script">

                     <activiti:string>

                        if (typeof bpm_workflowDueDate != 'undefined') task.dueDate = bpm_workflowDueDate

                        if (typeof bpm_workflowPriority != 'undefined') task.priority = bpm_workflowPriority;

                             </activiti:string>

                  </activiti:field>

               </activiti:taskListener>

            </extensionElements>

            <humanPerformer>

                <resourceAssignmentExpression>

                    <formalExpression>${initiator.exists() ? initiator.properties.userName : 'admin'}</formalExpression>

                </resourceAssignmentExpression>

            </humanPerformer>

        </userTask>       

        <userTask id="rejected" name="Document Rejected"

            activiti:formKey="wf:rejectedTask" >

            <documentation>

                The document was reviewed and rejected.

            </documentation>

            <extensionElements>

               <activiti:taskListener event="create" class="com.test.CustomListener"></activiti:taskListener>

            </extensionElements>

            <humanPerformer>

                <resourceAssignmentExpression>

                    <formalExpression>${initiator.exists() ? initiator.properties.userName : 'admin'}</formalExpression>

                </resourceAssignmentExpression>

            </humanPerformer>

        </userTask>

        <sequenceFlow id='flow5' sourceRef='approved'       targetRef='end' />

        <sequenceFlow id='flow6' sourceRef='rejected'         targetRef='end' />

        <endEvent id="end" />

    </process>

I have just created simple workflow by copying most of the part from out of box review workflow of Alfresco.

*Note: you also need to create context file to register your custom workflow. To get more details on how to create custom workflow you can refer this post.

Step2:

Add custom listener class tag in process definition file. In this case I have added it in previous step only you can check highlighted text where it is referring to com.test.CustomListner class.

Step3:

Create new Java class called CustomListner class under package com.test

package com.test; 

import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener; 

public class CustomListener implements TaskListener { 

                @Override

                public void notify(DelegateTask task) {

                System.out.println("CustomListener.notify()");

                                String ss="Take the appropriate action by clicking one of the links below:"+task.getName();

                                System.out.println(ss);

                } 

}

As you can see this class is extending Activiti engines TaskListener class and overriding notify method.

Step4:

Deploy everything

Workflow related xml files you can place under <TOMCAT_HOME>/shared/classes/alfresco/extension

For listener class you need to create jar file and place it inside <TOMCAT_HOME>/webapps/alfresco/WEB-INF/lib

Step5:

Restart your server and check changes.

Looking for quality Alfresco Web Hosting? Look no further than Arvixe Web Hosting!

 

Tags: , , , , , , , , , , , , | Posted under Alfresco | RSS 2.0

Author Spotlight

mitpatoliya

I love opensource technologies working with those technologies from the time I have stepped in to the Software Industry. Alfresco CMS is my area of expertise. I have worked on various complex implementations which involved integration of Alfresco with other technologies, extensively worked with JBPM workflows and Webscripts.

Leave a Reply

Your email address will not be published. Required fields are marked *