统一 id→名称解析:虚拟显示列用 columnsql 计算表达式(树/列表引用列同源,匹配老系统)

根因(dogfooding:材料分类树/列表全显 UUID):实体显示列(isentitydisplay)常是虚拟计算列
(如 RESOURCENAMEVIEW='['||CODE||']'||RESOURCENAME),各处 resolver 只 SELECT 物理列→守卫跳过→回退主键。
老系统 GeneralPO 用 columnsql 算出显示值。新增共享 EntityDisplayColumns.virtualColumnExpr 抽 columnsql
表达式,TemplateTreeController(树)+ UserColumnResolver(列表引用列)统一复用(表达式前导(跳 IDENT 守卫,
可信元数据)。实证:树 label '[root]材料分类'、列表 RESOURCETYPEID_DISPLAY__='[DA]安装A'(同老系统)。271测绿

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-14 12:05:02 +08:00
parent 8165b01874
commit 3feefb3f44
3 changed files with 104 additions and 12 deletions

View File

@@ -0,0 +1,71 @@
package com.eip4.platform.metadata.flow;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
/**
* 实体「显示列」统一解析的虚拟列分支 —— 消除 id→名称解析散落三处(树/列表引用列/选择器)的同病。
*
* <p><b>根因</b>(dogfooding):实体的显示列(SYS_ENTITYATTRIBUTE.isentitydisplay=1)常是<b>物理不存在的
* 虚拟列</b>(由 columnsql 计算,如 {@code RESOURCENAMEVIEW = '['||CODE||']'||RESOURCENAME})。各处 resolver
* 只 SELECT 物理列,遇虚拟列守卫跳过 → 回退主键 → 树/列表/选择器全显 UUID。
*
* <p><b>老系统</b>(GeneralPO/POInfo)是用虚拟列的 columnsql <b>算出</b>显示值(如「[001]材料分类」)。本类
* 统一复刻:把 columnsql 的 SQL 表达式抽出来,供 {@code SELECT <expr> AS lbl FROM table} 直接计算,三处
* resolver 共用,与老系统逐字一致。
*
* <p><b>安全</b>:columnsql 来自元数据表(管理员维护,非用户输入),与老引擎直接执行 columnsql 同一信任模型;
* 调用方对表名/键列仍走 IDENT 守卫 + 绑定参数,仅此可信元数据表达式豁免 IDENT(以前导 {@code (} 标记)。
*/
public final class EntityDisplayColumns {
private EntityDisplayColumns() {
}
/** 抽取 {@code SELECT <expr> AS <col>} 中的 {@code <expr>}(贪婪到最后一个 AS,容末尾分号)。 */
private static final Pattern SELECT_AS =
Pattern.compile("(?is)^\\s*select\\s+(.+?)\\s+as\\s+\\w+\\s*;?\\s*$");
/**
* 虚拟显示列 → 其 columnsql 计算表达式(已包一层括号、可直接嵌 {@code SELECT <expr> AS lbl});
* 非虚拟 / 无 columnsql / 解析失败 → {@code null}(调用方走原物理列回退)。
*
* @param jdbc 数据源
* @param upperTable 实体表名(大写)
* @param displayColUpper 元数据声明的显示列名(大写),疑似虚拟列
*/
public static String virtualColumnExpr(JdbcTemplate jdbc, String upperTable, String displayColUpper) {
if (jdbc == null || upperTable == null || displayColUpper == null) {
return null;
}
String columnsql;
try {
columnsql = jdbc.query(
"SELECT ea.columnsql FROM eip3.sys_entityattribute ea "
+ "JOIN eip3.sys_entity e ON UPPER(TRIM(e.sys_entity_id)) = UPPER(TRIM(ea.entityid)) "
+ "WHERE UPPER(TRIM(e.tablename)) = ? AND UPPER(TRIM(ea.columnname)) = ? "
+ "AND ea.columnsql IS NOT NULL",
rs -> rs.next() ? rs.getString(1) : null,
upperTable, displayColUpper);
} catch (DataAccessException ex) {
return null;
}
if (columnsql == null || columnsql.isBlank()) {
return null;
}
Matcher m = SELECT_AS.matcher(columnsql.trim());
if (!m.matches()) {
return null;
}
String expr = m.group(1).trim();
return expr.isBlank() ? null : "(" + expr + ")";
}
/** 该显示「列」是否为 columnsql 计算表达式(而非物理列名)——调用方据此跳过 IDENT 守卫。 */
public static boolean isExpression(String displayCol) {
return displayCol != null && displayCol.startsWith("(");
}
}

View File

@@ -236,17 +236,25 @@ public class UserColumnResolver {
if (entityDisplayCol != null && physExists(phys, entityDisplayCol)) { if (entityDisplayCol != null && physExists(phys, entityDisplayCol)) {
displayCol = entityDisplayCol; displayCol = entityDisplayCol;
} else { } else {
String nameCol = null; // 显示列是虚拟列(物理不存在)→ 统一用其 columnsql 计算表达式(与老系统 GeneralPO/树/选择器一致),
for (String cand : NAME_FALLBACKS) { // 否则引用列在列表里回退主键 → 显示 UUID(dogfooding:材料分类等引用列显 id 根因)。
if (physExists(phys, cand)) { String virtualExpr = entityDisplayCol != null
nameCol = cand; ? EntityDisplayColumns.virtualColumnExpr(jdbcTemplate, upperTarget, entityDisplayCol) : null;
break; if (virtualExpr != null) {
displayCol = virtualExpr; // (expr) 形态,loadDisplayMap 据前导 ( 跳过 IDENT 守卫
} else {
String nameCol = null;
for (String cand : NAME_FALLBACKS) {
if (physExists(phys, cand)) {
nameCol = cand;
break;
}
}
if (nameCol != null) {
displayCol = nameCol;
} else if (identCol != null && !identCol.equalsIgnoreCase(keyCol) && physExists(phys, identCol)) {
displayCol = identCol;
} }
}
if (nameCol != null) {
displayCol = nameCol;
} else if (identCol != null && !identCol.equalsIgnoreCase(keyCol) && physExists(phys, identCol)) {
displayCol = identCol;
} }
} }
return new RefTarget(upperTarget, keyCol, displayCol); return new RefTarget(upperTarget, keyCol, displayCol);
@@ -273,8 +281,10 @@ public class UserColumnResolver {
/** 批量加载某目标表的 id → 显示名进缓存。表/列名经 IDENT 守卫,id 走绑定参数。失败静默跳过。 */ /** 批量加载某目标表的 id → 显示名进缓存。表/列名经 IDENT 守卫,id 走绑定参数。失败静默跳过。 */
private void loadDisplayNames(RefTarget t, Set<String> ids) { private void loadDisplayNames(RefTarget t, Set<String> ids) {
// displayColumn 可能是虚拟列的 columnsql 表达式(前导 `(`,可信元数据)→ 跳过 IDENT;表/键/物理列名仍守卫。
boolean dispIsExpr = EntityDisplayColumns.isExpression(t.displayColumn());
if (!IDENT.matcher(t.table()).matches() || !IDENT.matcher(t.keyColumn()).matches() if (!IDENT.matcher(t.table()).matches() || !IDENT.matcher(t.keyColumn()).matches()
|| !IDENT.matcher(t.displayColumn()).matches()) { || (!dispIsExpr && !IDENT.matcher(t.displayColumn()).matches())) {
return; return;
} }
List<String> all = new ArrayList<>(ids); List<String> all = new ArrayList<>(ids);

View File

@@ -257,7 +257,9 @@ public class TemplateTreeController {
String table = lv.table(); String table = lv.table();
String keyCol = table + "_ID"; String keyCol = table + "_ID";
String displayCol = displayColumn(table, keyCol); String displayCol = displayColumn(table, keyCol);
if (!IDENT.matcher(keyCol).matches() || !IDENT.matcher(displayCol).matches()) { // displayCol 可能是虚拟列的 columnsql 表达式(前导 `(`,可信元数据)→ 跳过 IDENT;物理列名仍守卫。
boolean dispIsExpr = com.eip4.platform.metadata.flow.EntityDisplayColumns.isExpression(displayCol);
if (!IDENT.matcher(keyCol).matches() || (!dispIsExpr && !IDENT.matcher(displayCol).matches())) {
return; return;
} }
String parentCol = lv.parentCol(); String parentCol = lv.parentCol();
@@ -318,6 +320,15 @@ public class TemplateTreeController {
if (entityDisplay != null && physExists(phys, entityDisplay)) { if (entityDisplay != null && physExists(phys, entityDisplay)) {
return entityDisplay; return entityDisplay;
} }
// 显示列是虚拟列(如 RESOURCENAMEVIEW = '['||CODE||']'||RESOURCENAME,物理不存在)→ 统一用其 columnsql
// 计算表达式(与老系统 GeneralPO 一致)。否则树节点 label 回退主键 → 整棵树显示 UUID(材料分类树根因)。
if (entityDisplay != null) {
String expr = com.eip4.platform.metadata.flow.EntityDisplayColumns
.virtualColumnExpr(jdbcTemplate, table, entityDisplay);
if (expr != null) {
return expr; // (expr) 形态,loadLevel 据前导 ( 跳过 IDENT 守卫
}
}
for (String cand : NAME_FALLBACKS) { for (String cand : NAME_FALLBACKS) {
if (physExists(phys, cand)) { if (physExists(phys, cand)) {
return cand; return cand;