jBPM工作流应用

一、 环境配置
基础环境是eclipse3+myeclipse6+mysql5+tomcat6 。
首先从网站上下载jBPM开发包(jbpm-jpdl-suite-3.2.3.zip),据说现在jBPM3已经升到3.2.6了,而且据说已经推出了jBPM4了。将jbpm-jpdl-suite-3.2.3.zip解压缩后,里面有个\designer文件夹,这个是eclipse IDE的jBPM插件,是可视化的那种。
安装这个插件:
1、 HelpSoftware Updates
2、 Find and Install  Search for new features to install
3、 点击 New Local Site,选择jBPM开发包解压缩后的那个插件的地址,例如D:\jbpm-jpdl-3.2.3\designer
4、 一路点击Next就可以了,安装完成后,eclipse需要重启。重启后,可以在project中到到一个JBoss jBPM的项目。如图所示。



二、 设置一个User Library
很多人不习惯Process Project的那种代码结构,而我也不习惯。Process Project 的结构如图所示。



于是想给它设置一个自己的库,可以随时在各种WEB应用中调用。设置User Library的步骤如下:
1、 eclipse中,点击WindowPreferences
2、 JavaUser Library
3、 点击New,设置User Library name 为jBPM Library。点击OK。
4、 点击Add JARs,首先来到jBPM解压缩后的路径(例如:D:\jbpm-jpdl-3.2.3),有两个JAR,其中jbpm-jpdl.jar是一定要加的。
5、 还有其他的JARs要加,重复步骤4,进入lib包(例如:D:\jbpm-jpdl-3.2.3\lib),选中所有的JARs。
6、 这样,我们的jBPM Library就设置好了。如图所示:




三、 一个结合Struts 1.x的应用
首先,假定一个流程。这个流程是常用的报销流程,只考虑了最简单的流程。



角色有三种:普通员工(employee)、经理(manager)、BOSS(boss)。这里因为只是做个示例,没有考虑过多的权限方面的设置。这里设计了三个用户,分别对应了三种角色,并且员工有上级(经理)无下属、经理有上级(BOSS)有下属(员工)、BOSS无上级有下属(经理)。具体参看数据库设计。
好了,数据设计好了,开始搭建系统吧。步骤如下:
1、 FileNewWeb Project
2、 给新添加的web项目添加Struts 1.x类库。
3、 添加新的User Library,将刚才设置好的jBPM Library添加进来。
4、 在项目下新增一个Source Folder,命名为jbpm。
搭建好的结构如下图所示:



项目的流程定义文件和jBPM插件生成的图片在com.flowdemo.jbpm.expense包下。hibernate.cfg.xml配置文件需要做一些修改,原本配置的数据库连接是hsqldb。现在把它改成为连接MySQL。具体的配置信息查看代码清单。
Com.flowdemo.service包中包含有两个业务逻辑类,一个是关于对象持久化的:PersistenceService.java 一个是工作流的:FlowService.java 。
以下是PersistenceService.java源代码,主要是对持久化对象的一些操作。
Java代码 复制代码
  1. package com.flowdemo.service;   
  2.   
  3. import java.util.ArrayList;   
  4. import java.util.List;   
  5.   
  6. import org.hibernate.Query;   
  7. import org.hibernate.Session;   
  8.   
  9. import com.flowdemo.po.Expenses;   
  10. import com.flowdemo.po.Users;   
  11. import com.flowdemo.util.HibernateSessionFactory;   
  12.   
  13. public class PersistenceService {   
  14.   
  15.     /**  
  16.      * 保存一个报销单对象  
  17.      * @param expense  
  18.      */  
  19.     public static void save(Expenses expense){   
  20.         Session hibernateSession = HibernateSessionFactory.getSession();   
  21.         try{   
  22.             //hibernateSession.getTransaction().begin();   
  23.             hibernateSession.beginTransaction();   
  24.             hibernateSession.saveOrUpdate(expense);   
  25.             hibernateSession.getTransaction().commit();   
  26.         }catch(Exception e){   
  27.             e.printStackTrace();   
  28.             hibernateSession.getTransaction().rollback();   
  29.         }finally{   
  30.             HibernateSessionFactory.closeSession();   
  31.         }   
  32.     }   
  33.        
  34.     /**  
  35.      * 更新一个报销单对象  
  36.      * @param expense  
  37.      */  
  38.     public static void update(Expenses expense){   
  39.         Session hibernateSession = HibernateSessionFactory.getSession();   
  40.         try{   
  41.             //hibernateSession.getTransaction().begin();   
  42.             hibernateSession.beginTransaction();   
  43.             //hibernateSession.saveOrUpdate(expense);   
  44.             hibernateSession.update(expense);   
  45.             hibernateSession.getTransaction().commit();   
  46.         }catch(Exception e){   
  47.             e.printStackTrace();   
  48.             hibernateSession.getTransaction().rollback();   
  49.         }finally{   
  50.             HibernateSessionFactory.closeSession();   
  51.         }   
  52.     }   
  53.        
  54.     /**  
  55.      * 删除一个报销单对象  
  56.      * @param expense  
  57.      */  
  58.     public static void delete(Expenses expense){   
  59.         Session hibernateSession = HibernateSessionFactory.getSession();   
  60.         try{   
  61.             //hibernateSession.getTransaction().begin();   
  62.             hibernateSession.beginTransaction();   
  63.             hibernateSession.delete(expense);   
  64.             hibernateSession.getTransaction().commit();   
  65.         }catch(Exception e){   
  66.             e.printStackTrace();   
  67.             hibernateSession.getTransaction().rollback();   
  68.         }finally{   
  69.             HibernateSessionFactory.closeSession();   
  70.         }   
  71.     }   
  72.        
  73.     /**  
  74.      * 返回一个报销单列表  
  75.      * @return  
  76.      */  
  77.     public static List<Expenses> getExpenses(){   
  78.         Session hibernateSession = HibernateSessionFactory.getSession();   
  79.         List<Expenses> expenses = new ArrayList<Expenses>();   
  80.         try{   
  81.                
  82.             //hibernateSession.getTransaction().begin();   
  83.             hibernateSession.beginTransaction();   
  84.             //hibernateSession.delete(expense);   
  85.             Query query = hibernateSession.createQuery("from Expenses");   
  86.             expenses = query.list();   
  87.             hibernateSession.getTransaction().commit();   
  88.                
  89.         }catch(Exception e){   
  90.             e.printStackTrace();   
  91.             hibernateSession.getTransaction().rollback();   
  92.         }finally{   
  93.             HibernateSessionFactory.closeSession();   
  94.         }   
  95.         return expenses;   
  96.     }   
  97.        
  98.     /**  
  99.      * 根据用户ID返回一个对象  
  100.      * @param userId  
  101.      * @return  
  102.      */  
  103.     public static Users getUserById(String userId){   
  104.         Session hibernateSession = HibernateSessionFactory.getSession();   
  105.         Users user = new Users();   
  106.         try{   
  107.             hibernateSession.beginTransaction();   
  108.             user = (Users)hibernateSession.get(Users.class, userId);       
  109.             hibernateSession.getTransaction().commit();   
  110.                
  111.         }catch(Exception e){   
  112.             e.printStackTrace();   
  113.             hibernateSession.getTransaction().rollback();   
  114.         }finally{   
  115.             HibernateSessionFactory.closeSession();   
  116.         }   
  117.         return user;   
  118.     }   
  119.        
  120.     /**  
  121.      * 检查用户是否存在  
  122.      * @param user  
  123.      * @return  
  124.      */  
  125.     public static boolean checkUser(Users user){   
  126.         Users user2 = null;   
  127.         try{   
  128.             user2 = getUserById(user.getUserId());   
  129.                
  130.             System.out.println("用户的值==="+user2.getUserName());   
  131.         }catch(Exception e){   
  132.             e.printStackTrace();   
  133.         }finally{   
  134.             //HibernateSessionFactory.closeSession();   
  135.         }   
  136.         if(user2.getUserPass() == user.getUserPass() || user2.getUserPass().equals(user.getUserPass())){   
  137.             return true;   
  138.         }else{   
  139.             return false;   
  140.         }   
  141.     }   
  142.        
  143.     /**  
  144.      * 测试主方法  
  145.      * @param args  
  146.      */  
  147.     public static void main(String[]args){   
  148.         Session hibernateSession = HibernateSessionFactory.getSession();   
  149.         System.out.println("测试Session是否启动1"+hibernateSession.toString());   
  150.         hibernateSession.close();   
  151.         System.out.println("测试Session是否启动2"+hibernateSession.toString());   
  152.            
  153.         hibernateSession = HibernateSessionFactory.getSession();   
  154.         System.out.println("测试Session是否启动1"+hibernateSession.toString());   
  155.         HibernateSessionFactory.closeSession();   
  156.         System.out.println("测试Session是否启动2"+hibernateSession.toString());   
  157.            
  158.         Users user = new Users();   
  159.         user.setUserId("001");   
  160.         user.setUserPass("employee");   
  161.            
  162.         boolean flag = checkUser(user);   
  163.         System.out.println("是否是正确的?"+flag);   
  164.     }   
  165.        
  166. }  
package com.flowdemo.service;

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

import org.hibernate.Query;
import org.hibernate.Session;

import com.flowdemo.po.Expenses;
import com.flowdemo.po.Users;
import com.flowdemo.util.HibernateSessionFactory;

public class PersistenceService {

	/**
	 * 保存一个报销单对象
	 * @param expense
	 */
	public static void save(Expenses expense){
		Session hibernateSession = HibernateSessionFactory.getSession();
		try{
			//hibernateSession.getTransaction().begin();
			hibernateSession.beginTransaction();
			hibernateSession.saveOrUpdate(expense);
			hibernateSession.getTransaction().commit();
		}catch(Exception e){
			e.printStackTrace();
			hibernateSession.getTransaction().rollback();
		}finally{
			HibernateSessionFactory.closeSession();
		}
	}
	
	/**
	 * 更新一个报销单对象
	 * @param expense
	 */
	public static void update(Expenses expense){
		Session hibernateSession = HibernateSessionFactory.getSession();
		try{
			//hibernateSession.getTransaction().begin();
			hibernateSession.beginTransaction();
			//hibernateSession.saveOrUpdate(expense);
			hibernateSession.update(expense);
			hibernateSession.getTransaction().commit();
		}catch(Exception e){
			e.printStackTrace();
			hibernateSession.getTransaction().rollback();
		}finally{
			HibernateSessionFactory.closeSession();
		}
	}
	
	/**
	 * 删除一个报销单对象
	 * @param expense
	 */
	public static void delete(Expenses expense){
		Session hibernateSession = HibernateSessionFactory.getSession();
		try{
			//hibernateSession.getTransaction().begin();
			hibernateSession.beginTransaction();
			hibernateSession.delete(expense);
			hibernateSession.getTransaction().commit();
		}catch(Exception e){
			e.printStackTrace();
			hibernateSession.getTransaction().rollback();
		}finally{
			HibernateSessionFactory.closeSession();
		}
	}
	
	/**
	 * 返回一个报销单列表
	 * @return
	 */
	public static List<Expenses> getExpenses(){
		Session hibernateSession = HibernateSessionFactory.getSession();
		List<Expenses> expenses = new ArrayList<Expenses>();
		try{
			
			//hibernateSession.getTransaction().begin();
			hibernateSession.beginTransaction();
			//hibernateSession.delete(expense);
			Query query = hibernateSession.createQuery("from Expenses");
			expenses = query.list();
			hibernateSession.getTransaction().commit();
			
		}catch(Exception e){
			e.printStackTrace();
			hibernateSession.getTransaction().rollback();
		}finally{
			HibernateSessionFactory.closeSession();
		}
		return expenses;
	}
	
	/**
	 * 根据用户ID返回一个对象
	 * @param userId
	 * @return
	 */
	public static Users getUserById(String userId){
		Session hibernateSession = HibernateSessionFactory.getSession();
		Users user = new Users();
		try{
			hibernateSession.beginTransaction();
			user = (Users)hibernateSession.get(Users.class, userId);	
			hibernateSession.getTransaction().commit();
			
		}catch(Exception e){
			e.printStackTrace();
			hibernateSession.getTransaction().rollback();
		}finally{
			HibernateSessionFactory.closeSession();
		}
		return user;
	}
	
	/**
	 * 检查用户是否存在
	 * @param user
	 * @return
	 */
	public static boolean checkUser(Users user){
		Users user2 = null;
		try{
			user2 = getUserById(user.getUserId());
			
			System.out.println("用户的值==="+user2.getUserName());
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			//HibernateSessionFactory.closeSession();
		}
		if(user2.getUserPass() == user.getUserPass() || user2.getUserPass().equals(user.getUserPass())){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 测试主方法
	 * @param args
	 */
	public static void main(String[]args){
		Session hibernateSession = HibernateSessionFactory.getSession();
		System.out.println("测试Session是否启动1"+hibernateSession.toString());
		hibernateSession.close();
		System.out.println("测试Session是否启动2"+hibernateSession.toString());
		
		hibernateSession = HibernateSessionFactory.getSession();
		System.out.println("测试Session是否启动1"+hibernateSession.toString());
		HibernateSessionFactory.closeSession();
		System.out.println("测试Session是否启动2"+hibernateSession.toString());
		
		Users user = new Users();
		user.setUserId("001");
		user.setUserPass("employee");
		
		boolean flag = checkUser(user);
		System.out.println("是否是正确的?"+flag);
	}
	
}

以下是FlowService.java源代码:
Java代码 复制代码
  1. package com.flowdemo.service;   
  2.   
  3. import java.util.ArrayList;   
  4. import java.util.List;   
  5.   
  6. import org.jbpm.JbpmConfiguration;   
  7. import org.jbpm.JbpmContext;   
  8. import org.jbpm.db.GraphSession;   
  9. import org.jbpm.graph.def.ProcessDefinition;   
  10. import org.jbpm.graph.exe.ProcessInstance;   
  11. import org.jbpm.taskmgmt.exe.TaskInstance;   
  12.   
  13. import com.flowdemo.jbpm.util.PoTaskWrapper;   
  14. import com.flowdemo.po.Expenses;   
  15. import com.flowdemo.po.Users;   
  16. import com.flowdemo.util.HibernateSessionFactory;   
  17.   
  18. public class FlowService {   
  19.   
  20.     static JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance();   
  21.        
  22.     /**  
  23.      * 初始化流程  
  24.      */  
  25.     public static void initJbpm(){   
  26.         JbpmContext jbpmContext = jbpmConfiguration.getCurrentJbpmContext();   
  27.         if(jbpmContext == null || jbpmContext.equals("")){   
  28.             jbpmContext = jbpmConfiguration.createJbpmContext();   
  29.         }   
  30.         //如果流程定义没有,则重新部署   
  31.         try{   
  32.             ProcessDefinition processDefinition = ProcessDefinition.parseXmlResource("com/flowdemo/jbpm/expense/processdefinition.xml");;              
  33.                 //测试是否存在   
  34.             System.out.println("流程定义是否加载?==="+processDefinition.toString());   
  35.             jbpmContext.deployProcessDefinition(processDefinition);   
  36.                
  37.             GraphSession graphSession = jbpmContext.getGraphSession();   
  38.             processDefinition = graphSession.findLatestProcessDefinition("expense");   
  39.         }finally{   
  40.             jbpmContext.close();   
  41.         }   
  42.            
  43.     }   
  44.        
  45.     /**  
  46.      * 绑定报销单到流程中  
  47.      */  
  48.     public static void bindingExpenseToProcess(){   
  49.         JbpmContext jbpmContext = jbpmConfiguration.getCurrentJbpmContext();   
  50.         if(jbpmContext == null || jbpmContext.equals("")){   
  51.             jbpmContext = jbpmConfiguration.createJbpmContext();   
  52.         }   
  53.         try{   
  54.             jbpmContext.setSessionFactory(HibernateSessionFactory.getSessionFactory());   
  55.             ProcessDefinition processDefinition = jbpmContext.getGraphSession().findLatestProcessDefinition("expense");   
  56.             ProcessInstance processInstance = new ProcessInstance(processDefinition);   
  57.                
  58.             jbpmContext.save(processInstance);    
  59.                
  60.             long processInstanceId = processInstance.getId();   
  61.                
  62.             //用什么来get or load这个Expenses对象呢?   
  63.             Expenses expense = (Expenses)jbpmContext.getSession().createQuery("from Expenses where processInstanceId = "+null).list().get(0);   
  64.             //Expenses expense = (Expenses)jbpmContext.getSession().get(Expenses.class, 3);   
  65.             expense.setProcessInstanceId(new Long(processInstanceId).toString());   
  66.                
  67.             jbpmContext.getSession().update(expense);   
  68.             processInstance.getContextInstance().setVariable("expense_id", expense.getExpenseId());   
  69.                
  70.             processInstance.signal();   
  71.         }finally{   
  72.             jbpmContext.close();   
  73.         }   
  74.            
  75.     }   
  76.        
  77.     /**  
  78.      * 根据用户来返回自己需要处理的任务  
  79.      * @param user  
  80.      * @return  
  81.      */  
  82.     public static List getTasks(Users user){   
  83.         JbpmContext jbpmContext = jbpmConfiguration.getCurrentJbpmContext();   
  84.         if(jbpmContext == null || jbpmContext.equals("")){   
  85.             jbpmContext = jbpmConfiguration.createJbpmContext();   
  86.         }   
  87.         System.out.println("提供任务的人的名字"+user.getUserName());   
  88.         List tasks = new ArrayList();   
  89.         //List expenses = new ArrayList();   
  90.         List poAndTasks = new ArrayList();   
  91.         try{   
  92.             tasks = jbpmContext.getTaskMgmtSession().findTaskInstances(user.getUserName());   
  93.             System.out.println("需要处理的任务的多少"+tasks);   
  94.             TaskInstance taskInstance = null;   
  95.             Expenses expense = null;   
  96.                
  97.             for(int i=0;i<tasks.size();i++){   
  98.                 taskInstance = (TaskInstance)tasks.get(i);   
  99.                 Integer expenseId =(Integer)taskInstance.getProcessInstance().getContextInstance().getVariable("expense_id");   
  100.                 System.out.println("expenseId的值是多少呢?"+expenseId);   
  101.                    
  102.                 PoTaskWrapper poTask = new PoTaskWrapper();   
  103.                    
  104.                 jbpmContext.setSessionFactory(HibernateSessionFactory.getSessionFactory());   
  105.                 expense = (Expenses)jbpmContext.getSession().load(Expenses.class, expenseId);   
  106.                 System.out.println("对应的报销单的编号、金额和理由"+expense.getExpenseId()+" "+expense.getMoney()+" "+expense.getReason());   
  107.                 //expenses.add(expense);   
  108.                    
  109.                 poTask.setExpense(expense);   
  110.                 System.out.println("poTask获得的expense-->"+poTask.getExpense().getMoney());   
  111.                 poTask.setTaskInstanceId(taskInstance.getId());   
  112.                 poTask.setTokenId(taskInstance.getToken().getId());   
  113.                 poAndTasks.add(poTask);   
  114.             }   
  115.         }catch(Exception e){   
  116.             e.printStackTrace();   
  117.         }finally{   
  118.             jbpmContext.close();   
  119.         }   
  120.         //return expenses;   
  121.         System.out.println("队列的长度 -->"+poAndTasks.size());   
  122.         return poAndTasks;   
  123.     }   
  124.        
  125.     /**  
  126.      * @param processInstanceId  
  127.      * @return  
  128.      */  
  129.     public static boolean managerApprove(String processInstanceId){   
  130.         JbpmContext jbpmContext = jbpmConfiguration.getCurrentJbpmContext();   
  131.         if(jbpmContext == null || jbpmContext.equals("")){   
  132.             jbpmContext = jbpmConfiguration.createJbpmContext();   
  133.         }   
  134.         try{   
  135.             ProcessInstance processInstance = jbpmContext.getProcessInstance(new Long(processInstanceId).longValue());   
  136.             //ProcessDefinition processDefinition = jbpmContext.getGraphSession().findLatestProcessDefinition("expense");   
  137.             //ProcessInstance processInstance = new ProcessInstance(processDefinition);   
  138.             TaskInstance taskInstance = processInstance.getTaskMgmtInstance().createTaskInstance();   
  139.             taskInstance.end();   
  140.             processInstance.signal();   
  141.                
  142.         }catch(Exception e){   
  143.             e.printStackTrace();   
  144.         }finally{   
  145.             jbpmContext.close();   
  146.         }   
  147.            
  148.         return false;   
  149.     }   
  150.        
  151.        
  152.     /**  
  153.      * 审核  
  154.      * @param taskInstanceId  
  155.      */  
  156.     public static void approve(long taskInstanceId){   
  157.         JbpmContext jbpmContext = jbpmConfiguration.getCurrentJbpmContext();   
  158.         if(jbpmContext == null || jbpmContext.equals("")){   
  159.             jbpmContext = jbpmConfiguration.createJbpmContext();   
  160.         }   
  161.         try{   
  162.             TaskInstance taskInstance = jbpmContext.getTaskInstance(taskInstanceId);   
  163.             //taskInstance.end("manager_approve");   
  164.             taskInstance.end();   
  165.             //processInstance.signal();   
  166.                
  167.         }catch(Exception e){   
  168.             e.printStackTrace();   
  169.         }finally{   
  170.             jbpmContext.close();   
  171.         }   
  172.     }   
  173.   
  174. }  
package com.flowdemo.service;

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

import org.jbpm.JbpmConfiguration;
import org.jbpm.JbpmContext;
import org.jbpm.db.GraphSession;
import org.jbpm.graph.def.ProcessDefinition;
import org.jbpm.graph.exe.ProcessInstance;
import org.jbpm.taskmgmt.exe.TaskInstance;

import com.flowdemo.jbpm.util.PoTaskWrapper;
import com.flowdemo.po.Expenses;
import com.flowdemo.po.Users;
import com.flowdemo.util.HibernateSessionFactory;

public class FlowService {

	static JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance();
	
	/**
	 * 初始化流程
	 */
	public static void initJbpm(){
		JbpmContext jbpmContext = jbpmConfiguration.getCurrentJbpmContext();
		if(jbpmContext == null || jbpmContext.equals("")){
			jbpmContext = jbpmConfiguration.createJbpmContext();
		}
		//如果流程定义没有,则重新部署
		try{
			ProcessDefinition processDefinition = ProcessDefinition.parseXmlResource("com/flowdemo/jbpm/expense/processdefinition.xml");;			
				//测试是否存在
			System.out.println("流程定义是否加载?==="+processDefinition.toString());
			jbpmContext.deployProcessDefinition(processDefinition);
			
			GraphSession graphSession = jbpmContext.getGraphSession();
			processDefinition = graphSession.findLatestProcessDefinition("expense");
		}finally{
			jbpmContext.close();
		}
		
	}
	
	/**
	 * 绑定报销单到流程中
	 */
	public static void bindingExpenseToProcess(){
		JbpmContext jbpmContext = jbpmConfiguration.getCurrentJbpmContext();
		if(jbpmContext == null || jbpmContext.equals("")){
			jbpmContext = jbpmConfiguration.createJbpmContext();
		}
		try{
			jbpmContext.setSessionFactory(HibernateSessionFactory.getSessionFactory());
			ProcessDefinition processDefinition = jbpmContext.getGraphSession().findLatestProcessDefinition("expense");
			ProcessInstance processInstance = new ProcessInstance(processDefinition);
			
			jbpmContext.save(processInstance); 
			
			long processInstanceId = processInstance.getId();
			
			//用什么来get or load这个Expenses对象呢?
			Expenses expense = (Expenses)jbpmContext.getSession().createQuery("from Expenses where processInstanceId = "+null).list().get(0);
			//Expenses expense = (Expenses)jbpmContext.getSession().get(Expenses.class, 3);
			expense.setProcessInstanceId(new Long(processInstanceId).toString());
			
			jbpmContext.getSession().update(expense);
			processInstance.getContextInstance().setVariable("expense_id", expense.getExpenseId());
			
			processInstance.signal();
		}finally{
			jbpmContext.close();
		}
		
	}
	
	/**
	 * 根据用户来返回自己需要处理的任务
	 * @param user
	 * @return
	 */
	public static List getTasks(Users user){
		JbpmContext jbpmContext = jbpmConfiguration.getCurrentJbpmContext();
		if(jbpmContext == null || jbpmContext.equals("")){
			jbpmContext = jbpmConfiguration.createJbpmContext();
		}
		System.out.println("提供任务的人的名字"+user.getUserName());
		List tasks = new ArrayList();
		//List expenses = new ArrayList();
		List poAndTasks = new ArrayList();
		try{
			tasks = jbpmContext.getTaskMgmtSession().findTaskInstances(user.getUserName());
			System.out.println("需要处理的任务的多少"+tasks);
			TaskInstance taskInstance = null;
			Expenses expense = null;
			
			for(int i=0;i<tasks.size();i++){
				taskInstance = (TaskInstance)tasks.get(i);
				Integer expenseId =(Integer)taskInstance.getProcessInstance().getContextInstance().getVariable("expense_id");
				System.out.println("expenseId的值是多少呢?"+expenseId);
				
				PoTaskWrapper poTask = new PoTaskWrapper();
				
				jbpmContext.setSessionFactory(HibernateSessionFactory.getSessionFactory());
				expense = (Expenses)jbpmContext.getSession().load(Expenses.class, expenseId);
				System.out.println("对应的报销单的编号、金额和理由"+expense.getExpenseId()+" "+expense.getMoney()+" "+expense.getReason());
				//expenses.add(expense);
				
				poTask.setExpense(expense);
				System.out.println("poTask获得的expense-->"+poTask.getExpense().getMoney());
				poTask.setTaskInstanceId(taskInstance.getId());
				poTask.setTokenId(taskInstance.getToken().getId());
				poAndTasks.add(poTask);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			jbpmContext.close();
		}
		//return expenses;
		System.out.println("队列的长度 -->"+poAndTasks.size());
		return poAndTasks;
	}
	
	/**
	 * @param processInstanceId
	 * @return
	 */
	public static boolean managerApprove(String processInstanceId){
		JbpmContext jbpmContext = jbpmConfiguration.getCurrentJbpmContext();
		if(jbpmContext == null || jbpmContext.equals("")){
			jbpmContext = jbpmConfiguration.createJbpmContext();
		}
		try{
			ProcessInstance processInstance = jbpmContext.getProcessInstance(new Long(processInstanceId).longValue());
			//ProcessDefinition processDefinition = jbpmContext.getGraphSession().findLatestProcessDefinition("expense");
			//ProcessInstance processInstance = new ProcessInstance(processDefinition);
			TaskInstance taskInstance = processInstance.getTaskMgmtInstance().createTaskInstance();
			taskInstance.end();
			processInstance.signal();
			
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			jbpmContext.close();
		}
		
		return false;
	}
	
	
	/**
	 * 审核
	 * @param taskInstanceId
	 */
	public static void approve(long taskInstanceId){
		JbpmContext jbpmContext = jbpmConfiguration.getCurrentJbpmContext();
		if(jbpmContext == null || jbpmContext.equals("")){
			jbpmContext = jbpmConfiguration.createJbpmContext();
		}
		try{
			TaskInstance taskInstance = jbpmContext.getTaskInstance(taskInstanceId);
			//taskInstance.end("manager_approve");
			taskInstance.end();
			//processInstance.signal();
			
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			jbpmContext.close();
		}
	}

}

还有一点需要注意的是,在测试运行之前需要把jbpm的数据库表创建好,当然也可以通过JbpmContext jbpmContext = cfg.createJbpmContext();来创建,但是这是在每次运行时都会创建,所以还是直接通过导入jbpm的sql文件来创建的好。
好了,一个基于struts的jbpm应用差不多就完成了,我比较懒,所以也不太想在介绍代码上费功夫了,各位看官自己看咯,呵呵。
还想做一些集成spring的应用,但是由于公司也没用上这种技术,所以兴趣又降了一大半,⊙﹏⊙b汗。以后有机会再自己写写吧。以下附上源代码。有需要的请下载吧。按照习惯,我去掉了jar包,所有的需要的jar清单,请看图片:



  • 大小: 35.6 KB
  • 大小: 10.5 KB
  • 大小: 43 KB
  • 大小: 9.7 KB
  • 大小: 32.7 KB
  • 大小: 51.1 KB
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值