activiti封装驳回功能

一、封装驳回功能

由于activiti流程引擎不存在驳回功能所以我们需要自己封装驳回功能

 //根据流程实例ID和用户ID找到任务ID
                //Task task = taskService.createTaskQuery().taskCandidateOrAssigned(userId).processInstanceId(processInstance.getProcessInstanceId()).singleResult();
                Map<String, Object> map = basUserInfoMapper.taskCandidateOrAssigned(userCode, processInstance.getProcessInstanceId());

                //如果意见不为空则提交意见
                if (StringUtils.isNotEmpty(proposal)) {
                    taskService.addComment((String) map.get("ID_"), processInstance.getProcessInstanceId(), proposal);
                }

                //设置流程执行所需变量
                Map<String, Object> varialbes = new HashMap<>();
                varialbes.put("lczt", lczt);
                if (ObjectUtils.isEmpty(map)) {
                    actApproveInfo.setStatus(ActivitiConstants.REJECT_FAIL);
                    return actApproveInfo;
                }
                taskService.setVariable((String) map.get("ID_"), "lczt", lczt);
                //设置实际驳回人
                taskService.setAssignee((String) map.get("ID_"), userCode);

                //传入当前任务信息调用驳回接口
                Task task = taskService.createTaskQuery().taskId((String) map.get("ID_")).singleResult();
                String currActivityId = task.getTaskDefinitionKey();

                // 获取当前用户任务节点
                BpmnModel bpmnModel = processEngine.getRepositoryService().getBpmnModel(task.getProcessDefinitionId());
                org.activiti.bpmn.model.Process process = bpmnModel.getProcesses().get(0);

                List<Map<String, String>> incomeNodes = new ArrayList<>();


                String incomeNodesRecur = getIncomeNodesRecur(currActivityId, incomeNodes, process, false, processInstance.getProcessInstanceId(),
                        processEngine.getHistoryService().createHistoricTaskInstanceQuery().processInstanceId(processInstance.getProcessInstanceId())
                                .orderByHistoricTaskInstanceEndTime().desc().list().get(0).getName());
                if (StringUtils.equals(incomeNodesRecur, "success")) {
                    FlowNode currFlow = (FlowNode) bpmnModel.getMainProcess().getFlowElement(currActivityId);
                    if (currFlow == null) {
                        List<SubProcess> subProcessList = bpmnModel.getMainProcess().findFlowElementsOfType(SubProcess.class, true);
                        for (SubProcess subProcess : subProcessList) {
                            FlowElement flowElement = subProcess.getFlowElement(currActivityId);

                            if (flowElement != null) {
                                currFlow = (FlowNode) flowElement;
                                break;
                            }
                        }
                    }

                    // 记录原活动方向
                    List<SequenceFlow> oriSequenceFlows = Lists.newArrayList();
                    oriSequenceFlows.addAll(currFlow.getOutgoingFlows());

                    // 清理活动方向
                    currFlow.getOutgoingFlows().clear();

                    List<SequenceFlow> newSequenceFlows = Lists.newArrayList();

                    for (int i = 0; i < incomeNodes.size(); i++) {
                        Map<String, String> item = incomeNodes.get(i);
                        String nodeId = item.get("id");

                        // 获取目标节点
                        FlowNode target = (FlowNode) bpmnModel.getFlowElement(nodeId);

                        //如果不是同一个流程(子流程)不能驳回
                        if (!(currFlow.getParentContainer().equals(target.getParentContainer()))) {
                            continue;
                        }

                        // 建立新方向
                        SequenceFlow newSequenceFlow = new SequenceFlow();
                        String uuid = UUID.randomUUID().toString().replace("-", "");
                        newSequenceFlow.setId(uuid);
                        newSequenceFlow.setSourceFlowElement(currFlow);// 原节点
                        newSequenceFlow.setTargetFlowElement(target);// 目标节点
                        newSequenceFlows.add(newSequenceFlow);
                    }
                    ;

                    currFlow.setOutgoingFlows(newSequenceFlows);

                    // 拒接、通过、驳回指定节点
                    taskService.complete((String) map.get("ID_"));

                    basUserInfoMapper.updateDescription("驳回",(String) map.get("ID_"));
                    //恢复原方向
                    currFlow.setOutgoingFlows(oriSequenceFlows);
 /**
     * 退回到上一节点
     *
     * @param
     */
    public String getIncomeNodesRecur(String currentNodeId, List<Map<String, String>> incomeNodes, Process
            process, boolean isAll, String processInstanceId, String lastPoint) {
        try {
            FlowElement currentFlowElement = process.getFlowElement(currentNodeId);
            List<SequenceFlow> incomingFlows = null;
            if (currentFlowElement instanceof UserTask) {
                incomingFlows = ((UserTask) currentFlowElement).getIncomingFlows();
            } else if (currentFlowElement instanceof Gateway) {
                incomingFlows = ((Gateway) currentFlowElement).getIncomingFlows();
            } else if (currentFlowElement instanceof StartEvent) {
                incomingFlows = ((StartEvent) currentFlowElement).getIncomingFlows();
            }
            if (incomingFlows != null && incomingFlows.size() > 0) {
                incomingFlows.forEach(incomingFlow -> {
                    String expression = incomingFlow.getConditionExpression();
                    // 出线的上一节点
                    String sourceFlowElementID = incomingFlow.getSourceRef();
                    // 查询上一节点的信息
                    FlowElement preFlowElement = process.getFlowElement(sourceFlowElementID);

                    //用户任务
                    if (preFlowElement instanceof UserTask && lastPoint.equals(preFlowElement.getName())) {
                        Map<String, String> tempMap = new HashMap<>();
                        tempMap.put("id", preFlowElement.getId());
                        tempMap.put("name", preFlowElement.getName());
                        incomeNodes.add(tempMap);
                        if (isAll) {
                            getIncomeNodesRecur(preFlowElement.getId(), incomeNodes, process, true, processInstanceId,lastPoint);
                        }
                    }
                    //排他网关
                    else if (preFlowElement instanceof ExclusiveGateway) {
                        getIncomeNodesRecur(preFlowElement.getId(), incomeNodes, process, isAll, processInstanceId,lastPoint);
                    }
                    //并行网关
                    else if (preFlowElement instanceof ParallelGateway) {
                        if (StringUtils.isNotEmpty(processInstanceId)) {
                            List<SequenceFlow> outgoingFlows = ((ParallelGateway) preFlowElement).getOutgoingFlows().stream().filter(a -> a.getTargetRef().hashCode() != currentNodeId.hashCode()).collect(Collectors.toList());
                            for (SequenceFlow sequenceFlow : outgoingFlows) {
                                List<Task> list = taskService.createTaskQuery().taskDefinitionKey(sequenceFlow.getTargetRef()).processInstanceId(processInstanceId).list();
                                taskService.complete(list.get(0).getId());
                                taskService.deleteTask(list.get(0).getId());
                            }
                        }
                        getIncomeNodesRecur(preFlowElement.getId(), incomeNodes, process, isAll, processInstanceId,lastPoint);
                    }
                });
            }
            return "success";
        } catch (Exception e) {
            return "fail";
        }
    }
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Activiti 5 中,驳回功能可以通过以下两种方式实现: 1. 使用 `exclusiveGateway` 和 `userTask` 元素:在流程中添加一个 `exclusiveGateway` 元素,将其与要驳回的目标 `userTask` 元素相连。在 `exclusiveGateway` 元素的条件中,判断是否需要驳回,如果需要驳回,则将流程实例的当前任务设置为目标 `userTask` 元素,即可实现驳回功能。 以下是一个示例流程定义文件: ```xml <?xml version="1.0" encoding="UTF-8"?> <definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:activiti="http://activiti.org/bpmn" targetNamespace="http://www.activiti.org/processdef"> <process id="process" name="Process" isExecutable="true"> <startEvent id="startEvent" name="Start"> <outgoing>flow1</outgoing> </startEvent> <userTask id="userTask1" name="Task 1"> <incoming>flow1</incoming> <outgoing>flow2</outgoing> </userTask> <exclusiveGateway id="exclusiveGateway" name="Gateway"> <incoming>flow2</incoming> <outgoing>flow3</outgoing> <outgoing>flow4</outgoing> </exclusiveGateway> <userTask id="userTask2" name="Task 2"> <incoming>flow3</incoming> <outgoing>flow5</outgoing> </userTask> <userTask id="userTask3" name="Task 3"> <incoming>flow4</incoming> <outgoing>flow6</outgoing> </userTask> <sequenceFlow id="flow1" sourceRef="startEvent" targetRef="userTask1" /> <sequenceFlow id="flow2" sourceRef="userTask1" targetRef="exclusiveGateway" /> <sequenceFlow id="flow3" sourceRef="exclusiveGateway" targetRef="userTask2"> <conditionExpression xsi:type="tFormalExpression">${rejected}</conditionExpression> </sequenceFlow> <sequenceFlow id="flow4" sourceRef="exclusiveGateway" targetRef="userTask3"> <conditionExpression xsi:type="tFormalExpression">${!rejected}</conditionExpression> </sequenceFlow> <sequenceFlow id="flow5" sourceRef="userTask2" targetRef="endEvent" /> <sequenceFlow id="flow6" sourceRef="userTask3" targetRef="endEvent" /> <endEvent id="endEvent" name="End"> <incoming>flow5</incoming> <incoming>flow6</incoming> </endEvent> </process

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值