提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
一、activiti高亮当前节点
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
RuntimeService runtimeService = processEngine.getRuntimeService();
//通过businesskey查出正在运行中流程的实例ID
ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceBusinessKey(businessKey).singleResult();
//定义返回结果集
FlowChartsInfo flowChartsInfo = new FlowChartsInfo();
if (ObjectUtils.isEmpty(processInstance) || StringUtils.isEmpty(processInstance.getProcessInstanceId())) {
HistoricProcessInstance historicProcessInstance = processEngine.getHistoryService().createHistoricProcessInstanceQuery().processInstanceBusinessKey(businessKey).singleResult();
if((ObjectUtils.isEmpty(historicProcessInstance) || StringUtils.isEmpty(((HistoricProcessInstanceEntityImpl) historicProcessInstance).getProcessInstanceId()))){
//如果根据业务key未能查到processInstanceId则报错
return flowChartsInfo;
}else{
FlowChartsInfo modelByFlowKey = this.getModelByFlowKey(historicProcessInstance.getProcessDefinitionKey());
return modelByFlowKey;
}
} else{
try {
//获取流程图的流
InputStream processDiagram = getProcessDiagram(processInstance.getProcessInstanceId());
byte[] data = null;
data = new byte[processDiagram.available()];
processDiagram.read(data);
processDiagram.close();
// 加编码
BASE64Encoder encoder = new BASE64Encoder();
flowChartsInfo.setFlowChartsStream(encoder.encode(data));
return flowChartsInfo;
} catch (Exception e) {
flowChartsInfo.setStatus(ActivitiConstants.FLOW_CHARTS_FAIL);
return flowChartsInfo;
}
}
/**
* <p>
* 获取流程图的流
* </p >
*
* @param processInstanceId 流程实例ID、
* @return 所有需高亮的线
* @author liuyong
* @custom.date 2022/4/13 21:40
*/
public InputStream getProcessDiagram(String processInstanceId) {
/* TaskService taskService = processEngine.getTaskService();*/
ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
if (processInstance != null) {
RepositoryService repositoryService = processEngine.getRepositoryService();
BpmnModel model = repositoryService.getBpmnModel(processInstance.getProcessDefinitionId());
ProcessDiagramGenerator generator = new DefaultProcessDiagramGenerator();
//生成流程图 已启动的task高亮
//processEngine.getRuntimeService().getActiveActivityIds(processInstance.getId())
//BpmnModel bpmnModel, String imageType, List<String> highLightedActivities, List<String> highLightedFlows, String activityFontName, String labelFontName, ClassLoader customClassLoader, double scaleFactor
//高亮历史节点和当前节点
//List<List<String>> string = getModelStream(model, processInstanceId);
//return generator.generateDiagram(model, "png", string.get(1), string.get(0), "宋体", "宋体", null, 1.0);
List<String> activeActivityIds = processEngine.getRuntimeService().getActiveActivityIds(processInstance.getId());
List<String> aa = new ArrayList<>();
return generator.generateDiagram(model, "png", activeActivityIds, aa, "宋体", "宋体", null, 1.0);
}
return null;
}
如要高亮走过节点与线则只需把注释放出来、替换list的值就可以了。高亮走过的节点和线还需要下面这个方法
/**
* <p>
* 获取流程图需要高亮的节点和线
* </p >
*
* @param bpmnModel 业务模型、
* historicActivityInstanceList 历史节点集合
* @return 返回流程图需要高亮的节点和线
* @author liuyong
* @custom.date 2022/4/13 21:29
*/
public List<List<String>> getModelStream(BpmnModel bpmnModel, String processInstanceId) {
//BpmnModel bpmnModel = processEngine.getRepositoryService().getBpmnModel("myProcess_1:1:3");
/* ProcessInstance processInstance = processEngine.getRuntimeService().startProcessInstanceByKey("myProcess_1");*/
String executionId = processInstanceId;
List<String> activeActivityIds = processEngine.getRuntimeService().getActiveActivityIds(executionId);
HistoricActivityInstanceQuery historicActivityInstanceQuery = processEngine.getHistoryService().createHistoricActivityInstanceQuery();
List<HistoricActivityInstance> historicActivityInstanceList = historicActivityInstanceQuery.processInstanceId(executionId).list();
for (HistoricActivityInstance hi : historicActivityInstanceList) {
String taskKey = hi.getActivityId();
activeActivityIds.add(taskKey);
}
List<String> highLightedFlowsByIncomingFlows = getHighLightedFlowsByIncomingFlows(bpmnModel, activeActivityIds);
List<List<String>> result = new ArrayList<>();
result.add(highLightedFlowsByIncomingFlows);
result.add(activeActivityIds);
return result;
}