Flowable获取节点信息

1、传入当前任务ID,和当前任务的一个选择条件值(测试后发现传入任何值都行)

/**
     * 下一节点名称
     * @param taskId 流程id
     * @return
     */
    public void findFlowNode(String taskId, Object approved) { // taskId:任务ID,approved:任意选择条件
        //当前任务信息
        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
        //ProcessInstanceId流程实例
        String processlnstanceld = task.getProcessInstanceId();
        Map<String, Object> data = new HashMap<>();
        data.put("approved", approved);
        //获取流程发布工的信息
        String definitionld = runtimeService.createProcessInstanceQuery().processInstanceId(processlnstanceld).singleResult().getProcessDefinitionId();        //获取bpm(模型)对象
        BpmnModel bpmnModel = repositoryService.getBpmnModel(definitionld);
        //传节点定义key获取当前节点
        FlowNode flowNode = (FlowNode) bpmnModel.getFlowElement(task.getTaskDefinitionKey());
        // 获取本节点的Id和Name
        System.out.println("当前节点Id" + flowNode.getId());
        System.out.println("当前节点名称" + flowNode.getName());
        //输出连线
        List<SequenceFlow> outgoingFlows = flowNode.getOutgoingFlows();
        //遍历返回下一个节点信息
        for (SequenceFlow outgoingFlow : outgoingFlows) {
            //类型自己判断(获取下个节点是网关还是节点)
            FlowElement targetFlowElement = outgoingFlow.getTargetFlowElement();
            //下个是节点
            if(targetFlowElement instanceof UserTask) {
                // 判断是否是会签
                UserTask userTask = (UserTask) targetFlowElement;
                if (userTask.getBehavior() instanceof ParallelMultiInstanceBehavior) {
                    ParallelMultiInstanceBehavior behavior = (ParallelMultiInstanceBehavior) userTask.getBehavior();
                    if (behavior != null && behavior.getCollectionExpression() != null) {
						System.out.println("当前节点是会签");
                    }
                }else {
                    System.out.println("当前节点不是会签");
                }
                if (approvalFlowNodeDto.getBehaviorFlag()) { // 下个节点是会签
                	// 会签的候选用户Key
                    String assignees =((UserTask) targetFlowElement).getAssignee();;
					System.out.println("获取会签的id");
					String nextCandidateUsers = (((UserTask) targetFlowElement).getLoopCharacteristics().getInputDataItem());
					System.out.println("获取会签的collection,候选人List",nextCandidateUsers);
                } else { // 下个节点不是会签
                    String candidateUserses = ((UserTask) targetFlowElement).getCandidateUsers().get(0);
                    System.out.println("获取用户节点的candidateUsers,候选人",candidateUserses);
                    String candidateUsers = candidateUserses.substring(candidateUserses.indexOf("{")+1, candidateUserses.indexOf("}"));
                    System.out.println("查询出的值是${submitUser},截取获取用户节点的candidateUsers,候选人",candidateUsers);
                }
                System.out.println("下一节点: id=" + targetFlowElement.getId() + ",name=" + targetFlowElement.getName());
            }else if(targetFlowElement instanceof ExclusiveGateway){// 下个节点是网关(调用下面的方法)
                setExclusiveGateway(targetFlowElement,data);
            }
        }
    }

2、网关节点的方法

/**
     * 获取排他网关分支名称、分支表达式、下一级任务节点
     * @param flowElement
     * @param data
     */
    private void setExclusiveGateway(FlowElement flowElement,Map data){
    	// 获取所有网关分支
        List<SequenceFlow> targetFlows=((ExclusiveGateway)flowElement).getOutgoingFlows();
        // 循环每个网关分支
        for(SequenceFlow sequenceFlow : targetFlows){
            // 获取下一个网关和节点数据
            FlowElement targetFlowElement=sequenceFlow.getTargetFlowElement();
            // 网关数据不为空
            if (org.apache.commons.lang.StringUtils.isNotBlank(sequenceFlow.getConditionExpression())) {
                //计算连接线上的表达式
                Object result = managementService.executeCommand(new ExpressionEvaluateUtil(sequenceFlow.getConditionExpression(), data));
                System.out.println("排他网关中线条: id=" + sequenceFlow.getId() + ",name=" + sequenceFlow.getName()+",result="+result+",ConditionExpression="+sequenceFlow.getConditionExpression());
                // 获取网关判断条件
                String conditionExpression = null;
                if (sequenceFlow.getConditionExpression() != null) {
                    conditionExpression = sequenceFlow.getConditionExpression().substring(sequenceFlow.getConditionExpression().indexOf("==")+2,
                            sequenceFlow.getConditionExpression().indexOf("}"));
                }
                System.out.println("截取后的选择条件: id=" + conditionExpression);
            }
            // 网关的下个节点是用户节点
            if(targetFlowElement instanceof UserTask){
                // 判断是否是会签
                UserTask userTask = (UserTask) targetFlowElement;
                if (userTask.getBehavior() instanceof ParallelMultiInstanceBehavior) {
                    ParallelMultiInstanceBehavior behavior = (ParallelMultiInstanceBehavior) userTask.getBehavior();
                    if (behavior != null && behavior.getCollectionExpression() != null) {
                        System.out.println("当前节点是会签");
                    }
                }else {
                    System.out.println("当前节点不是会签");
                }
                System.out.println("排他网关的下一节点是UserTask: id=" + targetFlowElement.getId() + ",name=" + targetFlowElement.getName());
                System.out.println("排他网关的下一节点CandidateUsers候选者"+ ((UserTask) targetFlowElement).getCandidateUsers());
                if (BehaviorFlag()----自己去前边判断) { // 会签的候选用户Key
                	// 判断会签用户的用户提交人的List
                    MultiInstanceLoopCharacteristics a = (((UserTask) targetFlowElement).getLoopCharacteristics());
                    // "获取会签的collection,候选人List"
                    String = nextCandidateUsers(((UserTask) targetFlowElement).getLoopCharacteristics().getInputDataItem());
                } else { //不是会签
                	// 获取用户的候选人
                    String candidateUserses = ((UserTask) targetFlowElement).getCandidateUsers().get(0);
                    // 截取候选人的值
                    String candidateUsers = candidateUserses.substring(candidateUserses.indexOf("{")+1, candidateUserses.indexOf("}"));
                }
            }else if(targetFlowElement instanceof EndEvent){
                System.out.println("排他网关的下一节点是EndEvent: 结束节点");
            }else if(targetFlowElement instanceof ServiceTask){
                System.out.println("排他网关的下一节点是ServiceTask: 内部方法");
            }else if(targetFlowElement instanceof ExclusiveGateway){
                setExclusiveGateway(targetFlowElement,data);
            }else if(targetFlowElement instanceof SubProcess){
                System.out.println("排他网关的下一节点是SubProcess: 内部子流程");
            }
        }
    }

你可能感兴趣的:(后端,idea,activiti,java)