流程如下,首先需要部门经理审批,如果请假天数大于2天,则需要总经理审批,否则HR审批即可
一:创建maven项目,项目结构如下:
加入依赖
pom.xml
<dependency> <groupId>org.activiti</groupId> <artifactId>activiti-engine</artifactId> <version>5.17.0</version> </dependency> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-bpmn-layout</artifactId> <version>5.17.0</version> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>9.3-1103-jdbc41</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.12</version> </dependency>
activiti.cfg.xml
数据库使用postgresql
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration"> <property name="databaseSchemaUpdate" value="drop-create"/> <property name="jdbcUrl" value="jdbc:postgresql://127.0.0.1/db_cloud"/> <property name="jdbcDriver" value="org.postgresql.Driver"/> <property name="jdbcUsername" value="admin"/> <property name="jdbcPassword" value="root"/> <property name="jobExecutorActivate" value="true"/> </bean> </beans>
流程文件leave.bpmn20.xml
<?xml version="1.0" encoding="UTF-8"?> <definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:activiti="http://activiti.org/bpmn" targetNamespace="Examples" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.omg.org/spec/BPMN/20100524/MODEL http://www.omg.org/spec/BPMN/2.0/20100501/BPMN20.xsd"> <process id="leave_process" name="某公司请假流程"> <startEvent id="leave" name="请假申请"></startEvent> <userTask id="dept_manager" name="部门经理审批"></userTask> <userTask id="boss" name="总经理审批"></userTask> <userTask id="hr" name="HR审批"></userTask> <sequenceFlow id="flow1" sourceRef="leave" targetRef="dept_manager"></sequenceFlow> <sequenceFlow id="flow2" sourceRef="dept_manager" targetRef="boss"> <conditionExpression xsi:type="tFormalExpression"><![CDATA[${day >= 3}]]></conditionExpression> </sequenceFlow> <sequenceFlow id="flow3" sourceRef="boss" targetRef="hr"></sequenceFlow> <sequenceFlow id="flow4" sourceRef="dept_manager" targetRef="hr"> <conditionExpression xsi:type="tFormalExpression"><![CDATA[${day < 3}]]></conditionExpression> </sequenceFlow> <sequenceFlow id="flow5" sourceRef="hr" targetRef="end"></sequenceFlow> <endEvent id="end" name="结束"></endEvent> </process> </definitions>
流程图如下:
二:代码演示
Leave.java
package com.lala.activiti; 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.RepositoryService; import org.activiti.engine.RuntimeService; import org.activiti.engine.TaskService; import org.activiti.engine.task.Task; import org.activiti.engine.task.TaskQuery; public class Leave { static void log(Object obj) { System.out.println(obj); } static void run(ProcessEngine processEngine) throws Exception { RepositoryService repositoryService = processEngine.getRepositoryService(); repositoryService.createDeployment().addClasspathResource("leave.bpmn20.xml").deploy(); RuntimeService runtimeService = processEngine.getRuntimeService(); Map<String, Object> variables = new HashMap<>(); variables.put("userid", "10010"); variables.put("day", 3); runtimeService.startProcessInstanceByKey("leave_process", variables); TaskService taskService = processEngine.getTaskService(); TaskQuery query = taskService.createTaskQuery(); List<Task> tasks = query.list(); for(Task task : tasks) { log(task.getId() + "," + task.getName()); taskService.complete(task.getId(), variables); } log("--------------------"); tasks = query.list(); for(Task task : tasks) { log(task.getId() + "," + task.getName()); taskService.complete(task.getId(), variables); } log("--------------------"); tasks = query.list(); for(Task task : tasks) { log(task.getId() + "," + task.getName()); taskService.complete(task.getId(), variables); } } public static void main(String[] args) { ProcessEngine processEngine = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml").buildProcessEngine(); try { run(processEngine); } catch (Exception e) { e.printStackTrace(); } processEngine.close(); } }
当
variables.put("day", 3);
大于等于3的时候,输出
9,部门经理审批
--------------------
11,总经理审批
--------------------
13,HR审批
否则,输出
9,部门经理审批
--------------------
11,HR审批