[学习小结]Activiti5 流程规则管理

ProcessDefinitionTest:

import static org.junit.Assert.*;

import java.io.File;
import java.io.InputStream;
import java.util.List;
import java.util.zip.ZipInputStream;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.impl.pvm.process.ProcessDefinitionImpl;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.apache.commons.io.FileUtils;
import org.junit.Test;

/**
 * 使用API的步骤: 1)创建流程引擎ProcessEngine 2)获取相关服务对象实例 3)使用服务对象相关方法完成流程操作
 * 
 * @author LANHD
 *
 */
public class ProcessDefinitionTest {
//  初始化流程引擎对象
	private ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); 
	//  使用流程引擎获取到需要的服务对象实例
	private RepositoryService repositoryService = processEngine.getRepositoryService();
	
	// 1.发布规则
	/**
	 * 
	 * 	1.发布一个新的流程
	 *     会在数据库3张表中添加数据:
	 *        act_ge_bytearray      新增了2条数据(“规则文件”和“流程图片”)
	 *        act_re_deployment     新增了1条数据(流程的“显示别名”和“发布时间”)
	 *        act_re_procdef        新增了1条数据(流程规则相关信息[流程定义])
	 *        ID={KEY}:{VERSION}:{随机数}
	 * @throws Exception
	 */
	@Test
	public void deployProcess() throws Exception {
		// 1.创建一个发布配置对象
		
		DeploymentBuilder deploymentBuilder = repositoryService
				.createDeployment();
		// 2.添加发布的资源文件(“流程规则文件和流程图片”)
		InputStream in = Thread.currentThread().getContextClassLoader()
				.getResourceAsStream("LeaveFlow.bpmn");
		InputStream image = Thread.currentThread().getContextClassLoader()
				.getResourceAsStream("LeaveFlow.png");
		
		deploymentBuilder
					 .name("请假流程")
					 .addInputStream("LeaveFlow.bpmn", in) //添加规则文件
		             .addInputStream("LeaveFlow.png", image); //添加规则图片
		// 3.完成发布:使用deploy()发布流程
		deploymentBuilder.deploy();
	}
	
	/**
	 * 用ZIP方式发布流程
	 * this.getClass().getClassLoader().getResourceAsStream("LeaveFlow.zip");  从当前类所在包下加载指定名称文件的输入流
	 * this.getClass().getClassLoader().getResourceAsStream("/LeaveFlow.zip"); 从ClassPath根目录下加载指定名称文件的输入流
	 * this.getClass().getContextClassLoader().getResourceAsStream("LeaveFlow.zip") 从ClassPath根目录下加载指定名称文件的输入流
	 * Thread.currentThread().getContextClassLoader().getResourceAsStream("LeaveFlow.bpmn"); 从Classpath根目录下加载指定名称文件的输入流
	 *
	 *若果以ZIP文件方式发布流程,那么在发布成功后,Activiti框架会自动解压ZIP包中的文件,依次添加到act_ge_bytearray表中
	 */
	@Test
	public void deployeProcessByZIP(){
		//创建一个发布配置对象
		DeploymentBuilder deploymentBuilder=repositoryService.createDeployment();
		//添加发布的资源文件
		InputStream in=this.getClass().getResourceAsStream("/Leave.zip");
		System.out.println(in);
		ZipInputStream zipInputStream=new ZipInputStream(in);
		
//		调用deploy()发布流程
		deploymentBuilder.addZipInputStream(zipInputStream);
	}
	

	/**
	 * 
	 * 2.查看流程定义信息
	 * 
	 * 		在项目中,我们主要跟ProcessDefinition打交道
	 * 		ProcessDefinition:流程规则的整体信息(流程ID和版本)
	 * 		ID:		{key}:{version}:{随机数}
	 * 		NAME:	对应流程文件process节点的name属性
	 * 		KEY:	对应流程文件process节点的id属性
	 * 		VERSION:	发布时 ,查找当前系统中对应key的最高版本,如果找到了,在最高版本上“加1”,否则为1
	 * 
	 * 		ActivityImpl: 描述当前规则下每一个活动规则相关信息
	 * @throws Exception
	 */
	@Test
	public void queryProcessDefinition() throws Exception{
		//使用服务对象创建需要的查询对象
		ProcessDefinitionQuery definitionQuery=repositoryService.createProcessDefinitionQuery();
		//添加查询参数
		definitionQuery
					//过滤条件
					.processDefinitionKey("LeaveFlow")
//					.processDefinitionName(processDefinitionName)
//					分页条件
//					.listPage(firstResult, maxResults)
//					排序条件
					.orderByProcessDefinitionVersion().desc();
		
		//执行查询
		List<ProcessDefinition> pds=definitionQuery.list();
		
		for(ProcessDefinition pd:pds){
			System.out.println("id:"+pd.getId()+",name:"+pd.getName()+",key:"+pd.getKey()+",version:"+pd.getVersion());
			ProcessDefinitionImpl pdImpl=(ProcessDefinitionImpl) repositoryService.getProcessDefinition(pd.getId());
			System.out.println(pdImpl.getActivities());
		}
	}
	
	
	// 3.删除流程规则
	
	@Test
	public void deleteProcess() throws Exception{
		String deploymentId="1";
//		普通删除,如果当前规则下有正执行的流程,则删除失败(保护)
		repositoryService.deleteDeployment(deploymentId);
//		级联删除,相对比较暴力,会删除正在执行的流程信息和当前规则信息的所有历史(一般不推荐开放给普通用户)
//		repositoryService.deleteDeployment(deploymentId, true);
	}
	// 4.查看流程附件
	
	@Test
	public void quertImage() throws Exception{
		//获取发布ID
		String deploymentId="101";
		
		//查找这次发布的所有资源文件名称
		List<String> names= repositoryService.getDeploymentResourceNames(deploymentId);
		
		String imageName=null;
		
		for(String name:names){
			//指定规则,获取需要的文件名称
			if(name.indexOf(".png")>=0){
				imageName=name;
			}
		}
		
		if(imageName!=null){
			//通过文件名称去数据库中查询对应的输入流
			InputStream in=repositoryService.getResourceAsStream(deploymentId, imageName);
			//把流写入本地文件中
			File file=new File("D:/XXX.png");
			FileUtils.copyInputStreamToFile(in, file);
		}
		
		
		
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值