Flowable 实现节点驳回

在Flowable开发中可能遇到某个节点审批出问题需要驳回该节点任务 遇到此问题可以参考以下代码需要自己判断只能驳回当前节点之前的节点逻辑 直接上代码!

/**
     * 节点驳回
     * @author: Lu Yang
     * @date: 2019-05-05 10:30
     * @param taskId 任务ID
     * @param targetNodeId 目标节点ID
     * @return: com.tlsq.framework.core.protocol.Result
     */
    public Result flowBackBeforeNode(String taskId, String targetNodeId) {
        
        if(StringUtil.isEmpty(taskId)) {
            return Result.failure(
                    FlowResultCode.FLOW_TASK_ID_EMPTY.getErrCode(),
                    FlowResultCode.FLOW_TASK_ID_EMPTY.getErrMsg());
        }

        try {
            //获取流程实例
            Result taskEntityWrapper = getTaskEntity(taskId);
            if(!taskEntityWrapper.isSuccess()) {
                return Result.failure(
                        FlowResultCode.FLOW_CALL_GET_TASK_ENTITY_METHOD_ERROR.getErrCode(),
                        FlowResultCode.FLOW_CALL_GET_TASK_ENTITY_METHOD_ERROR.getErrMsg());
            }
            TaskEntity taskEntity = taskEntityWrapper.getResult();
            @SuppressWarnings({ "static-access", "rawtypes" })
            Collection flowElements = operationHelper.getFlowElements(taskEntity);
            List nodeIds = new ArrayList<>();
            String nodeId = null;
            for (Object flowElement : flowElements) {
                if (flowElement instanceof UserTask) {
                    nodeId = ((UserTask) flowElement).getId();
                    if (nodeId.equals(taskEntity.getTaskDefinitionKey())) {
                        // 如果为空就默认回退到上一节点
                        if (StringUtil.isEmpty(targetNodeId)) {
                            targetNodeId = ((UserTask) flowElement).getIncomingFlows().get(0).getSourceRef();
                        }
                        break;
                    }
                    nodeIds.add(nodeId);
                }
            }

            if (!nodeIds.contains(targetNodeId)) {
                return Result.failure(
                        FlowResultCode.FLOW_OTHER_TARGET_NODE_ID_INEXISTENCE.getErrCode(),
                        FlowResultCode.FLOW_OTHER_TARGET_NODE_ID_INEXISTENCE.getErrMsg());
            }

            runtimeService.createChangeActivityStateBuilder()
                          .processInstanceId(taskEntity.getProcessInstanceId())
                          .moveActivityIdTo(taskEntity.getTaskDefinitionKey(),targetNodeId)
                          .changeState();
            
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failure(
                    FlowResultCode.FLOW_CALL_FLOW_BACK_BEFORENODE_METHOD_ERROR.getErrCode(), 
                    FlowResultCode.FLOW_CALL_FLOW_BACK_BEFORENODE_METHOD_ERROR.getErrMsg());
        }

        return Result.success();
    }

https://gitee.com/cn-luyang/luyang-cloud.git
自己整了个项目,期待小伙伴一起玩
欢迎大佬们提交PR
朋友们帮忙点个starter,感谢了

你可能感兴趣的:(Flowable 实现节点驳回)