提交 578ff47f 编写于 作者: sq3536's avatar sq3536

剥离模型初始化

上级 7429b1c3
package cn.ibizlab.core.data.lite;
import cn.ibizlab.core.data.service.ModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
......@@ -14,6 +15,7 @@ public class LiteStorage {
private static LiteModelFeignClient liteModelFeignClient;
private static DynamicModelService dynamicModelService;
private static ModelService globalModelService;
......@@ -39,21 +41,15 @@ public class LiteStorage {
return liteModelFeignClient;
}
public static EntityModel getEntityModel(String system,String entity)
public static void setModelService(ModelService globalModelService)
{
EntityModel entityModel = null;
try {
entityModel = getDynamicModelService().getDynamicEntity(system,entity);
} catch (Exception exception) {
if(LiteStorage.globalModelService==null)
LiteStorage.globalModelService=globalModelService;
}
if(entityModel==null)
entityModel = getLiteModelService().getProxyEntityModel(system,entity);
return entityModel;
public static ModelService getModelService()
{
return globalModelService;
}
......@@ -64,22 +60,17 @@ public class LiteStorage {
@Lazy
private DynamicModelService dynamicService;
@Autowired
private ModelService modelService;
@Value("${ibiz.model.path:/app/file/model/}")
private String modelPath;
public String getModelPath()
{
if(modelPath.equals(File.separator))
return modelPath.substring(0,modelPath.length()-1);
return modelPath;
}
@PostConstruct
public void init(){
LiteStorage.setLiteModelService(liteService);
LiteStorage.setDynamicModelService(dynamicService);
LiteStorage.MODEL_PATH=getModelPath();
LiteStorage.setModelService(modelService);
LiteStorage.MODEL_PATH=modelService.getModelPath();
}
......
......@@ -2,6 +2,7 @@ package cn.ibizlab.core.data.service;
import cn.ibizlab.core.data.domain.DOModel;
import cn.ibizlab.core.data.lite.*;
import cn.ibizlab.core.data.model.POSchema;
import cn.ibizlab.core.data.model.PojoSchema;
import cn.ibizlab.core.data.model.TransUtils;
import cn.ibizlab.util.errors.BadRequestAlertException;
......@@ -16,6 +17,7 @@ import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import javax.annotation.PostConstruct;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
......@@ -30,6 +32,8 @@ import java.util.Map;
@Slf4j
public class ModelService {
public static String MODEL_PATH;
@Value("${ibiz.model.path:/app/file/model/}")
private String modelPath;
......@@ -55,10 +59,7 @@ public class ModelService {
private ModelService proxy;
public DOModel getDOModel(String system,String entity)
{
return doModelService.get(system+".domain."+entity);
}
......@@ -196,6 +197,36 @@ public class ModelService {
return entityModel;
}
public DOModel loadDOModel(String system,String entity)
{
EntityModel entityModel= proxy.getEntityModel(system,entity);
DOModel doModel=new DOModel();
if(entityModel!=null) {
PojoSchema schema = TransUtils.EntityModelModel2Schema(entityModel);
if (schema != null) {
doModel.setSchema(schema);
for (String dsType : entityModel.getDsTypes()) {
POSchema poSchema = TransUtils.EntityModelModel2PO(entityModel, dsType);
if (poSchema != null) {
doModel.addPOSchema(dsType, poSchema);
}
}
}
}
return doModel;
}
public DOModel getDOModel(String system,String entity)
{
return doModelService.get(system+".domain."+entity);
}
@PostConstruct
public void init()
{
MODEL_PATH=this.getModelPath();
}
}
......@@ -3,6 +3,7 @@ package cn.ibizlab.core.data.service.impl;
import cn.ibizlab.core.data.domain.DOModel;
import cn.ibizlab.core.data.dto.*;
import cn.ibizlab.core.data.model.DSLink;
import cn.ibizlab.core.data.service.IDOModelService;
import cn.ibizlab.core.data.service.IDSSettingService;
import cn.ibizlab.core.data.service.IDataService;
import cn.ibizlab.core.data.service.ModelService;
......@@ -44,6 +45,7 @@ public class BaseDataService implements IDataService {
@Autowired
private IDSSettingService dsSettingService;
public IDataService getProxyService(String datasource)
{
DSLink dsLink=dsSettingService.getDataSource(datasource);
......
......@@ -9,11 +9,8 @@ import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.*;
import cn.ibizlab.core.data.lite.EntityModel;
import cn.ibizlab.core.data.lite.LiteStorage;
import cn.ibizlab.core.data.model.POSchema;
import cn.ibizlab.core.data.model.PojoSchema;
import cn.ibizlab.core.data.model.TransUtils;
import cn.ibizlab.core.data.service.ModelService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
......@@ -72,14 +69,14 @@ public class DOModelServiceImpl implements IDOModelService {
@Cacheable( value="domodel",key = "'row:'+#p0")
@Override
public DOModel get(String key) {
DOModel doModel=new DOModel();
DOModel doModel=null;
PojoSchema schema= null;
String[] args=key.split("[.]");
String system=args[0];
String entity=args[2];
Path storePath = Paths.get(LiteStorage.MODEL_PATH,system,"repo",entity,"domain",entity+".json");
Path storePath = Paths.get(ModelService.MODEL_PATH,system,"repo",entity,"domain",entity+".json");
if(!Files.exists(storePath))
{
try {
......@@ -91,7 +88,7 @@ public class DOModelServiceImpl implements IDOModelService {
args=entityId.split("[.]");
system=args[0];
entity=args[2];
storePath = Paths.get(LiteStorage.MODEL_PATH,system,"repo",entity,"domain",entity+".json");
storePath = Paths.get(ModelService.MODEL_PATH,system,"repo",entity,"domain",entity+".json");
}
} catch (Exception exception) {
throw new BadRequestAlertException("获取模型失败","DOModel",key);
......@@ -100,23 +97,13 @@ public class DOModelServiceImpl implements IDOModelService {
if(!Files.exists(storePath))
{
EntityModel entityModel= modelService.getEntityModel(system,entity);
if(entityModel!=null) {
schema=TransUtils.EntityModelModel2Schema(entityModel);
doModel = modelService.loadDOModel(system,entity);
schema = doModel.getSchema();
if(schema!=null)
{
for(String dsType:entityModel.getDsTypes())
{
POSchema poSchema=TransUtils.EntityModelModel2PO(entityModel,dsType);
if(poSchema!=null)
{
doModel.addPOSchema(dsType,poSchema);
}
}
if(doModel.getPoSchemas()!=null)
{
Path poPath = Paths.get(LiteStorage.MODEL_PATH,entityModel.getSystemId(),"repo",entityModel.getEntityName(),"repository",entityModel.getEntityName()+".json");
Path poPath = Paths.get(ModelService.MODEL_PATH,doModel.getSystemId(),"repo",doModel.getName(),"repository",doModel.getName()+".json");
try {
File dir=poPath.getParent().toFile();
if(!dir.exists())
......@@ -126,30 +113,28 @@ public class DOModelServiceImpl implements IDOModelService {
throw new BadRequestAlertException("保存文件失败","POSchemas",poPath.toString());
}
}
schema.writeTo(Paths.get(LiteStorage.MODEL_PATH,entityModel.getSystemId(),"repo",entityModel.getEntityName(),"domain",entityModel.getEntityName()+".json"));
}
schema.writeTo(Paths.get(ModelService.MODEL_PATH,doModel.getSystemId(),"repo",doModel.getName(),"domain",doModel.getName()+".json"));
}
}
else {
doModel = new DOModel();
schema = PojoSchema.fromPath(storePath);
Path poPath = Paths.get(LiteStorage.MODEL_PATH,system,"repo",entity,"repository",entity+".json");
if(Files.exists(poPath))
{
if(schema!=null) {
doModel.setSchema(schema);
Path poPath = Paths.get(ModelService.MODEL_PATH, system, "repo", entity, "repository", entity + ".json");
if (Files.exists(poPath)) {
try {
doModel.setPoSchemas(JSON.parseObject(new String(Files.readAllBytes(poPath), StandardCharsets.UTF_8), new TypeReference<LinkedHashMap<String, POSchema>>(){} ));
doModel.setPoSchemas(JSON.parseObject(new String(Files.readAllBytes(poPath), StandardCharsets.UTF_8), new TypeReference<LinkedHashMap<String, POSchema>>() {
}));
} catch (IOException e) {
}
}
}
}
if(schema!=null)
doModel.setSchema(schema);
else
if(schema==null)
throw new BadRequestAlertException("未找到对应的模型","DOModel",key);
return doModel;
......
......@@ -7,8 +7,8 @@ import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.*;
import cn.ibizlab.core.data.lite.LiteStorage;
import cn.ibizlab.core.data.model.DSLink;
import cn.ibizlab.core.data.service.ModelService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
......@@ -47,7 +47,7 @@ public class DSSettingServiceImpl implements IDSSettingService {
public Map<String, DSLink> getAllConfigs()
{
Path path = Paths.get(LiteStorage.MODEL_PATH,"DATASOURCE.json");
Path path = Paths.get(ModelService.MODEL_PATH,"DATASOURCE.json");
try {
Map<String, DSLink> all=new LinkedHashMap<>();
......@@ -151,7 +151,7 @@ public class DSSettingServiceImpl implements IDSSettingService {
et.setDSLinkConfig(config);
}
});
Path path = Paths.get(LiteStorage.MODEL_PATH, "DATASOURCE.json");
Path path = Paths.get(ModelService.MODEL_PATH, "DATASOURCE.json");
try {
Files.write(path, JSON.toJSONBytes(newConfigs));
} catch (Exception e) {
......@@ -229,7 +229,7 @@ public class DSSettingServiceImpl implements IDSSettingService {
newConfigs.put(config.getName(), et.getDSLinkConfig());
}
});
Path path = Paths.get(LiteStorage.MODEL_PATH, "DATASOURCE.json");
Path path = Paths.get(ModelService.MODEL_PATH, "DATASOURCE.json");
try {
Files.write(path, JSON.toJSONBytes(newConfigs));
} catch (Exception e) {
......
......@@ -7,16 +7,15 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Collection;
import cn.ibizlab.core.data.lite.EntityModel;
import cn.ibizlab.core.data.lite.LiteStorage;
import cn.ibizlab.core.data.domain.DOModel;
import cn.ibizlab.core.data.model.PojoSchema;
import cn.ibizlab.core.data.model.TransUtils;
import cn.ibizlab.core.data.service.ModelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.beans.factory.annotation.Value;
import cn.ibizlab.util.errors.BadRequestAlertException;
import org.springframework.transaction.annotation.Transactional;
import cn.ibizlab.core.data.domain.DTOModel;
......@@ -31,8 +30,8 @@ import cn.ibizlab.core.data.service.IDTOModelService;
@Service
public class DTOModelServiceImpl implements IDTOModelService {
@Value("${ibiz.model.path:/app/file/model/}")
private String modelPath;
@Autowired
private ModelService modelService;
@Override
public boolean create(DTOModel et) {
......@@ -72,13 +71,24 @@ public class DTOModelServiceImpl implements IDTOModelService {
if(entity.endsWith("DTO"))
entity=entity.substring(0,entity.length()-3);
Path storePath = Paths.get(LiteStorage.MODEL_PATH,system,"repo",entity,"dto",dto+".json");
Path storePath = Paths.get(ModelService.MODEL_PATH,system,"repo",entity,"dto",dto+".json");
if(!Files.exists(storePath))
{
DOModel doModel = modelService.getDOModel(system,entity);
schema = doModel.getSchema().copy();
system=doModel.getSystemId();
dto=dto.replace(entity,doModel.getName());
entity=doModel.getName();
key=system.concat(".dto.").concat(dto);
storePath = Paths.get(ModelService.MODEL_PATH,system,"repo",entity,"dto",dto+".json");
}
EntityModel entityModel= LiteStorage.getEntityModel(system,entity);
if(entityModel!=null) {
schema= TransUtils.EntityModelModel2Schema(entityModel);
if(!Files.exists(storePath))
{
if(schema!=null) {
schema.setName(dto);
schema.setId(key);
if(schema!=null)
......
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册