| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- 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<FlowElement> findChildElements(FlowElement flowElement) {
- LinkedHashSet<FlowElement> elements = new LinkedHashSet<>();
- if (flowElement instanceof FlowNode) {
- FlowNode flowNode = (FlowNode) flowElement;
- List<SequenceFlow> outgoingFlows = flowNode.getOutgoingFlows();
- if (CollectionUtils.isNotEmpty(outgoingFlows)) {
- for (SequenceFlow sequenceFlow : outgoingFlows) {
- FlowElement targetElement = sequenceFlow.getTargetFlowElement();
- elements.add(targetElement);
- Set<FlowElement> childElements = findChildElements(targetElement);
- elements.addAll(childElements);
- }
- }
- }
- return elements;
- }
- /**
- * 递归查找所有父节点
- *
- * @param flowElement
- * @param clazz
- * @param <T>
- * @return
- */
- public static <T extends FlowElement> Set<T> findParentElements(FlowElement flowElement, Class<T> clazz) {
- Set<FlowElement> parentElements = findParentElements(flowElement);
- Set<T> elements = new LinkedHashSet<>();
- for (FlowElement parentElement : parentElements) {
- if (clazz.isInstance(parentElement)) {
- elements.add(clazz.cast(parentElement));
- }
- }
- return elements;
- }
- /**
- * 递归查找所有父节点
- *
- * @param flowElement
- * @return
- */
- public static Set<FlowElement> findParentElements(FlowElement flowElement) {
- LinkedHashSet<FlowElement> elements = new LinkedHashSet<>();
- if (flowElement instanceof FlowNode) {
- FlowNode flowNode = (FlowNode) flowElement;
- List<SequenceFlow> 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<FlowElement> parentElements = findParentElements(sourceFlowElement);
- elements.addAll(parentElements);
- }
- }
- }
- }
- return elements;
- }
- /**
- * 获取扩展元素值
- *
- * @param element
- * @param extensionElementName
- * @return
- */
- public static String getExtensionElementValue(BaseElement element, String extensionElementName) {
- Map<String, List<ExtensionElement>> extensionElementMap = element.getExtensionElements();
- List<ExtensionElement> 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<FieldExtension> 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;
- }
- }
|