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.*;
import
cn.ibizlab.core.lite.service.impl.MetaModelServiceImpl
;
import
cn.ibizlab.core.rule.domain.RuleItem
;
import
cn.ibizlab.core.rule.service.IRuleItemService
;
import
cn.ibizlab.util.errors.BadRequestAlertException
;
import
cn.ibizlab.util.errors.InternalServerErrorException
;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.JSONArray
;
...
...
@@ -30,12 +31,14 @@ import org.springframework.util.DigestUtils;
import
org.springframework.util.ObjectUtils
;
import
org.springframework.web.context.request.RequestContextHolder
;
import
org.springframework.web.context.request.ServletRequestAttributes
;
import
javax.annotation.Resource
;
import
javax.servlet.http.HttpServletResponse
;
import
java.io.*
;
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 {
@Override
@Transactional
public
MetaModel
importFile
(
MetaModel
et
)
{
JSONArray
contentArray
=
JSONArray
.
parseArray
(
et
.
getName
());
// 上传数据取最新一条
for
(
Object
s
:
contentArray
)
{
String
json
=
s
.
toString
();
Map
etMaps
=
(
Map
)
JSON
.
parse
(
json
);
File
file
=
getUploadFile
(
etMaps
.
get
(
"id"
).
toString
());
List
<
AssembleModel
>
assembleModels
=
readJsonFile
(
file
.
toString
());
importFile
(
assembleModels
);
JSONArray
fileArray
=
JSONArray
.
parseArray
(
et
.
getName
());
for
(
int
a
=
0
;
a
<
fileArray
.
size
();
a
++)
{
JSONObject
objFile
=
fileArray
.
getJSONObject
(
a
);
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
<>();
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
);
}
/**
* 批量导出数据到json文件中
*
* @param metaModels
* @return
*/
@Override
@Transactional
public
boolean
exportFileBatch
(
List
<
MetaModel
>
metaModels
)
{
HttpServletResponse
response
=
((
ServletRequestAttributes
)
RequestContextHolder
.
getRequestAttributes
()).
getResponse
();
String
filePath
=
getAssembleString
(
metaModels
);
File
file
=
getDownLoadFile
(
filePath
);
// 将请求头加上Content-Disposition,浏览器会自动识别为文件,并将其下载下来
response
.
setHeader
(
"Content-Disposition"
,
"attachment;filename="
+
getFileName
(
file
.
getName
()));
sendResponse
(
response
,
file
);
Object
req
=
RequestContextHolder
.
getRequestAttributes
();
if
(
ObjectUtils
.
isEmpty
(
req
)
||
!(
req
instanceof
ServletRequestAttributes
))
{
new
BadRequestAlertException
(
"模型导出失败"
,
""
,
""
);
}
List
<
AssembleModel
>
models
=
new
ArrayList
<>();
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
;
}
...
...
@@ -151,7 +193,7 @@ public class MetaModelExService extends MetaModelServiceImpl {
* @return
*/
@Transactional
public
AssembleModel
assemble
(
MetaModel
et
)
{
public
AssembleModel
getModel
(
MetaModel
et
)
{
String
modelId
=
et
.
getId
();
AssembleModel
models
=
new
AssembleModel
();
List
<
DAMetric
>
metrics
=
new
ArrayList
<>();
...
...
@@ -162,7 +204,6 @@ public class MetaModelExService extends MetaModelServiceImpl {
LinkedHashSet
<
MetaRelationship
>
nesteds
=
new
LinkedHashSet
<>();
LinkedHashSet
<
MetaField
>
metaFields
=
new
LinkedHashSet
<>();
// 查询模型
MetaModel
metaModel
=
metaModelService
.
get
(
et
.
getId
());
models
.
setMetaModel
(
metaModel
);
...
...
@@ -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
.
setReferences
(
references
);
models
.
setNesteds
(
nesteds
);
...
...
@@ -224,7 +265,7 @@ public class MetaModelExService extends MetaModelServiceImpl {
* @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
(
LinkedHashSet
<
String
>
entities
,
String
modelId
,
LinkedHashSet
<
MetaDataSet
>
metaDataSets
,
LinkedHashSet
<
MetaRelationship
>
references
,
LinkedHashSet
<
MetaRelationship
>
nesteds
,
LinkedHashSet
<
MetaField
>
metaFields
)
{
List
<
EntityModel
>
entityModels
=
new
ArrayList
<>();
DataModel
model
=
liteModelService
.
getDataModel
(
modelId
);
if
(
model
!=
null
)
{
...
...
@@ -251,51 +292,9 @@ public class MetaModelExService extends MetaModelServiceImpl {
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
* @return
*/
...
...
@@ -311,91 +310,78 @@ public class MetaModelExService extends MetaModelServiceImpl {
/**
* 保存文件在本地后返回一个文件名
*
* @param
jsonObject
* @param
strModel
* @return
*/
public
String
saveFile
(
String
jsonObject
)
{
Date
nowDate
=
new
Date
();
SimpleDateFormat
df
=
new
SimpleDateFormat
(
"yyyyMMddHHmmss"
);
String
fileDir
=
DigestUtils
.
md5DigestAsHex
(
UUID
.
randomUUID
().
toString
().
getBytes
());
String
fileName
=
"Model_"
+
df
.
format
(
nowDate
);
String
extname
=
".json"
;
public
File
GenerateModelFile
(
String
strModel
)
{
File
file
;
Date
now
=
new
Date
();
SimpleDateFormat
format
=
new
SimpleDateFormat
(
"yyyyMMddHHmmss"
);
String
fileId
=
DigestUtils
.
md5DigestAsHex
(
strModel
.
getBytes
());
String
fileName
=
format
.
format
(
now
)
+
".json"
;
FileWriter
fw
=
null
;
PrintWriter
out
=
null
;
try
{
String
fileFullPath
=
this
.
fileRoot
+
"ibizutil"
+
File
.
separator
+
fileDir
+
File
.
separator
+
fileName
+
extn
ame
;
File
file
=
new
File
(
fileFullPath
);
String
fileFullPath
=
this
.
fileRoot
+
"ibizutil"
+
File
.
separator
+
fileId
+
File
.
separator
+
fileN
ame
;
file
=
new
File
(
fileFullPath
);
File
parent
=
new
File
(
file
.
getParent
());
if
(!
parent
.
exists
())
{
parent
.
mkdirs
();
}
writeFile
(
fileFullPath
,
jsonObject
);
fw
=
new
FileWriter
(
fileFullPath
);
out
=
new
PrintWriter
(
fw
);
out
.
write
(
strModel
);
out
.
println
();
}
catch
(
Exception
e
)
{
throw
new
InternalServerErrorException
(
"文件上传失败,"
+
e
);
}
return
fileDir
+
File
.
separator
+
fileName
+
extname
;
}
/**
* 获取文件,导出时候使用
* @param fileId
* @return
*/
public
File
getDownLoadFile
(
String
fileId
)
{
String
dirPath
=
this
.
fileRoot
+
"ibizutil"
+
File
.
separator
+
fileId
;
File
parent
=
new
File
(
dirPath
);
if
(
parent
.
exists
())
{
return
parent
;
throw
new
BadRequestAlertException
(
"生成模型文件失败,"
+
e
,
""
,
""
);
}
finally
{
if
(
fw
!=
null
)
{
try
{
fw
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
if
(
out
!=
null
)
{
out
.
close
();
}
}
throw
new
InternalServerErrorException
(
"文件未找到"
)
;
return
file
;
}
/**
* 获取上传文件路径
* 去磁盘读取文件,并将其转化成组装模型对象
*
* @param fileId
* @return
*/
public
File
getUploadFile
(
String
fileId
)
{
String
dirPath
=
this
.
fileRoot
+
"ibizutil"
+
File
.
separator
+
fileId
;
File
parent
=
new
File
(
dirPath
);
if
(
parent
.
exists
()
&&
parent
.
isDirectory
()
&&
parent
.
listFiles
().
length
>
0
)
{
return
parent
.
listFiles
()[
0
];
}
throw
new
InternalServerErrorException
(
"文件未找到"
);
}
/**
* 将流写入文件
* @param filePath
* @param sets
* @throws IOException
*/
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
getModel
(
String
fileId
)
{
File
ModelFile
;
StringBuffer
strModel
=
new
StringBuffer
();
try
{
String
dirPath
=
this
.
fileRoot
+
"ibizutil"
+
File
.
separator
+
fileId
;
File
parent
=
new
File
(
dirPath
);
if
(
parent
.
exists
()
&&
parent
.
isDirectory
()
&&
parent
.
listFiles
().
length
>
0
)
{
ModelFile
=
parent
.
listFiles
()[
0
];
}
else
{
throw
new
InternalServerErrorException
(
"文件未找到"
);
}
Reader
reader
=
new
InputStreamReader
(
new
FileInputStream
(
ModelFile
),
"utf-8"
);
int
ch
=
0
;
while
((
ch
=
reader
.
read
())
!=
-
1
)
{
strModel
.
append
((
char
)
ch
);
}
reader
.
close
();
/**
* 将文件读取的模型拼装成json,保存到磁盘
*
* @param model
* @return
*/
public
String
getAssembleString
(
List
<
MetaModel
>
model
)
{
List
<
AssembleModel
>
temp
=
new
ArrayList
<>();
for
(
MetaModel
metaModel
:
model
)
{
temp
.
add
(
assemble
(
metaModel
));
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
String
json
=
JSON
.
toJSONString
(
temp
);
String
fileName
=
this
.
saveFile
(
json
);
return
fileName
;
return
strModel
.
toString
();
}
/**
* 发送文件响应给浏览器
*
* @param response
* @param file
*/
...
...
@@ -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
人
到此讨论。请谨慎行事。
先完成此消息的编辑!
取消
想要评论请
注册
或
登录