1 新建一个工程项目
加入jbpm的包
加入Hibernate的包和支持
加入 sturts的包和支持
2 添加一个流程
3 修改hibernate.cfg.xml的内容。
在中加入jbpm的那写.hbm.xml支持文件。
加入你所要求的POJO的那些.hbm.xml文件。
4 把jbpm要创建的数据表加入到数据库中(用户定义的数据表同时加入,在要传递的实体中要加入processInstanceId属性)。本内容只需创建一次即可。
jbpmConfiguration.getInstance().createSchema();
5 把流程部署到数据库中。
JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext(); if(jbpmContext == null || jbpmContext.equals("")){ jbpmContext = jbpmConfiguration.createJbpmContext(); } ProcessDefinition processDefinition = ProcessDefinition.parseXmlResource("com/flowdemo/jbpm/expense/processdefinition.xml");; //测试是否存在 System.out.println("流程定义是否加载?==="+processDefinition.toString()); //jbpmConfiguration.getInstance().createSchema(); try { jbpmContext.deployProcessDefinition(processDefinition); } catch (RuntimeException e) { System.out.println("流程部署错误!!!!!!!!!!!"); e.printStackTrace(); }finally{ jbpmContext.close(); }
特别注意要对jbpmContext进行close()操作。否则修改内容没有进行提交
6 根据用户来返回自己需要处理的数据
/** * 根据用户来返回自己需要处理的任务 * @param user为自己建的PLOJ * @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); //获得绑定到实例上的id号码 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; }
7 创建一个新的数据
先把要传递的数据存入数据库中
然后初始化一个流程。
public static void initJbpm(){ JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext(); //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()); //jbpmConfiguration.getInstance().createSchema(); try { jbpmContext.deployProcessDefinition(processDefinition); } catch (RuntimeException e) { // TODO Auto-generated catch block System.out.println("流程部署错误!!!!!!!!!!!"); e.printStackTrace(); } 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(); } }
8 审核
public static void approve(long taskInstanceId){ 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().createStartTaskInstance(); TaskInstance taskInstance = jbpmContext.getTaskInstance(taskInstanceId); //taskInstance.end("manager_approve"); taskInstance.end(); //processInstance.signal(); }catch(Exception e){ e.printStackTrace(); }finally{ jbpmContext.close(); } }