JBPM4整理001

1.新建一个动态的web项目

2.在lib目录下加入以下jar包(由于用到fileupload的上传,所以还要加入fileupload的依赖库)
引用

*JBPM4_HOME/jbpm.jar
*JBPM4_HOME/lib/*.jar
*jakarta-taglibs-standard-1.1.2_HOME/lib/jstl.jar
*jakarta-taglibs-standard-1.1.2_HOME/lib/standard.jar
*commons-fileupload-1.2.1_HOME/lib/commons-fileupload-1.2.1.jar


3.绘制流程图(在resources目录下建立一个leave.jpdl.xml文件)
JBPM4整理001
Activity(活动):

   1. start活动的意义在于指明一个流程的实例应该从哪里开始发起,即流程的入口。start活动必须有一个流出转移(transition);
   2. 流程实例流到State状态活动时,就陷入了一个中断、等待状态,必须得收到外部的干预操作后才能继续运行;
   3. decision活动可以拥有多个流出转移,当流程实例到达decision活动时,会根据最先匹配成功的一个条件自动地通过相应的流出转移。

      三种方式实现decision活动流向:
          * 使用decision活动的condition元素。
          * 使用decision活动的expr属性。
          * 使用decision活动的handler元素。
   4. fork-join:分支/聚合活动
   5. end:结束活动
   6. task:人工任务活动
   7. sub-process:子流程活动
   8. custom:自定义活动




Java代码
  1. <?xml version= "1.0"  encoding= "UTF-8" ?>  
  2.   
  3. <process name="leave"  xmlns= "http://jbpm.org/4.0/jpdl" >  
  4.    <start g="201,14,48,48"  name= "开始" >  
  5.       <transition g="-42,-10"  name= " 请假"  to= "填写请假单" />  
  6.    </start>  
  7.    <task assignee="Kayzhan"  g= "178,87,92,52"  name= "填写请假单" >  
  8.       <transition g="-97,2"  name= "判 断是不是经理"  to= "是不是经理" />  
  9.    </task>  
  10.    <decision expr="#{manager}"  g= "204,158,48,48"  name= "是不是经理" >  
  11.       <transition g="-23,-11"  name= " 否"  to= "经理审核" />  
  12.       <transition g="14,-11"  name= " 是"  to= "老板审批" />  
  13.    </decision>  
  14.    <task assignee="Lingo"  g= "103,252,92,52"  name= "经理审核" >  
  15.       <transition g="150,450:10,-21"  name= "经理批准"  to= "结束" />  
  16.       <transition g="-22,-22"  name= " 请假天数>3"  to= "老板审批" />  
  17.       <transition g="-61,-1"  name= " 经理不批准"  to= "终止" />  
  18.       <transition g="149,114:-55,82"  name= "经理驳回"  to= "填写请假 单" />  
  19.    </task>  
  20.    <task assignee="ForgetDavi"  g= "278,251,92,52"  name= "老板审批" >  
  21.       <transition g="326,450:-58,-24"  name= "老板批准"  to= "结束" />  
  22.       <transition g="7,0"  name= "老板不 批准"  to= "终止" />  
  23.       <transition g="323,114:13,61"  name= "老板驳回"  to= "填写请假 单" />  
  24.    </task>  
  25.    <end g="219,429,48,48"  name= "结束"  state= "confirm" />  
  26.    <end g="220,360,48,48"  name= "终止"  state= "dissent" />  
  27. </process>  
<?xml version="1.0" encoding="UTF-8"?>

<process name="leave" xmlns="http://jbpm.org/4.0/jpdl">
   <start g="201,14,48,48" name="开始">
      <transition g="-42,-10" name="请假" to="填写请假单"/>
   </start>
   <task assignee="Kayzhan" g="178,87,92,52" name="填写请假单">
      <transition g="-97,2" name="判断是不是经理" to="是不是经理"/>
   </task>
   <decision expr="#{manager}" g="204,158,48,48" name="是不是经理">
      <transition g="-23,-11" name="否" to="经理审核"/>
      <transition g="14,-11" name="是" to="老板审批"/>
   </decision>
   <task assignee="Lingo" g="103,252,92,52" name="经理审核">
      <transition g="150,450:10,-21" name="经理批准" to="结束"/>
      <transition g="-22,-22" name="请假天数>3" to="老板审批"/>
      <transition g="-61,-1" name="经理不批准" to="终止"/>
      <transition g="149,114:-55,82" name="经理驳回" to="填写请假单"/>
   </task>
   <task assignee="ForgetDavi" g="278,251,92,52" name="老板审批">
      <transition g="326,450:-58,-24" name="老板批准" to="结束"/>
      <transition g="7,0" name="老板不批准" to="终止"/>
      <transition g="323,114:13,61" name="老板驳回" to="填写请假单"/>
   </task>
   <end g="219,429,48,48" name="结束" state="confirm"/>
   <end g="220,360,48,48" name="终止" state="dissent"/>
</process>


4.编写流程发布及控制的类
Java代码
  1. package  zzg;  
  2.   
  3. import  java.io.IOException;  
  4. import  java.lang.reflect.Method;  
  5. import  java.util.ArrayList;  
  6. import  java.util.HashMap;  
  7. import  java.util.Iterator;  
  8. import  java.util.LinkedHashMap;  
  9. import  java.util.List;  
  10. import  java.util.Map;  
  11. import  java.util.Set;  
  12. import  java.util.zip.ZipInputStream;  
  13.   
  14. import  javax.servlet.ServletConfig;  
  15. import  javax.servlet.ServletException;  
  16. import  javax.servlet.http.HttpServlet;  
  17. import  javax.servlet.http.HttpServletRequest;  
  18. import  javax.servlet.http.HttpServletResponse;  
  19.   
  20. import  org.apache.commons.fileupload.DiskFileUpload;  
  21. import  org.apache.commons.fileupload.FileItem;  
  22. import  org.jbpm.api.Configuration;  
  23. import  org.jbpm.api.Execution;  
  24. import  org.jbpm.api.ExecutionService;  
  25. import  org.jbpm.api.ProcessDefinition;  
  26. import  org.jbpm.api.ProcessDefinitionQuery;  
  27. import  org.jbpm.api.ProcessEngine;  
  28. import  org.jbpm.api.ProcessInstance;  
  29. import  org.jbpm.api.RepositoryService;  
  30. import  org.jbpm.api.TaskService;  
  31. import  org.jbpm.api.task.Task;  
  32.   
  33. /**  
  34.  * Servlet implementation class LeaveServlet  
  35.  */   
  36. public   class  LeaveServlet  extends  HttpServlet {  
  37.     private   static   final   long  serialVersionUID = 1L;  
  38.     private  ProcessEngine processEngine =  null ;  
  39.     private  RepositoryService repositoryService =  null ;  
  40.     private  ExecutionService executionService =  null ;  
  41.     private  ProcessInstance processInstance =  null ;  
  42.     private  TaskService taskService =  null ;  
  43.     private  Task task =  null ;  
  44.     private  Execution execution =  null ;  
  45.     private  String url =  null ;  
  46.     private  List list =  null ;  
  47.     private  List<Task> taskList =  null ;  
  48.   
  49.     /* 初始化ProcessEngine. */   
  50.     public   void  init(ServletConfig config)  throws  ServletException {  
  51.         super .init(config);  
  52.         // 获取流程引擎  
  53.        // sdfsdfsdf
     
  54.         processEngine = new  Configuration().buildProcessEngine();  
  55.         repositoryService = processEngine.getRepositoryService();  
  56.         executionService = processEngine.getExecutionService();  
  57.         taskService = processEngine.getTaskService();  
  58.     }  
  59.   
  60.     protected   void  doGet(HttpServletRequest request,  
  61.             HttpServletResponse response) throws  ServletException, IOException {  
  62.         process(request, response);  
  63.     }  
  64.   
  65.     protected   void  doPost(HttpServletRequest request,  
  66.             HttpServletResponse response) throws  ServletException, IOException {  
  67.         process(request, response);  
  68.     }  
  69.   
  70.     protected   void  process(HttpServletRequest request,  
  71.             HttpServletResponse response) throws  ServletException, IOException {  
  72.         String zzg = request.getParameter("zzg" );  
  73.   
  74.         if  (zzg.equals( "deploy" )) {  
  75.             try  {  
  76.                 deploy(request, response);  
  77.             } catch  (Exception e) {  
  78.                 e.printStackTrace();  
  79.             }  
  80.             list = getLatestProcessDefinition(request, response);  
  81.             request.setAttribute("process" , list);  
  82.             request.getRequestDispatcher("/index.jsp" ).forward(request,  
  83.                     response);  
  84.         } else   if  (zzg.equals( "leave" )) {  
  85.             url = leave(request, response);  
  86.             request.getRequestDispatcher(url).forward(request, response);  
  87.         } else   if  (zzg.equals( "query" )) {  
  88.             list = getProcessInstanceById(request, response);  
  89.             request.setAttribute("pi" , list);  
  90.             request.getRequestDispatcher("/index.jsp" ).forward(request,  
  91.                     response);  
  92.         } else   if  (zzg.equals( "apply" )) {  
  93.             apply(request, response);  
  94.             list = getProcessInstanceById(request, response);  
  95.             request.setAttribute("pi" , list);  
  96.             request.getRequestDispatcher("/index.jsp" ).forward(request,  
  97.                     response);  
  98.         } else   if  (zzg.equals( "getTask" )) {  
  99.             list = getTask(request, response);  
  100.             request.setAttribute("taskList" , list);  
  101.             request.getRequestDispatcher("/check.jsp" ).forward(request,  
  102.                     response);  
  103.         } else   if  (zzg.equals( "view" )) {  
  104.             Map<String, Object> map = view(request, response);  
  105.             request.setAttribute("map" , map);  
  106.             request.getRequestDispatcher("/view.jsp" )  
  107.                     .forward(request, response);  
  108.         } else   if  (zzg.equals( "confirm" )) {  
  109.             confirm(request, response);  
  110.             list = getTask(request, response);  
  111.             request.setAttribute("taskList" , list);  
  112.             request.getRequestDispatcher("/check.jsp" ).forward(request,  
  113.                     response);  
  114.         } else   if  (zzg.equals( "dissent" )) {  
  115.             dissent(request, response);  
  116.             list = getTask(request, response);  
  117.             request.setAttribute("taskList" , list);  
  118.             request.getRequestDispatcher("/check.jsp" ).forward(request,  
  119.                     response);  
  120.         }  
  121.   
  122.         /*  
  123.          * try { Class clz = this.getClass(); Method method =  
  124.          * clz.getDeclaredMethod(action, HttpServletRequest.class,  
  125.          * HttpServletResponse.class); method.invoke(this, request, response); }  
  126.          * catch (Exception ex) { ex.printStackTrace(); }  
  127.          */   
  128.     }  
  129.   
  130.     protected   void  deploy(HttpServletRequest request,  
  131.             HttpServletResponse response) throws  Exception, IOException {  
  132.         String temp = getServletContext().getRealPath("/temp" );  
  133.         String upload = getServletContext().getRealPath("/upload" );  
  134.         DiskFileUpload diskFileUpload = new  DiskFileUpload();  
  135.         diskFileUpload.setFileSizeMax(1  *  1024  *  1024 );  
  136.         diskFileUpload.setSizeThreshold(4096 );  
  137.         diskFileUpload.setRepositoryPath(temp);  
  138.         List fileItems = diskFileUpload.parseRequest(request);  
  139.         Iterator iter = fileItems.iterator();  
  140.         if  (iter.hasNext()) {  
  141.             FileItem item = (FileItem) iter.next();  
  142.             if  (!item.isFormField()) {  
  143.                 String name = item.getName();  
  144.                 long  size = item.getSize();  
  145.                 System.out.println("name: "  + name +  "&size:"  + size);  
  146.                 if  (name !=  null  && !name.equals( "" ) && size >  0 ) {  
  147.                     // Deploying a process部署流程   
  148.                     repositoryService.createDeployment()  
  149.                             .addResourcesFromZipInputStream(  
  150.                                     new  ZipInputStream(item.getInputStream()))  
  151.                             .deploy();  
  152.                 }  
  153.             }  
  154.         }  
  155.     }  
  156.   
  157.     protected  String leave(HttpServletRequest request,  
  158.             HttpServletResponse response) throws  ServletException, IOException {  
  159.         // 根据流程定义的KEY值启动一个新的流程实例   
  160.         processInstance = executionService.startProcessInstanceByKey("leave" );  
  161.         //判断流程定义的活动是否是“填写请假单”的活动   
  162.         if  (processInstance.isActive( "填写请假单" )) {  
  163.             url = "/apply.jsp" ;  
  164.         }  
  165.         return  url;  
  166.     }  
  167.   
  168.     protected  Map<String, Object> apply(HttpServletRequest request,  
  169.             HttpServletResponse response) throws  ServletException, IOException {  
  170.   
  171.         Map<String, Object> variables = new  HashMap<String, Object>();  
  172.         String name = request.getParameter("applyName" );  
  173.         String time = request.getParameter("applyTime" );  
  174.         String leaveDay = request.getParameter("leaveDay" );  
  175.         String content = request.getParameter("content" );  
  176.         String position = request.getParameter("position" );  
  177.   
  178.         variables.put("name" , name);  
  179.         variables.put("time" , time);  
  180.         variables.put("leaveDay" , leaveDay);  
  181.         variables.put("content" , content);  
  182.         variables.put("position" , position);  
  183.         if  (position.trim().equals( "经理" )) {  
  184.             //"manager"变量名与leave.jpdl.xml中decision里面的变量一致,同时设置的值也要保 持一致   
  185.             variables.put("manager" "是" );  
  186.         } else  {  
  187.             variables.put("manager" "否" );  
  188.         }  
  189.         //获取任务列表,因为我们在填写请假单的时候是分配给“zzg”的,见leave.jpdl.xml   
  190.         taskList = taskService.findPersonalTasks("zzg" );  
  191.         //根据任务列表获取任务   
  192.         task = taskList.get(0 );  
  193.         //把设置好的变量放到任务服务里面并根据任务ID结束任务   
  194.         taskService.setVariables(task.getId(), variables);  
  195.         taskService.completeTask(task.getId());  
  196.         //判断任务活动是否到达老板审批活动   
  197. //      execution = executionService.findExecutionById(task.getExecutionId());   
  198. //      System.out.println("isActive:  "   
  199. //              + execution.getProcessInstance().isActive(" 老板审批"));   
  200.         return  variables;  
  201.     }  
  202.   
  203.     /**  
  204.      * 获 取任务列表  
  205.      *   
  206.      * @param request  
  207.      * @param response  
  208.      * @return  
  209.      * @throws ServletException  
  210.      * @throws IOException  
  211.      */   
  212.     protected  List<Task> getTask(HttpServletRequest request,  
  213.             HttpServletResponse response) throws  ServletException, IOException {  
  214.         String name = request.getParameter("name" );  
  215.         taskList = taskService.findPersonalTasks(name);  
  216.         return  taskList;  
  217.     }  
  218.   
  219.     /**  
  220.      * 查 看任务  
  221.      *   
  222.      * @param request  
  223.      * @param response  
  224.      * @return  
  225.      * @throws ServletException  
  226.      * @throws IOException  
  227.      */   
  228.     protected  Map<String, Object> view(HttpServletRequest request,  
  229.             HttpServletResponse response) throws  ServletException, IOException {  
  230.         String taskId = request.getParameter("id" );  
  231.         Set<String> set = taskService.getVariableNames(taskId);  
  232.         Map<String, Object> map = taskService.getVariables(taskId, set);  
  233.   
  234.         return  map;  
  235.     }  
  236.   
  237.     protected   void  confirm(HttpServletRequest request,  
  238.             HttpServletResponse response) throws  ServletException, IOException {  
  239.         String taskId = request.getParameter("id" );  
  240.         task = taskService.getTask(taskId);  
  241.         execution = executionService.findExecutionById(task.getExecutionId());  
  242.         if  (execution.getProcessInstance().isActive( "老板审批" )) {  
  243.             taskService.completeTask(taskId, "老板批准" );  
  244.         } else   if  (execution.getProcessInstance().isActive( "经理审核" )) {  
  245.             String variable = (String) taskService.getVariable(taskId,  
  246.                     "leaveDay" );  
  247.             if  (Integer.valueOf(variable) >  3 ) {  
  248.                 taskService.completeTask(taskId, "请假天数>3" );  
  249.             } else  {  
  250.                 taskService.completeTask(taskId, "经理批准" );  
  251.             }  
  252.         }  
  253.     }  
  254.   
  255.     protected   void  dissent(HttpServletRequest request,  
  256.             HttpServletResponse response) throws  ServletException, IOException {  
  257.         String taskId = request.getParameter("id" );  
  258.         task = taskService.getTask(taskId);  
  259.         execution = executionService.findExecutionById(task.getExecutionId());  
  260.         if  (execution.getProcessInstance().isActive( "老板审批" )) {  
  261.             taskService.completeTask(taskId, "老板不批准" );  
  262.         } else   if  (execution.getProcessInstance().isActive( "经理审核" )) {  
  263.             taskService.completeTask(taskId, "经理不批准" );  
  264.         }  
  265.     }  
  266.   
  267.     /**  
  268.      * 获 取最新版本的流程定义  
  269.      * @param request  
  270.      * @param response  
  271.      * @return  
  272.      * @throws ServletException  
  273.      * @throws IOException  
  274.      */   
  275.     protected  List<ProcessDefinition> getLatestProcessDefinition(  
  276.             HttpServletRequest request, HttpServletResponse response)  
  277.             throws  ServletException, IOException {  
  278.         List<ProcessDefinition> processDefinitions = repositoryService  
  279.                 .createProcessDefinitionQuery().orderAsc(  
  280.                         ProcessDefinitionQuery.PROPERTY_NAME).list();  
  281.         Map<String, ProcessDefinition> map = new  LinkedHashMap<String, ProcessDefinition>();  
  282.         for  (ProcessDefinition pd : processDefinitions) {  
  283.             String key = pd.getKey();  
  284.             ProcessDefinition definition = map.get(key);  
  285.             if  ((definition ==  null )  
  286.                     || (definition.getVersion() < pd.getVersion())) {  
  287.                 map.put(key, pd);  
  288.             }  
  289.         }  
  290.         return   new  ArrayList(map.values());  
  291.     }  
  292.   
  293.     /**  
  294.      * 根 据流程定义ID获取流程实例  
  295.      * @param request  
  296.      * @param response  
  297.      * @return  
  298.      * @throws ServletException  
  299.      * @throws IOException  
  300.      */   
  301.     protected  List<ProcessInstance> getProcessInstanceById(  
  302.             HttpServletRequest request, HttpServletResponse response)  
  303.             throws  ServletException, IOException {  
  304.         String pdId = request.getParameter("id" );  
  305.         return  executionService.createProcessInstanceQuery()  
  306.                 .processDefinitionId(pdId).list();  
  307.     }  
  308.   
  309. }  
package zzg;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.zip.ZipInputStream;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.jbpm.api.Configuration;
import org.jbpm.api.Execution;
import org.jbpm.api.ExecutionService;
import org.jbpm.api.ProcessDefinition;
import org.jbpm.api.ProcessDefinitionQuery;
import org.jbpm.api.ProcessEngine;
import org.jbpm.api.ProcessInstance;
import org.jbpm.api.RepositoryService;
import org.jbpm.api.TaskService;
import org.jbpm.api.task.Task;

/**
 * Servlet implementation class LeaveServlet
 */
public class LeaveServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private ProcessEngine processEngine = null;
	private RepositoryService repositoryService = null;
	private ExecutionService executionService = null;
	private ProcessInstance processInstance = null;
	private TaskService taskService = null;
	private Task task = null;
	private Execution execution = null;
	private String url = null;
	private List list = null;
	private List<Task> taskList = null;

	/* 初始化ProcessEngine. */
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		// 获取流程引擎
		processEngine = new Configuration().buildProcessEngine();
		repositoryService = processEngine.getRepositoryService();
		executionService = processEngine.getExecutionService();
		taskService = processEngine.getTaskService();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		process(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		process(request, response);
	}

	protected void process(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String zzg = request.getParameter("zzg");

		if (zzg.equals("deploy")) {
			try {
				deploy(request, response);
			} catch (Exception e) {
				e.printStackTrace();
			}
			list = getLatestProcessDefinition(request, response);
			request.setAttribute("process", list);
			request.getRequestDispatcher("/index.jsp").forward(request,
					response);
		} else if (zzg.equals("leave")) {
			url = leave(request, response);
			request.getRequestDispatcher(url).forward(request, response);
		} else if (zzg.equals("query")) {
			list = getProcessInstanceById(request, response);
			request.setAttribute("pi", list);
			request.getRequestDispatcher("/index.jsp").forward(request,
					response);
		} else if (zzg.equals("apply")) {
			apply(request, response);
			list = getProcessInstanceById(request, response);
			request.setAttribute("pi", list);
			request.getRequestDispatcher("/index.jsp").forward(request,
					response);
		} else if (zzg.equals("getTask")) {
			list = getTask(request, response);
			request.setAttribute("taskList", list);
			request.getRequestDispatcher("/check.jsp").forward(request,
					response);
		} else if (zzg.equals("view")) {
			Map<String, Object> map = view(request, response);
			request.setAttribute("map", map);
			request.getRequestDispatcher("/view.jsp")
					.forward(request, response);
		} else if (zzg.equals("confirm")) {
			confirm(request, response);
			list = getTask(request, response);
			request.setAttribute("taskList", list);
			request.getRequestDispatcher("/check.jsp").forward(request,
					response);
		} else if (zzg.equals("dissent")) {
			dissent(request, response);
			list = getTask(request, response);
			request.setAttribute("taskList", list);
			request.getRequestDispatcher("/check.jsp").forward(request,
					response);
		}

		/*
		 * try { Class clz = this.getClass(); Method method =
		 * clz.getDeclaredMethod(action, HttpServletRequest.class,
		 * HttpServletResponse.class); method.invoke(this, request, response); }
		 * catch (Exception ex) { ex.printStackTrace(); }
		 */
	}

	protected void deploy(HttpServletRequest request,
			HttpServletRespon

你可能感兴趣的:(jbpm4)