2021-11-09 07:26:08 +00:00
package cn.palmte.work.service ;
import cn.palmte.work.model.ActTaskDefRepository ;
import cn.palmte.work.pojo.ActProcDef ;
2021-11-09 11:24:42 +00:00
import org.activiti.bpmn.model.BpmnModel ;
2021-11-09 07:26:08 +00:00
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 ;
2021-11-09 11:24:42 +00:00
import top.jfunc.common.utils.IoUtil ;
2021-11-09 07:26:08 +00:00
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" ) ;
2021-11-09 11:24:42 +00:00
queryHelper . addCondition ( StringUtils . isNotEmpty ( name ) , "(p.NAME_=? or p.KEY_=?)" , name , name ) ;
2021-11-09 07:26:08 +00:00
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 ( ) ;
2021-11-09 11:24:42 +00:00
// 获取bpmnModel
BpmnModel bpmnModel = repositoryService . getBpmnModel ( processDefinition . getId ( ) ) ;
try ( InputStream inputStream = generateDiagramInputStream ( bpmnModel , new ArrayList < > ( ) , new ArrayList < > ( ) ) ) {
IoUtil . copy ( inputStream , response . getOutputStream ( ) ) ;
2021-11-09 07:26:08 +00:00
}
}
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 ) ;
}
}
}