美文网首页
activiti6.0 获得下一个审批人

activiti6.0 获得下一个审批人

作者: 九思而行 | 来源:发表于2018-03-16 11:08 被阅读2445次

首先必须说明,获得下一个审批人是有很大局限的,它只适用与少部分情况

image

先看一下这个图,我们如果想知道下一个审批人首先便会遇到排他网管的问题,如果遇到排他网管,我们就要预先知道流程会流向何处(当然,如果排他网管的数据是会更改的就另说了,毕竟获得下一个审批人本来就是有很大局限性的)

==猜想实现过程:==

  1. 我们一定要拿到整个流程模型的所有节点(通过流程的key获得模型,然后再获得所有节点)

==关键代码==

//根据流程id获得流程模式id
String processDefinitionId  = historyService.createHistoricProcessInstanceQuery()
        .processInstanceId(processId).singleResult().getProcessDefinitionId();
//获得流程模型
BpmnModel model = repositoryService.getBpmnModel(processDefinitionId);
//获得模式中所有节点
Collection<FlowElement> flowElements = model.getMainProcess().getFlowElements();

  1. 还必须知道流程当前所处位置,毕竟这个是支点般的存在(提示一下,有一个表叫做act_ru_task,里面的TASK_DEF_KEY_字段就是节点id)
  2. 还要获得流程中的所有变量信息,用于判断排他网管走向(act_ru_variable中有)
  3. 如果遇到其他情况,比这张图更复杂,毕竟这个是个很简单流程图

下面开始粘代码:

/**
 * 获得下一个节点的审批人或者节点名字
 * @param processId 流程实例id
 * @return
 * @throws Exception
 */
public String nextPrcessApprover(String processId)throws Exception{
        //获得流程当前所处位置
        List<ActRuTask> actRuTasks = actRuTaskMapper.getTaskByProcessId(processId);


        //根据流程id获得流程模式id
        String processDefinitionId  = historyService.createHistoricProcessInstanceQuery()
                .processInstanceId(processId).singleResult().getProcessDefinitionId();
        //获得流程模型
        BpmnModel model = repositoryService.getBpmnModel(processDefinitionId);

        //最终usertask节点
        List<FlowElement> userTasks = new ArrayList<>();
        if(model != null&&actRuTasks!=null&&actRuTasks.size()!=0) {
            //获得流程模型的所有节点
            Collection<FlowElement> flowElements = model.getMainProcess().getFlowElements();
            //便利节点
            for(FlowElement e : flowElements) {
                for(ActRuTask task : actRuTasks){
                    String currentKey = task.getTaskDefKey();
                    //
                    if(StringUtils.isNotEmpty(currentKey)){
                        //获得当前节点
                        if(e.getId().equals(currentKey)){
                            if(e instanceof UserTask){
                                List<SequenceFlow> currentUser =  ((UserTask) e).getOutgoingFlows();
                                //获得流程中的所有变量信息
                                List<ActRuVariable> actRuVariables = actRuVariableMapper.getVariableByProcessId(processId);
                                for (SequenceFlow s : currentUser){
                                    nextNode(flowElements,s,actRuVariables,userTasks);
                                }
                            }
                        }
                    }
                }

            }
        }

        StringBuffer sb = new StringBuffer();
        for(FlowElement e : userTasks){
            if(e instanceof UserTask){
                List<String> l =((UserTask) e).getCandidateUsers();
                for(String s :l){
                    List<SUser> sUsers = sUserMapper.getUserByRole(s);
                    if(sUsers!=null&&sUsers.size()!=0){
                        for(SUser sUser :sUsers){
                            sb.append(sUser.getName()+"【用户】,");
                        }
                    }else{
                        sb.append(e.getName()+"【节点名称】,");
                    }
                }
            }else if(e instanceof EndEvent){
                sb.append(e.getName());
            }
        }
        return sb.toString();
    }

获得下一个节点的递归:

 /**
     *
     * @param flowElements 该模型的所有节点
     * @param currentSequenceFlow 当前流程线
     * @param elKV  所有变量信息
     * @param userTasks 最终结果集
     * @throws Exception
     */
    private void nextNode(Collection<FlowElement> flowElements,SequenceFlow currentSequenceFlow,List<ActRuVariable> elKV,List<FlowElement> userTasks)throws Exception{
        //获得当前线在
        String  SequenceFlowId =   currentSequenceFlow.getTargetRef();
        for(FlowElement e : flowElements){
            if(e.getId().equals(SequenceFlowId)){
                //判断类型
                //如果是排他网管
                if(e instanceof ExclusiveGateway){
                    //默认流程线
                    String defaultFlowString = ((ExclusiveGateway) e).getDefaultFlow();
                    SequenceFlow defaultFlow = null;
                    List<SequenceFlow> egSequenceFlow  = ((ExclusiveGateway) e).getOutgoingFlows();
                    //标识
                    boolean boo = true;
                    for(int i=0;i<egSequenceFlow.size();i++){
                        if(egSequenceFlow.get(i).getId().equals(defaultFlowString)){
                            defaultFlow = egSequenceFlow.get(i);
                        }
                        if(!StringUtils.isEmpty(egSequenceFlow.get(i).getConditionExpression())){
                            //判断el选择路线
                            if(isCondition(egSequenceFlow.get(i).getConditionExpression(),elKV)){
                                boo=false;
                                //如果为真说明会走这条路线  递归
                                nextNode(flowElements,egSequenceFlow.get(i),elKV,userTasks);
                            }
                        }else{
                            continue;
                        }

                        //如果最后一个走完没有el为true的,则查看是否有默认流程,如果没有抛出异常
                        if(i==egSequenceFlow.size()-1&&boo){
                            if(StringUtils.isEmpty(defaultFlowString)){
                                throw  new Exception("流程异常");
                            }else{
                                //如果有默认流程 递归
                                nextNode(flowElements,defaultFlow,elKV,userTasks);
                            }
                        }
                    }
                    //如果是user用户审批节点
                }else if(e instanceof UserTask){
                    userTasks.add(e);
                }else if(e instanceof EndEvent){
                    userTasks.add(e);
                }
                // .... 现在就这么多
            }
        }
    }
//判断 el 表达式
private boolean isCondition( String el,List<ActRuVariable> elKV) {
    ExpressionFactory factory = new ExpressionFactoryImpl();
    SimpleContext context = new SimpleContext();
    for(int i =0 ;i<elKV.size();i++){
        context.setVariable(elKV.get(i).getName(), factory.createValueExpression(elKV.get(i).getText(), String.class));
    }
    ValueExpression e = factory.createValueExpression(context, el, boolean.class);
    return (Boolean) e.getValue(context);
}

相关文章

网友评论

      本文标题:activiti6.0 获得下一个审批人

      本文链接:https://www.haomeiwen.com/subject/uwylqftx.html