From b8a0affd03b5fa9fa33cd6f870e90394c2df86c7 Mon Sep 17 00:00:00 2001 From: 潘志宝 <979469083@qq.com> Date: 星期一, 06 一月 2025 13:31:07 +0800 Subject: [PATCH] Merge remote-tracking branch 'origin/master' --- iailab-module-bpm/iailab-module-bpm-biz/src/main/java/com/iailab/module/bpm/framework/flowable/core/util/FlowableUtils.java | 114 ++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 files changed, 96 insertions(+), 18 deletions(-) diff --git a/iailab-module-bpm/iailab-module-bpm-biz/src/main/java/com/iailab/module/bpm/framework/flowable/core/util/FlowableUtils.java b/iailab-module-bpm/iailab-module-bpm-biz/src/main/java/com/iailab/module/bpm/framework/flowable/core/util/FlowableUtils.java index d865d35..65b1eec 100644 --- a/iailab-module-bpm/iailab-module-bpm-biz/src/main/java/com/iailab/module/bpm/framework/flowable/core/util/FlowableUtils.java +++ b/iailab-module-bpm/iailab-module-bpm-biz/src/main/java/com/iailab/module/bpm/framework/flowable/core/util/FlowableUtils.java @@ -1,11 +1,16 @@ 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.module.bpm.framework.flowable.core.enums.BpmConstants; +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; @@ -16,11 +21,13 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; +import java.util.concurrent.Callable; /** * Flowable 相关的工具方法 * - * @author iailab + * @author 芋道源码 */ public class FlowableUtils { @@ -34,9 +41,30 @@ 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 相关的工具方法 ========== @@ -78,7 +106,28 @@ * @return 状态 */ private static Integer getProcessInstanceStatus(Map<String, Object> processVariables) { - return (Integer) processVariables.get(BpmConstants.PROCESS_INSTANCE_VARIABLE_STATUS); + 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); } /** @@ -88,9 +137,8 @@ * @return 表单 */ public static Map<String, Object> getProcessInstanceFormVariable(HistoricProcessInstance processInstance) { - Map<String, Object> formVariables = new HashMap<>(processInstance.getProcessVariables()); - filterProcessInstanceFormVariable(formVariables); - return formVariables; + Map<String, Object> processVariables = new HashMap<>(processInstance.getProcessVariables()); + return filterProcessInstanceFormVariable(processVariables); } /** @@ -102,7 +150,7 @@ * @return 过滤后的表单 */ public static Map<String, Object> filterProcessInstanceFormVariable(Map<String, Object> processVariables) { - processVariables.remove(BpmConstants.PROCESS_INSTANCE_VARIABLE_STATUS); + processVariables.remove(BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_STATUS); return processVariables; } @@ -112,10 +160,23 @@ * @param processInstance 流程实例 * @return 发起用户选择的审批人 Map */ - @SuppressWarnings("unchecked") public static Map<String, List<Long>> getStartUserSelectAssignees(ProcessInstance processInstance) { - return (Map<String, List<Long>>) processInstance.getProcessVariables().get( - BpmConstants.PROCESS_INSTANCE_VARIABLE_START_USER_SELECT_ASSIGNEES); + 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 相关的工具方法 ========== @@ -127,7 +188,7 @@ * @return 状态 */ public static Integer getTaskStatus(TaskInfo task) { - return (Integer) task.getTaskLocalVariables().get(BpmConstants.TASK_VARIABLE_STATUS); + return (Integer) task.getTaskLocalVariables().get(BpmnVariableConstants.TASK_VARIABLE_STATUS); } /** @@ -137,7 +198,7 @@ * @return 审批原因 */ public static String getTaskReason(TaskInfo task) { - return (String) task.getTaskLocalVariables().get(BpmConstants.TASK_VARIABLE_REASON); + return (String) task.getTaskLocalVariables().get(BpmnVariableConstants.TASK_VARIABLE_REASON); } /** @@ -161,20 +222,37 @@ * @return 过滤后的表单 */ public static Map<String, Object> filterTaskFormVariable(Map<String, Object> taskLocalVariables) { - taskLocalVariables.remove(BpmConstants.TASK_VARIABLE_STATUS); - taskLocalVariables.remove(BpmConstants.TASK_VARIABLE_REASON); + taskLocalVariables.remove(BpmnVariableConstants.TASK_VARIABLE_STATUS); + taskLocalVariables.remove(BpmnVariableConstants.TASK_VARIABLE_REASON); return taskLocalVariables; } // ========== Expression 相关的工具方法 ========== - public static Object getExpressionValue(VariableContainer variableContainer, String expressionString) { - ProcessEngineConfigurationImpl processEngineConfiguration = CommandContextUtil.getProcessEngineConfiguration(); - assert processEngineConfiguration != null; + private static Object getExpressionValue(VariableContainer variableContainer, String expressionString, + ProcessEngineConfigurationImpl processEngineConfiguration) { + assert processEngineConfiguration!= null; ExpressionManager expressionManager = processEngineConfiguration.getExpressionManager(); - assert expressionManager != null; + 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); + } + } -- Gitblit v1.9.3