数据集:groovy执行java代码

Raod 3 years ago
parent 0b251147a7
commit f09c737461

@ -151,6 +151,13 @@
<artifactId>poi-ooxml-schemas</artifactId> <artifactId>poi-ooxml-schemas</artifactId>
<version>4.1.2</version> <version>4.1.2</version>
</dependency> </dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
<version>3.0.9</version>
</dependency>
</dependencies> </dependencies>
<developers> <developers>

@ -131,6 +131,7 @@ public interface ResponseCode {
String SET_CODE_ISEXIST = "4008"; String SET_CODE_ISEXIST = "4008";
String SOURCE_CODE_ISEXIST = "4009"; String SOURCE_CODE_ISEXIST = "4009";
String CLASS_NOT_FOUND = "4010"; String CLASS_NOT_FOUND = "4010";
String EXECUTE_GROOVY_ERROR = "4011";
String REPORT_SHARE_LINK_INVALID = "report.share.link.invalid"; String REPORT_SHARE_LINK_INVALID = "report.share.link.invalid";

@ -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<JSONObject> transform(List<JSONObject> data);
}

@ -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<JSONObject> transform(DataSetTransformDto def, List<JSONObject> 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;
}
}

@ -58,6 +58,7 @@ public class JsTransformServiceImpl implements TransformStrategy {
} }
} catch (Exception ex) { } catch (Exception ex) {
log.info("执行js异常", ex);
throw BusinessExceptionBuilder.build(ResponseCode.EXECUTE_JS_ERROR, ex.getMessage()); throw BusinessExceptionBuilder.build(ResponseCode.EXECUTE_JS_ERROR, ex.getMessage());
} }
return null; return null;

@ -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;

@ -47,5 +47,6 @@ Component.load.check.error={0} Component not load
4008=The set code does not allow duplication 4008=The set code does not allow duplication
4009=The source code does not allow duplication 4009=The source code does not allow duplication
4010=Can't auto find match driver class 4010=Can't auto find match driver class
4011=execute javaBean error
report.share.link.invalid=report share link invalid report.share.link.invalid=report share link invalid

@ -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 4008=\u6570\u636E\u96C6\u7F16\u7801\u4E0D\u5141\u8BB8\u91CD\u590D
4009=\u6570\u636E\u6E90\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 4010=\u9A71\u52A8\u5305\u4E0D\u5B58\u5728
4011=\u6267\u884CjavaBean\u5931\u8D25
6001={0} 6001={0}
7001=\u89E3\u6790\u5931\u8D25 7001=\u89E3\u6790\u5931\u8D25

@ -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;
/**
* ideareport-core/src/test/java/com/DemoGroovyHandler.java
*/
public class DemoGroovyHandler implements IGroovyHandler {
@Override
public List<JSONObject> transform(List<JSONObject> data) {
return data;
}
}

@ -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<JSONObject> transform(List<JSONObject> data) {\n" +
" List<JSONObject> 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<JSONObject> transform = handler.transform(null);
System.out.println(JSONObject.toJSONString(transform));
} else {
System.err.println("转换失败!");
}
}
}
}
}

@ -229,7 +229,8 @@
<el-button <el-button
v-if=" v-if="
item.transformType == 'js' || item.transformType == 'js' ||
item.transformType == 'dict' item.transformType == 'dict' ||
item.transformType == 'javaBean'
" "
type="text" type="text"
class="editor" class="editor"
@ -251,7 +252,7 @@
min-height="400px" min-height="400px"
append-to-body append-to-body
> >
<div v-if="isItemFilterType.transformType == 'js'"> <div v-if="isItemFilterType.transformType == 'js' || isItemFilterType.transformType == 'javaBean'">
<div class="codemirror"> <div class="codemirror">
<!-- // --> <!-- // -->
<monaco-editor <monaco-editor
@ -750,25 +751,30 @@ export default {
? item.transformScript ? item.transformScript
: fnCont; : fnCont;
} else if (item.transformType == "dict") { } else if (item.transformType == "dict") {
// this.dialogTitle = ''
// this.itemFilterScriptId = item.itemFilterSort }else if (item.transformType == "javaBean") {
// const { code, data } = await getDictList('TRANSFORM_TYPE') this.itemFilterScriptId = item.itemFilterSort;
// if (code != '200') return const fnCont = `package com;
// const extend = data.find(function (y) {
// if (y.id == item.transformType) { import com.alibaba.fastjson.JSONObject;
// return y import com.anjiplus.template.gaea.business.modules.datasettransform.service.IGroovyHandler;
// }
// }) import java.util.List;
// const extendArry = []
// const extendObj = JSON.parse(extend.extend) /**
// for (const i in extendObj) { * 建议在idea写好复制整个类到此处位置report-core/src/test/java/com/DemoGroovyHandler.java
// const children = [] */
// for (const y in extendObj[i]) { public class DemoGroovyHandler implements IGroovyHandler {
// children.push({ name: y, value: extendObj[i][y] })
// } @Override
// extendArry.push({ name: i, children: children }) public List<JSONObject> transform(List<JSONObject> data) {
// }
// this.tableData2 = extendArry return data;
}
}`;
this.transformScript = item.transformScript
? item.transformScript
: fnCont;
} }
}, },
// js // js

Loading…
Cancel
Save