protected void checkAndFireEntryCriteria(List<String> satisfiedSentries) {
    if (isAvailable() || isNew()) {
      // do that only, when this child case execution
      // is available

      CmmnActivity activity = getActivity();
      ensureNotNull(
          PvmException.class,
          "Case execution '" + getId() + "': has no current activity.",
          "activity",
          activity);

      List<CmmnSentryDeclaration> criteria = activity.getEntryCriteria();
      for (CmmnSentryDeclaration sentryDeclaration : criteria) {
        if (sentryDeclaration != null && satisfiedSentries.contains(sentryDeclaration.getId())) {
          if (isAvailable()) {
            fireEntryCriteria();
          } else {
            entryCriterionSatisfied = true;
          }
          break;
        }
      }
    }
  }
  public void createSentryParts() {
    CmmnActivity activity = getActivity();
    ensureNotNull("Case execution '" + id + "': has no current activity", "activity", activity);

    List<CmmnSentryDeclaration> sentries = activity.getSentries();

    if (sentries != null && !sentries.isEmpty()) {

      for (CmmnSentryDeclaration sentryDeclaration : sentries) {

        CmmnIfPartDeclaration ifPartDeclaration = sentryDeclaration.getIfPart();
        if (ifPartDeclaration != null) {
          CmmnSentryPart ifPart = createIfPart(sentryDeclaration, ifPartDeclaration);
          addSentryPart(ifPart);
        }

        List<CmmnOnPartDeclaration> onPartDeclarations = sentryDeclaration.getOnParts();

        for (CmmnOnPartDeclaration onPartDeclaration : onPartDeclarations) {
          CmmnSentryPart onPart = createOnPart(sentryDeclaration, onPartDeclaration);
          addSentryPart(onPart);
        }

        List<CmmnVariableOnPartDeclaration> variableOnPartDeclarations =
            sentryDeclaration.getVariableOnParts();
        for (CmmnVariableOnPartDeclaration variableOnPartDeclaration : variableOnPartDeclarations) {
          CmmnSentryPart variableOnPart =
              createVariableOnPart(sentryDeclaration, variableOnPartDeclaration);
          addSentryPart(variableOnPart);
        }
      }
    }
  }
  protected void checkAndFireExitCriteria(List<String> satisfiedSentries) {
    if (isActive()) {
      CmmnActivity activity = getActivity();
      ensureNotNull(
          PvmException.class,
          "Case execution '" + getId() + "': has no current activity.",
          "activity",
          activity);

      // trigger first exitCriteria
      List<CmmnSentryDeclaration> exitCriteria = activity.getExitCriteria();
      for (CmmnSentryDeclaration sentryDeclaration : exitCriteria) {

        if (sentryDeclaration != null && satisfiedSentries.contains(sentryDeclaration.getId())) {
          fireExitCriteria();
          break;
        }
      }
    }
  }
  protected CmmnSentryPart createSentryPart(CmmnSentryDeclaration sentryDeclaration, String type) {
    CmmnSentryPart newSentryPart = newSentryPart();

    // set the type
    newSentryPart.setType(type);

    // set the case instance and case execution
    newSentryPart.setCaseInstance(getCaseInstance());
    newSentryPart.setCaseExecution(this);

    // set sentry id
    String sentryId = sentryDeclaration.getId();
    newSentryPart.setSentryId(sentryId);

    return newSentryPart;
  }
  protected CmmnSentryPart createOnPart(
      CmmnSentryDeclaration sentryDeclaration, CmmnOnPartDeclaration onPartDeclaration) {
    CmmnSentryPart sentryPart = createSentryPart(sentryDeclaration, PLAN_ITEM_ON_PART);

    // set the standard event
    String standardEvent = onPartDeclaration.getStandardEvent();
    sentryPart.setStandardEvent(standardEvent);

    // set source case execution
    CmmnActivity source = onPartDeclaration.getSource();
    ensureNotNull(
        "The source of sentry '" + sentryDeclaration.getId() + "' is null.", "source", source);

    String sourceActivityId = source.getId();
    sentryPart.setSource(sourceActivityId);

    // TODO: handle also sentryRef!!! (currently not implemented on purpose)

    return sentryPart;
  }
  protected boolean isSentryPartsSatisfied(
      String sentryId, List<? extends CmmnSentryPart> sentryParts) {
    // if part will be evaluated in the end
    CmmnSentryPart ifPart = null;

    if (sentryParts != null && !sentryParts.isEmpty()) {
      for (CmmnSentryPart sentryPart : sentryParts) {

        if (PLAN_ITEM_ON_PART.equals(sentryPart.getType())) {

          if (!sentryPart.isSatisfied()) {
            return false;
          }

        } else if (VARIABLE_ON_PART.equals(sentryPart.getType())) {
          if (!sentryPart.isSatisfied()) {
            return false;
          }
        } else {
            /* IF_PART.equals(sentryPart.getType) == true */

          ifPart = sentryPart;

          // once the ifPart has been satisfied the whole sentry is satisfied
          if (ifPart.isSatisfied()) {
            return true;
          }
        }
      }
    }

    if (ifPart != null) {

      CmmnExecution execution = ifPart.getCaseExecution();
      ensureNotNull("Case execution of sentry '" + ifPart.getSentryId() + "': is null", execution);

      CmmnActivity activity = ifPart.getCaseExecution().getActivity();
      ensureNotNull("Case execution '" + id + "': has no current activity", "activity", activity);

      CmmnSentryDeclaration sentryDeclaration = activity.getSentry(sentryId);
      ensureNotNull(
          "Case execution '" + id + "': has no declaration for sentry '" + sentryId + "'",
          "sentryDeclaration",
          sentryDeclaration);

      CmmnIfPartDeclaration ifPartDeclaration = sentryDeclaration.getIfPart();
      ensureNotNull(
          "Sentry declaration '"
              + sentryId
              + "' has no definied ifPart, but there should be one defined for case execution '"
              + id
              + "'.",
          "ifPartDeclaration",
          ifPartDeclaration);

      Expression condition = ifPartDeclaration.getCondition();
      ensureNotNull(
          "A condition was expected for ifPart of Sentry declaration '"
              + sentryId
              + "' for case execution '"
              + id
              + "'.",
          "condition",
          condition);

      Object result = condition.getValue(this);
      ensureInstanceOf("condition expression returns non-Boolean", "result", result, Boolean.class);

      Boolean booleanResult = (Boolean) result;
      ifPart.setSatisfied(booleanResult);
      return booleanResult;
    }

    // if all onParts are satisfied and there is no
    // ifPart then the whole sentry is satisfied.
    return true;
  }