天天看点

activiti7的流程图、模板图及子流程图展示

activiti7的流程图、模板图及子流程图展示

流程图需展示流程完成情况,模板图根据key找到最新模板展示。子流程需从主流程中找到CallActiviti的节点,找到对应的子流程id(已启动)或模板key(未启动),再进行对应的流程或模板展示。

//根据流程id 展示流程图

private void outPutImageByInstanceId(String instanceId,HttpServletResponse response){

if(StringUtils.isEmpty(instanceId)){

LOG.info(“流程id为空!”);

return;

}

LOG.info(“查看完整流程图!流程实例ID:{}”, instanceId);

// 获取流程实例(id查询只能查到单独的记录,如果是其他条件,此处需换为list)

HistoricProcessInstance processInstance = historyService.createHistoricProcessInstanceQuery().processInstanceId(instanceId).singleResult();

if (processInstance == null) {

LOG.error(“流程实例ID:{}没查询到流程实例!”, instanceId);

return;

}

// 根据流程对象获取流程对象模型

BpmnModel bpmnModel = repositoryService.getBpmnModel(processInstance.getProcessDefinitionId());

// 构造历史流程查询
    HistoricActivityInstanceQuery historyInstanceQuery = historyService.createHistoricActivityInstanceQuery().processInstanceId(instanceId);
    // 查询历史节点
    List<HistoricActivityInstance> historicActivityInstanceList = historyInstanceQuery.orderByHistoricActivityInstanceStartTime().asc().list();
    if (historicActivityInstanceList == null || historicActivityInstanceList.size() == 0) {
        LOG.info("流程实例ID:{}没有历史节点信息!", instanceId);
        outPutImg(response, bpmnModel, null, null);
        return;
    }
    // 已执行的节点ID集合(将historicActivityInstanceList中元素的activityId字段取出封装到executedActivityIdList)
    List<String> executedActivityIdList = historicActivityInstanceList.stream().map(item -> item.getActivityId()).collect(Collectors.toList());
    // 获取流程定义
    ProcessDefinitionEntity processDefinition = (ProcessDefinitionEntity) ((RepositoryServiceImpl) repositoryService).getDeployedProcessDefinition(processInstance.getProcessDefinitionId());
    //获取流程线
    List<String> flowIds = ActivitiUtils.getHighLightedFlows(bpmnModel, processDefinition, historicActivityInstanceList);
    //输出图像
    outPutImg(response, bpmnModel, flowIds, executedActivityIdList);
}
           

//展示模板图(根据流程定义key展示最新的模板图,此处也可以换为根据模板key展示最新模板图)

public void showImgTemplate(String processDefinitonKey, HttpServletResponse response) {

if (StringUtils.isEmpty(processDefinitonKey)) return;

//根据key查询最新的流程定义

List list = repositoryService.createProcessDefinitionQuery().processDefinitionKey(processDefinitonKey).latestVersion().list();

if(ListUtils.isEmpty(list)){

LOG.error(“流程定义Key:{}没查询到流程定义!”, processDefinitonKey);

return;

}

// 根据流程对象获取流程对象模型

BpmnModel bpmnModel = repositoryService.getBpmnModel(list.get(0).getId());

//输出图像

outPutImg(response, bpmnModel, null, null);

}

//图像输出(图像均为svg格式,activiti7如何指定图像格式,还待完善)

private void outPutImg(HttpServletResponse response, BpmnModel bpmnModel, List flowIds, List executedActivityIdList) {

InputStream imageStream = null;

try {

if(null == flowIds && null ==executedActivityIdList){

imageStream = processDiagramGenerator.generateDiagram(bpmnModel,“宋体”, “微软雅黑”, “黑体”);

}else{

imageStream = processDiagramGenerator.generateDiagram(bpmnModel, executedActivityIdList, flowIds,

“宋体”, “微软雅黑”, “黑体”, true, “png”);

}

// 输出资源内容到相应对象

byte[] b = new byte[1024];

int len;

while ((len = imageStream.read(b, 0, 1024)) != -1) {

response.getOutputStream().write(b, 0, len);

}

response.getOutputStream().flush();

response.setContentType(“image/png”);

} catch (Exception e) {

LOG.error(“流程图输出异常!”, e);

} finally { // 流关闭

if (null != imageStream) {

try {

imageStream.close();

} catch (IOException e) {

LOG.error(“流程图输入流关闭异常!”, e);

imageStream = null;

}

}

}

}

//查询流程图节点信息(可根据节点的位置信息,定位图中每个节点位置。添加相应的前端事件,展示节点信息。也可以根据其中callActiviti的节点,找到子流程的流程id或key,用于展示子流程图)

public List getImgNode(String instanceId ) {

String instanceId = inst.getInstanceId();

LOG.info(“查看完整流程图!流程实例ID:{}”, instanceId);

HistoricProcessInstance processInstance = historyService.createHistoricProcessInstanceQuery().processInstanceId(instanceId).singleResult();

if (processInstance == null) {

LOG.info(“流程实例ID:{}没查询到流程实例!”, instanceId);

result.setDetail(“activiti流程实例查询为空”);

return result;

}

// 根据流程对象获取流程对象模型

BpmnModel bpmnModel = repositoryService.getBpmnModel(processInstance.getProcessDefinitionId());

List nodeList = getNodeByModel(bpmnModel,inst.getTemplateId(),inst,null);

return nodeList ;

}

//根据BpmnModel查询补充节点信息

public List getNodeByModel(BpmnModel bpmnModel,String templateId) {

List nodeList = new ArrayList<>();

List<org.activiti.bpmn.model.Process> ps = bpmnModel.getProcesses();

if (ps != null && ps.size() > 0) {

for (Process process : ps) {

Collection elements = process.getFlowElements();

if (elements != null && elements.size() > 0) {

for (FlowElement el : elements) {

try{

if (el instanceof UserTask) {

BpmImageNode bpmNode = new BpmImageNode();

bpmNode.setType(“UserTask”);

UserTask ut = (UserTask) el;

GraphicInfo artifactGraphicInfo = bpmnModel.getGraphicInfo(ut.getId());//id为baseElement的id

if (artifactGraphicInfo != null) {

bpmNode.setX(artifactGraphicInfo.getX());

bpmNode.setY(artifactGraphicInfo.getY());

bpmNode.setWidth(artifactGraphicInfo.getWidth());

bpmNode.setHeight(artifactGraphicInfo.getHeight());

}

//TODO 此处根据自己的需要完善逻辑

//可补充任务提交信息,任务表单信息等节点信息

nodeList.add(bpmNode);

}else if(el instanceof CallActivity){

CallActivity ut = (CallActivity) el;

String processDefinitonKey = ((CallActivityBehavior)(ut.getBehavior())).getProcessDefinitonKey();

BpmImageNode bpmNode = new BpmImageNode();

bpmNode.setType(“CallActivity”);

List wfInstanceChildList = null;

//TODO WfInstanceChild是自己系统保存的流程信息,此处根据自己项目需要完善

if(ListUtils.isEmpty(wfInstanceChildList)){

//子流程未开始

bpmNode.setTemplateKey(processDefinitonKey);

}else{

//子流程已开始

List instanceIds = new ArrayList<>();

//TODO 此处可根据自己的项目来查询子流程的Id

bpmNode.setInstanceIds(instanceIds);

}

GraphicInfo artifactGraphicInfo = bpmnModel.getGraphicInfo(ut.getId());//id为baseElement的id

if (artifactGraphicInfo != null) {

bpmNode.setX(artifactGraphicInfo.getX());

bpmNode.setY(artifactGraphicInfo.getY());

bpmNode.setWidth(artifactGraphicInfo.getWidth());

bpmNode.setHeight(artifactGraphicInfo.getHeight());

}

nodeList.add(bpmNode);

}

}catch(Exception e){

LOG.error(e.getMessage(),e);

}

}

}

}

}

return nodeList;

}