@SuppressWarnings("unchecked")
  protected void addSignalEventSubscriptions(ProcessDefinitionEntity processDefinition) {
    List<EventSubscriptionDeclaration> eventDefinitions =
        (List<EventSubscriptionDeclaration>)
            processDefinition.getProperty(BpmnParse.PROPERTYNAME_EVENT_SUBSCRIPTION_DECLARATION);
    if (eventDefinitions != null) {
      for (EventSubscriptionDeclaration eventDefinition : eventDefinitions) {
        if (eventDefinition.getEventType().equals("signal") && eventDefinition.isStartEvent()) {

          SignalEventSubscriptionEntity subscriptionEntity = new SignalEventSubscriptionEntity();
          subscriptionEntity.setEventName(eventDefinition.getEventName());
          subscriptionEntity.setActivityId(eventDefinition.getActivityId());
          subscriptionEntity.setProcessDefinitionId(processDefinition.getId());
          if (processDefinition.getTenantId() != null) {
            subscriptionEntity.setTenantId(processDefinition.getTenantId());
          }
          subscriptionEntity.insert();
        }
      }
    }
  }
  @SuppressWarnings("unchecked")
  protected void addMessageEventSubscriptions(ProcessDefinitionEntity processDefinition) {
    CommandContext commandContext = Context.getCommandContext();
    List<EventSubscriptionDeclaration> eventDefinitions =
        (List<EventSubscriptionDeclaration>)
            processDefinition.getProperty(BpmnParse.PROPERTYNAME_EVENT_SUBSCRIPTION_DECLARATION);
    if (eventDefinitions != null) {

      Set<String> messageNames = new HashSet<String>();
      for (EventSubscriptionDeclaration eventDefinition : eventDefinitions) {
        if (eventDefinition.getEventType().equals("message") && eventDefinition.isStartEvent()) {

          if (!messageNames.contains(eventDefinition.getEventName())) {
            messageNames.add(eventDefinition.getEventName());
          } else {
            throw new ActivitiException(
                "Cannot deploy process definition '"
                    + processDefinition.getResourceName()
                    + "': there are multiple message event subscriptions for the message with name '"
                    + eventDefinition.getEventName()
                    + "'.");
          }

          // look for subscriptions for the same name in db:
          List<EventSubscriptionEntity> subscriptionsForSameMessageName =
              commandContext
                  .getEventSubscriptionEntityManager()
                  .findEventSubscriptionsByName(
                      MessageEventHandler.EVENT_HANDLER_TYPE,
                      eventDefinition.getEventName(),
                      processDefinition.getTenantId());

          // also look for subscriptions created in the session:
          List<MessageEventSubscriptionEntity> cachedSubscriptions =
              commandContext.getDbSqlSession().findInCache(MessageEventSubscriptionEntity.class);
          for (MessageEventSubscriptionEntity cachedSubscription : cachedSubscriptions) {
            if (eventDefinition.getEventName().equals(cachedSubscription.getEventName())
                && !subscriptionsForSameMessageName.contains(cachedSubscription)) {
              subscriptionsForSameMessageName.add(cachedSubscription);
            }
          }

          // remove subscriptions deleted in the same command
          subscriptionsForSameMessageName =
              commandContext
                  .getDbSqlSession()
                  .pruneDeletedEntities(subscriptionsForSameMessageName);

          for (EventSubscriptionEntity eventSubscriptionEntity : subscriptionsForSameMessageName) {
            // throw exception only if there's already a subscription as start event

            // no process instance-id = it's a message start event
            if (StringUtils.isEmpty(eventSubscriptionEntity.getProcessInstanceId())) {
              throw new ActivitiException(
                  "Cannot deploy process definition '"
                      + processDefinition.getResourceName()
                      + "': there already is a message event subscription for the message with name '"
                      + eventDefinition.getEventName()
                      + "'.");
            }
          }

          MessageEventSubscriptionEntity newSubscription = new MessageEventSubscriptionEntity();
          newSubscription.setEventName(eventDefinition.getEventName());
          newSubscription.setActivityId(eventDefinition.getActivityId());
          newSubscription.setConfiguration(processDefinition.getId());
          newSubscription.setProcessDefinitionId(processDefinition.getId());

          if (processDefinition.getTenantId() != null) {
            newSubscription.setTenantId(processDefinition.getTenantId());
          }

          newSubscription.insert();
        }
      }
    }
  }