From 576d405f562ca50ad72a14cca9e05f8a0ad55536 Mon Sep 17 00:00:00 2001 From: gongbin <47426305@qq.com> Date: Mon, 1 Aug 2022 22:10:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20'src/main/java/com/fr/plug?= =?UTF-8?q?in/dynamic/dbconnect/conf/DynamicDbConnectConfig.java'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../conf/DynamicDbConnectConfig.java | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 src/main/java/com/fr/plugin/dynamic/dbconnect/conf/DynamicDbConnectConfig.java diff --git a/src/main/java/com/fr/plugin/dynamic/dbconnect/conf/DynamicDbConnectConfig.java b/src/main/java/com/fr/plugin/dynamic/dbconnect/conf/DynamicDbConnectConfig.java new file mode 100644 index 0000000..2d7935b --- /dev/null +++ b/src/main/java/com/fr/plugin/dynamic/dbconnect/conf/DynamicDbConnectConfig.java @@ -0,0 +1,84 @@ +package com.fr.plugin.dynamic.dbconnect.conf; + +import com.fr.config.*; +import com.fr.config.holder.Conf; +import com.fr.config.holder.factory.Holders; +import com.fr.log.FineLoggerFactory; +import com.fr.plugin.dynamic.dbconnect.bean.DynamicDbConnectBean; +import org.json.JSONArray; +import org.json.JSONObject; + +import java.util.ArrayList; + + +@Visualization(category = "动态数据连接配置") +public class DynamicDbConnectConfig extends DefaultConfiguration { + + private static volatile DynamicDbConnectConfig config = null; + + public static DynamicDbConnectConfig getInstance(){ + if(null == config){ + config = (DynamicDbConnectConfig) ConfigContext.getConfigInstance(DynamicDbConnectConfig.class); + } + return config; + } + + public String getNameSpace() { + return "DynamicDbConnectConfig"; + } + + @Identifier(value = "",name="配置规则",description = "配置规则格式为:[{\"reportName\": \"WorkBook2.cpt\",\"connectName\":\"mysql\",\"role\": \"zhangsan\"}] ",status = Status.SHOW) + private Conf connectConfig = Holders.simple(""); + + public String getConnectConfig(){ + return connectConfig.get(); + } + + public void setConnectConfig(String value){ + connectConfig.set(value); + FineLoggerFactory.getLogger().info("set connectConfig :"+value); + parseConfig(); + } + + public String oldConfig = ""; + public ArrayList configBeans; + /** + * 解析配置 + */ + public void parseConfig(){ + String config = this.getConnectConfig(); + FineLoggerFactory.getLogger().info("set parseConfig :"+config); + if(config != ""){ + try{ + JSONArray array = new JSONArray(config); + configBeans = new ArrayList<>(); + for(int i = 0 ; i < array.length();i++){ + JSONObject item = (JSONObject)array.get(i); + DynamicDbConnectBean bean = new DynamicDbConnectBean( + item.get("reportName").toString(), + item.get("role").toString(), + item.get("connectName").toString() + ); + configBeans.add(bean); + } + } + catch(Exception ex){ + FineLoggerFactory.getLogger().error("动态数据连接配置解析配置失败:"+ex.getMessage()); + } + } + } + + public DynamicDbConnectBean getDbConnectBean(String reportName,String role){ + if(configBeans != null && configBeans.size() > 0){ + for(DynamicDbConnectBean bean : configBeans){ + if(bean.getReportName().equals(reportName) && bean.getRole().equals(role)){ + return bean; + } + } + return null; + } + else{ + return null; + } + } +}