import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.repository.DeploymentQuery;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.runtime.ProcessInstanceQuery;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
/**
* 使用Activiti框架的API操作流程
*
* @author PuHaiyang
* @createTime 2016年6月28日 下午4:50:39
* @email 761396462@qq.com
* @function Activiti基本功能测试
*
*/
public class MyActivitiAPITest {
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
@Test
public void deployActiviti() {
// 如果之前没有创建activiti相干的表,调用它会自动创建,由activiti-context.xml文件内容创建
DeploymentBuilder deploymentBuilder = processEngine.getRepositoryService().createDeployment();
deploymentBuilder.addClasspathResource("MyTest.bpmn");
deploymentBuilder.addClasspathResource("MyTest.png");
deploymentBuilder.name("i_am_name");
Deployment deploy = deploymentBuilder.deploy();
System.out.println(deploy.getId());
// 部署信息表 insert into ACT_RE_DEPLOYMENT(ID_, NAME_, CATEGORY_,
// DEPLOY_TIME_) values(?, ?, ?, ?)
// 2进制数据表,bpmn文件插入 insert into ACT_GE_BYTEARRAY(ID_, REV_, NAME_,
// BYTES_, DEPLOYMENT_ID_, GENERATED_) values (?, 1, ?, ?, ?, ?)
// 2进制数据表,png文件插入 insert into ACT_GE_BYTEARRAY(ID_, REV_, NAME_, BYTES_,
// DEPLOYMENT_ID_, GENERATED_) values (?, 1, ?, ?, ?, ?)
// 流程定义数据表 insert into ACT_RE_PROCDEF(ID_, REV_, CATEGORY_, NAME_, KEY_,
// VERSION_, DEPLOYMENT_ID_, RESOURCE_NAME_, DGRM_RESOURCE_NAME_,
// DESCRIPTION_, HAS_START_FORM_KEY_, SUSPENSION_STATE_) values (?, 1,
// ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
}
/**
* 查询部署列表
*/
@Test
public void queryDeployment() {
DeploymentQuery deploymentQuery = processEngine.getRepositoryService().createDeploymentQuery();
List<Deployment> list = deploymentQuery.list();
for (Deployment deployment : list) {
String id = deployment.getId();
System.out.println(id);
}
}
/**
* 删除部署信息
*/
@Test
public void deleteDeployment() {
String deploymentId = "1";
// 第2个参数代表级联操作
processEngine.getRepositoryService().deleteDeployment(deploymentId, true);
// 删除所有相干的activiti信息
}
/**
* 查询1次部署对应的流程定义文件名称和对应的输入流(bpmn png)
*
* @throws Exception
*/
@Test
public void queryDeploymentResource() throws Exception {
String deploymentId = "101";
List<String> names = processEngine.getRepositoryService().getDeploymentResourceNames(deploymentId);
for (String name : names) {
System.out.println(name);
InputStream in = processEngine.getRepositoryService().getResourceAsStream(deploymentId, name);
// 将文件保存到本地磁盘
// FileUtils来自commons-io-xxx.jar
FileUtils.copyInputStreamToFile(in, new File("d:\\" + name));
in.close();
}
}
/**
* 取得流程表中流程图
*
* @throws Exception
*/
@Test
public void getProcessDiagream() throws Exception {
String processDefinitionId = "myProcess:1:104";
InputStream pngInputStream = processEngine.getRepositoryService().getProcessDiagram(processDefinitionId);
FileUtils.copyInputStreamToFile(pngInputStream, new File("d:\\my.png"));
}
/**
* 启动流程实例 <br>
* 方式1:根据流程定义的id启动 <br>
* 方式2:根据流程定义的key启动(自动选择最新版本的流程定义启动流程实例)
*/
@Test
public void startProcess() {
/*
* 方式1:根据流程定义的id启动 String processDefinitionId = "myProcess:1:104";
* ProcessInstance processInstance =
* processEngine.getRuntimeService().startProcessInstanceById
* (processDefinitionId ); System.out.println(processInstance.getId());
*/
// 方式2:根据流程定义的key启动(自动选择最新版本的流程定义启动流程实例)
String processDefinitionKey = "myProcess";
ProcessInstance processInstance = processEngine.getRuntimeService()
.startProcessInstanceByKey(processDefinitionKey);
System.out.println(processInstance.getId());
// 调用SQL语句以下
// 1 ACT_RU_EXECUTION 运行时流程履行实例表
// insert into ACT_RU_EXECUTION (ID_, REV_, PROC_INST_ID_,
// BUSINESS_KEY_, PROC_DEF_ID_, ACT_ID_, IS_ACTIVE_, IS_CONCURRENT_,
// IS_SCOPE_,IS_EVENT_SCOPE_, PARENT_ID_, SUPER_EXEC_,
// SUSPENSION_STATE_, CACHED_ENT_STATE_) values ( ?, 1, ?, ?, ?, ?, ?,
// ?, ?, ?, ?, ?, ?, ? )
// 2 ACT_HI_PROCINST 历史流程实例表
// insert into ACT_HI_PROCINST ( ID_, PROC_INST_ID_, BUSINESS_KEY_,
// PROC_DEF_ID_, START_TIME_, END_TIME_, DURATION_, START_USER_ID_,
// START_ACT_ID_, END_ACT_ID_, SUPER_PROCESS_INSTANCE_ID_,
// DELETE_REASON_ ) values ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
// 3 ACT_HI_ACTINST 历史节点表
// insert into ACT_HI_ACTINST ( ID_, PROC_DEF_ID_, PROC_INST_ID_,
// EXECUTION_ID_, ACT_ID_, TASK_ID_, CALL_PROC_INST_ID_, ACT_NAME_,
// ACT_TYPE_, ASSIGNEE_, START_TIME_, END_TIME_, DURATION_ ) values ( ?,
// ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
// 4 ACT_HI_ACTINST 历史节点表
// insert into ACT_HI_ACTINST ( ID_, PROC_DEF_ID_, PROC_INST_ID_,
// EXECUTION_ID_, ACT_ID_, TASK_ID_, CALL_PROC_INST_ID_, ACT_NAME_,
// ACT_TYPE_, ASSIGNEE_, START_TIME_, END_TIME_, DURATION_ ) values ( ?,
// ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
// 5 ACT_RU_TASK 运行时任务节点表
// insert into ACT_RU_TASK (ID_, REV_, NAME_, PARENT_TASK_ID_,
// DESCRIPTION_, PRIORITY_, CREATE_TIME_, OWNER_, ASSIGNEE_,
// DELEGATION_, EXECUTION_ID_, PROC_INST_ID_, PROC_DEF_ID_,
// TASK_DEF_KEY_, DUE_DATE_, SUSPENSION_STATE_) values (?, 1, ?, ?, ?,
// ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
// 6 ACT_HI_TASKINST 历史任务实例表
// insert into ACT_HI_TASKINST ( ID_, PROC_DEF_ID_, PROC_INST_ID_,
// EXECUTION_ID_, NAME_, PARENT_TASK_ID_, DESCRIPTION_, OWNER_,
// ASSIGNEE_, START_TIME_, CLAIM_TIME_, END_TIME_, DURATION_,
// DELETE_REASON_, TASK_DEF_KEY_, FORM_KEY_, PRIORITY_, DUE_DATE_ )
// values ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
// 7 ACT_RU_IDENTITYLINK 运行时流程人员表,主要存储任务节点与参与者的相干信息
// insert into ACT_RU_IDENTITYLINK (ID_, REV_, TYPE_, USER_ID_,
// GROUP_ID_, TASK_ID_, PROC_INST_ID_, PROC_DEF_ID_) values (?, 1, ?, ?,
// ?, ?, ?, ?)
// 8 ACT_HI_IDENTITYLINK 历史流程人员表
// insert into ACT_HI_IDENTITYLINK (ID_, TYPE_, USER_ID_, GROUP_ID_,
// TASK_ID_, PROC_INST_ID_) values (?, ?, ?, ?, ?, ?)
}
/**
* 查询流程实例列表,查询act_ru_execution表
*/
@Test
public void queryRuntimeProcess() {
// 流程实例查询对象,查询act_ru_execution表
ProcessInstanceQuery query = processEngine.getRuntimeService().createProcessInstanceQuery();
query.processDefinitionKey("myProcess");
query.orderByProcessInstanceId().desc();
query.listPage(0, 2);
List<ProcessInstance> list = query.list();
for (ProcessInstance pi : list) {
System.out.println(pi.getId() + " " + pi.getActivityId());
}
}
/**
* 不通过<br>
* 删除流程实例
*/
@Test
public void deleteProcess() {
String processInstanceId = "201";
processEngine.getRuntimeService().deleteProcessInstance(processInstanceId, "不准逃课!");
// 履行删除部份SQL以下
// 1 ACT_RU_TASK 运行时任务节点表
// delete from ACT_RU_TASK where ID_ = ? and REV_ = ?
// 2 ACT_RU_IDENTITYLINK 运行时流程人员表,主要存储任务节点与参与者的相干信息
// delete from ACT_RU_IDENTITYLINK where ID_ = ?
// 3 ACT_RU_EXECUTION 运行时流程履行实例表
// delete from ACT_RU_EXECUTION where ID_ = ? and REV_ = ?
// 履行更新部份SQL以下
// 1 ACT_HI_PROCINST 历史流程实例表
// update ACT_HI_PROCINST set PROC_DEF_ID_ = ?, START_TIME_ = ?,
// END_TIME_ = ?, DURATION_ = ?, END_ACT_ID_ = ?, DELETE_REASON_ = ?
// where ID_ = ?
// 2 ACT_HI_ACTINST 历史节点表
// update ACT_HI_ACTINST set EXECUTION_ID_ = ?, ASSIGNEE_ = ?, END_TIME_
// = ?, DURATION_ = ? where ID_ = ?
// 3 ACT_HI_TASKINST 历史任务实例表
// update ACT_HI_TASKINST set EXECUTION_ID_ = ?, NAME_ = ?,
// PARENT_TASK_ID_ = ?, DESCRIPTION_ = ?, OWNER_ = ?, ASSIGNEE_ = ?,
// CLAIM_TIME_ = ?, END_TIME_ = ?, DURATION_ = ?, DELETE_REASON_ = ?,
// TASK_DEF_KEY_ = ?, FORM_KEY_ = ?, PRIORITY_ = ?, DUE_DATE_ = ? where
// ID_ = ?
}
/**
* 查询任务
*/
@Test
public void queryTask() {
// 任务查询对象,查询act_ru_task表
TaskQuery query = processEngine.getTaskService().createTaskQuery();
String assignee = "班主任";
query.taskAssignee(assignee);
query.orderByTaskCreateTime().desc();
List<Task> list = query.list();
for (Task task : list) {
System.out.println(task.getId());
}
// 履行sql以下
// 1 ACT_GE_PROPERTY 属性数据表存储全部流程引擎级别的数据,初始化表结构时,会默许插入3条记录,
// select * from ACT_GE_PROPERTY where NAME_ = ?
// 2 ACT_RU_TASK 运行时任务节点表
// select distinct RES.* from ACT_RU_TASK RES WHERE RES.ASSIGNEE_ = ?
// order by RES.CREATE_TIME_ desc LIMIT ? OFFSET ?
}
/**
* 处理任务
*/
@Test
public void completeTask() {
String taskId = "304";
processEngine.getTaskService().complete(taskId);
// 履行的主要SQL以下
// 1 ACT_GE_PROPERTY 属性数据表存储全部流程引擎级别的数据,初始化表结构时,会默许插入3条记录
// update ACT_GE_PROPERTY SET REV_ = ?, VALUE_ = ? where NAME_ = ? and
// REV_ = ?
// 2 ACT_HI_ACTINST 历史节点表
// insert into ACT_HI_ACTINST ( ID_, PROC_DEF_ID_, PROC_INST_ID_,
// EXECUTION_ID_, ACT_ID_, TASK_ID_, CALL_PROC_INST_ID_, ACT_NAME_,
// ACT_TYPE_, ASSIGNEE_, START_TIME_, END_TIME_, DURATION_ ) values ( ?,
// ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
// 3 ACT_RU_TASK 运行时任务节点表
// insert into ACT_RU_TASK (ID_, REV_, NAME_, PARENT_TASK_ID_,
// DESCRIPTION_, PRIORITY_, CREATE_TIME_, OWNER_, ASSIGNEE_,
// DELEGATION_, EXECUTION_ID_, PROC_INST_ID_, PROC_DEF_ID_,
// TASK_DEF_KEY_, DUE_DATE_, SUSPENSION_STATE_) values (?, 1, ?, ?, ?,
// ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
// 4 ACT_HI_TASKINST 历史任务实例表
// insert into ACT_HI_TASKINST ( ID_, PROC_DEF_ID_, PROC_INST_ID_,
// EXECUTION_ID_, NAME_, PARENT_TASK_ID_, DESCRIPTION_, OWNER_,
// ASSIGNEE_, START_TIME_, CLAIM_TIME_, END_TIME_, DURATION_,
// DELETE_REASON_, TASK_DEF_KEY_, FORM_KEY_, PRIORITY_, DUE_DATE_ )
// values ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
// 5 ACT_RU_IDENTITYLINK 运行时流程人员表,主要存储任务节点与参与者的相干信息
// insert into ACT_RU_IDENTITYLINK (ID_, REV_, TYPE_, USER_ID_,
// GROUP_ID_, TASK_ID_, PROC_INST_ID_, PROC_DEF_ID_) values (?, 1, ?, ?,
// ?, ?, ?, ?)
// 6 ACT_HI_IDENTITYLINK 历史流程人员表
// insert into ACT_HI_IDENTITYLINK (ID_, TYPE_, USER_ID_, GROUP_ID_,
// TASK_ID_, PROC_INST_ID_) values (?, ?, ?, ?, ?, ?)
// 7 ACT_HI_TASKINST 历史任务实例表
// update ACT_HI_TASKINST set EXECUTION_ID_ = ?, NAME_ = ?,
// PARENT_TASK_ID_ = ?, DESCRIPTION_ = ?, OWNER_ = ?, ASSIGNEE_ = ?,
// CLAIM_TIME_ = ?, END_TIME_ = ?, DURATION_ = ?, DELETE_REASON_ = ?,
// TASK_DEF_KEY_ = ?, FORM_KEY_ = ?, PRIORITY_ = ?, DUE_DATE_ = ? where
// ID_ = ?
// 8 ACT_RU_EXECUTION 运行时流程履行实例表
// update ACT_RU_EXECUTION set REV_ = ?, PROC_DEF_ID_ = ?, ACT_ID_ = ?,
// IS_ACTIVE_ = ?, IS_CONCURRENT_ = ?, IS_SCOPE_ = ?, IS_EVENT_SCOPE_ =
// ?, PARENT_ID_ = ?, SUPER_EXEC_ = ?, SUSPENSION_STATE_ = ?,
// CACHED_ENT_STATE_ = ? where ID_ = ? and REV_ = ?
// 9 ACT_HI_ACTINST 历史节点表
// update ACT_HI_ACTINST set EXECUTION_ID_ = ?, ASSIGNEE_ = ?, END_TIME_
// = ?, DURATION_ = ? where ID_ = ?
// 10 ACT_RU_TASK 运行时任务节点表
// delete from ACT_RU_TASK where ID_ = ? and REV_ = ?
}
/**
* 直接将流程向下履行1步
*/
@Test
public void signal() {
String executionId = "301";// 流程实例id
processEngine.getRuntimeService().signal(executionId);
}
/**
* 查询最新版本的流程定义列表
*/
@Test
public void getLastTasks() {
ProcessDefinitionQuery query = processEngine.getRepositoryService().createProcessDefinitionQuery();
query.orderByProcessDefinitionVersion().asc();
List<ProcessDefinition> list = query.list();
Map<String, ProcessDefinition> map = new HashMap<String, ProcessDefinition>();
for (ProcessDefinition pd : list) {
map.put(pd.getKey(), pd);
}
ArrayList<ProcessDefinition> lastList = new ArrayList<>(map.values());
for (ProcessDefinition processDefinition : lastList) {
System.out.println(processDefinition.getName() + " " + processDefinition.getVersion());
}
}
}