houzhongyi
2024-07-11 e7c1260db32209a078a962aaa0ad5492c35774fb
提交 | 用户 | 时间
e7c126 1 package com.iailab.module.bpm.framework.flowable.core.util;
H 2
3 import com.iailab.framework.tenant.core.context.TenantContextHolder;
4 import com.iailab.module.bpm.framework.flowable.core.enums.BpmConstants;
5 import org.flowable.common.engine.api.delegate.Expression;
6 import org.flowable.common.engine.api.variable.VariableContainer;
7 import org.flowable.common.engine.impl.el.ExpressionManager;
8 import org.flowable.common.engine.impl.identity.Authentication;
9 import org.flowable.engine.ProcessEngineConfiguration;
10 import org.flowable.engine.history.HistoricProcessInstance;
11 import org.flowable.engine.impl.cfg.ProcessEngineConfigurationImpl;
12 import org.flowable.engine.impl.util.CommandContextUtil;
13 import org.flowable.engine.runtime.ProcessInstance;
14 import org.flowable.task.api.TaskInfo;
15
16 import java.util.HashMap;
17 import java.util.List;
18 import java.util.Map;
19
20 /**
21  * Flowable 相关的工具方法
22  *
23  * @author iailab
24  */
25 public class FlowableUtils {
26
27     // ========== User 相关的工具方法 ==========
28
29     public static void setAuthenticatedUserId(Long userId) {
30         Authentication.setAuthenticatedUserId(String.valueOf(userId));
31     }
32
33     public static void clearAuthenticatedUserId() {
34         Authentication.setAuthenticatedUserId(null);
35     }
36
37     public static String getTenantId() {
38         Long tenantId = TenantContextHolder.getTenantId();
39         return tenantId != null ? String.valueOf(tenantId) : ProcessEngineConfiguration.NO_TENANT_ID;
40     }
41
42     // ========== Execution 相关的工具方法 ==========
43
44     /**
45      * 格式化多实例(并签、或签)的 collectionVariable 变量(多实例对应的多审批人列表)
46      *
47      * @param activityId 活动编号
48      * @return collectionVariable 变量
49      */
50     public static String formatExecutionCollectionVariable(String activityId) {
51         return activityId + "_assignees";
52     }
53
54     /**
55      * 格式化多实例(并签、或签)的 collectionElementVariable 变量(当前实例对应的一个审批人)
56      *
57      * @param activityId 活动编号
58      * @return collectionElementVariable 变量
59      */
60     public static String formatExecutionCollectionElementVariable(String activityId) {
61         return activityId + "_assignee";
62     }
63
64     // ========== ProcessInstance 相关的工具方法 ==========
65
66     public static Integer getProcessInstanceStatus(ProcessInstance processInstance) {
67         return getProcessInstanceStatus(processInstance.getProcessVariables());
68     }
69
70     public static Integer getProcessInstanceStatus(HistoricProcessInstance processInstance) {
71         return getProcessInstanceStatus(processInstance.getProcessVariables());
72     }
73
74     /**
75      * 获得流程实例的状态
76      *
77      * @param processVariables 流程实例的 variables
78      * @return 状态
79      */
80     private static Integer getProcessInstanceStatus(Map<String, Object> processVariables) {
81         return (Integer) processVariables.get(BpmConstants.PROCESS_INSTANCE_VARIABLE_STATUS);
82     }
83
84     /**
85      * 获得流程实例的表单
86      *
87      * @param processInstance 流程实例
88      * @return 表单
89      */
90     public static Map<String, Object> getProcessInstanceFormVariable(HistoricProcessInstance processInstance) {
91         Map<String, Object> formVariables = new HashMap<>(processInstance.getProcessVariables());
92         filterProcessInstanceFormVariable(formVariables);
93         return formVariables;
94     }
95
96     /**
97      * 过滤流程实例的表单
98      *
99      * 为什么要过滤?目前使用 processVariables 存储所有流程实例的拓展字段,需要过滤掉一部分的系统字段,从而实现表单的展示
100      *
101      * @param processVariables 流程实例的 variables
102      * @return 过滤后的表单
103      */
104     public static Map<String, Object> filterProcessInstanceFormVariable(Map<String, Object> processVariables) {
105         processVariables.remove(BpmConstants.PROCESS_INSTANCE_VARIABLE_STATUS);
106         return processVariables;
107     }
108
109     /**
110      * 获得流程实例的发起用户选择的审批人 Map
111      *
112      * @param processInstance 流程实例
113      * @return 发起用户选择的审批人 Map
114      */
115     @SuppressWarnings("unchecked")
116     public static Map<String, List<Long>> getStartUserSelectAssignees(ProcessInstance processInstance) {
117         return (Map<String, List<Long>>) processInstance.getProcessVariables().get(
118                 BpmConstants.PROCESS_INSTANCE_VARIABLE_START_USER_SELECT_ASSIGNEES);
119     }
120
121     // ========== Task 相关的工具方法 ==========
122
123     /**
124      * 获得任务的状态
125      *
126      * @param task 任务
127      * @return 状态
128      */
129     public static Integer getTaskStatus(TaskInfo task) {
130         return (Integer) task.getTaskLocalVariables().get(BpmConstants.TASK_VARIABLE_STATUS);
131     }
132
133     /**
134      * 获得任务的审批原因
135      *
136      * @param task 任务
137      * @return 审批原因
138      */
139     public static String getTaskReason(TaskInfo task) {
140         return (String) task.getTaskLocalVariables().get(BpmConstants.TASK_VARIABLE_REASON);
141     }
142
143     /**
144      * 获得任务的表单
145      *
146      * @param task 任务
147      * @return 表单
148      */
149     public static Map<String, Object> getTaskFormVariable(TaskInfo task) {
150         Map<String, Object> formVariables = new HashMap<>(task.getTaskLocalVariables());
151         filterTaskFormVariable(formVariables);
152         return formVariables;
153     }
154
155     /**
156      * 过滤任务的表单
157      *
158      * 为什么要过滤?目前使用 taskLocalVariables 存储所有任务的拓展字段,需要过滤掉一部分的系统字段,从而实现表单的展示
159      *
160      * @param taskLocalVariables 任务的 taskLocalVariables
161      * @return 过滤后的表单
162      */
163     public static Map<String, Object> filterTaskFormVariable(Map<String, Object> taskLocalVariables) {
164         taskLocalVariables.remove(BpmConstants.TASK_VARIABLE_STATUS);
165         taskLocalVariables.remove(BpmConstants.TASK_VARIABLE_REASON);
166         return taskLocalVariables;
167     }
168
169     // ========== Expression 相关的工具方法 ==========
170
171     public static Object getExpressionValue(VariableContainer variableContainer, String expressionString) {
172         ProcessEngineConfigurationImpl processEngineConfiguration = CommandContextUtil.getProcessEngineConfiguration();
173         assert processEngineConfiguration != null;
174         ExpressionManager expressionManager = processEngineConfiguration.getExpressionManager();
175         assert expressionManager != null;
176         Expression expression = expressionManager.createExpression(expressionString);
177         return expression.getValue(variableContainer);
178     }
179
180 }