flowable learning exclusive gateway

Posted by Enlightened on Wed, 30 Oct 2019 07:56:27 +0100

I. deployment Designer

1. Put flowable-idm.war and flowable-modeler.war into the webapps directory of tomcat, and then start Tomcat.

2. Access in browser http://localhost:8080/flowable-modeler , user name / password: admin/test login, save and export the file after designing the process.

Two code

1 create a process engine and deploy a process definition

@Before
public void Before() {
	ProcessEngineConfiguration cfg = new StandaloneProcessEngineConfiguration()
			.setJdbcUrl("jdbc:mysql://localhost:3306/flowable?useSSL=true&serverTimezone=UTC&nullCatalogMeansCurrent=true")
			.setJdbcUsername("xxxx")
			.setJdbcPassword("xxxx")
			.setJdbcDriver("com.mysql.cj.jdbc.Driver")
			.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);

	processEngine = cfg.buildProcessEngine();
}


@Test
public void Test01(){
	// Get the Flowable service
	RepositoryService repositoryService = processEngine.getRepositoryService();

	// Deployment process definition
	Deployment deployment = repositoryService.createDeployment().addClasspathResource("qing_jia_liu_cheng.bpmn20.xml").deploy();

	//Find deployment process definition
	ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).singleResult();
	System.out.println(processDefinition.getName() + "  " + processDefinition.getDeploymentId() + "   " + processDefinition.getId());
}

2. A complete process

@Test
public void test02() {
	String processDefinitionId = "qing_jia:1:4";

	RuntimeService runtimeService = processEngine.getRuntimeService();
	TaskService taskService = processEngine.getTaskService();

	//Leave days < = 5 approved by twoUser
	int days = 2;
	//Call externalsystemdelegate if approved, otherwise call SendRejectionMail
	boolean approved = false;

	Map<String, Object> variables = new HashMap<>();
	variables.put("user", "Da Da Zhang");
	variables.put("days", days);

	//In the first node, the candidate group is managers.
	ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinitionId, variables);
	System.out.println("processInstanceId = " + processInstance.getProcessInstanceId());

	//Query tasks under managers
	List<Task> tasks = taskService.createTaskQuery().taskCandidateGroup("managers").list();
	System.out.println("managers Yes" + tasks.size() + " Task:");

	//Set the conditions and end the first task to enter the gateway
	variables = new HashMap<>();
	variables.put("oneUser", "Li Da Da");
	variables.put("twoUser", "Da Da Wang");
	taskService.complete(tasks.get(0).getId(), variables);
	System.out.println("-----managers Finished the task-----");

	queryTask();

	if(days <= 5) {
		System.out.println("-----The task is up to Wang dada-----");
		tasks = taskService.createTaskQuery().taskAssignee("Da Da Wang").list();
	} else {
		System.out.println("-----The task has arrived at Li dada-----");
		tasks = taskService.createTaskQuery().taskAssignee("Li Da Da").list();
	}

	variables = new HashMap<>();
	variables.put("approved", approved);
	taskService.complete(tasks.get(0).getId(), variables);
	System.out.println("-----Da Da Wang/Li dada finished the task-----");

	queryTask();

	tasks = taskService.createTaskQuery().taskAssignee("Da Da Zhang").list();

	System.out.println("-----Zhang dada finished the task-----");
	taskService.complete(tasks.get(0).getId());

	queryTask();
}

private void queryTask() {
	TaskService taskService = processEngine.getTaskService();

	System.out.println("---------------------------");
	List<Task> tasks = taskService.createTaskQuery().taskAssignee("Da Da Zhang").list();
	System.out.println("Zhang Da you" + tasks.size() + " Task:");

	tasks = taskService.createTaskQuery().taskAssignee("Li Da Da").list();
	System.out.println("Li has a lot." + tasks.size() + " Task:");

	tasks = taskService.createTaskQuery().taskAssignee("Da Da Wang").list();
	System.out.println("Wang Da you" + tasks.size() + " Task:");
	System.out.println("---------------------------");
}

Execution results of the whole process:

processInstanceId = 7501
managers has one task:
-----managers ended the task-----
---------------------------
Zhang dada has 0 tasks:
Li dada has 0 tasks:
Wang dada has one task:
---------------------------
-----The task is up to Wang dada-----
SendRejectionMail start
oneUser = = Li dada
approved == false
days == 2
user = = Zhang dada
twoUser = = Wang dada
sid-0417597E-9B0F-4013-8F61-5139D73EF0D3      7504   []   null
SendRejectionMail start
-----Wang dada / Li dada finished the task-----
---------------------------
Zhang dada has one task:
Li dada has 0 tasks:
Wang dada has 0 tasks:
---------------------------
-----Zhang dada finished the task-----
---------------------------
Zhang dada has 0 tasks:
Li dada has 0 tasks:
Wang dada has 0 tasks:
---------------------------

3. Reject and submit again

@Test
public void Test04(){
	String processDefinitionId = "qing_jia:1:4";

	RuntimeService runtimeService = processEngine.getRuntimeService();
	TaskService taskService = processEngine.getTaskService();
	HistoryService historyService = processEngine.getHistoryService();

	int days = 2;
	boolean approved = true;

	Map<String, Object> variables = new HashMap<>();
	variables.put("user", "Da Da Zhang");
	variables.put("days", days);

	//In the first node, the candidate group is managers.
	ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinitionId, variables);
	System.out.println("processInstanceId = " + processInstance.getProcessInstanceId());

	//Query tasks under managers
	List<Task> tasks = taskService.createTaskQuery().taskCandidateGroup("managers").list();
	System.out.println("managers Yes" + tasks.size() + " Task:");

	//End the first task and enter the gateway judgment parameter approved true: external leave system and then go to the next user node false: send rejection email
	variables = new HashMap<>();
	variables.put("oneUser", "Li Da Da");
	variables.put("twoUser", "Da Da Wang");
	taskService.complete(tasks.get(0).getId(), variables);
	System.out.println("-----managers Finished the task-----");
	queryTask2();

	if(days <= 5) {
		System.out.println("-----The task is up to Wang dada-----");
		tasks = taskService.createTaskQuery().taskAssignee("Da Da Wang").list();
	} else {
		System.out.println("-----The task has arrived at Li dada-----");
		tasks = taskService.createTaskQuery().taskAssignee("Li Da Da").list();
	}

	variables = new HashMap<>();
	variables.put("approved", approved);
	taskService.complete(tasks.get(0).getId(), variables);
	System.out.println("-----Da Da Wang/Li dada finished the task-----");

	queryTask2();

	System.out.println("-----Zhang dada rejected the task-----");
	tasks = taskService.createTaskQuery().taskAssignee("Da Da Zhang").list();

	String processInstanceId = tasks.get(0).getProcessInstanceId();
	List<String> activeActivityIds = runtimeService.getActiveActivityIds(tasks.get(0).getExecutionId());
	String currentActivityId = activeActivityIds.get(0);

	String targetActivityId = getLastActivityId(historyService, processInstanceId, currentActivityId);

	//Zhang dada returns to the process
	runtimeService.createChangeActivityStateBuilder()
			.processInstanceId(processInstanceId)
			.moveActivityIdTo(currentActivityId, targetActivityId)
			.changeState();

	queryTask2();

	if(days <= 5) {
		System.out.println("-----Once again, it's Wang dada-----");
		tasks = taskService.createTaskQuery().taskAssignee("Da Da Wang").list();
	} else {
		System.out.println("-----The task has come to Li dada again-----");
		tasks = taskService.createTaskQuery().taskAssignee("Li Da Da").list();
	}

	System.out.println("-----Da Da Wang/Li dada rejected the task-----");
	processInstanceId = tasks.get(0).getProcessInstanceId();
	activeActivityIds = runtimeService.getActiveActivityIds(tasks.get(0).getExecutionId());
	currentActivityId = activeActivityIds.get(0);

	targetActivityId = getLastActivityId(historyService, processInstanceId, currentActivityId);
	//Wang dada / Li dada back out of the process
	runtimeService.createChangeActivityStateBuilder()
			.processInstanceId(processInstanceId)
			.moveActivityIdTo(currentActivityId, targetActivityId)
			.changeState();

	queryTask2();

	//Query tasks under managers
	tasks = taskService.createTaskQuery().taskCandidateGroup("managers").list();
	System.out.println("Query again managers Yes" + tasks.size() + " Task:");

	//Set the conditions and end the first task to enter the gateway
	variables = new HashMap<>();
	days = 9;
	variables.put("days", days);
	variables.put("oneUser", "Li Da 2");
	variables.put("twoUser", "Wang Da 2");
	taskService.complete(tasks.get(0).getId(), variables);
	System.out.println("-----managers End task and modify approver-----");

	queryTask2();

	if(days <= 5) {
		System.out.println("-----Mission to Wang dada 2-----");
		tasks = taskService.createTaskQuery().taskAssignee("Wang Da 2").list();
	} else {
		System.out.println("-----It's Li dada 2-----");
		tasks = taskService.createTaskQuery().taskAssignee("Li Da 2").list();
	}

	variables = new HashMap<>();
	variables.put("approved", !approved);
	taskService.complete(tasks.get(0).getId(), variables);
	System.out.println("-----Wang Da 2/Li dada 2 finished the task-----");

	queryTask2();

	tasks = taskService.createTaskQuery().taskAssignee("Da Da Zhang").list();

	System.out.println("-----Zhang dada finished the task-----");
	taskService.complete(tasks.get(0).getId());

	queryTask2();

}

private String getLastActivityId(HistoryService historyService, String processInstanceId, String currentActivityId) {

	List<HistoricActivityInstance> list = historyService.createHistoricActivityInstanceQuery()
			.processInstanceId(processInstanceId)
			.finished()
			.activityType("userTask")
			.orderByHistoricActivityInstanceStartTime().desc()
			.list();


	String lastActivityId = null;
	//Find the activityId of the previous process
	for (int i=0; i<list.size(); i++) {
		HistoricActivityInstance activityInstance = list.get(i);
		if(activityInstance.getActivityId().equals(currentActivityId)) {
			lastActivityId = list.get(++i).getActivityId();
			break;
		}
	}

	if(lastActivityId == null) {
		lastActivityId = list.get(0).getActivityId();
	}

	return lastActivityId;
}

private void queryTask2() {
	TaskService taskService = processEngine.getTaskService();
	queryTask();
	List<Task> tasks = taskService.createTaskQuery().taskAssignee("Zhang Da 2").list();
	System.out.println("Zhang dada 2 has" + tasks.size() + " Task:");

	tasks = taskService.createTaskQuery().taskAssignee("Li Da 2").list();
	System.out.println("Li dada 2 has" + tasks.size() + " Task:");

	tasks = taskService.createTaskQuery().taskAssignee("Wang Da 2").list();
	System.out.println("Wang dada 2 has" + tasks.size() + " Task:");

	System.out.println("---------------------------");
}

Execution results of the whole process:

processInstanceId = 10001
managers has one task:
-----managers ended the task-----
---------------------------
Zhang dada has 0 tasks:
Li dada has 0 tasks:
Wang dada has one task:
---------------------------
Zhang dada 2 has 0 tasks:
Li dada 2 has 0 tasks:
Wang dada 2 has 0 tasks:
---------------------------
-----The task is up to Wang dada-----
CallExternalSystemDelegate start
oneUser = = Li dada
approved == true
days == 2
user = = Zhang dada
twoUser = = Wang dada
sid-F69D3DA0-D2EC-4594-A6F3-2F6CBD4D76B4      10004   []   null
CallExternalSystemDelegate end
-----Wang dada / Li dada finished the task-----
---------------------------
Zhang dada has one task:
Li dada has 0 tasks:
Wang dada has 0 tasks:
---------------------------
Zhang dada 2 has 0 tasks:
Li dada 2 has 0 tasks:
Wang dada 2 has 0 tasks:
---------------------------
-----Zhang dada rejected the task-----
---------------------------
Zhang dada has 0 tasks:
Li dada has 0 tasks:
Wang dada has one task:
---------------------------
Zhang dada 2 has 0 tasks:
Li dada 2 has 0 tasks:
Wang dada 2 has 0 tasks:
---------------------------
-----Once again, it's Wang dada-----
-----Wang dada / Li dada rejected the task-----
---------------------------
Zhang dada has 0 tasks:
Li dada has 0 tasks:
Wang dada has 0 tasks:
---------------------------
Zhang dada 2 has 0 tasks:
Li dada 2 has 0 tasks:
Wang dada 2 has 0 tasks:
---------------------------
There is one task to query managers again:
-----managers ends the task and modifies the approver-----
---------------------------
Zhang dada has 0 tasks:
Li dada has 0 tasks:
Wang dada has 0 tasks:
---------------------------
Zhang dada 2 has 0 tasks:
Li dada 2 has one task:
Wang dada 2 has 0 tasks:
---------------------------
-----It's Li dada 2-----
SendRejectionMail start
oneUser = = Li dada 2
approved == false
days == 9
user = = Zhang dada
twoUser = = Wang dada 2
sid-0417597E-9B0F-4013-8F61-5139D73EF0D3      10028   []   null
SendRejectionMail start
-----Wang dada 2 / Li dada 2 finished the task-----
---------------------------
Zhang dada has one task:
Li dada has 0 tasks:
Wang dada has 0 tasks:
---------------------------
Zhang dada 2 has 0 tasks:
Li dada 2 has 0 tasks:
Wang dada 2 has 0 tasks:
---------------------------
-----Zhang dada finished the task-----
---------------------------
Zhang dada has 0 tasks:
Li dada has 0 tasks:
Wang dada has 0 tasks:
---------------------------
Zhang dada 2 has 0 tasks:
Li dada 2 has 0 tasks:
Wang dada 2 has 0 tasks:
---------------------------

Topics: Programming Tomcat JDBC MySQL xml