feat(approval): WfTemplate 数据驱动审批界面 + ApprovalDrawer + load/action 保真层执行
审批按钮/业务表单由 WfTemplate(view_wftemplate) 数据驱动取代写死。flowable-service 加 ApprovalContextDto + FormKeyResolver/StartFormKeyResolver(form_key 历史回补:运行任务经 (procDefId,taskDefKey) 查 act_hi_taskinst 回补)。flow-gateway 加 ApprovalController(审批上下文聚合)+WfTemplateService(模板按 buttonId 解析 servicename)+BusinessRecordLoader(直调 flowExcuteService 加载业务记录,4s 超时守护防挂死→loadPOJO 退化);action 按 buttonId 重解析 servicename 全字段执行。FlowController/FlowableClient/GatewayRequests/TodoController 接审批上下文与 reject/reback。WfTemplateServiceTest 覆盖。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
package com.eip4.flowservice.api;
|
||||
|
||||
/**
|
||||
* 任务审批上下文(供 gateway 解析 WfTemplate 驱动审批界面)。
|
||||
*
|
||||
* <p>聚合一次性返回办理审批所需的引擎侧事实:formKey(= view_wftemplate_id,已经 {@code FormKeyResolver}
|
||||
* 历史回补)、业务主键/表、实例状态。gateway 据 formKey 查 view_wftemplate 得按钮/脚本/业务表单。
|
||||
*/
|
||||
public class ApprovalContextDto {
|
||||
/** 是否查到任务(运行中或历史)。 */
|
||||
public boolean found;
|
||||
/** 任务 id。 */
|
||||
public String taskId;
|
||||
/** 任务名(节点名)。 */
|
||||
public String taskName;
|
||||
/** formKey = view_wftemplate_id(已历史回补;解析不到为 null → gateway 降级默认按钮)。 */
|
||||
public String formKey;
|
||||
/** 开始节点 formKey(BPMN startEvent activiti:formKey)。任务节点是裸模板(无pageid)时 gateway 回退到它(对齐老 getStartFormKey)。 */
|
||||
public String startFormKey;
|
||||
public String processInstanceId;
|
||||
public String processDefinitionId;
|
||||
public String taskDefinitionKey;
|
||||
/** 流程业务主键(= 业务记录 PK;从实例反查)。 */
|
||||
public String businessKey;
|
||||
/** 流程实例是否已结束。 */
|
||||
public boolean ended;
|
||||
/** 任务/实例是否挂起(挂起不可办理)。 */
|
||||
public boolean suspended;
|
||||
}
|
||||
@@ -43,15 +43,30 @@ public class FlowController {
|
||||
private final HistoryService historyService;
|
||||
private final RepositoryService repositoryService;
|
||||
private final JdbcTemplate bizJdbc;
|
||||
private final com.eip4.flowservice.wf.FormKeyResolver formKeyResolver;
|
||||
private final com.eip4.flowservice.wf.StartFormKeyResolver startFormKeyResolver;
|
||||
|
||||
@org.springframework.beans.factory.annotation.Autowired
|
||||
public FlowController(TaskService taskService, RuntimeService runtimeService,
|
||||
HistoryService historyService, RepositoryService repositoryService,
|
||||
JdbcTemplate bizJdbc) {
|
||||
JdbcTemplate bizJdbc, com.eip4.flowservice.wf.FormKeyResolver formKeyResolver,
|
||||
com.eip4.flowservice.wf.StartFormKeyResolver startFormKeyResolver) {
|
||||
this.taskService = taskService;
|
||||
this.runtimeService = runtimeService;
|
||||
this.historyService = historyService;
|
||||
this.repositoryService = repositoryService;
|
||||
this.bizJdbc = bizJdbc;
|
||||
this.formKeyResolver = formKeyResolver;
|
||||
this.startFormKeyResolver = startFormKeyResolver;
|
||||
}
|
||||
|
||||
/** 兼容老单测的五参构造(无解析器:就地构造)。生产由 Spring 走七参构造。 */
|
||||
public FlowController(TaskService taskService, RuntimeService runtimeService,
|
||||
HistoryService historyService, RepositoryService repositoryService,
|
||||
JdbcTemplate bizJdbc) {
|
||||
this(taskService, runtimeService, historyService, repositoryService, bizJdbc,
|
||||
new com.eip4.flowservice.wf.FormKeyResolver(historyService),
|
||||
new com.eip4.flowservice.wf.StartFormKeyResolver(repositoryService));
|
||||
}
|
||||
|
||||
@PostMapping("/deploy")
|
||||
@@ -227,8 +242,12 @@ public class FlowController {
|
||||
}
|
||||
|
||||
@PostMapping("/tasks/{taskId}/complete")
|
||||
@Operation(summary = "办理任务", description = "complete 任务,可带提交变量。挂起实例的任务会被引擎拒绝(语义回归)")
|
||||
@Operation(summary = "办理任务", description = "complete 任务,可带提交变量与审批意见(comment 经 addComment 记入流程评论供轨迹展示)。挂起实例的任务会被引擎拒绝(语义回归)")
|
||||
public ResponseEntity<Void> complete(@PathVariable String taskId, @RequestBody(required = false) Requests.CompleteRequest req) {
|
||||
// 审批意见落流程评论(对齐老 passProcess 的 addComment;complete 后任务消失,故先记)
|
||||
if (req != null && req.comment != null && !req.comment.isBlank()) {
|
||||
addTaskComment(taskId, req.comment);
|
||||
}
|
||||
if (req != null && req.variables != null) {
|
||||
taskService.complete(taskId, req.variables);
|
||||
} else {
|
||||
@@ -237,6 +256,18 @@ public class FlowController {
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
|
||||
/** 记审批意见为流程评论(轨迹经 getProcessInstanceComments 读)。任务不存在则静默跳过。 */
|
||||
private void addTaskComment(String taskId, String comment) {
|
||||
try {
|
||||
var t = taskService.createTaskQuery().taskId(taskId).singleResult();
|
||||
String pid = t != null ? t.getProcessInstanceId() : null;
|
||||
taskService.addComment(taskId, pid, comment);
|
||||
} catch (RuntimeException e) {
|
||||
org.slf4j.LoggerFactory.getLogger(FlowController.class)
|
||||
.warn("[审批意见] addComment 失败 taskId={}: {}", taskId, e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/process/{processInstanceId}")
|
||||
@Operation(summary = "查实例", description = "查流程实例:运行中返回 ended=false;已结束返回历史实例 ended=true;不存在 404。供 gateway 办理后判定流程是否结束以回写业务状态")
|
||||
public ResponseEntity<ProcessInstanceDto> processInstance(@PathVariable String processInstanceId) {
|
||||
@@ -285,6 +316,142 @@ public class FlowController {
|
||||
return ResponseEntity.ok(t != null ? TaskDto.of(t) : null);
|
||||
}
|
||||
|
||||
@GetMapping("/tasks/{taskId}/approval-context")
|
||||
@Operation(summary = "任务审批上下文",
|
||||
description = "聚合返回 WfTemplate 驱动审批界面所需事实:formKey(=view_wftemplate_id,经 FormKeyResolver 历史回补)、"
|
||||
+ "businessKey、processInstanceId、taskDefinitionKey、实例是否结束/挂起。任务不存在返回 found=false(前端降级)。"
|
||||
+ "运行中任务优先,查不到回退历史任务(供已办查看)。")
|
||||
public ResponseEntity<ApprovalContextDto> approvalContext(@PathVariable String taskId) {
|
||||
ApprovalContextDto dto = new ApprovalContextDto();
|
||||
dto.taskId = taskId;
|
||||
var t = taskService.createTaskQuery().taskId(taskId).singleResult();
|
||||
if (t != null) {
|
||||
dto.found = true;
|
||||
dto.taskName = t.getName();
|
||||
dto.processInstanceId = t.getProcessInstanceId();
|
||||
dto.processDefinitionId = t.getProcessDefinitionId();
|
||||
dto.taskDefinitionKey = t.getTaskDefinitionKey();
|
||||
dto.suspended = t.isSuspended();
|
||||
dto.formKey = formKeyResolver.resolve(t.getFormKey(), t.getProcessDefinitionId(), t.getTaskDefinitionKey());
|
||||
dto.startFormKey = startFormKeyResolver.resolve(t.getProcessDefinitionId());
|
||||
ProcessInstance pi = runtimeService.createProcessInstanceQuery()
|
||||
.processInstanceId(t.getProcessInstanceId()).singleResult();
|
||||
if (pi != null) {
|
||||
dto.businessKey = pi.getBusinessKey();
|
||||
dto.ended = false;
|
||||
}
|
||||
return ResponseEntity.ok(dto);
|
||||
}
|
||||
// 运行库无 → 历史任务(已办查看仍要解析 WfTemplate 显业务表单/轨迹)
|
||||
HistoricTaskInstance ht = historyService.createHistoricTaskInstanceQuery().taskId(taskId).singleResult();
|
||||
if (ht == null) {
|
||||
dto.found = false;
|
||||
return ResponseEntity.ok(dto);
|
||||
}
|
||||
dto.found = true;
|
||||
dto.taskName = ht.getName();
|
||||
dto.processInstanceId = ht.getProcessInstanceId();
|
||||
dto.processDefinitionId = ht.getProcessDefinitionId();
|
||||
dto.taskDefinitionKey = ht.getTaskDefinitionKey();
|
||||
dto.formKey = formKeyResolver.resolve(ht.getFormKey(), ht.getProcessDefinitionId(), ht.getTaskDefinitionKey());
|
||||
dto.startFormKey = startFormKeyResolver.resolve(ht.getProcessDefinitionId());
|
||||
var hpi = historyService.createHistoricProcessInstanceQuery()
|
||||
.processInstanceId(ht.getProcessInstanceId()).singleResult();
|
||||
if (hpi != null) {
|
||||
dto.businessKey = hpi.getBusinessKey();
|
||||
dto.ended = hpi.getEndTime() != null;
|
||||
}
|
||||
return ResponseEntity.ok(dto);
|
||||
}
|
||||
|
||||
@PostMapping("/tasks/{taskId}/reject")
|
||||
@Operation(summary = "拒绝(终止流程)",
|
||||
description = "对齐老 ProcessCoreServiceImpl.endProcess:审批拒绝=终止整个流程实例(deleteProcessInstance),"
|
||||
+ "记审批意见为评论、置 approveResult=reject 供轨迹。返回 businessKey/processEnded 供 gateway 回写 SYS_DATASTATUS=4(驳回)。")
|
||||
public ResponseEntity<Map<String, Object>> reject(@PathVariable String taskId,
|
||||
@RequestBody(required = false) Requests.ActionRequest req) {
|
||||
var t = taskService.createTaskQuery().taskId(taskId).singleResult();
|
||||
if (t == null) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
String pid = t.getProcessInstanceId();
|
||||
ProcessInstance pi = runtimeService.createProcessInstanceQuery().processInstanceId(pid).singleResult();
|
||||
String businessKey = pi != null ? pi.getBusinessKey() : null;
|
||||
String comment = req != null ? req.comment : null;
|
||||
boolean hasComment = comment != null && !comment.isBlank();
|
||||
if (hasComment) {
|
||||
taskService.addComment(taskId, pid, comment);
|
||||
}
|
||||
try {
|
||||
runtimeService.setVariable(pid, "approveResult", "reject");
|
||||
} catch (RuntimeException ignore) {
|
||||
/* 变量写入失败不阻断终止 */
|
||||
}
|
||||
runtimeService.deleteProcessInstance(pid, hasComment ? comment : "审批拒绝");
|
||||
Map<String, Object> r = new java.util.LinkedHashMap<>();
|
||||
r.put("rejected", true);
|
||||
r.put("processEnded", true);
|
||||
r.put("businessKey", businessKey);
|
||||
r.put("processInstanceId", pid);
|
||||
return ResponseEntity.ok(r);
|
||||
}
|
||||
|
||||
@PostMapping("/tasks/{taskId}/reback")
|
||||
@Operation(summary = "退回(发起人/起始节点)",
|
||||
description = "对齐老 ProcessCoreServiceImpl.backProcess(isStart):把执行流退回到流程最早的历史任务节点(发起/拟稿节点)。"
|
||||
+ "用 changeActivityState moveActivityIdTo;记意见、置 approveResult=reback。返回 businessKey 供 gateway 回写 SYS_DATASTATUS=5(重新提交)。"
|
||||
+ "无法定位目标或已在起始节点 → 409。")
|
||||
public ResponseEntity<Map<String, Object>> reback(@PathVariable String taskId,
|
||||
@RequestBody(required = false) Requests.ActionRequest req) {
|
||||
var t = taskService.createTaskQuery().taskId(taskId).singleResult();
|
||||
if (t == null) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
String pid = t.getProcessInstanceId();
|
||||
String currentKey = t.getTaskDefinitionKey();
|
||||
ProcessInstance pi = runtimeService.createProcessInstanceQuery().processInstanceId(pid).singleResult();
|
||||
String businessKey = pi != null ? pi.getBusinessKey() : null;
|
||||
String comment = req != null ? req.comment : null;
|
||||
boolean hasComment = comment != null && !comment.isBlank();
|
||||
if (hasComment) {
|
||||
taskService.addComment(taskId, pid, comment);
|
||||
}
|
||||
// 退回目标 = 流程最早的历史任务节点(发起/拟稿节点)
|
||||
List<HistoricTaskInstance> first = historyService.createHistoricTaskInstanceQuery()
|
||||
.processInstanceId(pid).orderByHistoricTaskInstanceStartTime().asc().listPage(0, 1);
|
||||
String targetKey = first.isEmpty() ? null : first.get(0).getTaskDefinitionKey();
|
||||
if (targetKey == null || targetKey.equals(currentKey)) {
|
||||
Map<String, Object> err = new java.util.LinkedHashMap<>();
|
||||
err.put("status", 1);
|
||||
err.put("msg", "已在起始节点或无法定位退回目标节点");
|
||||
return ResponseEntity.status(org.springframework.http.HttpStatus.CONFLICT).body(err);
|
||||
}
|
||||
try {
|
||||
runtimeService.setVariable(pid, "approveResult", "reback");
|
||||
} catch (RuntimeException ignore) {
|
||||
/* 变量写入失败不阻断退回 */
|
||||
}
|
||||
try {
|
||||
runtimeService.createChangeActivityStateBuilder()
|
||||
.processInstanceId(pid)
|
||||
.moveActivityIdTo(currentKey, targetKey)
|
||||
.changeState();
|
||||
} catch (RuntimeException e) {
|
||||
org.slf4j.LoggerFactory.getLogger(FlowController.class)
|
||||
.error("[退回失败] pid={} {}→{}: {}", pid, currentKey, targetKey, e.toString());
|
||||
Map<String, Object> err = new java.util.LinkedHashMap<>();
|
||||
err.put("status", 1);
|
||||
err.put("msg", "退回失败(流程结构不支持该跳转): " + e.getMessage());
|
||||
return ResponseEntity.status(org.springframework.http.HttpStatus.CONFLICT).body(err);
|
||||
}
|
||||
Map<String, Object> r = new java.util.LinkedHashMap<>();
|
||||
r.put("reback", true);
|
||||
r.put("targetKey", targetKey);
|
||||
r.put("businessKey", businessKey);
|
||||
r.put("processInstanceId", pid);
|
||||
return ResponseEntity.ok(r);
|
||||
}
|
||||
|
||||
@GetMapping("/process/{processInstanceId}/trace")
|
||||
@Operation(summary = "流程轨迹", description = "返回该流程实例所有历史任务节点(含当前活动节点),按开始时间升序。每节点包含办理人姓名(经 sys_user 反查)、审批意见(Flowable 评论)、办理结果变量。")
|
||||
public ResponseEntity<List<TraceNodeDto>> trace(@PathVariable String processInstanceId) {
|
||||
@@ -344,6 +511,25 @@ public class FlowController {
|
||||
return ResponseEntity.ok(nodes);
|
||||
}
|
||||
|
||||
@GetMapping("/process/business/{businessKey}/trace")
|
||||
@Operation(summary = "按业务主键查流程轨迹",
|
||||
description = "列表行只有业务主键(=流程 businessKey),本端点按 businessKey 反查最近一次历史流程实例,"
|
||||
+ "再复用 trace 组装审批轨迹(供列表「查看审批」按钮)。无对应流程 → 返回空列表(前端显「暂无审批记录」)。")
|
||||
public ResponseEntity<List<TraceNodeDto>> traceByBusinessKey(@PathVariable String businessKey) {
|
||||
if (businessKey == null || businessKey.isBlank()) {
|
||||
return ResponseEntity.ok(new ArrayList<>());
|
||||
}
|
||||
// 一个业务主键可能多次提交(驳回后重提)→ 取最近一次历史实例的轨迹。
|
||||
var list = historyService.createHistoricProcessInstanceQuery()
|
||||
.processInstanceBusinessKey(businessKey.trim())
|
||||
.orderByProcessInstanceStartTime().desc()
|
||||
.list();
|
||||
if (list == null || list.isEmpty()) {
|
||||
return ResponseEntity.ok(new ArrayList<>());
|
||||
}
|
||||
return trace(list.get(0).getId());
|
||||
}
|
||||
|
||||
/**
|
||||
* 经 sys_user 表反查用户姓名;biz 库连不上时 1s 超时降级返回 userId。
|
||||
* 用独立线程 + Future.get(timeout) 保证不阻塞主流程(biz 库离线时快速失败)。
|
||||
|
||||
@@ -21,6 +21,8 @@ public final class Requests {
|
||||
/** 办理(complete)任务。 */
|
||||
public static class CompleteRequest {
|
||||
public Map<String, Object> variables;
|
||||
/** 审批意见(非空则经 taskService.addComment 记入流程评论,供轨迹展示)。 */
|
||||
public String comment;
|
||||
}
|
||||
|
||||
/** 委托(delegate)任务。 */
|
||||
@@ -28,6 +30,17 @@ public final class Requests {
|
||||
public String delegateUserId;
|
||||
}
|
||||
|
||||
/**
|
||||
* 审批动作(reject 拒绝终止 / reback 退回发起)通用入参。
|
||||
* 对齐老 {@code TaskProcessService}:意见记评论,targetUserId 供 agent/assist/circulated 指定对象。
|
||||
*/
|
||||
public static class ActionRequest {
|
||||
/** 审批意见(记入流程评论)。 */
|
||||
public String comment;
|
||||
/** 目标用户(转办/协办/转阅用;reject/reback 不需要)。 */
|
||||
public String targetUserId;
|
||||
}
|
||||
|
||||
/**
|
||||
* F2-6 JWT 瘦身:待办查询 body(替代 GET candidateGroup 多值参数,
|
||||
* 避免多角色用户 URL 超 8KB Tomcat 默认限制)。
|
||||
|
||||
@@ -16,6 +16,12 @@ public class TaskDto {
|
||||
public String taskDefinitionKey;
|
||||
/** 流程业务主键(Flowable 6.8.1 Task/HistoricTaskInstance 接口无此字段,由查询侧从实例补全;供 gateway 聚合业务摘要)。 */
|
||||
public String businessKey;
|
||||
/**
|
||||
* 任务表单 key(= view_wftemplate_id,WfTemplate 驱动审批界面的钥匙)。
|
||||
* 取 {@code task.getFormKey()};运行中任务该字段多为空(迁移仅落 HI 行未回填 RU.form_key_),
|
||||
* 由查询侧 {@code FormKeyResolver} 按 (procDefId, taskDefKey) 从历史回补(实测 99.98% 可补)。
|
||||
*/
|
||||
public String formKey;
|
||||
public Long createTimeMillis;
|
||||
public Long endTimeMillis;
|
||||
public boolean suspended;
|
||||
@@ -29,6 +35,7 @@ public class TaskDto {
|
||||
d.processInstanceId = t.getProcessInstanceId();
|
||||
d.processDefinitionId = t.getProcessDefinitionId();
|
||||
d.taskDefinitionKey = t.getTaskDefinitionKey();
|
||||
d.formKey = t.getFormKey();
|
||||
d.createTimeMillis = t.getCreateTime() != null ? t.getCreateTime().getTime() : null;
|
||||
d.suspended = t.isSuspended();
|
||||
return d;
|
||||
@@ -43,6 +50,7 @@ public class TaskDto {
|
||||
d.processInstanceId = t.getProcessInstanceId();
|
||||
d.processDefinitionId = t.getProcessDefinitionId();
|
||||
d.taskDefinitionKey = t.getTaskDefinitionKey();
|
||||
d.formKey = t.getFormKey();
|
||||
d.createTimeMillis = t.getCreateTime() != null ? t.getCreateTime().getTime() : null;
|
||||
d.endTimeMillis = t.getEndTime() != null ? t.getEndTime().getTime() : null;
|
||||
return d;
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
package com.eip4.flowservice.wf;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import org.flowable.engine.HistoryService;
|
||||
import org.flowable.task.api.history.HistoricTaskInstance;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 任务 formKey(= view_wftemplate_id)解析器:WfTemplate 驱动审批界面的钥匙。
|
||||
*
|
||||
* <p><b>根因(已实测坐实)</b>:迁移把老 activiti 历史任务整表搬进 {@code act_hi_taskinst}
|
||||
* (form_key_ 覆盖 56900/56905 ≈ 99.99%),但<b>运行中任务表 {@code act_ru_task} 的 form_key_
|
||||
* 几乎全空</b>(9378 中仅 57 带值 ≈ 0.6%)——迁移未回填运行库该列。于是 {@code task.getFormKey()}
|
||||
* 对绝大多数待办返空,数据驱动审批退化为默认按钮,等于没用 1016 个 WfTemplate。
|
||||
*
|
||||
* <p><b>回补策略</b>:同一 userTask 定义(同 procDefId + taskDefKey)的 formKey 恒定,故运行任务
|
||||
* 缺 formKey 时,按 {@code (processDefinitionId, taskDefinitionKey)} 从历史表回查一行非空 formKey。
|
||||
* 实测可回补 9376/9378 ≈ 99.98% 的运行任务,且回查出的 286 个去重 formKey 100% 命中 view_wftemplate。
|
||||
* 对齐老系统 {@code FormService.getTaskFormKey(procDefId, taskDefKey)} 的取法(老靠 BPMN,本系统迁移后
|
||||
* BPMN 未带 formKey,故改走等价的历史回查)。
|
||||
*
|
||||
* <p>结果带进程内缓存(同定义重复命中,待办列表常多任务共享定义),命中即定,不重复查历史。
|
||||
*/
|
||||
@Component
|
||||
public class FormKeyResolver {
|
||||
|
||||
/** 缓存键分隔符(procDefId 与 taskDefKey 均不含此控制字符)。 */
|
||||
private static final String SEP = "\n";
|
||||
|
||||
private final HistoryService historyService;
|
||||
private final ConcurrentHashMap<String, String> cache = new ConcurrentHashMap<>();
|
||||
|
||||
public FormKeyResolver(HistoryService historyService) {
|
||||
this.historyService = historyService;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析任务 formKey:运行时值非空则直接用;否则按 (procDefId, taskDefKey) 历史回补。
|
||||
*
|
||||
* @param runtimeFormKey {@code task.getFormKey()} 的原值(可空)
|
||||
* @return formKey(= view_wftemplate_id);无法解析返回 null(调用方降级默认按钮)
|
||||
*/
|
||||
public String resolve(String runtimeFormKey, String processDefinitionId, String taskDefinitionKey) {
|
||||
if (notBlank(runtimeFormKey)) {
|
||||
return runtimeFormKey;
|
||||
}
|
||||
if (!notBlank(processDefinitionId) || !notBlank(taskDefinitionKey)) {
|
||||
return null;
|
||||
}
|
||||
String cacheKey = processDefinitionId + SEP + taskDefinitionKey;
|
||||
// computeIfAbsent 不缓存 null:回查不到则每次重查(可接受,命中后即缓存)。
|
||||
return cache.computeIfAbsent(cacheKey, k -> resolveFromHistory(processDefinitionId, taskDefinitionKey));
|
||||
}
|
||||
|
||||
/** 按 (procDefId, taskDefKey) 取一行非空 formKey 的历史任务。取小页扫描,避免全量加载。 */
|
||||
private String resolveFromHistory(String processDefinitionId, String taskDefinitionKey) {
|
||||
List<HistoricTaskInstance> rows = historyService.createHistoricTaskInstanceQuery()
|
||||
.processDefinitionId(processDefinitionId)
|
||||
.taskDefinitionKey(taskDefinitionKey)
|
||||
.listPage(0, 10);
|
||||
for (HistoricTaskInstance ht : rows) {
|
||||
if (notBlank(ht.getFormKey())) {
|
||||
return ht.getFormKey();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static boolean notBlank(String s) {
|
||||
return s != null && !s.isBlank();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
package com.eip4.flowservice.wf;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
|
||||
import org.flowable.bpmn.model.BpmnModel;
|
||||
import org.flowable.bpmn.model.FlowElement;
|
||||
import org.flowable.bpmn.model.StartEvent;
|
||||
import org.flowable.engine.RepositoryService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.NodeList;
|
||||
|
||||
/**
|
||||
* 流程「开始节点」formKey 解析器(= 老审批页对裸节点的回退依据)。
|
||||
*
|
||||
* <p><b>为何需要它(对齐老 {@code TaskDetailPage.initPage})</b>:老系统取任务 formKey 后,若该 WfTemplate
|
||||
* <b>没有 pageid(裸审批节点,只配按钮)</b>,就回退到 {@code FormService.getStartFormKey(procDef)}——即流程
|
||||
* <b>开始节点</b>的 formKey,其 WfTemplate 才有真正的 pageid(展示页)+ {@code <load>}(加载服务)+ 只读脚本。
|
||||
*
|
||||
* <p><b>取值</b>:迁移后的 BPMN 用 <b>activiti 命名空间</b> {@code activiti:formKey}(flowable 的
|
||||
* {@code task.getFormKey()} 读不到它,故运行库 form_key_ 全空)。本类先试 flowable 的 {@link BpmnModel}
|
||||
* (部分版本能读 activiti:formKey),读不到再<b>直接解析 BPMN XML 资源</b>取 {@code <startEvent>} 的 formKey
|
||||
* (activiti/flowable/无前缀三种命名空间都试)。结果按 procDefId 缓存(流程定义不可变)。
|
||||
*/
|
||||
@Component
|
||||
public class StartFormKeyResolver {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(StartFormKeyResolver.class);
|
||||
|
||||
private static final String NS_ACTIVITI = "http://activiti.org/bpmn";
|
||||
private static final String NS_FLOWABLE = "http://flowable.org/bpmn";
|
||||
|
||||
private final RepositoryService repositoryService;
|
||||
private final ConcurrentHashMap<String, String> cache = new ConcurrentHashMap<>();
|
||||
|
||||
public StartFormKeyResolver(RepositoryService repositoryService) {
|
||||
this.repositoryService = repositoryService;
|
||||
}
|
||||
|
||||
/** 取流程开始节点 formKey;解析不到返回 null(调用方不回退)。 */
|
||||
public String resolve(String processDefinitionId) {
|
||||
if (processDefinitionId == null || processDefinitionId.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
return cache.computeIfAbsent(processDefinitionId, this::resolveUncached);
|
||||
}
|
||||
|
||||
private String resolveUncached(String procDefId) {
|
||||
String viaModel = fromBpmnModel(procDefId);
|
||||
if (notBlank(viaModel)) {
|
||||
return viaModel;
|
||||
}
|
||||
return fromXmlResource(procDefId);
|
||||
}
|
||||
|
||||
/** 经 flowable BpmnModel 取开始节点 formKey(若该版本把 activiti:formKey 读进模型)。 */
|
||||
private String fromBpmnModel(String procDefId) {
|
||||
try {
|
||||
BpmnModel model = repositoryService.getBpmnModel(procDefId);
|
||||
if (model == null || model.getMainProcess() == null) {
|
||||
return null;
|
||||
}
|
||||
for (FlowElement el : model.getMainProcess().getFlowElements()) {
|
||||
if (el instanceof StartEvent se && notBlank(se.getFormKey())) {
|
||||
return se.getFormKey();
|
||||
}
|
||||
}
|
||||
} catch (RuntimeException e) {
|
||||
log.debug("BpmnModel 取开始 formKey 失败(转 XML 解析) procDef={}: {}", procDefId, e.toString());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** 直接解析 BPMN XML 资源取 startEvent 的 formKey(activiti/flowable/无前缀都试)。 */
|
||||
private String fromXmlResource(String procDefId) {
|
||||
try (InputStream is = repositoryService.getProcessModel(procDefId)) {
|
||||
if (is == null) {
|
||||
return null;
|
||||
}
|
||||
DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
|
||||
f.setNamespaceAware(true);
|
||||
f.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
|
||||
DocumentBuilder b = f.newDocumentBuilder();
|
||||
org.w3c.dom.Document doc = b.parse(is);
|
||||
NodeList starts = doc.getElementsByTagNameNS("*", "startEvent");
|
||||
if (starts.getLength() == 0) {
|
||||
return null;
|
||||
}
|
||||
Element se = (Element) starts.item(0);
|
||||
String fk = firstNonBlank(
|
||||
se.getAttributeNS(NS_ACTIVITI, "formKey"),
|
||||
se.getAttributeNS(NS_FLOWABLE, "formKey"),
|
||||
se.getAttribute("formKey"),
|
||||
se.getAttribute("activiti:formKey"),
|
||||
se.getAttribute("flowable:formKey"));
|
||||
return notBlank(fk) ? fk : null;
|
||||
} catch (Exception e) {
|
||||
log.warn("解析 BPMN XML 取开始 formKey 失败 procDef={}: {}", procDefId, e.toString());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static String firstNonBlank(String... vals) {
|
||||
for (String v : vals) {
|
||||
if (notBlank(v)) {
|
||||
return v;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static boolean notBlank(String s) {
|
||||
return s != null && !s.isBlank();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user