P4-5: 实装改写后脚本挂回 amis onEvent 重放链路
转换器侧(事件段):TemplateToAmisConverter.attachScriptOnEvent 在页面
body 挂 inited+change 两类 onEvent,动作链 ajax 调 /api/script/execute-template
(传 templateId/seq/item:${&})→ replayScriptCommands 重放返回指令。
执行端点:ScriptExecuteController /api/script/execute-template 改接
{templateId,seq,item},RewrittenScriptStore 按 (tid,seq) 从改写产物
output/rewritten 取脚本正文(去头注释)经 facade 执行;原按名端点改
-by-name 保留 P4-2 兼容。
测试:RewrittenScriptStoreTest 4 + ScriptExecuteControllerTest 4 +
TemplateToAmisConverterTest 新增 4 onEvent 例;converter 28 / metadata 77 /
ArchUnit 1 全绿。
出入回写报告第五章 §5.3;新坑入手册 §8.5(组件级动作路由)/§8.6(改写产物取数键)。
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
package com.eip4.platform.metadata.script;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 改写后脚本库读取(P4-5 脚本挂回 onEvent 重放链路)。
|
||||
*
|
||||
* <p>S3 改写器(tools/s3_rewriter)把存量脚本按 {@code template_id + 脚本序号} 落盘到
|
||||
* {@code output/rewritten/<template_id>/<seq>.<label>.groovy}(见 P4 脚本迁移执行报告 §1.2)。
|
||||
* 本类按 {@code (templateId, seq)} 定位改写后脚本,去掉头注释块返回可直接执行的正文,
|
||||
* 是「改写后脚本挂回页面 schema」的取数端:转换器生成的 amis onEvent 调
|
||||
* {@code /api/script/execute-template}(带 templateId/seq/item)时由本类供脚本正文。
|
||||
*
|
||||
* <p>路径根经 {@code eip4.script.rewritten-root} 配置,默认相对启动工作目录
|
||||
* {@code tools/s3_rewriter/output/rewritten}(与工程内落盘产物对齐)。
|
||||
*/
|
||||
@Component
|
||||
public class RewrittenScriptStore {
|
||||
|
||||
/** 头注释块前缀(改写器落盘的每段脚本头部,见 §1.2),正文从首个非注释非空行起算。 */
|
||||
private static final String HEADER_PREFIX = "//";
|
||||
|
||||
private final Path root;
|
||||
|
||||
public RewrittenScriptStore(
|
||||
@Value("${eip4.script.rewritten-root:tools/s3_rewriter/output/rewritten}") String rewrittenRoot) {
|
||||
this.root = Path.of(rewrittenRoot);
|
||||
}
|
||||
|
||||
/**
|
||||
* 取改写后脚本正文(去头注释块)。
|
||||
*
|
||||
* @param templateId 模板 ID(落盘目录名)
|
||||
* @param seq 脚本序号(落盘文件名前缀,如 {@code 2.FULL_AUTO.groovy} 的 2)
|
||||
* @return 去头注释的脚本正文;定位失败返回 {@code null}(调用方降级处理,不抛异常)
|
||||
*/
|
||||
public String load(String templateId, int seq) {
|
||||
if (templateId == null || templateId.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
Path dir = root.resolve(templateId);
|
||||
if (!Files.isDirectory(dir)) {
|
||||
return null;
|
||||
}
|
||||
String prefix = seq + ".";
|
||||
try (Stream<Path> files = Files.list(dir)) {
|
||||
Path match = files
|
||||
.filter(p -> p.getFileName().toString().startsWith(prefix)
|
||||
&& p.getFileName().toString().endsWith(".groovy"))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
if (match == null) {
|
||||
return null;
|
||||
}
|
||||
return stripHeader(Files.readString(match, StandardCharsets.UTF_8));
|
||||
} catch (IOException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 去掉改写器头注释块:跳过文件起始处连续的 {@code //} 注释行与空行,从首个实质代码行起返回。
|
||||
* 注释块中间的业务注释(脚本正文里的 {@code //...})不受影响(只剥离起始处的头块)。
|
||||
*/
|
||||
static String stripHeader(String raw) {
|
||||
if (raw == null) {
|
||||
return null;
|
||||
}
|
||||
String[] lines = raw.split("\n", -1);
|
||||
int start = 0;
|
||||
while (start < lines.length) {
|
||||
String trimmed = lines[start].strip();
|
||||
if (trimmed.isEmpty() || trimmed.startsWith(HEADER_PREFIX)) {
|
||||
start++;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
List<String> body = new ArrayList<>();
|
||||
for (int i = start; i < lines.length; i++) {
|
||||
body.add(lines[i]);
|
||||
}
|
||||
return String.join("\n", body).strip();
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,7 @@ import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.dsasp.server.dataDic.binding.ScriptingEnginesManager;
|
||||
import com.eip4.platform.metadata.script.RewrittenScriptStore;
|
||||
import com.eip4.platform.metadata.script.ScriptFacade;
|
||||
|
||||
/**
|
||||
@@ -36,9 +37,11 @@ import com.eip4.platform.metadata.script.ScriptFacade;
|
||||
public class ScriptExecuteController {
|
||||
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
private final RewrittenScriptStore rewrittenScriptStore;
|
||||
|
||||
public ScriptExecuteController(JdbcTemplate jdbcTemplate) {
|
||||
public ScriptExecuteController(JdbcTemplate jdbcTemplate, RewrittenScriptStore rewrittenScriptStore) {
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
this.rewrittenScriptStore = rewrittenScriptStore;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -75,12 +78,15 @@ public class ScriptExecuteController {
|
||||
}
|
||||
|
||||
/**
|
||||
* 按模板名从客户库取真实脚本执行(改写后脚本挂回页面 schema 的端到端验证)。
|
||||
* 按模板名从客户库取「库内原始脚本」执行(P4-2 端到端验证端点,保留兼容)。
|
||||
*
|
||||
* <p>注:P4-5 改写后脚本挂回链路走 {@link #executeTemplateRewritten}(按 templateId/seq 取
|
||||
* 改写产物);本端点按模板名取库内 XML 原始脚本,端点路径区隔为 {@code -by-name}。
|
||||
*
|
||||
* @param template 模板名(VIEW_TEMPLATE.NAME)
|
||||
* @param state 注入 item.ISPLACE 的取值,便于驱动分支
|
||||
*/
|
||||
@PostMapping("/api/script/execute-template")
|
||||
@PostMapping("/api/script/execute-template-by-name")
|
||||
public Map<String, Object> executeTemplate(
|
||||
@RequestParam String template,
|
||||
@RequestParam(required = false) String state) throws Exception {
|
||||
@@ -104,6 +110,61 @@ public class ScriptExecuteController {
|
||||
return resp;
|
||||
}
|
||||
|
||||
/**
|
||||
* 改写后脚本挂回 onEvent 重放链路的执行端点(P4-5)。
|
||||
*
|
||||
* <p>转换器在 amis 页面 onEvent 里配置「调本端点,传 templateId/seq/item」;本端点按
|
||||
* {@code (templateId, seq)} 从改写后脚本库({@link RewrittenScriptStore})取脚本正文,
|
||||
* 经 facade 执行,返回 amis 事件动作指令数组供前端逐条重放(show/hidden/setValue/setOptions/toast 等)。
|
||||
*
|
||||
* <p>请求体:{@code {"templateId":"021e...","seq":2,"item":{"STORE_ID":"X",...}}}<br>
|
||||
* 返回 :{@code {"commands":[{actionType,componentId,args},...], "passed":true, "item":{...}}}
|
||||
*
|
||||
* @param body templateId(落盘目录名)+ seq(脚本序号)+ item(amis 表单当前数据域)
|
||||
*/
|
||||
@PostMapping("/api/script/execute-template")
|
||||
public Map<String, Object> executeTemplateRewritten(@RequestBody Map<String, Object> body) {
|
||||
Map<String, Object> resp = new LinkedHashMap<>();
|
||||
String templateId = body.get("templateId") == null ? null : String.valueOf(body.get("templateId"));
|
||||
if (templateId == null || templateId.isBlank()) {
|
||||
resp.put("passed", false);
|
||||
resp.put("error", "缺少 templateId");
|
||||
return resp;
|
||||
}
|
||||
int seq = body.get("seq") instanceof Number n ? n.intValue() : parseSeq(body.get("seq"));
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> item = body.get("item") instanceof Map
|
||||
? (Map<String, Object>) body.get("item") : new LinkedHashMap<>();
|
||||
|
||||
String script = rewrittenScriptStore.load(templateId, seq);
|
||||
if (script == null || script.isBlank()) {
|
||||
resp.put("passed", false);
|
||||
resp.put("error", "改写后脚本未找到: templateId=" + templateId + " seq=" + seq);
|
||||
return resp;
|
||||
}
|
||||
resp.put("templateId", templateId);
|
||||
resp.put("seq", seq);
|
||||
try {
|
||||
List<Map<String, Object>> commands = runWithFacade("rw:" + templateId + ":" + seq, script, item);
|
||||
resp.put("passed", true);
|
||||
resp.put("item", item);
|
||||
resp.put("commands", commands);
|
||||
resp.put("commandCount", commands.size());
|
||||
} catch (Exception e) {
|
||||
resp.put("passed", false);
|
||||
resp.put("error", e.getMessage());
|
||||
}
|
||||
return resp;
|
||||
}
|
||||
|
||||
private static int parseSeq(Object raw) {
|
||||
try {
|
||||
return raw == null ? 1 : Integer.parseInt(String.valueOf(raw).trim());
|
||||
} catch (NumberFormatException e) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/** 用 facade 动词表执行脚本,返回捕获的 amis 指令。 */
|
||||
private List<Map<String, Object>> runWithFacade(String key, String script, Map<String, Object> item) {
|
||||
ScriptFacade facade = new ScriptFacade(item);
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
package com.eip4.platform.metadata.script;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
/**
|
||||
* P4-5 单测:改写后脚本库读取(templateId/seq 定位 + 头注释剥离)。
|
||||
*/
|
||||
class RewrittenScriptStoreTest {
|
||||
|
||||
@Test
|
||||
void stripHeader_剥离起始头注释块保留正文() {
|
||||
String raw = "// === EIP4 P4 脚本迁移产出 ===\n"
|
||||
+ "// template_id : 0d9eac07\n"
|
||||
+ "// label : FULL_AUTO\n"
|
||||
+ "\n"
|
||||
+ "setFieldVisible(\"BEGINDATE\", false);";
|
||||
String body = RewrittenScriptStore.stripHeader(raw);
|
||||
assertEquals("setFieldVisible(\"BEGINDATE\", false);", body);
|
||||
}
|
||||
|
||||
@Test
|
||||
void stripHeader_不误删正文中部业务注释() {
|
||||
String raw = "// header line\n"
|
||||
+ "def searchMap = [\"UNITTYPE\":7];//建设单位\n"
|
||||
+ "setFieldFilter(\"BUILDCOMPANY\", searchMap);";
|
||||
String body = RewrittenScriptStore.stripHeader(raw);
|
||||
assertTrue(body.startsWith("def searchMap"), body);
|
||||
assertTrue(body.contains("//建设单位"), "正文中部注释不应被剥离");
|
||||
}
|
||||
|
||||
@Test
|
||||
void load_按templateId与seq定位改写脚本去头注释(@TempDir Path tmp) throws IOException {
|
||||
Path dir = tmp.resolve("tid-abc");
|
||||
Files.createDirectories(dir);
|
||||
Files.writeString(dir.resolve("2.FULL_AUTO.groovy"),
|
||||
"// === EIP4 P4 脚本迁移产出 ===\n// seq : 2\n\naddSubTableButton(\"GOODSDETAIL\", \"SELECT\", 0);",
|
||||
StandardCharsets.UTF_8);
|
||||
RewrittenScriptStore store = new RewrittenScriptStore(tmp.toString());
|
||||
String body = store.load("tid-abc", 2);
|
||||
assertEquals("addSubTableButton(\"GOODSDETAIL\", \"SELECT\", 0);", body);
|
||||
}
|
||||
|
||||
@Test
|
||||
void load_缺目录或缺序号返回null(@TempDir Path tmp) {
|
||||
RewrittenScriptStore store = new RewrittenScriptStore(tmp.toString());
|
||||
assertNull(store.load("no-such-tid", 1));
|
||||
assertNull(store.load(null, 1));
|
||||
assertNull(store.load("", 1));
|
||||
assertFalse(store.load("no-such-tid", 9) != null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
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.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
import com.eip4.platform.metadata.script.RewrittenScriptStore;
|
||||
|
||||
/**
|
||||
* P4-5 单测:改写后脚本挂回执行端点(/api/script/execute-template)。
|
||||
*
|
||||
* <p>构造一个落盘的改写后脚本(templateId/seq 命名 + 头注释块),经端点取出去头执行,
|
||||
* 断言返回 amis 事件动作指令数组(actionType/componentId/args),是「改写后脚本挂回」取数→
|
||||
* 执行→指令产出的端到端单测(脚本经保真层引擎真实跑通,非桩)。
|
||||
*/
|
||||
class ScriptExecuteControllerTest {
|
||||
|
||||
private ScriptExecuteController controller(Path root) {
|
||||
return new ScriptExecuteController(mock(JdbcTemplate.class), new RewrittenScriptStore(root.toString()));
|
||||
}
|
||||
|
||||
private void writeScript(Path root, String tid, String fileName, String content) throws IOException {
|
||||
Path dir = root.resolve(tid);
|
||||
Files.createDirectories(dir);
|
||||
Files.writeString(dir.resolve(fileName), content, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
void executeTemplateRewritten_取改写脚本执行产出amis指令(@TempDir Path root) throws IOException {
|
||||
// 福利保险结算单 seq=1:setFieldVisible("BEGINDATE", false) → hidden
|
||||
writeScript(root, "0d9eac07", "1.FULL_AUTO.groovy",
|
||||
"// === EIP4 P4 脚本迁移产出 ===\n// seq : 1\n\nsetFieldVisible(\"BEGINDATE\", false);");
|
||||
ScriptExecuteController c = controller(root);
|
||||
|
||||
Map<String, Object> body = new LinkedHashMap<>();
|
||||
body.put("templateId", "0d9eac07");
|
||||
body.put("seq", 1);
|
||||
body.put("item", new LinkedHashMap<>());
|
||||
Map<String, Object> resp = c.executeTemplateRewritten(body);
|
||||
|
||||
assertEquals(Boolean.TRUE, resp.get("passed"));
|
||||
List<Map<String, Object>> cmds = (List<Map<String, Object>>) resp.get("commands");
|
||||
assertEquals(1, cmds.size());
|
||||
assertEquals("hidden", cmds.get(0).get("actionType"));
|
||||
assertEquals("BEGINDATE", cmds.get(0).get("componentId"));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
void executeTemplateRewritten_服务端语义动词从item求值并联动(@TempDir Path root) throws IOException {
|
||||
// 办公用品领用申请 seq=2:addSubTableButton + bindField(getFieldValue("STORE_ID"))
|
||||
writeScript(root, "021e3ba7", "2.FULL_AUTO.groovy",
|
||||
"// header\naddSubTableButton(\"GOODSDETAIL\", \"SELECT\", 0);\n"
|
||||
+ "bindField(\"OLD_STORE_ID\",getFieldValue(\"STORE_ID\"));");
|
||||
ScriptExecuteController c = controller(root);
|
||||
|
||||
Map<String, Object> item = new LinkedHashMap<>();
|
||||
item.put("STORE_ID", "S-99");
|
||||
Map<String, Object> body = new LinkedHashMap<>();
|
||||
body.put("templateId", "021e3ba7");
|
||||
body.put("seq", "2"); // 字符串 seq 也能解析
|
||||
body.put("item", item);
|
||||
Map<String, Object> resp = c.executeTemplateRewritten(body);
|
||||
|
||||
assertEquals(Boolean.TRUE, resp.get("passed"));
|
||||
List<Map<String, Object>> cmds = (List<Map<String, Object>>) resp.get("commands");
|
||||
assertEquals(2, cmds.size());
|
||||
assertEquals("addSubTableButton", cmds.get(0).get("actionType"));
|
||||
Map<String, Object> setValue = cmds.get(1);
|
||||
assertEquals("setValue", setValue.get("actionType"));
|
||||
assertEquals("OLD_STORE_ID", setValue.get("componentId"));
|
||||
Map<String, Object> args = (Map<String, Object>) setValue.get("args");
|
||||
assertEquals("S-99", args.get("value")); // getFieldValue 从 item 求值闭环
|
||||
}
|
||||
|
||||
@Test
|
||||
void executeTemplateRewritten_缺templateId或脚本未找到时passed为false(@TempDir Path root) {
|
||||
ScriptExecuteController c = controller(root);
|
||||
|
||||
Map<String, Object> noTid = new LinkedHashMap<>();
|
||||
Map<String, Object> r1 = c.executeTemplateRewritten(noTid);
|
||||
assertEquals(Boolean.FALSE, r1.get("passed"));
|
||||
assertNotNull(r1.get("error"));
|
||||
|
||||
Map<String, Object> missing = new LinkedHashMap<>();
|
||||
missing.put("templateId", "no-such");
|
||||
missing.put("seq", 1);
|
||||
Map<String, Object> r2 = c.executeTemplateRewritten(missing);
|
||||
assertEquals(Boolean.FALSE, r2.get("passed"));
|
||||
assertTrue(((String) r2.get("error")).contains("未找到"));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
void execute_直接执行脚本仍可用(@TempDir Path root) {
|
||||
ScriptExecuteController c = controller(root);
|
||||
Map<String, Object> body = new LinkedHashMap<>();
|
||||
body.put("script", "showToast(\"ok\");");
|
||||
body.put("item", new LinkedHashMap<>());
|
||||
Map<String, Object> resp = c.execute(body);
|
||||
assertEquals(Boolean.TRUE, resp.get("passed"));
|
||||
List<Map<String, Object>> cmds = (List<Map<String, Object>>) resp.get("commands");
|
||||
assertEquals("toast", cmds.get(0).get("actionType"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user