Ver código fonte

重构重复代码

liwh 1 ano atrás
pai
commit
1541f307ba

+ 80 - 116
cql-service/src/main/java/cn/com/taiji/cql/service/impl/ECqlServiceImpl.java

@@ -51,20 +51,19 @@ public class ECqlServiceImpl implements IECqlService{
     @Override
     public PageResult cqlQuery(Map<String, Object> dsParams,Map<String,Object> paramMaps) {
 
+
+        int respCode = 0;
+
+        String respMsg = "操作成功";
+
         List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
 
         DataStore dataStore = null;
 
-        JDBCDataStoreFactory jDBCDataStoreFactory = null;
-
         String host = dsParams.get(JDBCDataStoreFactory.HOST.key).toString();
 
-        String port = dsParams.get(JDBCDataStoreFactory.PORT.key).toString();
-
         String database = dsParams.get(JDBCDataStoreFactory.DATABASE.key).toString();
 
-        int hashKey = HashCodeUtil.hashCode(host+"-"+port+"-"+database);
-
         //查询记录总数
         Long count =0l;
 
@@ -80,44 +79,7 @@ public class ECqlServiceImpl implements IECqlService{
 
         try {
 
-            if(dataStoreMaps.containsKey(String.valueOf(hashKey))){
-
-                dataStore = dataStoreMaps.get(String.valueOf(hashKey));
-
-                log.info("系统从内存连接到位于:" + host + "的空间数据库" + database + "成功!");
-
-            }else{
-
-
-                DataSource dataSource = DataSourceFinder.getDataSource(dsParams);
-
-                if(dataSource !=null){
-
-                    if(dsParams.get(DBCPDataSourceFactory.DRIVERCLASS.key).equals("com.mysql.cj.jdbc.Driver")){
-
-                        jDBCDataStoreFactory = new MySQLDataStoreFactory();
-                    }
-
-                    Map<String, Object> dataStoreParams = new HashMap<String, Object>();
-
-                    dataStoreParams.put(JDBCDataStoreFactory.SCHEMA.key, database);
-                    dataStoreParams.put(JDBCDataStoreFactory.DATASOURCE.key,dataSource);
-                    dataStoreParams.put(JDBCDataStoreFactory.BATCH_INSERT_SIZE.key, 1000);
-
-                    dataStore = jDBCDataStoreFactory.createDataStore(dataStoreParams);
-
-                    dataStoreMaps.put(String.valueOf(hashKey),dataStore);
-
-                    log.info("系统新建连接到位于:" + host + "的空间数据库" + database + "成功!");
-
-                }else{
-
-                    log.info("获取"+host+"-"+port+"-"+database+"的数据源连接池失败!");
-                }
-
-
-
-            }
+            dataStore = createDataStore(dsParams);
 
             if (dataStore != null) {
 
@@ -252,9 +214,12 @@ public class ECqlServiceImpl implements IECqlService{
 
                 }
 
-            } else {
+            } else{
 
-                log.info("系统连接到位于:" + host + "的空间数据库" + database + "失败!请检查相关参数");
+                log.info("系统连接到位于:" + host + "的空间数据库" + database + "失败,获取DataStore的实例对象为null!");
+
+                respCode = -1;
+                respMsg = "系统连接到位于:" + host + "的空间数据库" + database + "失败,获取DataStore的实例对象为null!";
 
             }
 
@@ -263,11 +228,10 @@ public class ECqlServiceImpl implements IECqlService{
             e.printStackTrace();
             log.error("系统连接到位于:" + dsParams.get(JDBCDataStoreFactory.HOST.key) + "的空间数据库" + dsParams.get(JDBCDataStoreFactory.DATABASE.key) + "失败!请检查相关参数");
 
-        }
+            respCode = -1;
+            respMsg = "系统连接到位于:" + dsParams.get(JDBCDataStoreFactory.HOST.key) + "的空间数据库" + dsParams.get(JDBCDataStoreFactory.DATABASE.key) + "失败!请检查相关参数";
 
-        int respCode = 0;
-
-        String respMsg = "操作成功";
+        }
 
         if(count == 0){
 
@@ -306,7 +270,7 @@ public class ECqlServiceImpl implements IECqlService{
 
             try{
 
-                dataStore = DataStoreFinder.getDataStore(mapParams);
+                dataStore = createDataStore(mapParams);
 
                 if (dataStore != null) {
 
@@ -330,7 +294,7 @@ public class ECqlServiceImpl implements IECqlService{
                 }else{
 
                     respCode = -1;
-                    respMsg = "系统连接到位于:" + host + "的空间数据库" + database + "失败";
+                    respMsg = "系统连接到位于:" + host + "的空间数据库" + database + "失败,获取DataStore的实例对象为null!";
 
                 }
 
@@ -344,13 +308,6 @@ public class ECqlServiceImpl implements IECqlService{
                 respCode = -1;
                 respMsg = "系统连接到位于:" + mapParams.get(JDBCDataStoreFactory.HOST.key) + "的空间数据库" + mapParams.get(JDBCDataStoreFactory.DATABASE.key) + "失败!请检查相关参数";
 
-            }finally {
-
-                if(dataStore !=null){
-
-                    dataStore.dispose();
-
-                }
             }
 
 
@@ -389,7 +346,7 @@ public class ECqlServiceImpl implements IECqlService{
 
             try{
 
-                dataStore = DataStoreFinder.getDataStore(mapParams);
+                dataStore = createDataStore(mapParams);
 
                 if (dataStore != null) {
 
@@ -408,7 +365,7 @@ public class ECqlServiceImpl implements IECqlService{
                 }else{
 
                     respCode = -1;
-                    respMsg = "系统连接到位于:" + host + "的空间数据库" + database + "失败";
+                    respMsg = "系统连接到位于:" + host + "的空间数据库" + database + "失败,获取DataStore的实例对象为null!";
 
                 }
 
@@ -421,13 +378,6 @@ public class ECqlServiceImpl implements IECqlService{
                 respCode = -1;
                 respMsg = "系统连接到位于:" + mapParams.get(JDBCDataStoreFactory.HOST.key) + "的空间数据库" + mapParams.get(JDBCDataStoreFactory.DATABASE.key) + "失败!请检查相关参数";
 
-            }finally {
-
-                if(dataStore !=null){
-
-                    dataStore.dispose();
-
-                }
             }
 
 
@@ -463,7 +413,7 @@ public class ECqlServiceImpl implements IECqlService{
 
             try{
 
-                dataStore = DataStoreFinder.getDataStore(mapParams);
+                dataStore = createDataStore(mapParams);
 
                 if (dataStore != null) {
 
@@ -478,7 +428,9 @@ public class ECqlServiceImpl implements IECqlService{
                 }else{
 
                     respCode = -1;
-                    respMsg = "系统连接到位于:" + host + "的空间数据库" + database + "失败!";
+                    respMsg = "系统连接到位于:" + host + "的空间数据库" + database + "失败,获取DataStore的实例对象为null!";
+
+                    log.error("系统连接到位于:" + host + "的空间数据库" + database + "失败,获取DataStore的实例对象为null!");
 
                 }
 
@@ -491,13 +443,6 @@ public class ECqlServiceImpl implements IECqlService{
                 respCode = -1;
                 respMsg = "系统连接到位于:" + mapParams.get(JDBCDataStoreFactory.HOST.key) + "的空间数据库" + mapParams.get(JDBCDataStoreFactory.DATABASE.key) + "失败!请检查相关参数";
 
-            }finally {
-
-                if(dataStore !=null){
-
-                    dataStore.dispose();
-
-                }
             }
 
 
@@ -515,86 +460,105 @@ public class ECqlServiceImpl implements IECqlService{
 
         String host = mapParams.get(JDBCDataStoreFactory.HOST.key).toString();
 
-        String port = mapParams.get(JDBCDataStoreFactory.PORT.key).toString();
-
         String database = mapParams.get(JDBCDataStoreFactory.DATABASE.key).toString();
 
-        int hashKey = HashCodeUtil.hashCode(host+"-"+port+"-"+database);
-
         DataStore dataStore = null;
 
-        JDBCDataStoreFactory jDBCDataStoreFactory = null;
-
         try{
 
-            if(dataStoreMaps.containsKey(String.valueOf(hashKey))){
+            dataStore = createDataStore(mapParams);
+
+            if (dataStore != null) {
+
+                log.info("系统连接到位于:" + host + "的空间数据库" + database + "成功!");
 
-                dataStore = dataStoreMaps.get(String.valueOf(hashKey));
+                Query query = new Query();
 
-                log.info("系统从内存连接到位于:" + host + "的空间数据库" + database + "成功!");
+                if(cql !=null && !"".equals(cql)){
+
+                    Filter filter = ECQL.toFilter(cql);
+
+                    query.setFilter(filter);
+
+                }
+
+                //根据表名获取source
+                SimpleFeatureSource fSource = dataStore.getFeatureSource(mapParams.get("tableName").toString());
+
+                SimpleFeatureCollection simpleFeatureCollection =fSource.getFeatures(query);
+
+                simpleFeatureCollection.accepts(visitor,null);
 
             }else{
 
+                log.info("系统连接到位于:" + host + "的空间数据库" + database + "失败,获取DataStore的实例对象为null!");
+
+            }
 
-                DataSource dataSource = DataSourceFinder.getDataSource(mapParams);
 
-                if(dataSource !=null){
+        }catch (Exception e) {
 
-                    if(mapParams.get(DBCPDataSourceFactory.DRIVERCLASS.key).equals("com.mysql.cj.jdbc.Driver")){
+            e.printStackTrace();
+            log.error("系统连接到位于:" + mapParams.get(JDBCDataStoreFactory.HOST.key) + "的空间数据库" + mapParams.get(JDBCDataStoreFactory.DATABASE.key) + "失败!请检查相关参数");
 
-                        jDBCDataStoreFactory = new MySQLDataStoreFactory();
-                    }
+        }
+    }
 
-                    Map<String, Object> dataStoreParams = new HashMap<String, Object>();
+    private DataStore createDataStore(Map<String, Object> mapParams) throws Exception{
 
-                    dataStoreParams.put(JDBCDataStoreFactory.SCHEMA.key, database);
-                    dataStoreParams.put(JDBCDataStoreFactory.DATASOURCE.key,dataSource);
-                    dataStoreParams.put(JDBCDataStoreFactory.BATCH_INSERT_SIZE.key, 1000);
 
-                    dataStore = jDBCDataStoreFactory.createDataStore(dataStoreParams);
+        String host = mapParams.get(JDBCDataStoreFactory.HOST.key).toString();
 
-                    dataStoreMaps.put(String.valueOf(hashKey),dataStore);
+        String port = mapParams.get(JDBCDataStoreFactory.PORT.key).toString();
 
-                    log.info("系统新建连接到位于:" + host + "的空间数据库" + database + "成功!");
+        String database = mapParams.get(JDBCDataStoreFactory.DATABASE.key).toString();
 
-                }else{
+        int hashKey = HashCodeUtil.hashCode(host+"-"+port+"-"+database);
 
-                    log.info("获取"+host+"-"+port+"-"+database+"的数据源连接池失败!");
-                }
+        DataStore dataStore = null;
 
+        JDBCDataStoreFactory jDBCDataStoreFactory = null;
 
+        if(dataStoreMaps.containsKey(String.valueOf(hashKey))){
 
-            }
+            dataStore = dataStoreMaps.get(String.valueOf(hashKey));
 
-            if (dataStore != null) {
+            log.info("系统从内存连接到位于:" + host + "的空间数据库" + database + "成功!");
 
-                log.info("系统连接到位于:" + host + "的空间数据库" + database + "成功!");
+        }else{
 
-                Query query = new Query();
 
-                if(cql !=null && !"".equals(cql)){
+            DataSource dataSource = DataSourceFinder.getDataSource(mapParams);
 
-                    Filter filter = ECQL.toFilter(cql);
+            if(dataSource !=null){
 
-                    query.setFilter(filter);
+                if(mapParams.get(DBCPDataSourceFactory.DRIVERCLASS.key).equals("com.mysql.cj.jdbc.Driver")){
 
+                    jDBCDataStoreFactory = new MySQLDataStoreFactory();
                 }
 
-                //根据表名获取source
-                SimpleFeatureSource fSource = dataStore.getFeatureSource(mapParams.get("tableName").toString());
+                Map<String, Object> dataStoreParams = new HashMap<String, Object>();
 
-                SimpleFeatureCollection simpleFeatureCollection =fSource.getFeatures(query);
+                dataStoreParams.put(JDBCDataStoreFactory.SCHEMA.key, database);
+                dataStoreParams.put(JDBCDataStoreFactory.DATASOURCE.key,dataSource);
+                dataStoreParams.put(JDBCDataStoreFactory.BATCH_INSERT_SIZE.key, 1000);
 
-                simpleFeatureCollection.accepts(visitor,null);
+                dataStore = jDBCDataStoreFactory.createDataStore(dataStoreParams);
 
-            }
+                dataStoreMaps.put(String.valueOf(hashKey),dataStore);
 
+                log.info("系统新建连接到位于:" + host + "的空间数据库" + database + "成功!");
+
+            }else{
+
+                log.info("获取"+host+"-"+port+"-"+database+"的数据源连接池失败!");
+            }
 
-        }catch (Exception e) {
 
-            e.printStackTrace();
-            log.error("系统连接到位于:" + mapParams.get(JDBCDataStoreFactory.HOST.key) + "的空间数据库" + mapParams.get(JDBCDataStoreFactory.DATABASE.key) + "失败!请检查相关参数");
 
         }
+
+        return dataStore;
+
     }
 }