Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
提交反馈
为 GitLab 提交贡献
登录
切换导航
I
ibzdst
项目
项目
详情
动态
版本
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
议题
1
议题
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
ibiz4jteam
ibzdst
提交
156edbc8
提交
156edbc8
编写于
1月 04, 2021
作者:
zhouweidong
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
模型导入、导出逻辑优化
上级
f926479f
变更
1
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
113 行增加
和
153 行删除
+113
-153
MetaModelExService.java
...n/ibizlab/core/extensions/service/MetaModelExService.java
+113
-153
未找到文件。
ibzdst-core/src/main/java/cn/ibizlab/core/extensions/service/MetaModelExService.java
浏览文件 @
156edbc8
...
@@ -15,6 +15,7 @@ import cn.ibizlab.core.lite.service.*;
...
@@ -15,6 +15,7 @@ import cn.ibizlab.core.lite.service.*;
import
cn.ibizlab.core.lite.service.impl.MetaModelServiceImpl
;
import
cn.ibizlab.core.lite.service.impl.MetaModelServiceImpl
;
import
cn.ibizlab.core.rule.domain.RuleItem
;
import
cn.ibizlab.core.rule.domain.RuleItem
;
import
cn.ibizlab.core.rule.service.IRuleItemService
;
import
cn.ibizlab.core.rule.service.IRuleItemService
;
import
cn.ibizlab.util.errors.BadRequestAlertException
;
import
cn.ibizlab.util.errors.InternalServerErrorException
;
import
cn.ibizlab.util.errors.InternalServerErrorException
;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.JSONArray
;
import
com.alibaba.fastjson.JSONArray
;
...
@@ -30,12 +31,14 @@ import org.springframework.util.DigestUtils;
...
@@ -30,12 +31,14 @@ import org.springframework.util.DigestUtils;
import
org.springframework.util.ObjectUtils
;
import
org.springframework.util.ObjectUtils
;
import
org.springframework.web.context.request.RequestContextHolder
;
import
org.springframework.web.context.request.RequestContextHolder
;
import
org.springframework.web.context.request.ServletRequestAttributes
;
import
org.springframework.web.context.request.ServletRequestAttributes
;
import
javax.annotation.Resource
;
import
javax.annotation.Resource
;
import
javax.servlet.http.HttpServletResponse
;
import
javax.servlet.http.HttpServletResponse
;
import
java.io.*
;
import
java.io.*
;
import
java.text.SimpleDateFormat
;
import
java.text.SimpleDateFormat
;
import
java.util.*
;
import
java.util.ArrayList
;
import
java.util.Date
;
import
java.util.LinkedHashSet
;
import
java.util.List
;
/**
/**
* 实体[模型] 自定义服务对象
* 实体[模型] 自定义服务对象
...
@@ -115,32 +118,71 @@ public class MetaModelExService extends MetaModelServiceImpl {
...
@@ -115,32 +118,71 @@ public class MetaModelExService extends MetaModelServiceImpl {
@Override
@Override
@Transactional
@Transactional
public
MetaModel
importFile
(
MetaModel
et
)
{
public
MetaModel
importFile
(
MetaModel
et
)
{
JSONArray
contentArray
=
JSONArray
.
parseArray
(
et
.
getName
());
JSONArray
fileArray
=
JSONArray
.
parseArray
(
et
.
getName
());
// 上传数据取最新一条
for
(
int
a
=
0
;
a
<
fileArray
.
size
();
a
++)
{
for
(
Object
s
:
contentArray
)
{
JSONObject
objFile
=
fileArray
.
getJSONObject
(
a
);
String
json
=
s
.
toString
();
String
strModel
=
getModel
(
objFile
.
getString
(
"id"
));
Map
etMaps
=
(
Map
)
JSON
.
parse
(
json
);
List
<
AssembleModel
>
models
=
JSONObject
.
parseArray
(
strModel
,
AssembleModel
.
class
);
File
file
=
getUploadFile
(
etMaps
.
get
(
"id"
).
toString
());
for
(
AssembleModel
model
:
models
)
{
List
<
AssembleModel
>
assembleModels
=
readJsonFile
(
file
.
toString
());
// 前置铺垫实体模型,防止外键冲突
importFile
(
assembleModels
);
if
(
model
.
getEntityModels
()
!=
null
)
{
LinkedHashSet
<
MetaEntity
>
metaEntities
=
new
LinkedHashSet
<>();
model
.
getEntityModels
().
forEach
(
item
->
{
metaEntities
.
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
(
model
.
getMetaModel
()
!=
null
)
{
metaModelService
.
save
(
model
.
getMetaModel
());
}
if
(
model
.
getDaBuilds
().
size
()
>
0
)
{
idaBuildService
.
saveBatch
(
model
.
getDaBuilds
());
}
if
(
model
.
getMetrics
().
size
()
>
0
)
{
idaMetricService
.
saveBatch
(
model
.
getMetrics
());
}
if
(
model
.
getRuleItems
().
size
()
>
0
)
{
ruleItemService
.
saveBatch
(
model
.
getRuleItems
());
}
}
}
}
return
super
.
importFile
(
et
);
return
super
.
importFile
(
et
);
}
}
/**
/**
* 批量导出数据到json文件中
* 批量导出数据到json文件中
*
* @param metaModels
* @param metaModels
* @return
* @return
*/
*/
@Override
@Override
@Transactional
@Transactional
public
boolean
exportFileBatch
(
List
<
MetaModel
>
metaModels
)
{
public
boolean
exportFileBatch
(
List
<
MetaModel
>
metaModels
)
{
HttpServletResponse
response
=
((
ServletRequestAttributes
)
RequestContextHolder
.
getRequestAttributes
()).
getResponse
();
Object
req
=
RequestContextHolder
.
getRequestAttributes
();
String
filePath
=
getAssembleString
(
metaModels
);
if
(
ObjectUtils
.
isEmpty
(
req
)
||
!(
req
instanceof
ServletRequestAttributes
))
{
File
file
=
getDownLoadFile
(
filePath
);
new
BadRequestAlertException
(
"模型导出失败"
,
""
,
""
);
// 将请求头加上Content-Disposition,浏览器会自动识别为文件,并将其下载下来
}
response
.
setHeader
(
"Content-Disposition"
,
"attachment;filename="
+
getFileName
(
file
.
getName
()));
List
<
AssembleModel
>
models
=
new
ArrayList
<>();
sendResponse
(
response
,
file
);
for
(
MetaModel
metaModel
:
metaModels
)
{
models
.
add
(
getModel
(
metaModel
));
}
String
strModel
=
JSON
.
toJSONString
(
models
);
File
modelFile
=
GenerateModelFile
(
strModel
);
HttpServletResponse
resp
=
((
ServletRequestAttributes
)
req
).
getResponse
();
resp
.
setHeader
(
"Content-Disposition"
,
"attachment;filename="
+
getFileName
(
modelFile
.
getName
()));
sendResponse
(
resp
,
modelFile
);
return
true
;
return
true
;
}
}
...
@@ -151,7 +193,7 @@ public class MetaModelExService extends MetaModelServiceImpl {
...
@@ -151,7 +193,7 @@ public class MetaModelExService extends MetaModelServiceImpl {
* @return
* @return
*/
*/
@Transactional
@Transactional
public
AssembleModel
assemble
(
MetaModel
et
)
{
public
AssembleModel
getModel
(
MetaModel
et
)
{
String
modelId
=
et
.
getId
();
String
modelId
=
et
.
getId
();
AssembleModel
models
=
new
AssembleModel
();
AssembleModel
models
=
new
AssembleModel
();
List
<
DAMetric
>
metrics
=
new
ArrayList
<>();
List
<
DAMetric
>
metrics
=
new
ArrayList
<>();
...
@@ -162,7 +204,6 @@ public class MetaModelExService extends MetaModelServiceImpl {
...
@@ -162,7 +204,6 @@ public class MetaModelExService extends MetaModelServiceImpl {
LinkedHashSet
<
MetaRelationship
>
nesteds
=
new
LinkedHashSet
<>();
LinkedHashSet
<
MetaRelationship
>
nesteds
=
new
LinkedHashSet
<>();
LinkedHashSet
<
MetaField
>
metaFields
=
new
LinkedHashSet
<>();
LinkedHashSet
<
MetaField
>
metaFields
=
new
LinkedHashSet
<>();
// 查询模型
// 查询模型
MetaModel
metaModel
=
metaModelService
.
get
(
et
.
getId
());
MetaModel
metaModel
=
metaModelService
.
get
(
et
.
getId
());
models
.
setMetaModel
(
metaModel
);
models
.
setMetaModel
(
metaModel
);
...
@@ -206,7 +247,7 @@ public class MetaModelExService extends MetaModelServiceImpl {
...
@@ -206,7 +247,7 @@ public class MetaModelExService extends MetaModelServiceImpl {
}
}
}
}
// 将关联的数据集,上下级关系,字段一并存入
// 将关联的数据集,上下级关系,字段一并存入
models
.
setEntityModels
(
getEntityModel
(
entities
,
modelId
,
metaDataSets
,
references
,
nesteds
,
metaFields
));
models
.
setEntityModels
(
getEntityModel
(
entities
,
modelId
,
metaDataSets
,
references
,
nesteds
,
metaFields
));
models
.
setMetaDataSets
(
metaDataSets
);
models
.
setMetaDataSets
(
metaDataSets
);
models
.
setReferences
(
references
);
models
.
setReferences
(
references
);
models
.
setNesteds
(
nesteds
);
models
.
setNesteds
(
nesteds
);
...
@@ -224,7 +265,7 @@ public class MetaModelExService extends MetaModelServiceImpl {
...
@@ -224,7 +265,7 @@ public class MetaModelExService extends MetaModelServiceImpl {
* @param modelId 模型id
* @param modelId 模型id
* @return 实体模型集合
* @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
(
LinkedHashSet
<
String
>
entities
,
String
modelId
,
LinkedHashSet
<
MetaDataSet
>
metaDataSets
,
LinkedHashSet
<
MetaRelationship
>
references
,
LinkedHashSet
<
MetaRelationship
>
nesteds
,
LinkedHashSet
<
MetaField
>
metaFields
)
{
List
<
EntityModel
>
entityModels
=
new
ArrayList
<>();
List
<
EntityModel
>
entityModels
=
new
ArrayList
<>();
DataModel
model
=
liteModelService
.
getDataModel
(
modelId
);
DataModel
model
=
liteModelService
.
getDataModel
(
modelId
);
if
(
model
!=
null
)
{
if
(
model
!=
null
)
{
...
@@ -251,51 +292,9 @@ public class MetaModelExService extends MetaModelServiceImpl {
...
@@ -251,51 +292,9 @@ public class MetaModelExService extends MetaModelServiceImpl {
return
entityModels
;
return
entityModels
;
}
}
/**
* 根据组装模型包集合来对数据进行导入
*
* @param assembleModels 组装模型包
*/
@Transactional
public
void
importFile
(
List
<
AssembleModel
>
assembleModels
)
{
for
(
AssembleModel
assembleModel
:
assembleModels
)
{
// 前置铺垫实体模型,防止外键冲突
if
(
assembleModel
.
getEntityModels
()
!=
null
)
{
LinkedHashSet
<
MetaEntity
>
metaEntities
=
new
LinkedHashSet
<>();
assembleModel
.
getEntityModels
().
forEach
(
item
->{
metaEntities
.
add
(
item
.
getEntity
());
});
metaEntityService
.
saveBatch
(
metaEntities
);
}
if
(
assembleModel
.
getMetaDataSets
().
size
()
>
0
)
{
metaDataSetService
.
saveBatch
(
assembleModel
.
getMetaDataSets
());
}
if
(
assembleModel
.
getNesteds
().
size
()
>
0
)
{
metaRelationshipService
.
saveBatch
(
assembleModel
.
getNesteds
());
}
if
(
assembleModel
.
getReferences
().
size
()
>
0
)
{
metaRelationshipService
.
saveBatch
(
assembleModel
.
getReferences
());
}
if
(
assembleModel
.
getMetaFields
().
size
()
>
0
)
{
metaFieldService
.
saveBatch
(
assembleModel
.
getMetaFields
());
}
if
(
assembleModel
.
getMetaModel
()
!=
null
)
{
metaModelService
.
save
(
assembleModel
.
getMetaModel
());
}
if
(
assembleModel
.
getDaBuilds
().
size
()
>
0
)
{
idaBuildService
.
saveBatch
(
assembleModel
.
getDaBuilds
());
}
if
(
assembleModel
.
getMetrics
().
size
()
>
0
)
{
idaMetricService
.
saveBatch
(
assembleModel
.
getMetrics
());
}
if
(
assembleModel
.
getRuleItems
().
size
()
>
0
)
{
ruleItemService
.
saveBatch
(
assembleModel
.
getRuleItems
());
}
}
}
/**
/**
* 格式化文件名给请求识别
* 格式化文件名给请求识别
*
* @param fileName
* @param fileName
* @return
* @return
*/
*/
...
@@ -311,91 +310,78 @@ public class MetaModelExService extends MetaModelServiceImpl {
...
@@ -311,91 +310,78 @@ public class MetaModelExService extends MetaModelServiceImpl {
/**
/**
* 保存文件在本地后返回一个文件名
* 保存文件在本地后返回一个文件名
*
*
* @param
jsonObject
* @param
strModel
* @return
* @return
*/
*/
public
String
saveFile
(
String
jsonObject
)
{
public
File
GenerateModelFile
(
String
strModel
)
{
Date
nowDate
=
new
Date
();
File
file
;
SimpleDateFormat
df
=
new
SimpleDateFormat
(
"yyyyMMddHHmmss"
);
Date
now
=
new
Date
();
String
fileDir
=
DigestUtils
.
md5DigestAsHex
(
UUID
.
randomUUID
().
toString
().
getBytes
());
SimpleDateFormat
format
=
new
SimpleDateFormat
(
"yyyyMMddHHmmss"
);
String
fileId
=
DigestUtils
.
md5DigestAsHex
(
strModel
.
getBytes
());
String
fileName
=
"Model_"
+
df
.
format
(
nowDate
);
String
fileName
=
format
.
format
(
now
)
+
".json"
;
String
extname
=
".json"
;
FileWriter
fw
=
null
;
PrintWriter
out
=
null
;
try
{
try
{
String
fileFullPath
=
this
.
fileRoot
+
"ibizutil"
+
File
.
separator
+
fileDir
+
File
.
separator
+
fileName
+
extn
ame
;
String
fileFullPath
=
this
.
fileRoot
+
"ibizutil"
+
File
.
separator
+
fileId
+
File
.
separator
+
fileN
ame
;
File
file
=
new
File
(
fileFullPath
);
file
=
new
File
(
fileFullPath
);
File
parent
=
new
File
(
file
.
getParent
());
File
parent
=
new
File
(
file
.
getParent
());
if
(!
parent
.
exists
())
{
if
(!
parent
.
exists
())
{
parent
.
mkdirs
();
parent
.
mkdirs
();
}
}
writeFile
(
fileFullPath
,
jsonObject
);
fw
=
new
FileWriter
(
fileFullPath
);
out
=
new
PrintWriter
(
fw
);
out
.
write
(
strModel
);
out
.
println
();
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
throw
new
InternalServerErrorException
(
"文件上传失败,"
+
e
);
throw
new
BadRequestAlertException
(
"生成模型文件失败,"
+
e
,
""
,
""
);
}
}
finally
{
return
fileDir
+
File
.
separator
+
fileName
+
extname
;
if
(
fw
!=
null
)
{
}
try
{
fw
.
close
();
/**
}
catch
(
IOException
e
)
{
* 获取文件,导出时候使用
e
.
printStackTrace
();
* @param fileId
}
* @return
}
*/
if
(
out
!=
null
)
{
public
File
getDownLoadFile
(
String
fileId
)
{
out
.
close
();
String
dirPath
=
this
.
fileRoot
+
"ibizutil"
+
File
.
separator
+
fileId
;
}
File
parent
=
new
File
(
dirPath
);
if
(
parent
.
exists
())
{
return
parent
;
}
}
throw
new
InternalServerErrorException
(
"文件未找到"
)
;
return
file
;
}
}
/**
/**
* 获取上传文件路径
* 去磁盘读取文件,并将其转化成组装模型对象
*
* @param fileId
* @param fileId
* @return
* @return
*/
*/
public
File
getUploadFile
(
String
fileId
)
{
public
String
getModel
(
String
fileId
)
{
String
dirPath
=
this
.
fileRoot
+
"ibizutil"
+
File
.
separator
+
fileId
;
File
ModelFile
;
File
parent
=
new
File
(
dirPath
);
StringBuffer
strModel
=
new
StringBuffer
();
if
(
parent
.
exists
()
&&
parent
.
isDirectory
()
&&
parent
.
listFiles
().
length
>
0
)
{
try
{
return
parent
.
listFiles
()[
0
];
String
dirPath
=
this
.
fileRoot
+
"ibizutil"
+
File
.
separator
+
fileId
;
}
File
parent
=
new
File
(
dirPath
);
throw
new
InternalServerErrorException
(
"文件未找到"
);
if
(
parent
.
exists
()
&&
parent
.
isDirectory
()
&&
parent
.
listFiles
().
length
>
0
)
{
}
ModelFile
=
parent
.
listFiles
()[
0
];
}
else
{
/**
throw
new
InternalServerErrorException
(
"文件未找到"
);
* 将流写入文件
}
* @param filePath
Reader
reader
=
new
InputStreamReader
(
new
FileInputStream
(
ModelFile
),
"utf-8"
);
* @param sets
int
ch
=
0
;
* @throws IOException
while
((
ch
=
reader
.
read
())
!=
-
1
)
{
*/
strModel
.
append
((
char
)
ch
);
public
static
void
writeFile
(
String
filePath
,
String
sets
)
throws
IOException
{
}
FileWriter
fw
=
new
FileWriter
(
filePath
);
reader
.
close
();
PrintWriter
out
=
new
PrintWriter
(
fw
);
out
.
write
(
sets
);
out
.
println
();
fw
.
close
();
out
.
close
();
}
/**
}
catch
(
IOException
e
)
{
* 将文件读取的模型拼装成json,保存到磁盘
e
.
printStackTrace
();
*
* @param model
* @return
*/
public
String
getAssembleString
(
List
<
MetaModel
>
model
)
{
List
<
AssembleModel
>
temp
=
new
ArrayList
<>();
for
(
MetaModel
metaModel
:
model
)
{
temp
.
add
(
assemble
(
metaModel
));
}
}
String
json
=
JSON
.
toJSONString
(
temp
);
return
strModel
.
toString
();
String
fileName
=
this
.
saveFile
(
json
);
return
fileName
;
}
}
/**
/**
* 发送文件响应给浏览器
* 发送文件响应给浏览器
*
* @param response
* @param response
* @param file
* @param file
*/
*/
...
@@ -430,30 +416,4 @@ public class MetaModelExService extends MetaModelServiceImpl {
...
@@ -430,30 +416,4 @@ public class MetaModelExService extends MetaModelServiceImpl {
}
}
}
}
/**
* 去磁盘读取文件,并将其转化成组装模型对象
* @param fileName
* @return
*/
public
static
List
<
AssembleModel
>
readJsonFile
(
String
fileName
)
{
String
jsonStr
=
""
;
try
{
File
jsonFile
=
new
File
(
fileName
);
FileReader
fileReader
=
new
FileReader
(
jsonFile
);
Reader
reader
=
new
InputStreamReader
(
new
FileInputStream
(
jsonFile
),
"utf-8"
);
int
ch
=
0
;
StringBuffer
sb
=
new
StringBuffer
();
while
((
ch
=
reader
.
read
())
!=
-
1
)
{
sb
.
append
((
char
)
ch
);
}
fileReader
.
close
();
reader
.
close
();
jsonStr
=
sb
.
toString
();
List
<
AssembleModel
>
assembleModel
=
JSONObject
.
parseArray
(
jsonStr
,
AssembleModel
.
class
);
return
assembleModel
;
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
return
null
;
}
}
}
}
编辑
预览
Markdown
格式
0%
请重试
or
添加新附件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
先完成此消息的编辑!
取消
想要评论请
注册
或
登录