146 lines
6.1 KiB
Java
146 lines
6.1 KiB
Java
|
|
package cn.palmte.work.service;
|
|||
|
|
|
|||
|
|
import cn.palmte.work.model.ActTaskDefRepository;
|
|||
|
|
import cn.palmte.work.pojo.ActProcDef;
|
|||
|
|
import org.activiti.bpmn.model.*;
|
|||
|
|
import org.activiti.engine.ProcessEngine;
|
|||
|
|
import org.activiti.engine.RepositoryService;
|
|||
|
|
import org.activiti.engine.repository.ProcessDefinition;
|
|||
|
|
import org.activiti.image.ProcessDiagramGenerator;
|
|||
|
|
import org.apache.commons.lang.StringUtils;
|
|||
|
|
import org.slf4j.Logger;
|
|||
|
|
import org.slf4j.LoggerFactory;
|
|||
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|||
|
|
import org.springframework.stereotype.Service;
|
|||
|
|
import org.springframework.transaction.annotation.Transactional;
|
|||
|
|
import top.jfunc.common.db.QueryHelper;
|
|||
|
|
import top.jfunc.common.db.bean.Page;
|
|||
|
|
import top.jfunc.common.db.utils.Pagination;
|
|||
|
|
|
|||
|
|
import javax.servlet.http.HttpServletResponse;
|
|||
|
|
import java.io.IOException;
|
|||
|
|
import java.io.InputStream;
|
|||
|
|
import java.util.ArrayList;
|
|||
|
|
import java.util.List;
|
|||
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|||
|
|
|
|||
|
|
|
|||
|
|
@Service
|
|||
|
|
public class ActProcDefService {
|
|||
|
|
private static final Logger logger = LoggerFactory.getLogger(ActProcDefService.class);
|
|||
|
|
|
|||
|
|
@Autowired
|
|||
|
|
private ProcessEngine processEngine; //流程引擎对象
|
|||
|
|
@Autowired
|
|||
|
|
private RepositoryService repositoryService; //管理流程定义 与流程定义和部署对象相关的Service
|
|||
|
|
|
|||
|
|
@Autowired
|
|||
|
|
private ActTaskDefRepository actTaskDefRepository;
|
|||
|
|
|
|||
|
|
|
|||
|
|
@Autowired
|
|||
|
|
Pagination pagination;
|
|||
|
|
|
|||
|
|
|
|||
|
|
public Page<ActProcDef> list(ConcurrentHashMap<String, String> searchInfo, int pageNumber, int pageSize) {
|
|||
|
|
String select = "select p.ID_ as id,p.NAME_ as procName,p.KEY_ as procKey,p.VERSION_ as version,p.DEPLOYMENT_ID_ as deploymentId,p.RESOURCE_NAME_ as resourceName,\n" +
|
|||
|
|
" p.DGRM_RESOURCE_NAME_ as dgrmResourceName,p.SUSPENSION_STATE_ as suspensionState, d.DEPLOY_TIME_ as deployTime ";
|
|||
|
|
QueryHelper queryHelper = new QueryHelper(select, " act_re_procdef p LEFT JOIN act_re_deployment d on p.DEPLOYMENT_ID_ = d.ID_");
|
|||
|
|
String name = searchInfo.get("name");
|
|||
|
|
queryHelper.addCondition(StringUtils.isNotEmpty(name), "p.NAME_=? or p.KEY_=?", name, name);
|
|||
|
|
queryHelper.addOrderProperty("p.KEY_,p.VERSION_", false);
|
|||
|
|
return pagination.paginate(queryHelper.getSql(), ActProcDef.class, pageNumber, pageSize);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void getXmlByDeploymentId(HttpServletResponse response, String deploymentId) throws IOException {
|
|||
|
|
InputStream pic=null;
|
|||
|
|
try {
|
|||
|
|
pic= getXmlStreamByDeploymentId(deploymentId);
|
|||
|
|
byte[] b = new byte[1024];
|
|||
|
|
int len = -1;
|
|||
|
|
while ((len = pic.read(b, 0, 1024)) != -1) {
|
|||
|
|
response.getOutputStream().write(b, 0, len);
|
|||
|
|
}
|
|||
|
|
}catch (Exception e){
|
|||
|
|
logger.error("an exception happens in try catch statement", e);
|
|||
|
|
}finally {
|
|||
|
|
if(pic!=null) {
|
|||
|
|
pic.close();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public InputStream getXmlStreamByDeploymentId(String deploymentId) throws IOException{
|
|||
|
|
List<String> names = repositoryService.getDeploymentResourceNames(deploymentId);
|
|||
|
|
for (String name : names) {
|
|||
|
|
if(name.contains("xml") ) {
|
|||
|
|
return repositoryService.getResourceAsStream(deploymentId, name);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 创建默认的png
|
|||
|
|
*
|
|||
|
|
* @param response
|
|||
|
|
* @param deploymentId
|
|||
|
|
* @throws IOException
|
|||
|
|
*/
|
|||
|
|
public void createProcDefPng(HttpServletResponse response, String deploymentId) throws IOException {
|
|||
|
|
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deploymentId).singleResult();
|
|||
|
|
BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinition.getId()); // 获取bpmnModel
|
|||
|
|
InputStream inputStream = generateDiagramInputStream(bpmnModel, new ArrayList<>(), new ArrayList<>());
|
|||
|
|
responsePng(response, inputStream);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void responsePng(HttpServletResponse response, InputStream inputStream) throws IOException {
|
|||
|
|
InputStream pic = null;
|
|||
|
|
try {
|
|||
|
|
pic = inputStream;
|
|||
|
|
byte[] b = new byte[1024];
|
|||
|
|
int len = -1;
|
|||
|
|
while ((len = pic.read(b, 0, 1024)) != -1) {
|
|||
|
|
response.getOutputStream().write(b, 0, len);
|
|||
|
|
}
|
|||
|
|
} catch (Exception e) {
|
|||
|
|
logger.error("an exception happens in try catch statement", e);
|
|||
|
|
} finally {
|
|||
|
|
if (pic != null) {
|
|||
|
|
pic.close();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
public InputStream generateDiagramInputStream(BpmnModel bpmnModel, List<String> executedActivityIdList, List<String> flowIds) {
|
|||
|
|
try {
|
|||
|
|
ProcessDiagramGenerator processDiagramGenerator = processEngine.getProcessEngineConfiguration().getProcessDiagramGenerator();
|
|||
|
|
return processDiagramGenerator.generateDiagram(bpmnModel, "png", executedActivityIdList,
|
|||
|
|
flowIds, "宋体", "微软雅黑", "黑体", null, 2.0); //使用默认配置获得流程图表生成器,并生成追踪图片字符流
|
|||
|
|
} catch (Exception e) {
|
|||
|
|
logger.error("an exception happens in try catch statement", e);
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
@Transactional(rollbackFor = Exception.class)
|
|||
|
|
public void deleteDeployment(String deploymentId) {
|
|||
|
|
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deploymentId).singleResult();
|
|||
|
|
actTaskDefRepository.deleteByProcDefId(processDefinition.getId());
|
|||
|
|
|
|||
|
|
//repositoryService.deleteDeployment(deploymentId); //不带级联的删除,此删除只能删除没有启动的流程,否则抛出异常 .act_re_deployment,act_re_procdef 和 act_ge_bytearray 三张表中相关数据都删除
|
|||
|
|
repositoryService.deleteDeployment(deploymentId, true); //级联删除,不管流程是否启动,都可以删除
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void suspend(String procDefId, int status) {
|
|||
|
|
if (1 == status) {
|
|||
|
|
repositoryService.activateProcessDefinitionById(procDefId, true, null);
|
|||
|
|
}else{
|
|||
|
|
repositoryService.suspendProcessDefinitionById(procDefId, true, null);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|