/*
   * (non-Javadoc)
   * @see org.springframework.data.repository.query.RepositoryQuery#execute(java.lang.Object[])
   */
  @Override
  public Object execute(Object[] parameters) {
    ParametersParameterAccessor accessor =
        new ParametersParameterAccessor(method.getParameters(), parameters);

    QueryString query =
        (isUserDefinedQuery()
            ? this.query
            : this.query.forRegion(
                method.getEntityInformation().getJavaType(), template.getRegion()));

    for (Integer index : query.getInParameterIndexes()) {
      query = query.bindIn(toCollection(accessor.getBindableValue(index - 1)));
    }

    Collection<?> result = toCollection(template.find(query.toString(), parameters));

    if (method.isCollectionQuery()) {
      return result;
    } else if (method.isQueryForEntity()) {
      if (result.isEmpty()) {
        return null;
      } else if (result.size() == 1) {
        return result.iterator().next();
      } else {
        throw new IncorrectResultSizeDataAccessException(1, result.size());
      }
    } else {
      throw new IllegalStateException("Unsupported query: " + query.toString());
    }
  }
  /*
   * (non-Javadoc)
   * @see org.springframework.data.repository.query.parser.AbstractQueryCreator#complete(java.lang.Object, org.springframework.data.domain.Sort)
   */
  @Override
  protected QueryString complete(Predicates criteria, Sort sort) {

    QueryString result = query.create(criteria);

    if (LOG.isDebugEnabled()) {
      LOG.debug("Created query: " + result.toString());
    }

    return result;
  }