preface

In this article, we implement a simple process of application for leave, I, is based on the fixed allocation task perform tasks step by step, and performs to each task will be assigned to a task, according to the configuration of BPMN to in the actual project production, certainly can’t do this, we need to dynamically allocated tasks, To complete each node should go through the approval process.


1. Expression assignment responsible person

1. UEL expressions

Activiti uses UEL expressions as part of the Java EE6 specification. UEL (Unified Expression Language) supports two UEL expressions: UEL – value and UEL – method.

1) Uel-value definition

Assignee, which is a process variable of Activiti, can write a ${} expression to dynamically assign principals

Employee application Node director: employeeId Department manager Approval node director: userId

1. Deployment process

@PostMapping("deployment")
public Map<String, Object> deployment(a) { 
Deployment deployment = repositoryService.createDeployment()
		.name("Deployment Leave Application Process")
		.addClasspathResource("processes/MyFlow.bpmn")
		.addClasspathResource("processes/MyFlow.png")
		.deploy();
Map<String, Object> map = new HashMap<>();
map.put("msg"."->>> Application for leave deployment successful!");
map.put("Deployment Process ID:", deployment.getId());
map.put("Deployment Process Name :", deployment.getName());
map.put("Deployment time :", deployment.getDeploymentTime());
return map;
}
Copy the code

2. Start the process

// Set the task delegate
Map<String, Object> variables = new HashMap<String, Object>();
variables.put("employeeId"."Zhang");
variables.put("userId"."Manager Li");
Copy the code
@PostMapping("startProcess")
public Map<String, Object> startProcess(String deploymentId) {
	// Query the act_re_procdef table based on the deployment process ID.
	ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deploymentId).singleResult();
	// Set the task delegate
	Map<String, Object> variables = new HashMap<String, Object>();
	variables.put("employeeId"."Zhang");
	variables.put("userId"."Manager Li");
	// Start the process definition based on the process definition key and return the process instance
	ProcessInstance pi = runtimeService.startProcessInstanceByKey(processDefinition.getKey(),variables);
	Map<String, Object> map = new HashMap<>();
	map.put("Process instance ID:", pi.getId());
	map.put("Process definition ID:",pi.getProcessDefinitionId());
	return map;
}
Copy the code

3. My to-do list

.singleresult () // Returns a unique result set.count () // Returns the number of result sets.listPage (firstResult, maxResults); // paging query.list(); // set paging query

@PostMapping("myTask")
public Map<String, Object> myTask(String name) {
	Task task = taskService.createTaskQuery().taskAssignee(name).singleResult();
	// Display the task list
	Map<String, Object> map = new HashMap<>();
		map.put("Process instance ID:", task.getProcessInstanceId());
		map.put("The task ID.", task.getId());
		map.put("Mission leader :", task.getAssignee());
		map.put("Task Name :", task.getName());
	return map;
}
Copy the code

4. Perform tasks

@PostMapping("startTask")
public Map<String, Object> startTask(String name) {
	// Query by process instance ID and user name to uniquely identify a task that needs to be processed
	Task task = taskService.createTaskQuery()
			.taskAssignee(name)
			.singleResult();
	
	Map<String, Object> map = new HashMap<>();
	map.put("msg", name + "->>> Submitted the task!");
	
	// Execute the approval process
	taskService.complete(task.getId());
	map.put("result1", name + "->>> Approval completed!");
	
	// Query the next task point
	map.put("result2", name + "->>> Go to next task node!");
	List<Task> taskList = taskService.createTaskQuery().list();
	if(! taskList.isEmpty()) {for(Task task1 : taskList){
    		map.put("Process instance ID:", task1.getProcessInstanceId());
    		map.put("The task ID.", task1.getId());
    		map.put("Mission leader :", task1.getAssignee());
    		map.put("Task Name :", task1.getName()); }}else {
		map.put("msg"."The whole process approval task is over!!");
	}
	return map;
}
Copy the code

2. Uel-method

UserBean is a bean in the Spring container that calls the getUserId() method of that bean.

1. Create SpringBean

@Component
public class UserBean {
	
	public String getEmpId(a) {
		return "Zhang";
	}
	
	public String getUserId(a) {
		return "Manager Li"; }}Copy the code

2. Deployment process

Deployment deployment = repositoryService.createDeployment()
				.name("Deployment Leave Application Process")
				.addClasspathResource("processes/MyFlowBean.bpmn")
				.addClasspathResource("processes/MyFlowBean.png")
				.deploy();
Copy the code

3. Start the process

/** * Start the process instance *@return* /
@PostMapping("startProcess")
public Map<String, Object> startProcess(String deploymentId) {
	// Query the act_re_procdef table based on the deployment process ID.
	ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deploymentId).singleResult();
	// Start the process definition based on the process definition key and return the process instance
	ProcessInstance pi = runtimeService.startProcessInstanceByKey(processDefinition.getKey());
	Map<String, Object> map = new HashMap<>();
	map.put("Process instance ID:", pi.getId());
	map.put("Process definition ID:",pi.getProcessDefinitionId());
	return map;
}
Copy the code

When we successfully started the process instance, the surprise happened. Check the database, the list of running tasks:

Automatically get the methods in our userBean and set the responsible;

4. Perform tasks

@PostMapping("startTask")
public Map<String, Object> startTask(String name) {
	// Query by process instance ID and user name to uniquely identify a task that needs to be processed
	Task task = taskService.createTaskQuery()
			.taskAssignee(name)
			.singleResult();
	
	Map<String, Object> map = new HashMap<>();
	map.put("msg", name + "->>> Submitted the task!");
	
	// Execute the approval process
	taskService.complete(task.getId());
	map.put("result1", name + "->>> Approval completed!");
	
	// Query the next task point
	map.put("result2", name + "->>> Go to next task node!");
	List<Task> taskList = taskService.createTaskQuery().list();
	if(! taskList.isEmpty()) {for(Task task1 : taskList){
    		map.put("Process instance ID:", task1.getProcessInstanceId());
    		map.put("The task ID.", task1.getId());
    		map.put("Mission leader :", task1.getAssignee());
    		map.put("Task Name :", task1.getName()); }}else {
		map.put("msg"."The whole process approval task is over!!");
	}
	return map;
}
Copy the code

3. Uel-method is combined with UEL-value

${empService. FindManagerForEmployee (employeeId)} empService is a bean, spring container findManagerForEmployee is one of the bean method, EmployeeId is activiti process variables, employeeId as a parameter to empService. FindManagerForEmployee method.

1. Create the Service

Create an interface to query user names by ID

Interface implementation class

SQL part

2. Create the Spring Bean

@Component
public class EmpService {

	@Autowired
	private UserService userService;
	
	public String findManagerForEmployee(Integer employeeId) {
		System.out.println("bean1->>>"+employeeId);
		//return "";
		return userService.findManagerForEmployee(employeeId);
	}
	
	public String findManagerForUser(Integer userId) {
		System.out.println("bean2->>>"+userId);
		//return "manager Li ";
		returnuserService.findManagerForUser(userId); }}Copy the code

3. Deployment process

Deployment deployment = repositoryService.createDeployment()
				.name("Deployment Leave Application Process")
				.addClasspathResource("processes/MyFlowService.bpmn")
				.addClasspathResource("processes/MyFlowService.png")
				.deploy();
Copy the code

4. Start the process

Important >>> Go to the database to check our responsible person


@PostMapping("startProcess")
public Map<String, Object> startProcess(String deploymentId) {
	// Query the act_re_procdef table based on the deployment process ID.
	ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deploymentId).singleResult();
	// Set task delegate -- Important >>> Go to the database to query our responsible person
	Map<String, Object> variables = new HashMap<String, Object>();
	variables.put("employeeId".1);
	variables.put("userId".2);
	// Start the process definition based on the process definition key and return the process instance
	ProcessInstance pi = runtimeService.startProcessInstanceByKey(processDefinition.getKey(),variables);
	Map<String, Object> map = new HashMap<>();
	map.put("Process instance ID:", pi.getId());
	map.put("Process definition ID:",pi.getProcessDefinitionId());
	return map;
}
Copy the code

Start the process to query the first node owner:

Task sheet record:

5. Your to-do list

@PostMapping("myTask")
public Map<String, Object> myTask(String name) {
	Task task = taskService.createTaskQuery().taskAssignee(name).singleResult();
	// Display the task list
	Map<String, Object> map = new HashMap<>();
		map.put("Process instance ID:", task.getProcessInstanceId());
		map.put("The task ID.", task.getId());
		map.put("Mission leader :", task.getAssignee());
		map.put("Task Name :", task.getName());
	return map;
}
Copy the code

5. Perform tasks

@PostMapping("startTask")
public Map<String, Object> startTask(String name) {
	// Query by process instance ID and user name to uniquely identify a task that needs to be processed
	Task task = taskService.createTaskQuery()
			.taskAssignee(name)
			.singleResult();
	
	Map<String, Object> map = new HashMap<>();
	map.put("msg", name + "->>> Submitted the task!");
	
	// Execute the approval process
	taskService.complete(task.getId());
	map.put("result1", name + "->>> Approval completed!");
	
	// Query the next task point
	map.put("result2", name + "->>> Go to next task node!");
	List<Task> taskList = taskService.createTaskQuery().list();
	if(! taskList.isEmpty()) {for(Task task1 : taskList){
    		map.put("Process instance ID:", task1.getProcessInstanceId());
    		map.put("The task ID.", task1.getId());
    		map.put("Mission leader :", task1.getAssignee());
    		map.put("Task Name :", task1.getName()); }}else {
		map.put("msg"."The whole process approval task is over!!");
	}
	return map;
}
Copy the code

Execute the first node to query the next node owner:

Task sheet record:

4. Other ways

Expressions support parsing of base types, beans, lists, arrays, and maps, as well as conditional judgment.

${user.day >= 5 && user.day < 5}

We’ll talk about these process variables later in the gateway section, which is basically the first way we did it above.

conclusion

This is the end of UEL Expressions, this is the foundation, from this article, we know that there are a lot of things to master Activiti, need to get down to it, slowly taste, next gateway, pay attention! Attention!!!! Attention!!!! Attention!!!! Attention!!!! Attention!!!! Attention!!!! Attention!!!! Stay up late for three consecutive releases, one key three link!! .

Stay up late dry goods, creation is not easy, move small hands point praise !!!! Behind will continue to output more dry goods to you, like please pay attention to xiaobian CSDN: blog.csdn.net/qq_41107231 and nuggets: juejin.cn/user/394024…