Activiti 5.15.1 dynamically manually through Java coding, to create user tasks, dynamically specify the individual, user groups, roles, to specify the implementation of monitoring

Source: Internet
Author: User

Because of our business needs, recently have been engaged in dynamic Java program to implement a user task binding listener. Touched a lot of walls, looked at the API documentation, finally found a solution, so posted out, hoping to stay a bottom, but also to help people in need.

----------tool classes that dynamically generate process files--------

Package com.travesky.bluesky.activiti.utils;

Import Java.io.File;
Import Java.io.InputStream;
Import java.util.ArrayList;
Import Java.util.Arrays;
Import java.util.List;

Import Org.activiti.bpmn.BpmnAutoLayout;
Import Org.activiti.bpmn.model.ActivitiListener;
Import Org.activiti.bpmn.model.BpmnModel;
Import org.activiti.bpmn.model.EndEvent;
Import Org.activiti.bpmn.model.ExclusiveGateway;
Import Org.activiti.bpmn.model.ExtensionAttribute;
Import org.activiti.bpmn.model.ExtensionElement;
Import org.activiti.bpmn.model.FlowElement;
Import Org.activiti.bpmn.model.ImplementationType;
Import Org.activiti.bpmn.model.SequenceFlow;
Import Org.activiti.bpmn.model.ServiceTask;
Import org.activiti.bpmn.model.StartEvent;
Import Org.activiti.bpmn.model.UserTask;
Import Org.activiti.engine.RepositoryService;
Import Org.activiti.engine.RuntimeService;
Import Org.activiti.engine.TaskService;
Import Org.activiti.engine.delegate.TaskListener;
Import Org.activiti.engine.impl.bpmn.parser.BpmnParse;
Import org.activiti.engine.repository.Deployment;
Import org.activiti.engine.runtime.ProcessInstance;
Import Org.activiti.engine.task.Task;
Import Org.apache.commons.io.FileUtils;
Import org.springframework.beans.factory.annotation.Autowired;
Import org.springframework.stereotype.Component;
Import Org.springframework.web.context.ContextLoader;
/**
* Process file Generation tool class, already injected in spring file
* @author Ancan
* @date 2017/4/10
*/
public class Autodeployprocessbpmnandimageutils {
@Autowired
Private Repositoryservice Repositoryservice;
@Autowired
Private Runtimeservice Runtimeservice;
@Autowired
Private Taskservice Taskservice;
Private final String start_event = "START";
Private final String end_event = "END";
/**
*
* @param PROCESSID Process ID
* @param PROCESSNAME Process Name
* @throws Exception
*/
public void Dynamicdeploybpmnandimage (final String PROCESSID,
Final String PROCESSNAME) throws Exception {
System.out.println ("...... start ...");
String basepath = Contextloader.getcurrentwebapplicationcontext (). Getservletcontext (). Getrealpath ("/") + "bpmn\\" ;
System.out.println ("... start...=====" +basepath);
1. Build up the model from scratch
Bpmnmodel model = new Bpmnmodel ();
Org.activiti.bpmn.model.Process process=new org.activiti.bpmn.model.Process ();
Model.addprocess (process);
Process.setid (PROCESSID);
Process.setname (PROCESSNAME);
Set the Start node
Process.addflowelement (Createstartevent ());
Set up Task Node 1
Process.addflowelement (Createusertask ("Task1", "Team leader Review", "CandidateGroup1"));
Setting exclusive Nodes 1
Process.addflowelement (Createexclusivegateway ("CreateExclusiveGateway1"));
Set up Task Node 2
Process.addflowelement (Createusertask ("Task2", "Project manager Review", "candidateGroup2"));
Setting Exclusive Nodes 2
Process.addflowelement (Createexclusivegateway ("CreateExclusiveGateway2"));
Set Task Node 3 common tasks
Process.addflowelement (Createusertask ("Task3", "Product Department manager Review", "CANDIDATEGROUP3"));
Set Task Node 3 Listener task
list<string> lisenerlist = new arraylist<string> ();
Lisenerlist.add ("Com.travesky.bluesky.activiti.utils.MangerTaskHandlerCandidateUsers");
Process.addflowelement (Createusertask ("Task3", "Product Department manager Review", Lisenerlist));
Setting exclusive Nodes 3
Process.addflowelement (Createexclusivegateway ("CreateExclusiveGateway3"));
Process.addflowelement (Createusertask ("Task4", "General manager Audit", "CANDIDATEGROUP4"));
Set End Node
Process.addflowelement (Createendevent ());

Process.addflowelement (Createsequenceflow (start_event, "Task1", "" "," ""));
Process.addflowelement (Createsequenceflow ("Task1", "Task2", "" "," "));
//
Process.addflowelement (Createsequenceflow ("Task2", "createExclusiveGateway1"));
Process.addflowelement (Createsequenceflow ("CreateExclusiveGateway1", "Task1", "does not pass", "${pass== ' 2 '}"));
Process.addflowelement (Createsequenceflow ("CreateExclusiveGateway1", "Task3", "Through", "${pass== ' 1 '}"));
Process.addflowelement (Createsequenceflow ("Task3", "CreateExclusiveGateway2"));
Process.addflowelement (Createsequenceflow ("CreateExclusiveGateway2", "Task2", "does not pass", "${pass== ' 2 '}"));
Process.addflowelement (Createsequenceflow ("CreateExclusiveGateway2", "Task4", "Through", "${pass== ' 1 '}"));
Process.addflowelement (Createsequenceflow ("Task4", "createExclusiveGateway3"));
Process.addflowelement (Createsequenceflow ("CreateExclusiveGateway3", "Task3", "does not pass", "${pass== ' 2 '}"));
Process.addflowelement (Createsequenceflow ("CreateExclusiveGateway3", End_event, "Pass", "${pass== ' 1 '}"));
2. Generate Graphical Information
New Bpmnautolayout (model). Execute ();

3. Deploy the process to the engine
Deployment Deployment = Repositoryservice.createdeployment (). Addbpmnmodel (processid+ ". BPMN", model). Name (PROCESSID + "_deployment"). deploy ();

4. Start a Process instance
ProcessInstance processinstance = Runtimeservice.startprocessinstancebykey (PROCESSID);

5. Check if task is available
list<task> tasks = Taskservice.createtaskquery (). Processinstanceid (Processinstance.getid ()). List ();
Assert.assertequals (1, tasks.size ());

6. Save process diagram to a file
InputStream Processdiagram = Repositoryservice.getprocessdiagram (Processinstance.getprocessdefinitionid ());
Fileutils.copyinputstreamtofile (Processdiagram, New File ("target/" +processid+ ". png"));

7. Save resulting BPMN XML to a file
InputStream PROCESSBPMN = Repositoryservice.getresourceasstream (Deployment.getid (), PROCESSID+ ". BPMN");
Fileutils.copyinputstreamtofile (processbpmn,new File ("target/" +processid+ ". BPMN"));

System.out.println ("... end ..."););
}
/**
* Create node Task personal task
* @param ID task ID identification
* @param name Task
* @param Assignee Assign personal tasks
* @return
*/
Public Usertask Createusertask (string ID, string name, String assignee) {
Usertask usertask = new Usertask ();
Usertask.setname (name);
Usertask.setid (ID);
Usertask.setassignee (assignee);
return usertask;
}
/**
* Create node task multi-person task
* @param ID task ID identification
* @param name Task
* @param a collection of candidateusers task people
* @return
*/
Public Usertask Createusertask (string ID, string name, string[] candidateusers) {
Usertask usertask = new Usertask ();
Usertask.setname (name);
Usertask.setid (ID);
if (null!=candidateusers&&candidateusers.length>0) {
Usertask.setcandidateusers (Arrays.aslist (candidateusers));
}
return usertask;
}
/**
* Create node task using listener settings Handler
* @param ID task ID identification
* @param name Task
* @param the collection of tasklistenerlist listening, Example of a specific path to a Tasklistener implementation class: Com.sky.bluesky.activiti.utils.MangerTaskHandlerCandidateUsers
* @return
*/
Public Usertask Createusertask (string ID, string name, List<string> tasklistenerlist) {
Usertask usertask = new Usertask ();
Usertask.setname (name);
Usertask.setid (ID);


list<activitilistener> list = new arraylist<activitilistener> ();
for (String tasklistener:tasklistenerlist) {
Activitilistener listener = new Activitilistener ();
Listener.setevent ("create");
The spring configuration cannot be written as a variable call, only by inheriting the Tasklistener method.
Listener.setimplementationtype ("class");
Listener.setimplementation (Tasklistener);

List.add (listener);

}
Usertask.settasklisteners (list);
return usertask;
}


/**
* Set up connection
* @param from where to start
* Where to connect @param to
* @return
*/
Public Sequenceflow Createsequenceflow (string from, string to) {
Sequenceflow flow = new Sequenceflow ();
Flow.setsourceref (from);
Flow.settargetref (to);
return flow;
}

/**
* Set Start node
* @return
*/
Public Startevent createstartevent () {
Startevent startevent = new Startevent ();
Startevent.setid (start_event);
return startevent;
}

/**
* Exclusive Mesh Joint point
* @param ID
* @return
*/
public static Exclusivegateway Createexclusivegateway (String ID) {
Exclusivegateway Exclusivegateway = new Exclusivegateway ();
Exclusivegateway.setid (ID);
return exclusivegateway;
}
/**
* Set End Node
* @return
*/
Public EndEvent createendevent () {
EndEvent endevent = new EndEvent ();
Endevent.setid (end_event);
return endevent;
}
/**
* Set up connection
* @param from where to start
* Where to connect @param to
* @param name Connection
* @param conditionexpression judging condition ${arg>2}
* @return
*/
public static Sequenceflow Createsequenceflow (string from, String to,string name,string conditionexpression) {
Sequenceflow flow = new Sequenceflow ();
Flow.setsourceref (from);
Flow.settargetref (to);
Flow.setname (name);
if (null!=conditionexpression&&! "". Equals (conditionexpression)) {
Flow.setconditionexpression (conditionexpression);
}
return flow;
}
Public flowelement Createservicetask (String name) {
Servicetask stask = new Servicetask ();
Stask.setid ("Sid");
Stask.setname (name);
Stask.setimplementation ("Activititest.printvariables");
String implementationtype = implementationtype.implementation_type_delegateexpression;
Stask.setimplementationtype (Implementationtype);
return stask;
}
}

------------Listener Class--------

Package com.travesky.bluesky.activiti.utils;

Import Java.util.Arrays;

Import Org.activiti.bpmn.model.ActivitiListener;
Import Org.activiti.engine.delegate.DelegateTask;
Import Org.activiti.engine.delegate.TaskListener;

public class Mangertaskhandlercandidateusers extends Activitilistener implements Tasklistener {
/**
*
*/
Private static final long serialversionuid = 1L;

@Override
public void Notify (Delegatetask delegatetask) {
System.out.println ("Enter mangertaskhandlercandidateusers=========");
/** new Query The current user, and then get the current user's corresponding leader */
Current user
String[] EmpLoyees = {"Feng Xiaogang agent", "Fan Bingbing agent", "Feng Xiaogang"};
Delegatetask.addcandidateusers (Arrays.aslist (empLoyees));
SYSTEM.OUT.PRINTLN ("Node task person ======== Feng Xiaogang brokers, Fan Bingbing brokers, Feng Xiaogang");

}


}

---There is no further encapsulation and refinement, because it is still being perfected. Just wanted to cast tasklistener to Activitilistener, but that was wrong. Must be in accordance with the specifications of Activiti.

Activiti 5.15.1 dynamically manually through Java coding, to create user tasks, dynamically specify the individual, user groups, roles, to specify the implementation of monitoring

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.