api.services
package com.gch.test02;
import static org.junit.Assert.assertNotNull;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipInputStream;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
/**
*
* @author Skywalker
*
*/
public class Test01 {
/**
* 说明
* ACT_RE_*:repository,包含流程定义和流程静态资源(图片,规则等)
* ACT_RU_*:runtime,包含流程实例,任务,变量,异步任务等运行中的数据
* 只在流程实例执行过程中保存这些数据,在流程结束时就会删除这些记录
* ACT_ID_*:identity,包含身份信息,比如用户和组等
* ACT_HI_*:history,包含历史数据,比如历史流程实例,变量,任务等
* ACT_GE_*:通用数据,存放资源文件
*/
//迈出第一步:初始化数据库
//1.执行sql脚本
//2.使用代码Create table
//3.使用配置文件:在类路径下创建Activiti.cfg.xml
//备注(建表策略):true不存在就创建,存在就直接使用
// false不存在抛异常
// create_drop先删除在创建
//迈出第二步:helloworld
//1获取流程引擎
@Test
public void testProcessEngine(){
ProcessEngine processEngine=ProcessEngines.getDefaultProcessEngine();
assertNotNull(processEngine);
}
//2部署流程定义
@Test
public void deploy(){
ProcessEngine processEngine=ProcessEngines.getDefaultProcessEngine();
// assertNotNull(processEngine);
Deployment deployment= processEngine.getRepositoryService()
.createDeployment()
.addClasspathResource("diagrams/helloworld.bpmn")
.addClasspathResource("diagrams/helloworld.png")
.deploy();
System.out.println(deployment.getId()+":"+deployment.getName());//17501:null
}
//3启动流程实例
@Test
public void startProcess(){
ProcessEngine processEngine=ProcessEngines.getDefaultProcessEngine();
//启动流程
//使用流程定义的key启动流程实例,默认会按照最新版本启动流程实例
String processDefinitionKey="helloworld";
ProcessInstance processInstance=processEngine.getRuntimeService()
.startProcessInstanceByKey(processDefinitionKey);
System.out.println(processInstance.getId()+":"+processInstance.getActivityId());//20001:usertask1
}
//3查看任务
@Test
public void queryMyTask(){
//指定任务办理者
String assignee="张三";
//获取流程引擎对象
ProcessEngine processEngine=ProcessEngines.getDefaultProcessEngine();
assertNotNull(processEngine);
//查询任务的列表
List<Task> tasks=processEngine.getTaskService().createTaskQuery()
.taskAssignee(assignee)
.list();
//遍历查看内容
for(Task task:tasks){
System.out.println(task.getId()+":"+task.getName());
System.out.println("++++++++++++++++++++++++++++++");
}
}
//4办理任务
@Test
public void completeTask(){
String taskId="12504";
//获取流程引擎对象
ProcessEngine processEngine=ProcessEngines.getDefaultProcessEngine();
//完成任务
processEngine.getTaskService().complete(taskId);
System.out.println("任务完成!");
}
//迈出第三步:流程定义的CURD
//1.部署流程定义
//部署(classpath)
@Test
public void deployByClassPath(){
//获取流程引擎对象
ProcessEngine processEngine=ProcessEngines.getDefaultProcessEngine();
//获取仓库服务,完成部署
Deployment deployment=processEngine.getRepositoryService()
.createDeployment()
.name("测试Helloworld")
.addClasspathResource("diagrams/helloworld.bpmn")
.addClasspathResource("diagrams/helloworld.png")
.deploy();
System.out.println(deployment.getId()+":"+deployment.getName());//25001:测试Helloworld
}
//2.部署(zip)
@Test
public void testDeploy_zip(){
//获取流程引擎对象
ProcessEngine processEngine=ProcessEngines.getDefaultProcessEngine();
InputStream in=this.getClass().getClassLoader().getResourceAsStream("diagrams/helloworld.zip");
ZipInputStream zipInputStream=new ZipInputStream(in);
//获取仓库服务,从类路径下部署
Deployment deployment=processEngine.getRepositoryService()
.createDeployment().name("CeshiHelloWorld")
.addZipInputStream(zipInputStream)
.deploy();
System.out.println(deployment.getId()+":"+deployment.getName());//27501:CeshiHelloWorld
}
//2.查询流程定义
@Test
public void queryProcessDefinition(){
//获取流程引擎对象
ProcessEngine processEngine=ProcessEngines.getDefaultProcessEngine();
//获取仓库服务对象,使用版本的升序排序,查询列表
List<ProcessDefinition> processDefinitions=processEngine.getRepositoryService().createProcessDefinitionQuery()
.orderByProcessDefinitionVersion()
.asc().list();
//遍历
for(ProcessDefinition processDefinition:processDefinitions){
System.out.println("Id:"+processDefinition.getId());
System.out.println("DeploymentId:"+processDefinition.getDeploymentId());
System.out.println("Name:"+processDefinition.getName());
System.out.println("Key:"+processDefinition.getKey());
System.out.println("Version:"+processDefinition.getVersion());
System.out.println("DiagramResourceName:"+processDefinition.getDiagramResourceName());
System.out.println("****************************************************************");
}
}
//3.删除流程定义(使用部署流程定义ID)
@Test
public void deleteDeployment(){
String deploymentId="5001";
//获取流程引擎对象
ProcessEngine processEngine=ProcessEngines.getDefaultProcessEngine();
//获取仓库服务对象
RepositoryService repositoryService=processEngine.getRepositoryService();
//普通删除
// repositoryService.deleteDeployment(deploymentId);
//级联删除,会删除和当前规则相关的所有信息,这在执行的信息,还有历史信息
boolean cascade=true;
repositoryService.deleteDeployment(deploymentId, cascade);
// repositoryService.deleteDeploymentCascade(deploymentId);
}
//4.查看流程定义的流程图
@Test
public void showView() {
String deploymentId="17501";
//获取流程引擎对象
ProcessEngine processEngine=ProcessEngines.getDefaultProcessEngine();
//获取仓库服务对象
List<String> names=processEngine.getRepositoryService()
.getDeploymentResourceNames(deploymentId);
String imageName="";
for(String name:names){
if(name.indexOf(".png")>=0){
imageName=name;
}
}
if(imageName!=null){
File f=new File("e://"+imageName);
InputStream in=processEngine.getRepositoryService()
.getResourceAsStream(deploymentId, imageName);
try {
FileUtils.copyInputStreamToFile(in, f);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//5.查询所有最新版本的流程定义
@Test
public void queryAllLastVersions() {
//获取流程引擎对象
ProcessEngine processEngine=ProcessEngines.getDefaultProcessEngine();
//查询,把最大的版本都排到后面
List<ProcessDefinition> processDefinitions=processEngine.getRepositoryService()
.createProcessDefinitionQuery()
.orderByProcessDefinitionVersion()
.asc()
.list();
//过滤出最新的版本
Map<String ,ProcessDefinition> map=new LinkedHashMap<String ,ProcessDefinition>();
for(ProcessDefinition pd:processDefinitions){
map.put(pd.getKey(), pd);
}
for(ProcessDefinition processDefinition:map.values()){
System.out.println("Id:"+processDefinition.getId());
System.out.println("DeploymentId:"+processDefinition.getDeploymentId());
System.out.println("Name:"+processDefinition.getName());
System.out.println("Key:"+processDefinition.getKey());
System.out.println("Version:"+processDefinition.getVersion());
System.out.println("DiagramResourceName:"+processDefinition.getDiagramResourceName());
System.out.println("****************************************************************");
}
}
//6.删除流程定义(删除key相同的所有不同版本的流程定义)
@Test
public void deleteByKey() {
//获取流程引擎对象
ProcessEngine processEngine=ProcessEngines.getDefaultProcessEngine();
//查询指定key的所有版本的流程定义
List<ProcessDefinition> list=processEngine.getRepositoryService()
.createProcessDefinitionQuery()
.processDefinitionKey("helloworld")
.list();
//循环删除
for(ProcessDefinition pf:list){
processEngine.getRepositoryService().deleteDeployment(pf.getDeploymentId(),true);
}
System.out.println("删除成功!");
}
//迈出第四步:流程实例和任务的执行
//部署
@Test
public void deployZIP(){
//获取流程引擎对象
ProcessEngine processEngine=ProcessEngines.getDefaultProcessEngine();
InputStream in=this.getClass().getClassLoader().getResourceAsStream("diagrams/StudentLeave.zip");
ZipInputStream zipInputStream=new ZipInputStream(in);
//获取仓库服务,从类路径下部署
Deployment deployment=processEngine.getRepositoryService()
.createDeployment().name("请假流程")
.addZipInputStream(zipInputStream)
.deploy();
System.out.println(deployment.getId()+":"+deployment.getName());//45001:请假流程
}
//启动
@Test
public void start4Process(){
ProcessEngine processEngine=ProcessEngines.getDefaultProcessEngine();
//启动流程
//使用流程定义的key启动流程实例,默认会按照最新版本启动流程实例
String processDefinitionKey="studentLeave";
ProcessInstance processInstance=processEngine
.getRuntimeService()
.startProcessInstanceByKey(processDefinitionKey);
System.out.println(processInstance.getId()+":"+processInstance.getActivityId());//47501:usertask1
}
//查看个人任务
@Test
public void query4MyTask(){
//指定任务办理者
String assignee="Miss Li";
// String assignee="Tom";
//获取流程引擎对象
ProcessEngine processEngine=ProcessEngines.getDefaultProcessEngine();
assertNotNull(processEngine);
//查询任务的列表
List<Task> tasks=processEngine.getTaskService().createTaskQuery()
.taskAssignee(assignee)
.orderByTaskCreateTime()
.desc()
.list();
//遍历查看内容
for(Task task:tasks){
System.out.println(task.getId()+":"+task.getName());
System.out.println("++++++++++++++++++++++++++++++");
}
}
//办理任务
@Test
public void complete4Task(){
String taskId="47504";
//获取流程引擎对象
ProcessEngine processEngine=ProcessEngines.getDefaultProcessEngine();
//完成任务
processEngine.getTaskService().complete(taskId);
System.out.println("任务完成!");
}
//查看流程状态(判断流程是正在执行还是已经结束)
@Test
public void queryProcessState(){
String processInstanceId="47501";
//获取流程引擎对象
ProcessEngine processEngine=ProcessEngines.getDefaultProcessEngine();
//通过流程实例id查询流程实例
ProcessInstance pi=processEngine.getRuntimeService()
.createProcessInstanceQuery()
.processInstanceId(processInstanceId)
.singleResult();
if(pi!=null){
System.out.println("当前流程在"+pi.getActivityId());
}else{
System.out.println("流程已结束!");
}
}
//查询历史任务
@Test
public void queryHistoryTask(){
//历史任务办理人
String taskAssignee="Tom";
//获取流程引擎对象
ProcessEngine processEngine=ProcessEngines.getDefaultProcessEngine();
//使用办理人查询流程实例
List<HistoricTaskInstance> list=processEngine.getHistoryService().createHistoricTaskInstanceQuery()
.taskAssignee(taskAssignee)
.list();
if(list!=null&&list.size()>0){
for(HistoricTaskInstance task:list){
System.out.println("任务id:"+task.getId());
System.out.println("流程实例ID:"+task.getProcessInstanceId());
System.out.println("任务办理人:"+task.getExecutionId());
System.out.println("执行对象id:"+task.getExecutionId());
System.out.println("start:"+task.getStartTime());
System.out.println("end:"+task.getEndTime());
System.out.println("dur:"+task.getDurationInMillis());
}
}
}
//查询历史的流程实例
@Test
public void queryHistoryProcessInstance(){
String processInstanceId="47501";
//获取流程引擎对象
ProcessEngine processEngine=ProcessEngines.getDefaultProcessEngine();
//使用办理人查询流程实例
HistoricProcessInstance hpi=processEngine.getHistoryService()
.createHistoricProcessInstanceQuery()
.processInstanceId(processInstanceId)
.singleResult();
System.out.println("流程定义id:"+hpi.getProcessDefinitionId());
System.out.println("流程实例ID:"+hpi.getId());
System.out.println("start:"+hpi.getStartTime());
System.out.println("end:"+hpi.getEndTime());
System.out.println("dur:"+hpi.getDurationInMillis());
}
/**
* Execution 执行对象
* 按照流程定义的规则执行一次的过程
* 对应的表
* act_ru_execution:正在执行的信息
* act_hi_procinst:已经执行完的历史流程实例信息
* act_hi_actinst:存放历史所有完成的活动
*
* processInstance流程实例
* 特指流程从开始到结束的最大的执行分支,一个执行的流程中,流程实例只有一个
* 注意
* 如果是单例流程,执行对象的id就是流程实例的id
* 如果一个流程有分支和聚合,那么执行对象id和流程实例id不同
* 一个流程中,流程实例只有一个,执行对象可以多个
*
* Task任务
* 执行到某任务环节时生成的任务信息
* 对应的表
* act_ru_task:正在执行的任务信息
* act_hi_taskinst:已经执行完成的历史任务信息
*/
}