flowable 模拟审批流条件审批

请求

ProcessDefinition processDefinition = processEngineService.getProcessDefinitionKey("实例key");
        String processDefinitionId = processDefinition.getId();
        BpmnModel bpmnModel = processEngineService.getBpmnModel(processDefinitionId);
        Process mainProcess = bpmnModel.getMainProcess();
        FlowNode currentFlowElement = (FlowNode)mainProcess.getFlowElement("执行节点ID", true);
        FlowElement nextFlowElement = ProcessNodeUtils.getNextFlowElement(variables, currentFlowElement);
if (nextFlowElement != null){
            String approveId = processDefinitionKey.substring(2);
            String nodeId = nextFlowElement.getId();
            String nodeName = nextFlowElement.getName();
           //根据自己的业务填写
    }



获取审批节点

/**
     * 根据当前节点获取下一个节点
     */
    public static FlowElement getNextFlowElement(Map<String, Object> variables, FlowElement sourceFlowElement) {
        //遇到下一个节点是UserTask就返回

        FlowElement flowElement = null;
        if (sourceFlowElement instanceof FlowNode) {
            //当前节点必须是FlowNode才做处理,比如UserTask或者GateWay
            FlowNode thisFlowNode = (FlowNode) sourceFlowElement;
            List<SequenceFlow> outgoingFlows = thisFlowNode.getOutgoingFlows();
            if (CollectionUtils.isNotEmpty(outgoingFlows)){
                if (outgoingFlows.size() == 1) {
                    //如果只有一条连接线,直接找这条连接线的出口节点,然后继续递归获得接下来的节点
                    SequenceFlow sequenceFlow = outgoingFlows.get(0);
                    FlowElement targetFlowElement = sequenceFlow.getTargetFlowElement();
                    if (targetFlowElement instanceof UserTask) {
                        flowElement = targetFlowElement;
                    } else {
                        flowElement = getNextFlowElement(variables, targetFlowElement);
                    }
                } else if (outgoingFlows.size() > 1) {
                    //如果有多条连接线,遍历连接线,找出一个连接线条件执行为True的,获得它的出口节点
                    for (SequenceFlow sequenceFlow : outgoingFlows) {
                        boolean result = true;
                        if (StringUtils.isNotBlank(sequenceFlow.getConditionExpression())) {
                            //计算连接线上的表达式
                            ProcessEngineService processEngineService = ApplicationContextHolder.getBean(ProcessEngineService.class);
                            result = processEngineService.executeCommand(new ImitateExpressionCmd(variables, sequenceFlow.getConditionExpression()));
                        }
                        if (result) {
                            FlowElement targetFlowElement = sequenceFlow.getTargetFlowElement();
                            if (targetFlowElement instanceof UserTask) {
                                flowElement = targetFlowElement;
                            } else {
                                flowElement = getNextFlowElement(variables, targetFlowElement);
                            }
                        }
                    }
                }
            }
        }
        return flowElement;
    }

条件规则


import org.flowable.common.engine.api.variable.VariableContainer;
import org.apache.commons.lang3.StringUtils;
import org.flowable.app.engine.impl.util.CommandContextUtil;
import org.flowable.common.engine.api.delegate.Expression;
import org.flowable.common.engine.impl.interceptor.Command;
import org.flowable.common.engine.impl.interceptor.CommandContext;

import java.io.Serializable;
import java.util.Map;

public class ImitateExpressionCmd implements Command<Boolean>, Serializable {

    protected Map<String, Object> variables;

    protected String exp;

    public ImitateExpressionCmd(Map<String, Object> variables, String exp) {
        this.variables = variables;
        this.exp = exp;
    }

    @Override
    public Boolean execute(CommandContext commandContext) {
        //表达式为空时,直接返回true
        if (StringUtils.isBlank(this.exp)) {
            return Boolean.TRUE;
        }
        Expression expression = CommandContextUtil.getExpressionManager().createExpression(this.exp);

        //自定义变量处理
        ProcessEmptyVariableContainer processEmptyVariableContainer = new ProcessEmptyVariableContainer(variables);

        Object value = expression.getValue(processEmptyVariableContainer);
        return value != null && "true".equals(value.toString());    }

}
class ProcessEmptyVariableContainer implements VariableContainer {

    private Map<String, Object> variables;

    public ProcessEmptyVariableContainer(Map<String, Object> variables) {
        this.variables = variables;
    }

    public void setVariables(Map<String, Object> variables) {
        this.variables = variables;
    }

    @Override
    public boolean hasVariable(String variableName) {
        return variables.containsKey(variableName);
    }

    @Override
    public Object getVariable(String variableName) {
        return variables.get(variableName);
    }

    @Override
    public void setVariable(String variableName, Object variableValue) {
        throw new UnsupportedOperationException("Empty object, no variables can be set");
    }

    @Override
    public void setTransientVariable(String variableName, Object variableValue) {
        throw new UnsupportedOperationException("Empty object, no variables can be set");
    }

    @Override
    public String getTenantId() {
        return null;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值