diff --git a/report-core/pom.xml b/report-core/pom.xml index 423b3b05..7118783c 100644 --- a/report-core/pom.xml +++ b/report-core/pom.xml @@ -151,6 +151,13 @@ poi-ooxml-schemas 4.1.2 + + + org.codehaus.groovy + groovy + 3.0.9 + + diff --git a/report-core/src/main/java/com/anjiplus/template/gaea/business/code/ResponseCode.java b/report-core/src/main/java/com/anjiplus/template/gaea/business/code/ResponseCode.java index 94494b03..f0fe7039 100644 --- a/report-core/src/main/java/com/anjiplus/template/gaea/business/code/ResponseCode.java +++ b/report-core/src/main/java/com/anjiplus/template/gaea/business/code/ResponseCode.java @@ -131,6 +131,7 @@ public interface ResponseCode { String SET_CODE_ISEXIST = "4008"; String SOURCE_CODE_ISEXIST = "4009"; String CLASS_NOT_FOUND = "4010"; + String EXECUTE_GROOVY_ERROR = "4011"; String REPORT_SHARE_LINK_INVALID = "report.share.link.invalid"; diff --git a/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/datasettransform/service/IGroovyHandler.java b/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/datasettransform/service/IGroovyHandler.java new file mode 100644 index 00000000..eb03cdaa --- /dev/null +++ b/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/datasettransform/service/IGroovyHandler.java @@ -0,0 +1,14 @@ +package com.anjiplus.template.gaea.business.modules.datasettransform.service; + +import com.alibaba.fastjson.JSONObject; + +import java.util.List; + +/** + * @author: Raod + * @since: 2022-02-23 + */ +public interface IGroovyHandler { + + List transform(List data); +} diff --git a/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/datasettransform/service/impl/GroovyTransformServiceImpl.java b/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/datasettransform/service/impl/GroovyTransformServiceImpl.java new file mode 100644 index 00000000..dc2de478 --- /dev/null +++ b/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/datasettransform/service/impl/GroovyTransformServiceImpl.java @@ -0,0 +1,65 @@ +package com.anjiplus.template.gaea.business.modules.datasettransform.service.impl; + +import com.alibaba.fastjson.JSONObject; +import com.anji.plus.gaea.exception.BusinessExceptionBuilder; +import com.anjiplus.template.gaea.business.code.ResponseCode; +import com.anjiplus.template.gaea.business.modules.datasettransform.controller.dto.DataSetTransformDto; +import com.anjiplus.template.gaea.business.modules.datasettransform.service.IGroovyHandler; +import com.anjiplus.template.gaea.business.modules.datasettransform.service.TransformStrategy; +import groovy.lang.GroovyClassLoader; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +import javax.script.Invocable; +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; +import java.util.List; + +/** + * Created by raodeming on 2021/3/23. + */ +@Component +@Slf4j +public class GroovyTransformServiceImpl implements TransformStrategy { + + private GroovyClassLoader groovyClassLoader = new GroovyClassLoader(); + + /** + * 数据清洗转换 类型 + * + * @return + */ + @Override + public String type() { + return "javaBean"; + } + + /*** + * 清洗转换算法接口 + * @param def + * @param data + * @return + */ + @Override + public List transform(DataSetTransformDto def, List data) { + String transformScript = def.getTransformScript(); + Class clazz = groovyClassLoader.parseClass(transformScript); + if (clazz != null) { + try { + Object instance = clazz.newInstance(); + if (instance!=null) { + if (instance instanceof IGroovyHandler) { + IGroovyHandler handler = (IGroovyHandler) instance; + return handler.transform(data); + } else { + System.err.println("转换失败!"); + } + } + } catch (Exception e) { + log.info("执行javaBean异常", e); + throw BusinessExceptionBuilder.build(ResponseCode.EXECUTE_GROOVY_ERROR, e.getMessage()); + } + } + return data; + } +} diff --git a/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/datasettransform/service/impl/JsTransformServiceImpl.java b/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/datasettransform/service/impl/JsTransformServiceImpl.java index 082a2796..a33a7dff 100644 --- a/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/datasettransform/service/impl/JsTransformServiceImpl.java +++ b/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/datasettransform/service/impl/JsTransformServiceImpl.java @@ -58,6 +58,7 @@ public class JsTransformServiceImpl implements TransformStrategy { } } catch (Exception ex) { + log.info("执行js异常", ex); throw BusinessExceptionBuilder.build(ResponseCode.EXECUTE_JS_ERROR, ex.getMessage()); } return null; diff --git a/report-core/src/main/resources/db/migration/V1.0.15__update_javaBean.sql b/report-core/src/main/resources/db/migration/V1.0.15__update_javaBean.sql new file mode 100644 index 00000000..9d1b9738 --- /dev/null +++ b/report-core/src/main/resources/db/migration/V1.0.15__update_javaBean.sql @@ -0,0 +1 @@ +UPDATE `aj_report`.`gaea_dict_item` SET `dict_code` = 'TRANSFORM_TYPE', `item_name` = 'java脚本', `item_value` = 'javaBean', `item_extend` = NULL, `enabled` = 1, `locale` = 'zh', `remark` = NULL, `sort` = 2, `create_by` = 'admin', `create_time` = '2021-03-23 10:54:08', `update_by` = 'admin', `update_time` = '2021-03-23 10:54:08', `version` = 1 WHERE `id` = 151; diff --git a/report-core/src/main/resources/i18n/messages_en_US.properties b/report-core/src/main/resources/i18n/messages_en_US.properties index 4219c464..43cf12ef 100644 --- a/report-core/src/main/resources/i18n/messages_en_US.properties +++ b/report-core/src/main/resources/i18n/messages_en_US.properties @@ -47,5 +47,6 @@ Component.load.check.error={0} Component not load 4008=The set code does not allow duplication 4009=The source code does not allow duplication 4010=Can't auto find match driver class +4011=execute javaBean error report.share.link.invalid=report share link invalid diff --git a/report-core/src/main/resources/i18n/messages_zh_CN.properties b/report-core/src/main/resources/i18n/messages_zh_CN.properties index c25afa66..a8e54261 100644 --- a/report-core/src/main/resources/i18n/messages_zh_CN.properties +++ b/report-core/src/main/resources/i18n/messages_zh_CN.properties @@ -48,6 +48,7 @@ Component.load.check.error={0}\u7EC4\u4EF6\u672A\u52A0\u8F7D 4008=\u6570\u636E\u96C6\u7F16\u7801\u4E0D\u5141\u8BB8\u91CD\u590D 4009=\u6570\u636E\u6E90\u7F16\u7801\u4E0D\u5141\u8BB8\u91CD\u590D 4010=\u9A71\u52A8\u5305\u4E0D\u5B58\u5728 +4011=\u6267\u884CjavaBean\u5931\u8D25 6001={0} 7001=\u89E3\u6790\u5931\u8D25 diff --git a/report-core/src/test/java/com/DemoGroovyHandler.java b/report-core/src/test/java/com/DemoGroovyHandler.java new file mode 100644 index 00000000..1ec6cb54 --- /dev/null +++ b/report-core/src/test/java/com/DemoGroovyHandler.java @@ -0,0 +1,18 @@ +package com; + +import com.alibaba.fastjson.JSONObject; +import com.anjiplus.template.gaea.business.modules.datasettransform.service.IGroovyHandler; + +import java.util.List; + +/** + * 建议在idea写好复制整个类到此处,位置report-core/src/test/java/com/DemoGroovyHandler.java + */ +public class DemoGroovyHandler implements IGroovyHandler { + + @Override + public List transform(List data) { + + return data; + } +} diff --git a/report-core/src/test/java/com/GroovyTest.java b/report-core/src/test/java/com/GroovyTest.java new file mode 100644 index 00000000..b695c361 --- /dev/null +++ b/report-core/src/test/java/com/GroovyTest.java @@ -0,0 +1,58 @@ +package com; + +import com.alibaba.fastjson.JSONObject; +import com.anjiplus.template.gaea.business.modules.datasettransform.service.IGroovyHandler; +import groovy.lang.GroovyClassLoader; + +import java.util.List; + +/** + * @author: Raod + * @since: 2022-02-23 + */ +public class GroovyTest { + + + public static void main(String[] args) throws InstantiationException, IllegalAccessException { + // codeSource来自DemoGroovyHandler + String codeSource = "package com;\n" + + "\n" + + "import com.alibaba.fastjson.JSONObject;\n" + + "import com.anjiplus.template.gaea.business.modules.datasettransform.service.IGroovyHandler;\n" + + "\n" + + "import java.util.ArrayList;\n" + + "import java.util.List;\n" + + "\n" + + "/**\n" + + " * @author: Raod\n" + + " * @since: 2022-02-23\n" + + " */\n" + + "public class DemoGroovyHandler implements IGroovyHandler {\n" + + "\n" + + " @Override\n" + + " public List transform(List data) {\n" + + " List result = new ArrayList<>();\n" + + " JSONObject jsonObject = new JSONObject();\n" + + " jsonObject.put(\"test\", \"demo\");\n" + + " result.add(jsonObject);\n" + + " return result;\n" + + " }\n" + + "}"; + GroovyClassLoader groovyClassLoader = new GroovyClassLoader(); + Class clazz = groovyClassLoader.parseClass(codeSource); + if (clazz != null) { + Object instance = clazz.newInstance(); + if (instance!=null) { + if (instance instanceof IGroovyHandler) { + IGroovyHandler handler = (IGroovyHandler) instance; + List transform = handler.transform(null); + System.out.println(JSONObject.toJSONString(transform)); + + } else { + System.err.println("转换失败!"); + } + } + } + } + +} diff --git a/report-ui/src/views/resultset/components/EditDataSet.vue b/report-ui/src/views/resultset/components/EditDataSet.vue index 966d17b2..562a78b9 100644 --- a/report-ui/src/views/resultset/components/EditDataSet.vue +++ b/report-ui/src/views/resultset/components/EditDataSet.vue @@ -229,7 +229,8 @@ -
+
transform(List data) { + + return data; + } +}`; + this.transformScript = item.transformScript + ? item.transformScript + : fnCont; } }, // js 脚本编辑确定