package com.iailab.module.bpm.framework.flowable.core.util; import cn.hutool.core.util.ObjectUtil; import cn.hutool.extra.spring.SpringUtil; import com.iailab.framework.tenant.core.context.TenantContextHolder; import com.iailab.framework.tenant.core.util.TenantUtils; import com.iailab.module.bpm.framework.flowable.core.enums.BpmnVariableConstants; import org.flowable.common.engine.api.delegate.Expression; import org.flowable.common.engine.api.variable.VariableContainer; import org.flowable.common.engine.impl.el.ExpressionManager; import org.flowable.common.engine.impl.identity.Authentication; import org.flowable.common.engine.impl.variable.MapDelegateVariableContainer; import org.flowable.engine.ManagementService; import org.flowable.engine.ProcessEngineConfiguration; import org.flowable.engine.history.HistoricProcessInstance; import org.flowable.engine.impl.cfg.ProcessEngineConfigurationImpl; import org.flowable.engine.impl.util.CommandContextUtil; import org.flowable.engine.runtime.ProcessInstance; import org.flowable.task.api.TaskInfo; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.concurrent.Callable; /** * Flowable 相关的工具方法 * * @author èŠ‹é“æºç */ public class FlowableUtils { // ========== User 相关的工具方法 ========== public static void setAuthenticatedUserId(Long userId) { Authentication.setAuthenticatedUserId(String.valueOf(userId)); } public static void clearAuthenticatedUserId() { Authentication.setAuthenticatedUserId(null); } public static <V> V executeAuthenticatedUserId(Long userId, Callable<V> callable) { setAuthenticatedUserId(userId); try { return callable.call(); } catch (Exception e) { throw new RuntimeException(e); } finally { clearAuthenticatedUserId(); } } public static String getTenantId() { Long tenantId = TenantContextHolder.getTenantId(); return tenantId != null ? String.valueOf(tenantId) : ProcessEngineConfiguration.NO_TENANT_ID; } public static void execute(String tenantIdStr, Runnable runnable) { if (ObjectUtil.isEmpty(tenantIdStr) || Objects.equals(tenantIdStr, ProcessEngineConfiguration.NO_TENANT_ID)) { runnable.run(); } else { Long tenantId = Long.valueOf(tenantIdStr); TenantUtils.execute(tenantId, runnable); } } // ========== Execution 相关的工具方法 ========== /** * æ ¼å¼åŒ–多实例(并ç¾ã€æˆ–ç¾ï¼‰çš„ collectionVariable å˜é‡ï¼ˆå¤šå®žä¾‹å¯¹åº”的多审批人列表) * * @param activityId æ´»åŠ¨ç¼–å· * @return collectionVariable å˜é‡ */ public static String formatExecutionCollectionVariable(String activityId) { return activityId + "_assignees"; } /** * æ ¼å¼åŒ–多实例(并ç¾ã€æˆ–ç¾ï¼‰çš„ collectionElementVariable å˜é‡ï¼ˆå½“å‰å®žä¾‹å¯¹åº”的一个审批人) * * @param activityId æ´»åŠ¨ç¼–å· * @return collectionElementVariable å˜é‡ */ public static String formatExecutionCollectionElementVariable(String activityId) { return activityId + "_assignee"; } // ========== ProcessInstance 相关的工具方法 ========== public static Integer getProcessInstanceStatus(ProcessInstance processInstance) { return getProcessInstanceStatus(processInstance.getProcessVariables()); } public static Integer getProcessInstanceStatus(HistoricProcessInstance processInstance) { return getProcessInstanceStatus(processInstance.getProcessVariables()); } /** * 获得æµç¨‹å®žä¾‹çš„çŠ¶æ€ * * @param processVariables æµç¨‹å®žä¾‹çš„ variables * @return çŠ¶æ€ */ private static Integer getProcessInstanceStatus(Map<String, Object> processVariables) { return (Integer) processVariables.get(BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_STATUS); } /** * 获得æµç¨‹å®žä¾‹çš„å®¡æ‰¹åŽŸå› * * @param processInstance æµç¨‹å®žä¾‹ * @return å®¡æ‰¹åŽŸå› */ public static String getProcessInstanceReason(HistoricProcessInstance processInstance) { return (String) processInstance.getProcessVariables().get(BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_REASON); } /** * 获得æµç¨‹å®žä¾‹çš„è¡¨å• * * @param processInstance æµç¨‹å®žä¾‹ * @return è¡¨å• */ public static Map<String, Object> getProcessInstanceFormVariable(ProcessInstance processInstance) { Map<String, Object> processVariables = new HashMap<>(processInstance.getProcessVariables()); return filterProcessInstanceFormVariable(processVariables); } /** * 获得æµç¨‹å®žä¾‹çš„è¡¨å• * * @param processInstance æµç¨‹å®žä¾‹ * @return è¡¨å• */ public static Map<String, Object> getProcessInstanceFormVariable(HistoricProcessInstance processInstance) { Map<String, Object> processVariables = new HashMap<>(processInstance.getProcessVariables()); return filterProcessInstanceFormVariable(processVariables); } /** * 过滤æµç¨‹å®žä¾‹çš„è¡¨å• * * 为什么è¦è¿‡æ»¤ï¼Ÿç›®å‰ä½¿ç”¨ processVariables å˜å‚¨æ‰€æœ‰æµç¨‹å®žä¾‹çš„æ‹“展嗿®µï¼Œéœ€è¦è¿‡æ»¤æŽ‰ä¸€éƒ¨åˆ†çš„ç³»ç»Ÿå—æ®µï¼Œä»Žè€Œå®žçŽ°è¡¨å•的展示 * * @param processVariables æµç¨‹å®žä¾‹çš„ variables * @return 过滤åŽçš„è¡¨å• */ public static Map<String, Object> filterProcessInstanceFormVariable(Map<String, Object> processVariables) { processVariables.remove(BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_STATUS); return processVariables; } /** * 获得æµç¨‹å®žä¾‹çš„å‘起用户选择的审批人 Map * * @param processInstance æµç¨‹å®žä¾‹ * @return å‘起用户选择的审批人 Map */ public static Map<String, List<Long>> getStartUserSelectAssignees(ProcessInstance processInstance) { return processInstance != null ? getStartUserSelectAssignees(processInstance.getProcessVariables()) : null; } /** * 获得æµç¨‹å®žä¾‹çš„å‘起用户选择的审批人 Map * * @param processVariables æµç¨‹å˜é‡ * @return å‘起用户选择的审批人 Map */ @SuppressWarnings("unchecked") public static Map<String, List<Long>> getStartUserSelectAssignees(Map<String, Object> processVariables) { if (processVariables == null) { return null; } return (Map<String, List<Long>>) processVariables.get( BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_START_USER_SELECT_ASSIGNEES); } // ========== Task 相关的工具方法 ========== /** * èŽ·å¾—ä»»åŠ¡çš„çŠ¶æ€ * * @param task 任务 * @return çŠ¶æ€ */ public static Integer getTaskStatus(TaskInfo task) { return (Integer) task.getTaskLocalVariables().get(BpmnVariableConstants.TASK_VARIABLE_STATUS); } /** * èŽ·å¾—ä»»åŠ¡çš„å®¡æ‰¹åŽŸå› * * @param task 任务 * @return å®¡æ‰¹åŽŸå› */ public static String getTaskReason(TaskInfo task) { return (String) task.getTaskLocalVariables().get(BpmnVariableConstants.TASK_VARIABLE_REASON); } /** * èŽ·å¾—ä»»åŠ¡çš„è¡¨å• * * @param task 任务 * @return è¡¨å• */ public static Map<String, Object> getTaskFormVariable(TaskInfo task) { Map<String, Object> formVariables = new HashMap<>(task.getTaskLocalVariables()); filterTaskFormVariable(formVariables); return formVariables; } /** * è¿‡æ»¤ä»»åŠ¡çš„è¡¨å• * * 为什么è¦è¿‡æ»¤ï¼Ÿç›®å‰ä½¿ç”¨ taskLocalVariables å˜å‚¨æ‰€æœ‰ä»»åŠ¡çš„æ‹“å±•å—æ®µï¼Œéœ€è¦è¿‡æ»¤æŽ‰ä¸€éƒ¨åˆ†çš„ç³»ç»Ÿå—æ®µï¼Œä»Žè€Œå®žçŽ°è¡¨å•的展示 * * @param taskLocalVariables 任务的 taskLocalVariables * @return 过滤åŽçš„è¡¨å• */ public static Map<String, Object> filterTaskFormVariable(Map<String, Object> taskLocalVariables) { taskLocalVariables.remove(BpmnVariableConstants.TASK_VARIABLE_STATUS); taskLocalVariables.remove(BpmnVariableConstants.TASK_VARIABLE_REASON); return taskLocalVariables; } // ========== Expression 相关的工具方法 ========== private static Object getExpressionValue(VariableContainer variableContainer, String expressionString, ProcessEngineConfigurationImpl processEngineConfiguration) { assert processEngineConfiguration!= null; ExpressionManager expressionManager = processEngineConfiguration.getExpressionManager(); assert expressionManager!= null; Expression expression = expressionManager.createExpression(expressionString); return expression.getValue(variableContainer); } public static Object getExpressionValue(VariableContainer variableContainer, String expressionString) { ProcessEngineConfigurationImpl processEngineConfiguration = CommandContextUtil.getProcessEngineConfiguration(); if (processEngineConfiguration != null) { return getExpressionValue(variableContainer, expressionString, processEngineConfiguration); } // 如果 ProcessEngineConfigurationImpl 获å–ä¸åˆ°ï¼Œåˆ™éœ€è¦é€šè¿‡ ManagementService æ¥èŽ·å– ManagementService managementService = SpringUtil.getBean(ManagementService.class); assert managementService != null; return managementService.executeCommand(context -> getExpressionValue(variableContainer, expressionString, CommandContextUtil.getProcessEngineConfiguration())); } public static Object getExpressionValue(Map<String, Object> variable, String expressionString) { VariableContainer variableContainer = new MapDelegateVariableContainer(variable, VariableContainer.empty()); return getExpressionValue(variableContainer, expressionString); } }