package com.flow.flowable.utils; import org.flowable.bpmn.model.*; import org.flowable.editor.language.json.converter.util.CollectionUtils; import org.flowable.engine.impl.persistence.entity.ExecutionEntity; import org.springframework.util.StringUtils; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Set; public class ProcessElementUtil { /** * 递归查找所有子节点 * * @param flowElement * @return */ public static Set findChildElements(FlowElement flowElement) { LinkedHashSet elements = new LinkedHashSet<>(); if (flowElement instanceof FlowNode) { FlowNode flowNode = (FlowNode) flowElement; List outgoingFlows = flowNode.getOutgoingFlows(); if (CollectionUtils.isNotEmpty(outgoingFlows)) { for (SequenceFlow sequenceFlow : outgoingFlows) { FlowElement targetElement = sequenceFlow.getTargetFlowElement(); elements.add(targetElement); Set childElements = findChildElements(targetElement); elements.addAll(childElements); } } } return elements; } /** * 递归查找所有父节点 * * @param flowElement * @param clazz * @param * @return */ public static Set findParentElements(FlowElement flowElement, Class clazz) { Set parentElements = findParentElements(flowElement); Set elements = new LinkedHashSet<>(); for (FlowElement parentElement : parentElements) { if (clazz.isInstance(parentElement)) { elements.add(clazz.cast(parentElement)); } } return elements; } /** * 递归查找所有父节点 * * @param flowElement * @return */ public static Set findParentElements(FlowElement flowElement) { LinkedHashSet elements = new LinkedHashSet<>(); if (flowElement instanceof FlowNode) { FlowNode flowNode = (FlowNode) flowElement; List incomingFlows = flowNode.getIncomingFlows(); if (CollectionUtils.isNotEmpty(incomingFlows)) { for (SequenceFlow sequenceFlow : incomingFlows) { FlowElement sourceElement = sequenceFlow.getSourceFlowElement(); if (sourceElement instanceof FlowNode) { FlowElement sourceFlowElement = sequenceFlow.getSourceFlowElement(); elements.add(sourceElement); Set parentElements = findParentElements(sourceFlowElement); elements.addAll(parentElements); } } } } return elements; } /** * 获取扩展元素值 * * @param element * @param extensionElementName * @return */ public static String getExtensionElementValue(BaseElement element, String extensionElementName) { Map> extensionElementMap = element.getExtensionElements(); List extensionElements = extensionElementMap.get(extensionElementName); if (!org.springframework.util.CollectionUtils.isEmpty(extensionElements)) { for (ExtensionElement extensionElement : extensionElements) { if (extensionElement.getName().equals(extensionElementName)) { return extensionElement.getElementText(); } } } return null; } /** * 获取字段扩展 * * @param element * @param fieldName * @return */ public static FieldExtension getFieldExtension(TaskWithFieldExtensions element, String fieldName) { List fieldExtensions = element.getFieldExtensions(); for (FieldExtension fieldExtension : fieldExtensions) { if (fieldName.equals(fieldExtension.getFieldName())) { return fieldExtension; } } return null; } /** * 获取字段扩展值 * * @param element * @param fieldName * @return */ public static String getFieldExtensionValue(TaskWithFieldExtensions element, String fieldName) { FieldExtension fieldExtension = getFieldExtension(element, fieldName); return fieldExtension != null ? fieldExtension.getStringValue() : null; } /** * 获取字段扩展表达式 * * @param element * @param fieldName * @return */ public static String getFieldExtensionExpression(TaskWithFieldExtensions element, String fieldName) { FieldExtension fieldExtension = getFieldExtension(element, fieldName); return fieldExtension != null ? fieldExtension.getExpression() : null; } /** * 递归获取父执行 * * @param executionEntity * @param id * @return */ public static ExecutionEntity findParentExecution(ExecutionEntity executionEntity, String id) { if (!StringUtils.isEmpty(id)) { if (id.equals(executionEntity.getId())) { return executionEntity; } ExecutionEntity parent = executionEntity.getParent(); if (parent != null) { return findParentExecution(parent, id); } } return null; } }