提交 4aa1949f 编写于 作者: zhouweidong's avatar zhouweidong

优化初始化模型逻辑

上级 6844c433
......@@ -2,37 +2,50 @@ package cn.ibizlab.core.extensions.service;
import cn.ibizlab.core.lite.domain.DynamicModelConfig;
import cn.ibizlab.core.lite.service.impl.DynamicModelConfigServiceImpl;
import cn.ibizlab.util.cache.listener.RedisPublisher;
import cn.ibizlab.util.client.IBZWFFeignClient;
import cn.ibizlab.util.dict.StaticDict;
import cn.ibizlab.util.domain.FileItem;
import cn.ibizlab.util.enums.RedisChannelTopic;
import cn.ibizlab.util.errors.BadRequestAlertException;
import cn.ibizlab.util.helper.FileHelper;
import cn.ibizlab.util.service.FileService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.SneakyThrows;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
@Primary
public class DynamicModelConfigExService extends DynamicModelConfigServiceImpl {
@Override
protected Class currentModelClass() {
return com.baomidou.mybatisplus.core.toolkit.ReflectionKit.getSuperClassGenericType(this.getClass().getSuperclass(), 1);
}
@Value("${ibiz.filePath:/app/file/}")
private String fileRoot;
private final String workflowFilename="PSWFVERSION.json.bpm";
private final String workflowFilename = "PSWFVERSION.json.bpm";
@Autowired
private FileService fileService;
......@@ -41,86 +54,218 @@ public class DynamicModelConfigExService extends DynamicModelConfigServiceImpl {
@Lazy
private IBZWFFeignClient wfClient;
@Value("${ibiz.dynamic.path:E:\\app\\}")
private String dynamicPath;
@Autowired
RedisTemplate<String, Object> redisOperations;
@Override
public boolean create(DynamicModelConfig et) {
publish(et);
unzip(et);
return super.create(et);
}
@Override
public boolean update(DynamicModelConfig et) {
publish(et);
// unzip(et);
return super.update(et);
}
/**
* 解压缩zip,部署流程及逻辑
* 将zip解压到file目录
*
* @param et
*/
@SneakyThrows
public void publish(DynamicModelConfig et) {
String strModelFile = et.getModelfile();
if (StringUtils.isEmpty(strModelFile))
return;
List<FileItem> items = JSONArray.parseArray(strModelFile, FileItem.class);
if (items.size() == 0 || StaticDict.DynamicModelStatus.ITEM_1.getValue().equalsIgnoreCase(et.getStatus()))
return;
String fileName = "";
String modelName = "";
List<Map<String, Object>> workflow = new ArrayList<>();
for (FileItem item : items) {
File modelFile = fileService.getFile(item.getId());
if (!modelFile.exists())
private void unzip(DynamicModelConfig et) {
try {
String strModelFile = et.getModelfile();
if (StringUtils.isEmpty(strModelFile))
return;
List<FileItem> items = JSONArray.parseArray(strModelFile, FileItem.class);
if (items.size() == 0 || StaticDict.DynamicModelStatus.ITEM_1.getValue().equalsIgnoreCase(et.getStatus()))
return;
File pathFile = new File(fileRoot);
if (!pathFile.exists()) {
pathFile.mkdirs();
JSONObject system =null;
for (FileItem item : items) {
File modelFile = fileService.getFile(item.getId());
if (!modelFile.exists())
return;
String unzipPath = modelFile.getParent().replace("ibizutil", "dynamicmodel");
boolean success = FileHelper.unzip(modelFile, unzipPath,true);
if (success) {
system = getSystem(unzipPath);
}
}
ZipFile zip = new ZipFile(modelFile, Charset.forName("GBK"));
fileName = item.getName();
for (Enumeration entries = zip.entries(); entries.hasMoreElements(); ) {
ZipEntry entry = (ZipEntry) entries.nextElement();
String zipEntryName = entry.getName();
InputStream in = zip.getInputStream(entry);
if (StringUtils.isEmpty(modelName)) {
modelName = zipEntryName;
if (ObjectUtils.isEmpty(system)) {
throw new BadRequestAlertException("无法获取系统标识", "DynamicModelConfigExService", "unzip");
}
et.setSystemId(system.getString("name"));
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 发布动态模型
*
* @param et
*/
@SneakyThrows
@Override
public DynamicModelConfig publish(DynamicModelConfig et) {
if (!StringUtils.isEmpty(et.getConfigid())) {
et = get(et.getConfigid());
String systemId = et.getSystemId();
String strModelFile = et.getModelfile();
if (StringUtils.isEmpty(strModelFile) && StringUtils.isEmpty(et.getSystemId()))
return et;
List<FileItem> items = JSONArray.parseArray(strModelFile, FileItem.class);
for (FileItem item : items) {
File modelFile = fileService.getFile(item.getId());
if (!modelFile.exists())
return et;
String unzipPath = modelFile.getParent().replace("ibizutil", "dynamicmodel");
File publishFile =new File(dynamicPath + systemId);
if(publishFile.exists()){
deleteDir(publishFile.getPath());
}
String outPath = (fileRoot + zipEntryName).replaceAll("\\*", "/").replaceFirst(modelName, "dynamicmodel/");
//判断路径是否存在,不存在则创建文件路径
File file = new File(outPath.substring(0, outPath.lastIndexOf('/')));
if (!file.exists()) {
file.mkdirs();
File unzipFile = new File(unzipPath);
//拷贝文件到发布目录
FileHelper.copyFolder(unzipFile.getPath(), publishFile.getParent());
File dynamicFile = new File(publishFile.getParent()+File.separator+unzipFile.getName());
if(dynamicFile.exists()){
dynamicFile.renameTo(publishFile);
}
//判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压
if (new File(outPath).isDirectory()) {
continue;
//部署工作流
List<Map<String, Object>> workflow = new ArrayList<>();
//部署流程
if (workflow.size() > 0) {
wfClient.deployBpmnFile(workflow);
}
//输出文件路径信息
System.out.println(outPath);
OutputStream out = new FileOutputStream(outPath);
byte[] bytes = new byte[1024];
int len;
while ((len = in.read(bytes)) > 0) {
out.write(bytes, 0, len);
//发送redis广播
Map<String, Object> message = new HashMap<>();
message.put("cacheName", "dynamicModel");
message.put("dynamicModel", et.getConfigname());
RedisPublisher redisPublisher = new RedisPublisher(redisOperations, RedisChannelTopic.REDIS_CACHE_DYNAMICMODEL_TOPIC.getChannelTopic());// 创建redis发布者
redisPublisher.publisher(message);//发布消息
}
// 激活当前et.
et.setStatus(StaticDict.DynamicModelStatus.ITEM_1.getValue());
update(Wrappers.<DynamicModelConfig>lambdaUpdate().set(DynamicModelConfig::getStatus, StaticDict.DynamicModelStatus.ITEM_0.getValue()).eq(DynamicModelConfig::getSystemId, systemId));
update(et);
}
return et;
}
/**
* 获取动态模型
*
* @param systemId
* @return
*/
@SneakyThrows
public List<Map<String, Object>> getDynamicModel(String systemId) {
String publishPath = dynamicPath + systemId;
List<Map<String, Object>> dynamicModels = search(publishPath);
if (ObjectUtils.isEmpty(dynamicModels)) {
throw new BadRequestAlertException(String.format("在发布目录[%s]中没有找到系统[%s]动态模型文件!", publishPath, systemId), "DynamicModelConfigExService", "getDynamicModel");
}
return dynamicModels;
}
/**
* 从发布目录中查询 bpmn及drl
*
* @param publishPath
* @return
*/
@SneakyThrows
public List<Map<String, Object>> search(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()), "DynamicModelConfigExService", "search");
}
File[] subFiles = publish.listFiles();
if (null != subFiles) {
for (File subFile : subFiles) {
if (subFile.isDirectory())
{
List<Map<String, Object>> subModels = search(subFile.getAbsolutePath());
if (!ObjectUtils.isEmpty(subModels)) {
models.addAll(subModels);
}
} else if (subFile.isFile() && (!subFile.getName().equalsIgnoreCase("PSWFVERSION.json.bpm")) && (subFile.getName().endsWith(".bpmn") || subFile.getName().endsWith(".drl"))) {
InputStream file = new FileInputStream(subFile);
if (!ObjectUtils.isEmpty(file)) {
Map fileMap = new HashMap();
fileMap.put("filename", subFile.getName());
fileMap.put("filepath", subFile.getPath().replace(dynamicPath, ""));
fileMap.put("filecontent", IOUtils.toString(file, "UTF-8"));
models.add(fileMap);
}
}
if (outPath.endsWith(workflowFilename)) {
File bpmn = new File(outPath);
workflow.add(new HashMap<String, Object>() {{
put(bpmn.getName(), IOUtils.toString(zip.getInputStream(entry)));
}});
}
}
return models;
}
/**
* 获取系统标识
*
* @param filePath
* @return
*/
private JSONObject getSystem(String filePath) {
JSONObject system = null ;
InputStream in = null;
byte[] bytes = null;
try {
File file = new File(filePath + File.separator + "PSSYSTEM.json");
if (!file.exists()) {
throw new BadRequestAlertException(String.format("无法找到动态系统模型文件[%s]", file.getPath()), "DynamicModelConfigExService", "getSystemId");
}
in = new FileInputStream(file);
bytes = new byte[in.available()];
in.read(bytes);
} catch (Exception e) {
} finally {
try {
if (in != null) {
in.close();
}
in.close();
out.close();
} catch (IOException e) {
}
}
//部署流程
if (workflow.size() > 0) {
wfClient.deployBpmnFile(workflow);
if (bytes != null) {
String strSystem = new String(bytes);
if (!StringUtils.isEmpty(strSystem)) {
system = JSONObject.parseObject(strSystem);
}
}
et.setStatus(StaticDict.DynamicModelStatus.ITEM_1.getValue());
et.setConfigname(fileName);
return system;
}
/**
* 删除历史动态模型
* @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();
}
}
}
}
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册