提交 381669ad 编写于 作者: sq3536's avatar sq3536

lite

上级 ed3832ae
package cn.ibizlab.core.lite.extensions.domain;
import cn.ibizlab.core.lite.domain.MetaDataSet;
import cn.ibizlab.core.lite.domain.MetaEntity;
import cn.ibizlab.core.lite.domain.MetaField;
import cn.ibizlab.core.lite.domain.MetaRelationship;
import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.TableField;
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;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@Getter
@Setter
@NoArgsConstructor
@JsonIgnoreProperties(value = "handler")
public class EntityModel {
private String entityId;
private String codeName;
private String entityName;
private String tableName;
private String logicName;
private String systemId;
private MetaEntity entity;
private List<MetaDataSet> dataSets;
private List<FieldModel> fields;
private List<RelationshipModel> references;
private List<RelationshipModel> nesteds;
@JsonIgnore
@JSONField(serialize = false)
private Map<String,FieldModel> fieldMap = null;
@JsonIgnore
@JSONField(serialize = false)
public Map<String,FieldModel> getFieldMap()
{
if(fields!=null&&fieldMap==null)
{
fieldMap=new HashMap<>();
fields.forEach(field->{
fieldMap.put(field.getColumnName(),field);
fieldMap.put(field.getCodeName(),field);
});
}
return fieldMap;
}
public FieldModel getField(String name)
{
if(StringUtils.isEmpty(name))
return null;
if(fields==null)
return null;
if(getFieldMap()!=null)
return getFieldMap().get(name);
return null;
}
}
package cn.ibizlab.core.lite.extensions.domain;
import cn.ibizlab.core.lite.domain.MetaField;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter
@Setter
@NoArgsConstructor
@JsonIgnoreProperties(value = "handler")
public class FieldModel {
private String columnName;
private String codeName;
private String unionName;
private String showName;
private String comment;
private MetaField field;
private RelationshipModel reference;
}
package cn.ibizlab.core.lite.extensions.domain;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter
@Setter
@NoArgsConstructor
@JsonIgnoreProperties(value = "handler")
public class Lookup {
private String mainEntityName;
private String fieldName;
private String refCodeName;
private String refEntityName;
private String refFieldName;
}
package cn.ibizlab.core.lite.extensions.domain;
import cn.ibizlab.core.lite.domain.MetaRelationship;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter
@Setter
@NoArgsConstructor
@JsonIgnoreProperties(value = "handler")
public class RelationshipModel {
private String codeName;
private String entityName;
private String entityCodeName;
private String entityLogicName;
private String systemId;
private String dataSourceName;
private String tableName;
private String entityId;
}
package cn.ibizlab.core.lite.extensions.service;
import cn.ibizlab.core.lite.domain.DstSystem;
import cn.ibizlab.core.lite.domain.MetaEntity;
import cn.ibizlab.core.lite.domain.MetaRelationship;
import cn.ibizlab.core.lite.extensions.domain.EntityModel;
import cn.ibizlab.core.lite.extensions.domain.FieldModel;
import cn.ibizlab.core.lite.extensions.domain.RelationshipModel;
import cn.ibizlab.core.lite.extensions.util.LiteStorage;
import cn.ibizlab.core.lite.service.*;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import javax.annotation.PostConstruct;
import java.sql.Wrapper;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@Service
public class LiteModelService {
@PostConstruct
public void init(){
LiteStorage.setLiteModelService(this);
}
@Autowired
private IMetaEntityService metaEntityService;
public IMetaEntityService getMetaEntityService()
{
return metaEntityService;
}
@Autowired
private IMetaFieldService metaFieldService;
@Autowired
private IMetaRelationshipService metaRelationshipService;
@Autowired
private IDstSystemService dstSystemService;
@Autowired
private IDstDataSourceService dstDataSourceService;
@Autowired
private IMetaDataSetService metaDataSetService;
@Cacheable( value="entitymodel",key = "'row:'+#p0+'.'+#p1")
public EntityModel getEntityModel(String systemId, String name)
{
MetaEntity entity = LiteStorage.getMetaEntity(String.format("%1$s.%2$s",systemId,name));
EntityModel entityModel = new EntityModel();
entityModel.setEntity(entity);
entityModel.setDataSets(metaDataSetService.selectByEntityId(entity.getEntityId()));
Map<String, RelationshipModel> parentSet = new LinkedHashMap();
List<RelationshipModel> references = new ArrayList<>();
metaRelationshipService.selectByEntityId(entity.getEntityId()).forEach(item->
{
RelationshipModel model = new RelationshipModel();
MetaEntity parentEntity = LiteStorage.getMetaEntity(item.getRefEntityId());
if(parentEntity!=null){
model.setCodeName(item.getCodeName());
model.setDataSourceName(parentEntity.getDsName());
model.setEntityCodeName(parentEntity.getCodeName());
model.setEntityId(parentEntity.getEntityId());
model.setEntityLogicName(parentEntity.getLogicName());
model.setEntityName(parentEntity.getEntityName());
model.setSystemId(parentEntity.getSystemId());
model.setTableName(parentEntity.getTableName());
parentSet.put(item.getId(),model);
references.add(model);
}
});
entityModel.setReferences(references);
List<RelationshipModel> nesteds = new ArrayList<>();
metaRelationshipService.selectByRefEntityId(entity.getEntityId()).forEach(item->
{
RelationshipModel model = new RelationshipModel();
MetaEntity subEntity = LiteStorage.getMetaEntity(item.getEntityId());
if(subEntity!=null){
model.setCodeName(StringUtils.isEmpty(item.getNestedName())?item.getCodeName()+"_"+item.getEntityName():item.getNestedName());
model.setDataSourceName(subEntity.getDsName());
model.setEntityCodeName(subEntity.getCodeName());
model.setEntityId(subEntity.getEntityId());
model.setEntityLogicName(subEntity.getLogicName());
model.setEntityName(subEntity.getEntityName());
model.setSystemId(subEntity.getSystemId());
model.setTableName(subEntity.getTableName());
nesteds.add(model);
}
});
entityModel.setNesteds(nesteds);
List<FieldModel> fields = new ArrayList<>();
metaFieldService.selectByEntityId(entity.getEntityId()).forEach(item->
{
FieldModel model = new FieldModel();
model.setField(item);
model.setCodeName(item.getCodeName());
model.setColumnName(item.getFieldName());
model.setComment(item.getFieldLogicName());
model.setShowName(item.getFieldShowName());
model.setUnionName(item.getFieldUniName());
if((!StringUtils.isEmpty(item.getRelationId()))&&(parentSet.containsKey(item.getRelationId())))
model.setReference(parentSet.get(item.getRelationId()));
fields.add(model);
});
entityModel.setFields(fields);
return entityModel;
}
@CacheEvict( value="entitymodel",key = "'row:'+#p0+'.'+#p1")
public void resetEntityModel(String systemId, String name)
{
LiteStorage.resetMetaEntity(String.format("%1$s.%2$s",systemId,name));
}
public void initMetaEntity()
{
metaEntityService.list().forEach(metaEntity -> {
LiteStorage.putMetaEntity(metaEntity);
});
}
}
package cn.ibizlab.core.lite.extensions.util;
import cn.ibizlab.core.lite.domain.MetaEntity;
import cn.ibizlab.core.lite.extensions.service.LiteModelService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import java.util.HashMap;
import java.util.Map;
public class LiteStorage {
private static Map<String,MetaEntity> metaEntityStorage= new HashMap<String, MetaEntity>();
private static Object entityLock=new Object();
public static Map<String,MetaEntity> putMetaEntity(MetaEntity metaEntity)
{
synchronized (entityLock)
{
metaEntityStorage.put(metaEntity.getEntityId(),metaEntity);
metaEntityStorage.put(String.format("%1$s.%2$s",metaEntity.getSystemId(),metaEntity.getEntityName()),metaEntity);
metaEntityStorage.put(String.format("%1$s.%2$s",metaEntity.getSystemId(),metaEntity.getCodeName()),metaEntity);
return metaEntityStorage;
}
}
private static LiteModelService service;
public static void setLiteModelService(LiteModelService service)
{
if(LiteStorage.service==null)
LiteStorage.service=service;
}
public static MetaEntity getMetaEntity(String name)
{
synchronized (entityLock)
{
if(metaEntityStorage.containsKey(name))
return metaEntityStorage.get(name);
else if(name.contains(".")) {
String[] args=name.split(".");
String systemId=args[0];
String code=args[1];
MetaEntity metaEntity = service.getMetaEntityService().getOne(Wrappers.<MetaEntity>lambdaQuery().and(
wrapper ->
wrapper.eq(MetaEntity::getSystemId,systemId).or().eq(MetaEntity::getSystemName,systemId)).and(
wrapper ->
wrapper.eq(MetaEntity::getEntityName,code).or().eq(MetaEntity::getCodeName,code))
,false);
if(metaEntity!=null) {
metaEntityStorage.put(metaEntity.getEntityId(),metaEntity);
metaEntityStorage.put(String.format("%1$s.%2$s",metaEntity.getSystemId(),metaEntity.getEntityName()),metaEntity);
metaEntityStorage.put(String.format("%1$s.%2$s",metaEntity.getSystemId(),metaEntity.getCodeName()),metaEntity);
return metaEntity;
}
}
else {
MetaEntity metaEntity = service.getMetaEntityService().getById(name);
if(metaEntity!=null) {
metaEntityStorage.put(metaEntity.getEntityId(),metaEntity);
metaEntityStorage.put(String.format("%1$s.%2$s",metaEntity.getSystemId(),metaEntity.getEntityName()),metaEntity);
metaEntityStorage.put(String.format("%1$s.%2$s",metaEntity.getSystemId(),metaEntity.getCodeName()),metaEntity);
return metaEntity;
}
}
}
return null;
}
public static void resetMetaEntity(String name)
{
MetaEntity metaEntity = getMetaEntity(name);
if(metaEntity!=null)
{
synchronized (entityLock)
{
metaEntityStorage.remove(metaEntity.getEntityId());
metaEntityStorage.remove(String.format("%1$s.%2$s",metaEntity.getSystemId(),metaEntity.getEntityName()));
metaEntityStorage.remove(String.format("%1$s.%2$s",metaEntity.getSystemId(),metaEntity.getCodeName()));
}
}
}
}
......@@ -121,7 +121,8 @@ public class apiSecurityConfig extends WebSecurityConfigurerAdapter {
// 文件操作
.antMatchers("/"+downloadpath+"/**").permitAll()
.antMatchers("/"+uploadpath).permitAll()
.antMatchers("/"+previewpath+"/**").permitAll();
.antMatchers("/"+previewpath+"/**").permitAll()
.antMatchers("/lite/**").permitAll();
for (String excludePattern : excludesPattern) {
authenticationTokenFilter.addExcludePattern(excludePattern);
......
package cn.ibizlab.api.rest.extensions;
import cn.ibizlab.api.dto.DstAPIDTO;
import cn.ibizlab.core.lite.domain.DstAPI;
import cn.ibizlab.core.lite.domain.DstComponent;
import cn.ibizlab.core.lite.domain.MetaEntity;
import cn.ibizlab.core.lite.extensions.domain.EntityModel;
import cn.ibizlab.core.lite.extensions.service.LiteModelService;
import cn.ibizlab.core.lite.service.IDstComponentService;
import cn.ibizlab.util.errors.BadRequestAlertException;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@Api(tags = {"接口" })
@RestController("api-litecore")
@RequestMapping("")
public class LiteCoreResource {
@Autowired
private LiteModelService liteModelService;
@Autowired
private IDstComponentService dstComponentService;
@RequestMapping(method = RequestMethod.GET, value = "/lite/{system}/entitys/{entity}")
public ResponseEntity<EntityModel> getEntityModel(@PathVariable("system") String system,@PathVariable("entity") String entity) {
return ResponseEntity.status(HttpStatus.OK).body(liteModelService.getEntityModel(system,entity));
}
@RequestMapping(method = RequestMethod.GET, value = "/lite/{app}/components/{component}")
public ResponseEntity<JSON> getComponent(@PathVariable("app") String app, @PathVariable("component") String component) {
DstComponent dstComponent = dstComponentService.getOne(Wrappers.<DstComponent>lambdaQuery().eq(DstComponent::getAppId,app).and(
wrapper ->
wrapper.eq(DstComponent::getCodeName,component).or().eq(DstComponent::getName,component)),
true);
if(StringUtils.isEmpty(dstComponent.getConfig()))
throw new BadRequestAlertException("未找到配置","DstComponent",component);
return ResponseEntity.status(HttpStatus.OK).body(JSON.parseObject(dstComponent.getConfig()));
}
}
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册