提交 277ba0e5 编写于 作者: sq3536's avatar sq3536

支持多级地址栏参数,mongodb实现

上级 294a10fa
......@@ -175,18 +175,18 @@ public class DOModel implements Serializable {
Map<String,PojoSchema> keyMap= this.getKeyMap();
if(keyMap!=null)
{
if (keyMap.size()==1)
PojoSchema keyProperty=this.getKeyProperty();
String keyPropertyName=keyProperty.getName().toLowerCase();
Object key=data.get(keyPropertyName);
if(ObjectUtils.isEmpty(key))
{
PojoSchema keyProperty=this.getKeyProperty();
String keyPropertyName=keyProperty.getName().toLowerCase();
Object key=data.get(keyPropertyName);
if(ObjectUtils.isEmpty(key))
{
key=data.getKey();
if(!ObjectUtils.isEmpty(key))
data.set(keyPropertyName, key);
}
if(ObjectUtils.isEmpty(key))
key=data.getKey();
if(!ObjectUtils.isEmpty(key))
data.set(keyPropertyName, key);
}
if(ObjectUtils.isEmpty(key))
{
if (keyMap.size()==1)
{
if(genKeyWhenNotExists)
{
......@@ -213,28 +213,28 @@ public class DOModel implements Serializable {
}
data.set(keyPropertyName,key);
data.setKey(key);
}
else
return null;
}
return (Serializable)key;
}
else if(keyMap.size()>1)
{
String key="";
for(PojoSchema keySchema:keyMap.values())
else if(keyMap.size()>1)
{
if(ObjectUtils.isEmpty(data.get(keySchema.getName().toLowerCase())))
return null;
if(!StringUtils.isEmpty(key))
key+="||";
else
key+=data.getStringValue(keySchema.getName().toLowerCase());
for(PojoSchema keySchema:keyMap.values())
{
if(ObjectUtils.isEmpty(data.get(keySchema.getName().toLowerCase())))
return null;
if(!StringUtils.isEmpty(key))
key+="||";
else
key+=data.getStringValue(keySchema.getName().toLowerCase());
}
data.setKey(key);
}
return key;
}
return (Serializable)key;
}
return null;
}
......@@ -246,6 +246,7 @@ public class DOModel implements Serializable {
public BaseData setKeyValue(BaseData data,Object keyValue)
{
Map<String,PojoSchema> keyMap= this.getKeyMap();
data.setKey(keyValue);
if(keyMap!=null)
{
if (keyMap.size()==1)
......
......@@ -4,13 +4,16 @@ import cn.ibizlab.core.data.domain.DOModel;
import cn.ibizlab.core.data.domain.DTOModel;
import cn.ibizlab.core.data.model.POSchema;
import cn.ibizlab.core.data.model.PojoSchema;
import cn.ibizlab.util.security.AuthenticationUser;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.Map;
@JsonInclude(JsonInclude.Include.ALWAYS)
......@@ -119,14 +122,105 @@ public class BaseData extends DataObj
return _pojoschema;
}
@JsonIgnore
@JSONField(serialize = false)
public BaseData getParams()
{
BaseData param=null;
Object obj=this.get("params");
if(obj!=null)
{
if(obj instanceof BaseData)
param=(BaseData)obj;
else if(obj instanceof Map)
{
param=new BaseData().setAll((Map)obj);
this.set("params",param);
}
}
else
{
return this;
}
return param;
}
public BaseData setParams(BaseData params)
{
return this.set("params",params);
}
@JsonIgnore
@JSONField(serialize = false)
public BaseData getDatacontext() {
return getParams();
}
@JsonIgnore
@JSONField(serialize = false)
public BaseData getWebcontext() {
return getParams();
}
@JsonIgnore
@JSONField(serialize = false)
public BaseData sessioncontext;
@JsonIgnore
@JSONField(serialize = false)
public BaseData getSessioncontext() {
if(sessioncontext==null)
sessioncontext=new BaseData().setAll(AuthenticationUser.getAuthenticationUser().getSessionParams());
return sessioncontext;
}
@Override
public Object get(Object key) {
if(key==null)
return null;
if(key.toString().equalsIgnoreCase("datacontext"))
return getDatacontext();
else if(key.toString().equalsIgnoreCase("webcontext"))
return getWebcontext();
else if(key.toString().equalsIgnoreCase("sessioncontext"))
return getSessioncontext();
return super.get(key);
}
public BaseData setKey(Object key)
{
return this.set("_key",key);
return this.set("_id",key);
}
@JsonIgnore
@JSONField(serialize = false)
public Serializable getKey()
{
return getSerializableValue("_key");
return getSerializableValue("_id");
}
@JsonIgnore
@JSONField(serialize = false)
private Map<String,BaseData> parentDatas;
@JsonIgnore
@JSONField(serialize = false)
public Map<String,BaseData> getParentData()
{
return parentDatas;
}
public BaseData addParent(String entity,String key)
{
if(StringUtils.isEmpty(entity)||StringUtils.isEmpty(key))
return this;
if(parentDatas==null)
parentDatas=new LinkedHashMap<>();
parentDatas.put(entity,new BaseData().setKey(key));
return this;
}
......
package cn.ibizlab.core.data.dto;
import cn.ibizlab.core.data.filter.QueryBuildContext;
import cn.ibizlab.core.data.model.POSchema;
import cn.ibizlab.util.errors.BadRequestAlertException;
import cn.ibizlab.core.data.filter.QueryFilter;
......@@ -11,6 +12,7 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.mongodb.QueryBuilder;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
......@@ -129,72 +131,12 @@ public class FilterData<T> extends BaseData
return this;
}
public BaseData getParams()
public FilterData addParent(String entity,String key)
{
BaseData param=null;
Object obj=this.get("params");
if(obj!=null)
{
if(obj instanceof BaseData)
param=(BaseData)obj;
else if(obj instanceof Map)
{
param=new BaseData().setAll((Map)obj);
this.set("params",param);
}
}
else
{
return this;
}
return param;
}
public FilterData setParams(BaseData params)
{
return this.set("params",params);
}
@JsonIgnore
@JSONField(serialize = false)
public BaseData getDatacontext() {
return getParams();
}
@JsonIgnore
@JSONField(serialize = false)
public BaseData getWebcontext() {
return getParams();
}
@JsonIgnore
@JSONField(serialize = false)
public BaseData sessioncontext;
@JsonIgnore
@JSONField(serialize = false)
public BaseData getSessioncontext() {
if(sessioncontext==null)
sessioncontext=new BaseData().setAll(AuthenticationUser.getAuthenticationUser().getSessionParams());
return sessioncontext;
}
@Override
public Object get(Object key) {
if(key==null)
return null;
if(key.toString().equalsIgnoreCase("datacontext"))
return getDatacontext();
else if(key.toString().equalsIgnoreCase("webcontext"))
return getWebcontext();
else if(key.toString().equalsIgnoreCase("sessioncontext"))
return getSessioncontext();
return super.get(key);
super.addParent(entity,key);
return this;
}
public String getUserTaskId() {
return this.getStringValue("userTaskId",this.getParams().getStringValue("userTaskId"));
}
......@@ -354,6 +296,26 @@ public class FilterData<T> extends BaseData
return searchCond;
}
@JsonIgnore
@JSONField(serialize = false)
private QueryBuilder queryBuilder=null;
@JsonIgnore
@JSONField(serialize = false)
public QueryBuilder getQueryBuilder() {
if(queryBuilder==null)
{
if(!ObjectUtils.isEmpty(filter)){
QueryBuildContext context=new QueryBuildContext();
context.setFilter(this.getFilter());
queryBuilder=context.getSelectCond();
}
else
queryBuilder=new QueryBuilder();
}
return queryBuilder;
}
public String getSql(String codename)
{
if(getPOSchema()==null)
......
......@@ -124,4 +124,10 @@ public class RequestData<T> extends BaseData
}
public RequestData addParent(String entity,String key)
{
getBaseData().addParent(entity,key);
return this;
}
}
......@@ -70,6 +70,17 @@ public class DSLink
return this;
}
@JSONField(serialize = false)
@JsonIgnore
public Object source;
public DSLink setSource(Object source)
{
this.source=source;
return this;
}
private static DSLink defaultLink;
public static DSLink getDefaultLink()
{
......
package cn.ibizlab.core.data.mongodb;
import cn.ibizlab.core.data.model.DSLink;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.SimpleMongoClientDbFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
@Component
public class DynamicMongoContextHolder
{
private static final Map<String, MongoDbFactory> MONGO_CLIENT_DB_FACTORY_MAP = new HashMap<>();
private static final ThreadLocal<MongoDbFactory> MONGO_DB_FACTORY_THREAD_LOCAL =
new ThreadLocal<>();
public static MongoDbFactory getMongoDbFactory() {
return MONGO_DB_FACTORY_THREAD_LOCAL.get();
}
public static void push(String name) {
MONGO_DB_FACTORY_THREAD_LOCAL.set(MONGO_CLIENT_DB_FACTORY_MAP.get(name));
}
public static void poll() {
MONGO_DB_FACTORY_THREAD_LOCAL.remove();
}
public static synchronized void addFactory(String ds, DSLink link)
{
addFactory(ds,link.getUrl());
}
public static synchronized void addFactory(String ds, String uri)
{
MONGO_CLIENT_DB_FACTORY_MAP.put(ds, new SimpleMongoClientDbFactory(uri));
}
public static synchronized void removeFactory(String ds)
{
MONGO_CLIENT_DB_FACTORY_MAP.remove(ds);
}
@Value("${spring.data.mongodb.uri}")
private String uri;
@PostConstruct
public void init() {
if (!StringUtils.isEmpty(uri)) {
addFactory("master",uri);
}
}
@Bean(name = "mongoTemplate")
public DynamicMongoTemplate dynamicMongoTemplate() {
Iterator<MongoDbFactory> iterator = MONGO_CLIENT_DB_FACTORY_MAP.values().iterator();
return new DynamicMongoTemplate(iterator.next());
}
@Bean(name = "mongoDbFactory")
public MongoDbFactory mongoDbFactory() {
Iterator<MongoDbFactory> iterator = MONGO_CLIENT_DB_FACTORY_MAP.values().iterator();
return iterator.next();
}
}
package cn.ibizlab.core.data.mongodb;
import com.mongodb.client.MongoDatabase;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
@Slf4j
public class DynamicMongoTemplate extends MongoTemplate {
public DynamicMongoTemplate(MongoDbFactory mongoDbFactory) {
super(mongoDbFactory);
}
@Override
protected MongoDatabase doGetDatabase() {
MongoDbFactory mongoDbFactory = DynamicMongoContextHolder.getMongoDbFactory();
return mongoDbFactory == null ? super.doGetDatabase() : mongoDbFactory.getDb();
}
}
......@@ -8,6 +8,7 @@ import java.nio.file.StandardOpenOption;
import java.util.*;
import cn.ibizlab.core.data.model.DSLink;
import cn.ibizlab.core.data.mongodb.DynamicMongoContextHolder;
import cn.ibizlab.core.data.service.ModelService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
......@@ -111,6 +112,8 @@ public class DSSettingServiceImpl implements IDSSettingService {
DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
ds.removeDataSource(et.getDsId());
}
else if(et.isMongodb())
DynamicMongoContextHolder.removeFactory(et.getDsId());
check.remove(et.getDsId());
}
if (!et.getDsId().equals(et.getDsId().toLowerCase()))
......@@ -279,22 +282,29 @@ public class DSSettingServiceImpl implements IDSSettingService {
{
synchronized (lock)
{
DSLink dsLink=dstDataSource.getDSLinkConfig();
if(dstDataSource.isDatabase())
{
DataSourceProperty dataSourceProperty = dstDataSource.getDataSourceProperty();
DynamicRoutingDataSource ds = dataSource;
DataSource dataSource = druidDataSourceCreator.createDataSource(dataSourceProperty);
ds.addDataSource(dstDataSource.getDsId(), dataSource);
dsLink.setSource(dataSource);
}
check.put(dstDataSource.getDsId(), dstDataSource.getDSLinkConfig());
else if(dstDataSource.isMongodb())
{
DynamicMongoContextHolder.addFactory(dstDataSource.getDsId(),dsLink);
}
check.put(dstDataSource.getDsId(), dsLink);
if(!dstDataSource.getDsId().equals(dstDataSource.getDsId().toLowerCase()))
check.put(dstDataSource.getDsId().toLowerCase(), dstDataSource.getDSLinkConfig());
check.put(dstDataSource.getDsId().toLowerCase(), dsLink);
if(!ObjectUtils.isEmpty(dstDataSource.getUsings()))
{
dstDataSource.getUsings().forEach(using->{
check.put(using, dstDataSource.getDSLinkConfig());
check.put(using, dsLink);
if(!using.equals(using.toLowerCase()))
check.put(using.toLowerCase(), dstDataSource.getDSLinkConfig());
check.put(using.toLowerCase(), dsLink);
});
}
}
......@@ -337,6 +347,8 @@ public class DSSettingServiceImpl implements IDSSettingService {
return new PageImpl<DSSetting>(list,context.getPageable(),list.size());
}
}
......@@ -22,6 +22,8 @@ import java.util.List;
public class MongoDataServiceImpl implements IDataService
{
@Override
public ResponseData call(DOModel model, String scope, String datasource, String method, RequestData requestData)
{
......@@ -31,6 +33,7 @@ public class MongoDataServiceImpl implements IDataService
@Override
public boolean create(DOModel model, String scope, String datasource, BaseData et)
{
return false;
}
......
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册