麻烦帮看看Java查询类:
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import com.epoint.smartbi.gx.util.OkHttpUtil;
- import okhttp3.OkHttpClient;
- import smartbi.framework.IModule;
- import smartbi.freequery.metadata.IJavaQueryData;
- import smartbi.freequery.metadata.JavaQueryConfig;
- import smartbi.freequery.metadata.JavaQueryOutputField;
- import smartbi.freequery.metadata.JavaQueryParameter;
- import smartbi.freequery.querydata.CellData;
- import smartbi.freequery.querydata.GridData;
- import smartbi.util.StringUtil;
- import smartbi.util.ValueType;
- import java.util.*;
- /**
- * smartbi JAVA查询扩展 自定义JSON请求
- */
- public class JsonQuery implements IJavaQueryData,IModule, IJavaQueryData.ISimpleData {
- private static JsonQuery instance;
- /**
- * 配置参数
- */
- private final Map<String, JavaQueryConfig> configs = new LinkedHashMap<>();
- /**
- * 结果集字段定义
- */
- private Map<String, JavaQueryOutputField> outputFields = new HashMap<>();
- /**
- * 请求参数字段定义
- */
- private Map<String, JavaQueryParameter> requestParams = new HashMap<>();
- /**
- * 数据总数
- */
- private Integer total;
- public static JsonQuery getInstance() {
- if (instance == null) {
- instance = new JsonQuery();
- }
- return instance;
- }
- public JsonQuery() {
- System.out.println("JsonQuery");
- }
- /**
- * 获取Java查询需要的配置信息
- */
- @Override
- public List<JavaQueryConfig> getConfigs() {
- System.out.println("getConfigs");
- if (configs.size() == 0) {
- setConfig("RequestUrl", "请求URL", "请求输入请求URL", "http://", true);
- setConfig("RequestMethod", "请求方法", "限定:POST或GET", "POST", true);
- setConfig("RequestParams", "请求参数", "请输入请求参数,压缩JSON格式,例子:{'name':'张三','age':'30','areacode':'450100'}", "{"name":"张三","age":"30","areacode":"450100"}", false);
- }
- return new ArrayList<>(configs.values());
- }
- /**
- * 添加一个配置项
- *
- * @param name 名称
- * @param alias 别名
- * @param desc 描述
- * @param defaultValue 默认值
- * @param isNotNull 是否允许为空
- */
- private void setConfig(String name, String alias, String desc, String defaultValue, boolean isNotNull) {
- JavaQueryConfig p = new JavaQueryConfig();
- p.setName(name);
- p.setAlias(alias);
- p.setDesc(desc);
- p.setValue(defaultValue);
- p.setNotNull(isNotNull);
- configs.put(name, p);
- }
- private void setParam(String name, String alias, String desc, String defaultValue, boolean isNull) {
- JavaQueryParameter p = new JavaQueryParameter();
- p.setId(name);
- p.setName(name);
- p.setAlias(alias);
- p.setDesc(desc);
- p.setValue(defaultValue);
- p.setDataType(ValueType.STRING);
- p.setNullable(isNull);
- requestParams.put(name, p);
- }
- private void addOutField(String name, String alias) {
- JavaQueryOutputField f = new JavaQueryOutputField();
- f.setId(name);
- f.setName(name);
- f.setAlias(alias);
- f.setDataType(ValueType.STRING);
- outputFields.put(name, f);
- }
- /**
- * 从保存的字符串中恢复配置信息
- *
- * @param configStr 配置字符串
- */
- @Override
- public void loadConfigs(String configStr) {
- System.out.println("loadConfigs");
- if (StringUtil.isNullOrEmpty(configStr)) {
- return;
- }
- JSONObject obj = JSONObject.parseObject(configStr);
- setConfig("RequestUrl", "RequestUrl", "", obj.getString("RequestUrl"), false);
- setConfig("RequestMethod", "RequestMethod", "", obj.getString("RequestMethod"), false);
- setConfig("RequestParams", "RequestParams", "", obj.getString("RequestParams"), false);
- }
- /**
- * 保存配置信息
- *
- * @return 返回配置字符串
- */
- @Override
- public String saveConfigs() {
- System.out.println("saveConfigs");
- JSONObject json = new JSONObject();
- if (configs.containsKey("RequestMethod")) {
- json.put("RequestMethod", configs.get("RequestMethod").getValue());
- }
- if (configs.containsKey("RequestUrl")) {
- json.put("RequestUrl", configs.get("RequestUrl").getValue());
- }
- if (configs.containsKey("RequestParams")) {
- json.put("RequestParams", configs.get("RequestParams").getValue());
- }
- return json.toJSONString();
- }
- /**
- * 设置配置信息
- *
- * @param key 名称
- * @param value 配置值
- */
- @Override
- public void setConfigValue(String key, String value) {
- System.out.println("setConfigValue");
- if (configs.containsKey(key)) {
- configs.get(key).setValue(value);
- }
- else {
- setConfig(key, key, "", value, false);
- }
- }
- /**
- * 设置配置信息
- */
- @Override
- public void setConfigValues(Map<String, String> configValues) {
- System.out.println("setConfigValues");
- for (Map.Entry<String, String> config : configValues.entrySet()) {
- configs.get(config.getKey()).setValue(config.getValue());
- }
- }
- /**
- * 根据配置信息初始化Java查询对象
- * 此方法初始化 字段
- */
- @Override
- public void init() {
- // 判断是否有初始化标识
- if (configs.containsKey("initData")) {
- // 查询数据、字段
- loadDatas(1, 10);
- }
- System.out.println("init");
- }
- /**
- * 关闭Java查询对象,关闭必要的资源
- */
- @Override
- public void close() {
- System.out.println("close");
- }
- /**
- * 返回参数对象
- */
- @Override
- public List<JavaQueryParameter> getParameters() {
- System.out.println("getParameters");
- if (requestParams.size() == 0) {
- if (configs.containsKey("RequestParams")) {
- JSONObject object = JSON.parseObject(configs.get("RequestParams").getValue());
- for (String key : object.keySet()) {
- setParam(key, key, "", "", false);
- }
- }
- }
- if (configs.containsKey("savedata")) {
- requestParams.clear();
- JSONObject jsonObject = JSON.parseObject(configs.get("RequestParams").getValue());
- for (String key : jsonObject.keySet()) {
- setParam(key, key, "", "", false);
- }
- }
- return new ArrayList<>(requestParams.values());
- }
- /**
- * 设置参数值
- */
- @Override
- public void setParameterValue(String id, String value, String displayValue) {
- System.out.println("setParameterValue");
- if (requestParams.containsKey(id)) {
- requestParams.get(id).setValue(value);
- }
- else {
- setParam(id, id, "", value, true);
- }
- }
- /**
- * 返回Java查询的输出字段
- */
- @Override
- public List<JavaQueryOutputField> getOutputFields() {
- System.out.println("getOutputFields");
- // 判断是否结果保存操作
- if (configs.containsKey("savedata")) {
- outputFields.clear();
- JSONArray jsonArray = JSON.parseArray(configs.get("jsonfields").getValue());
- // 转成outputFields
- if (!jsonArray.isEmpty()) {
- for (int i = 0; i < jsonArray.size(); i++) {
- addOutField(jsonArray.getJSONObject(i).getString("name"), jsonArray.getJSONObject(i).getString("name"));
- }
- }
- }
- return new ArrayList<>(outputFields.values());
- }
- /**
- * 测试连接并加载字段
- *
- * @return
- */
- public String testConnect(Map<String, String> config) {
- try {
- JSONObject result = new JSONObject();
- OkHttpClient client = new OkHttpClient();
- // 获取请求地址
- String url = config.get("RequestUrl");
- String params = config.get("RequestParams");
- String method = config.get("RequestMethod");
- JSONObject object = JSON.parseObject(params);
- object.put("pageFrom", "1");
- object.put("pageSize", "10");
- // 判断请求方法
- String res;
- if ("post".equalsIgnoreCase(method)) {
- res = OkHttpUtil.getInstance().postJson(url, object);
- }
- else if ("get".equalsIgnoreCase(method)) {
- res = OkHttpUtil.getInstance().getData(url, object);
- }
- else {
- throw new RuntimeException("请求方法未定义,限定 POST GET");
- }
- if (!StringUtil.isNullOrEmpty(res)) {
- return res;
- }
- }
- catch (Exception e) {
- throw new RuntimeException(e);
- }
- return "";
- }
- /**
- * 加载数据
- *
- * @param from
- * @param count
- * @return
- */
- public String loadDatas(int from, int count) {
- requestParams = new HashMap<>();
- outputFields = new HashMap<>();
- try {
- OkHttpClient client = new OkHttpClient();
- // 获取请求地址
- String url = configs.get("RequestUrl").getValue();
- String params = configs.get("RequestParams").getValue();
- String method = configs.get("RequestMethod").getValue();
- JSONObject object = new JSONObject();
- if (!StringUtil.isNullOrEmpty(params)) {
- object = JSON.parseObject(params);
- if (object != null) {
- for (String s : object.keySet()) {
- setParam(s, s, s, "", true);
- }
- }
- }
- object.put("pageFrom", from);
- object.put("pageSize", count);
- // 判断请求方法
- String res;
- if ("post".equalsIgnoreCase(method)) {
- res = OkHttpUtil.getInstance().postJson(url, object);
- }
- else if ("get".equalsIgnoreCase(method)) {
- res = OkHttpUtil.getInstance().getData(url, object);
- }
- else {
- throw new RuntimeException("请求方法未定义,限定 POST GET");
- }
- if (!StringUtil.isNullOrEmpty(res)) {
- // 解析结果,识别输出参数
- Object parse = JSON.parse(res);
- if (parse instanceof JSONObject) {
- JSONObject jsonObject = (JSONObject) parse;
- Integer total = jsonObject.getInteger("total");
- if (total != null) {
- this.total = total;
- }
- JSONArray list = jsonObject.getJSONArray("list");
- if (list != null && !list.isEmpty()) {
- Set<String> keys = new HashSet<>();
- // 循环每个数组,检查key
- for (int i = 0; i < list.size(); i++) {
- JSONObject row = list.getJSONObject(i);
- row.keySet().forEach(m -> keys.add(m));
- }
- if (keys.size() > 0) {
- for (String key : keys) {
- addOutField(key, key);
- }
- }
- }
- }
- else if (parse instanceof JSONArray) {
- JSONArray list = (JSONArray) parse;
- if (list != null && !list.isEmpty()) {
- Set<String> keys = new HashSet<>();
- // 循环每个数组,检查key
- for (int i = 0; i < list.size(); i++) {
- JSONObject row = list.getJSONObject(i);
- row.keySet().forEach(m -> keys.add(m));
- }
- if (keys.size() > 0) {
- for (String key : keys) {
- addOutField(key, key);
- }
- }
- }
- }
- return res;
- }
- }
- catch (Exception e) {
- throw new RuntimeException(e);
- }
- return "";
- }
- /**
- * 获取指定行的数据
- */
- @Override
- public GridData getGridData(int from, int count) {
- System.out.println("getGridData");
- try {
- GridData d = new GridData();
- List<List<CellData>> datas = new ArrayList<>();
- // 数据字段
- List<String> headers = new ArrayList<>();
- for (JavaQueryOutputField f : outputFields.values()) {
- headers.add(f.getName());
- }
- // 请求数据
- String responseData = loadDatas(from, count);
- // 数据结果
- if (!StringUtil.isNullOrEmpty(responseData)) {
- // 解析结果,识别输出参数
- Object parse = JSON.parse(responseData);
- if (parse instanceof JSONObject) {
- JSONObject jsonObject = (JSONObject) parse;
- Integer total = jsonObject.getInteger("total");
- if (total != null) {
- this.total = total;
- }
- JSONArray list = jsonObject.getJSONArray("list");
- if (list != null && !list.isEmpty()) {
- Set<String> keys = new HashSet<>();
- // 循环每个数组,检查key
- for (int i = 0; i < list.size(); i++) {
- LinkedHashMap<String, CellData> row = new LinkedHashMap<>();
- JSONObject object = list.getJSONObject(i);
- for (String header : headers) {
- CellData cell = new CellData();
- cell.setStringValue(object.getString(header));
- cell.setValue(object.getString(header));
- row.put(header, cell);
- }
- datas.add(new ArrayList<CellData>(row.values()) {
- });
- }
- }
- }
- else if (parse instanceof JSONArray) {
- JSONArray list = (JSONArray) parse;
- if (list != null && !list.isEmpty()) {
- this.total = list.size();
- Set<String> keys = new HashSet<>();
- // 循环每个数组,检查key
- for (int i = 0; i < list.size(); i++) {
- LinkedHashMap<String, CellData> row = new LinkedHashMap<>();
- JSONObject object = list.getJSONObject(i);
- for (String header : headers) {
- CellData cell = new CellData();
- cell.setStringValue(object.getString(header));
- cell.setValue(object.getString(header));
- row.put(header, cell);
- }
- datas.add(new ArrayList<CellData>(row.values()));
- }
- }
- }
- }
- d.setStringHeaders(headers);
- d.setData(datas);
- d.setTotalRowsCount(total);
- return d;
- }
- catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
- /**
- * 返回总行数,返回Integer.MAX_VALUE表示未知总行数
- */
- @Override
- public int getRowCount() {
- System.out.println("getRowCount");
- return total == null ? Integer.MAX_VALUE : total;
- }
- @Override
- public void activate() {
- System.out.println("activate");
- }
- }
复制代码 |