Activiti工作流--流程启动的相关操作--之五

首先还是先获取连接

private ProcessEngine processEngine;
	
@Before
public void initProcessEngine(){
	processEngine = ProcessEngines.getDefaultProcessEngine();
}
流程定义(流程图的设计)

流程部署涉及到的几个动作和表
流程部署表 act_re_deployment
流程定义表 act_re_procdef
流程资源表 act_ge_bytearray

@Test
public void testProcessDeployment(){
	InputStream inputStream = this.getClass()
								.getClassLoader()
								.getResourceAsStream("diagram/leave.zip");
	ZipInputStream zipInputStream = new ZipInputStream(inputStream); 
	Deployment deployment = processEngine.getRepositoryService()
								.createDeployment()
								.addZipInputStream(zipInputStream)
								.name("请假流程")
								.deploy();
	System.out.println("流程部署ID:"+deployment.getId());
	System.out.println("流程部署名称:"+deployment.getName());
}

流程图
leave.bpmn代码

<?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/test">
  <process id="leave" name="leaveProcess" isExecutable="true">
    <startEvent id="startevent1" name="Start"></startEvent>
    <userTask id="usertask1" name="请假申请" activiti:assignee="张三"></userTask>
    <userTask id="usertask2" name="领导审批" activiti:assignee="李四"></userTask>
    <endEvent id="endevent1" name="End"></endEvent>
    <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
    <sequenceFlow id="flow2" sourceRef="usertask1" targetRef="usertask2"></sequenceFlow>
    <sequenceFlow id="flow3" sourceRef="usertask2" targetRef="endevent1"></sequenceFlow>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_leave">
    <bpmndi:BPMNPlane bpmnElement="leave" id="BPMNPlane_leave">
      <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="375.0" y="22.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
        <omgdc:Bounds height="55.0" width="105.0" x="340.0" y="122.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask2" id="BPMNShape_usertask2">
        <omgdc:Bounds height="55.0" width="105.0" x="340.0" y="242.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="375.0" y="352.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
        <omgdi:waypoint x="392.0" y="57.0"></omgdi:waypoint>
        <omgdi:waypoint x="392.0" y="122.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
        <omgdi:waypoint x="392.0" y="177.0"></omgdi:waypoint>
        <omgdi:waypoint x="392.0" y="242.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">
        <omgdi:waypoint x="392.0" y="297.0"></omgdi:waypoint>
        <omgdi:waypoint x="392.0" y="352.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

Activiti工作流--流程启动的相关操作--之五_第1张图片

流程的启动(流程实例的产生) 考核请假的一个流程实例 new Class->Object

涉及到的表 :
act_ru_execution 流程实例表
act_ru_task 会产生一条待执行的任务记录
act_hi_taskinst 也会产生一条历史任务记录(注意:endtime is null)
注意: 以流程定义的key启动的话,默认会进入版本最新的流程

@Test
public void testStartProcess(){
	String processDefinitionKey = "leave";
	ProcessInstance processInstance = processEngine.getRuntimeService()
										.startProcessInstanceByKey(processDefinitionKey);
	System.out.println("流程部署的ID:"+processInstance.getDeploymentId());
	System.out.println("流程定义的ID:"+processInstance.getProcessDefinitionId());
	System.out.println("流程实例的ID:"+processInstance.getProcessInstanceId());
	
}
流程处理过程:查询个人任务

处理流程的步骤:查询个人任务 完成个人任务
涉及到的表:act_ru_task

@Test
public void testTaskQuery(){
	String processInstanceId = "40001";
	String assignee = "李四";
	List<Task> tasks = processEngine.getTaskService()
							.createTaskQuery()
							.taskAssignee(assignee)
							.processInstanceId(processInstanceId)
							.list();
	if (tasks != null && tasks.size() > 0) {
		for (Task task : tasks) {
			System.out.println("流程定义ID:"+task.getProcessDefinitionId());
			System.out.println("流程实例ID:"+task.getProcessInstanceId());
			System.out.println("执行对象ID:"+task.getExecutionId());
			System.out.println("任务ID:"+task.getId());
			System.out.println("任务名称:"+task.getName());
			System.out.println("任务创建时间:"+task.getCreateTime());
		}
	}
}
流程处理过程:完成个人任务

处理流程的步骤:查询个人任务 完成个人任务
涉及到的表:act_ru_task

@Test
public void testCompleteTask(){
	String taskId = "42502";
	processEngine.getTaskService().complete(taskId);
	System.out.println("领导审批");
}
在流程的执行过程中,我们需要查询流程执行到了哪一个状态

查询的是act_ru_execution 流程实例表
当流程结束了 那么在act_ru_execution 在这张表中就没有了数据记录

@Test
public void testQueryProinsatanceState(){
	String processInstanceId = "40001";
	ProcessInstance singleResult = processEngine.getRuntimeService()
										.createProcessInstanceQuery()
										.processInstanceId(processInstanceId)
										.singleResult();
	if (singleResult != null) {
		System.out.println("流程执行到:"+singleResult.getActivityId());
	}else{
		System.out.println("流程执行完毕");
	}
}
当某个任务结束以后

当某个任务结束以后,在任务表act_ru_task中的记录会被删除 ,
但是act_hi_taskinst记录的endtime会加上 ,所以我们可以从这个表查询我们的任务执行历史记录

@Test
public void testQueryHistoryTassk(){
	String assignee = "李四";
	List<HistoricTaskInstance> historyTaskList = processEngine.getHistoryService()
										.createHistoricTaskInstanceQuery()
										.taskAssignee(assignee)
										.list();
	if (historyTaskList != null && historyTaskList.size() > 0) {
		for (HistoricTaskInstance historicTaskInstance : historyTaskList) {
			System.out.println("流程定义ID:"+historicTaskInstance.getProcessDefinitionId());
			System.out.println("流程实例ID:"+historicTaskInstance.getProcessInstanceId());
			System.out.println("执行对象ID:"+historicTaskInstance.getExecutionId());
			System.out.println("历史任务ID:"+historicTaskInstance.getId());
			System.out.println("历史任务名称:"+historicTaskInstance.getName());
			System.out.println("历史任务结束时间:"+historicTaskInstance.getEndTime());
			System.out.println("=================================================");
		}
	}
}
当某个流程实例执行完

当某个流程实例执行完以后以后,在流程实例表表act_ru_execution中的记录会被删除 ,
但是act_hi_procinst表中记录的endtime会加上 ,所以我们可以从这个表查询我们的历史流程实例记录

@Test
public void testQueryHistoryProcessInstance(){
	String processInstanceId = "40001";
	HistoricProcessInstance hisProcessInst = processEngine.getHistoryService()
		.createHistoricProcessInstanceQuery()
		.processInstanceId(processInstanceId)
		.singleResult();
	if (hisProcessInst != null) {
		System.out.println("流程定义ID:"+hisProcessInst.getProcessDefinitionId());
			System.out.println("执行对象ID:"+hisProcessInst.getId());
	}
}

【上一篇:Activiti工作流–流程定义的相关操作–之四】
【下一篇:Activiti工作流–流程变量的设置与应用–之六】

你可能感兴趣的:(Activiti工作流)