jbpm4学习--一个订单审核流程

在上文的基础上,开始第一个流程的编写。

一、准备工作

1、先想一个订单的审批流程

     主管审核--金额判断---总经理审核

2、先浏览jbpm4自带的example,

    找到可以CCP(copy、cut、paste)的代码、配置文件

   总计有:decision、state、eventListner等

二、画流程图

 用jbpm4带的eclipse插件画

经过长时间地与这个插件作斗争,我终于屈服了,不在画图界面上输入汉字。

步骤如下:

1、先画一个流程,只输入英文。

2、切换到到source标签下,把英文改成中文。。。再保存。

3、千万别再切回design标签下并保存。

画出来的结果如下:order.jpdl.xml

<?xml version="1.0" encoding="UTF-8"?>

<process key="orderAudit" name="orderAudit" version="1" xmlns="http://jbpm.org/4.0/jpdl">
 <on event="start">
    <event-listener class="ex.order.LogListener">
      <field name="msg"><string value="有一个定单提请主管审核"/></field>
    </event-listener>
  </on>
  <start g="35,28,48,48" name="start1">
       <transition name="提请主管审核" to="superAudit" g="-83,-17"/>
  </start>
 
 
   <state g="104,25,92,52" name="superAudit">
     <on event="start">
     <event-listener class="ex.order.LogListener">
        <field name="msg"><string value="等候主管审核"/></field>
      </event-listener>
   </on>
    <on event="end">
      <event-listener class="ex.order.LogListener">
        <field name="msg"><string value="主管审核完毕"/></field>
      </event-listener>
    </on>
      <transition name="主管同意" to="moneyAutoAudit" g="-107,-17"/>
      <transition name="主管驳回" to="cancel1" g="-65,-17"/>
   </state>
  
   <decision g="273,27,48,48" name="moneyAutoAudit" >
    <on event="start">
     <event-listener class="ex.order.LogListener">
        <field name="msg"><string value="等候金额判断"/></field>
      </event-listener>
   </on>
    <on event="end">
      <event-listener class="ex.order.LogListener">
        <field name="msg"><string value="金额判断完毕"/></field>
      </event-listener>
    </on>
      <handler class="ex.order.OrderMoneyEvaluation"/>
      <transition name="needGMAduit" to="GMAduit" g="-65,-17"/>
      <transition name="通过" to="end" g="-41,-17"/>
   </decision>
  
   <state g="255,121,92,52" name="GMAduit">
        <on event="start">
     <event-listener class="ex.order.LogListener">
        <field name="msg"><string value="等候GM审核"/></field>
      </event-listener>
   </on>
    <on event="end">
      <event-listener class="ex.order.LogListener">
        <field name="msg"><string value="GM审核完毕"/></field>
      </event-listener>
    </on>
      <transition name="GM同意" to="end" g="428,149:-41,-17"/>
      <transition name="GM驳回" to="cancel1" g="-65,-17"/>
   </state>

   <end-cancel name="cancel1" g="130,122,48,48">
     <on event="start">
    <event-listener class="ex.order.LogListener">
      <field name="msg"><string value="有一个定单被驳回"/></field>
    </event-listener>
  </on>
   </end-cancel>
 
    <end g="404,27,80,40" name="end">
   <on event="start">
    <event-listener class="ex.order.LogListener">
      <field name="msg"><string value="有一个定单批准通过"/></field>
    </event-listener>
  </on>
  </end>
</process>

三、写java

1、在HelloJBPM的基础上改出一个OrderDemo,代码如下

package ex.order;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.jbpm.api.Configuration;
import org.jbpm.api.Execution;
import org.jbpm.api.ExecutionService;
import org.jbpm.api.HistoryService;
import org.jbpm.api.IdentityService;
import org.jbpm.api.ManagementService;
import org.jbpm.api.ProcessEngine;
import org.jbpm.api.ProcessInstance;
import org.jbpm.api.RepositoryService;
import org.jbpm.api.TaskService;

import ex.HelloJBPM4;
import ex.HibernateUtil;

public class OrderDemo {

 
 protected final Log log = LogFactory.getLog(getClass());
   protected static ProcessEngine processEngine = null;

   protected static RepositoryService repositoryService;
   protected static ExecutionService executionService;
   protected static ManagementService managementService;
   protected static TaskService taskService;
   protected static HistoryService historyService;
   protected static IdentityService identityService;
   protected String deploymentId;
   protected synchronized void initialize() {
      if (processEngine==null) {

        String jbpmTestCfgType = System.getProperty("jbpm.test.cfg.type");
        log.debug(jbpmTestCfgType);
        Configuration configuration = new Configuration(jbpmTestCfgType);

        String jbpmTestCfgResource = System.getProperty("jbpm.test.cfg.resource");
        log.debug("jbpmTestCfgResource-->"+jbpmTestCfgResource);
        if (jbpmTestCfgResource!=null) {
          configuration.setResource(jbpmTestCfgResource);
        }

        processEngine = configuration.buildProcessEngine();
       
        log.debug("using ProcessEngine "+System.identityHashCode(processEngine));

        repositoryService = processEngine.get(RepositoryService.class);
        executionService = processEngine.getExecutionService();
        historyService = processEngine.getHistoryService();
        managementService = processEngine.getManagementService();
        taskService = processEngine.getTaskService();
        identityService = processEngine.getIdentityService();
      }
   }
   private void doOrder(){
      }
   private void deploy(){
    deploymentId = repositoryService.createDeployment()
         .addResourceFromClasspath("ex/order/order.jpdl.xml")
         .deploy();
   }
  
   private void doJob(){
 //   doOrder();
 //   if(1>0)return;
    initialize();
    deploy();
    OrderUI ui=new OrderUI();
    String answer="";
    String orderno="003";
    Map<String, Object> variables = new HashMap<String, Object>();
    variables.put("orderno",orderno);
     
   
    ProcessInstance processInstance = executionService.startProcessInstanceByKey("orderAudit",variables);
    log.debug("after start");
      Execution executionSuperAudit = processInstance.findActiveExecutionIn("superAudit");
      String executionId = executionSuperAudit.getId();
      answer=ui.getAnswer();
      if("yes".equals(answer))
       answer="主管同意";
      else
       answer="主管驳回";
      processInstance=executionService.signalExecutionById(executionId,answer);    
      log.debug("after superAudit");
     
     
          Execution executionGMAudit = processInstance.findActiveExecutionIn("GMAduit");
      executionId = executionGMAudit.getId();
      answer=ui.getAnswer();
      if("yes".equals(answer))
       answer="GM同意";
      else
       answer="GM驳回";
      processInstance=executionService.signalExecutionById(executionId,answer);
     
      repositoryService.deleteDeploymentCascade(deploymentId);
   }
  public static void main(String arg[]){
   OrderDemo ex=new OrderDemo();
   ex.doJob();
  
  }

}

其实doOrder()那个方法没什么用。。。。可以去掉

四、其它一些java类

package ex.order;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jbpm.api.listener.EventListener;
import org.jbpm.api.listener.EventListenerExecution;


public class LogListener implements EventListener {
 
 protected final Log log = LogFactory.getLog(getClass());
  private static final long serialVersionUID = 1L;
 
  // value gets injected from process definition
  String msg;
 
  public void notify(EventListenerExecution execution) {
    List<String> logs = (List<String>) execution.getVariable("logs");
    if (logs==null) {
      logs = new ArrayList<String>();
      execution.setVariable("logs", logs);
    }
    log.debug(msg);
    logs.add(msg);
  
    execution.setVariable("logs", logs);
  }
}


package ex.order;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jbpm.api.jpdl.DecisionHandler;
import org.jbpm.api.model.OpenExecution;

public class OrderMoneyEvaluation implements DecisionHandler {
 protected final Log log = LogFactory.getLog(getClass());
 
  public String decide(OpenExecution execution) {
   log.debug("I was called");
  
   OrderDetail od =new OrderDetail();
   od.setQuantity(100f);
   od.setPrice(10.1f);
  
  log.debug("order no:-->"+ execution.getVariable("orderno"));
    if (od.getQuantity()*od.getPrice()>1000) {
      return "needGMAduit";
    }
    else
     return "通过";
  }
}

package ex.order;

public class OrderUI {
 public String getAnswer(){
  return "yes";
 }
}

五、执行后,可以在console里,看到如下结果:

15:54:36[DEBUG]LogListener.java->notify(26)  - 有一个定单提请主管审核
2009-7-19 15:54:36 org.hibernate.event.def.DefaultDeleteEventListener deleteTransientEntity
信息: handling transient entity in delete processing
15:54:36[DEBUG]LogListener.java->notify(26)  - 等候主管审核
2009-7-19 15:54:36 org.hibernate.event.def.DefaultDeleteEventListener deleteTransientEntity
信息: handling transient entity in delete processing
15:54:36[DEBUG]OrderDemo.java->doJob(115)  - after start
15:54:36[DEBUG]LogListener.java->notify(26)  - 主管审核完毕
15:54:36[DEBUG]LogListener.java->notify(26)  - 等候金额判断
15:54:36[DEBUG]OrderMoneyEvaluation.java->decide(13)  - I was called
15:54:36[DEBUG]OrderMoneyEvaluation.java->decide(19)  - order no:-->003
2009-7-19 15:54:36 org.hibernate.event.def.DefaultDeleteEventListener deleteTransientEntity
信息: handling transient entity in delete processing
2009-7-19 15:54:36 org.hibernate.event.def.DefaultDeleteEventListener deleteTransientEntity
信息: handling transient entity in delete processing
2009-7-19 15:54:36 org.hibernate.event.def.DefaultDeleteEventListener deleteTransientEntity
信息: handling transient entity in delete processing
15:54:36[DEBUG]LogListener.java->notify(26)  - 金额判断完毕
15:54:36[DEBUG]LogListener.java->notify(26)  - 等候GM审核
15:54:36[DEBUG]OrderDemo.java->doJob(124)  - after superAudit
15:54:36[DEBUG]LogListener.java->notify(26)  - GM审核完毕
15:54:36[DEBUG]LogListener.java->notify(26)  - 有一个定单批准通过

研究一下,就可以知道,如何执行一个jbpm的流程了。

OrderUI的表示需要与用户交互,取得同意/驳回。

五、下一步工作

如何组织机构建模?

如何 订单与流程结合。

组织机构模型与流程结合。

 

你可能感兴趣的:(apache,eclipse,UI,Hibernate,jbpm)