TreeGrid 二次修复:左树 heightAuto 撑高 + 点父节点子树聚合(右表 IN 过滤)
4337720 只做通 happy path(有数据单级树 + 点叶子),浏览器 QA 发现两处运行时缺陷:
1. 左树只显示一半(全 45 个 treegrid 页):input-tree 在 grid 列内约 270px
定高滚动框、页面下方大片留白 → convertTreeGrid 加 heightAuto:true 随内容撑满。
2. 点父/中间节点右表「暂无数据」(29 个多级 type-4 树):右表按 {rcol}=${tree}
精确匹配点中节点自身,而业务行常只挂叶子(文件挂「技术部」非父级「技术中心」),
点父级 0 行;老系统点父级聚合整棵子树。修法:新增 GET /api/template/treegrid
子树聚合端点——解析「点中节点 + 全部后代」id 列表,以 List 值委托合成 _SEARCH,
引擎 GeneralPO.get_SearchWhereClause 见 List 即构 rcol IN(...)(已 escapeSqlLiteral
转义、无注入面,不改保真层),复用 _DISPLAY__/数据权限/分页;转换器右表 api 改指
该端点(treeRef/gridTable/rcol/node=${tree})。
TemplateTreeController 重构出可复用 build() + collectSubtreeIds + 子树端点
(补 7 单测:构树多级/子树展开/陈旧节点退化/空节点/List IN 委托/空 node 返全部/
IDENT 守卫);转换器 tree_treeGrid 断言更新为新 api + heightAuto(60 测全过)。
浏览器实证:资料库点父级「技术中心」现出子级「技术部」文件(修前空);
单级树「通知收藏」点叶子仍正确过滤 1 行(无回归)。
注:节点级增删改按钮、type=3 声明式多级树接端点属范围外后置项。
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -914,22 +914,23 @@ public class TemplateToAmisConverter {
|
|||||||
private Map<String, Object> convertTreeGrid(Element tree, Element treeTemplate, Element gridTemplate,
|
private Map<String, Object> convertTreeGrid(Element tree, Element treeTemplate, Element gridTemplate,
|
||||||
ConversionResult result, String templateName) {
|
ConversionResult result, String templateName) {
|
||||||
final String gridCrudId = "treegrid_crud";
|
final String gridCrudId = "treegrid_crud";
|
||||||
|
String treeRef = treeTemplate != null ? treeTemplate.attributeValue("ref", "") : "";
|
||||||
Map<String, Object> treeSchema = new LinkedHashMap<>();
|
Map<String, Object> treeSchema = new LinkedHashMap<>();
|
||||||
treeSchema.put("type", "input-tree");
|
treeSchema.put("type", "input-tree");
|
||||||
treeSchema.put("name", "tree");
|
treeSchema.put("name", "tree");
|
||||||
treeSchema.put("showOutline", true);
|
treeSchema.put("showOutline", true);
|
||||||
treeSchema.put("initiallyOpen", true);
|
treeSchema.put("initiallyOpen", true);
|
||||||
if (treeTemplate != null) {
|
// heightAuto:amis input-tree 默认在约 200~270px 的定高框内滚动,节点多时只露一半(且页面下方大片留白)。
|
||||||
String ref = treeTemplate.attributeValue("ref", "");
|
// 设 heightAuto 让树随内容自然撑高,对齐老系统满高树;超大树由页面滚动承载。
|
||||||
if (!ref.isBlank()) {
|
treeSchema.put("heightAuto", true);
|
||||||
treeSchema.put("source", "/api/template/" + ref + "/tree");
|
if (treeTemplate != null && !treeRef.isBlank()) {
|
||||||
|
treeSchema.put("source", "/api/template/" + treeRef + "/tree");
|
||||||
// 选中树节点 → 重载右表(${tree} 进右表 api 过滤);TemplateTreeController 运行时构树
|
// 选中树节点 → 重载右表(${tree} 进右表 api 过滤);TemplateTreeController 运行时构树
|
||||||
treeSchema.put("onEvent", new LinkedHashMap<>(Map.of(
|
treeSchema.put("onEvent", new LinkedHashMap<>(Map.of(
|
||||||
"change", Map.of("actions", List.of(
|
"change", Map.of("actions", List.of(
|
||||||
new LinkedHashMap<>(Map.of("actionType", "reload", "componentId", gridCrudId)))))));
|
new LinkedHashMap<>(Map.of("actionType", "reload", "componentId", gridCrudId)))))));
|
||||||
result.note("tree+grid: treetemplate ref=" + ref
|
result.note("tree+grid: treetemplate ref=" + treeRef
|
||||||
+ " → input-tree source /api/template/" + ref + "/tree(运行时构树)+ 选中重载右表");
|
+ " → input-tree source /api/template/" + treeRef + "/tree(运行时构树)+ 选中重载右表");
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Map<String, Object> gridSchema = new LinkedHashMap<>();
|
Map<String, Object> gridSchema = new LinkedHashMap<>();
|
||||||
gridSchema.put("type", "crud");
|
gridSchema.put("type", "crud");
|
||||||
@@ -942,11 +943,14 @@ public class TemplateToAmisConverter {
|
|||||||
String gridEntityId = gridTemplate.attributeValue("entityid", "");
|
String gridEntityId = gridTemplate.attributeValue("entityid", "");
|
||||||
String gridTable = resolveTableName(gridEntityId, result);
|
String gridTable = resolveTableName(gridEntityId, result);
|
||||||
if (gridTable != null && !gridTable.isBlank() && !rcol.isBlank()) {
|
if (gridTable != null && !gridTable.isBlank() && !rcol.isBlank()) {
|
||||||
// 右表复用合成搜索流(已存在、可用),按选中树节点值过滤;原 /api/template/{id}/list 是不存在端点(404)
|
// 右表走子树聚合端点 /api/template/treegrid:点中节点+全部后代 id 以 IN 过滤合成搜索流。
|
||||||
gridSchema.put("api", "/api/flow/" + gridTable.toUpperCase() + "_SEARCH?"
|
// 这样点父级也出整棵子树的业务行(业务行常只挂叶子节点,对齐老系统)。treeRef 与左树同源。
|
||||||
+ rcol + "=${tree}&page=${page}&perPage=${perPage}");
|
// 端点内部委托合成 _SEARCH,_DISPLAY__/数据权限/分页全在。空 node(未选中)→ 全部行。
|
||||||
|
gridSchema.put("api", "/api/template/treegrid?treeRef=" + treeRef
|
||||||
|
+ "&gridTable=" + gridTable.toUpperCase() + "&rcol=" + rcol
|
||||||
|
+ "&node=${tree}&page=${page}&perPage=${perPage}");
|
||||||
result.note("tree+grid: gridtemplate 实体=" + gridTable + " rcolumn=" + rcol
|
result.note("tree+grid: gridtemplate 实体=" + gridTable + " rcolumn=" + rcol
|
||||||
+ " → 右表 /api/flow/" + gridTable.toUpperCase() + "_SEARCH?" + rcol + "=${tree}(合成搜索流按树节点过滤)");
|
+ " → 右表 /api/template/treegrid(子树聚合 IN 过滤,treeRef=" + treeRef + ")");
|
||||||
} else {
|
} else {
|
||||||
result.degrade("tree+grid: gridtemplate 实体/rcolumn 解析失败(entityid=" + gridEntityId
|
result.degrade("tree+grid: gridtemplate 实体/rcolumn 解析失败(entityid=" + gridEntityId
|
||||||
+ ",rcol=" + rcol + ") → 右表数据源缺");
|
+ ",rcol=" + rcol + ") → 右表数据源缺");
|
||||||
|
|||||||
@@ -403,8 +403,9 @@ class TemplateToAmisConverterTest {
|
|||||||
@Test
|
@Test
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
void tree_treeGrid引用式() throws Exception {
|
void tree_treeGrid引用式() throws Exception {
|
||||||
// TreeGrid:左 input-tree 走 /api/template/{ref}/tree(运行时构树),右 crud 走合成 _SEARCH
|
// TreeGrid:左 input-tree 走 /api/template/{ref}/tree(运行时构树),右 crud 走子树聚合端点
|
||||||
// 按 rcolumn 过滤,选中树节点 onEvent 重载右表。converterWithMeta 使 resolveTableName→PM_DEMO。
|
// /api/template/treegrid(点中节点+后代 IN 过滤),选中树节点 onEvent 重载右表。
|
||||||
|
// converterWithMeta 使 resolveTableName→PM_DEMO。
|
||||||
TemplateToAmisConverter c = converterWithMeta(new String[0][], new String[0][]);
|
TemplateToAmisConverter c = converterWithMeta(new String[0][], new String[0][]);
|
||||||
String xml = "<tree>"
|
String xml = "<tree>"
|
||||||
+ "<treetemplate ref='t1' entityid='e1'/>"
|
+ "<treetemplate ref='t1' entityid='e1'/>"
|
||||||
@@ -417,12 +418,18 @@ class TemplateToAmisConverterTest {
|
|||||||
Map<String, Object> gridItem = (Map<String, Object>) cols.get(1).get("body");
|
Map<String, Object> gridItem = (Map<String, Object>) cols.get(1).get("body");
|
||||||
assertEquals("input-tree", treeItem.get("type"));
|
assertEquals("input-tree", treeItem.get("type"));
|
||||||
assertEquals("/api/template/t1/tree", treeItem.get("source"));
|
assertEquals("/api/template/t1/tree", treeItem.get("source"));
|
||||||
|
assertEquals(Boolean.TRUE, treeItem.get("heightAuto"), "BUG1:树须 heightAuto 撑高,不可定高滚动只露一半");
|
||||||
assertNotNull(treeItem.get("onEvent"), "选中树节点应触发右表重载");
|
assertNotNull(treeItem.get("onEvent"), "选中树节点应触发右表重载");
|
||||||
assertEquals("crud", gridItem.get("type"));
|
assertEquals("crud", gridItem.get("type"));
|
||||||
String api = (String) gridItem.get("api");
|
String api = (String) gridItem.get("api");
|
||||||
assertTrue(api.startsWith("/api/flow/PM_DEMO_SEARCH?EVENTID=${tree}"),
|
// BUG2:右表走子树聚合端点,携 treeRef(与左树同源)/gridTable/rcol/node=${tree}
|
||||||
"右表应走合成搜索流按 rcol 过滤,实际: " + api);
|
assertTrue(api.startsWith("/api/template/treegrid?"),
|
||||||
assertFalse(api.contains("/api/template/"), "不应再用不存在的 /api/template/list 端点");
|
"右表应走子树聚合端点,实际: " + api);
|
||||||
|
assertTrue(api.contains("treeRef=t1"), "应携左树同源 treeRef,实际: " + api);
|
||||||
|
assertTrue(api.contains("gridTable=PM_DEMO"), "应携右表实体表名,实际: " + api);
|
||||||
|
assertTrue(api.contains("rcol=EVENTID"), "应携 rcolumn,实际: " + api);
|
||||||
|
assertTrue(api.contains("node=${tree}"), "应把选中节点作 node 传入,实际: " + api);
|
||||||
|
assertFalse(api.contains("/api/flow/"), "不应再裸用合成流精确匹配(无子树聚合)");
|
||||||
}
|
}
|
||||||
|
|
||||||
// ── report (A 类) ────────────────────────────────────────────────────
|
// ── report (A 类) ────────────────────────────────────────────────────
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
package com.eip4.platform.metadata.web;
|
package com.eip4.platform.metadata.web;
|
||||||
|
|
||||||
|
import java.util.ArrayDeque;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Deque;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.LinkedHashSet;
|
import java.util.LinkedHashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -14,9 +17,11 @@ import org.dom4j.Element;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.dao.DataAccessException;
|
import org.springframework.dao.DataAccessException;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.jdbc.core.JdbcTemplate;
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -31,7 +36,12 @@ import org.springframework.web.bind.annotation.RestController;
|
|||||||
* <p><b>构树算法</b>:把所有层级的行装进单一 {@code id→节点} 映射(id 归一化大写去空白),再按各行所在
|
* <p><b>构树算法</b>:把所有层级的行装进单一 {@code id→节点} 映射(id 归一化大写去空白),再按各行所在
|
||||||
* 层级的 parentid 列值找父节点(父在任意层级皆可,id 全局唯一)——父在则挂为 children,否则为根。
|
* 层级的 parentid 列值找父节点(父在任意层级皆可,id 全局唯一)——父在则挂为 children,否则为根。
|
||||||
* 一次性 eager 构建(树/分类表通常小);节点总数封顶 {@code MAX_NODES},超限截断并 WARN。
|
* 一次性 eager 构建(树/分类表通常小);节点总数封顶 {@code MAX_NODES},超限截断并 WARN。
|
||||||
* 每个节点 {@code value}=原始主键(供右表 {@code ?rcol=${tree}} 过滤),{@code label}=实体显示列。
|
* 每个节点 {@code value}=原始主键(供右表过滤),{@code label}=实体显示列。
|
||||||
|
*
|
||||||
|
* <p><b>子树聚合(右表过滤)</b>:多级树里业务行往往只挂在叶子节点(如文件挂「技术部」而非父级「技术中心」),
|
||||||
|
* 老系统点父节点时聚合整棵子树。本控制器的 {@code /api/template/treegrid} 端点解析「点中节点+全部后代」的
|
||||||
|
* id 列表,以 {@code List} 值委托合成搜索流 {@code <TABLE>_SEARCH}——引擎 {@code GeneralPO.get_SearchWhereClause}
|
||||||
|
* 见 List 即构 {@code rcol IN (...)}(已转义,无注入面),从而复用 _DISPLAY__/数据权限/分页,无需改保真层。
|
||||||
*/
|
*/
|
||||||
@RestController
|
@RestController
|
||||||
public class TemplateTreeController {
|
public class TemplateTreeController {
|
||||||
@@ -42,9 +52,11 @@ public class TemplateTreeController {
|
|||||||
private static final String[] NAME_FALLBACKS = {"NAME", "SORT_NAME", "FULLNAME", "TITLE", "SUBJECT", "DISPLAYNAME"};
|
private static final String[] NAME_FALLBACKS = {"NAME", "SORT_NAME", "FULLNAME", "TITLE", "SUBJECT", "DISPLAYNAME"};
|
||||||
|
|
||||||
private final JdbcTemplate jdbcTemplate;
|
private final JdbcTemplate jdbcTemplate;
|
||||||
|
private final FlowController flowController;
|
||||||
|
|
||||||
public TemplateTreeController(JdbcTemplate jdbcTemplate) {
|
public TemplateTreeController(JdbcTemplate jdbcTemplate, FlowController flowController) {
|
||||||
this.jdbcTemplate = jdbcTemplate;
|
this.jdbcTemplate = jdbcTemplate;
|
||||||
|
this.flowController = flowController;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 树层级:实体表 + 该级的 parentid 列(null=该级行为根)。 */
|
/** 树层级:实体表 + 该级的 parentid 列(null=该级行为根)。 */
|
||||||
@@ -57,10 +69,107 @@ public class TemplateTreeController {
|
|||||||
String parentRef;
|
String parentRef;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 构树结果:{@code byId}=归一化 id→节点(children 已链);{@code roots}=根级 option 列表(稳定序)。 */
|
||||||
|
private record Built(Map<String, Node> byId, List<Map<String, Object>> roots) {}
|
||||||
|
|
||||||
@GetMapping("/api/template/{templateId}/tree")
|
@GetMapping("/api/template/{templateId}/tree")
|
||||||
public Map<String, Object> tree(@PathVariable String templateId) {
|
public Map<String, Object> tree(@PathVariable String templateId) {
|
||||||
List<Map<String, Object>> options = new ArrayList<>();
|
List<Map<String, Object>> options;
|
||||||
try {
|
try {
|
||||||
|
options = build(templateId).roots();
|
||||||
|
} catch (DataAccessException ex) {
|
||||||
|
log.warn("[TREE] 构树失败 template={}: {}", templateId, ex.getMessage());
|
||||||
|
options = new ArrayList<>();
|
||||||
|
}
|
||||||
|
Map<String, Object> data = new LinkedHashMap<>();
|
||||||
|
data.put("options", options);
|
||||||
|
Map<String, Object> resp = new LinkedHashMap<>();
|
||||||
|
resp.put("status", 0);
|
||||||
|
resp.put("msg", "");
|
||||||
|
resp.put("data", data);
|
||||||
|
return resp;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 树+grid 右表数据源端点(子树聚合)。转换器对 type=4 树+grid 的右表产出
|
||||||
|
* {@code source=/api/template/treegrid?treeRef=&gridTable=&rcol=&node=${tree}&page=&perPage=}。
|
||||||
|
*
|
||||||
|
* <p>{@code node} 为点中的树节点 id(空=未选中,返回全部行);本端点解析「该节点+全部后代」id 列表,
|
||||||
|
* 以 {@code List} 值塞进合成搜索流入参 {@code map[rcol]},委托 {@link FlowController#executePost}——
|
||||||
|
* 引擎见 List 构 {@code rcol IN (子树 id...)},于是点父级也出整棵子树的业务行(对齐老系统)。
|
||||||
|
* 复用合成流故 _DISPLAY__/数据权限/分页全在;{@code gridTable}/{@code rcol} 经 {@code IDENT} 守卫。
|
||||||
|
*/
|
||||||
|
@GetMapping("/api/template/treegrid")
|
||||||
|
public ResponseEntity<Map<String, Object>> treeGrid(
|
||||||
|
@RequestParam String treeRef,
|
||||||
|
@RequestParam String gridTable,
|
||||||
|
@RequestParam String rcol,
|
||||||
|
@RequestParam(required = false) String node,
|
||||||
|
@RequestParam(required = false) String page,
|
||||||
|
@RequestParam(required = false) String perPage) {
|
||||||
|
if (!IDENT.matcher(gridTable).matches() || !IDENT.matcher(rcol).matches()) {
|
||||||
|
Map<String, Object> err = new LinkedHashMap<>();
|
||||||
|
err.put("status", 1);
|
||||||
|
err.put("msg", "非法表名/列名");
|
||||||
|
err.put("data", new LinkedHashMap<>());
|
||||||
|
return ResponseEntity.badRequest().body(err);
|
||||||
|
}
|
||||||
|
Map<String, Object> body = new HashMap<>();
|
||||||
|
if (node != null && !node.isBlank()) {
|
||||||
|
List<String> ids;
|
||||||
|
try {
|
||||||
|
ids = collectSubtreeIds(treeRef, node);
|
||||||
|
} catch (DataAccessException ex) {
|
||||||
|
log.warn("[TREEGRID] 子树解析失败 treeRef={} node={}: {}", treeRef, node, ex.getMessage());
|
||||||
|
ids = List.of(node.trim()); // 退化为精确匹配自身,绝不放空成「全部」
|
||||||
|
}
|
||||||
|
if (!ids.isEmpty()) {
|
||||||
|
body.put(rcol, ids); // List → 引擎 get_SearchWhereClause 构 IN(已转义)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (page != null) {
|
||||||
|
body.put("page", page);
|
||||||
|
}
|
||||||
|
if (perPage != null) {
|
||||||
|
body.put("perPage", perPage);
|
||||||
|
}
|
||||||
|
return flowController.executePost(gridTable.toUpperCase() + "_SEARCH", body);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 解析「点中节点 + 全部后代」的原始 id 列表(供右表 IN 过滤)。节点不在树中(陈旧)→ 退化为仅自身。 */
|
||||||
|
List<String> collectSubtreeIds(String treeRef, String nodeId) {
|
||||||
|
if (nodeId == null || nodeId.isBlank()) {
|
||||||
|
return List.of();
|
||||||
|
}
|
||||||
|
Built built = build(treeRef);
|
||||||
|
Node start = built.byId().get(nodeId.trim().toUpperCase());
|
||||||
|
if (start == null) {
|
||||||
|
return List.of(nodeId.trim());
|
||||||
|
}
|
||||||
|
List<String> ids = new ArrayList<>();
|
||||||
|
Deque<Map<String, Object>> stack = new ArrayDeque<>();
|
||||||
|
stack.push(start.opt);
|
||||||
|
while (!stack.isEmpty() && ids.size() < MAX_NODES) {
|
||||||
|
Map<String, Object> opt = stack.pop();
|
||||||
|
Object v = opt.get("value");
|
||||||
|
if (v != null) {
|
||||||
|
ids.add(String.valueOf(v));
|
||||||
|
}
|
||||||
|
if (opt.get("children") instanceof List<?> kids) {
|
||||||
|
for (Object k : kids) {
|
||||||
|
if (k instanceof Map<?, ?> child) {
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
Map<String, Object> co = (Map<String, Object>) child;
|
||||||
|
stack.push(co);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ids;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 构树:装载各层级行 → 按 parentid 链父 → 返回 byId 索引 + 根级 option(稳定序)。 */
|
||||||
|
private Built build(String templateId) {
|
||||||
List<Level> levels = resolveLevels(templateId);
|
List<Level> levels = resolveLevels(templateId);
|
||||||
Map<String, Node> byId = new LinkedHashMap<>();
|
Map<String, Node> byId = new LinkedHashMap<>();
|
||||||
List<Node> order = new ArrayList<>();
|
List<Node> order = new ArrayList<>();
|
||||||
@@ -71,24 +180,16 @@ public class TemplateTreeController {
|
|||||||
}
|
}
|
||||||
loadLevel(lv, byId, order);
|
loadLevel(lv, byId, order);
|
||||||
}
|
}
|
||||||
|
List<Map<String, Object>> roots = new ArrayList<>();
|
||||||
for (Node n : order) {
|
for (Node n : order) {
|
||||||
Node parent = n.parentRef != null ? byId.get(n.parentRef) : null;
|
Node parent = n.parentRef != null ? byId.get(n.parentRef) : null;
|
||||||
if (parent != null && parent != n) {
|
if (parent != null && parent != n) {
|
||||||
parent.children.add(n.opt);
|
parent.children.add(n.opt);
|
||||||
} else {
|
} else {
|
||||||
options.add(n.opt);
|
roots.add(n.opt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (DataAccessException ex) {
|
return new Built(byId, roots);
|
||||||
log.warn("[TREE] 构树失败 template={}: {}", templateId, ex.getMessage());
|
|
||||||
}
|
|
||||||
Map<String, Object> data = new LinkedHashMap<>();
|
|
||||||
data.put("options", options);
|
|
||||||
Map<String, Object> resp = new LinkedHashMap<>();
|
|
||||||
resp.put("status", 0);
|
|
||||||
resp.put("msg", "");
|
|
||||||
resp.put("data", data);
|
|
||||||
return resp;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 解析树模板 XML → 各层级(实体表 + parentid 列)。entityId→tablename;同实体取有 parentid 的声明(自嵌套)。 */
|
/** 解析树模板 XML → 各层级(实体表 + parentid 列)。entityId→tablename;同实体取有 parentid 的声明(自嵌套)。 */
|
||||||
|
|||||||
@@ -0,0 +1,168 @@
|
|||||||
|
package com.eip4.platform.metadata.web;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyString;
|
||||||
|
import static org.mockito.ArgumentMatchers.contains;
|
||||||
|
import static org.mockito.ArgumentMatchers.eq;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.never;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.mockito.ArgumentCaptor;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TemplateTreeController 单测(BUG5 空白补齐 + BUG2 子树聚合守护)。
|
||||||
|
*
|
||||||
|
* <p>mock JdbcTemplate 造一棵自嵌套树(实体 CAT 按 PARENTID 自链:P1→C1→C2),覆盖:
|
||||||
|
* 构树多级嵌套、{@link TemplateTreeController#collectSubtreeIds} 子树展开、
|
||||||
|
* {@code /api/template/treegrid} 端点以 List 值(IN 过滤)委托 FlowController、IDENT 守卫、空 node 返全部。
|
||||||
|
*/
|
||||||
|
class TemplateTreeControllerTest {
|
||||||
|
|
||||||
|
private static final String TREE_REF = "tref-1";
|
||||||
|
/** 自嵌套树 XML:根节点 entityid=E1,parentid=PARENTID(同实体按 PARENTID 链父)。 */
|
||||||
|
private static final String TREE_XML =
|
||||||
|
"<tree isshowroot='true' rootname='分类'>"
|
||||||
|
+ "<node entityid='E1' parentid='PARENTID'><nextnodes/></node></tree>";
|
||||||
|
|
||||||
|
private JdbcTemplate jdbc;
|
||||||
|
private FlowController flow;
|
||||||
|
private TemplateTreeController controller;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||||
|
void setup() {
|
||||||
|
jdbc = mock(JdbcTemplate.class);
|
||||||
|
flow = mock(FlowController.class);
|
||||||
|
controller = new TemplateTreeController(jdbc, flow);
|
||||||
|
|
||||||
|
// resolveLevels:treeRef → 树 XML
|
||||||
|
when(jdbc.queryForList(contains("view_template"), eq(String.class), eq(TREE_REF)))
|
||||||
|
.thenReturn(List.of(TREE_XML));
|
||||||
|
// entityId E1 → 表 CAT
|
||||||
|
when(jdbc.queryForList(contains("tablename"), eq(String.class), eq("E1")))
|
||||||
|
.thenReturn(List.of("CAT"));
|
||||||
|
// 实体显示列 isentitydisplay=1 → NAME
|
||||||
|
when(jdbc.queryForList(contains("isentitydisplay"), eq(String.class), eq("CAT")))
|
||||||
|
.thenReturn(List.of("NAME"));
|
||||||
|
// 物理列(守卫显示列/主键存在)
|
||||||
|
when(jdbc.queryForList(contains("information_schema"), eq(String.class), eq("CAT")))
|
||||||
|
.thenReturn(List.of("CAT_ID", "NAME", "PARENTID"));
|
||||||
|
// loadLevel:CAT 三行,P1(根) → C1 → C2 自链
|
||||||
|
List<Map<String, Object>> rows = new ArrayList<>();
|
||||||
|
rows.add(row("P1", "顶级分类", null));
|
||||||
|
rows.add(row("C1", "二级分类", "P1"));
|
||||||
|
rows.add(row("C2", "三级分类", "C1"));
|
||||||
|
when((List) jdbc.queryForList(contains("FROM eip3.CAT"))).thenReturn((List) rows);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Map<String, Object> row(String k, String lbl, String pref) {
|
||||||
|
Map<String, Object> m = new LinkedHashMap<>();
|
||||||
|
m.put("k", k);
|
||||||
|
m.put("lbl", lbl);
|
||||||
|
m.put("pref", pref);
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("构树:自嵌套多级正确嵌套 P1>C1>C2")
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
void tree_多级嵌套() {
|
||||||
|
Map<String, Object> resp = controller.tree(TREE_REF);
|
||||||
|
assertEquals(0, resp.get("status"));
|
||||||
|
Map<String, Object> data = (Map<String, Object>) resp.get("data");
|
||||||
|
List<Map<String, Object>> roots = (List<Map<String, Object>>) data.get("options");
|
||||||
|
assertEquals(1, roots.size(), "仅 P1 为根");
|
||||||
|
Map<String, Object> p1 = roots.get(0);
|
||||||
|
assertEquals("P1", p1.get("value"));
|
||||||
|
assertEquals("顶级分类", p1.get("label"));
|
||||||
|
List<Map<String, Object>> c1List = (List<Map<String, Object>>) p1.get("children");
|
||||||
|
assertEquals(1, c1List.size());
|
||||||
|
assertEquals("C1", c1List.get(0).get("value"));
|
||||||
|
List<Map<String, Object>> c2List = (List<Map<String, Object>>) c1List.get(0).get("children");
|
||||||
|
assertEquals(1, c2List.size());
|
||||||
|
assertEquals("C2", c2List.get(0).get("value"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("子树聚合:点父级 P1 → [P1,C1,C2];点中间 C1 → [C1,C2]")
|
||||||
|
void subtree_展开后代() {
|
||||||
|
assertEquals(List.of("P1", "C1", "C2"), controller.collectSubtreeIds(TREE_REF, "P1"));
|
||||||
|
assertEquals(List.of("C1", "C2"), controller.collectSubtreeIds(TREE_REF, "C1"));
|
||||||
|
assertEquals(List.of("C2"), controller.collectSubtreeIds(TREE_REF, "C2"), "叶子只含自身");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("子树聚合:节点不在树中(陈旧) → 退化为仅自身,绝不放空成全部")
|
||||||
|
void subtree_陈旧节点退化自身() {
|
||||||
|
assertEquals(List.of("X-NOT-IN-TREE"), controller.collectSubtreeIds(TREE_REF, "X-NOT-IN-TREE"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("子树聚合:空节点 → 空列表(端点据此返回全部行)")
|
||||||
|
void subtree_空节点() {
|
||||||
|
assertTrue(controller.collectSubtreeIds(TREE_REF, "").isEmpty());
|
||||||
|
assertTrue(controller.collectSubtreeIds(TREE_REF, null).isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("treegrid 端点:点父级 → 以 List(子树 id) 委托 _SEARCH(引擎构 IN)+ 透传分页")
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
void treeGrid_子树IN委托() {
|
||||||
|
when(flow.executePost(eq("OA_KNOWLEDGE_FILE_SEARCH"), any()))
|
||||||
|
.thenReturn(ResponseEntity.ok(Map.of("status", 0)));
|
||||||
|
|
||||||
|
controller.treeGrid(TREE_REF, "OA_KNOWLEDGE_FILE", "FOLDERID", "P1", "2", "10");
|
||||||
|
|
||||||
|
ArgumentCaptor<Map<String, Object>> cap = ArgumentCaptor.forClass(Map.class);
|
||||||
|
verify(flow).executePost(eq("OA_KNOWLEDGE_FILE_SEARCH"), cap.capture());
|
||||||
|
Map<String, Object> body = cap.getValue();
|
||||||
|
assertEquals(List.of("P1", "C1", "C2"), body.get("FOLDERID"), "rcol 须为子树 id 列表(List→引擎 IN)");
|
||||||
|
assertEquals("2", body.get("page"));
|
||||||
|
assertEquals("10", body.get("perPage"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("treegrid 端点:空 node(未选中) → 不带 rcol 过滤(返回全部) + 仍透传分页")
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
void treeGrid_空node返全部() {
|
||||||
|
when(flow.executePost(eq("OA_KNOWLEDGE_FILE_SEARCH"), any()))
|
||||||
|
.thenReturn(ResponseEntity.ok(Map.of("status", 0)));
|
||||||
|
|
||||||
|
controller.treeGrid(TREE_REF, "OA_KNOWLEDGE_FILE", "FOLDERID", "", "1", "10");
|
||||||
|
|
||||||
|
ArgumentCaptor<Map<String, Object>> cap = ArgumentCaptor.forClass(Map.class);
|
||||||
|
verify(flow).executePost(eq("OA_KNOWLEDGE_FILE_SEARCH"), cap.capture());
|
||||||
|
Map<String, Object> body = cap.getValue();
|
||||||
|
assertFalse(body.containsKey("FOLDERID"), "未选中不应带 rcol 过滤");
|
||||||
|
assertEquals("1", body.get("page"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("treegrid 端点:非法表名/列名 → 400,不触达引擎")
|
||||||
|
void treeGrid_IDENT守卫() {
|
||||||
|
ResponseEntity<Map<String, Object>> resp =
|
||||||
|
controller.treeGrid(TREE_REF, "BAD TABLE;DROP", "FOLDERID", "P1", "1", "10");
|
||||||
|
assertTrue(resp.getStatusCode().is4xxClientError());
|
||||||
|
verify(flow, never()).executePost(anyString(), any());
|
||||||
|
|
||||||
|
ResponseEntity<Map<String, Object>> resp2 =
|
||||||
|
controller.treeGrid(TREE_REF, "OA_KNOWLEDGE_FILE", "FOLDER ID--", "P1", "1", "10");
|
||||||
|
assertTrue(resp2.getStatusCode().is4xxClientError());
|
||||||
|
verify(flow, never()).executePost(anyString(), any());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user