activiti 获取流程图

//根据业务id获取流程图 并且高亮显示

public String getProcessImage(String key) {

// ProcessInstance instance = runtimeService
// .createProcessInstanceQuery()
// .processDefinitionKey(PROCESS_NAME)
// .processInstanceBusinessKey(key)
// .singleResult();
HistoricProcessInstance instance = historyService.createHistoricProcessInstanceQuery()
.processDefinitionKey(PROCESS_NAME)
.processInstanceBusinessKey(key)
.singleResult();
String processInstanceId = instance.getId();
HistoricProcessInstance processInstance =  historyService
.createHistoricProcessInstanceQuery()
.processInstanceId(processInstanceId)
.singleResult();
//获取流程图
BpmnModel bpmnModel = repositoryService.getBpmnModel(processInstance.getProcessDefinitionId());
processEngineConfiguration = processEngine.getProcessEngineConfiguration();
Context.setProcessEngineConfiguration((ProcessEngineConfigurationImpl) processEngineConfiguration);
ProcessDefinitionEntity definitionEntity = (ProcessDefinitionEntity)repositoryService
.getProcessDefinition(processInstance.getProcessDefinitionId());
List highLightedActivitList =  historyService
.createHistoricActivityInstanceQuery()
.processInstanceId(processInstanceId)
.list();
//高亮环节id集合
List highLightedActivitis = new ArrayList();
//高亮线路id集合
List highLightedFlows = getHighLightedFlows(definitionEntity,highLightedActivitList);
for(HistoricActivityInstance tempActivity : highLightedActivitList){
String activityId = tempActivity.getActivityId();
highLightedActivitis.add(activityId);
}
//中文显示的是口口口,设置字体就好了
InputStream imageStream = new DefaultProcessDiagramGenerator().generateDiagram(
bpmnModel,   
"png",   
highLightedActivitis,   
highLightedFlows,   
"宋体",   
"宋体",   
null,   
1.0);
byte[] result = null;
try {
result = new byte[imageStream.available()];
imageStream.read(result);
imageStream.close();
} catch (IOException e) {
log.error("流程图获取失败:"+e.getMessage());
}
return new BASE64Encoder().encode(result);
}
/**
* 获取需要高亮的线
* @param processDefinitionEntity
* @param historicActivityInstances
* @return
*/
private List getHighLightedFlows(
ProcessDefinitionEntity processDefinitionEntity,
List historicActivityInstances) {
List highFlows = new ArrayList();// 用以保存高亮的线flowId
for (int i = 0; i < historicActivityInstances.size() - 1; i++) {// 对历史流程节点进行遍历
ActivityImpl activityImpl = processDefinitionEntity
.findActivity(historicActivityInstances.get(i)
.getActivityId());// 得到节点定义的详细信息
List sameStartTimeNodes = new ArrayList();// 用以保存后需开始时间相同的节点
ActivityImpl sameActivityImpl1 = processDefinitionEntity
.findActivity(historicActivityInstances.get(i + 1)
.getActivityId());
// 将后面第一个节点放在时间相同节点的集合里
sameStartTimeNodes.add(sameActivityImpl1);
for (int j = i + 1; j < historicActivityInstances.size() - 1; j++) {
HistoricActivityInstance activityImpl1 = historicActivityInstances
.get(j);// 后续第一个节点
HistoricActivityInstance activityImpl2 = historicActivityInstances
.get(j + 1);// 后续第二个节点
if (activityImpl1.getStartTime().equals(
activityImpl2.getStartTime())) {
// 如果第一个节点和第二个节点开始时间相同保存
ActivityImpl sameActivityImpl2 = processDefinitionEntity
.findActivity(activityImpl2.getActivityId());
sameStartTimeNodes.add(sameActivityImpl2);
} else {
// 有不相同跳出循环
break;
}
}
List pvmTransitions = activityImpl
.getOutgoingTransitions();// 取出节点的所有出去的线
for (PvmTransition pvmTransition : pvmTransitions) {
// 对所有的线进行遍历
ActivityImpl pvmActivityImpl = (ActivityImpl) pvmTransition
.getDestination();
// 如果取出的线的目标节点存在时间相同的节点里,保存该线的id,进行高亮显示
if (sameStartTimeNodes.contains(pvmActivityImpl)) {
highFlows.add(pvmTransition.getId());
}
}
}
return highFlows;

}


js代码

/**
 * 获取图片
 */
function getProcessImg(key){
var params = {};
params['key'] = key;
$.ajaxSetup({ cache: false });
showLoading();
$.post("/general/visitorProcess/getProcessImage.do",params,function(result){
closeLoading();
$('#testImg').attr('src',"data:image/png;base64,"+result);
showDiv('div_process_image');
$('#popupLayerScreenLocker').click(function(){
closeDiv('div_process_image');
});
$.ajaxSetup({ cache: true });
});
}

你可能感兴趣的:(activiti)