Skip to content

Commit a4559ad

Browse files
committed
将nop-report-spl移动到nop-extensions模块中
1 parent 5eb156f commit a4559ad

File tree

154 files changed

+103917
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

154 files changed

+103917
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
集成润乾集算器
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>io.github.entropy-cloud.extensions</groupId>
8+
<artifactId>nop-report-ext</artifactId>
9+
<version>1.0.0-SNAPSHOT</version>
10+
</parent>
11+
12+
<artifactId>nop-report-spl</artifactId>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>io.github.entropy-cloud</groupId>
17+
<artifactId>nop-ooxml-xlsx</artifactId>
18+
</dependency>
19+
20+
<dependency>
21+
<groupId>io.github.entropy-cloud</groupId>
22+
<artifactId>nop-dao</artifactId>
23+
</dependency>
24+
25+
<dependency>
26+
<groupId>com.scudata.esproc</groupId>
27+
<artifactId>esproc</artifactId>
28+
<version>20230228</version>
29+
</dependency>
30+
31+
<dependency>
32+
<groupId>io.github.entropy-cloud</groupId>
33+
<artifactId>nop-autotest-junit</artifactId>
34+
<scope>test</scope>
35+
</dependency>
36+
37+
<dependency>
38+
<groupId>org.junit.jupiter</groupId>
39+
<artifactId>junit-jupiter</artifactId>
40+
<scope>test</scope>
41+
</dependency>
42+
43+
<dependency>
44+
<groupId>org.junit.jupiter</groupId>
45+
<artifactId>junit-jupiter-params</artifactId>
46+
<scope>test</scope>
47+
</dependency>
48+
49+
</dependencies>
50+
51+
52+
</project>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Copyright (c) 2017-2024 Nop Platform. All rights reserved.
3+
4+
* Blog: https://www.zhihu.com/people/canonical-entropy
5+
* Gitee: https://gitee.com/canonical-entropy/nop-entropy
6+
* Github: https://github.com/entropy-cloud/nop-entropy
7+
*/
8+
package io.nop.report.spl;
9+
10+
import io.nop.commons.util.CollectionHelper;
11+
12+
import java.util.List;
13+
14+
public interface SplConstants {
15+
String MODEL_TYPE_SPL = "spl";
16+
17+
String FILE_TYPE_SPL_XLSX = "spl.xlsx";
18+
19+
String FILE_TYPE_SPLX = "splx";
20+
21+
String FILE_TYPE_SPL = "spl";
22+
23+
List<String> FILE_TYPES_SPL_MODEL = CollectionHelper.buildImmutableList(FILE_TYPE_SPL, FILE_TYPE_SPLX);
24+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Copyright (c) 2017-2024 Nop Platform. All rights reserved.
3+
4+
* Blog: https://www.zhihu.com/people/canonical-entropy
5+
* Gitee: https://gitee.com/canonical-entropy/nop-entropy
6+
* Github: https://github.com/entropy-cloud/nop-entropy
7+
*/
8+
package io.nop.report.spl;
9+
10+
import io.nop.api.core.exceptions.ErrorCode;
11+
12+
import static io.nop.api.core.exceptions.ErrorCode.define;
13+
14+
public interface SplErrors {
15+
String ARG_RESULT_TYPE = "resultType";
16+
String ARG_PATH = "path";
17+
String ARG_PARAM_NAME = "paramName";
18+
19+
ErrorCode ERR_XPT_SPL_RESULT_NOT_CURSOR = define("nop.err.xpt.spl-result-not-cursor",
20+
"结果类型为[{resultType}],不是ICursor类型", ARG_RESULT_TYPE);
21+
22+
ErrorCode ERR_XPT_INVALID_SPL_MODEL_FILE_TYPE = define("nop.err.xpt.invalid-spl-model-file-type",
23+
"SPL模型文件名后缀必须是splx、spl等: {path}", ARG_PATH);
24+
25+
ErrorCode ERR_XPT_UNKNOWN_SPL_RESOURCE = define("nop.err.xpt.unknown-spl-resource",
26+
"SPL模型文件不存在:{path}", ARG_PATH);
27+
28+
ErrorCode ERR_XPT_UNKNOWN_SPL_PARAM =
29+
define("nop.err.xpt.unknown-spl-param",
30+
"未定义的SPL参数:{paramName}", ARG_PARAM_NAME);
31+
}
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
/**
2+
* Copyright (c) 2017-2024 Nop Platform. All rights reserved.
3+
4+
* Blog: https://www.zhihu.com/people/canonical-entropy
5+
* Gitee: https://gitee.com/canonical-entropy/nop-entropy
6+
* Github: https://github.com/entropy-cloud/nop-entropy
7+
*/
8+
package io.nop.report.spl.execute;
9+
10+
import com.esproc.jdbc.Server;
11+
import com.scudata.cellset.datamodel.PgmCellSet;
12+
import com.scudata.common.DBSession;
13+
import com.scudata.common.Logger;
14+
import com.scudata.common.UUID;
15+
import com.scudata.dm.Context;
16+
import com.scudata.dm.JobSpace;
17+
import com.scudata.dm.JobSpaceManager;
18+
import com.scudata.dm.Param;
19+
import com.scudata.dm.ParamList;
20+
import io.nop.api.core.annotations.lang.EvalMethod;
21+
import io.nop.api.core.exceptions.NopException;
22+
import io.nop.commons.util.StringHelper;
23+
import io.nop.core.lang.eval.IEvalScope;
24+
import io.nop.core.resource.component.ResourceComponentManager;
25+
import io.nop.report.spl.SplConstants;
26+
import io.nop.report.spl.model.SplModel;
27+
28+
import jakarta.annotation.PostConstruct;
29+
import java.util.Iterator;
30+
import java.util.Map;
31+
32+
import static io.nop.report.spl.SplErrors.ARG_PARAM_NAME;
33+
import static io.nop.report.spl.SplErrors.ARG_PATH;
34+
import static io.nop.report.spl.SplErrors.ERR_XPT_INVALID_SPL_MODEL_FILE_TYPE;
35+
import static io.nop.report.spl.SplErrors.ERR_XPT_UNKNOWN_SPL_PARAM;
36+
import static io.nop.report.spl.execute.SplHelper.spl2CellSet;
37+
38+
public class SplExecutor {
39+
40+
private String configPath = "config/raqsoftConfig.xml";
41+
42+
public void setConfigPath(String configPath) {
43+
this.configPath = configPath;
44+
}
45+
46+
@PostConstruct
47+
public void init() {
48+
try {
49+
Server.getInstance().initConfig(null, configPath);
50+
} catch (Exception e) {
51+
throw NopException.adapt(e);
52+
}
53+
}
54+
55+
@EvalMethod
56+
public Object executeForPath(IEvalScope scope, String path, Map<String, Object> params) {
57+
String fileType = StringHelper.fileType(path);
58+
if (!SplConstants.FILE_TYPES_SPL_MODEL.contains(fileType))
59+
throw new NopException(ERR_XPT_INVALID_SPL_MODEL_FILE_TYPE)
60+
.param(ARG_PATH, path);
61+
62+
SplModel model = (SplModel) ResourceComponentManager.instance().loadComponentModel(path);
63+
return executeForModel(scope, model, params);
64+
}
65+
66+
@EvalMethod
67+
public Object executeForModel(IEvalScope scope, SplModel model, Map<String, Object> params) {
68+
return executeSPL(scope, model.getSource(), params);
69+
}
70+
71+
@EvalMethod
72+
public Object executeSPL(IEvalScope scope, String source, Map<String, Object> params) {
73+
PgmCellSet pgmCellSet = spl2CellSet(source); // dfx, sqlx 二进制文件
74+
Context context = new Context(); //上下文,参数..设置
75+
76+
try {
77+
setParams(pgmCellSet, params);
78+
prepare(context);
79+
pgmCellSet.setContext(context);
80+
Object result = pgmCellSet.execute();
81+
return SplHelper.normalizeResult(result);
82+
} finally {
83+
close(context);
84+
}
85+
}
86+
87+
private void setParams(PgmCellSet pgmCellSet, Map<String, Object> params) {
88+
if (params != null && !params.isEmpty()) {
89+
ParamList list = pgmCellSet.getParamList();
90+
params.forEach((name, value) -> {
91+
Param param = list.get(name);
92+
if (param == null)
93+
throw new NopException(ERR_XPT_UNKNOWN_SPL_PARAM).param(ARG_PARAM_NAME, name);
94+
95+
param.setValue(value);
96+
});
97+
}
98+
}
99+
100+
private void prepare(Context context) {
101+
String uuid = UUID.randomUUID().toString();
102+
JobSpace jobSpace = JobSpaceManager.getSpace(uuid);
103+
context.setJobSpace(jobSpace);
104+
}
105+
106+
/**
107+
* 这里的资源清理代码拷贝自 InternalConnection的close方法
108+
*/
109+
private void close(Context ctx) {
110+
/* Close automatically opened connections */
111+
Map<String, DBSession> map = ctx.getDBSessionMap();
112+
if (map != null) {
113+
Iterator<String> iter = map.keySet().iterator();
114+
while (iter.hasNext()) {
115+
String name = iter.next().toString();
116+
DBSession sess = ctx.getDBSession(name);
117+
if (sess == null || sess.isClosed())
118+
continue;
119+
Object o = ctx.getDBSession(name).getSession();
120+
if (o != null && o instanceof java.sql.Connection) {
121+
try {
122+
((java.sql.Connection) o).close();
123+
} catch (Exception e) {
124+
Logger.warn(e.getMessage(), e);
125+
}
126+
}
127+
}
128+
}
129+
/* Close the connection opened by the user through an expression */
130+
ParamList pl = ctx.getParamList();
131+
for (int i = 0; i < pl.count(); i++) {
132+
Object o = pl.get(i).getValue();
133+
if (o != null && o instanceof java.sql.Connection) {
134+
try {
135+
((java.sql.Connection) o).close();
136+
} catch (Exception e) {
137+
Logger.warn(e.getMessage(), e);
138+
}
139+
}
140+
}
141+
JobSpace jobSpace = ctx.getJobSpace();
142+
/* Close the JobSpace */
143+
if (jobSpace != null) {
144+
jobSpace.closeResource();
145+
JobSpaceManager.closeSpace(jobSpace.getID());
146+
}
147+
}
148+
}

0 commit comments

Comments
 (0)