Activit的心路历程:获取当前节点的上一节点【可能存在多个】的nodeId

时间:2024-01-27 17:04:38

在我的开发任务中,突然给我提出了一个待办任务需要获取当前任务节点上以任务节点的表单信息,刚开始搞得我有点措手不及,后来仔细是靠后,灵感一下,直接操作流程的bpmn信息就可以获取到节点信息嘛,顺着这个思路,我整理出了自己的思路:
(1)将节点大体分为两类,一类是网关节点,另外一类就是用户任务节点,使用List集合,将网关与用户任务进行分类
(2)获取上一节点,我们就需要从bpmn的连线信息入手,固定连线的targtaetRef,辨别sourceRef节点的类型,当是用户任务时,放进 List frontNodeIdlist = new ArrayList<>();,当是GateWay节点时,将targtaetRef设为网关的,继续遍历上一节点,就是跳过网关节点,只要用户任务节点

 /**
     * 获取当前节点上一节点id【可能多个节点id】
     * @param task
     * @return
     */
    private List<String> getFrontUserTaskIds(Task task){
        String nodeId=task.getTaskDefinitionKey();
        //网关集合
        List<Gateway> gateways = new ArrayList<>();
        //用户任务集合
        List<UserTask> userTasks = new ArrayList<>();

        //网关节点id
        List<String> gatewayNodelIdList = new ArrayList<>();
        //用户任务节点id
        List<String> usertaskNodelIdList = new ArrayList<>();

//        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(task.getProcessInstanceId()).singleResult();
//        String processDefinitionId = processInstance.getProcessDefinitionId();
        String processDefinitionId = task.getProcessDefinitionId();
//        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionId(processDefinitionId).singleResult();
        BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinitionId);
        List<Process> processes = bpmnModel.getProcesses();
        Process process = processes.get(0);
        Collection<FlowElement> flowElements = process.getFlowElements();
        flowElements.forEach(flowElement -> {
            if(flowElement instanceof Gateway){
                gatewayNodelIdList.add(flowElement.getId());
                gateways.add((Gateway) flowElement);
            }
            if(flowElement instanceof UserTask){
                usertaskNodelIdList.add(flowElement.getId());
                userTasks.add((UserTask) flowElement);
            }
        });

        List<String> frontNodeIdlist = new ArrayList<>();
        for (UserTask userTask : userTasks) {
            List<SequenceFlow> incomingFlows = userTask.getIncomingFlows();
            for (SequenceFlow incomingFlow : incomingFlows) {
                String sourceRef = incomingFlow.getSourceRef();
                String targetRef = incomingFlow.getTargetRef();
                if(nodeId.equals(targtaetRef)){
                    //当前任务的上一节点是网关
                    if (gatewayNodelIdList.contains(sourceRef)) {
                        for (Gateway gateway : gateways) {
                            List<SequenceFlow> incomingFlowsGateWay = gateway.getIncomingFlows();
                            for (SequenceFlow sequenceFlow : incomingFlowsGateWay) {
                                String sourceRefGateWay = sequenceFlow.getSourceRef();
                                String targetRefGateWay = sequenceFlow.getTargetRef();
                                if(sourceRef.equals(targetRefGateWay)){
                                    frontNodeIdlist.add(sourceRefGateWay);
                                }

                            }
                        }
                    }else{
                        frontNodeIdlist.add(sourceRef);
                    }
                }
            }
        }
        return frontNodeIdlist;
    }