feat(design): RecipePageComposer 共享蓝图装配器 + PageBlueprintField 引用字段扩展(向后兼容)
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -6,5 +6,13 @@ public record PageBlueprintField(
|
|||||||
String type,
|
String type,
|
||||||
boolean required,
|
boolean required,
|
||||||
boolean listVisible,
|
boolean listVisible,
|
||||||
boolean formVisible) {
|
boolean formVisible,
|
||||||
|
String referenceTable,
|
||||||
|
String displayColumn) {
|
||||||
|
|
||||||
|
/** 6-arg 兼容构造:非引用字段(referenceTable/displayColumn 置空)。保持既有调用方/测试零改。 */
|
||||||
|
public PageBlueprintField(String name, String label, String type,
|
||||||
|
boolean required, boolean listVisible, boolean formVisible) {
|
||||||
|
this(name, label, type, required, listVisible, formVisible, null, null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,94 @@
|
|||||||
|
package com.eip4.platform.metadata.schema.blueprint;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 共享蓝图装配器:实体属性 → 受控 {@link PageBlueprint} → {@link PageBlueprintCompiler#compile}。
|
||||||
|
*
|
||||||
|
* <p>人工单步端点(DesignFeatureController)与未来 AI 路径共用同一装配口径(DRY),UI/UE 规则集中在
|
||||||
|
* recipe/compiler 而非散落在调用方。本类不写库、不校验白名单(严格 typed 闸在端点层),只负责装配+编译。
|
||||||
|
*
|
||||||
|
* <p>装配口径镜像 {@code AiFeatureDraftGenerator.pageBlueprint}:tree-crud 的 PARENTID 列 listVisible=false;
|
||||||
|
* displayField 缺省回退首个 NAME/required 列。引用字段(type=reference + referenceTable/displayColumn)
|
||||||
|
* 透传到 {@link PageBlueprintField},由 compiler 渲染显名列(见 applyColumnRender)。
|
||||||
|
*/
|
||||||
|
public final class RecipePageComposer {
|
||||||
|
|
||||||
|
private final PageBlueprintCompiler compiler;
|
||||||
|
|
||||||
|
public RecipePageComposer(ObjectMapper objectMapper) {
|
||||||
|
this.compiler = new PageBlueprintCompiler(objectMapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ObjectNode compose(Spec spec) {
|
||||||
|
if (spec == null) {
|
||||||
|
throw new IllegalArgumentException("compose spec 不能为空");
|
||||||
|
}
|
||||||
|
PageBlueprint blueprint = toBlueprint(spec);
|
||||||
|
return compiler.compile(blueprint);
|
||||||
|
}
|
||||||
|
|
||||||
|
PageBlueprint toBlueprint(Spec spec) {
|
||||||
|
List<PageBlueprintField> fields = new ArrayList<>();
|
||||||
|
for (AttrSpec attr : spec.attributes()) {
|
||||||
|
boolean treeParentField = PageBlueprint.RECIPE_TREE_CRUD.equals(spec.recipe())
|
||||||
|
&& "PARENTID".equals(attr.column());
|
||||||
|
fields.add(new PageBlueprintField(
|
||||||
|
attr.column(), attr.display(), attr.type(),
|
||||||
|
attr.required(), !treeParentField, true,
|
||||||
|
attr.referenceTable(), attr.displayColumn()));
|
||||||
|
}
|
||||||
|
String displayField = firstNonBlank(spec.displayField(), deriveDisplayField(spec.attributes()));
|
||||||
|
int pageSize = spec.pageSize() < 1 ? 20 : spec.pageSize();
|
||||||
|
// templateId 由端点层服务端生成;compose 阶段用占位(compiler.validate 只要求非空)。
|
||||||
|
return new PageBlueprint(
|
||||||
|
PageBlueprint.VERSION, spec.recipe(),
|
||||||
|
"compose-placeholder", spec.title(), spec.tableName(), spec.entityName(),
|
||||||
|
spec.tableName() + "_ID", displayField, fields,
|
||||||
|
true, true, true, pageSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String deriveDisplayField(List<AttrSpec> attrs) {
|
||||||
|
for (AttrSpec a : attrs) {
|
||||||
|
if ("NAME".equals(a.column()) || a.required()) {
|
||||||
|
return a.column();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return attrs.isEmpty() ? "NAME" : attrs.get(0).column();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String firstNonBlank(String preferred, String fallback) {
|
||||||
|
if (preferred != null && !preferred.isBlank()) {
|
||||||
|
return preferred.trim();
|
||||||
|
}
|
||||||
|
return fallback;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 装配输入:等价于 GET /api/meta/entity/{table}/attributes 的读取结果 + recipe/title/displayField/pageSize。 */
|
||||||
|
public record Spec(
|
||||||
|
String tableName,
|
||||||
|
String entityName,
|
||||||
|
String recipe,
|
||||||
|
String title,
|
||||||
|
String displayField,
|
||||||
|
int pageSize,
|
||||||
|
List<AttrSpec> attributes) {
|
||||||
|
public Spec {
|
||||||
|
attributes = attributes == null ? List.of() : List.copyOf(attributes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 单个实体属性。referenceTable/displayColumn 仅引用列非空(type=reference)。 */
|
||||||
|
public record AttrSpec(
|
||||||
|
String column,
|
||||||
|
String display,
|
||||||
|
String type,
|
||||||
|
boolean required,
|
||||||
|
String referenceTable,
|
||||||
|
String displayColumn) {
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,80 @@
|
|||||||
|
package com.eip4.platform.metadata.schema.blueprint;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||||
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||||
|
|
||||||
|
class RecipePageComposerTest {
|
||||||
|
|
||||||
|
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||||
|
private final RecipePageComposer composer = new RecipePageComposer(MAPPER);
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("1. compose maintenance-crud → crud.api == /api/flow/<TABLE>_SEARCH,列= listVisible 业务列")
|
||||||
|
void composesMaintenanceCrud() {
|
||||||
|
RecipePageComposer.Spec spec = new RecipePageComposer.Spec(
|
||||||
|
"EIP4AI_CUSTOMER", "客户", PageBlueprint.RECIPE_MAINTENANCE_CRUD,
|
||||||
|
"客户维护", "NAME", 20,
|
||||||
|
List.of(
|
||||||
|
new RecipePageComposer.AttrSpec("NAME", "名称", "string", true, null, null),
|
||||||
|
new RecipePageComposer.AttrSpec("LEVEL", "等级", "integer", false, null, null)));
|
||||||
|
|
||||||
|
ObjectNode page = composer.compose(spec);
|
||||||
|
|
||||||
|
assertEquals("page", page.path("type").asText());
|
||||||
|
assertEquals("客户维护", page.path("title").asText());
|
||||||
|
ObjectNode crud = (ObjectNode) page.path("body");
|
||||||
|
assertEquals("/api/flow/EIP4AI_CUSTOMER_SEARCH", crud.path("api").asText());
|
||||||
|
ArrayNode columns = (ArrayNode) crud.path("columns");
|
||||||
|
assertEquals(2, columns.size());
|
||||||
|
assertEquals("NAME", columns.get(0).path("name").asText());
|
||||||
|
assertEquals("LEVEL", columns.get(1).path("name").asText());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("2. compose tree-crud → 注入 PARENTID 字段且列表不显示该列")
|
||||||
|
void composesTreeCrudWithParent() {
|
||||||
|
RecipePageComposer.Spec spec = new RecipePageComposer.Spec(
|
||||||
|
"EIP4AI_CATEGORY", "分类", PageBlueprint.RECIPE_TREE_CRUD,
|
||||||
|
"分类维护", "NAME", 20,
|
||||||
|
List.of(
|
||||||
|
new RecipePageComposer.AttrSpec("NAME", "名称", "string", true, null, null),
|
||||||
|
new RecipePageComposer.AttrSpec("PARENTID", "上级", "string", false, null, null)));
|
||||||
|
|
||||||
|
ObjectNode page = composer.compose(spec);
|
||||||
|
ObjectNode crud = (ObjectNode) page.path("body");
|
||||||
|
assertEquals("eip4aiCategoryTreeCrud", crud.path("name").asText());
|
||||||
|
// 业务列里不含 PARENTID(compiler.treeColumns 跳过 PARENTID),但保留「操作」列。
|
||||||
|
ArrayNode columns = (ArrayNode) crud.path("columns");
|
||||||
|
boolean hasParentColumn = false;
|
||||||
|
for (int i = 0; i < columns.size(); i++) {
|
||||||
|
if ("PARENTID".equals(columns.get(i).path("name").asText())) {
|
||||||
|
hasParentColumn = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assertTrue(!hasParentColumn);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("3. displayField 省略时回退首个 NAME/required 列")
|
||||||
|
void derivesDisplayFieldWhenBlank() {
|
||||||
|
RecipePageComposer.Spec spec = new RecipePageComposer.Spec(
|
||||||
|
"EIP4AI_ITEM", "条目", PageBlueprint.RECIPE_MAINTENANCE_CRUD,
|
||||||
|
"条目维护", null, 20,
|
||||||
|
List.of(
|
||||||
|
new RecipePageComposer.AttrSpec("CODE", "编码", "string", false, null, null),
|
||||||
|
new RecipePageComposer.AttrSpec("NAME", "名称", "string", true, null, null)));
|
||||||
|
|
||||||
|
ObjectNode page = composer.compose(spec);
|
||||||
|
// displayField 回退到 NAME,filter 兜底用它(此处只需 compose 成功不抛)。
|
||||||
|
assertEquals("page", page.path("type").asText());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user