jBPM開(kāi)發(fā)入門(mén)指南(4)
7 jBPM 的客戶(hù)端開(kāi)發(fā)
有了前面的 HelloWorld 后臺(tái)流程,我們就要開(kāi)始客戶(hù)端程序了。正如前面提到的,本文不寫(xiě) JSP ,而改采用 JUnit 的形式,輸出則用 System.out.println 。舉一反三,知道在方法中輸入及用 println 輸出,在 JSP 和 SWING 等 GUI 界面還不是一樣嘛。
這個(gè) JUnit 客戶(hù)端,我們就借用創(chuàng)建項(xiàng)目時(shí)自動(dòng)生成的 SimpleProcessTest.java 了,改寫(xiě)后如下:
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 對(duì)應(yīng)于 jbpm_processdefinition 表的 name 字段值,也即 processdefinition.xml 的 name
??? // 這個(gè)值得取比較耗時(shí),實(shí)際項(xiàng)目里最好和“數(shù)據(jù)庫(kù)的 JDBC 連接”一樣,讓它共享,不要頻繁打開(kāi)關(guān)閉。
??? 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();// 關(guān)閉 jbpm 容器
??? }
??? /**
??? ? * 創(chuàng)建一個(gè)請(qǐng)假單
??? ? *
??? ? * @return
??? ? */
??? private long newRequest() {
??????? // 創(chuàng)建一個(gè)新流程
??????? ProcessInstance pi = processDefinition.createProcessInstance();
??????? // 取得流程的數(shù)據(jù)環(huán)境
??????? ContextInstance ci = pi.getContextInstance();
??????? // 創(chuàng)建一張請(qǐng)假單
??????? ci.setVariable("name", "
??????? ci.setVariable("day", 2);
??????? assertEquals(null, ci.getVariable("note"));
??????? // 請(qǐng)假申請(qǐng)結(jié)束,轉(zhuǎn)到下一個(gè)流程結(jié)點(diǎn)
??????? pi.signal();
??????? return pi.getId();
??? }
??? /**
??? ? * 檢查請(qǐng)假單的數(shù)據(jù)
??? ? *
??? ? * @param id
??? ? */
??? private void checkNewRequest(long id) {
??????? // 從數(shù)據(jù)庫(kù)提取原流程
??????? ProcessInstance pi = ctx.loadProcessInstance(id);
??????? // 取得流程的數(shù)據(jù)環(huán)境
??????? ContextInstance ci = pi.getContextInstance();
??????? // 創(chuàng)建一張請(qǐng)假單
???????
assertEquals("
??????? assertEquals(Integer.valueOf(2), ci.getVariable("day"));
??????? assertEquals(" 我要請(qǐng)假 " , ci.getVariable("note"));
??????? // 當(dāng)前是結(jié)點(diǎn)為 confirm
??????? assertEquals(pi.getRootToken().getNode().getName(), "confirm");
??????? // 流程還沒(méi)結(jié)束
??????? assertFalse(pi.hasEnded());
??? }
??? /**
???
?
*
審批
??? ? *
??? ? * @param id
??? ? */
??? private void confirmRequest(long id) {
??????? ProcessInstance pi = ctx.loadProcessInstance(id);
??????? ContextInstance ci = pi.getContextInstance();
??????? // 不通過(guò)
??????? ci.setVariable("note", " 不準(zhǔn)請(qǐng)假,繼續(xù)加班 " );
??????? // 審批結(jié)束,到下一個(gè)流程結(jié)點(diǎn)
??????? pi.signal();
??? }
??? private void checkConfirmRequest(long id) {
??????? ProcessInstance pi = ctx.loadProcessInstance(id);
??????? ContextInstance ci = pi.getContextInstance();
??????? // ConfirmAction 類(lèi)在 signal 后執(zhí)行,所以覆蓋了經(jīng)理的審批意見(jiàn)
??????? assertEquals(" 準(zhǔn)假 " , ci.getVariable("note"));
??????? // 當(dāng)前是結(jié)點(diǎn)為 end
??????? assertEquals(pi.getRootToken().getNode().getName(), "end");
??????? // 流程結(jié)束了
??????? assertTrue(pi.hasEnded());
??? }
}
posted on 2006-08-24 18:15 陳剛 閱讀(30967) 評(píng)論(37) 編輯 收藏 所屬分類(lèi): jBPM