您的位置:首页 > 产品设计 > 产品经理

jbpm4.4 demo2

2015-09-01 09:46 666 查看
package cn.itcast.b_processdefinition;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Set;
import java.util.zip.ZipInputStream;

import org.jbpm.api.Configuration;
import org.jbpm.api.ProcessDefinition;
import org.jbpm.api.ProcessEngine;
import org.junit.Test;

/**
* 管理流程定义有关的操作
*
* <pre>
* 	部署(添加)
* 	删除
* 	查询(查询所有、自定义条件查询)
* 	获取部署添加的文件信息(查看流程图)
*  !!没有更新操作!策略为:重新部署 + 版本递增。
*
* </pre>
*
* @author tyg
*
*/
public class ProcessDefinitionTest {

private ProcessEngine processEngine = Configuration.getProcessEngine();

// 部署(添加)
@Test
public void testDeploy() throws Exception {
String deploymentId = processEngine.getRepositoryService()//
.createDeployment()// 创建一个部署对象
.addResourceFromClasspath("helloworld/helloworld.jpdl.xml")// 添加一个要部署的文件
.addResourceFromClasspath("helloworld/helloworld.png")// 添加一个要部署的文件
.deploy(); // 执行部署操作
System.out.println("部署成功:deploymentId = " + deploymentId);
}

// 部署(把多个文件打包为一个zip一起部署)
@Test
public void testDeploy_zip() throws Exception {
// 准备
ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream("d:/helloworld.zip"));

// 部署
String deploymentId = processEngine.getRepositoryService()//
.createDeployment()// 创建一个部署对象
.addResourcesFromZipInputStream(zipInputStream)// 指定zip流
.deploy(); // 执行部署操作
System.out.println("部署成功:deploymentId = " + deploymentId);
}

// 删除
@Test
public void testDelete() throws Exception {
// // 删除指定的部署对象,如果有关联的信息,就报错
// processEngine.getRepositoryService().deleteDeployment(deploymentId);
// // 删除指定的部署对象,如果有关联的信息,就一并删除
// processEngine.getRepositoryService().deleteDeploymentCascade(deploymentId);

String deploymentId = "40001";
processEngine.getRepositoryService().deleteDeploymentCascade(deploymentId);
}

// 查询流程定义(查询所有、自定义条件查询)
@Test
public void testFindAllProcessDefinition() throws Exception {
// 查询
List<ProcessDefinition> list = processEngine.getRepositoryService()//
.createProcessDefinitionQuery()//
// 过滤条件
// .processDefinitionId(processDefinitionId);
// .deploymentId(deploymentId)
// .processDefinitionKey(key)
// .processDefinitionName(name)
// 排序条件
// .orderAsc(property)
// .orderDesc(property)
// .orderAsc(ProcessDefinitionQuery.PROPERTY_VERSION)//
// 执行查询得到结果
// .page(firstResult, maxResults)
// .list()
// .count()
// .uniqueResult()
.list();

// 显示
for (ProcessDefinition pd : list) {
System.out.println("id=" + pd.getId()// 格式为:{key}-{version}
+ ", name=" + pd.getName()// .jpdl.xml中根元素的name属性的值。
+ ", key=" + pd.getKey()// .jpdl.xml中根元素的key属性的值。如果不写,默认为name属性的值。
+ ", version=" + pd.getVersion()// .jpdl.xml中根元素的version属性的值。如果不写,默认的效果为同名称的第一个version为1,以后的就递增。(一般不要写)
+ ", deploymentId=" + pd.getDeploymentId()); // 所属的部署对象的id
}
}

// 获取部署时添加的文件信息(查看流程图)
@Test
public void testGetProcessImage() throws Exception {
String deploymentId = "90001";
String resourceName = "helloworld/helloworld.png";

// 获取指定的部署对象中的所有的文件名称
//使用给定的id 返回所有的资源存储
Set<String> nameSet = processEngine.getRepositoryService().getResourceNames(deploymentId);
System.out.println("所有的文件:");
for (String name : nameSet) {
System.out.println("\t" + name);
}

// 获取指定的部署对象中的指定名称的文件内容
InputStream in = processEngine.getRepositoryService().getResourceAsStream(deploymentId, resourceName);

// 存到d:/中
FileOutputStream out = new FileOutputStream("d:/process.png");
byte[] buf = new byte[1024];
for (int len = -1; (len = in.read(buf)) != -1;) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
}


<?xml version="1.0" encoding="UTF-8"?>

<process name="hellokey" key="hellokey" xmlns="http://jbpm.org/4.4/jpdl">
<start g="115,21,48,48" name="start1">
<transition g="-71,-17" name="to 提交申请" to="提交申请"/>
</start>
<end g="115,353,48,48" name="end1"/>
<task assignee="员工" g="81,101,117,52" name="提交申请">
<transition g="-78,-15" name="to 部门经理[审批]" to="部门经理[审批]"/>
</task>
<task assignee="部门经理" g="81,185,117,52" name="部门经理[审批]">
<transition g="-95,-17" name="to 总经理[审批]" to="总经理[审批]"/>
</task>
<task assignee="总经理" g="81,269,117,52" name="总经理[审批]">
<transition g="-47,-17" name="to end1" to="end1"/>
</task>
</process>


下载demo
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: