preface

Task delegator: The task leader.

It is simple, we can specify more than one spare tire, help me to complete the process of examination and approval, such as business trip application, need gm approval, but the general manager as long as a man, if he have something on that day, can’t go to the examination and approval, the day of the process is invalid, would have been in the activated state, no one to deal with, if we appointed general manager of several people, so zhang SAN occupy, Li Can deal with it, Li can deal with it, Wang Can deal with it, or when the three of them are engaged, they can randomly appoint a company delegate to deal with the application approval.


First, process design

Ii. Deployment process

@PostMapping("deployment")
public Map<String, Object> deployment(a) { 
	Deployment deployment = repositoryService.createDeployment()
			.name("Deployment Leave Application Process")
			.addClasspathResource("processes/MyFlowDelegate.bpmn")
			.addClasspathResource("processes/MyFlowDelegate.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 instance

When you start the process instance, Can specify the appointed person (multiple) ProcessInstance startProcessInstanceByKey (String processDefinitionKey, Map < String, Object > variables); Variables: a key corresponds to one value. Multiple values are separated by commas (,)


@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 multiple task delegators
	Map<String, Object> variables = new HashMap<>();
	variables.put("empIds"."Three, four, five.");
	variables.put("userId"."White");
	// 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

Select * from act_ru_task; select * from act_task; select * from act_ru_task;

My to-do list

Candidate query method: taskCandidateUser(name) Participant query method: taskAssignee(name)

When a process instance is started, the identity category of the current process executor is visible in the ACT_ru_identitylink table.

It can be seen that Zhang SAN has two identities, that is, candidate and participant, but as long as the candidate identity adopts TaskId, that is to say, if It is Zhang SAN to complete the creation of the business trip application, it is completed as a candidate.

@PostMapping("myTask")
public Map<String, Object> myTask(String name) {
	// How to query candidates
	//Task task = taskService.createTaskQuery().taskCandidateUser(name).singleResult();
	// Participant query method
	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

Query my to-do task, because there are multiple candidates (Zhang SAN, Li Si, Wang Wu), and they belong to the same node, so when they go to query my to-do task, they can be found, and it is the same task instance, and note that the query methods of candidates and participants are different.

Zhang SAN query to do:

Li Si enquiry to do:

Wang Wu inquiry to do:

You can see they all have the same mission ID.

Appoint the delegate

Three candidates (Zhang SAN, Li Si, Wang Wu), when Li Si (or Zhang SAN, or Wang Wu) delegate tasks to Wang to do, Wang will become the node participant, at this time, Zhang SAN, Li Si, Wang Wu is no longer the delegate candidate, the current node can only be completed by Wang.

@PostMapping("setAssigneeTask")
public Map<String, Object> setAssigneeTask(String name,String otherName){
	Map<String, Object> map = new HashMap<>();
	/ / task ID
	Task task = taskService.createTaskQuery().taskCandidateUser(name).singleResult();
	// When Li Si assigns tasks to Wang, Wang becomes a participant, and Zhang SAN, Li Si and Wang Wu are no longer candidates for delegation.
	String taskId = task.getId();
	// Assign other task handlers
	taskService.setAssignee(taskId, otherName);
	map.put("message"."Mission manager authorization successful!! Send instant message notification!!");
	return map;
}
Copy the code

When the delegate has been specified successfully, the delegate can be asked to complete the process operation. At this time running task instance table, is small three to complete the creation process application, and has the name of the person in charge.

Query the tasks of the person in charge xiao Wang:

Iv. Mission execution

@PostMapping("startTask")
public Map<String, Object> startTask(String name,String taskId) {
	// Decide whether you are a candidate or a participant
	Task result = taskService.createTaskQuery().taskId(taskId).singleResult();
	System.out.println("Candidate or participant ->>>"+result.getAssignee());
	
	Task task = null;
	if(StringUtils.isNoneBlank(result.getAssignee())) {
		System.out.println("Participant");
		// My to-do list
		task = taskService.createTaskQuery()
				TaskCandidateUser (name) // taskCandidateUser(name) // taskCandidateUser(name) // taskCandidateUser(name)
				.taskAssignee(name) // The query method of the candidate (no matter which candidate: the current task ID is the same, the current node process is completed through the task ID)
				.singleResult();
	}else {
		System.out.println("Candidate");
		// My to-do list
		task = taskService.createTaskQuery()
				.taskCandidateUser(name) // The query method of the candidate (no matter which candidate: the current task ID is the same, the current node process is completed through the task ID)
				//. TaskAssignee (name) // The query method of the candidate (no matter which candidate: the current task ID is the same, and the current node process is completed by the task ID)
				.singleResult();
	}
	
	// Display of my 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());
	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

conclusion

Of course, there are also ways to specify delegates, and there will be an article on listeners, which will follow at !!!!

There are a lot of things about Activiti that need to be taken down and tasted slowly. The next article will be an advanced one. Keep an eye out! Attention!!!! Attention!!!! Attention!!!! Attention!!!! Attention!!!! Attention!!!! Attention!!!! .

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…