• org.activiti.validation.validator


    Survive by day and develop by night.
    talk for import biz , show your perfect code,full busy,skip hardness,make a better result,wait for change,challenge Survive.
    happy for hardess to solve denpendies.

    目录

    在这里插入图片描述

    概述

    验证的是一个非常常见的需求。

    需求:

    设计思路

    实现思路分析

    1.ActivitiEventListenerValidator

    在这里插入图片描述

    ublic class ActivitiEventListenerValidator extends ProcessLevelValidator {
    
      @Override
      protected void executeValidation(BpmnModel bpmnModel, Process process, List<ValidationError> errors) {
        List<EventListener> eventListeners = process.getEventListeners();
        if (eventListeners != null) {
          for (EventListener eventListener : eventListeners) {
    
            if (eventListener.getImplementationType() != null && eventListener.getImplementationType().equals(ImplementationType.IMPLEMENTATION_TYPE_INVALID_THROW_EVENT)) {
    
              addError(errors, Problems.EVENT_LISTENER_INVALID_THROW_EVENT_TYPE, process, eventListener, "Invalid or unsupported throw event type on event listener");
    
            } else if (eventListener.getImplementationType() == null || eventListener.getImplementationType().length() == 0) {
    
              addError(errors, Problems.EVENT_LISTENER_IMPLEMENTATION_MISSING, process, eventListener, "Element 'class', 'delegateExpression' or 'throwEvent' is mandatory on eventListener");
    
            } else if (eventListener.getImplementationType() != null) {
    
              if (!ImplementationType.IMPLEMENTATION_TYPE_CLASS.equals(eventListener.getImplementationType())
                  && !ImplementationType.IMPLEMENTATION_TYPE_DELEGATEEXPRESSION.equals(eventListener.getImplementationType())
                  && !ImplementationType.IMPLEMENTATION_TYPE_THROW_SIGNAL_EVENT.equals(eventListener.getImplementationType())
                  && !ImplementationType.IMPLEMENTATION_TYPE_THROW_GLOBAL_SIGNAL_EVENT.equals(eventListener.getImplementationType())
                  && !ImplementationType.IMPLEMENTATION_TYPE_THROW_MESSAGE_EVENT.equals(eventListener.getImplementationType())
                  && !ImplementationType.IMPLEMENTATION_TYPE_THROW_ERROR_EVENT.equals(eventListener.getImplementationType())) {
                addError(errors, Problems.EVENT_LISTENER_INVALID_IMPLEMENTATION, process, eventListener, "Unsupported implementation type for event listener");
              }
    
            }
    
          }
    
        }
      }
    
    • 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
    • 33

    3.AssociationValidator

    public class AssociationValidator extends ValidatorImpl {
    
      @Override
      public void validate(BpmnModel bpmnModel, List<ValidationError> errors) {
    
        // Global associations
        Collection<Artifact> artifacts = bpmnModel.getGlobalArtifacts();
        if (artifacts != null) {
          for (Artifact artifact : artifacts) {
            if (artifact instanceof Association) {
              validate(null, (Association) artifact, errors);
            }
          }
        }
    
        // Process associations
        for (Process process : bpmnModel.getProcesses()) {
          artifacts = process.getArtifacts();
          for (Artifact artifact : artifacts) {
            if (artifact instanceof Association) {
              validate(process, (Association) artifact, errors);
            }
          }
        }
    
      }
    
      protected void validate(Process process, Association association, List<ValidationError> errors) {
        if (StringUtils.isEmpty(association.getSourceRef())) {
          addError(errors, Problems.ASSOCIATION_INVALID_SOURCE_REFERENCE, process, association, "association element missing attribute 'sourceRef'");
        }
        if (StringUtils.isEmpty(association.getTargetRef())) {
          addError(errors, Problems.ASSOCIATION_INVALID_TARGET_REFERENCE, process, association, "association element missing attribute 'targetRef'");
        }
      }
    
    • 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
    • 33
    • 34
    • 35

    4.validateAtLeastOneExecutable

    	 * Returns 'true' if at least one process definition in the {@link BpmnModel} is executable.
    	 */
      protected boolean validateAtLeastOneExecutable(BpmnModel bpmnModel, List<ValidationError> errors) {
    	  int nrOfExecutableDefinitions = 0;
    		for (Process process : bpmnModel.getProcesses()) {
    			if (process.isExecutable()) {
    				nrOfExecutableDefinitions++;
    			}
    		}
    
    		if (nrOfExecutableDefinitions == 0) {
    			addError(errors, Problems.ALL_PROCESS_DEFINITIONS_NOT_EXECUTABLE,
    					"All process definition are set to be non-executable (property 'isExecutable' on process). This is not allowed.");
    		}
    
    		return nrOfExecutableDefinitions > 0;
      }
    
      protected List<Process> getProcessesWithSameId(final List<Process> processes) {
                List<Process> filteredProcesses = processes.stream()
                    .filter(process -> process.getName() != null).collect(Collectors.toList());
              return getDuplicatesMap(filteredProcesses).values().stream()
                  .filter(duplicates -> duplicates.size() > 1)
                  .flatMap(Collection::stream)
                  .collect(Collectors.toList());
      }
    
      private static Map<String, List<Process>> getDuplicatesMap(List<Process> processes) {
            return processes.stream().collect(Collectors.groupingBy(Process::getId));
        }
    
    
    • 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

    5.DataObjectValidator

    
     */
    public class DataObjectValidator extends ProcessLevelValidator {
    
      @Override
      protected void executeValidation(BpmnModel bpmnModel, Process process, List<ValidationError> errors) {
    
        // Gather data objects
        List<ValuedDataObject> allDataObjects = new ArrayList<ValuedDataObject>();
        allDataObjects.addAll(process.getDataObjects());
        List<SubProcess> subProcesses = process.findFlowElementsOfType(SubProcess.class, true);
        for (SubProcess subProcess : subProcesses) {
          allDataObjects.addAll(subProcess.getDataObjects());
        }
    
        // Validate
        for (ValuedDataObject dataObject : allDataObjects) {
          if (StringUtils.isEmpty(dataObject.getName())) {
            addError(errors, Problems.DATA_OBJECT_MISSING_NAME, process, dataObject, "Name is mandatory for a data object");
          }
        }
    
      }
    
    }
    
    • 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

    拓展实现

    public class EventValidator extends ProcessLevelValidator {
    
      @Override
      protected void executeValidation(BpmnModel bpmnModel, Process process, List<ValidationError> errors) {
        List<Event> events = process.findFlowElementsOfType(Event.class);
        for (Event event : events) {
          if (event.getEventDefinitions() != null) {
            for (EventDefinition eventDefinition : event.getEventDefinitions()) {
    
              if (eventDefinition instanceof MessageEventDefinition) {
                handleMessageEventDefinition(bpmnModel, process, event, eventDefinition, errors);
              } else if (eventDefinition instanceof SignalEventDefinition) {
                handleSignalEventDefinition(bpmnModel, process, event, eventDefinition, errors);
              } else if (eventDefinition instanceof TimerEventDefinition) {
                handleTimerEventDefinition(process, event, eventDefinition, errors);
              } else if (eventDefinition instanceof CompensateEventDefinition) {
                handleCompensationEventDefinition(bpmnModel, process, event, eventDefinition, errors);
              }
    
            }
          }
        }
      }
    
      protected void handleMessageEventDefinition(BpmnModel bpmnModel, Process process, Event event, EventDefinition eventDefinition, List<ValidationError> errors) {
        MessageEventDefinition messageEventDefinition = (MessageEventDefinition) eventDefinition;
    
        if (StringUtils.isEmpty(messageEventDefinition.getMessageRef())) {
    
          if (StringUtils.isEmpty(messageEventDefinition.getMessageExpression())) {
            // message ref should be filled in
            addError(errors, Problems.MESSAGE_EVENT_MISSING_MESSAGE_REF, process, event, "attribute 'messageRef' is required");
          }
    
        } else if (!bpmnModel.containsMessageId(messageEventDefinition.getMessageRef())) {
          // message ref should exist
          addError(errors, Problems.MESSAGE_EVENT_INVALID_MESSAGE_REF, process, event, "Invalid 'messageRef': no message with that id can be found in the model");
        }
      }
    
    
    • 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
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    参考资料和推荐阅读

    [1]. https://www.activiti.org/

    欢迎阅读,各位老铁,如果对你有帮助,点个赞加个关注呗!~

  • 相关阅读:
    python
    24张图攻克border-image
    MySQL全库只读,不推荐使用set global readonly=true
    测试需求分析
    Geoserver中TileLayers中切割离线瓦片预览时地图模糊不清解决方法2
    一个可以让你有更多时间摸鱼的WPF控件(一)
    日常--Kali开启SSH(详细教程)
    【微服务】Gunicorn服务器替换Flask自带的WSGI Server服务器
    C# chatGPT API调用示例
    设计模式详解(十七)——迭代子模式
  • 原文地址:https://blog.csdn.net/xiamaocheng/article/details/127873977