activiti流程定义

ProcessDefinition(流程定义) : 用于描述流程规则相关的信息。

package com.liuzm.activiti.day1;

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

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
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;

// ProcessDefinition(流程定义) : 用于描述流程规则相关的信息。
public class ProcessDefinationTest {
	//获取核心对象
	private ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
	
	// 1.部署流程
	@Test
	public void deployProcess() throws Exception {
		//获取仓库服务对象
		RepositoryService repositoryService = processEngine.getRepositoryService();
		//通过服务创建一个发布配置对象
		DeploymentBuilder builder = repositoryService.createDeployment();
	    //做配置
		builder.name("请假流程")
			   .addClasspathResource("qingjia2.bpmn")
			   .addClasspathResource("qingjia2.png");
		//发布
		builder.deploy();
	}

	
	// 2.查看流程定义信息
	@Test
	public void queryProcessDefination() throws Exception {
		//获取仓库服务对象
		RepositoryService repositoryService = processEngine.getRepositoryService();
		//创建查询对象
		ProcessDefinitionQuery query = repositoryService.createProcessDefinitionQuery();
		//添加查询条件
		String processDefinitionKey = "myProcess";
	    query
	    	 .processDefinitionKey(processDefinitionKey )
	    	 .orderByProcessDefinitionVersion().desc();
	    //调用list()方法查询
	    List<ProcessDefinition> list = query.list();
	    //遍历查询结果
	    for (ProcessDefinition pd : list) {
			System.out.println("id:"+pd.getId()+",name:"+pd.getName()+",key:"+pd.getKey()+",version:"+pd.getVersion());
		}
	}
	
	// 3.删除部署
	@Test
	public void deleteProcess() throws Exception {
		//获取仓库服务对象
		RepositoryService repositoryService = processEngine.getRepositoryService();
		//删除指定的流程
		String deploymentId = "1";
		repositoryService.deleteDeployment(deploymentId,true);//级联删除,慎用
		//调用上边的查看流程信息方法,看是否删除成功
		this.queryProcessDefination();
	}
	
	// 4.查看规则流程图
	@Test
	public void viewImage() throws Exception {
		//获取仓库服务对象
		RepositoryService repositoryService = processEngine.getRepositoryService();
		//获取图片
		String processDefinitionId = "myProcess:2:304";
		InputStream inputStream = repositoryService.getProcessDiagram(processDefinitionId );
		//将二进制流的图片输出到磁盘
		FileUtils.copyInputStreamToFile(inputStream, new File("F:\\2.png"));
	}
}

 RepositoryService是Activiti的仓库服务类。所谓的仓库指流程定义文档的两个文件:bpmn文件和流程图片。

 

你可能感兴趣的:(Activiti)