|
@@ -2,20 +2,23 @@ package cn.com.taiji.cql.service.impl;
|
|
|
import cn.com.taiji.common.domain.PageResult;
|
|
|
import cn.com.taiji.common.domain.Result;
|
|
|
import cn.com.taiji.common.enums.CommonConstant;
|
|
|
+import cn.com.taiji.common.util.IdGenerator;
|
|
|
import cn.com.taiji.cql.service.IECqlService;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.geotools.api.data.DataStore;
|
|
|
-import org.geotools.api.data.DataStoreFinder;
|
|
|
-import org.geotools.api.data.Query;
|
|
|
-import org.geotools.api.data.SimpleFeatureSource;
|
|
|
+import org.geotools.api.data.*;
|
|
|
import org.geotools.api.feature.FeatureVisitor;
|
|
|
import org.geotools.api.feature.simple.SimpleFeature;
|
|
|
import org.geotools.api.feature.type.AttributeDescriptor;
|
|
|
import org.geotools.api.filter.Filter;
|
|
|
+import org.geotools.api.filter.identity.FeatureId;
|
|
|
import org.geotools.api.filter.sort.SortBy;
|
|
|
import org.geotools.api.filter.sort.SortOrder;
|
|
|
+import org.geotools.data.collection.ListFeatureCollection;
|
|
|
import org.geotools.data.simple.SimpleFeatureCollection;
|
|
|
import org.geotools.data.simple.SimpleFeatureIterator;
|
|
|
+import org.geotools.data.store.ContentFeatureSource;
|
|
|
+import org.geotools.feature.simple.SimpleFeatureBuilder;
|
|
|
import org.geotools.filter.FilterFactoryImpl;
|
|
|
import org.geotools.filter.SortByImpl;
|
|
|
import org.geotools.filter.text.ecql.ECQL;
|
|
@@ -231,6 +234,438 @@ public class ECqlServiceImpl implements IECqlService{
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * cql 通用查询接口
|
|
|
+ * @param dsParams
|
|
|
+ * @param paramMaps
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public PageResult cqlQuery(Map<String, Object> dsParams,Map<String,Object> paramMaps) {
|
|
|
+
|
|
|
+ List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
|
|
|
+
|
|
|
+ DataStore dataStore = null;
|
|
|
+
|
|
|
+ String host = dsParams.get(JDBCDataStoreFactory.HOST.key).toString();
|
|
|
+
|
|
|
+ String database = dsParams.get(JDBCDataStoreFactory.DATABASE.key).toString();
|
|
|
+
|
|
|
+ //查询记录总数
|
|
|
+ Long count =0l;
|
|
|
+
|
|
|
+ //当前页数
|
|
|
+ Long pageNumber= CommonConstant.PAGE_NUMBER;
|
|
|
+
|
|
|
+ //一页显示多少条记录
|
|
|
+ Long pageSize=CommonConstant.PAGE_SIZE;
|
|
|
+
|
|
|
+ //总页数
|
|
|
+ Long pageCount =0l;
|
|
|
+
|
|
|
+
|
|
|
+ try {
|
|
|
+
|
|
|
+ dataStore = DataStoreFinder.getDataStore(dsParams);
|
|
|
+
|
|
|
+ if (dataStore != null) {
|
|
|
+
|
|
|
+ log.info("系统连接到位于:" + host + "的空间数据库" + database + "成功!");
|
|
|
+
|
|
|
+ //根据表名获取source
|
|
|
+ SimpleFeatureSource fSource=dataStore.getFeatureSource(dsParams.get("tableName").toString());
|
|
|
+
|
|
|
+
|
|
|
+ FilterFactoryImpl filterFactory = new FilterFactoryImpl();
|
|
|
+
|
|
|
+ //组转cql查询条件
|
|
|
+ Query query = new Query();
|
|
|
+
|
|
|
+ if(paramMaps.containsKey("cql")) {
|
|
|
+
|
|
|
+ String cql = paramMaps.get("cql").toString();
|
|
|
+
|
|
|
+ if(cql !=null && !"".equals(cql)){
|
|
|
+
|
|
|
+ Filter filter = ECQL.toFilter(cql);
|
|
|
+
|
|
|
+ query.setFilter(filter);
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //组装排序参数
|
|
|
+ if(paramMaps.containsKey("sort")){
|
|
|
+
|
|
|
+ List<SortByImpl> sortByList = new ArrayList<SortByImpl>();
|
|
|
+
|
|
|
+ JSONObject sortJSONObject = JSONObject.parseObject(paramMaps.get("sort").toString());
|
|
|
+
|
|
|
+ for(String sortKey : sortJSONObject.keySet()) {
|
|
|
+
|
|
|
+ Object sortValue = sortJSONObject.get(sortKey);
|
|
|
+
|
|
|
+ if("asc".equals(sortValue.toString().toLowerCase())){
|
|
|
+
|
|
|
+ SortByImpl sb = new SortByImpl(filterFactory.property(sortKey),SortOrder.ASCENDING);
|
|
|
+
|
|
|
+ sortByList.add(sb);
|
|
|
+ }else{
|
|
|
+
|
|
|
+ SortByImpl sb = new SortByImpl(filterFactory.property(sortKey),SortOrder.DESCENDING);
|
|
|
+
|
|
|
+ sortByList.add(sb);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ query.setSortBy(sortByList.toArray(new SortBy[sortByList.size()]));
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //组装分页参数
|
|
|
+ if(paramMaps.containsKey("pageSize") && paramMaps.containsKey("pageNumber")){
|
|
|
+
|
|
|
+ //查询记录总数
|
|
|
+ count = Long.valueOf(fSource.getCount(query));
|
|
|
+
|
|
|
+ pageNumber = Long.valueOf(paramMaps.get("pageNumber").toString());
|
|
|
+
|
|
|
+ pageSize = Long.valueOf(paramMaps.get("pageSize").toString());
|
|
|
+
|
|
|
+ int startIndex = pageNumber.intValue()-1 <=0?0:pageNumber.intValue()-1;
|
|
|
+
|
|
|
+ //设置分页查询参数
|
|
|
+ query.setStartIndex(startIndex*pageSize.intValue());
|
|
|
+
|
|
|
+ query.setMaxFeatures(pageSize.intValue());
|
|
|
+
|
|
|
+ //计算总页数
|
|
|
+ if (count%pageSize ==0){
|
|
|
+
|
|
|
+ pageCount = count/pageSize;
|
|
|
+
|
|
|
+ }else{
|
|
|
+
|
|
|
+ pageCount = count/pageSize +1;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ SimpleFeatureCollection simpleFeatureCollection =fSource.getFeatures(query);
|
|
|
+
|
|
|
+ if(simpleFeatureCollection !=null){
|
|
|
+
|
|
|
+ // 获取当前矢量数据有哪些属性字段值
|
|
|
+ List<AttributeDescriptor> attributeList = simpleFeatureCollection.getSchema().getAttributeDescriptors();
|
|
|
+
|
|
|
+
|
|
|
+ SimpleFeatureIterator simpleFeatureIterator = simpleFeatureCollection.features();
|
|
|
+
|
|
|
+ while (simpleFeatureIterator.hasNext()){
|
|
|
+ //获取每一个要素
|
|
|
+ SimpleFeature simpleFeature = simpleFeatureIterator.next();
|
|
|
+
|
|
|
+ Map<String, Object> dataMap = new HashMap<String, Object>();
|
|
|
+
|
|
|
+ String id = simpleFeature.getID();
|
|
|
+ id = id.substring(id.lastIndexOf(".")+1);
|
|
|
+
|
|
|
+ dataMap.put("id",id);
|
|
|
+
|
|
|
+ for(AttributeDescriptor attribute:attributeList){
|
|
|
+
|
|
|
+// System.out.println(attribute.getLocalName() + ":" + simpleFeature.getAttribute(attribute.getLocalName()));
|
|
|
+
|
|
|
+ dataMap.put(attribute.getLocalName(),simpleFeature.getAttribute(attribute.getLocalName()));
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ dataList.add(dataMap);
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ simpleFeatureIterator.close();
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ if(!paramMaps.containsKey("pageSize") && !paramMaps.containsKey("pageNumber")){
|
|
|
+
|
|
|
+ count = Long.valueOf(dataList.size());
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ } else {
|
|
|
+
|
|
|
+ log.info("系统连接到位于:" + host + "的空间数据库" + database + "失败!请检查相关参数");
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }catch (Exception e) {
|
|
|
+
|
|
|
+ e.printStackTrace();
|
|
|
+ log.error("系统连接到位于:" + dsParams.get(JDBCDataStoreFactory.HOST.key) + "的空间数据库" + dsParams.get(JDBCDataStoreFactory.DATABASE.key) + "失败!请检查相关参数");
|
|
|
+
|
|
|
+ }finally {
|
|
|
+
|
|
|
+ if(dataStore !=null){
|
|
|
+
|
|
|
+ dataStore.dispose();
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ int respCode = 0;
|
|
|
+
|
|
|
+ String respMsg = "操作成功";
|
|
|
+
|
|
|
+ if(count == 0){
|
|
|
+
|
|
|
+ respCode = 1;
|
|
|
+ respMsg = "操作成功,但没有查找到记录!";
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return PageResult.<List<Map<String, Object>>>builder().datas(dataList).resp_code(respCode).resp_msg(respMsg).pageSize(pageSize).pageNumber(pageNumber).pageCount(pageCount).recordCount(count).build();
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * cql 通用增加接口
|
|
|
+ * @param mapParams
|
|
|
+ * @param attributeMap
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result cqlAdd(Map<String, Object> mapParams, Map<String, Object> attributeMap) {
|
|
|
+
|
|
|
+ int respCode = 0;
|
|
|
+
|
|
|
+ String respMsg = "操作成功";
|
|
|
+
|
|
|
+ if(attributeMap !=null && attributeMap.keySet().size() >0){
|
|
|
+
|
|
|
+ String host = mapParams.get(JDBCDataStoreFactory.HOST.key).toString();
|
|
|
+
|
|
|
+ String database = mapParams.get(JDBCDataStoreFactory.DATABASE.key).toString();
|
|
|
+
|
|
|
+ DataStore dataStore = null;
|
|
|
+
|
|
|
+ try{
|
|
|
+
|
|
|
+ dataStore = DataStoreFinder.getDataStore(mapParams);
|
|
|
+
|
|
|
+ if (dataStore != null) {
|
|
|
+
|
|
|
+ log.info("系统连接到位于:" + host + "的空间数据库" + database + "成功!");
|
|
|
+
|
|
|
+ ContentFeatureSource featureSource = (ContentFeatureSource) dataStore.getFeatureSource(mapParams.get("tableName").toString());
|
|
|
+ SimpleFeatureStore store = (SimpleFeatureStore)featureSource;
|
|
|
+ SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(store.getSchema());
|
|
|
+
|
|
|
+ for (Map.Entry<String, Object> attributeEntry : attributeMap.entrySet()) {
|
|
|
+
|
|
|
+ featureBuilder.set(attributeEntry.getKey(),attributeEntry.getValue());
|
|
|
+
|
|
|
+ }
|
|
|
+ SimpleFeature feature = featureBuilder.buildFeature(IdGenerator.getIdStr());
|
|
|
+ ListFeatureCollection featureCollection = new ListFeatureCollection(store.getSchema(), List.of(feature));
|
|
|
+ List<FeatureId> addFeatures = store.addFeatures(featureCollection);
|
|
|
+
|
|
|
+ return Result.of(addFeatures,respCode,respMsg);
|
|
|
+
|
|
|
+ }else{
|
|
|
+
|
|
|
+ respCode = -1;
|
|
|
+ respMsg = "系统连接到位于:" + host + "的空间数据库" + database + "失败!";
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }catch (Exception exception) {
|
|
|
+
|
|
|
+ exception.printStackTrace();
|
|
|
+
|
|
|
+ log.error("系统连接到位于:" + mapParams.get(JDBCDataStoreFactory.HOST.key) + "的空间数据库" + mapParams.get(JDBCDataStoreFactory.DATABASE.key) + "失败!请检查相关参数");
|
|
|
+
|
|
|
+ respCode = -1;
|
|
|
+ respMsg = "系统连接到位于:" + mapParams.get(JDBCDataStoreFactory.HOST.key) + "的空间数据库" + mapParams.get(JDBCDataStoreFactory.DATABASE.key) + "失败!请检查相关参数";
|
|
|
+
|
|
|
+ }finally {
|
|
|
+
|
|
|
+ if(dataStore !=null){
|
|
|
+
|
|
|
+ dataStore.dispose();
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }else{
|
|
|
+
|
|
|
+ respCode = 2;
|
|
|
+ respMsg = "删除数据的过滤条件cql为空或者为null";
|
|
|
+ }
|
|
|
+
|
|
|
+ return Result.of(null,respCode,respMsg);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * cql 通用修改接口
|
|
|
+ * @param mapParams
|
|
|
+ * @param attributeMap
|
|
|
+ * @param cql 过滤条件
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result cqlUpdate(Map<String, Object> mapParams, Map<String, Object> attributeMap, String cql) {
|
|
|
+
|
|
|
+ int respCode = 0;
|
|
|
+
|
|
|
+ String respMsg = "操作成功";
|
|
|
+
|
|
|
+ if(cql !=null && !"".equals(cql)){
|
|
|
+
|
|
|
+ String host = mapParams.get(JDBCDataStoreFactory.HOST.key).toString();
|
|
|
+
|
|
|
+ String database = mapParams.get(JDBCDataStoreFactory.DATABASE.key).toString();
|
|
|
+
|
|
|
+ DataStore dataStore = null;
|
|
|
+
|
|
|
+ try{
|
|
|
+
|
|
|
+ dataStore = DataStoreFinder.getDataStore(mapParams);
|
|
|
+
|
|
|
+ if (dataStore != null) {
|
|
|
+
|
|
|
+ log.info("系统连接到位于:" + host + "的空间数据库" + database + "成功!");
|
|
|
+
|
|
|
+ String[] names = attributeMap.keySet().toArray(new String[0]);
|
|
|
+
|
|
|
+ String[] attributeValues = attributeMap.values().toArray(new String[0]);
|
|
|
+
|
|
|
+ Filter filter = ECQL.toFilter(cql);
|
|
|
+
|
|
|
+ ContentFeatureSource featureSource = (ContentFeatureSource) dataStore.getFeatureSource(mapParams.get("tableName").toString());
|
|
|
+ SimpleFeatureStore store = (SimpleFeatureStore)featureSource;
|
|
|
+ store.modifyFeatures(names,attributeValues,filter);
|
|
|
+
|
|
|
+ }else{
|
|
|
+
|
|
|
+ respCode = -1;
|
|
|
+ respMsg = "系统连接到位于:" + host + "的空间数据库" + database + "失败!";
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }catch (Exception e) {
|
|
|
+
|
|
|
+ e.printStackTrace();
|
|
|
+ log.error("系统连接到位于:" + mapParams.get(JDBCDataStoreFactory.HOST.key) + "的空间数据库" + mapParams.get(JDBCDataStoreFactory.DATABASE.key) + "失败!请检查相关参数");
|
|
|
+
|
|
|
+ respCode = -1;
|
|
|
+ respMsg = "系统连接到位于:" + mapParams.get(JDBCDataStoreFactory.HOST.key) + "的空间数据库" + mapParams.get(JDBCDataStoreFactory.DATABASE.key) + "失败!请检查相关参数";
|
|
|
+
|
|
|
+ }finally {
|
|
|
+
|
|
|
+ if(dataStore !=null){
|
|
|
+
|
|
|
+ dataStore.dispose();
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }else{
|
|
|
+
|
|
|
+ respCode = 2;
|
|
|
+ respMsg = "修改数据的过滤条件cql为空或者为null";
|
|
|
+ }
|
|
|
+
|
|
|
+ return Result.of(null,respCode,respMsg);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * cql 通用删除接口
|
|
|
+ * @param mapParams
|
|
|
+ * @param cql 过滤条件
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public Result cqlRemove(Map<String, Object> mapParams, String cql) {
|
|
|
+
|
|
|
+ int respCode = 0;
|
|
|
+
|
|
|
+ String respMsg = "操作成功";
|
|
|
+
|
|
|
+ if(cql !=null && !"".equals(cql)){
|
|
|
+
|
|
|
+ String host = mapParams.get(JDBCDataStoreFactory.HOST.key).toString();
|
|
|
+
|
|
|
+ String database = mapParams.get(JDBCDataStoreFactory.DATABASE.key).toString();
|
|
|
+
|
|
|
+ DataStore dataStore = null;
|
|
|
+
|
|
|
+ try{
|
|
|
+
|
|
|
+ dataStore = DataStoreFinder.getDataStore(mapParams);
|
|
|
+
|
|
|
+ if (dataStore != null) {
|
|
|
+
|
|
|
+ log.info("系统连接到位于:" + host + "的空间数据库" + database + "成功!");
|
|
|
+
|
|
|
+ Filter filter = ECQL.toFilter(cql);
|
|
|
+
|
|
|
+ ContentFeatureSource featureSource = (ContentFeatureSource) dataStore.getFeatureSource(mapParams.get("tableName").toString());
|
|
|
+ SimpleFeatureStore store = (SimpleFeatureStore)featureSource;
|
|
|
+ store.removeFeatures(filter);
|
|
|
+
|
|
|
+ }else{
|
|
|
+
|
|
|
+ respCode = -1;
|
|
|
+ respMsg = "系统连接到位于:" + host + "的空间数据库" + database + "失败!";
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }catch (Exception e) {
|
|
|
+
|
|
|
+ e.printStackTrace();
|
|
|
+ log.error("系统连接到位于:" + mapParams.get(JDBCDataStoreFactory.HOST.key) + "的空间数据库" + mapParams.get(JDBCDataStoreFactory.DATABASE.key) + "失败!请检查相关参数");
|
|
|
+
|
|
|
+ respCode = -1;
|
|
|
+ respMsg = "系统连接到位于:" + mapParams.get(JDBCDataStoreFactory.HOST.key) + "的空间数据库" + mapParams.get(JDBCDataStoreFactory.DATABASE.key) + "失败!请检查相关参数";
|
|
|
+
|
|
|
+ }finally {
|
|
|
+
|
|
|
+ if(dataStore !=null){
|
|
|
+
|
|
|
+ dataStore.dispose();
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }else{
|
|
|
+
|
|
|
+ respCode = 2;
|
|
|
+ respMsg = "删除数据的过滤条件cql为空或者为null";
|
|
|
+ }
|
|
|
+
|
|
|
+ return Result.of(null,respCode,respMsg);
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public void cqlAggQuery4Mysql(Map<String, Object> mapParams, String cql, FeatureVisitor visitor) {
|
|
|
|