From 308d62ffc3e7fe6adfa967ba8f8627fab54b327b Mon Sep 17 00:00:00 2001 From: richie Date: Tue, 12 Nov 2019 14:32:30 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E7=9A=84=E5=87=A0=E4=B8=AA=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E5=A4=84=E7=90=86=E7=9A=84API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/fanruan/api/cal/ParameterKit.java | 61 +++++++++++++++++++ .../api/report/script/JavaScriptKit.java | 2 +- src/test/java/com/fanruan/api/Prepare.java | 4 ++ .../com/fanruan/api/cal/ParameterKitTest.java | 21 +++++++ .../api/report/script/JavaScriptKitTest.java | 22 +++++++ 5 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 src/test/java/com/fanruan/api/report/script/JavaScriptKitTest.java diff --git a/src/main/java/com/fanruan/api/cal/ParameterKit.java b/src/main/java/com/fanruan/api/cal/ParameterKit.java index 388726f..0cbb687 100644 --- a/src/main/java/com/fanruan/api/cal/ParameterKit.java +++ b/src/main/java/com/fanruan/api/cal/ParameterKit.java @@ -5,11 +5,19 @@ import com.fanruan.api.util.AssistKit; import com.fr.base.Parameter; import com.fr.base.ParameterHelper; import com.fr.base.ParameterMapNameSpace; +import com.fr.base.ParameterTypeHandler; +import com.fr.data.impl.TableColumn; +import com.fr.general.DateUtils; +import com.fr.json.JSON; +import com.fr.json.JSONArray; +import com.fr.json.JSONFactory; +import com.fr.json.JSONObject; import com.fr.stable.ArrayUtils; import com.fr.stable.ParameterProvider; import com.fr.stable.script.NameSpace; import org.jetbrains.annotations.NotNull; +import java.util.Date; import java.util.Map; /** @@ -135,4 +143,57 @@ public class ParameterKit { return parameters; } + + /** + * 从JSON中解析出来参数信息 + * + * @param jsonObject JSON文本 + * @return 参数信息 + */ + public static ParameterProvider json2Parameter(JSONObject jsonObject) { + Parameter parameter = new Parameter(); + parameter.setName(jsonObject.getString("name")); + String type = jsonObject.getString("type"); + if (jsonObject.has("value")) { + ParameterTypeHandler handler = ParameterTypeHandler.valueOf(type); + return handler.parseJson(jsonObject, parameter); + } + return parameter; + } + + /** + * 将多个参数转化为JSON数组 + * + * @param parameters 参数数组 + * @return JSON数组 + */ + public static JSONArray parameters2Json(ParameterProvider[] parameters) { + JSONArray jsonArray = JSONFactory.createJSON(JSON.ARRAY); + for (int i = 0; i < parameters.length; i++) { + JSONObject jsonObject = JSONFactory.createJSON(JSON.OBJECT); + ParameterProvider parameter = parameters[i]; + + jsonObject.put("name", parameter.getName()); + + if (parameter.getValue() instanceof Date) { + String dateString = DateUtils.DATEFORMAT2.format(parameter.getValue()); + jsonObject.put("value", dateString); + } else if (parameter.getValue() instanceof TableColumn) { + jsonObject.put("value", JSONFactory.createJSON(JSON.OBJECT, parameter.valueToString())); + } else { + jsonObject.put("value", parameter.getValue()); + } + + String type = parameter.getValue().getClass().getName(); + int firstChar = type.lastIndexOf('.') + 1; + if (firstChar > 0) { + type = type.substring(firstChar); + } + jsonObject.put("type", type); + + jsonArray.put(jsonObject); + } + + return jsonArray; + } } diff --git a/src/main/java/com/fanruan/api/report/script/JavaScriptKit.java b/src/main/java/com/fanruan/api/report/script/JavaScriptKit.java index 385f3d9..aafffd6 100644 --- a/src/main/java/com/fanruan/api/report/script/JavaScriptKit.java +++ b/src/main/java/com/fanruan/api/report/script/JavaScriptKit.java @@ -17,7 +17,7 @@ public class JavaScriptKit { * @param content 脚本内容 * @return JavaScript实例 */ - public JavaScript newSimpleJavaScript(String content) { + public static JavaScript newSimpleJavaScript(String content) { return new JavaScriptImpl(content); } } diff --git a/src/test/java/com/fanruan/api/Prepare.java b/src/test/java/com/fanruan/api/Prepare.java index d74dfbf..5f8c4b5 100644 --- a/src/test/java/com/fanruan/api/Prepare.java +++ b/src/test/java/com/fanruan/api/Prepare.java @@ -1,10 +1,13 @@ package com.fanruan.api; +import com.fr.base.Formula; import com.fr.config.dao.DaoContext; import com.fr.config.dao.impl.LocalClassHelperDao; import com.fr.config.dao.impl.LocalEntityDao; import com.fr.config.dao.impl.LocalXmlEntityDao; import com.fr.runtime.FineRuntime; +import com.fr.stable.FormulaProvider; +import com.fr.stable.bridge.StableFactory; import com.fr.store.StateHubManager; import com.fr.store.impl.MemoryLock; import com.fr.store.impl.MemoryStore; @@ -29,6 +32,7 @@ public class Prepare { Configurations.setHelper(new LocalConfigurationHelper()); StateHubManager.setStorage(new MemoryStore()); StateHubManager.setLock(new MemoryLock()); + StableFactory.registerXMLDescription(FormulaProvider.XML_TAG, new Formula()); } @After diff --git a/src/test/java/com/fanruan/api/cal/ParameterKitTest.java b/src/test/java/com/fanruan/api/cal/ParameterKitTest.java index 623650f..8498e15 100644 --- a/src/test/java/com/fanruan/api/cal/ParameterKitTest.java +++ b/src/test/java/com/fanruan/api/cal/ParameterKitTest.java @@ -2,6 +2,8 @@ package com.fanruan.api.cal; import com.fanruan.api.Prepare; import com.fr.base.Parameter; +import com.fr.json.JSON; +import com.fr.json.JSONFactory; import com.fr.script.Calculator; import com.fr.stable.ParameterProvider; import com.fr.stable.UtilEvalError; @@ -129,4 +131,23 @@ public class ParameterKitTest extends Prepare { } } } + + @Test + public void json2Parameter() { + String text = "{\"name\":\"p1\", \"type\":\"Formula\", \"value\":\"=1+1\"}"; + ParameterProvider p = ParameterKit.json2Parameter(JSONFactory.createJSON(JSON.OBJECT, text)); + Assert.assertEquals("p1", p.getName()); + } + + @Test + public void parameters2Json() { + ParameterProvider[] providers = new ParameterProvider[]{ + ParameterKit.newParameter("p1", 1), + ParameterKit.newParameter("p2", "abc"), + ParameterKit.newParameter("p3", "=sum(1, 2, 4)") + }; + Assert.assertEquals( + "[{\"name\":\"p1\",\"value\":1,\"type\":\"Integer\"},{\"name\":\"p2\",\"value\":\"abc\",\"type\":\"String\"},{\"name\":\"p3\",\"value\":\"=sum(1, 2, 4)\",\"type\":\"String\"}]", + ParameterKit.parameters2Json(providers).toString()); + } } \ No newline at end of file diff --git a/src/test/java/com/fanruan/api/report/script/JavaScriptKitTest.java b/src/test/java/com/fanruan/api/report/script/JavaScriptKitTest.java new file mode 100644 index 0000000..ead637f --- /dev/null +++ b/src/test/java/com/fanruan/api/report/script/JavaScriptKitTest.java @@ -0,0 +1,22 @@ +package com.fanruan.api.report.script; + +import com.fr.js.JavaScriptImpl; +import org.junit.Assert; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019/11/12 + */ +public class JavaScriptKitTest { + + @Test + public void newSimpleJavaScript() { + String text = "function add(a, b) {return a + b};"; + JavaScriptImpl script = new JavaScriptImpl(text); + Assert.assertEquals(script, JavaScriptKit.newSimpleJavaScript(text)); + } +} \ No newline at end of file