十四、从起点(StartState)出发,如何得知下一步可选的路径列表
<process-definition? name="test3">
?? <start-state name="开始">
????? <!-- 流转条件,从ContextInstance中取days变量 -->
????? <transition name="流向李四审批" to="李四审批" condition="#{days gt 10}"></transition>
????? <transition name="流向王五审批" to="王五审批" condition="#{days le 10}"></transition>
?? </start-state>
?? <task-node name="李四审批">
?? ??? ? <task name="审批">
?? ??? ? ??? <assignment actor-id="李四"></assignment>
?? ??? ? </task>
????? <transition name="" to="结束"></transition>
?? </task-node>
?? <task-node name="王五审批">
????? <task name="审批">
?? ??? ? ??? <assignment actor-id="王五"></assignment>
?? ??? ? </task>
????? <transition name="" to="结束"></transition>
?? </task-node>
?? <end-state name="结束"></end-state>
</process-definition>
?
?
??? //创建公文
??? @Test
??? public void createDocTest(){
??? ???
??? ??? JbpmContext context = null;
??? ???
??? ??? ?try {
??? ??? ??? ?
??? ??? ??? //默认从classpath中查找名为hibernate.cfg.xml的配置文件
??? ??? ??? JbpmConfiguration configuration = JbpmConfiguration.getInstance();
??? ??? ???
??? ??? ??? //JbpmContext是对hibernate session的封装,提供了对JBPM相关对象的持久化功能
??? ??? ??? context = configuration.createJbpmContext();
??? ??? ???
??? ??? ??? Document document = new Document();
??? ??? ??? document.setTitle("公文"+new Random().nextInt());
??? ??? ???
??? ??? ??? context.getSession().save(document);
??? ??? ???
??? ??? ??? //创建流程实例
??? ??? ??? //1、首先从数据库中加载ProcessDefinition对象
??? ??? ??? ProcessDefinition definition = context.getGraphSession().findLatestProcessDefinition("test3");
??? ??? ??? //2、根据ProcessDefinition对象,创建流程实例对象
??? ??? ??? ProcessInstance instance = new ProcessInstance(definition);
??? ??? ??? instance.setKey(document.getId()+"");
??? ??? ??? context.save(instance);
??? ??? ??? //3、把公文对象和流程实例对象互相绑定
??? ??? ??? System.out.println("流程实例ID:"+instance.getId());
??? ??? ??? System.out.println("公文ID:"+document.getId());
??? ??? ??? document.setProcessInstanceId(instance.getId());
??? ??? ??? instance.getContextInstance().setVariable("documentId", document.getId());
??? ??? ??? //为流转条件赋值
??? ??? ??? instance.getContextInstance().setVariable("days", 5);
??? ??? } catch (RuntimeException e) {
??? ??? ??? e.printStackTrace();
??? ??? ??? //回滚
??? ??? ??? context.setRollbackOnly();
??? ??? }finally{
??? ??? ??? context.close();
??? ??? }
??? ???
??? }
?
//查询开始节点的下一个流转方向
??? @SuppressWarnings("unchecked")
??? @Test
??? public void searchNextTransitionsFromStartStateTest(){
??? ???
??? ??? JbpmContext context = null;
??? ???
??? ??? ?try {
??? ??? ??? ?
??? ??? ??? //默认从classpath中查找名为hibernate.cfg.xml的配置文件
??? ??? ??? JbpmConfiguration configuration = JbpmConfiguration.getInstance();
??? ??? ???
??? ??? ??? //JbpmContext是对hibernate session的封装,提供了对JBPM相关对象的持久化功能
??? ??? ??? context = configuration.createJbpmContext();
??? ??? ???
??? ??? ??? //已知公文Id=5
??? ??? ??? Document document = (Document) context.getSession().load(Document.class, 5l);
??? ??? ??? //得到公文对应的流程实例
??? ??? ??? long processInstanceId = document.getProcessInstanceId();
??? ??? ??? ProcessInstance instance = context.getProcessInstance(processInstanceId);
??? ??? ??? //当遇到流转是有条件的时候,该方法不合适
??? ??? ??? //List<Transition> list = instance.getRootToken().getNode().getLeavingTransitions();
??? ??? ??? Set<Transition> list = instance.getRootToken().getAvailableTransitions();
??? ??? ??? System.out.println("当前节点可以选择的流转方向有:");
??? ??? ??? for (Transition transition : list) {
??? ??? ??? ??? System.out.println(transition.getName());
??? ??? ??? }
??? ??? ???
??? ??? } catch (RuntimeException e) {
??? ??? ??? e.printStackTrace();
??? ??? }finally{
??? ??? ??? context.close();
??? ??? }
??? ???
??? }
?
?
?
public void submitDocTest(){
??? ???
??? ??? JbpmContext context = null;
??? ???
??? ??? ?try {
??? ??? ??? ?
??? ??? ??? //默认从classpath中查找名为hibernate.cfg.xml的配置文件
??? ??? ??? JbpmConfiguration configuration = JbpmConfiguration.getInstance();
??? ??? ???
??? ??? ??? //JbpmContext是对hibernate session的封装,提供了对JBPM相关对象的持久化功能
??? ??? ??? context = configuration.createJbpmContext();
??? ??? ???
??? ??? ??? //已知公文Id=6
??? ??? ??? Document document = (Document) context.getSession().load(Document.class, 6l);
??? ??? ??? //得到公文对应的流程实例
??? ??? ??? long processInstanceId = document.getProcessInstanceId();
??? ??? ??? ProcessInstance instance = context.getProcessInstance(processInstanceId);
??? ??? ???
??? ??? ??? /*将公文提交到下一个环节,只有在流程处于开始节点的时候使用,
??? ??? ??? 当流程流转到某个任务节点时,不能使用该方法,因为使用该法只
??? ??? ??? 能使流程流转到下一个节点,而不能保证该任务节点的任务已经完成,
??? ??? ??? 使任务处于关闭状态*/
??? ??? ??? //无参数的signal()方法只能随机选择一个流转,当流转有条件时,就会出现冲突,提交失败
??? ??? ??? //可能会抛出org.jbpm.JbpmException: transition condition #{days gt 10} evaluated to 'false'
??? ??? ??? instance.signal("流向王五审批");//对于一个公文只能执行一次
??? ??? ???
??? ??? } catch (RuntimeException e) {
??? ??? ??? e.printStackTrace();
??? ??? ??? //回滚
??? ??? ??? context.setRollbackOnly();
??? ??? }finally{
??? ??? ??? context.close();
??? ??? }
??? ???
??? }