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