提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.module.bpm.framework.flowable.core.util; |
H |
2 |
|
bb2880
|
3 |
import cn.hutool.core.util.ObjectUtil; |
H |
4 |
import cn.hutool.extra.spring.SpringUtil; |
e7c126
|
5 |
import com.iailab.framework.tenant.core.context.TenantContextHolder; |
bb2880
|
6 |
import com.iailab.framework.tenant.core.util.TenantUtils; |
H |
7 |
import com.iailab.module.bpm.framework.flowable.core.enums.BpmnVariableConstants; |
e7c126
|
8 |
import org.flowable.common.engine.api.delegate.Expression; |
H |
9 |
import org.flowable.common.engine.api.variable.VariableContainer; |
|
10 |
import org.flowable.common.engine.impl.el.ExpressionManager; |
|
11 |
import org.flowable.common.engine.impl.identity.Authentication; |
bb2880
|
12 |
import org.flowable.common.engine.impl.variable.MapDelegateVariableContainer; |
H |
13 |
import org.flowable.engine.ManagementService; |
e7c126
|
14 |
import org.flowable.engine.ProcessEngineConfiguration; |
H |
15 |
import org.flowable.engine.history.HistoricProcessInstance; |
|
16 |
import org.flowable.engine.impl.cfg.ProcessEngineConfigurationImpl; |
|
17 |
import org.flowable.engine.impl.util.CommandContextUtil; |
|
18 |
import org.flowable.engine.runtime.ProcessInstance; |
|
19 |
import org.flowable.task.api.TaskInfo; |
|
20 |
|
|
21 |
import java.util.HashMap; |
|
22 |
import java.util.List; |
|
23 |
import java.util.Map; |
bb2880
|
24 |
import java.util.Objects; |
f4f940
|
25 |
import java.util.concurrent.Callable; |
e7c126
|
26 |
|
H |
27 |
/** |
|
28 |
* Flowable 相关的工具方法 |
|
29 |
* |
bb2880
|
30 |
* @author 芋道源码 |
e7c126
|
31 |
*/ |
H |
32 |
public class FlowableUtils { |
|
33 |
|
|
34 |
// ========== User 相关的工具方法 ========== |
|
35 |
|
|
36 |
public static void setAuthenticatedUserId(Long userId) { |
|
37 |
Authentication.setAuthenticatedUserId(String.valueOf(userId)); |
|
38 |
} |
|
39 |
|
|
40 |
public static void clearAuthenticatedUserId() { |
|
41 |
Authentication.setAuthenticatedUserId(null); |
|
42 |
} |
|
43 |
|
f4f940
|
44 |
public static <V> V executeAuthenticatedUserId(Long userId, Callable<V> callable) { |
H |
45 |
setAuthenticatedUserId(userId); |
|
46 |
try { |
|
47 |
return callable.call(); |
|
48 |
} catch (Exception e) { |
|
49 |
throw new RuntimeException(e); |
|
50 |
} finally { |
|
51 |
clearAuthenticatedUserId(); |
|
52 |
} |
|
53 |
} |
|
54 |
|
e7c126
|
55 |
public static String getTenantId() { |
H |
56 |
Long tenantId = TenantContextHolder.getTenantId(); |
|
57 |
return tenantId != null ? String.valueOf(tenantId) : ProcessEngineConfiguration.NO_TENANT_ID; |
bb2880
|
58 |
} |
H |
59 |
|
|
60 |
public static void execute(String tenantIdStr, Runnable runnable) { |
|
61 |
if (ObjectUtil.isEmpty(tenantIdStr) |
|
62 |
|| Objects.equals(tenantIdStr, ProcessEngineConfiguration.NO_TENANT_ID)) { |
|
63 |
runnable.run(); |
|
64 |
} else { |
|
65 |
Long tenantId = Long.valueOf(tenantIdStr); |
|
66 |
TenantUtils.execute(tenantId, runnable); |
|
67 |
} |
e7c126
|
68 |
} |
H |
69 |
|
|
70 |
// ========== Execution 相关的工具方法 ========== |
|
71 |
|
|
72 |
/** |
|
73 |
* 格式化多实例(并签、或签)的 collectionVariable 变量(多实例对应的多审批人列表) |
|
74 |
* |
|
75 |
* @param activityId 活动编号 |
|
76 |
* @return collectionVariable 变量 |
|
77 |
*/ |
|
78 |
public static String formatExecutionCollectionVariable(String activityId) { |
|
79 |
return activityId + "_assignees"; |
|
80 |
} |
|
81 |
|
|
82 |
/** |
|
83 |
* 格式化多实例(并签、或签)的 collectionElementVariable 变量(当前实例对应的一个审批人) |
|
84 |
* |
|
85 |
* @param activityId 活动编号 |
|
86 |
* @return collectionElementVariable 变量 |
|
87 |
*/ |
|
88 |
public static String formatExecutionCollectionElementVariable(String activityId) { |
|
89 |
return activityId + "_assignee"; |
|
90 |
} |
|
91 |
|
|
92 |
// ========== ProcessInstance 相关的工具方法 ========== |
|
93 |
|
|
94 |
public static Integer getProcessInstanceStatus(ProcessInstance processInstance) { |
|
95 |
return getProcessInstanceStatus(processInstance.getProcessVariables()); |
|
96 |
} |
|
97 |
|
|
98 |
public static Integer getProcessInstanceStatus(HistoricProcessInstance processInstance) { |
|
99 |
return getProcessInstanceStatus(processInstance.getProcessVariables()); |
|
100 |
} |
|
101 |
|
|
102 |
/** |
|
103 |
* 获得流程实例的状态 |
|
104 |
* |
|
105 |
* @param processVariables 流程实例的 variables |
|
106 |
* @return 状态 |
|
107 |
*/ |
|
108 |
private static Integer getProcessInstanceStatus(Map<String, Object> processVariables) { |
bb2880
|
109 |
return (Integer) processVariables.get(BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_STATUS); |
H |
110 |
} |
|
111 |
|
|
112 |
/** |
|
113 |
* 获得流程实例的审批原因 |
|
114 |
* |
|
115 |
* @param processInstance 流程实例 |
|
116 |
* @return 审批原因 |
|
117 |
*/ |
|
118 |
public static String getProcessInstanceReason(HistoricProcessInstance processInstance) { |
|
119 |
return (String) processInstance.getProcessVariables().get(BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_REASON); |
|
120 |
} |
|
121 |
|
|
122 |
/** |
|
123 |
* 获得流程实例的表单 |
|
124 |
* |
|
125 |
* @param processInstance 流程实例 |
|
126 |
* @return 表单 |
|
127 |
*/ |
|
128 |
public static Map<String, Object> getProcessInstanceFormVariable(ProcessInstance processInstance) { |
|
129 |
Map<String, Object> processVariables = new HashMap<>(processInstance.getProcessVariables()); |
|
130 |
return filterProcessInstanceFormVariable(processVariables); |
e7c126
|
131 |
} |
H |
132 |
|
|
133 |
/** |
|
134 |
* 获得流程实例的表单 |
|
135 |
* |
|
136 |
* @param processInstance 流程实例 |
|
137 |
* @return 表单 |
|
138 |
*/ |
|
139 |
public static Map<String, Object> getProcessInstanceFormVariable(HistoricProcessInstance processInstance) { |
bb2880
|
140 |
Map<String, Object> processVariables = new HashMap<>(processInstance.getProcessVariables()); |
H |
141 |
return filterProcessInstanceFormVariable(processVariables); |
e7c126
|
142 |
} |
H |
143 |
|
|
144 |
/** |
|
145 |
* 过滤流程实例的表单 |
|
146 |
* |
|
147 |
* 为什么要过滤?目前使用 processVariables 存储所有流程实例的拓展字段,需要过滤掉一部分的系统字段,从而实现表单的展示 |
|
148 |
* |
|
149 |
* @param processVariables 流程实例的 variables |
|
150 |
* @return 过滤后的表单 |
|
151 |
*/ |
|
152 |
public static Map<String, Object> filterProcessInstanceFormVariable(Map<String, Object> processVariables) { |
bb2880
|
153 |
processVariables.remove(BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_STATUS); |
e7c126
|
154 |
return processVariables; |
H |
155 |
} |
|
156 |
|
|
157 |
/** |
|
158 |
* 获得流程实例的发起用户选择的审批人 Map |
|
159 |
* |
|
160 |
* @param processInstance 流程实例 |
|
161 |
* @return 发起用户选择的审批人 Map |
|
162 |
*/ |
|
163 |
public static Map<String, List<Long>> getStartUserSelectAssignees(ProcessInstance processInstance) { |
bb2880
|
164 |
return processInstance != null ? getStartUserSelectAssignees(processInstance.getProcessVariables()) : null; |
H |
165 |
} |
|
166 |
|
|
167 |
/** |
|
168 |
* 获得流程实例的发起用户选择的审批人 Map |
|
169 |
* |
|
170 |
* @param processVariables 流程变量 |
|
171 |
* @return 发起用户选择的审批人 Map |
|
172 |
*/ |
|
173 |
@SuppressWarnings("unchecked") |
|
174 |
public static Map<String, List<Long>> getStartUserSelectAssignees(Map<String, Object> processVariables) { |
|
175 |
if (processVariables == null) { |
|
176 |
return null; |
|
177 |
} |
|
178 |
return (Map<String, List<Long>>) processVariables.get( |
|
179 |
BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_START_USER_SELECT_ASSIGNEES); |
e7c126
|
180 |
} |
H |
181 |
|
|
182 |
// ========== Task 相关的工具方法 ========== |
|
183 |
|
|
184 |
/** |
|
185 |
* 获得任务的状态 |
|
186 |
* |
|
187 |
* @param task 任务 |
|
188 |
* @return 状态 |
|
189 |
*/ |
|
190 |
public static Integer getTaskStatus(TaskInfo task) { |
bb2880
|
191 |
return (Integer) task.getTaskLocalVariables().get(BpmnVariableConstants.TASK_VARIABLE_STATUS); |
e7c126
|
192 |
} |
H |
193 |
|
|
194 |
/** |
|
195 |
* 获得任务的审批原因 |
|
196 |
* |
|
197 |
* @param task 任务 |
|
198 |
* @return 审批原因 |
|
199 |
*/ |
|
200 |
public static String getTaskReason(TaskInfo task) { |
bb2880
|
201 |
return (String) task.getTaskLocalVariables().get(BpmnVariableConstants.TASK_VARIABLE_REASON); |
e7c126
|
202 |
} |
H |
203 |
|
|
204 |
/** |
|
205 |
* 获得任务的表单 |
|
206 |
* |
|
207 |
* @param task 任务 |
|
208 |
* @return 表单 |
|
209 |
*/ |
|
210 |
public static Map<String, Object> getTaskFormVariable(TaskInfo task) { |
|
211 |
Map<String, Object> formVariables = new HashMap<>(task.getTaskLocalVariables()); |
|
212 |
filterTaskFormVariable(formVariables); |
|
213 |
return formVariables; |
|
214 |
} |
|
215 |
|
|
216 |
/** |
|
217 |
* 过滤任务的表单 |
|
218 |
* |
|
219 |
* 为什么要过滤?目前使用 taskLocalVariables 存储所有任务的拓展字段,需要过滤掉一部分的系统字段,从而实现表单的展示 |
|
220 |
* |
|
221 |
* @param taskLocalVariables 任务的 taskLocalVariables |
|
222 |
* @return 过滤后的表单 |
|
223 |
*/ |
|
224 |
public static Map<String, Object> filterTaskFormVariable(Map<String, Object> taskLocalVariables) { |
bb2880
|
225 |
taskLocalVariables.remove(BpmnVariableConstants.TASK_VARIABLE_STATUS); |
H |
226 |
taskLocalVariables.remove(BpmnVariableConstants.TASK_VARIABLE_REASON); |
e7c126
|
227 |
return taskLocalVariables; |
H |
228 |
} |
|
229 |
|
|
230 |
// ========== Expression 相关的工具方法 ========== |
|
231 |
|
bb2880
|
232 |
private static Object getExpressionValue(VariableContainer variableContainer, String expressionString, |
H |
233 |
ProcessEngineConfigurationImpl processEngineConfiguration) { |
|
234 |
assert processEngineConfiguration!= null; |
e7c126
|
235 |
ExpressionManager expressionManager = processEngineConfiguration.getExpressionManager(); |
bb2880
|
236 |
assert expressionManager!= null; |
e7c126
|
237 |
Expression expression = expressionManager.createExpression(expressionString); |
H |
238 |
return expression.getValue(variableContainer); |
|
239 |
} |
|
240 |
|
bb2880
|
241 |
public static Object getExpressionValue(VariableContainer variableContainer, String expressionString) { |
H |
242 |
ProcessEngineConfigurationImpl processEngineConfiguration = CommandContextUtil.getProcessEngineConfiguration(); |
|
243 |
if (processEngineConfiguration != null) { |
|
244 |
return getExpressionValue(variableContainer, expressionString, processEngineConfiguration); |
|
245 |
} |
|
246 |
// 如果 ProcessEngineConfigurationImpl 获取不到,则需要通过 ManagementService 来获取 |
|
247 |
ManagementService managementService = SpringUtil.getBean(ManagementService.class); |
|
248 |
assert managementService != null; |
|
249 |
return managementService.executeCommand(context -> |
|
250 |
getExpressionValue(variableContainer, expressionString, CommandContextUtil.getProcessEngineConfiguration())); |
|
251 |
} |
|
252 |
|
|
253 |
public static Object getExpressionValue(Map<String, Object> variable, String expressionString) { |
|
254 |
VariableContainer variableContainer = new MapDelegateVariableContainer(variable, VariableContainer.empty()); |
|
255 |
return getExpressionValue(variableContainer, expressionString); |
|
256 |
} |
|
257 |
|
e7c126
|
258 |
} |