Flowable根据任务id、参数,预测、获取下一任务节点集合、候选用户集合、候选组集合、参与用户id集合等

4 篇文章 5 订阅
3 篇文章 0 订阅

根据任务id、参数,预测、获取下一任务节点集合、候选用户集合、候选组集合、参与用户id集合等。

List<UserTaskVo> nextUserTasks = newProcessService.getNextUserTasks(taskId, variableMap);

  1. Service

1.1 NewProcessService
package com.example.wf.service;

import com.example.wf.vo.UserTaskVo;

import java.util.List;
import java.util.Map;

public interface NewProcessService {

    /**
     * 获取el表达式的值
     * @param exp el表达式
     * @param variableMap map参数
     * @return true/false
     */
    boolean getElValue(String exp, Map<String, Object> variableMap);

    /**
     * 获取el表达式的值
     * @param processInstanceId 流程实例id
     * @param exp el表达式
     * @param variableMap map参数
     * @return true/false
     */
    boolean getElValue(String processInstanceId, String exp, Map<String, Object> variableMap);

    /**
     * 获取el表达式的值
     * @param taskId 流程任务id
     * @param exp el表达式
     * @param variableMap map参数
     * @return true/false
     */
    boolean getElValueByTaskId(String taskId, String exp, Map<String, Object> variableMap);

    /**
     * 获取skip是否跳过节点el表达式的值
     * @param exp el表达式
     * @param variableMap map参数
     * @return true 跳过节点/false 不跳过节点
     */
    boolean getSkipElValue(String exp, Map<String, Object> variableMap);

    /**
     * 根据参数,预测、获取下一任务节点集合
     *
     * @param taskId 任务id
     * @param variables 参数
     * @return 下一任务节点集合
     */
    List<UserTaskVo> getNextUserTasks(String taskId, Map<String, Object> variables);

    /**
     * 根据参数,预测、获取下一任务节点集合
     *
     * @param taskId 任务id
     * @param code 业务ID
     * @param variables 参数
     * @return 下一任务节点集合
     */
    List<UserTaskVo> getNextUserTasks(String taskId, String code, Map<String, Object> variables);

}

  1. ServiceImpl

2.1 NewProcessServiceImpl
package com.example.wf.service.impl;

import com.example.utils.CastUtils;
import com.example.utils.VariableUtils;
import com.example.wf.command.ExpressionCmd;
import com.example.wf.mapper.ActRuVariableMapper;
import com.example.wf.mapper.VUserGroupMapper;
import com.example.wf.model.VUserGroup;
import com.example.wf.service.NewProcessService;
import com.example.wf.vo.CandidateGroupVo;
import com.example.wf.vo.CandidateUserVo;
import com.example.wf.vo.UserTaskVo;
import org.apache.commons.lang.StringUtils;
import org.flowable.bpmn.model.BpmnModel;
import org.flowable.bpmn.model.EventGateway;
import org.flowable.bpmn.model.ExclusiveGateway;
import org.flowable.bpmn.model.FlowElement;
import org.flowable.bpmn.model.FlowNode;
import org.flowable.bpmn.model.InclusiveGateway;
import org.flowable.bpmn.model.ParallelGateway;
import org.flowable.bpmn.model.SequenceFlow;
import org.flowable.bpmn.model.UserTask;
import org.flowable.engine.HistoryService;
import org.flowable.engine.IdentityService;
import org.flowable.engine.ManagementService;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.idm.api.Group;
import org.flowable.idm.api.User;
import org.flowable.task.api.Task;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.Sqls;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class NewProcessServiceImpl implements NewProcessService {

    protected Logger log = LoggerFactory.getLogger(super.getClass());

    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private HistoryService historyService;
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private ProcessEngine processEngine;
    @Autowired
    private TaskService taskService;
    @Autowired
    private IdentityService identityService;

    @Autowired
    private VUserGroupMapper vUserGroupMapper;

    @Autowired
    private ActRuVariableMapper actRuVariableMapper;

    @Override
    public boolean getElValue(String exp, Map<String, Object> variableMap) {
        return this.getElValue(null, exp, variableMap);
    }

    @Override
    public boolean getElValue(String processInstanceId, String exp, Map<String, Object> variableMap) {
        if (StringUtils.isBlank(exp)) {
            return true;
        }

        ManagementService managementService = processEngine.getManagementService();
        boolean flag = managementService.executeCommand(new ExpressionCmd(processEngine, processInstanceId, exp, variableMap));

        //删除垃圾数据
        this.deleteException();
        return flag;
    }

    @Override
    public boolean getElValueByTaskId(String taskId, String exp, Map<String, Object> variableMap) {
        if (StringUtils.isBlank(exp)) {
            return true;
        }

        Map<String, Object> variables = taskService.getVariables(taskId);
        variables.putAll(variableMap);

        ManagementService managementService = processEngine.getManagementService();
        boolean flag = managementService.executeCommand(new ExpressionCmd(exp, variables));

        //删除垃圾数据
        this.deleteException();
        return flag;
    }

    @Override
    public boolean getSkipElValue(String exp, Map<String, Object> variableMap) {
        //表达式为空,节点不跳过
        if (StringUtils.isBlank(exp)) {
            return false;
        }

        //skip:true 跳过节点/false 不跳过节点
        boolean skip = false;
        Map<String, Object> variables = new HashMap<>(variableMap);
        Object flowableSkipExpressionEnabled = variables.get("_FLOWABLE_SKIP_EXPRESSION_ENABLED");
        //流程参数含有_FLOWABLE_SKIP_EXPRESSION_ENABLED,且为true时,表示FLOWABLE跳过表达式已启用
        if (null != flowableSkipExpressionEnabled && "true".equals(flowableSkipExpressionEnabled.toString())) {
            //返回true,表示跳过该任务节点;
            ManagementService managementService = processEngine.getManagementService();
            skip = managementService.executeCommand(new ExpressionCmd(exp, variables));

            //删除垃圾数据
            this.deleteException();
        }

        return skip;
    }

    public void deleteException(){
        //删除垃圾数据
        actRuVariableMapper.deleteRuVariableException();
        actRuVariableMapper.deleteHiVariableException();
    }

    /**
     * 获取下一任务节点集合
     *
     * @param taskId 任务id
     * @param variables 参数
     * @return 下一任务节点集合
     */
    @Override
    public List<UserTaskVo> getNextUserTasks(String taskId, Map<String, Object> variables) {
        return this.getNextUserTasks(taskId, null, variables);
    }

    @Override
    public List<UserTaskVo> getNextUserTasks(String taskId, String code, Map<String, Object> variables) {
        if (StringUtils.isBlank(taskId)) {
            return new ArrayList<>();
        }

        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
        if (null == task) {
            return new ArrayList<>();
        }

        Map<String, Object> variableMap = taskService.getVariables(task.getId());
        variableMap.putAll(variables);

        String procId = "";
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .processDefinitionId(task.getProcessDefinitionId()).singleResult();
        if (null != processDefinition) {
            procId = processDefinition.getKey();
        }

        List<UserTaskVo> userTasks = new ArrayList<>();

        List<FlowElement> nextFlowNodes = this.getNextFlowNodes(task, variableMap);
        for (FlowElement nextFlowNode : nextFlowNodes) {
            List<String> candidateUserIdList = new ArrayList<>(); //参与用户id
            List<CandidateUserVo> candidateUsers = new ArrayList<>(); //候选用户
            List<CandidateGroupVo> candidateGroups = new ArrayList<>(); //候选组

            UserTask flowElement = (UserTask) nextFlowNode;
            String flowId = flowElement.getId();
            String flowName = flowElement.getName();
            String assignee = flowElement.getAssignee();

            if (StringUtils.isNotBlank(assignee)) {
                // 节点分配给了具体参与者
                String key = VariableUtils.formatVariableName(assignee);
                Object userIdStr = variableMap.get(key);
                if (null != userIdStr && StringUtils.isNotBlank(userIdStr.toString())) {
                    // 节点分配给了具体参与者
                    String userId = userIdStr.toString();
                    String userName = "";
                    User user = identityService.createUserQuery().userId(userId).singleResult();
                    if (null != user) {
                        userName = user.getDisplayName();
                    }
                    candidateUserIdList.add(userId);
                    candidateUsers.add(new CandidateUserVo(userId, userName));
                }

            } else {

                // 候选用户
                for (String candidateUser : flowElement.getCandidateUsers()) {
                    String key = VariableUtils.formatVariableName(candidateUser);
                    Object userIdStr = variableMap.get(key);
                    if (null != userIdStr && StringUtils.isNotBlank(userIdStr.toString())) {
                        //value可能是集合
                        List<String> userIds = CastUtils.castObjToStringList(userIdStr);
                        for (String userId : userIds) {
                            String userName = "";
                            User user = identityService.createUserQuery().userId(userId).singleResult();
                            if (null != user) {
                                userName = user.getDisplayName();
                            }
                            candidateUserIdList.add(userId);
                            candidateUsers.add(new CandidateUserVo(userId, userName));
                        }
                    }
                }

                //候选组
                for (String candidateGroup : flowElement.getCandidateGroups()) {
                    String key = VariableUtils.formatVariableName(candidateGroup);
                    Object groupIdStr = variableMap.get(key);
                    if (null != groupIdStr && StringUtils.isNotBlank(groupIdStr.toString())) {
                        //value可能是集合
                        List<String> groupIds = CastUtils.castObjToStringList(groupIdStr);
                        for (String groupId : groupIds) {
                            String groupName = "";
                            Group group = this.identityService.createGroupQuery().groupId(groupId).singleResult();
                            if (null != group) {
                                groupName = group.getName();
                            }

                            candidateGroups.add(new CandidateGroupVo(groupId, groupName));

                            // 查询候选组用户id
                            Example.Builder builderGroup = new Example.Builder(VUserGroup.class);
                            // 条件
                            Sqls sqlGroup = Sqls.custom();
                            sqlGroup.andEqualTo("groupId", groupId);
                            builderGroup.where(sqlGroup);
                            List<VUserGroup> vUserGroupList = vUserGroupMapper.selectByExample(builderGroup.build());
                            if (!CollectionUtils.isEmpty(vUserGroupList)) {
                                for (VUserGroup vUserGroup : vUserGroupList) {
                                    candidateUserIdList.add(vUserGroup.getId());
                                }
                            }
                        }
                    }
                }
            }

            UserTaskVo userTask = new UserTaskVo(null, flowId, flowName, procId, code,
                    candidateUserIdList, candidateUsers, candidateGroups);
            userTasks.add(userTask);
        }

        return userTasks;
    }

    /**
     * 获取下一任务节点集合
     *
     * @param taskId 任务id
     * @param variables 参数
     * @return 下一任务节点集合
     */
    public List<FlowElement> getNextFlowNodes(String taskId, Map<String, Object> variables) {
        if (StringUtils.isBlank(taskId)) {
            return new ArrayList<>();
        }

        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
        if (null == task) {
            return new ArrayList<>();
        }

        Map<String, Object> variableMap = taskService.getVariables(task.getId());
        variableMap.putAll(variables);

        return this.getNextFlowNodes(task, variableMap);
    }

    /**
     * 获取下一任务节点集合
     *
     * @param task 当前任务
     * @param variables 参数
     * @return 下一任务节点集合
     */
    public List<FlowElement> getNextFlowNodes(Task task, Map<String, Object> variables) {
        if (null == task) {
            return new ArrayList<>();
        }

        // 当前审批节点
        String currentActivityId = task.getTaskDefinitionKey(); //当前环节id
        BpmnModel bpmnModel = repositoryService.getBpmnModel(task.getProcessDefinitionId());

        //复制一份
        List<FlowElement> flowElements = new ArrayList<>(bpmnModel.getMainProcess().getFlowElements());
        List<FlowElement> nextElements = new ArrayList<>();

        FlowNode flowNode = (FlowNode) bpmnModel.getFlowElement(currentActivityId);
        // 输出连线
        List<SequenceFlow> outFlows = flowNode.getOutgoingFlows();

        FlowElement targetElement;
        List<SequenceFlow> sequenceFlows = this.getSequenceFlows(variables, outFlows);
        for (SequenceFlow sequenceFlow : sequenceFlows) {
            targetElement = this.getFlowElement(flowElements, sequenceFlow.getTargetRef());
            this.getNextElementList(nextElements, flowElements, targetElement, variables);
        }
        return nextElements;
    }

    /**
     * 获取下一环节任务节点集合
     */
    private void getNextElementList(List<FlowElement> nextElements, Collection<FlowElement> flowElements, FlowElement curFlowElement, Map<String, Object> variableMap) {
        if (null == curFlowElement) {
            return;
        }

        // 任务节点
        if (curFlowElement instanceof UserTask) {
            this.dueUserTaskElement(nextElements, flowElements, curFlowElement, variableMap);
            return;
        }
        // 排他网关
        if (curFlowElement instanceof ExclusiveGateway) {
            this.dueExclusiveGateway(nextElements, flowElements, curFlowElement, variableMap);
            return;
        }
        // 并行网关
        if (curFlowElement instanceof ParallelGateway) {
            this.dueParallelGateway(nextElements, flowElements, curFlowElement, variableMap);
        }
        // 包含网关
        if (curFlowElement instanceof InclusiveGateway) {
            this.dueInclusiveGateway(nextElements, flowElements, curFlowElement, variableMap);
        }
        // 事件网关
        if (curFlowElement instanceof EventGateway) {
            this.dueEventGateway(nextElements, flowElements, curFlowElement, variableMap);
        }
    }

    /**
     * 任务节点
     */
    private void dueUserTaskElement(List<FlowElement> nextElements, Collection<FlowElement> flowElements, FlowElement curFlowElement, Map<String, Object> variableMap) {
        UserTask userTask = (UserTask) curFlowElement;
        //skip:true 跳过节点/false 不跳过节点
        boolean skip = this.getSkipElValue(userTask.getSkipExpression(), variableMap);
        if (!skip) {
            //不跳过节点,
            nextElements.add(curFlowElement);
        } else {

            //跳过节点,查询下一节点
            List<SequenceFlow> outgoingFlows = userTask.getOutgoingFlows();
            if (outgoingFlows.size() > 0) {
                String targetRef = outgoingFlows.get(0).getTargetRef();
                if (outgoingFlows.size() > 1) {
                    // 找到表达式成立的sequenceFlow
                    SequenceFlow sequenceFlow = getSequenceFlow(variableMap, outgoingFlows);
                    targetRef = sequenceFlow.getTargetRef();
                }
                // 根据ID找到FlowElement
                FlowElement targetElement = this.getFlowElement(flowElements, targetRef);
                this.getNextElementList(nextElements, flowElements, targetElement, variableMap);
            }
        }
    }

    /**
     * 排他网关
     */
    private void dueExclusiveGateway(List<FlowElement> nextElements, Collection<FlowElement> flowElements, FlowElement curFlowElement, Map<String, Object> variableMap) {
        // 获取符合条件的sequenceFlow的目标FlowElement
        List<SequenceFlow> exclusiveGatewayOutgoingFlows = ((ExclusiveGateway) curFlowElement).getOutgoingFlows();
        flowElements.remove(curFlowElement);
        // 找到表达式成立的sequenceFlow
        SequenceFlow sequenceFlow = this.getSequenceFlow(variableMap, exclusiveGatewayOutgoingFlows);
        // 根据ID找到FlowElement
        FlowElement targetElement = this.getFlowElement(flowElements, sequenceFlow.getTargetRef());
        this.getNextElementList(nextElements, flowElements, targetElement, variableMap);
    }

    /**
     * 并行网关
     */
    private void dueParallelGateway(List<FlowElement> nextElements, Collection<FlowElement> flowElements, FlowElement curFlowElement, Map<String, Object> variableMap) {
        FlowElement targetElement;
        List<SequenceFlow> parallelGatewayOutgoingFlows = ((ParallelGateway) curFlowElement).getOutgoingFlows();
        for (SequenceFlow sequenceFlow : parallelGatewayOutgoingFlows) {
            targetElement = this.getFlowElement(flowElements, sequenceFlow.getTargetRef());
            this.getNextElementList(nextElements, flowElements, targetElement, variableMap);
        }
    }

    /**
     * 包含网关
     */
    private void dueInclusiveGateway(List<FlowElement> nextElements, Collection<FlowElement> flowElements, FlowElement curFlowElement, Map<String, Object> variableMap) {
        FlowElement targetElement;
        List<SequenceFlow> inclusiveGatewayOutgoingFlows = ((InclusiveGateway) curFlowElement).getOutgoingFlows();
        List<SequenceFlow> sequenceFlows = this.getSequenceFlows(variableMap, inclusiveGatewayOutgoingFlows);
        for (SequenceFlow sequenceFlow : sequenceFlows) {
            targetElement = this.getFlowElement(flowElements, sequenceFlow.getTargetRef());
            this.getNextElementList(nextElements, flowElements, targetElement, variableMap);
        }
    }

    /**
     * 事件网关
     */
    private void dueEventGateway(List<FlowElement> nextElements, Collection<FlowElement> flowElements, FlowElement curFlowElement, Map<String, Object> variableMap) {
    }

    /**
     * 根据ID找到FlowElement
     * @param flowElements 流程节点集合
     * @param targetRef id
     * @return FlowElement
     */
    private FlowElement getFlowElement(Collection<FlowElement> flowElements, String targetRef) {
        return flowElements.stream().filter(flowElement -> targetRef.equals(flowElement.getId())).findFirst().orElse(null);
    }

    /**
     * 根据传入的变量,计算出表达式成立的那一条SequenceFlow
     *
     * @param variableMap   变量map
     * @param outgoingFlows 输出流
     * @return SequenceFlow连线
     */
    private SequenceFlow getSequenceFlow(Map<String, Object> variableMap, List<SequenceFlow> outgoingFlows) {
        List<SequenceFlow> sequenceFlows = this.getSequenceFlows(variableMap, outgoingFlows);
        if (null != sequenceFlows && sequenceFlows.size() > 0) {
            return sequenceFlows.get(0);
        }

        return outgoingFlows.get(0);
    }

    /**
     * 根据传入的变量,计算出表达式成立的SequenceFlow连线集合
     *
     * @param variableMap   变量map
     * @param outgoingFlows 输出流
     * @return SequenceFlow连线集合
     */
    private List<SequenceFlow> getSequenceFlows(Map<String, Object> variableMap, List<SequenceFlow> outgoingFlows) {
        List<SequenceFlow> sequenceFlows;
        sequenceFlows = outgoingFlows.stream().filter(item -> {
            try {
                return this.getElValue(item.getConditionExpression(), variableMap);
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }).collect(Collectors.toList());
        return sequenceFlows;
    }

}

  1. command

  1. ExpressionCmd
package com.example.wf.command;

import org.apache.commons.lang.StringUtils;
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 org.flowable.engine.ProcessEngine;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.impl.persistence.entity.ExecutionEntity;
import org.flowable.engine.impl.persistence.entity.ExecutionEntityImpl;
import org.flowable.variable.service.impl.util.CommandContextUtil;

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

/**
 * 表达式
 */
public class ExpressionCmd implements Command<Boolean>, Serializable {

    protected ProcessEngine processEngine;

    protected String processInstanceId;

    protected String exp;

    protected Map<String, Object> variableMap;

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

    public ExpressionCmd(ProcessEngine processEngine, String processInstanceId, String exp, Map<String, Object> variableMap) {
        this.processEngine = processEngine;
        this.processInstanceId = processInstanceId;
        this.exp = exp;
        this.variableMap = variableMap;
    }

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

        //复制一份
        Map<String, Object> variables = new HashMap<>(this.variableMap);
        Expression expression = CommandContextUtil.getExpressionManager().createExpression(this.exp);

        //流程实例id不为空
        if (StringUtils.isNotBlank(this.processInstanceId)) {
            RuntimeService runtimeService = this.processEngine.getRuntimeService();
            ExecutionEntity processExecutionEntity = (ExecutionEntity) runtimeService.createProcessInstanceQuery().processInstanceId(this.processInstanceId).includeProcessVariables().singleResult();
            if (null != processExecutionEntity) {
                //流程实例参数
                Map<String, Object> processVariables = processExecutionEntity.getVariables();
                if (null != processVariables) {
                    processVariables.putAll(this.variableMap);

                    variables = processVariables;
                }
            }
        }

        ExecutionEntity executionEntity = new ExecutionEntityImpl();
        executionEntity.setVariables(variables);

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

}

  1. mapper

  1. ActRuVariableMapper
package com.example.wf.mapper;

import com.example.common.base.MyMapper;
import com.example.wf.model.ActRuVariable;

public interface ActRuVariableMapper extends MyMapper<ActRuVariable> {

    /**
     * 删除运行变量(异常)
     */
    int deleteRuVariableException();

    /**
     * 删除历史变量(异常)
     */
    int deleteHiVariableException();

}

  1. mapperXML

  1. ActRuVariableMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.wf.mapper.ActRuVariableMapper">
    <resultMap id="BaseResultMap" type="com.example.wf.model.ActRuVariable">
        <!--
          WARNING - @mbg.generated
        -->
        <id column="ID_" jdbcType="VARCHAR" property="id"/>
        <result column="REV_" jdbcType="INTEGER" property="rev"/>
        <result column="TYPE_" jdbcType="VARCHAR" property="type"/>
        <result column="NAME_" jdbcType="VARCHAR" property="name"/>
        <result column="EXECUTION_ID_" jdbcType="VARCHAR" property="executionId"/>
        <result column="PROC_INST_ID_" jdbcType="VARCHAR" property="procInstId"/>
        <result column="TASK_ID_" jdbcType="VARCHAR" property="taskId"/>
        <result column="SCOPE_ID_" jdbcType="VARCHAR" property="scopeId"/>
        <result column="SUB_SCOPE_ID_" jdbcType="VARCHAR" property="subScopeId"/>
        <result column="SCOPE_TYPE_" jdbcType="VARCHAR" property="scopeType"/>
        <result column="BYTEARRAY_ID_" jdbcType="VARCHAR" property="bytearrayId"/>
        <result column="DOUBLE_" jdbcType="DOUBLE" property="double_"/>
        <result column="LONG_" jdbcType="BIGINT" property="long_"/>
        <result column="TEXT_" jdbcType="VARCHAR" property="text_"/>
        <result column="TEXT2_" jdbcType="VARCHAR" property="text2_"/>
    </resultMap>

    <delete id="deleteRuVariableException">
        delete from ACT_RU_VARIABLE where EXECUTION_ID_ is null
    </delete>

    <delete id="deleteHiVariableException">
        delete from act_hi_varinst where EXECUTION_ID_ is null
    </delete>

</mapper>

  1. model

  1. ActRuVariable
package com.example.wf.model;

import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
import java.math.BigDecimal;

/**
 * 流程运行变量
 */
@Table(name = "act_ru_variable")
public class ActRuVariable implements Serializable {

    @Id
    @Column(name = "ID_")
    private String id;

    @Column(name = "REV_")
    private BigDecimal rev;

    @Column(name = "TYPE_")
    private String type;

    @Column(name = "NAME_")
    private String name;

    @Column(name = "EXECUTION_ID_")
    private String executionId;

    @Column(name = "PROC_INST_ID_")
    private String procInstId;

    @Column(name = "TASK_ID_")
    private String taskId;

    @Column(name = "SCOPE_ID_")
    private String scopeId;

    @Column(name = "SUB_SCOPE_ID_")
    private String subScopeId;

    @Column(name = "SCOPE_TYPE_")
    private String scopeType;

    @Column(name = "BYTEARRAY_ID_")
    private String bytearrayId;

    @Column(name = "DOUBLE_")
    private Double double_;

    @Column(name = "LONG_")
    private Long long_;

    @Column(name = "TEXT_")
    private String text_;

    @Column(name = "TEXT2_")
    private String text2_;

    /**
     * @return ID_
     */
    public String getId() {
        return id;
    }

    /**
     * @param id
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * @return REV_
     */
    public BigDecimal getRev() {
        return rev;
    }

    /**
     * @param rev
     */
    public void setRev(BigDecimal rev) {
        this.rev = rev;
    }

    /**
     * @return TYPE_
     */
    public String getType() {
        return type;
    }

    /**
     * @param type
     */
    public void setType(String type) {
        this.type = type;
    }

    /**
     * @return NAME_
     */
    public String getName() {
        return name;
    }

    /**
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return EXECUTION_ID_
     */
    public String getExecutionId() {
        return executionId;
    }

    /**
     * @param executionId
     */
    public void setExecutionId(String executionId) {
        this.executionId = executionId;
    }

    /**
     * @return PROC_INST_ID_
     */
    public String getProcInstId() {
        return procInstId;
    }

    /**
     * @param procInstId
     */
    public void setProcInstId(String procInstId) {
        this.procInstId = procInstId;
    }

    /**
     * @return TASK_ID_
     */
    public String getTaskId() {
        return taskId;
    }

    /**
     * @param taskId
     */
    public void setTaskId(String taskId) {
        this.taskId = taskId;
    }

    /**
     * @return SCOPE_ID_
     */
    public String getScopeId() {
        return scopeId;
    }

    /**
     * @param scopeId
     */
    public void setScopeId(String scopeId) {
        this.scopeId = scopeId;
    }

    /**
     * @return SUB_SCOPE_ID_
     */
    public String getSubScopeId() {
        return subScopeId;
    }

    /**
     * @param subScopeId
     */
    public void setSubScopeId(String subScopeId) {
        this.subScopeId = subScopeId;
    }

    /**
     * @return SCOPE_TYPE_
     */
    public String getScopeType() {
        return scopeType;
    }

    /**
     * @param scopeType
     */
    public void setScopeType(String scopeType) {
        this.scopeType = scopeType;
    }

    /**
     * @return BYTEARRAY_ID_
     */
    public String getBytearrayId() {
        return bytearrayId;
    }

    /**
     * @param bytearrayId
     */
    public void setBytearrayId(String bytearrayId) {
        this.bytearrayId = bytearrayId;
    }

    /**
     * @return DOUBLE_
     */
    public Double getDouble_() {
        return double_;
    }

    /**
     * @param double_
     */
    public void setDouble_(Double double_) {
        this.double_ = double_;
    }

    /**
     * @return LONG_
     */
    public Long getLong_() {
        return long_;
    }

    /**
     * @param long_
     */
    public void setLong_(Long long_) {
        this.long_ = long_;
    }

    /**
     * @return TEXT_
     */
    public String getText_() {
        return text_;
    }

    /**
     * @param text_
     */
    public void setText_(String text_) {
        this.text_ = text_;
    }

    /**
     * @return TEXT2_
     */
    public String getText2_() {
        return text2_;
    }

    /**
     * @param text2_
     */
    public void setText2_(String text2_) {
        this.text2_ = text2_;
    }
}
  1. Tasklist
package com.example.wf.model;

import javax.persistence.Column;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Set;

@Table(name = "v_tasklist")
public class Tasklist implements Serializable {
    private static final long serialVersionUID = 1L;
    @Column(name = "TASK_ID")
    private String taskId;
    @Column(name = "PROC_INST_ID")
    private String procInstId;
    @Column(name = "ACT_ID")
    private String actId;
    @Column(name = "ACT_NAME")
    private String actName;
    @Column(name = "ASSIGNEE")
    private String assignee;
    @Column(name = "DELEGATION_ID")
    private String delegationId;
    @Column(name = "DESCRIPTION")
    private String description;
    @Column(name = "CREATE_TIME")
    private Date createTime;
    @Column(name = "DUE_DATE")
    private Date dueDate;
    @Column(name = "CANDIDATE")
    private String candidate;
    @Column(name = "PROC_ID")
    private String procId;
    @Column(name = "CODE")
    private String code;
    @Column(name = "SUSPENSION_STATE")
    private Integer suspensionState;
    @Transient
    private Integer taskCount;
    @Transient
    private Set<String> authProjects;
    @Transient
    private Set<String> noAuthProjects;
    @Transient
    private List<Object> businessList;

    public Set<String> getAuthProjects() {
        return this.authProjects;
    }

    public void setAuthProjects(Set<String> authProjects) {
        this.authProjects = authProjects;
    }

    public Set<String> getNoAuthProjects() {
        return this.noAuthProjects;
    }

    public void setNoAuthProjects(Set<String> noAuthProjects) {
        this.noAuthProjects = noAuthProjects;
    }

    public Integer getTaskCount() {
        return this.taskCount;
    }

    public void setTaskCount(Integer taskCount) {
        this.taskCount = taskCount;
    }

    public String getCode() {
        return this.code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getTaskId() {
        return this.taskId;
    }

    public void setTaskId(String taskId) {
        this.taskId = taskId;
    }

    public String getProcInstId() {
        return this.procInstId;
    }

    public void setProcInstId(String procInstId) {
        this.procInstId = procInstId;
    }

    public String getActId() {
        return this.actId;
    }

    public void setActId(String actId) {
        this.actId = actId;
    }

    public String getActName() {
        return this.actName;
    }

    public void setActName(String actName) {
        this.actName = actName;
    }

    public String getAssignee() {
        return this.assignee;
    }

    public void setAssignee(String assignee) {
        this.assignee = assignee;
    }

    public String getDelegationId() {
        return this.delegationId;
    }

    public void setDelegationId(String delegationId) {
        this.delegationId = delegationId;
    }

    public String getDescription() {
        return this.description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Date getCreateTime() {
        return this.createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getDueDate() {
        return this.dueDate;
    }

    public void setDueDate(Date dueDate) {
        this.dueDate = dueDate;
    }

    public String getCandidate() {
        return this.candidate;
    }

    public void setCandidate(String candidate) {
        this.candidate = candidate;
    }

    public String getProcId() {
        return this.procId;
    }

    public void setProcId(String procId) {
        this.procId = procId;
    }

    public Integer getSuspensionState() {
        return this.suspensionState;
    }

    public void setSuspensionState(Integer suspensionState) {
        this.suspensionState = suspensionState;
    }

    public List<Object> getBusinessList() {
        return this.businessList;
    }

    public void setBusinessList(List<Object> businessList) {
        this.businessList = businessList;
    }
}

VUserGroup

package com.example.wf.model;

import javax.persistence.Column;
import javax.persistence.Table;
import java.io.Serializable;

@Table(name = "v_user_group")
public class VUserGroup implements Serializable {
    private static final long serialVersionUID = 1L;
    @Column(name = "id")
    private String id;
    @Column(name = "user_name")
    private String userName;
    @Column(name = "group_id")
    private String groupId;
    @Column(name = "group_name")
    private String groupName;
    private String type;

    public VUserGroup() {
    }

    public String getId() {
        return this.id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getUserName() {
        return this.userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getGroupId() {
        return this.groupId;
    }

    public void setGroupId(String groupId) {
        this.groupId = groupId;
    }

    public String getGroupName() {
        return this.groupName;
    }

    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }

    public String getType() {
        return this.type;
    }

    public void setType(String type) {
        this.type = type;
    }
}

  1. vo

  1. CandidateGroupVo
package com.example.wf.vo;

import java.io.Serializable;

@SuppressWarnings("serial")
public class CandidateGroupVo implements Serializable {

    private String groupId; //候选组

    private String groupName; //候选组

    public CandidateGroupVo() {
        super();
    }

    public CandidateGroupVo(String groupId, String groupName) {
        super();
        this.groupId = groupId;
        this.groupName = groupName;
    }

    public String getGroupId() {
        return groupId;
    }

    public void setGroupId(String groupId) {
        this.groupId = groupId;
    }

    public String getGroupName() {
        return groupName;
    }

    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }

}
  1. CandidateUserVo
package com.example.wf.vo;

import java.io.Serializable;

@SuppressWarnings("serial")
public class CandidateUserVo implements Serializable {

    private String userId; //候选用户

    private String userName; //候选用户

    public CandidateUserVo() {
        super();
    }

    public CandidateUserVo(String userId, String userName) {
        super();
        this.userId = userId;
        this.userName = userName;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

}
  1. UserTaskVo
package com.example.wf.vo;

import com.example.wf.model.Tasklist;
import org.springframework.beans.BeanUtils;

import javax.persistence.Transient;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

@SuppressWarnings("serial")
public class UserTaskVo extends Tasklist {

    /**
     * 拼接任务数量(0)
     */
    @Transient
    private String actConcatName;

    @Transient
    private Long menuId;

    @Transient
    private List<String> candidateUserIdList; //参与用户id

    @Transient
    private List<CandidateUserVo> candidateUsers; //候选用户

    @Transient
    private List<CandidateGroupVo> candidateGroups; //候选组

    public UserTaskVo() {
        super();
    }

    public UserTaskVo(Tasklist tasklist) {
        super();
        BeanUtils.copyProperties(tasklist, this);
        this.actConcatName = this.getActName() + "(" + this.getTaskCount() + ")";
    }

    public UserTaskVo(String taskId, String actId, String actName, String procId, List<CandidateUserVo> candidateUsers, List<CandidateGroupVo> candidateGroups) {
        this(taskId, actId, actName, procId, null, candidateUsers, candidateGroups);
    }

    public UserTaskVo(String taskId, String actId, String actName, String procId, String code, List<CandidateUserVo> candidateUsers, List<CandidateGroupVo> candidateGroups) {
        this(taskId, actId, actName, procId, code, null, candidateUsers, candidateGroups);
    }

    public UserTaskVo(String taskId, String actId, String actName, String procId, String code, List<String> candidateUserIdList, List<CandidateUserVo> candidateUsers, List<CandidateGroupVo> candidateGroups) {
        super();
        this.setTaskId(taskId);
        this.setActId(actId);
        this.setActName(actName);
        this.setProcId(procId);
        this.setCode(code);

        //去重
        if (null != candidateUserIdList && candidateUserIdList.size() > 0) {
            candidateUserIdList = candidateUserIdList.stream().distinct().collect(Collectors.toList());
        }
        if (null != candidateUsers && candidateUsers.size() > 0) {
            candidateUsers = candidateUsers.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(CandidateUserVo::getUserId))), ArrayList::new));
        }
        if (null != candidateGroups && candidateGroups.size() > 0) {
            candidateGroups = candidateGroups.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(CandidateGroupVo::getGroupId))), ArrayList::new));
        }
        this.candidateUserIdList = candidateUserIdList;
        this.candidateUsers = candidateUsers;
        this.candidateGroups = candidateGroups;
    }

    public String getActConcatName() {
        return actConcatName;
    }

    public void setActConcatName(String actConcatName) {
        this.actConcatName = actConcatName;
    }

    public Long getMenuId() {
        return menuId;
    }

    public void setMenuId(Long menuId) {
        this.menuId = menuId;
    }

    public List<String> getCandidateUserIdList() {
        return candidateUserIdList;
    }

    public void setCandidateUserIdList(List<String> candidateUserIdList) {
        //去重
        if (null != candidateUserIdList && candidateUserIdList.size() > 0) {
            candidateUserIdList = candidateUserIdList.stream().distinct().collect(Collectors.toList());
        }
        this.candidateUserIdList = candidateUserIdList;
    }

    public List<CandidateUserVo> getCandidateUsers() {
        return candidateUsers;
    }

    public void setCandidateUsers(List<CandidateUserVo> candidateUsers) {
        //去重
        if (null != candidateUsers && candidateUsers.size() > 0) {
            candidateUsers = candidateUsers.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(CandidateUserVo::getUserId))), ArrayList::new));
        }
        this.candidateUsers = candidateUsers;
    }

    public List<CandidateGroupVo> getCandidateGroups() {
        return candidateGroups;
    }

    public void setCandidateGroups(List<CandidateGroupVo> candidateGroups) {
        //去重
        if (null != candidateGroups && candidateGroups.size() > 0) {
            candidateGroups = candidateGroups.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(CandidateGroupVo::getGroupId))), ArrayList::new));
        }
        this.candidateGroups = candidateGroups;
    }

}

  1. utils

  1. CastUtils
package com.example.utils;

import java.util.ArrayList;
import java.util.List;

/**
 * 转换工具类
 */
public class CastUtils {

    /**
     * 将obj转换为字符串列表
     * @param obj obj
     * @return 字符串列表
     */
    public static List<String> castObjToStringList(Object obj) {
        if (null == obj) {
            return null;
        }

        List<String> result = new ArrayList<>();
        if (obj instanceof List<?>) {
            for (Object o : (List<?>) obj) {
                result.add(o.toString());
            }
        } else if (obj instanceof String) {
            result.add(obj.toString());
        }

        return result;
    }

    /**
     * obj转list
     * @param obj obj
     * @param clazz clazz
     * @param <T> T
     * @return T
     */
    public static <T> List<T> castList(Object obj, Class<T> clazz) {
        if (null == obj) {
            return null;
        }

        if (obj instanceof List<?>) {
            List<T> result = new ArrayList<>();
            for (Object o : (List<?>) obj) {
                result.add(clazz.cast(o));
            }
            return result;
        }

        return null;
    }

}
  1. VariableUtils
package com.example.utils;

import com.alibaba.fastjson.util.TypeUtils;
import org.apache.commons.lang.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * 变量工具类
 */
public class VariableUtils {

    /**
     * 格式化变量名,去除${}符号
     * @param variableName 变量名
     * @return 变量名
     */
    public static String formatVariableName(String variableName) {
        variableName = variableName.replaceAll("[${}]", "").trim();
        return variableName;
    }

    /**
     * 获取格式化后的变量值
     * @param variableType 变量类型
     * @param variableValue 变量值
     * @return 格式化后的变量值
     */
    public static Object getFormatVariableValue(String variableType, String variableValue) {
        if (StringUtils.isBlank(variableType)) {
            throw new RuntimeException("变量类型为空!");
        }
        if (!"java.lang.String".equals(variableType) && StringUtils.isBlank(variableValue)) {
            throw new RuntimeException("变量值为空!");
        }

        Object value;
        try {
            switch (variableType) {
                case "java.lang.String": //字符串
                    value = variableValue;
                    break;
                case "java.lang.Integer": //整数
                    value = new Integer(variableValue);
                    break;
                case "java.math.BigDecimal": //金额
                    value = new BigDecimal(variableValue);
                    break;
                case "java.util.ArrayList": //列表
                    String[] split = variableValue.split(",");
                    value = new ArrayList<>(Arrays.asList(split));
                    break;
                default:
                    // 不是以上的类型!但愿可以通过反射注入吧!
                    Class<?> aClass = Class.forName(variableType);
//                    Constructor<?> constructor = aClass.getConstructor(String.class);
//                    value = constructor.newInstance(variableValue);

                    value = TypeUtils.castToJavaBean(variableValue, aClass);
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();

            throw new RuntimeException(e.getMessage(), e);
        }

        return value;
    }

}

  1. Test

package com.example.demo;

import com.example.wf.service.NewProcessService;
import com.example.wf.vo.UserTaskVo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class TestFlowable {

    @Autowired
    private NewProcessService newProcessService;

    @Test
    public void getElValueTest() {
        String exp = "${abc == 1}";
        Map<String, Object> variableMap = new HashMap<>();
        variableMap.put("abc", "1");

        boolean flag = newProcessService.getElValue(exp, variableMap);
        System.err.println(flag);
    }

    @Test
    public void getElValueTest2() {
        String exp = "${halt == 'B'}";
        String taskId = "00b55b40-5904-11ed-acc1-005056b2814b";
        Map<String, Object> variableMap = new HashMap<>();
        variableMap.put("halt", "B");

        List<UserTaskVo> nextUserTasks = newProcessService.getNextUserTasks(taskId, variableMap);
        System.err.println(nextUserTasks.size());
    }

}
  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值