后端FlowableFlowable-自定义属性
cmyang flowable已经存在一些通用的流程属性,在业务中常有自定义的属性,本节内容主要记录,如何创建自定义标签,获取标签中的内容,在流程事件中获取自定义属性,并做相应的处理
设置自定义属性
首先我们自定义的属性要使用flowable的api获取并解析,需要贵自定义属性标签做如下规范
- 自定义标签必须放在
<extensionElements>
标签内
- 标签名必须以flowable:开头
- 标签的属性值最好使用
<![CDATA[]]>
包裹起来,防止有特殊字符导致flowable解析出错
1 2 3
| <extensionElements> <flowable:selfDefAttr_startCondition><![CDATA[formValue.f1>3&&bizObj.a=="xxx"]]></flowable:selfDefAttr_startCondition> </extensionElements>
|
获取自定义属性
- 获取流程定义id(processDefinitionId)和节点id(activityId),可以通过以下方式获取
- TaskEntity的方法
taskEntity.getProcessDefinitionId()
和taskEntity.getTaskDefinitionKey()
- DelegateExecution的方法
delegateExecution.getProcessDefinitionId()
和delegateExecution.getCurrentActivityId()
- 获取BpmnModel,
BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinitionId);
- 通过bpmnModel可以获取流程定义里的标签和属性值
完整示例(场景:从事件监听中获取自定义属性):
监听器代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| @Component @Slf4j public class SelfDefAttrListener implements FlowableEventListener { @Override public void onEvent(FlowableEvent event) { log.info("自定义任务属性监听...{}", event.getType().name()); if(FlowableEngineEventType.TASK_CREATED.equals(event.getType())) { TaskEntity taskEntity = (TaskEntity) ((org.flowable.common.engine.impl.event.FlowableEntityEventImpl) event).getEntity(); String attribute = getSelfDefineTaskAttribute(taskEntity.getProcessDefinitionId(), taskEntity.getTaskDefinitionKey()); } else if(FlowableEngineEventType.TASK_COMPLETED.equals(event.getType())) { TaskEntity taskEntity = (TaskEntity) ((org.flowable.engine.delegate.event.impl.FlowableEntityEventImpl) event).getEntity(); String attribute = getSelfDefineTaskAttribute(taskEntity.getProcessDefinitionId(), taskEntity.getTaskDefinitionKey()); } else if(FlowableEngineEventType.ACTIVITY_CANCELLED.equals(event.getType())) { DelegateExecution delegateExecution = ((FlowableActivityCancelledEventImpl) event).getExecution(); String attribute = getSelfDefineTaskAttribute(delegateExecution.getProcessDefinitionId(), delegateExecution.getCurrentActivityId()); } }
private String getSelfDefineTaskAttribute(String processDefinitionId, String activityId) { BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinitionId); Map<String, String> selfDefineAttribute = FlowableUtils.getSelfDefineAttribute(bpmnModel, activityId); if(CollUtil.isNotEmpty(selfDefineAttribute)) { return selfDefineAttribute.get("selfDefAttr_taskAll"); } return null; } }
|
FlowableUtils类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public static Map<String, String> getSelfDefineAttribute(BpmnModel bpmnModel, String activityId) { List<Process> processes = bpmnModel.getProcesses(); for(Process p: processes) { FlowElement flowElement = p.getFlowElement(activityId); if(flowElement != null) { Map<String, List<ExtensionElement>> extensionElements = flowElement.getExtensionElements(); Map<String, String> result = new HashMap<>(); extensionElements.forEach((k, v) -> { if(k.startsWith("selfDefAttr_") && v.size()>0 && StringUtils.isNotBlank(v.get(0).getElementText())) { result.put(k, StringUtils.trim(v.get(0).getElementText())); } }); log.info("获取任务节点中自定义的属性->{}", result); return result; } } return null; }
|
如果自定义属性设置在主流程里可以直接从主流程获取,不用遍历
1 2 3 4
| BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefId); Process mainProcess = bpmnModel.getMainProcess(); Map<String, List<ExtensionElement>> extensionElements = mainProcess.getExtensionElements(); List<ExtensionElement> elements = extensionElements.get("selfDefAttr_startCondition");
|