Browse Source

新的几个参数处理的API

pull/1/head
richie 5 years ago
parent
commit
308d62ffc3
  1. 61
      src/main/java/com/fanruan/api/cal/ParameterKit.java
  2. 2
      src/main/java/com/fanruan/api/report/script/JavaScriptKit.java
  3. 4
      src/test/java/com/fanruan/api/Prepare.java
  4. 21
      src/test/java/com/fanruan/api/cal/ParameterKitTest.java
  5. 22
      src/test/java/com/fanruan/api/report/script/JavaScriptKitTest.java

61
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.Parameter;
import com.fr.base.ParameterHelper; import com.fr.base.ParameterHelper;
import com.fr.base.ParameterMapNameSpace; 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.ArrayUtils;
import com.fr.stable.ParameterProvider; import com.fr.stable.ParameterProvider;
import com.fr.stable.script.NameSpace; import com.fr.stable.script.NameSpace;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.Date;
import java.util.Map; import java.util.Map;
/** /**
@ -135,4 +143,57 @@ public class ParameterKit {
return parameters; 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;
}
} }

2
src/main/java/com/fanruan/api/report/script/JavaScriptKit.java

@ -17,7 +17,7 @@ public class JavaScriptKit {
* @param content 脚本内容 * @param content 脚本内容
* @return JavaScript实例 * @return JavaScript实例
*/ */
public JavaScript newSimpleJavaScript(String content) { public static JavaScript newSimpleJavaScript(String content) {
return new JavaScriptImpl(content); return new JavaScriptImpl(content);
} }
} }

4
src/test/java/com/fanruan/api/Prepare.java

@ -1,10 +1,13 @@
package com.fanruan.api; package com.fanruan.api;
import com.fr.base.Formula;
import com.fr.config.dao.DaoContext; import com.fr.config.dao.DaoContext;
import com.fr.config.dao.impl.LocalClassHelperDao; import com.fr.config.dao.impl.LocalClassHelperDao;
import com.fr.config.dao.impl.LocalEntityDao; import com.fr.config.dao.impl.LocalEntityDao;
import com.fr.config.dao.impl.LocalXmlEntityDao; import com.fr.config.dao.impl.LocalXmlEntityDao;
import com.fr.runtime.FineRuntime; 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.StateHubManager;
import com.fr.store.impl.MemoryLock; import com.fr.store.impl.MemoryLock;
import com.fr.store.impl.MemoryStore; import com.fr.store.impl.MemoryStore;
@ -29,6 +32,7 @@ public class Prepare {
Configurations.setHelper(new LocalConfigurationHelper()); Configurations.setHelper(new LocalConfigurationHelper());
StateHubManager.setStorage(new MemoryStore()); StateHubManager.setStorage(new MemoryStore());
StateHubManager.setLock(new MemoryLock()); StateHubManager.setLock(new MemoryLock());
StableFactory.registerXMLDescription(FormulaProvider.XML_TAG, new Formula());
} }
@After @After

21
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.fanruan.api.Prepare;
import com.fr.base.Parameter; import com.fr.base.Parameter;
import com.fr.json.JSON;
import com.fr.json.JSONFactory;
import com.fr.script.Calculator; import com.fr.script.Calculator;
import com.fr.stable.ParameterProvider; import com.fr.stable.ParameterProvider;
import com.fr.stable.UtilEvalError; 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());
}
} }

22
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));
}
}
Loading…
Cancel
Save