正在学习jbpm4, 于是就写了个例子
这个例子的流程就用借款申请的流程, 因为学jbpm3的时候对这个例子比较熟悉.
流程图如下:
流程定义:
<?xml version="1.0" encoding="UTF-8"?>
<process name="loan" xmlns="http://jbpm.org/4.0/jpdl">
<start g="172,-1,48,48" name="start1">
<transition g="-27,-6" name="发起借款" to="填写借款申请" />
</start>
<task assignee="#{proposer}" g="260,63,109,56" name="填写借款申请">
<transition g="-28,-16" name="提交申请" to="部门经理审批" />
</task>
<task assignee="manager" g="113,141,114,52" name="部门经理审批">
<transition g="61,167:-36,62" name="部门经理驳回" to="cancel" />
<transition g="-51,-8" name="部门经理审批通过" to="金额判断" />
</task>
<decision expr="#{money>=5000?'>=5000元':'<5000元'}"
g="301,205,48,48" name="金额判断">
<transition g="-20,-2" name=">=5000元" to="总经理审批" />
<transition g="-18,-13" name="<5000元" to="财务拨款" />
</decision>
<end g="190,407,48,48" name="end" />
<end-cancel g="37,374,48,48" name="cancel" />
<task assignee="ceo" g="150,286,100,52" name="总经理审批">
<transition g="-41,-6" name="总经理审批通过" to="财务拨款" />
<transition g="-17,-12" name="总经理驳回" to="cancel" />
</task>
<task assignee="finance" g="345,325,92,52" name="财务拨款">
<transition g="-26,-6" name="完成拨款" to="end" />
</task>
</process>
测试代码:
/**
* CopyRight (C) 2006-2009 yeyong
* @author yy
* @project JbpmExamples
* @version 1.0
* @mail yy629_86 at 163 dot com
* @date 2009-9-9 下午3:12:13
* @description
*/
package loan;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jbpm.api.ProcessInstance;
import org.jbpm.api.history.HistoryProcessInstance;
import org.jbpm.api.task.Task;
import org.jbpm.test.JbpmTestCase;
/**
* @author yeyong
*/
public class LoanTest extends JbpmTestCase {
String deploymentId;
@Override
protected void setUp() throws Exception {
super.setUp();
deploymentId = repositoryService.createDeployment().addResourceFromClasspath("loan/loan.jpdl.xml")
.deploy();
}
@Override
protected void tearDown() throws Exception {
repositoryService.deleteDeploymentCascade(deploymentId);
super.tearDown();
}
/**
* 测试用例
* <p>
* 张三 申请4000元, 部门经理驳回
*/
public void test4000ButManagerDisapprove() {
String curUser = "Zhang San";
ProcessInstance pi = startLoanProcess(curUser);
String piId = pi.getId();
filloutApplication(piId, curUser, 4000);
curUser = "manager";
managerApprove(piId, curUser, false, "最近资金紧张,暂不同意."); // 部门经理不同意
pi = executionService.findProcessInstanceById(piId);
assertNull(pi); // 流程结束
HistoryProcessInstance hpi = getHistoryProcessInstanceByProcessInstanceId(piId);
assertEquals(hpi.getState(), "cancel"); // 流程结束状态是cancel , 取消
}
/**
* 测试用例
* <p>
* 李四 申请4000元,部门经理同意
*/
public void test4000AndManagerApprove() {
String curUser = "Li si";
ProcessInstance pi = startLoanProcess(curUser);
String piId = pi.getId();
filloutApplication(piId, curUser, 4000);
curUser = "manager";
managerApprove(piId, curUser, true, "同意."); // 部门经理同意
pi = executionService.findProcessInstanceById(piId);
assertNotNull(pi); // 流程未结束
assertNotNull(pi.findActiveExecutionIn("财务拨款")); // 财务拨款处于活动状态
curUser = "finance";
earmark(piId, curUser, piId); // 拨款
pi = executionService.findProcessInstanceById(piId);
assertNull(pi); //流程结束
HistoryProcessInstance hpi = getHistoryProcessInstanceByProcessInstanceId(piId);
assertEquals(hpi.getState(), "ended"); // 流程结束状态是ended, 正常结束
}
/**
* 测试用例
* <p>
* 王五 申请6000元,部门经理驳回
*/
public void test6000ButManagerDisapprove() {
String curUser = "Wang Wu";
ProcessInstance pi = startLoanProcess(curUser);
String piId = pi.getId();
filloutApplication(piId, curUser, 6000);
curUser = "manager";
managerApprove(piId, curUser, false, "最近资金紧张,暂不同意.");
pi = executionService.findProcessInstanceById(piId);
assertNull(pi);
HistoryProcessInstance hpi = getHistoryProcessInstanceByProcessInstanceId(piId);
assertEquals(hpi.getState(), "cancel");
}
/**
* 测试用例
* <p>
* 赵六 申请6000元,部门经理同意,总经理驳回
*/
public void test6000AndManagerApproveButCeoDisapprove() {
String curUser = "Zhao Liu";
ProcessInstance pi = startLoanProcess(curUser);
String piId = pi.getId();
filloutApplication(piId, curUser, 6000);
curUser = "manager";
managerApprove(piId, curUser, true, "同意. manager");
pi = executionService.findProcessInstanceById(piId);
assertNotNull(pi);
assertNotNull(pi.findActiveExecutionIn("总经理审批"));
curUser = "ceo";
ceoApprove(piId, curUser, false, "不同意.等资金周转过来再说吧");
pi = executionService.findProcessInstanceById(piId);
assertNull(pi);
HistoryProcessInstance hpi = getHistoryProcessInstanceByProcessInstanceId(piId);
assertEquals(hpi.getState(), "cancel");
}
/**
* 测试用例
* <p>
* 孙七 申请6000元,部门经理同意,总经理也同意
*/
public void test6000AndManagerApproveAdnCeoApprove() {
String curUser = "Sun Qi";
ProcessInstance pi = startLoanProcess(curUser);
String piId = pi.getId();
filloutApplication(piId, curUser, 6000);
curUser = "manager";
managerApprove(piId, curUser, true, "同意. manager");
pi = executionService.findProcessInstanceById(piId);
assertNotNull(pi);
assertNotNull(pi.findActiveExecutionIn("总经理审批"));
curUser = "ceo";
ceoApprove(piId, curUser, true, "同意. ceo");
pi = executionService.findProcessInstanceById(piId);
assertNotNull(pi);
assertNotNull(pi.findActiveExecutionIn("财务拨款"));
curUser = "finance";
earmark(piId, curUser, piId);
pi = executionService.findProcessInstanceById(piId);
assertNull(pi);
HistoryProcessInstance hpi =
getHistoryProcessInstanceByProcessInstanceId(piId);
assertEquals(hpi.getState(), "ended");
}
/**
* 用户发起借款申请
*
* @param userId
* @return
*/
protected ProcessInstance startLoanProcess(String user) {
Map<String, Object> variables = new HashMap<String, Object>();
variables.put("proposer", user);
return executionService.startProcessInstanceByKey("loan", variables);
}
/**
* 填写借款申请
*
* @param piId
* @param user
* @param money
*/
protected void filloutApplication(String piId, String user, double money) {
List<Task> tasks = taskService.findPersonalTasks(user);
Task filloutTask = null;
for (Task task : tasks) {
if (task.getExecutionId().equals(piId)) {
filloutTask = task;
break;
}
}
assertNotNull(filloutTask);
Map<String, Object> variables = new HashMap<String, Object>();
variables.put("money", money);
executionService.setVariables(piId, variables);
taskService.completeTask(filloutTask.getId());
}
/**
* 经理审批
*
* @param piId
* @param user
* @param isPass
* @param comment
*/
protected void managerApprove(String piId, String user, boolean isPass, String comment) {
List<Task> tasks = taskService.findPersonalTasks(user);
Task approveTask = null;
for (Task task : tasks) {
if (task.getExecutionId().equals(piId)) {
approveTask = task;
break;
}
}
assertNotNull(approveTask);
taskService.addTaskComment(approveTask.getId(), comment);
taskService.completeTask(approveTask.getId(),
isPass ? "部门经理审批通过" : "部门经理驳回");
}
/**
* 总经理审批
*
* @param piId
* @param user
* @param isPass
* @param comment
*/
protected void ceoApprove(String piId, String user, boolean isPass, String comment) {
List<Task> tasks = taskService.findPersonalTasks(user);
Task approveTask = null;
for (Task task : tasks) {
if (task.getExecutionId().equals(piId)) {
approveTask = task;
break;
}
}
assertNotNull(approveTask);
taskService.addTaskComment(approveTask.getId(), comment);
taskService.completeTask(approveTask.getId(),
isPass ? "总经理审批通过" : "总经理驳回");
}
/**
* 拨款
*
* @param piId
* @param user
* @param isPass
* @param comment
*/
/**
* @param piId
* @param user
* @param comment
*/
protected void earmark(String piId, String user, String comment) {
List<Task> tasks = taskService.findPersonalTasks(user);
Task earmarkask = null;
for (Task task : tasks) {
if (task.getExecutionId().equals(piId)) {
earmarkask = task;
break;
}
}
assertNotNull(earmarkask);
taskService.addTaskComment(earmarkask.getId(), comment);
// HistoryProcessInstance hpi =
getHistoryProcessInstanceByProcessInstanceId(piId,"填写借款申请");
String proposer =(String) executionService.getVariable(piId,"proposer");
taskService.completeTask(earmarkask.getId());
// 通知借款申请人
System.err.println(">> to " + proposer +", 财务科已经给你拨款.");
}
public HistoryProcessInstance
getHistoryProcessInstanceByProcessInstanceId(String piId) {
return historyService.createHistoryProcessInstanceQuery().
processInstanceId(piId).uniqueResult();
}