Skip to content
Closed
7 changes: 7 additions & 0 deletions springboot-starter-script/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@
<artifactId>groovy-xml</artifactId>
</dependency>

<!-- 仅提供 AttributeConverter 契约,JPA 实现由使用方引入 -->
<dependency>
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
<optional>true</optional>
</dependency>


<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.codingapi.springboot.script.persistence;

import lombok.Getter;

/**
* 脚本持久化对象:实体字段上是脚本对象,数据库列上是 key
*
* <p>实体字段声明为 {@code GroovyScriptField} 即可被 {@link GroovyScriptFieldConverter} 自动转换,
* 业务侧不需要在转换器里处理 key,也不会因为字段是普通 String 而漏掉处理。</p>
*
* <p>key 生命周期:</p>
* <ul>
* <li>业务已指定 key:{@link #of(String, String)},落库直接使用业务 key;</li>
* <li>业务未指定 key:{@link #of(String)},落库时由策略生成;</li>
* <li>更新:{@link #carry(GroovyScriptField, String)} 沿用上一版本的 key,只替换正文。</li>
* </ul>
*/
@Getter
public class GroovyScriptField {

/**
* 脚本唯一编码,null 表示尚未落库
*/
private String key;

/**
* 脚本正文
*/
private String content;

private GroovyScriptField(String key, String content) {
this.key = key;
this.content = content;
}

/**
* 新建脚本(key 由框架按策略生成)
*
* @param content 脚本正文
* @return 脚本持久化对象
*/
public static GroovyScriptField of(String content) {
return new GroovyScriptField(null, content);
}

/**
* 业务指定 key 的脚本(或从库中还原)
*
* @param key 脚本唯一编码
* @param content 脚本正文
* @return 脚本持久化对象
*/
public static GroovyScriptField of(String key, String content) {
return new GroovyScriptField(key, content);
}

/**
* 沿用上一版本已持久化的 key,替换脚本正文
*
* @param previous 上一版本,null 表示新建
* @param content 新的脚本正文
* @return 脚本持久化对象
*/
public static GroovyScriptField carry(GroovyScriptField previous, String content) {
if (previous == null) {
return of(content);
}
return new GroovyScriptField(previous.key, content);
}

/**
* 是否尚未落库
*/
public boolean isNew() {
return this.key == null;
}

/**
* 落库:key 为空时由 {@link GroovyScriptKeyGeneratorContext} 生成,非空时复用同一 key 覆盖内容
*
* @return 脚本 key
*/
public String persist() {
this.key = GroovyScriptPersistenceContext.getInstance().save(this.key, this.content);
return this.key;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.codingapi.springboot.script.persistence;

import jakarta.persistence.AttributeConverter;
import jakarta.persistence.Converter;

/**
* 脚本字段转换器:数据库列存 key,实体属性是脚本持久化对象
*
* <p>标注 {@code autoApply = true},实体字段只要是 {@link GroovyScriptField} 类型即自动生效,
* 业务侧无需为每个字段写转换代码。</p>
*
* <p>key 的生成、复用与内容存取都委托给 {@link GroovyScriptPersistenceContext},
* 因此非 JPA 的持久化实现可以复用同一套抽象,而不是绑死在 JPA 上。</p>
*/
@Converter(autoApply = true)
public class GroovyScriptFieldConverter implements AttributeConverter<GroovyScriptField, String> {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GroovyScriptFieldConverter 再jpa下是可以使用的,但是考虑到业务模块的持久化未必是通过jpa实现的。那就可能需要提供抽象的GroovyScriptRepository来实现了

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已解耦:Converter 只做 JPA 适配,转换与 key 生命周期委托 GroovyScriptRepository 抽象(GroovyScriptPersistenceContext 持有,可替换)。新增用例演示非 JPA 场景(模拟文档库存 key)直接复用同一抽象完成内容存取与 key 复用。


/**
* 落库:脚本持久化对象转换为 key
*
* @param attribute 脚本持久化对象
* @return 脚本 key
*/
@Override
public String convertToDatabaseColumn(GroovyScriptField attribute) {
if (attribute == null) {
return null;
}
return attribute.persist();
}

/**
* 读取:key 还原为脚本持久化对象
*
* @param dbData 脚本 key
* @return 脚本持久化对象
*/
@Override
public GroovyScriptField convertToEntityAttribute(String dbData) {
if (dbData == null) {
return null;
}
String content = GroovyScriptPersistenceContext.getInstance().getScript(dbData);
return GroovyScriptField.of(dbData, content);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.codingapi.springboot.script.persistence;

import java.util.UUID;

/**
* 脚本 key 生成策略
*
* <p>业务可以先自行决定 key({@link GroovyScriptField#of(String, String)}),
* 只有业务未指定 key 时才由该策略兜底。默认 UUID。</p>
*/
@FunctionalInterface
public interface GroovyScriptKeyGenerator {

/**
* 生成脚本 key
*
* @return 脚本唯一编码
*/
String generate();

/**
* 默认策略:UUID(去掉分隔符)
*/
static GroovyScriptKeyGenerator uuid() {
return () -> UUID.randomUUID().toString().replace("-", "");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.codingapi.springboot.script.persistence;

import lombok.Getter;

/**
* 脚本 key 生成策略上下文
*
* <p>业务在启动时通过 {@link #setGenerator(GroovyScriptKeyGenerator)} 注入自己的策略,
* 未注入时使用默认 UUID 策略。</p>
*/
public class GroovyScriptKeyGeneratorContext {

@Getter
private final static GroovyScriptKeyGeneratorContext instance = new GroovyScriptKeyGeneratorContext();

private GroovyScriptKeyGenerator generator = GroovyScriptKeyGenerator.uuid();

private GroovyScriptKeyGeneratorContext() {
}

/**
* 替换 key 生成策略,入参为 null 时回退为默认 UUID 策略
*
* @param generator key 生成策略
*/
public void setGenerator(GroovyScriptKeyGenerator generator) {
this.generator = (generator == null) ? GroovyScriptKeyGenerator.uuid() : generator;
}

/**
* 生成脚本 key
*
* @return 脚本唯一编码
*/
public String generate() {
return this.generator.generate();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.codingapi.springboot.script.persistence;

import lombok.Getter;

/**
* 脚本持久化仓库上下文
*
* <p>默认使用 {@link GroovyScriptRepositoryImpl};非 JPA 的持久化实现(文档库、缓存库等)
* 可以通过 {@link #setRepository(GroovyScriptRepository)} 替换为自己的实现。</p>
*/
public class GroovyScriptPersistenceContext {

@Getter
private final static GroovyScriptPersistenceContext instance = new GroovyScriptPersistenceContext();

private GroovyScriptRepository repository = new GroovyScriptRepositoryImpl();

private GroovyScriptPersistenceContext() {
}

/**
* 替换脚本持久化仓库实现,入参为 null 时回退为默认实现
*
* @param repository 脚本持久化仓库
*/
public void setRepository(GroovyScriptRepository repository) {
this.repository = (repository == null) ? new GroovyScriptRepositoryImpl() : repository;
}

/**
* 保存脚本正文
*
* @param key 已有 key,null 表示新建
* @param content 脚本正文
* @return 最终使用的 key
*/
public String save(String key, String content) {
return this.repository.save(key, content);
}

/**
* 按 key 读取脚本正文
*
* @param key 脚本 key
* @return 脚本正文,不存在时返回空串
*/
public String getScript(String key) {
return this.repository.getScript(key);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.codingapi.springboot.script.persistence;

/**
* 脚本持久化仓库抽象:对外只暴露「脚本正文」,内部负责 key 的生成、复用与存储
*
* <p>JPA 场景由 {@link GroovyScriptFieldConverter} 委托本仓库实现;
* 非 JPA 场景(文档库、缓存库、自定义表结构等)可直接通过
* {@link GroovyScriptPersistenceContext} 使用同一套能力,无需自己处理 key。</p>
*/
public interface GroovyScriptRepository {

/**
* 保存脚本正文
*
* @param key 已有 key,null 表示新建
* @param content 脚本正文
* @return 最终使用的 key
*/
String save(String key, String content);

/**
* 按 key 读取脚本正文
*
* @param key 脚本 key
* @return 脚本正文,不存在时返回空串
*/
String getScript(String key);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.codingapi.springboot.script.persistence;

import com.codingapi.springboot.script.GroovyScript;
import com.codingapi.springboot.script.repository.GroovyScriptRepositoryContext;

/**
* 默认脚本持久化仓库实现:基于脚本仓储完成正文与 key 的映射
*
* <p>key 生命周期:key 为空时由 {@link GroovyScriptKeyGeneratorContext} 生成,非空时复用并覆盖内容。</p>
*/
public class GroovyScriptRepositoryImpl implements GroovyScriptRepository {

/**
* 保存脚本正文
*
* @param key 已有 key,null 表示新建
* @param content 脚本正文
* @return 最终使用的 key
*/
@Override
public String save(String key, String content) {
String targetKey = (key == null || key.isBlank())
? GroovyScriptKeyGeneratorContext.getInstance().generate()
: key;
GroovyScript.builder(targetKey).script(content).build().save();
return targetKey;
}

/**
* 按 key 读取脚本正文
*
* @param key 脚本 key
* @return 脚本正文,不存在时返回空串
*/
@Override
public String getScript(String key) {
if (key == null || key.isBlank()) {
return "";
}
GroovyScript groovyScript = GroovyScriptRepositoryContext.getInstance().get(key);
return groovyScript == null ? "" : groovyScript.getScript();
}
}
Loading
Loading