ProcessElementUtil.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package com.flow.flowable.utils;
  2. import org.flowable.bpmn.model.*;
  3. import org.flowable.editor.language.json.converter.util.CollectionUtils;
  4. import org.flowable.engine.impl.persistence.entity.ExecutionEntity;
  5. import org.springframework.util.StringUtils;
  6. import java.util.LinkedHashSet;
  7. import java.util.List;
  8. import java.util.Map;
  9. import java.util.Set;
  10. public class ProcessElementUtil {
  11. /**
  12. * 递归查找所有子节点
  13. *
  14. * @param flowElement
  15. * @return
  16. */
  17. public static Set<FlowElement> findChildElements(FlowElement flowElement) {
  18. LinkedHashSet<FlowElement> elements = new LinkedHashSet<>();
  19. if (flowElement instanceof FlowNode) {
  20. FlowNode flowNode = (FlowNode) flowElement;
  21. List<SequenceFlow> outgoingFlows = flowNode.getOutgoingFlows();
  22. if (CollectionUtils.isNotEmpty(outgoingFlows)) {
  23. for (SequenceFlow sequenceFlow : outgoingFlows) {
  24. FlowElement targetElement = sequenceFlow.getTargetFlowElement();
  25. elements.add(targetElement);
  26. Set<FlowElement> childElements = findChildElements(targetElement);
  27. elements.addAll(childElements);
  28. }
  29. }
  30. }
  31. return elements;
  32. }
  33. /**
  34. * 递归查找所有父节点
  35. *
  36. * @param flowElement
  37. * @param clazz
  38. * @param <T>
  39. * @return
  40. */
  41. public static <T extends FlowElement> Set<T> findParentElements(FlowElement flowElement, Class<T> clazz) {
  42. Set<FlowElement> parentElements = findParentElements(flowElement);
  43. Set<T> elements = new LinkedHashSet<>();
  44. for (FlowElement parentElement : parentElements) {
  45. if (clazz.isInstance(parentElement)) {
  46. elements.add(clazz.cast(parentElement));
  47. }
  48. }
  49. return elements;
  50. }
  51. /**
  52. * 递归查找所有父节点
  53. *
  54. * @param flowElement
  55. * @return
  56. */
  57. public static Set<FlowElement> findParentElements(FlowElement flowElement) {
  58. LinkedHashSet<FlowElement> elements = new LinkedHashSet<>();
  59. if (flowElement instanceof FlowNode) {
  60. FlowNode flowNode = (FlowNode) flowElement;
  61. List<SequenceFlow> incomingFlows = flowNode.getIncomingFlows();
  62. if (CollectionUtils.isNotEmpty(incomingFlows)) {
  63. for (SequenceFlow sequenceFlow : incomingFlows) {
  64. FlowElement sourceElement = sequenceFlow.getSourceFlowElement();
  65. if (sourceElement instanceof FlowNode) {
  66. FlowElement sourceFlowElement = sequenceFlow.getSourceFlowElement();
  67. elements.add(sourceElement);
  68. Set<FlowElement> parentElements = findParentElements(sourceFlowElement);
  69. elements.addAll(parentElements);
  70. }
  71. }
  72. }
  73. }
  74. return elements;
  75. }
  76. /**
  77. * 获取扩展元素值
  78. *
  79. * @param element
  80. * @param extensionElementName
  81. * @return
  82. */
  83. public static String getExtensionElementValue(BaseElement element, String extensionElementName) {
  84. Map<String, List<ExtensionElement>> extensionElementMap = element.getExtensionElements();
  85. List<ExtensionElement> extensionElements = extensionElementMap.get(extensionElementName);
  86. if (!org.springframework.util.CollectionUtils.isEmpty(extensionElements)) {
  87. for (ExtensionElement extensionElement : extensionElements) {
  88. if (extensionElement.getName().equals(extensionElementName)) {
  89. return extensionElement.getElementText();
  90. }
  91. }
  92. }
  93. return null;
  94. }
  95. /**
  96. * 获取字段扩展
  97. *
  98. * @param element
  99. * @param fieldName
  100. * @return
  101. */
  102. public static FieldExtension getFieldExtension(TaskWithFieldExtensions element, String fieldName) {
  103. List<FieldExtension> fieldExtensions = element.getFieldExtensions();
  104. for (FieldExtension fieldExtension : fieldExtensions) {
  105. if (fieldName.equals(fieldExtension.getFieldName())) {
  106. return fieldExtension;
  107. }
  108. }
  109. return null;
  110. }
  111. /**
  112. * 获取字段扩展值
  113. *
  114. * @param element
  115. * @param fieldName
  116. * @return
  117. */
  118. public static String getFieldExtensionValue(TaskWithFieldExtensions element, String fieldName) {
  119. FieldExtension fieldExtension = getFieldExtension(element, fieldName);
  120. return fieldExtension != null ? fieldExtension.getStringValue() : null;
  121. }
  122. /**
  123. * 获取字段扩展表达式
  124. *
  125. * @param element
  126. * @param fieldName
  127. * @return
  128. */
  129. public static String getFieldExtensionExpression(TaskWithFieldExtensions element, String fieldName) {
  130. FieldExtension fieldExtension = getFieldExtension(element, fieldName);
  131. return fieldExtension != null ? fieldExtension.getExpression() : null;
  132. }
  133. /**
  134. * 递归获取父执行
  135. *
  136. * @param executionEntity
  137. * @param id
  138. * @return
  139. */
  140. public static ExecutionEntity findParentExecution(ExecutionEntity executionEntity, String id) {
  141. if (!StringUtils.isEmpty(id)) {
  142. if (id.equals(executionEntity.getId())) {
  143. return executionEntity;
  144. }
  145. ExecutionEntity parent = executionEntity.getParent();
  146. if (parent != null) {
  147. return findParentExecution(parent, id);
  148. }
  149. }
  150. return null;
  151. }
  152. }