org.activiti.validation.validator

本文详细介绍了Activiti流程定义验证的实现,包括EventListenerValidator、AssociationValidator、validateAtLeastOneExecutable和DataObjectValidator等关键类的代码分析,确保流程定义的正确性和有效性。同时,文章还提及了拓展实现EventValidator的思路,对不同类型的事件定义进行验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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");
          }

        }

      }

    }
  }

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'");
    }
  }

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));
    }

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");
      }
    }

  }

}

拓展实现

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]. https://www.activiti.org/

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

迅捷的软件产品制作专家

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值