回顾:
工作流之flow管理
10张表:
select * from act_re_deployment;
select * from act_re_procdef;
select * from act_ge_bytearray;
select * from act_ru_execution;
select * from act_ru_task;
select * from act_hi_taskinst;
select * from act_hi_procinst;
select * from act_hi_actinst;
select * from act_hi_varinst;
select * from act_ru_variable;
新建bpmn图, 流程定义的key设置为”ruzhi”:
在”经理审批通过”和”发送老板审批”的分支上设置流程变量:
① 测试: 部署–>启动流程, 此时流程执行到”经理审批”的节点上, 然后选择流程变量的分支为”经理审批通过”执行下去
package com.rl.activiti.test1;
import java.util.HashMap;
import java.util.Map;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.task.Task;
import org.junit.Before;
import org.junit.Test;
public class ActivitiTest1 {
//定义一个成员变量
RepositoryService repositoryService;
RuntimeService runtimeService;
TaskService taskService;
/**
* 获取流程引擎
*/
@Before
public void setUp() throws Exception {
//获取流程引擎对象, 默认是读取配置文件"activiti.cfg.xml"
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取RepositoryService接口的实现类
repositoryService = processEngine.getRepositoryService();
runtimeService = processEngine.getRuntimeService();
taskService = processEngine.getTaskService();
}
/**
* 部署流程①: 通过单文件的形式加载部署
*/
@Test
public void delpoyActiviti1() {
//获取部署对象
DeploymentBuilder deploymentBuilder = repositoryService.createDeployment();
deploymentBuilder.addClasspathResource("diagrams1/ruzhi.bpmn")
.name("入职流程").category("入职")
.addClasspathResource("diagrams1/ruzhi.png");
//部署流程
deploymentBuilder.deploy();
}
/**
* 启动流程
*/
@Test
public void startProcess() {
//根据流程的key来启动相对应的流程, 默认是启动最新版本的流程
runtimeService.startProcessInstanceByKey("ruzhi");
}
/**
* 完成任务: 执行"经理审批通过"分支
*/
@Test
public void completeTask1(){
String assignee = "manager";
Task task = taskService.createTaskQuery()
.processDefinitionKey("ruzhi").taskAssignee(assignee).singleResult();
Map<String, Object> map = new HashMap<String, Object>();
map.put("outcome", "经理审批通过");
taskService.complete(task.getId(), map);
}
}
结果: “经理审批通过”分支完成, 任务结束.
② 测试: 执行”发送老板审批”分支
/**
* 完成任务: 执行"发送老板审批"分支
*/
@Test
public void completeTask2(){
String assignee = "manager";
Task task = taskService.createTaskQuery()
.processDefinitionKey("ruzhi").taskAssignee(assignee).singleResult();
Map<String, Object> map = new HashMap<String, Object>();
map.put("outcome", "发送老板审批");
taskService.complete(task.getId(), map);
}
/**
* 老板办理任务
*/
@Test
public void completeTask3(){
String assignee = "boss";
Task task = taskService.createTaskQuery()
.processDefinitionKey("ruzhi").taskAssignee(assignee).singleResult();
taskService.complete(task.getId());
}
结果: “发送老板审批”分支完成, 任务结束.
获取outcome和income的连接线
获取分支连接线非常重要, 可以在项目中自动生成按钮.
测试代码:
@Test
public void getOutcomeFlow(){
String assignee = "manager";
Task task = taskService.createTaskQuery()
.processDefinitionKey("ruzhi").taskAssignee(assignee).singleResult();
//获取流程定义
// repositoryService.createProcessDefinitionQuery().processDefinitionKey("ruzhi").singleResult();
//或者
//获取流程定义实体的对象(不需要创建查询对象*****)
ProcessDefinitionEntity processDefinition = (ProcessDefinitionEntity) repositoryService
.getProcessDefinition(task.getProcessDefinitionId());
//获取流程实例
ProcessInstance processInstance = runtimeService.createProcessInstanceQuery()
.processInstanceId(task.getProcessInstanceId()).singleResult();
//获取当前流程实例的活动id
String activityId = processInstance.getActivityId();
//获取当前流程定义中的活动节点(包括开始和结束节点)
ActivityImpl activityImpl = processDefinition.findActivity(activityId);
//获取输出的flow
List<PvmTransition> transitionList = activityImpl.getOutgoingTransitions();//改为getIncomingTransitions()就是获取income的连接线
for(PvmTransition transition: transitionList){
String flowName = (String) transition.getProperty("name");
System.out.println(flowName);
}
}
问题: 获取当前节点的来路income
新建一个bpmn文件:
测试代码:
/**
* 获取来路
*/
public List<String> getIncomeFlow(String assignee){
List<String> strList = new ArrayList<String>();
Task task = taskService.createTaskQuery()
.processDefinitionKey("leave_bill").taskAssignee(assignee).singleResult();
ProcessDefinitionEntity processDefinition = (ProcessDefinitionEntity) repositoryService
.getProcessDefinition(task.getProcessDefinitionId());
//获取流程实例
ProcessInstance processInstance = runtimeService.createProcessInstanceQuery()
.processInstanceId(task.getProcessInstanceId()).singleResult();
//获取当前流程实例的活动id
String activityId = processInstance.getActivityId();
//获取当前流程定义中的活动节点(包括开始和结束节点)
ActivityImpl activityImpl = processDefinition.findActivity(activityId);
//获取输出的flow
List<PvmTransition> transitionList = activityImpl.getIncomingTransitions();
for(PvmTransition transition: transitionList){
String flowName = (String) transition.getProperty("name");
strList.add(flowName);
}
return strList;
}
/**
* 获取当前节点的来路
* ① 获得当前节点的所有来路(其中一条是来路)
* ② 拿到上一步的节点(历史节点),找到它的所有出路(其中有一条出路是当前节点的来路)
* ③ 找到以上两步中的重复的连线就是当前节点的来路
*/
@Test
public void getIncomingFlow(){
String assignee = "employee";
List<String> preActivityOutcomeList = new ArrayList<String>();
Task task = taskService.createTaskQuery().processDefinitionKey("leave_bill")
.taskAssignee(assignee).singleResult();
List<HistoricActivityInstance> historicActivityInstanceList = historyService
.createHistoricActivityInstanceQuery()
.processInstanceId(task.getProcessInstanceId())
.finished()//已经结束的活动节点
.orderByHistoricActivityInstanceStartTime().desc().list();
for(HistoricActivityInstance historicActivityInstance: historicActivityInstanceList){
System.out.println("历史活动ID: "+historicActivityInstance.getActivityId());
System.out.println("历史活动名称: "+historicActivityInstance.getActivityName());
}
//获取当前节点的前一个活动节点
HistoricActivityInstance historicActivityInstance = historicActivityInstanceList.get(0);
//获取前一个活动节点的id
String activityId = historicActivityInstance.getActivityId();
//获取流程定义实体的对象(不需要创建查询对象*****)
ProcessDefinitionEntity processDefinition = (ProcessDefinitionEntity) repositoryService
.getProcessDefinition(task.getProcessDefinitionId());
//获取前一个活动节点的实现类对象
ActivityImpl activityImpl = processDefinition.findActivity(activityId);
//获取前一个活动节点的所有出路
List<PvmTransition> outgoingList = activityImpl.getOutgoingTransitions();
for(PvmTransition outgoing: outgoingList){
String flowName = (String) outgoing.getProperty("name");
System.out.println(flowName);
preActivityOutcomeList.add(flowName);
}
//获取当前节点的所有入路
List<String> incomeFlows = this.getIncomeFlow(assignee);
System.out.println(incomeFlows);
String incomeFlow = null;
loop : for(String income: incomeFlows){
for(String outcome: preActivityOutcomeList){
if(income.equals(outcome)){
incomeFlow = income;//incomeFlow = outcome;
break loop;
}
}
}
System.out.println("当前节点的来路是: "+incomeFlow);
}
版权声明:本文为ip_JL原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。