提交 2aa2dc28 编写于 作者: tangyaolong's avatar tangyaolong

【dst模型初始化导入导出】第三版

上级 156edbc8
......@@ -8,6 +8,8 @@ import cn.ibizlab.core.analysis.service.IDAMetricService;
import cn.ibizlab.core.extensions.domain.AssembleModel;
import cn.ibizlab.core.lite.domain.*;
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.model.DataModel;
import cn.ibizlab.core.lite.extensions.model.Property;
import cn.ibizlab.core.lite.extensions.service.LiteModelService;
......@@ -34,6 +36,7 @@ import org.springframework.web.context.request.ServletRequestAttributes;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
......@@ -73,15 +76,15 @@ public class MetaModelExService extends MetaModelServiceImpl {
@Autowired
@Lazy
private IMetaDataSetService metaDataSetService;
private IMetaDataSetService dataSetService;
@Autowired
@Lazy
private IMetaRelationshipService metaRelationshipService;
private IMetaRelationshipService relationshipService;
@Autowired
@Lazy
private IMetaFieldService metaFieldService;
private IMetaFieldService fieldService;
@Value("${ibiz.filePath:/app/file/}")
private String fileRoot;
......@@ -124,26 +127,58 @@ public class MetaModelExService extends MetaModelServiceImpl {
String strModel = getModel(objFile.getString("id"));
List<AssembleModel> models = JSONObject.parseArray(strModel, AssembleModel.class);
for (AssembleModel model : models) {
// 前置铺垫实体模型,防止外键冲突
//基础数据导入
if (model.getEntityModels() != null) {
LinkedHashSet<MetaEntity> metaEntities = new LinkedHashSet<>();
LinkedHashSet<MetaEntity> entities = new LinkedHashSet<>();
LinkedHashSet<MetaRelationship> p_relations = new LinkedHashSet<>();
LinkedHashSet<MetaRelationship> s_relations = new LinkedHashSet<>();
LinkedHashSet<MetaField> fields = new LinkedHashSet<>();
LinkedHashSet<MetaDataSet> dataSets = new LinkedHashSet<>();
model.getEntityModels().forEach(item -> {
metaEntities.add(item.getEntity());
if(!ObjectUtils.isEmpty(item.getReferences())){
for(RelationshipModel relationModel : item.getReferences()){
if(!ObjectUtils.isEmpty(relationModel.getRelation())){
p_relations.add(relationModel.getRelation());
}
}
}
if(!ObjectUtils.isEmpty(item.getNesteds())){
for(RelationshipModel relationModel : item.getNesteds()){
if(!ObjectUtils.isEmpty(relationModel.getRelation())){
s_relations.add(relationModel.getRelation());
}
}
}
if(!ObjectUtils.isEmpty(item.getFields())){
List<FieldModel> fieldModels = item.getFields();
for(FieldModel fieldModel : fieldModels){
if(!ObjectUtils.isEmpty(fieldModel.getField())){
fields.add(fieldModel.getField());
}
}
}
if(!ObjectUtils.isEmpty(item.getDataSets())){
dataSets.addAll(item.getDataSets());
}
entities.add(item.getEntity());
});
metaEntityService.saveBatch(metaEntities);
}
if (model.getMetaDataSets().size() > 0) {
metaDataSetService.saveBatch(model.getMetaDataSets());
}
if (model.getNesteds().size() > 0) {
metaRelationshipService.saveBatch(model.getNesteds());
}
if (model.getReferences().size() > 0) {
metaRelationshipService.saveBatch(model.getReferences());
}
if (model.getMetaFields().size() > 0) {
metaFieldService.saveBatch(model.getMetaFields());
if(entities.size()>0){
metaEntityService.saveBatch(entities);
}
if(p_relations.size()>0){
relationshipService.saveBatch(p_relations);
}
if(s_relations.size()>0){
relationshipService.saveBatch(s_relations);
}
if(dataSets.size()>0){
dataSetService.saveBatch(dataSets);
}
if(fields.size()>0){
fieldService.saveBatch(fields);
}
}
//核心数据导入
if (model.getMetaModel() != null) {
metaModelService.save(model.getMetaModel());
}
......@@ -179,9 +214,10 @@ public class MetaModelExService extends MetaModelServiceImpl {
models.add(getModel(metaModel));
}
String strModel = JSON.toJSONString(models);
File modelFile = GenerateModelFile(strModel);
File modelFile = generateModelFile(strModel);
HttpServletResponse resp = ((ServletRequestAttributes) req).getResponse();
resp.setHeader("Content-Disposition", "attachment;filename=" + getFileName(modelFile.getName()));
if (resp != null)
resp.setHeader("Content-Disposition", "attachment;filename=" + getFileName(modelFile.getName()));
sendResponse(resp, modelFile);
return true;
}
......@@ -198,15 +234,8 @@ public class MetaModelExService extends MetaModelServiceImpl {
AssembleModel models = new AssembleModel();
List<DAMetric> metrics = new ArrayList<>();
LinkedHashSet<String> entities = new LinkedHashSet<>();
LinkedHashSet<MetaDataSet> metaDataSets = new LinkedHashSet<>();
LinkedHashSet<MetaRelationship> references = new LinkedHashSet<>();
LinkedHashSet<MetaRelationship> nesteds = new LinkedHashSet<>();
LinkedHashSet<MetaField> metaFields = new LinkedHashSet<>();
// 查询模型
MetaModel metaModel = metaModelService.get(et.getId());
models.setMetaModel(metaModel);
// 查询模型对应的N个规则
for (RuleItem ruleItem : metaModel.getRuleitems()) {
......@@ -247,11 +276,8 @@ public class MetaModelExService extends MetaModelServiceImpl {
}
}
// 将关联的数据集,上下级关系,字段一并存入
models.setEntityModels(getEntityModel(entities, modelId, metaDataSets, references, nesteds, metaFields));
models.setMetaDataSets(metaDataSets);
models.setReferences(references);
models.setNesteds(nesteds);
models.setMetaFields(metaFields);
models.setEntityModels(getEntityModel(modelId));
models.setMetaModel(metaModel);
models.setRuleItems(metaModel.getRuleitems());
models.setDaBuilds(metaModel.getBuilds());
models.setMetrics(metrics);
......@@ -260,34 +286,21 @@ public class MetaModelExService extends MetaModelServiceImpl {
/**
* 传入一组实体模型名和模型id来返回实体模型集合
*
* @param entities 实体模型名组
* @param modelId 模型id
* @return 实体模型集合
*/
private List<EntityModel> getEntityModel(LinkedHashSet<String> entities, String modelId, LinkedHashSet<MetaDataSet> metaDataSets, LinkedHashSet<MetaRelationship> references, LinkedHashSet<MetaRelationship> nesteds, LinkedHashSet<MetaField> metaFields) {
private List<EntityModel> getEntityModel(String modelId) {
List<EntityModel> entityModels = new ArrayList<>();
DataModel model = liteModelService.getDataModel(modelId);
if (model != null) {
for (String entity : entities) {
metaDataSets.addAll(metaDataSetService.selectByEntityId(entity));
references.addAll(metaRelationshipService.selectByEntityId(entity));
nesteds.addAll(metaRelationshipService.selectByRefEntityId(entity));
EntityModel entityModel = null;
Property subModel = model.getObjectProperty(entity);
if (subModel != null) {
entityModel = subModel.getEntityModel();
metaFields.addAll(metaFieldService.selectByEntityId(entityModel.getEntityId()));
} else {
if (model.getNestedDataModel(entity) != null) {
entityModel = model.getNestedDataModel(entity).getFactEntityModel();
}
}
if (entityModel != null) {
entityModels.add(entityModel);
}
}
DataModel dataModel = liteModelService.getDataModel(modelId);
List<Property> parents = dataModel.getParentProperty();
LinkedHashSet<DataModel> childs = dataModel.getNestedDataModels();
for(Property parent : parents){
EntityModel model = parent.getEntityModel();
entityModels.add(model);
}
for(DataModel child : childs){
EntityModel model = child.getFactEntityModel();
entityModels.add(model);
}
return entityModels;
}
......@@ -300,7 +313,8 @@ public class MetaModelExService extends MetaModelServiceImpl {
*/
protected String getFileName(String fileName) {
try {
return new String(fileName.getBytes("utf-8"), "iso8859-1");//防止中文乱码
//防止中文乱码
return new String(fileName.getBytes("utf-8"), "iso8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
......@@ -313,7 +327,7 @@ public class MetaModelExService extends MetaModelServiceImpl {
* @param strModel
* @return
*/
public File GenerateModelFile(String strModel) {
public File generateModelFile(String strModel) {
File file;
Date now = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
......@@ -366,8 +380,8 @@ public class MetaModelExService extends MetaModelServiceImpl {
} else {
throw new InternalServerErrorException("文件未找到");
}
Reader reader = new InputStreamReader(new FileInputStream(ModelFile), "utf-8");
int ch = 0;
Reader reader = new InputStreamReader(new FileInputStream(ModelFile), StandardCharsets.UTF_8);
int ch;
while ((ch = reader.read()) != -1) {
strModel.append((char) ch);
}
......@@ -415,5 +429,4 @@ public class MetaModelExService extends MetaModelServiceImpl {
}
}
}
}
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;
......@@ -27,4 +28,6 @@ public class RelationshipModel {
private String entityId;
private MetaRelationship relation;
}
package cn.ibizlab.core.lite.extensions.service;
import cn.ibizlab.core.lite.domain.*;
import cn.ibizlab.core.extensions.domain.AssembleModel;
import cn.ibizlab.core.extensions.service.MetaModelExService;
import cn.ibizlab.core.lite.domain.DstApp;
import cn.ibizlab.core.lite.domain.DstSystem;
import cn.ibizlab.core.lite.domain.MetaEntity;
import cn.ibizlab.core.lite.domain.MetaModel;
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.model.DataModel;
import cn.ibizlab.core.lite.extensions.util.LiteStorage;
import cn.ibizlab.core.lite.filter.MetaEntitySearchContext;
import cn.ibizlab.core.lite.filter.MetaFieldSearchContext;
import cn.ibizlab.core.lite.service.*;
import cn.ibizlab.util.domain.FileItem;
import cn.ibizlab.util.errors.BadRequestAlertException;
import cn.ibizlab.util.errors.InternalServerErrorException;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.context.annotation.Lazy;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.PostConstruct;
import java.sql.Wrapper;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.io.*;
import java.nio.file.Files;
import java.util.*;
import static cn.ibizlab.util.service.SimpleFileService.getExtensionName;
@Service
public class LiteModelService {
......@@ -91,7 +99,7 @@ public class LiteModelService {
EntityModel entityModel = new EntityModel();
entityModel.setEntity(entity);
entityModel.setDataSets(metaDataSetService.selectByEntityId(entity.getEntityId()));
entityModel.setDataSets(metaDataSetService.selectByEntityId(entity != null ? entity.getEntityId() : null));
Map<String, RelationshipModel> parentSet = new LinkedHashMap();
List<RelationshipModel> references = new ArrayList<>();
......@@ -100,6 +108,7 @@ public class LiteModelService {
RelationshipModel model = new RelationshipModel();
MetaEntity parentEntity = LiteStorage.getMetaEntity(item.getRefEntityId());
if(parentEntity!=null){
model.setRelation(item);
model.setCodeName(item.getCodeName());
model.setDataSourceName(parentEntity.getDsName());
model.setEntityCodeName(parentEntity.getCodeName());
......@@ -120,6 +129,7 @@ public class LiteModelService {
RelationshipModel model = new RelationshipModel();
MetaEntity subEntity = LiteStorage.getMetaEntity(item.getEntityId());
if(subEntity!=null){
model.setRelation(item);
model.setCodeName(StringUtils.isEmpty(item.getNestedName())?item.getCodeName()+"_"+item.getEntityName():item.getNestedName());
model.setDataSourceName(subEntity.getDsName());
model.setEntityCodeName(subEntity.getCodeName());
......@@ -195,6 +205,11 @@ public class LiteModelService {
@Lazy
private IDstSystemService dstSystemService;
@Value("${ibiz.filePath:/app/file/}")
private String fileRoot;
@Autowired
private MetaModelExService metaModelExService;
@Cacheable( value="syspssystem",key = "'row:all-dst-apps'")
public LinkedHashMap<String, DstApp> getApps() {
......@@ -247,4 +262,103 @@ public class LiteModelService {
return list;
}
public FileItem uploadFile(MultipartFile multipartFile) {
FileItem item = null;
// 获取文件名
String fileName = multipartFile.getOriginalFilename();
// 获取文件后缀
String extname = "."+getExtensionName(fileName);
try {
String fileid = DigestUtils.md5DigestAsHex(multipartFile.getInputStream());
String fileFullPath = this.fileRoot+"ibizutil"+File.separator+File.separator+fileName;
File file = new File(fileFullPath);
File parent = new File(file.getParent());
if(!parent.exists()) {
parent.mkdirs();
}
FileCopyUtils.copy(multipartFile.getInputStream(), Files.newOutputStream(file.toPath()));
item = new FileItem(fileid, fileName, fileid, fileName, (int)multipartFile.getSize(), extname);
} catch (IOException e) {
throw new InternalServerErrorException("文件上传失败,"+e);
}
return item;
}
public String saveFile(String jsonObject) {
// 获取文件名
String fileName = "packageModel_"+UUID.randomUUID().toString().replace("-","").toUpperCase();
// 获取文件后缀
String extname = ".json";
try {
String fileFullPath = this.fileRoot+"ibizutil"+File.separator+fileName+extname;
File file = new File(fileFullPath);
File parent = new File(file.getParent());
if(!parent.exists()) {
parent.mkdirs();
}
writeFile(fileFullPath,jsonObject);
} catch (Exception e) {
throw new InternalServerErrorException("文件上传失败,"+e);
}
return fileName+extname;
}
public File getFile(String fileId) {
String dirpath = this.fileRoot + "ibizutil" + File.separator + fileId;
File parent = new File(dirpath);
if (parent.exists()) {
return parent;
}
throw new InternalServerErrorException("文件未找到");
}
public static void writeFile(String filePath, String sets) throws IOException {
FileWriter fw = new FileWriter(filePath);
PrintWriter out = new PrintWriter(fw);
out.write(sets);
out.println();
fw.close();
out.close();
}
public String exportFile(List<MetaModel> model){
List<AssembleModel> temp = new ArrayList<>();
for (MetaModel metaModel : model) {
temp.add(null);
}
String json = JSON.toJSONString(temp);
String fileName = this.saveFile(json);
return fileName;
}
public List<AssembleModel> translateFile(MultipartFile file){
BufferedReader reader = null;
String ans = "";
try{
Reader read = new InputStreamReader(file.getInputStream(), "utf-8");
reader = new BufferedReader(read);
String tmpString = null;
while((tmpString = reader.readLine()) != null){
ans += tmpString ;
}
if(StringUtils.isEmpty(ans)){
throw new BadRequestAlertException("文件内容为空","","");
}
List<AssembleModel> assembleModel = JSONObject.parseArray(ans, AssembleModel.class);
return assembleModel;
}catch (Exception e){
return null;
}finally{
if(reader != null){
try{
reader.close();
}catch(IOException ee){
ee.printStackTrace();
}
}
}
}
}
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册