Search code examples
workflowalfrescoalfresco-sharebpmnalfresco-webscripts

Alfresco Workflow - multiple assignee and java code


I'm trying to create a workflow that have multiple assignees (done) and launch to them, a task. When I start the workflow, I want to run a java code that makes modifications on file that the task has been assigned. How can I run this java code on "Start Workflow"?

And then, I want that each assignee have a task to approve (this moment, the assignees don't have task to approve, appear task done, how can I do that?), and to approve I want to run other java code. How can I make this ? Assign this java codes to workflow?

I don't know how to define bpmn too, I think some problems are because of this.


Solution

  • In order to run java code from your bpmn, you need to use java Listener classes. Listeners run on task events(create or complete ) or workflow events (start or end). These listeners need to be defined in your bpmn , and in your spring context file.

    I ll give an example from my own code:

    spring context file:

    <bean id="StartTaskListener" class="com.crius.epub.wf.StartTaskListener" parent="activitiCreateTaskListener">
            </bean>
    <bean id="StartTaskListener.activitiBeanRegistry" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean" depends-on="activitiBeanRegistry">
        <property name="targetObject">
            <ref bean="activitiBeanRegistry" />
        </property>
        <property name="targetMethod" value="put" />
        <property name="arguments">
            <list>
                <value>StartTaskListener</value>
                <ref bean="StartTaskListener" />
            </list>
        </property>
    </bean>
    

    Similarly, you have to define CompleteTaskListener (runs at end of task), and ExecutionFlowListener (runs at begin and end of workflow)

    In my bpmn, it looks like this

    <?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:xsd="http://www.w3.org/2001/XMLSchema" 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://www.activiti.org/test">
      <process id="epubcreate" name="Create epub" isExecutable="true">
        <extensionElements>
          <activiti:executionListener event="end" delegateExpression="${ExecutionFlowListener}"></activiti:executionListener>
          <activiti:executionListener event="start" delegateExpression="${ExecutionFlowListener}"></activiti:executionListener>
        </extensionElements>
        <startEvent id="start" name="Start" activiti:initiator="initiatorUserName" activiti:formKey="epubwf:start"></startEvent>
        <userTask id="create" name="Create proof" activiti:assignee="${epubwf_creator.properties.userName}" activiti:formKey="epubwf:create">
          <extensionElements>
            <activiti:taskListener event="create" delegateExpression="${StartTaskListener}"></activiti:taskListener>
            <activiti:taskListener event="complete" delegateExpression="${CompleteTaskListener}"></activiti:taskListener>
          </extensionElements>
        </userTask>
    

    An example of a Executionlistener class looks like this:

     public class ExecutionFlowListener  extends DelegateExecutionScriptBase implements ExecutionListener{
    
    public void notify(DelegateExecution execution){
      ExecutionEntity executionEntity = (ExecutionEntity)execution;
      if ("start".equals(executionEntity.getActivityId())){
          // workflow has started
          // get value of property mymodel:myproperty
          Object assignees =  execution.getVariable("mymodel_myproperty");
      }  else if ((executionEntity.getTransition() != null && "end".equals(executionEntity.getTransition().getDestination().getId())) || "end".equals(execution.getEventName())){ 
         // workflow has ended
       }
     }
    

    Hopefully this will get you started...