jBPM開發入門指南(4)
7 jBPM 的客戶端開發
有了前面的 HelloWorld 后臺流程,我們就要開始客戶端程序了。正如前面提到的,本文不寫 JSP ,而改采用 JUnit 的形式,輸出則用 System.out.println 。舉一反三,知道在方法中輸入及用 println 輸出,在 JSP 和 SWING 等 GUI 界面還不是一樣嘛。
這個 JUnit 客戶端,我們就借用創建項目時自動生成的 SimpleProcessTest.java 了,改寫后如下:
package com.sample;
import junit.framework.TestCase;
import org.jbpm.JbpmConfiguration;
import org.jbpm.JbpmContext;
import org.jbpm.context.exe.ContextInstance;
import org.jbpm.graph.def.ProcessDefinition;
import org.jbpm.graph.exe.ProcessInstance;
public class SimpleProcessTest extends TestCase {
??? private JbpmConfiguration config = JbpmConfiguration.getInstance();
??? private JbpmContext ctx = config.createJbpmContext();
??? // helloworld 對應于 jbpm_processdefinition 表的 name 字段值,也即 processdefinition.xml 的 name
??? // 這個值得取比較耗時,實際項目里最好和“數據庫的 JDBC 連接”一樣,讓它共享,不要頻繁打開關閉。
??? private ProcessDefinition processDefinition = ctx.getGraphSession().findLatestProcessDefinition("helloworld");
??? public void testNewRequest() {
??????? long id = newRequest();
??????? System.out.println("id=" + id);
??????? checkNewRequest(id);
??????? confirmRequest(id);
??????? checkconfirmRequest(id);
??????? ctx.close();// 關閉 jbpm 容器
??? }
??? /**
??? ? * 創建一個請假單
??? ? *
??? ? * @return
??? ? */
??? private long newRequest() {
??????? // 創建一個新流程
??????? ProcessInstance pi = processDefinition.createProcessInstance();
??????? // 取得流程的數據環境
??????? ContextInstance ci = pi.getContextInstance();
??????? // 創建一張請假單
??????? ci.setVariable("name", "
??????? ci.setVariable("day", 2);
??????? assertEquals(null, ci.getVariable("note"));
??????? // 請假申請結束,轉到下一個流程結點
??????? pi.signal();
??????? return pi.getId();
??? }
??? /**
??? ? * 檢查請假單的數據
??? ? *
??? ? * @param id
??? ? */
??? private void checkNewRequest(long id) {
??????? // 從數據庫提取原流程
??????? ProcessInstance pi = ctx.loadProcessInstance(id);
??????? // 取得流程的數據環境
??????? ContextInstance ci = pi.getContextInstance();
??????? // 創建一張請假單
???????
assertEquals("
??????? assertEquals(Integer.valueOf(2), ci.getVariable("day"));
??????? assertEquals(" 我要請假 " , ci.getVariable("note"));
??????? // 當前是結點為 confirm
??????? assertEquals(pi.getRootToken().getNode().getName(), "confirm");
??????? // 流程還沒結束
??????? assertFalse(pi.hasEnded());
??? }
??? /**
???
?
*
審批
??? ? *
??? ? * @param id
??? ? */
??? private void confirmRequest(long id) {
??????? ProcessInstance pi = ctx.loadProcessInstance(id);
??????? ContextInstance ci = pi.getContextInstance();
??????? // 不通過
??????? ci.setVariable("note", " 不準請假,繼續加班 " );
??????? // 審批結束,到下一個流程結點
??????? pi.signal();
??? }
??? private void checkConfirmRequest(long id) {
??????? ProcessInstance pi = ctx.loadProcessInstance(id);
??????? ContextInstance ci = pi.getContextInstance();
??????? // ConfirmAction 類在 signal 后執行,所以覆蓋了經理的審批意見
??????? assertEquals(" 準假 " , ci.getVariable("note"));
??????? // 當前是結點為 end
??????? assertEquals(pi.getRootToken().getNode().getName(), "end");
??????? // 流程結束了
??????? assertTrue(pi.hasEnded());
??? }
}
posted on 2006-08-24 18:15 陳剛 閱讀(30971) 評論(37) 編輯 收藏 所屬分類: jBPM