jbpm流程定义基本操作 布署 查询 删除

package cn.itcast.init;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import java.util.zip.ZipInputStream;

import org.hibernate.cfg.Configuration;
import org.jbpm.api.ProcessDefinition;
import org.jbpm.api.ProcessDefinitionQuery;
import org.jbpm.api.ProcessEngine;
import org.junit.Test;

/**
 * 基本操作,布署,CRD
 * 
 * @author Administrator
 * 
 */
public class CreateSchema {

	ProcessEngine processEngine = org.jbpm.api.Configuration.getProcessEngine();

	@Test
	public void createSchema() {// 初始化表
		new Configuration().configure("jbpm.hibernate.cfg.xml").buildSessionFactory();
	}

	@Test
	public void definition() {// 流程定义

		processEngine.getRepositoryService()//
				.createDeployment()//
				.addResourceFromClasspath("test.jpdl.xml")//
				.addResourceFromClasspath("test.png")//
				.deploy();
	}

	@Test
	public void deploy() {// 流程定义,从zip文件中布署
		InputStream is = this.getClass().getClassLoader().getResourceAsStream("test.zip");
		ZipInputStream zis = new ZipInputStream(is);
		processEngine.getRepositoryService()//
				.createDeployment()//
				.addResourcesFromZipInputStream(zis)//
				.deploy();
	}

	@Test
	public void query01() {// 取出所有流程
		List<ProcessDefinition> list = processEngine.getRepositoryService()//
				.createProcessDefinitionQuery()//
				.orderAsc(ProcessDefinitionQuery.PROPERTY_ID)//
				.list();
		for (ProcessDefinition p : list) {
			System.out.print("   name=");
			System.out.print(p.getName());
			System.out.print("   key=");
			System.out.print(p.getKey());
			System.out.print("   version=");
			System.out.print(p.getVersion());
			System.out.print("   deploymentId=");
			System.out.print(p.getDeploymentId());

			System.out.println();

		}
	}

	@Test
	public void query02() {// 按版本排序,并取出最新版本
		List<ProcessDefinition> list = processEngine.getRepositoryService()//
				.createProcessDefinitionQuery()//
				.orderAsc(ProcessDefinitionQuery.PROPERTY_VERSION)//
				.list();

		Map<String, ProcessDefinition> all = new HashMap<String, ProcessDefinition>();

		for (ProcessDefinition p : list) {
			System.out.print("   version=");
			System.out.print(p.getVersion());
			System.out.print("   name=");
			System.out.print(p.getName());
			System.out.print("   key=");
			System.out.print(p.getKey());
			System.out.print("   deploymentId=");
			System.out.print(p.getDeploymentId());
			System.out.println();
			all.put(p.getKey(), p);
		}
		Set<Entry<String, ProcessDefinition>> entrySet = all.entrySet();
		for (Entry<String, ProcessDefinition> entry : entrySet) {
			System.out.println("   key=" + entry.getKey() + "   version=" + entry.getValue().getVersion());
		}
	}

	@Test
	public void query03() {// 按版本排序,并分页
		ProcessDefinitionQuery query = processEngine.getRepositoryService()//
				.createProcessDefinitionQuery()//
				.orderAsc(ProcessDefinitionQuery.PROPERTY_VERSION);//

		long count = query.count();
		List<ProcessDefinition> list = query.page(1, 10).list();
	}

	@Test
	public void deleteById() {
		processEngine.getRepositoryService().deleteDeployment("50001");// 单个删除,有关联信息会报错
	}

	@Test
	public void deleteByCascade() {
		processEngine.getRepositoryService().deleteDeploymentCascade("60001");// 级联删除有关联信息
	}

	@Test
	public void deleteByKey() {
		List<ProcessDefinition> list = processEngine.getRepositoryService()// 单个删除,关联key都会删除
				.createProcessDefinitionQuery()//
				.processDefinitionKey("dddt")//
				.list();
		for (ProcessDefinition p : list) {
			String id = p.getDeploymentId();
			processEngine.getRepositoryService()//
					.deleteDeploymentCascade(id);
		}
	}

	@Test
	public void getResourceNameByDeployment() {
		Set<String> resourceNames = processEngine.getRepositoryService().getResourceNames("100001");// 获取资源名称
		for (String s : resourceNames) {
			System.out.println(s);
		}

	}

	@Test
	public void getResourceByDeployment() {// 获取资源
		InputStream is = processEngine.getRepositoryService().getResourceAsStream("70001", "test.jpdl.xml");
		InputStreamReader isr = null;
		OutputStreamWriter osw = null;
		try {
			isr = new InputStreamReader(is);
			osw = new OutputStreamWriter(new FileOutputStream("c:/test.jpdl.xml"), "utf-8");
			char[] b = new char[1024];
			while (isr.read(b) != -1) {
				osw.write(b);
				osw.flush();
			}
		} catch (Exception e) {

			e.printStackTrace();
		} finally {
			try {
				osw.close();
				isr.close();
			} catch (IOException e) {

				e.printStackTrace();
			}
		}

	}

}

详细相关项目实例,可到我的资料列表去下载

http://download.csdn.net/detail/albert_lz/6744245

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值