activiti获取流程跟踪图
@RequestMapping("/generateProcessImageBase64")
public String generateProcessImageBase64(String processInstanceId) {
HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery()
.processInstanceId(processInstanceId).singleResult();
ProcessDefinitionEntity processDefinitionEntity = (ProcessDefinitionEntity) ((RepositoryServiceImpl)
repositoryService).getDeployedProcessDefinition(historicProcessInstance.getProcessDefinitionId());
List<HistoricActivityInstance> historicActivityInstanceList = historyService
.createHistoricActivityInstanceQuery().processInstanceId(processInstanceId)
.orderByHistoricActivityInstanceStartTime().asc().list();
List<String> executedActivityIdList = new ArrayList();
for (HistoricActivityInstance historicActivityInstance : historicActivityInstanceList) {
executedActivityIdList.add(historicActivityInstance.getActivityId());
}
BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinitionEntity.getId());
List<String> executedFlowIdList = executedFlowIdList(bpmnModel, historicActivityInstanceList);
ProcessDiagramGenerator processDiagramGenerator = processEngine.getProcessEngineConfiguration()
.getProcessDiagramGenerator();
InputStream inputStream = processDiagramGenerator.generateDiagram(bpmnModel, "png", executedActivityIdList,
executedFlowIdList, processEngine.getProcessEngineConfiguration().getActivityFontName(),
processEngine.getProcessEngineConfiguration().getLabelFontName(), processEngine
.getProcessEngineConfiguration().getAnnotationFontName(), processEngine
.getProcessEngineConfiguration().getClassLoader(), 1.0);
String image = Base64Convert.getBase64FromInputStream(inputStream);
return "data:image/png;base64," + image;
}
private static List<String> executedFlowIdList(BpmnModel bpmnModel, List<HistoricActivityInstance>
historicActivityInstanceList) {
List<String> executedFlowIdList = new ArrayList();
int count = 0;
if ("endEvent".equals(historicActivityInstanceList.get(historicActivityInstanceList.size() - 1)
.getActivityType())) {
count = historicActivityInstanceList.size();
} else {
count = historicActivityInstanceList.size() - 1;
}
for (int i = 0; i < count; i++) {
HistoricActivityInstance hai = historicActivityInstanceList.get(i);
FlowNode flowNode = (FlowNode) bpmnModel.getFlowElement(hai.getActivityId());
List<SequenceFlow> sequenceFlows = flowNode.getOutgoingFlows();
if (sequenceFlows.size() > 1) {
HistoricActivityInstance nextHai = historicActivityInstanceList.get(i + 1);
for (SequenceFlow sequenceFlow : sequenceFlows) {
if (sequenceFlow.getTargetRef().equals(nextHai.getActivityId())) {
executedFlowIdList.add(sequenceFlow.getId());
}
}
} else {
if (sequenceFlows.size() != 0)
executedFlowIdList.add(sequenceFlows.get(0).getId());
}
}
return executedFlowIdList;
}