protected void addVariables(
      HistoricProcessInstanceQuery processInstanceQuery, List<QueryVariable> variables) {
    RestResponseFactory responseFactory =
        getApplication(ActivitiRestServicesApplication.class).getRestResponseFactory();

    for (QueryVariable variable : variables) {
      if (variable.getVariableOperation() == null) {
        throw new ActivitiIllegalArgumentException(
            "Variable operation is missing for variable: " + variable.getName());
      }
      if (variable.getValue() == null) {
        throw new ActivitiIllegalArgumentException(
            "Variable value is missing for variable: " + variable.getName());
      }

      boolean nameLess = variable.getName() == null;

      Object actualValue = responseFactory.getVariableValue(variable);

      // A value-only query is only possible using equals-operator
      if (nameLess && variable.getVariableOperation() != QueryVariableOperation.EQUALS) {
        throw new ActivitiIllegalArgumentException(
            "Value-only query (without a variable-name) is only supported when using 'equals' operation.");
      }

      switch (variable.getVariableOperation()) {
        case EQUALS:
          if (nameLess) {
            processInstanceQuery.variableValueEquals(actualValue);
          } else {
            processInstanceQuery.variableValueEquals(variable.getName(), actualValue);
          }
          break;

        case EQUALS_IGNORE_CASE:
          if (actualValue instanceof String) {
            processInstanceQuery.variableValueEqualsIgnoreCase(
                variable.getName(), (String) actualValue);
          } else {
            throw new ActivitiIllegalArgumentException(
                "Only string variable values are supported when ignoring casing, but was: "
                    + actualValue.getClass().getName());
          }
          break;

        case NOT_EQUALS:
          processInstanceQuery.variableValueNotEquals(variable.getName(), actualValue);
          break;

        case LIKE:
          if (actualValue instanceof String) {
            processInstanceQuery.variableValueLike(variable.getName(), (String) actualValue);
          } else {
            throw new ActivitiIllegalArgumentException(
                "Only string variable values are supported for like, but was: "
                    + actualValue.getClass().getName());
          }
          break;

        case GREATER_THAN:
          processInstanceQuery.variableValueGreaterThan(variable.getName(), actualValue);
          break;

        case GREATER_THAN_OR_EQUALS:
          processInstanceQuery.variableValueGreaterThanOrEqual(variable.getName(), actualValue);
          break;

        case LESS_THAN:
          processInstanceQuery.variableValueLessThan(variable.getName(), actualValue);
          break;

        case LESS_THAN_OR_EQUALS:
          processInstanceQuery.variableValueLessThanOrEqual(variable.getName(), actualValue);
          break;

        default:
          throw new ActivitiIllegalArgumentException(
              "Unsupported variable query operation: " + variable.getVariableOperation());
      }
    }
  }