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

剥离模型初始化

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