Activiti6:模拟钉钉上面的请假流程(使用web画图并导出xml然后使用java执行流程)

1.声明

当前内容主要为本人学习和测试Activiti6这个工作流的基本操作,模拟钉钉上面的请假流程(简单版)

当前内容主要有:

  1. 使用官方的web-app方式画图
  2. 将当前流程图导出为xml配置
  3. 将xml配置导入项目,并使用java方式加载并实现流程执行

2.画出请假执行流程图

在这里插入图片描述

其中都是使用最简单的元素

重要点:
1.启动当前流程需要三个:员工名称,请假时间,请假类型
在这里插入图片描述
例如下面参数:
在这里插入图片描述
此时发起流程就必须这些参数,注意required设置为true

流程发起后,通过审核人,此时审核人需要一个参数来执行后面是否允许请假通过,同样的设置为approve,设置为boolean类型

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
之后流程到了条件判断,这里其实就是判断当前的定义的变量approve==true或者approve==false

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
然后后面的审核人和判断就和这个一样即可完成条件判断(所以这里省略后面的userTask和条件判断)

之后到达抄送人
在这里插入图片描述
在这里插入图片描述
此时流程执行结束

保存后(上面的请假类型是枚举所以需要在生成的xml中修改)

3.将画出的图导出为xml文件

在这里插入图片描述
导出内容如下

<?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/processdef">
  <process id="OffWork" name="OffWork" isExecutable="true">
    <documentation>钉钉上的请假流程</documentation>
    <startEvent id="offWorkStart" name="发起请求">
      <extensionElements>
        <activiti:formProperty id="username" name="员工名称" type="string" required="true"></activiti:formProperty>
        <activiti:formProperty id="offwork_type" name="请假类型" type="enum" required="true"></activiti:formProperty>
        <activiti:formProperty id="offwork_day" name="请假天数" type="long" required="true"></activiti:formProperty>
      </extensionElements>
    </startEvent>
    <userTask id="sp_1" name="审批人:人事部" activiti:candidateGroups="management">
      <extensionElements>
        <activiti:formProperty id="approve" name="是否同意" type="boolean" required="true"></activiti:formProperty>
      </extensionElements>
    </userTask>
    <userTask id="sp_2" name="审批人:人事部主管" activiti:candidateGroups="management">
      <extensionElements>
        <activiti:formProperty id="approve" name="是否同意" type="boolean" required="true"></activiti:formProperty>
      </extensionElements>
    </userTask>
    <endEvent id="offWorkEnd" name="结束"></endEvent>
    <exclusiveGateway id="sp_1_check" name="判断"></exclusiveGateway>
    <exclusiveGateway id="sp_2_check" name="判断"></exclusiveGateway>
    <userTask id="send_person" name="抄送人" activiti:candidateGroups="system"></userTask>
    <sequenceFlow id="sd_boss" name="抄送给总经理" sourceRef="send_person" targetRef="mail_to_boss"></sequenceFlow>
    <sequenceFlow id="diapatcher_to_csr" name="转发" sourceRef="sp_2" targetRef="sp_2_check"></sequenceFlow>
    <sequenceFlow id="cs_ms_end" name="抄送结束" sourceRef="mail_to_ms" targetRef="offWorkEnd"></sequenceFlow>
    <sequenceFlow id="cs_boss_end" name="抄送结束" sourceRef="mail_to_boss" targetRef="offWorkEnd"></sequenceFlow>
    <sequenceFlow id="sp_1_disapprove" name="不同意" sourceRef="sp_1_check" targetRef="offWorkEnd">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${approve=='false'}]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="sp_2_approve" name="同意" sourceRef="sp_2_check" targetRef="send_person">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${approve=='true'}]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="sp_2_disapprove" name="不同意" sourceRef="sp_2_check" targetRef="offWorkEnd">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${approve=='false'}]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="dispatcher_to_zg" name="转发" sourceRef="sp_1" targetRef="sp_1_check"></sequenceFlow>
    <sequenceFlow id="sp_1_approve" name="同意" sourceRef="sp_1_check" targetRef="sp_2">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${approve=='true'}]]></conditionExpression>
    </sequenceFlow>
    <manualTask id="mail_to_ms" name="发送邮件给秘书"></manualTask>
    <manualTask id="mail_to_boss" name="发送邮件给总经理"></manualTask>
    <sequenceFlow id="sd_ms" name="抄送给秘书" sourceRef="send_person" targetRef="mail_to_ms"></sequenceFlow>
    <sequenceFlow id="submit_offwork" name="提交请假请求" sourceRef="offWorkStart" targetRef="sp_1"></sequenceFlow>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_OffWork">
    <bpmndi:BPMNPlane bpmnElement="OffWork" id="BPMNPlane_OffWork">
      <bpmndi:BPMNShape bpmnElement="offWorkStart" id="BPMNShape_offWorkStart">
        <omgdc:Bounds height="30.0" width="30.0" x="120.0" y="163.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sp_1" id="BPMNShape_sp_1">
        <omgdc:Bounds height="80.0" width="100.0" x="240.0" y="138.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sp_2" id="BPMNShape_sp_2">
        <omgdc:Bounds height="80.0" width="100.0" x="480.0" y="138.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="offWorkEnd" id="BPMNShape_offWorkEnd">
        <omgdc:Bounds height="28.0" width="28.0" x="1050.0" y="330.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sp_1_check" id="BPMNShape_sp_1_check">
        <omgdc:Bounds height="40.0" width="40.0" x="390.0" y="158.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sp_2_check" id="BPMNShape_sp_2_check">
        <omgdc:Bounds height="40.0" width="40.0" x="625.0" y="158.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="send_person" id="BPMNShape_send_person">
        <omgdc:Bounds height="80.0" width="100.0" x="720.0" y="138.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="mail_to_ms" id="BPMNShape_mail_to_ms">
        <omgdc:Bounds height="80.0" width="100.0" x="930.0" y="75.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="mail_to_boss" id="BPMNShape_mail_to_boss">
        <omgdc:Bounds height="80.0" width="100.0" x="930.0" y="180.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="sd_boss" id="BPMNEdge_sd_boss">
        <omgdi:waypoint x="820.0" y="178.375"></omgdi:waypoint>
        <omgdi:waypoint x="930.0" y="219.625"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="cs_boss_end" id="BPMNEdge_cs_boss_end">
        <omgdi:waypoint x="980.0" y="260.0"></omgdi:waypoint>
        <omgdi:waypoint x="980.0" y="344.0"></omgdi:waypoint>
        <omgdi:waypoint x="1050.0" y="344.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="diapatcher_to_csr" id="BPMNEdge_diapatcher_to_csr">
        <omgdi:waypoint x="580.0" y="178.2164502164502"></omgdi:waypoint>
        <omgdi:waypoint x="625.4130434782609" y="178.41304347826087"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sp_2_disapprove" id="BPMNEdge_sp_2_disapprove">
        <omgdi:waypoint x="645.5" y="197.5"></omgdi:waypoint>
        <omgdi:waypoint x="645.5" y="344.0"></omgdi:waypoint>
        <omgdi:waypoint x="1050.0" y="344.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sp_1_disapprove" id="BPMNEdge_sp_1_disapprove">
        <omgdi:waypoint x="410.5" y="197.5"></omgdi:waypoint>
        <omgdi:waypoint x="410.5" y="344.0"></omgdi:waypoint>
        <omgdi:waypoint x="1050.0" y="344.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="submit_offwork" id="BPMNEdge_submit_offwork">
        <omgdi:waypoint x="150.0" y="178.0"></omgdi:waypoint>
        <omgdi:waypoint x="240.0" y="178.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sd_ms" id="BPMNEdge_sd_ms">
        <omgdi:waypoint x="820.0" y="157.61607142857144"></omgdi:waypoint>
        <omgdi:waypoint x="930.0" y="115.38392857142857"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sp_2_approve" id="BPMNEdge_sp_2_approve">
        <omgdi:waypoint x="664.5766129032259" y="178.4233870967742"></omgdi:waypoint>
        <omgdi:waypoint x="720.0" y="178.2008032128514"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="dispatcher_to_zg" id="BPMNEdge_dispatcher_to_zg">
        <omgdi:waypoint x="340.0" y="178.20746887966806"></omgdi:waypoint>
        <omgdi:waypoint x="390.4166666666667" y="178.41666666666666"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sp_1_approve" id="BPMNEdge_sp_1_approve">
        <omgdi:waypoint x="429.6357142857143" y="178.36428571428573"></omgdi:waypoint>
        <omgdi:waypoint x="480.0" y="178.00709219858157"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="cs_ms_end" id="BPMNEdge_cs_ms_end">
        <omgdi:waypoint x="1030.0" y="115.0"></omgdi:waypoint>
        <omgdi:waypoint x="1064.0" y="115.0"></omgdi:waypoint>
        <omgdi:waypoint x="1064.0" y="330.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

4.开始补充部分内容

直接将该xml拷贝到项目中,然后找到enum这个请假类型的地方开始补充
在这里插入图片描述

<activiti:formProperty id="offwork_type" name="请假类型" type="enum" required="true">
	<activiti:value id="illVacation" name="病假"></activiti:value>
  	<activiti:value id="birthVacation" name="产假"></activiti:value>
  	<activiti:value id="ownerVacation" name="个人假"></activiti:value>
</activiti:formProperty>

5.执行java测试

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;

public class OffWorkActiviti6Test {
    
    
	public static void main(String[] args) {
    
    
		/*
		ProcessEngine processEngine = ProcessEngineConfiguration.createStandaloneInMemProcessEngineConfiguration()
		 * .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_FALSE)
		 * .setJdbcUrl("jdbc:h2:mem:my-own-db;DB_CLOSE_DELAY=1000")
		 * .setAsyncExecutorActivate(false) .buildProcessEngine();
		 */

		// 1. 加载对应的activiti的xml配置文件
		// ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
		ProcessEngine processEngine = ProcessEngineConfiguration.createStandaloneInMemProcessEngineConfiguration()
				.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE)
				.setJdbcUrl("jdbc:mysql://localhost:3306/hy_activiti6")
				.setJdbcDriver("com.mysql.jdbc.Driver")
				.setJdbcUsername("root")
				.setJdbcPassword("root")
				.setAsyncExecutorActivate(false)
				.buildProcessEngine();
		RepositoryService repositoryService = processEngine.getRepositoryService();
		repositoryService.createDeployment()
				.addClasspathResource("OffWork.bpmn20.xml").deploy();

		System.out .println("Number of process definitions: " + repositoryService.createProcessDefinitionQuery().count());
		
		// 创建一个雇员的名称为:Kermit发起一个请假流程,请1天病假
		String username = "Kermit";
		Map<String, Object> variables = new HashMap<String, Object>();
		variables.put("username", username);
		variables.put("type", "illVacation");
		variables.put("offWorkDays", 1);
		RuntimeService runtimeService = processEngine.getRuntimeService();
		// 这里为发起实际的请假请求
		ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("OffWork", variables);
		
		// Verify that we started a new process instance
		System.out.println("Number of process instances: " + runtimeService.createProcessInstanceQuery().count());
		
		System.out.println("员工:" + username + ",发起" + 1 + "天病假的请求");
		
		// 管理组获取所有的任务请求(包括请假等事项)
		// Fetch all tasks for the management group
		TaskService taskService = processEngine.getTaskService();
		List<Task> tasks = taskService.createTaskQuery().taskCandidateGroup("management").list();
		for (Task task : tasks) {
    
    
			System.out.println("Task available: " + task.getName());
		}

		
		// 同意请求,让下一位进行处理
		Task offWorkTask = tasks.get(0);
		variables = new HashMap<String, Object>();
		variables.put("approve", true);
		taskService.complete(offWorkTask.getId(), variables);
		System.out.println("人事部人员同意请假请求....");
		
		// 再次获取,下一个人的执行流程
		tasks = taskService.createTaskQuery().taskCandidateGroup("management").list();
		for (Task task : tasks) {
    
    
			System.out.println("Task available: " + task.getName());
		}
		
		// 人事部主管同意请求
		offWorkTask = tasks.get(0);
		variables = new HashMap<String, Object>();
		variables.put("approve", true);
		taskService.complete(offWorkTask.getId(), variables);
		System.out.println("人事部主管同意请假请求....");
		
		//系统执行抄送任务给当前的经理和秘书		
		tasks = taskService.createTaskQuery().taskCandidateGroup("system").list();
		for (Task task : tasks) {
    
    
			System.out.println("Task available: " + task.getName());
		}
		// 任务完成
		offWorkTask = tasks.get(0);
		taskService.complete(offWorkTask.getId());
		System.out.println("系统自动转发当前的请假给总经理和秘书....");
	}
}

执行结果:
在这里插入图片描述

查看数据库结果
在这里插入图片描述
整个流程执行成功!

6.总结

使用官方的web app的流程设计器和下载xml方式简化了开发步骤,和流程xml的编写,很容易编写(但是要小心传递的参数问题和判断条件问题)

猜你喜欢

转载自blog.csdn.net/weixin_45492007/article/details/115049239
今日推荐