小菜毛毛技術(shù)分享

          與大家共同成長(zhǎng)

            BlogJava :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
            164 Posts :: 141 Stories :: 94 Comments :: 0 Trackbacks

          jBPM開發(fā)入門指南(4)

          7   jBPM 的客戶端開發(fā)

          有了前面的 HelloWorld 后臺(tái)流程,我們就要開始客戶端程序了。正如前面提到的,本文不寫 JSP ,而改采用 JUnit 的形式,輸出則用 System.out.println 。舉一反三,知道在方法中輸入及用 println 輸出,在 JSP SWING GUI 界面還不是一樣嘛。

           

          這個(gè) JUnit 客戶端,我們就借用創(chuàng)建項(xiàng)目時(shí)自動(dòng)生成的 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 對(duì)應(yīng)于 jbpm_processdefinition 表的 name 字段值,也即 processdefinition.xml name

              // 這個(gè)值得取比較耗時(shí),實(shí)際項(xiàng)目里最好和“數(shù)據(jù)庫(kù)的 JDBC 連接”一樣,讓它共享,不要頻繁打開關(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", " 陳剛 www.chengang.com.cn" );

                  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(" 陳剛 www.chengang.com.cn" , ci.getVariable("name"));

                  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");

                  // 流程還沒結(jié)束

                  assertFalse(pi.hasEnded());

              }

           

              /**

                * 審批陳剛的請(qǐng)假申請(qǐng)

                *

                * @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 類在 signal 后執(zhí)行,所以覆蓋了經(jīng)理的審批意見

                  assertEquals(" 準(zhǔn)假 " , ci.getVariable("note"));

           

                  // 當(dāng)前是結(jié)點(diǎn)為 end

                  assertEquals(pi.getRootToken().getNode().getName(), "end");

                  // 流程結(jié)束了

                  assertTrue(pi.hasEnded());

              }

           

          }

           

          這個(gè)例子還是很簡(jiǎn)單,而且關(guān)鍵是缺少用戶、組、權(quán)限等工作流系統(tǒng)必須的東西,不過(guò)沒關(guān)系。下面的內(nèi)容我將完成一個(gè)更完整的實(shí)例

          http://97wen.com/

          http://77.haowyt.com/

          posted on 2009-05-05 15:14 小菜毛毛 閱讀(500) 評(píng)論(0)  編輯  收藏 所屬分類: JBPM
          主站蜘蛛池模板: 拜泉县| 当雄县| 循化| 安国市| 和平县| 莲花县| 汶川县| 德令哈市| 茌平县| 祁东县| 聂拉木县| 彭阳县| 平顺县| 金坛市| 泰安市| 清丰县| 蒙城县| 万荣县| 凤城市| 巩留县| 凤山县| 嘉禾县| 运城市| 册亨县| 明光市| 徐水县| 龙山县| 科技| 封丘县| 健康| 大余县| 景泰县| 安义县| 雷波县| 博湖县| 琼中| 九台市| 绥滨县| 郯城县| 枝江市| 织金县|