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);
/*
* 获取activiti流程实例
*/
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;
}

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值