package com.jzsoft.jbpm;
import org.jbpm.JbpmConfiguration;
import org.jbpm.JbpmContext;
import org.jbpm.graph.def.ProcessDefinition;
import junit.framework.TestCase;
public class Jbpm_02_DeployProcessDefinition extends TestCase {
static JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance();
//将流程定义对象持久化到数据库
public void testDeployProcessDefinition(){
//读取流程定义文件,得到流程定义对象
ProcessDefinition processDefinition = ProcessDefinition.parseXmlResource("test01/processdefinition.xml");
//这个时候,我们可以从流程定义对象中得到它的名称(流程名称)
Persistence.setVariable("processName", processDefinition.getName());
//JbpmContext对象封装了hibernate session对象的功能,可以执行
//与数据库相关的操作
JbpmContext context = jbpmConfiguration.createJbpmContext();
try{
//持久化到数据库
context.deployProcessDefinition(processDefinition);
}catch(Exception e){
e.printStackTrace();
context.setRollbackOnly();
}finally{
context.close();
}
}
}
package com.jzsoft.jbpm;
import java.util.Random;
import junit.framework.TestCase;
import org.hibernate.Session;
import org.jbpm.JbpmConfiguration;
import org.jbpm.JbpmContext;
public class Jbpm_03_CreateDocument extends TestCase {
static JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance();
//创建公文对象
public void testCreateDocument(){
//JbpmContext对象封装了hibernate session对象的功能,可以执行
//与数据库相关的操作
JbpmContext context = jbpmConfiguration.createJbpmContext();
try{
Session session = context.getSession();
Document document = new Document();
document.setTitle("测试公文"+new Random().nextInt(9999));
session.save(document);
Persistence.setVariable("docId", document.getId());
Persistence.setVariable("docTitle", document.getTitle());
}catch(Exception e){
e.printStackTrace();
context.setRollbackOnly();
}finally{
context.close();
}
}
}
package com.jzsoft.jbpm;
import java.util.Random;
import org.hibernate.Session;
import org.jbpm.JbpmConfiguration;
import org.jbpm.JbpmContext;
import org.jbpm.graph.def.ProcessDefinition;
import org.jbpm.graph.exe.ProcessInstance;
import junit.framework.TestCase;
public class Jbpm_04_CreateProcessInstance extends TestCase {
static JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance();
//创建流程实例对象,同时把流程实例与公文互相绑定
public void testCreateProcessInstance(){
//JbpmContext对象封装了hibernate session对象的功能,可以执行
//与数据库相关的操作
JbpmContext context = jbpmConfiguration.createJbpmContext();
try{
//1、从数据库中加载流程定义对象
ProcessDefinition processDefinition = context.getGraphSession()
.findLatestProcessDefinition(
(String)Persistence.getVariable("processName")
);
//2、根据流程定义对象创建流程实例对象
ProcessInstance instance = new ProcessInstance(processDefinition);
//3、保存流程实例对象
context.save(instance);
//流程实例的ID
long processInstanceId = instance.getId();
//把公文ID放入流程实例变量,使得公文可以随着流程实例的流转而流转
instance.getContextInstance().setVariable("docId", Persistence.getVariable("docId"));
instance.getContextInstance().setVariable("documentTitle", Persistence.getVariable("docTitle"));
//绑定流程实例ID到公文中
Document document = (Document)context.getSession().load(Document.class, Persistence.getVariable("docId"));
document.setProcessInstanceId(processInstanceId);
}catch(Exception e){
e.printStackTrace();
context.setRollbackOnly();
}finally{
context.close();
}
}
}