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; + } + } +}