jbpm例子
2009-12-16
jbpm练习小例子
文章分类:Java编程 关键字: jbpm小例子
在此非常感谢的人:linliangyi2007:http://linliangyi2007.iteye.com/blog/176345 主要是参考的是他的内容;
还有就是:http://www.blogjava.net/chengang/category/12967.html。以及网上其他的孜孜不倦贡献的人们。
这次模仿写的是:请假的流程:
<?xml version="1.0" encoding="UTF-8"?>
<process-definition xmlns="urn:jbpm.org:jpdl-3.1" name="testone">
<swimlane name="bossswimlane" >
<assignment to="填写病假申请单">
</transition>
</start-state>
<task-node name="填写病假申请单">
<task name="填写申请内容">
<controller>
<variable name="days"></variable>
</controller>
<assignment name="病假申请填写完成"></transition>
</task-node>
<decision name="天数判定">
<transition to="经理审批" name="天数小于5天">
<condition expression='#{days lt 5}'></condition>
</transition>
<transition to="总经理审批" name="天数大于或等于5天">
<condition expression='#{days ge 5}'></condition>
</transition>
</decision>
<task-node name="经理审批">
<task name="经理开始审批">
<controller>
<variable name="fork"></variable>
</controller>
<assignment actor-id="manager" ></assignment>
</task>
<transition to="总经理审批" name="经理审批通过"></transition>
<transition to="结束" name="经理审批未通过"></transition>
</task-node>
<task-node name="总经理审批">
<task name="总经理开始审批" swimlane="bossswimlane"></task>
<transition to="发通知" name="总经理审批通过"></transition>
<transition to="结束" name="总经理审批未通过"></transition>
</task-node>
<node name="发通知">
<action 员工 " + user + " 请假 " + days + " 天, 已批准!!!");
/*
* 这句别漏了,否则流程不会向下走哒
*/
executionContext.leaveNode();
}
}
当这些都定义好之后。就是要发布这个流程到数据库中了。
DesployTest1 类就是把流程发布到数据库中去,当然首先要一点就是你先要把jbpm的类映射到数据库中去。
public class DesployTest1 {
/**
* @param args
*/
public static void main(String[] args) {
JbpmConfiguration config = JbpmConfiguration.getInstance();
// 创建一个 jbpm 容器
JbpmContext jbpmContext = config.createJbpmContext();
// 由 processdefinition.xml 生成相对应的流程定义类 ProcessDefinition
InputStream is = null;
try {
is = new FileInputStream("src/main/jpdl/firstflow1/processdefinition.xml");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
ProcessDefinition processDefinition = ProcessDefinition.parseXmlInputStream(is);
// 利用容器的方法将流程定义数据部署到数据库上
jbpmContext.deployProcessDefinition(processDefinition);
// 关闭 jbpmContext
jbpmContext.close();
System.out.println("OK");
}
}
现在就是测试类了:
MyFlowProcessDBTest 就是测试的类
public class MyFlowProcessDBTest {
/*
* 初始化jBPM配置
* 包含对Hibernate的数据库初始化
*/
static JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance();
public static void main(String[] args){
MyFlowProcessDBTest test = new MyFlowProcessDBTest();
test.testApplication();
// test.test6000YuanApplication();
// test.test7000YuanApplication();
}
public void testApplication(){
ProcessInstance pi = createProcessInstance("lecord");
submitApplication("lecord" , 6);
approveByManager(true);
approveByBoss(true);
checkTasks(pi);
}
/**
* 生成流程实例
*/
protected ProcessInstance createProcessInstance(String user){
System.out.println("==请假流程开始==");
JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
try {
GraphSession graphSession = jbpmContext.getGraphSession();
/*
* 从数据库获取指定的流程定义
*/
ProcessDefinition pdf = graphSession.findLatestProcessDefinition("testone");
//生成流程实例
ProcessInstance pi = pdf.createProcessInstance();
//设置流程发起人
pi.getContextInstance().createVariable("actor", user);
//触发流程转向
pi.signal();
/*
* 保存流程实例
*/
jbpmContext.save(pi);
return pi;
}finally{
jbpmContext.close();
}
}
/**
* 填写提交申请单
* @param money
*/
@SuppressWarnings("unchecked")
protected void submitApplication(String actorId , int days){
System.out.println("==填写请假内容==");
JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
try {
/*
*根据操作者ID,获取属于该操作者的任务集
*/
List<TaskInstance> taskInstances = jbpmContext.getTaskList(actorId);
for(TaskInstance ti : taskInstances){
System.out.println(ti.getCreate());
System.out.println("ti.name = " + ti.getName());
System.out.println("ti.actor = " + ti.getActorId());
ContextInstance ci = ti.getContextInstance();
ci.setVariable("days",new Integer(days));
ti.end();
}
}finally{
jbpmContext.close();
}
}
/**
* 部门经理审批
* @param pass
*/
@SuppressWarnings("unchecked")
protected void approveByManager(boolean pass){
System.out.println("==经理审批==");
JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
try{
List<TaskInstance> taskInstances = jbpmContext.getTaskList("manager");
System.out.println(taskInstances.size()+"-------经理该审批的taskInstances---------");
for(TaskInstance ti : taskInstances){
System.out.println("ti.name = " + ti.getName());
System.out.println("ti.actor = " + ti.getActorId());
if(pass){
ti.end("经理审批通过");
}else{
ti.end("经理审批未通过");
}
}
}finally{
jbpmContext.close();
}
}
/**
* 总经理审批
* @param pass
*/
@SuppressWarnings("unchecked")
protected void approveByBoss(boolean pass){
System.out.println("==总经理审批==");
JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
try{
List<TaskInstance> taskInstances = jbpmContext.getTaskList("boss");
for(TaskInstance ti : taskInstances){
System.out.println("ti.name = " + ti.getName());
System.out.println("ti.actor = " + ti.getActorId());
if(pass){
ti.end("总经理审批通过");
}else{
ti.end("总经理审批未通过");
}
}
}finally{
jbpmContext.close();
}
}
}
这个只是模仿的做的。还有很多的不足。引用了别人的成功希望别介意哦。。