服务流分派: amis ids 批量形态适配——按流 <input> 必填 *_ID 逐 id 扇出执行
修 F2-17 删除/提交审批/文件归档断链:bulkActions 只送 ids=主键逗号串,而老 服务流 <input> 声明单条必填主键(如 OA_NOTICEMANAGE_ID)→透传 ids 必填缺失 500、行未动(API 实证)。NAMED_WRITE 收到 ids 且流声明了缺失的唯一必填 *_ID 字符串入参时,拆逗号逐 id 执行(对齐老系统逐行跑流语义);无声明/歧义→原样 透传。实证:ids 形态 status 0 + isdel=1 落库;e2e f2-17 5/5 过。 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -141,6 +141,92 @@ public class FlowInputCoercer {
|
||||
}
|
||||
}
|
||||
|
||||
/** flowId → 流 {@code <input>} 声明的必填 *_ID 入参名缓存({@code ""} = 无/歧义)。 */
|
||||
private final Cache<String, String> requiredIdInputCache = Caffeine.newBuilder()
|
||||
.maximumSize(5_000)
|
||||
.build();
|
||||
|
||||
/** {@code <input>} 块提取。 */
|
||||
private static final Pattern INPUT_BLOCK_PATTERN = Pattern.compile(
|
||||
"<input>(.*?)</input>", Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
|
||||
|
||||
/** input 块内单个 field 标签。 */
|
||||
private static final Pattern INPUT_FIELD_PATTERN = Pattern.compile(
|
||||
"<field\\b[^>]*>", Pattern.CASE_INSENSITIVE);
|
||||
|
||||
/**
|
||||
* amis bulkActions 契约适配(F2-17 删除/提交审批/文件归档断链根因):批量按钮只送
|
||||
* {@code ids}=选中行主键逗号串,而老服务流 {@code <input>} 声明的是单条主键入参
|
||||
* (如 {@code OA_NOTICEMANAGE_ID}, required)——直接透传 ids 会因必填缺失 500。
|
||||
*
|
||||
* <p>返回该流声明的、调用方 map 里没给的必填 {@code *_ID} 字符串入参名;
|
||||
* 无此入参 / 有多个(歧义不猜)/ map 已显式提供 → null。
|
||||
*/
|
||||
public String missingRequiredIdInput(String flowId, Map<String, Object> map) {
|
||||
if (flowId == null || flowId.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
String field = requiredIdInputCache.get(flowId, this::resolveRequiredIdInput);
|
||||
if (field.isEmpty() || (map != null && map.containsKey(field))) {
|
||||
return null;
|
||||
}
|
||||
return field;
|
||||
}
|
||||
|
||||
private String resolveRequiredIdInput(String flowId) {
|
||||
try {
|
||||
String xml = jdbcTemplate.queryForObject(
|
||||
"SELECT contentxml::text FROM eip3.sys_serviceflow WHERE sys_serviceflow_id = ?",
|
||||
String.class, flowId);
|
||||
String field = extractRequiredIdInput(xml);
|
||||
return field != null ? field : "";
|
||||
} catch (org.springframework.dao.EmptyResultDataAccessException e) {
|
||||
return ""; // 流不存在
|
||||
} catch (RuntimeException e) {
|
||||
log.warn("[FLOW-IDS] 查 contentxml 失败 flowId={}: {}", sanitize(flowId), e.getMessage());
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 contentxml 的 {@code <input>} 块提取必填 {@code *_ID} 字符串入参名。
|
||||
* 条件:{@code required="true"}、无 {@code initValue}(有初值的不缺)、name 以 _ID 结尾
|
||||
* (大小写不敏感)。命中 0 个或 ≥2 个(歧义)→ null。
|
||||
*/
|
||||
static String extractRequiredIdInput(String contentXml) {
|
||||
if (contentXml == null || contentXml.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
Matcher block = INPUT_BLOCK_PATTERN.matcher(contentXml);
|
||||
if (!block.find()) {
|
||||
return null;
|
||||
}
|
||||
String hit = null;
|
||||
Matcher fields = INPUT_FIELD_PATTERN.matcher(block.group(1));
|
||||
while (fields.find()) {
|
||||
String tag = fields.group();
|
||||
String name = attr(tag, "name");
|
||||
if (name == null || !name.toUpperCase().endsWith("_ID")) {
|
||||
continue;
|
||||
}
|
||||
if (!"true".equalsIgnoreCase(attr(tag, "required")) || attr(tag, "initValue") != null) {
|
||||
continue;
|
||||
}
|
||||
if (hit != null) {
|
||||
return null; // 多个必填 *_ID,歧义不猜
|
||||
}
|
||||
hit = name;
|
||||
}
|
||||
return hit;
|
||||
}
|
||||
|
||||
/** 从单个标签文本里取属性值;无该属性 → null。 */
|
||||
private static String attr(String tag, String attrName) {
|
||||
Matcher m = Pattern.compile("\\b" + attrName + "\\s*=\\s*\"([^\"]*)\"", Pattern.CASE_INSENSITIVE)
|
||||
.matcher(tag);
|
||||
return m.find() ? m.group(1).trim() : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 按 flowId 从 sys_serviceflow 查 contentxml,提取 tableName。
|
||||
* 找不到 → 返回 {@code ""}(空字符串,会被缓存以避免重查)。
|
||||
|
||||
@@ -195,6 +195,31 @@ public class FlowController {
|
||||
if (!isPost) {
|
||||
yield forbiddenWriteViaGet(id);
|
||||
}
|
||||
// amis bulkActions 契约适配(F2-17 删除/提交审批/文件归档断链根因):
|
||||
// 批量按钮只送 ids=选中行主键逗号串,而老服务流 <input> 声明的是单条必填主键
|
||||
// (如 OA_NOTICEMANAGE_ID, required)——透传 ids 会因必填缺失 500。
|
||||
// 这里拆 ids 逐 id 执行(对齐老系统逐行跑流的语义),其余 body 键原样带上。
|
||||
// 注意:每次 excute 各自一个引擎事务,部分失败时已执行的 id 不回滚(与老系统逐行点击一致)。
|
||||
String pkInput = (inputCoercer != null && map.get("ids") instanceof String s && !s.isBlank())
|
||||
? inputCoercer.missingRequiredIdInput(id, map) : null;
|
||||
if (pkInput != null) {
|
||||
Properties fanoutCtx = currentCtx();
|
||||
List<Object> results = new ArrayList<>();
|
||||
for (String one : ((String) map.get("ids")).split(",")) {
|
||||
if (one.isBlank()) {
|
||||
continue;
|
||||
}
|
||||
Map<String, Object> per = new HashMap<>(map);
|
||||
per.remove("ids");
|
||||
per.put(pkInput, one.trim());
|
||||
inputCoercer.coerceByFlowId(id, per);
|
||||
results.add(flowExcuteService.excute(per, fanoutCtx, id));
|
||||
}
|
||||
Map<String, Object> agg = new LinkedHashMap<>();
|
||||
agg.put("count", results.size());
|
||||
agg.put("results", results);
|
||||
yield ok(agg, null);
|
||||
}
|
||||
// amis JSON body 全为 String,引擎 GeneralPO.load 按 POJOInfo 列类型直接 cast:
|
||||
// ENUMERATE/INTEGER/STATUS→Integer、BOOLEAN→Boolean、DECIMAL→BigDecimal。
|
||||
// coerceByFlowId 按 SYS_ENTITYATTRIBUTE 就地转换,fail-safe 不阻断写路径。
|
||||
|
||||
@@ -36,6 +36,55 @@ class FlowInputCoercerTest {
|
||||
coercer = new FlowInputCoercer(jdbc);
|
||||
}
|
||||
|
||||
// ── extractRequiredIdInput(amis ids → 流必填主键入参适配)──────────────────
|
||||
|
||||
@Test
|
||||
@DisplayName("extractRequiredIdInput: 命中唯一必填 *_ID 入参(OA_NOTICEMANAGE_DELETE 实形态)")
|
||||
void extractRequiredIdInputHit() {
|
||||
String xml = "<flow id=\"OA_NOTICEMANAGE_DELETE\"><input>"
|
||||
+ "<field name=\"tableName\" type=\"java.lang.String\" required=\"true\" initValue=\"OA_NOTICEMANAGE\"/>"
|
||||
+ "<field name=\"map\" type=\"java.util.Map\" required=\"false\"/>"
|
||||
+ "<field name=\"ctx\" type=\"java.util.Properties\" required=\"true\"/>"
|
||||
+ "<field name=\"OA_NOTICEMANAGE_ID\" type=\"java.lang.String\" required=\"true\"/>"
|
||||
+ "</input><start id=\"Start\"/></flow>";
|
||||
assertEquals("OA_NOTICEMANAGE_ID", FlowInputCoercer.extractRequiredIdInput(xml));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("extractRequiredIdInput: 无必填 *_ID / 多个歧义 / 带 initValue → null")
|
||||
void extractRequiredIdInputMisses() {
|
||||
// 无 *_ID 入参
|
||||
assertNull(FlowInputCoercer.extractRequiredIdInput(
|
||||
"<flow><input><field name=\"map\" type=\"java.util.Map\"/></input></flow>"));
|
||||
// 两个必填 *_ID → 歧义不猜
|
||||
assertNull(FlowInputCoercer.extractRequiredIdInput(
|
||||
"<flow><input>"
|
||||
+ "<field name=\"A_ID\" required=\"true\"/>"
|
||||
+ "<field name=\"B_ID\" required=\"true\"/>"
|
||||
+ "</input></flow>"));
|
||||
// 带 initValue 的不缺
|
||||
assertNull(FlowInputCoercer.extractRequiredIdInput(
|
||||
"<flow><input><field name=\"X_ID\" required=\"true\" initValue=\"fixed\"/></input></flow>"));
|
||||
// required != true
|
||||
assertNull(FlowInputCoercer.extractRequiredIdInput(
|
||||
"<flow><input><field name=\"X_ID\" required=\"false\"/></input></flow>"));
|
||||
// 无 input 块 / 空
|
||||
assertNull(FlowInputCoercer.extractRequiredIdInput("<flow/>"));
|
||||
assertNull(FlowInputCoercer.extractRequiredIdInput(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("missingRequiredIdInput: map 已显式提供该键 → null(不重复扇出)")
|
||||
void missingRequiredIdInputProvided() {
|
||||
when(jdbc.queryForObject(anyString(), eq(String.class), eq("F1")))
|
||||
.thenReturn("<flow><input><field name=\"X_ID\" required=\"true\"/></input></flow>");
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("X_ID", "given");
|
||||
assertNull(coercer.missingRequiredIdInput("F1", map));
|
||||
// 未提供 → 返回入参名
|
||||
assertEquals("X_ID", coercer.missingRequiredIdInput("F1", new HashMap<>()));
|
||||
}
|
||||
|
||||
// ── extractTableName ─────────────────────────────────────────────────────
|
||||
|
||||
@Test
|
||||
|
||||
@@ -440,6 +440,71 @@ class FlowControllerTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("17b. NAMED_WRITE + amis ids 批量形态 → 拆 ids 按流必填主键入参逐 id 执行(F2-17 删除断链修复)")
|
||||
void writeFlowFansOutAmisIdsToRequiredPkInput() {
|
||||
String flowId = "OA_NOTICEMANAGE_DELETE";
|
||||
FlowExcuteService engine = mock(FlowExcuteService.class);
|
||||
when(engine.excute(any(), any(), eq(flowId))).thenReturn(new HashMap<>());
|
||||
com.eip4.platform.metadata.flow.FlowInputCoercer coercer =
|
||||
mock(com.eip4.platform.metadata.flow.FlowInputCoercer.class);
|
||||
when(coercer.missingRequiredIdInput(eq(flowId), any())).thenReturn("OA_NOTICEMANAGE_ID");
|
||||
FlowController controller = new FlowController(
|
||||
engine, resolverReturning(flowId, namedWrite()), null, null, coercer);
|
||||
|
||||
Properties realUser = new Properties();
|
||||
realUser.setProperty(LoggedInUser.CURRENTUSER, "ceshi");
|
||||
try (MockedStatic<UserSession> us = org.mockito.Mockito.mockStatic(UserSession.class)) {
|
||||
us.when(UserSession::get).thenReturn(realUser);
|
||||
|
||||
Map<String, Object> body = new HashMap<>();
|
||||
body.put("ids", "id-a,id-b"); // amis bulkActions 契约:选中行主键逗号串
|
||||
ResponseEntity<Map<String, Object>> resp = controller.executePost(flowId, body);
|
||||
|
||||
assertEquals(HttpStatus.OK, resp.getStatusCode());
|
||||
assertEquals(0, resp.getBody().get("status"));
|
||||
|
||||
// 每个 id 一次引擎执行,入参带流声明的主键名、不带 ids
|
||||
@SuppressWarnings("unchecked")
|
||||
ArgumentCaptor<Map<String, Object>> mapCap = ArgumentCaptor.forClass(Map.class);
|
||||
org.mockito.Mockito.verify(engine, org.mockito.Mockito.times(2))
|
||||
.excute(mapCap.capture(), any(Properties.class), eq(flowId));
|
||||
java.util.List<Map<String, Object>> sent = mapCap.getAllValues();
|
||||
assertEquals("id-a", sent.get(0).get("OA_NOTICEMANAGE_ID"));
|
||||
assertEquals("id-b", sent.get(1).get("OA_NOTICEMANAGE_ID"));
|
||||
org.junit.jupiter.api.Assertions.assertFalse(sent.get(0).containsKey("ids"),
|
||||
"扇出后不应再透传 ids(流的 <input> 不认它)");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("17c. NAMED_WRITE + ids 但流无必填 *_ID 入参 → 不扇出,原样单次执行")
|
||||
void writeFlowKeepsIdsWhenFlowHasNoPkInput() {
|
||||
String flowId = "SOME_BATCH_FLOW";
|
||||
FlowExcuteService engine = mock(FlowExcuteService.class);
|
||||
when(engine.excute(any(), any(), eq(flowId))).thenReturn(new HashMap<>());
|
||||
com.eip4.platform.metadata.flow.FlowInputCoercer coercer =
|
||||
mock(com.eip4.platform.metadata.flow.FlowInputCoercer.class);
|
||||
when(coercer.missingRequiredIdInput(eq(flowId), any())).thenReturn(null);
|
||||
FlowController controller = new FlowController(
|
||||
engine, resolverReturning(flowId, namedWrite()), null, null, coercer);
|
||||
|
||||
try (MockedStatic<UserSession> us = org.mockito.Mockito.mockStatic(UserSession.class)) {
|
||||
us.when(UserSession::get).thenReturn(null);
|
||||
us.when(UserSession::getAdmin).thenReturn(adminCtx());
|
||||
|
||||
Map<String, Object> body = new HashMap<>();
|
||||
body.put("ids", "id-a,id-b");
|
||||
controller.executePost(flowId, body);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
ArgumentCaptor<Map<String, Object>> mapCap = ArgumentCaptor.forClass(Map.class);
|
||||
org.mockito.Mockito.verify(engine, org.mockito.Mockito.times(1))
|
||||
.excute(mapCap.capture(), any(Properties.class), eq(flowId));
|
||||
assertEquals("id-a,id-b", mapCap.getValue().get("ids"), "无主键入参声明 → ids 原样透传");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("18. NAMED_WRITE 经 POST 但未登录(UserSession.get()=null) → 回退 ADMIN 执行(不崩)")
|
||||
void writeFlowViaPostFallsBackToAdminWhenUnbound() {
|
||||
|
||||
Reference in New Issue
Block a user