提交 80e8a8d1 编写于 作者: zhouweidong's avatar zhouweidong

优化初始化模型逻辑

上级 4aa1949f
...@@ -45,8 +45,6 @@ public class DynamicModelConfigExService extends DynamicModelConfigServiceImpl { ...@@ -45,8 +45,6 @@ public class DynamicModelConfigExService extends DynamicModelConfigServiceImpl {
@Value("${ibiz.filePath:/app/file/}") @Value("${ibiz.filePath:/app/file/}")
private String fileRoot; private String fileRoot;
private final String workflowFilename = "PSWFVERSION.json.bpm";
@Autowired @Autowired
private FileService fileService; private FileService fileService;
...@@ -54,8 +52,8 @@ public class DynamicModelConfigExService extends DynamicModelConfigServiceImpl { ...@@ -54,8 +52,8 @@ public class DynamicModelConfigExService extends DynamicModelConfigServiceImpl {
@Lazy @Lazy
private IBZWFFeignClient wfClient; private IBZWFFeignClient wfClient;
@Value("${ibiz.dynamic.path:E:\\app\\}") @Value("${ibiz.dynamic.publicpath:/app/file/dynamicModel/publicpath}")
private String dynamicPath; private String publicPath;
@Autowired @Autowired
RedisTemplate<String, Object> redisOperations; RedisTemplate<String, Object> redisOperations;
...@@ -66,13 +64,6 @@ public class DynamicModelConfigExService extends DynamicModelConfigServiceImpl { ...@@ -66,13 +64,6 @@ public class DynamicModelConfigExService extends DynamicModelConfigServiceImpl {
return super.create(et); return super.create(et);
} }
@Override
public boolean update(DynamicModelConfig et) {
// unzip(et);
return super.update(et);
}
/** /**
* 将zip解压到file目录 * 将zip解压到file目录
* *
...@@ -82,27 +73,17 @@ public class DynamicModelConfigExService extends DynamicModelConfigServiceImpl { ...@@ -82,27 +73,17 @@ public class DynamicModelConfigExService extends DynamicModelConfigServiceImpl {
try { try {
String strModelFile = et.getModelfile(); String strModelFile = et.getModelfile();
if (StringUtils.isEmpty(strModelFile)) if (StringUtils.isEmpty(strModelFile))
return; throw new BadRequestAlertException("模型文件为空", "DynamicModelConfig", "unzip");
List<FileItem> items = JSONArray.parseArray(strModelFile, FileItem.class); List<FileItem> items = JSONArray.parseArray(strModelFile, FileItem.class);
if (items.size() == 0 || StaticDict.DynamicModelStatus.ITEM_1.getValue().equalsIgnoreCase(et.getStatus())) if (!ObjectUtils.isEmpty(items) && items.size() > 1)
return; throw new BadRequestAlertException("单次只允许上传一个模型文件", "DynamicModelConfig", "unzip");
JSONObject system =null; File modelFile = fileService.getFile(items.get(0).getId());
for (FileItem item : items) {
File modelFile = fileService.getFile(item.getId());
if (!modelFile.exists())
return;
String unzipPath = modelFile.getParent().replace("ibizutil", "dynamicmodel"); String unzipPath = modelFile.getParent().replace("ibizutil", "dynamicmodel");
boolean success = FileHelper.unzip(modelFile, unzipPath,true); FileHelper.unzip(modelFile, unzipPath, true);
if (success) { JSONObject system = getSystem(unzipPath);
system = getSystem(unzipPath);
}
}
if (ObjectUtils.isEmpty(system)) {
throw new BadRequestAlertException("无法获取系统标识", "DynamicModelConfigExService", "unzip");
}
et.setSystemId(system.getString("name")); et.setSystemId(system.getString("name"));
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); throw new BadRequestAlertException("解析动态模型文件失败," + e, "DynamicModelConfig", "unzip");
} }
} }
...@@ -114,65 +95,60 @@ public class DynamicModelConfigExService extends DynamicModelConfigServiceImpl { ...@@ -114,65 +95,60 @@ public class DynamicModelConfigExService extends DynamicModelConfigServiceImpl {
@SneakyThrows @SneakyThrows
@Override @Override
public DynamicModelConfig publish(DynamicModelConfig et) { public DynamicModelConfig publish(DynamicModelConfig et) {
if (!StringUtils.isEmpty(et.getConfigid())) {
et = get(et.getConfigid()); et = get(et.getConfigid());
String systemId = et.getSystemId(); String systemId = et.getSystemId();
String strModelFile = et.getModelfile(); String strModelFile = et.getModelfile();
if (StringUtils.isEmpty(strModelFile) && StringUtils.isEmpty(et.getSystemId())) if (StringUtils.isEmpty(strModelFile) && StringUtils.isEmpty(et.getSystemId()))
return et; throw new BadRequestAlertException(String.format("信息不足,请检查[%s]模型数据是否存在系统标识及模型文件", et.getConfigid()), "DynamicModelConfig", "unzip");
List<FileItem> items = JSONArray.parseArray(strModelFile, FileItem.class); List<FileItem> items = JSONArray.parseArray(strModelFile, FileItem.class);
for (FileItem item : items) { if (!ObjectUtils.isEmpty(items) && items.size() > 1)
File modelFile = fileService.getFile(item.getId()); throw new BadRequestAlertException(String.format("模型解析失败,[%s]模型文件过多", et.getConfigid()), "DynamicModelConfig", "unzip");
if (!modelFile.exists()) File publishFile = new File(publicPath + systemId);
return et; //删除发布目录中的历史文件
if (publishFile.exists()) {
FileHelper.deleteDir(publishFile.getPath());
}
File modelFile = fileService.getFile(items.get(0).getId());
String unzipPath = modelFile.getParent().replace("ibizutil", "dynamicmodel"); String unzipPath = modelFile.getParent().replace("ibizutil", "dynamicmodel");
File publishFile =new File(dynamicPath + systemId);
if(publishFile.exists()){
deleteDir(publishFile.getPath());
}
File unzipFile = new File(unzipPath); File unzipFile = new File(unzipPath);
//拷贝文件到发布目录 //拷贝文件到发布目录
FileHelper.copyFolder(unzipFile.getPath(), publishFile.getParent()); FileHelper.copyFolder(unzipFile.getPath(), publishFile.getParent());
File dynamicFile = new File(publishFile.getParent()+File.separator+unzipFile.getName()); File dynamicFile = new File(publishFile.getParent() + File.separator + unzipFile.getName());
if(dynamicFile.exists()){ if (dynamicFile.exists()) {
dynamicFile.renameTo(publishFile); dynamicFile.renameTo(publishFile);
} }
//部署工作流 List<Map<String, Object>> workflow = searchWorkFlow(publishFile.getPath());
List<Map<String, Object>> workflow = new ArrayList<>();
//部署流程 //部署流程
if (workflow.size() > 0) { if (workflow.size() > 0) {
wfClient.deployBpmnFile(workflow); //wfClient.deployBpmnFile(workflow);
} }
//发送redis广播 //发送redis广播
Map<String, Object> message = new HashMap<>(); Map<String, Object> message = new HashMap<>();
message.put("cacheName", "dynamicModel"); message.put("cacheName", "dynamicModel");
message.put("dynamicModel", et.getConfigname()); message.put("dynamicModel", et.getConfigname());
RedisPublisher redisPublisher = new RedisPublisher(redisOperations, RedisChannelTopic.REDIS_CACHE_DYNAMICMODEL_TOPIC.getChannelTopic());// 创建redis发布者 RedisPublisher redisPublisher = new RedisPublisher(redisOperations, RedisChannelTopic.REDIS_CACHE_DYNAMICMODEL_TOPIC.getChannelTopic());
redisPublisher.publisher(message);//发布消息 redisPublisher.publisher(message);
} //激活当前et.
// 激活当前et.
et.setStatus(StaticDict.DynamicModelStatus.ITEM_1.getValue()); et.setStatus(StaticDict.DynamicModelStatus.ITEM_1.getValue());
update(Wrappers.<DynamicModelConfig>lambdaUpdate().set(DynamicModelConfig::getStatus, StaticDict.DynamicModelStatus.ITEM_0.getValue()).eq(DynamicModelConfig::getSystemId, systemId)); update(Wrappers.<DynamicModelConfig>lambdaUpdate().set(DynamicModelConfig::getStatus, StaticDict.DynamicModelStatus.ITEM_0.getValue()).eq(DynamicModelConfig::getSystemId, systemId));
update(et); update(et);
}
return et; return et;
} }
/** /**
* 获取动态模型 * 获取处理逻辑
* *
* @param systemId * @param systemId
* @return * @return
*/ */
@SneakyThrows @SneakyThrows
public List<Map<String, Object>> getDynamicModel(String systemId) { public List<Map<String, Object>> getDynamicModel(String systemId) {
String publishPath = dynamicPath + systemId; String publishPath = publicPath + systemId;
List<Map<String, Object>> dynamicModels = search(publishPath); List<Map<String, Object>> delogics = searchLogic(publishPath);
if (ObjectUtils.isEmpty(dynamicModels)) { if (ObjectUtils.isEmpty(delogics)) {
throw new BadRequestAlertException(String.format("在发布目录[%s]中没有找到系统[%s]动态模型文件!", publishPath, systemId), "DynamicModelConfigExService", "getDynamicModel"); throw new BadRequestAlertException(String.format("在发布目录[%s]中没有找到系统[%s]动态模型文件", publishPath, systemId), "DynamicModelConfig", "getDynamicModel");
} }
return dynamicModels; return delogics;
} }
/** /**
...@@ -181,30 +157,79 @@ public class DynamicModelConfigExService extends DynamicModelConfigServiceImpl { ...@@ -181,30 +157,79 @@ public class DynamicModelConfigExService extends DynamicModelConfigServiceImpl {
* @param publishPath * @param publishPath
* @return * @return
*/ */
@SneakyThrows public List<Map<String, Object>> searchLogic(String publishPath) {
public List<Map<String, Object>> search(String publishPath) {
List<Map<String, Object>> models = new ArrayList<>(); List<Map<String, Object>> models = new ArrayList<>();
File publish = new File(publishPath); File publish = new File(publishPath);
if (!publish.exists()) { if (!publish.exists()) {
throw new BadRequestAlertException(String.format("发布目录[%s]不存在", publish.getPath()), "DynamicModelConfigExService", "search"); throw new BadRequestAlertException(String.format("发布目录[%s]不存在", publish.getPath()), "DynamicModelConfig", "search");
} }
File[] subFiles = publish.listFiles(); File[] subFiles = publish.listFiles();
if (null != subFiles) { if (null != subFiles) {
for (File subFile : subFiles) { for (File subFile : subFiles) {
if (subFile.isDirectory()) if (subFile.isDirectory()) {
{ List<Map<String, Object>> subModels = searchLogic(subFile.getAbsolutePath());
List<Map<String, Object>> subModels = search(subFile.getAbsolutePath());
if (!ObjectUtils.isEmpty(subModels)) { if (!ObjectUtils.isEmpty(subModels)) {
models.addAll(subModels); models.addAll(subModels);
} }
} else if (subFile.isFile() && (!subFile.getName().equalsIgnoreCase("PSWFVERSION.json.bpm")) && (subFile.getName().endsWith(".bpmn") || subFile.getName().endsWith(".drl"))) { } else if (subFile.isFile() && (!subFile.getName().equalsIgnoreCase("PSWFVERSION.json.bpm")) && (subFile.getName().endsWith(".bpmn") || subFile.getName().endsWith(".drl"))) {
InputStream file = new FileInputStream(subFile); InputStream file = null;
if (!ObjectUtils.isEmpty(file)) { try {
file = new FileInputStream(subFile);
Map fileMap = new HashMap(); Map fileMap = new HashMap();
fileMap.put("filename", subFile.getName()); fileMap.put("filename", subFile.getName());
fileMap.put("filepath", subFile.getPath().replace(dynamicPath, "")); fileMap.put("filepath", subFile.getPath().replace(publicPath, "").toLowerCase());
fileMap.put("filecontent", IOUtils.toString(file, "UTF-8")); fileMap.put("filecontent", IOUtils.toString(file, "UTF-8"));
models.add(fileMap); models.add(fileMap);
} catch (IOException e) {
} finally {
try {
if (file != null) {
file.close();
}
} catch (IOException e) {
}
}
}
}
}
return models;
}
/**
* 从查询工作流 bpmn
*
* @param publishPath
* @return
*/
public List<Map<String, Object>> searchWorkFlow(String publishPath) {
List<Map<String, Object>> models = new ArrayList<>();
File publish = new File(publishPath);
if (!publish.exists()) {
throw new BadRequestAlertException(String.format("发布目录[%s]不存在", publish.getPath()), "DynamicModelConfig", "search");
}
File[] subFiles = publish.listFiles();
if (null != subFiles) {
for (File subFile : subFiles) {
if (subFile.isDirectory()) {
List<Map<String, Object>> subModels = searchWorkFlow(subFile.getAbsolutePath());
if (!ObjectUtils.isEmpty(subModels)) {
models.addAll(subModels);
}
} else if (subFile.isFile() && (subFile.getName().equalsIgnoreCase("PSWFVERSION.json.bpm"))) {
InputStream file = null;
try {
file = new FileInputStream(subFile);
Map fileMap = new HashMap();
fileMap.put(subFile.getName(), IOUtils.toString(file, "UTF-8"));
models.add(fileMap);
} catch (IOException e) {
} finally {
try {
if (file != null) {
file.close();
}
} catch (IOException e) {
}
} }
} }
} }
...@@ -219,13 +244,13 @@ public class DynamicModelConfigExService extends DynamicModelConfigServiceImpl { ...@@ -219,13 +244,13 @@ public class DynamicModelConfigExService extends DynamicModelConfigServiceImpl {
* @return * @return
*/ */
private JSONObject getSystem(String filePath) { private JSONObject getSystem(String filePath) {
JSONObject system = null ; JSONObject system = null;
InputStream in = null; InputStream in = null;
byte[] bytes = null; byte[] bytes = null;
try { try {
File file = new File(filePath + File.separator + "PSSYSTEM.json"); File file = new File(filePath + File.separator + "PSSYSTEM.json");
if (!file.exists()) { if (!file.exists()) {
throw new BadRequestAlertException(String.format("无法找到动态系统模型文件[%s]", file.getPath()), "DynamicModelConfigExService", "getSystemId"); throw new BadRequestAlertException(String.format("无法找到动态系统模型文件[%s]", file.getPath()), "DynamicModelConfig", "getSystemId");
} }
in = new FileInputStream(file); in = new FileInputStream(file);
bytes = new byte[in.available()]; bytes = new byte[in.available()];
...@@ -245,27 +270,9 @@ public class DynamicModelConfigExService extends DynamicModelConfigServiceImpl { ...@@ -245,27 +270,9 @@ public class DynamicModelConfigExService extends DynamicModelConfigServiceImpl {
system = JSONObject.parseObject(strSystem); system = JSONObject.parseObject(strSystem);
} }
} }
return system; if (ObjectUtils.isEmpty(system)) {
} throw new BadRequestAlertException("无法获取系统标识", "DynamicModelConfig", "getSystem");
/**
* 删除历史动态模型
* @param dirPath
*/
public void deleteDir(String dirPath) {
File file = new File(dirPath);
if (file.isFile()) {
file.delete();
} else {
File[] files = file.listFiles();
if (files == null) {
file.delete();
} else {
for (int i = 0; i < files.length; i++) {
deleteDir(files[i].getAbsolutePath());
}
file.delete();
}
} }
return system;
} }
} }
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册