提交 219aade2 编写于 作者: sq3536's avatar sq3536

model

上级 1f452fbb
package cn.ibizlab.core.extensions.mapper;
import cn.ibizlab.core.lite.extensions.domain.EntityObj;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface DbEntityMapper extends BaseMapper<EntityObj>{
@Select("${sql} \n" +
"<where><if test=\"ew!=null and ew.sqlSegment!=null and !ew.emptyOfWhere\">${ew.sqlSegment}</if></where> \n" +
"<if test=\"ew!=null and ew.sqlSegment!=null and ew.emptyOfWhere\">${ew.sqlSegment}</if> ")
List<EntityObj> search(@Param("sql") String sql,@Param("ew") Wrapper<EntityObj> wrapper);
}
\ No newline at end of file
package cn.ibizlab.core.lite.extensions.domain;
import cn.ibizlab.util.domain.DTOBase;
import cn.ibizlab.util.domain.EntityBase;
import cn.ibizlab.util.helper.DataObject;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.util.ObjectUtils;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
public class DataObj<K,V> extends ConcurrentHashMap<K,V> {
public String getRowKey()
{
return this.getStringValue("ROWKEY","");
}
public <T> T setRowKey(String rowKey)
{
return this.set("ROWKEY",(V)rowKey);
}
public Timestamp getTimestamp()
{
return getTimestampBegin("TIMESTAMP");
}
public <T> T setTimestamp(Timestamp timestamp)
{
return this.set("TIMESTAMP",(V)DataObject.getTimestampValue(timestamp,DataObject.getBeginDate()));
}
public <T> T set(String key, V value)
{
return (T)this.put((K)key.toUpperCase(),value);
}
@Override
public V get(Object key) {
if(key==null)
return null;
V objValue=super.get(key);
if(objValue==null)
objValue=super.get(key.toString().toUpperCase());
if(objValue==null)
objValue=super.get(key.toString().toLowerCase());
return objValue;
}
public JSONObject getJSONObjectValue(String strParamName) {
return getJSONObjectValue(strParamName,new JSONObject());
}
public JSONObject getJSONObjectValue(String strParamName, JSONObject jDefault) {
return DataObject.getJSONObjectValue(this.get(strParamName),jDefault);
}
public List<String> getListValue( String strParamName) {
return DataObject.getListValue(strParamName);
}
public JSONArray getJSONArrayValue( String strParamName) {
return getJSONArrayValue(strParamName,new JSONArray());
}
public JSONArray getJSONArrayValue( String strParamName, JSONArray jDefault) {
return DataObject.getJSONArrayValue(this.get(strParamName),jDefault);
}
public Integer getIntegerValue(String objValue) {
return getIntegerValue(objValue, Integer.MIN_VALUE);
}
public int getIntegerValue( String strParamName, int nDefault) {
return DataObject.getIntegerValue(this.get(strParamName),nDefault);
}
public Float getFloatValue(String objValue) {
return this.getFloatValue(objValue,-9999f);
}
public Float getFloatValue( String strParamName, float fDefault) {
return DataObject.getFloatValue(this.get(strParamName),fDefault);
}
public BigDecimal getBigDecimalValue(String objValue) {
return this.getBigDecimalValue(objValue,BigDecimal.valueOf(-9999));
}
public BigDecimal getBigDecimalValue( String strParamName, BigDecimal fDefault) {
return DataObject.getBigDecimalValue(this.get(strParamName),fDefault);
}
public Long getLongValue( String strParamName) {
return this.getLongValue(strParamName,Long.MIN_VALUE);
}
public Long getLongValue( String strParamName, long nDefault) {
return DataObject.getLongValue(this.get(strParamName),nDefault);
}
public String getStringValue(String objValue) {
return getStringValue(objValue, "");
}
public String getStringValue( String strParamName, String strDefault) {
return DataObject.getStringValue(this.get(strParamName),strDefault);
}
public byte[] getBinaryValue(String objValue) {
return getBinaryValue(objValue, null);
}
public byte[] getBinaryValue(String strParamName, byte[] def) {
return DataObject.getBinaryValue(this.get(strParamName),def);
}
public Timestamp getTimestampBegin( String strParamName) {
return getTimestampValue(strParamName,DataObject.getBeginDate());
}
public Timestamp getTimestampEnd( String strParamName) {
Object objValue = this.get(strParamName);
if (objValue == null) {
return DataObject.getEndDate();
}
try {
Timestamp t= DataObject.getTimestampValue(objValue,DataObject.getEndDate());
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String time = df.format(t);
Calendar cl=Calendar.getInstance(TimeZone.getTimeZone("GMT+8"));
cl.setTime(Timestamp.valueOf(time+" 23:59:59"));
return new Timestamp(cl.getTime().getTime());
} catch (Exception ex) {
return DataObject.getEndDate();
}
}
public Timestamp getTimestampValue( String strParamName, Timestamp dtDefault) {
Object objValue = this.get(strParamName);
if (objValue == null || objValue.equals("")) {
return dtDefault;
}
try {
return DataObject.getTimestampValue(objValue,null);
} catch (Exception ex) {
return dtDefault;
}
}
public <T> T copyTo(T targetEntity, boolean bIncEmpty){
if(targetEntity instanceof EntityBase){
for(K field : this.keySet()){
Object value=this.get(field);
if( !ObjectUtils.isEmpty(value) || ObjectUtils.isEmpty(value) && bIncEmpty ){
((EntityBase)targetEntity).set((String)field,value);
}
}
}
else if(targetEntity instanceof DTOBase){
for(K field : this.keySet()){
Object value=this.get(field);
if( !ObjectUtils.isEmpty(value) || ObjectUtils.isEmpty(value) && bIncEmpty ){
((DTOBase)targetEntity).set(((String)field).toLowerCase(),value);
}
}
}
else if(targetEntity instanceof DataObj){
for(K field : this.keySet()){
Object value=this.get(field);
if( !ObjectUtils.isEmpty(value) || ObjectUtils.isEmpty(value) && bIncEmpty ){
((DataObj) targetEntity).set((String)field,value);
}
}
}
else if(targetEntity instanceof Map){
for(K field : this.keySet()){
Object value=this.get(field);
if( !ObjectUtils.isEmpty(value) || ObjectUtils.isEmpty(value) && bIncEmpty ){
((Map) targetEntity).put(field,value);
}
}
}
return targetEntity;
}
}
......@@ -91,5 +91,62 @@ public class EntityModel {
}
public String getSqlSegment(String dataSet)
{
if("BASE".equalsIgnoreCase(dataSet))
{
String columnSet="";
for(FieldModel fieldModel:fields)
{
String columnExp=fieldModel.getColumnExp();
if(StringUtils.isEmpty(columnExp))
continue;
if(!StringUtils.isEmpty(columnSet)){
columnSet=columnSet+",";
}
columnSet=columnSet+columnExp;
}
return "select "+columnSet+" from "+this.getTableName()+" ";
}
else if("CORE".equalsIgnoreCase(dataSet))
{
String columnSet="";
for(FieldModel fieldModel:fields)
{
if(StringUtils.isEmpty(fieldModel.getField().getUnionKey())&&1!=fieldModel.getField().getKeyField())
continue;
String columnExp=fieldModel.getColumnExp();
if(StringUtils.isEmpty(columnExp))
continue;
if(!StringUtils.isEmpty(columnSet)){
columnSet=columnSet+",";
}
columnSet=columnSet+columnExp;
}
return "select "+columnSet+" from "+this.getTableName()+" ";
}
else
{
if(dataSets!=null)
for (MetaDataSet metaDataSet:dataSets)
if(metaDataSet.getCodeName().equalsIgnoreCase(dataSet)&&(!StringUtils.isEmpty(metaDataSet.getDsCode())))
return "select t1.* from ("+metaDataSet.getDsCode()+") t1";
}
return "";
}
public String getDsName()
{
String dsName=this.getEntity().getDsName();
if(StringUtils.isEmpty(dsName))
{
dsName=this.getSystemId()+"-master";
}
return dsName;
}
}
package cn.ibizlab.core.lite.extensions.domain;
import cn.ibizlab.core.lite.extensions.model.DataModel;
import cn.ibizlab.core.lite.extensions.model.Property;
import cn.ibizlab.core.lite.extensions.util.LiteStorage;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.util.StringUtils;
public class EntityObj extends DataObj<String,Object> {
@JsonIgnore
@JSONField(serialize = false)
private EntityModel entityModel;
@JsonIgnore
@JSONField(serialize = false)
public EntityModel getEntityModel()
{
if(entityModel==null)
entityModel=LiteStorage.getLiteModelService().getEntityModel(this.getDstSystemId(),this.getMetaEntityName());
return entityModel;
}
@JsonIgnore
@JSONField(serialize = false)
private DataModel dataModel;
@JsonIgnore
@JSONField(serialize = false)
public DataModel getDataModel() {
if(this.dataModel==null&&this.getProperty()!=null)
this.dataModel=this.getProperty().getOwnerDataModel();
return dataModel;
}
@JsonIgnore
@JSONField(serialize = false)
public EntityObj setDataModel(DataModel dataModel) {
this.dataModel = dataModel;
return this;
}
@JsonIgnore
@JSONField(serialize = false)
private Property property;
@JsonIgnore
@JSONField(serialize = false)
public Property getProperty() {
return property;
}
@JsonIgnore
@JSONField(serialize = false)
public EntityObj setProperty(Property property) {
if(property!=null) {
if(!StringUtils.isEmpty(property.getSystem()))
this.setDstSystemId(property.getSystem());
if(!StringUtils.isEmpty(property.getPropertyEntity()))
this.setMetaEntityName(property.getPropertyEntity());
}
this.property = property;
return this;
}
@JsonIgnore
@JSONField(serialize = false)
public String getMetaEntityName()
{
return this.getStringValue("METAENTITYNAME","");
}
@JsonIgnore
@JSONField(serialize = false)
public EntityObj setMetaEntityName(String metaEntityName)
{
return this.set("METAENTITYNAME",metaEntityName);
}
@JsonIgnore
@JSONField(serialize = false)
public String getDstSystemId()
{
return this.getStringValue("DSTSYSTEMID","");
}
@JsonIgnore
@JSONField(serialize = false)
public EntityObj setDstSystemId(String dstSystemId)
{
return this.set("DSTSYSTEMID",dstSystemId);
}
}
package cn.ibizlab.core.lite.extensions.domain;
import cn.ibizlab.core.lite.domain.MetaField;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.util.StringUtils;
@Getter
@Setter
......@@ -25,4 +28,19 @@ public class FieldModel {
private MetaField field;
private RelationshipModel reference;
@JsonIgnore
@JSONField(serialize = false)
public String getColumnExp()
{
if(1==field.getPhysicalField())
{
return columnName+" as \""+columnName.toUpperCase()+"\"";
}
else if(!StringUtils.isEmpty(field.getExpression()))
{
return field.getExpression()+" as \""+columnName.toUpperCase()+"\"";
}
return "";
}
}
package cn.ibizlab.core.lite.extensions.domain;
import cn.ibizlab.core.lite.extensions.model.DataModel;
import cn.ibizlab.core.lite.extensions.model.Property;
import cn.ibizlab.core.lite.extensions.util.LiteStorage;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.List;
public class ModelObj extends DataObj<String,Object> {
@JsonIgnore
@JSONField(serialize = false)
private DataModel dataModel;
@JsonIgnore
@JSONField(serialize = false)
public DataModel getDataModel() {
return dataModel;
}
@JsonIgnore
@JSONField(serialize = false)
public void setDataModel(DataModel dataModel) {
this.dataModel = dataModel;
}
public EntityObj getEntity(String name)
{
Property property=this.getDataModel().getObjectProperty(name);
Object obj=this.get(name);
if(obj!=null&&obj instanceof EntityObj)
return (EntityObj) obj;
else if(property==null)
return new EntityObj().setDataModel(this.getDataModel()).setProperty(property).setRowKey(this.getRowKey());
else
return new EntityObj().setDataModel(this.getDataModel()).setDstSystemId(this.getDataModel().getFactPorperty().getSystem()).setMetaEntityName(name).setRowKey(this.getRowKey());
}
public List<ModelObj> getNested(String name)
{
Object list=this.get(name);
if(list!=null&&list instanceof List)
return (List<ModelObj>)list;
else
return new ArrayList<>();
}
}
package cn.ibizlab.core.lite.extensions.filter;
import cn.ibizlab.core.lite.domain.MetaEntity;
import cn.ibizlab.core.lite.extensions.domain.EntityObj;
import cn.ibizlab.util.filter.QueryWrapperContext;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
/**
* 关系型数据实体[MetaEntity] 查询条件对象
*/
@Slf4j
@Data
public class DbEntitySearchContext extends QueryWrapperContext<EntityObj> {
}
......@@ -23,6 +23,38 @@ public class DataModel {
@JSONField(serialize = false)
private DataModel parentDataModel;
@JsonIgnore
@JSONField(serialize = false)
public DataModel getRootDataModel()
{
if(this.getParentDataModel()==null)
return this;
else
return this.getParentDataModel().getRootDataModel();
}
@JsonIgnore
@JSONField(serialize = false)
public int getLayerNo()
{
int layerNo=1;
DataModel parent=this.getParentDataModel();
while (parent!=null)
{
layerNo++;
parent=parent.getParentDataModel();
}
return layerNo;
}
@JsonIgnore
@JSONField(serialize = false)
public Property getRootFactPorperty()
{
return this.getRootFactPorperty().getFactPorperty();
}
@JSONField(ordinal = 1)
private String dataModelName;
public String getDataModelName()
......@@ -75,25 +107,92 @@ public class DataModel {
return null;
}
public Property getObjectProperty(String propertyName)
public Property getObjectProperty(String name)
{
if(this.getObjectProperties()!=null&&(!StringUtils.isEmpty(propertyName)))
if(this.getObjectProperties()!=null&&(!StringUtils.isEmpty(name)))
{
for (Property property:this.getObjectProperties())
if(propertyName.equalsIgnoreCase(property.getPropertyName()))
if(name.equalsIgnoreCase(property.getPropertyName()))
return property;
for (Property property:this.getObjectProperties())
if(name.equalsIgnoreCase(property.getPropertyEntity()))
return property;
}
return null;
}
public DataModel getNestedDataModel(String dataModelName)
public Property findObjectProperty(String name)
{
Property rt=this.getObjectProperty(name);
if(rt!=null)
return rt;
if(this.getNestedDataModels()!=null)
{
for(DataModel dm:this.getNestedDataModels())
{
rt=dm.findObjectProperty(name);
if(rt!=null)
return rt;
}
}
DataModel parent=this.getParentDataModel();
while (parent!=null)
{
rt=parent.getObjectProperty(name);
if(rt!=null)
return rt;
else
parent=parent.getParentDataModel();
}
return null;
}
public DataModel getNestedDataModel(String name)
{
if(this.getNestedDataModels()!=null&&(!StringUtils.isEmpty(dataModelName)))
if(this.getNestedDataModels()!=null&&(!StringUtils.isEmpty(name)))
{
for (DataModel dataModel:this.getNestedDataModels())
if(dataModelName.equalsIgnoreCase(dataModel.getDataModelName()))
if(name.equalsIgnoreCase(dataModel.getDataModelName()))
return dataModel;
for (DataModel dataModel:this.getNestedDataModels())
if(dataModel.getObjectProperty(name)!=null)
return dataModel;
}
return null;
}
public DataModel findDataModel(String name)
{
if(name.equalsIgnoreCase(this.getDataModelName()))
return this;
DataModel rt=this.getNestedDataModel(name);
if(rt!=null)
return rt;
if(this.getNestedDataModels()!=null)
{
for(DataModel dm:this.getNestedDataModels())
{
rt=dm.getNestedDataModel(name);
if(rt!=null)
return rt;
}
}
if(this.getObjectProperty(name)!=null)
return this;
DataModel parent=this.getParentDataModel();
while (parent!=null)
{
if(name.equalsIgnoreCase(parent.getDataModelName()))
rt=parent;
if(rt==null&&parent.getObjectProperty(name)!=null)
rt=parent;
if(rt!=null)
return rt;
else
parent=parent.getParentDataModel();
}
return null;
}
......
......@@ -25,6 +25,34 @@ public class Property {
@JSONField(serialize = false)
private DataModel ownerDataModel;
@JsonIgnore
@JSONField(serialize = false)
public DataModel getRootDataModel()
{
return this.getOwnerDataModel().getRootDataModel();
}
@JsonIgnore
@JSONField(serialize = false)
public Property getRootFactPorperty()
{
return this.getOwnerDataModel().getRootFactPorperty();
}
@JsonIgnore
@JSONField(serialize = false)
public Property getFactPorperty()
{
return this.getOwnerDataModel().getFactPorperty();
}
@JsonIgnore
@JSONField(serialize = false)
public int getLayerNo()
{
return this.getOwnerDataModel().getLayerNo();
}
@JSONField(ordinal = 1)
private String propertyName;
......
package cn.ibizlab.core.lite.extensions.service;
import cn.ibizlab.core.lite.extensions.domain.EntityModel;
import cn.ibizlab.core.lite.extensions.domain.EntityObj;
import cn.ibizlab.util.filter.QueryFilter;
import com.sun.jndi.toolkit.dir.SearchFilter;
import java.util.List;
public interface CommonEntityService {
List<EntityObj> selectBase(EntityModel entityModel, QueryFilter filter);
List<EntityObj> selectCore(EntityModel entityModel, QueryFilter filter);
List<EntityObj> search(String dataSet,EntityModel entityModel, QueryFilter filter);
List<EntityObj> search(String dsName,String sql, QueryFilter filter);
}
package cn.ibizlab.core.lite.extensions.service;
import cn.ibizlab.core.lite.extensions.domain.EntityModel;
import cn.ibizlab.core.lite.extensions.domain.EntityObj;
import cn.ibizlab.core.lite.extensions.filter.DbEntitySearchContext;
import cn.ibizlab.core.extensions.mapper.DbEntityMapper;
import cn.ibizlab.util.filter.QueryFilter;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.List;
@Service
@Primary
@Slf4j
public class DbEntityService extends ServiceImpl<DbEntityMapper, EntityObj> implements CommonEntityService{
@Override
public List<EntityObj> selectBase(EntityModel entityModel, QueryFilter filter) {
return search("BASE",entityModel,filter);
}
@Override
public List<EntityObj> selectCore(EntityModel entityModel, QueryFilter filter) {
return search("CORE",entityModel,filter);
}
@Override
public List<EntityObj> search(String dataSet, EntityModel entityModel, QueryFilter filter) {
String sql=entityModel.getSqlSegment(dataSet);
if(StringUtils.isEmpty(sql))
return new ArrayList<>();
else
return search(entityModel.getDsName(),sql,filter);
}
@Override
public List<EntityObj> search(String dsName, String sql, QueryFilter filter) {
try
{
DynamicDataSourceContextHolder.push(dsName);
DbEntitySearchContext context=new DbEntitySearchContext();
context.setFilter(filter);
return baseMapper.search(sql,context.getSelectCond());
}
catch(Exception ex)
{
log.error("详细错误信息:" + ex.getMessage() + ", 执行sql:" + sql);
return null;
}
finally
{
DynamicDataSourceContextHolder.poll();
}
}
}
package cn.ibizlab.core.lite.extensions.service;
import cn.ibizlab.core.lite.extensions.domain.EntityModel;
import cn.ibizlab.core.lite.extensions.domain.EntityObj;
import cn.ibizlab.util.filter.QueryFilter;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class MongoEntityService implements CommonEntityService{
@Override
public List<EntityObj> selectBase(EntityModel entityModel, QueryFilter filter) {
return null;
}
@Override
public List<EntityObj> selectCore(EntityModel entityModel, QueryFilter filter) {
return null;
}
@Override
public List<EntityObj> search(String dataSet, EntityModel entityModel, QueryFilter filter) {
return null;
}
@Override
public List<EntityObj> search(String dsName, String sql, QueryFilter filter) {
return null;
}
}
......@@ -32,6 +32,11 @@ public class LiteStorage {
LiteStorage.service=service;
}
public static LiteModelService getLiteModelService()
{
return service;
}
public static MetaEntity getMetaEntity(String name)
{
synchronized (entityLock)
......
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册