/*
   * (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.CrudRepository#findAll(java.lang.
   * Iterable)
   */
  @Override
  @SuppressWarnings("unchecked")
  public Collection<T> findAll(Iterable<ID> ids) {

    List<ID> parameters = new ArrayList<ID>();
    for (ID id : ids) {
      parameters.add(id);
    }

    return (Collection<T>) template.getAll(parameters).values();
  }
 /*
  * (non-Javadoc)
  *
  * @see org.springframework.data.repository.CrudRepository#findAll()
  */
 @Override
 public Collection<T> findAll() {
   return template.execute(
       new GemfireCallback<Collection<T>>() {
         @Override
         @SuppressWarnings({"rawtypes", "unchecked"})
         public Collection<T> doInGemfire(Region region) {
           return region.values();
         }
       });
 }
  /*
   * (non-Javadoc)
   *
   * @see org.springframework.data.repository.CrudRepository#deleteAll()
   */
  @Override
  public void deleteAll() {

    template.execute(
        new GemfireCallback<Void>() {
          @Override
          @SuppressWarnings("rawtypes")
          public Void doInGemfire(Region region) {
            // clear() does not work for partitioned regions
            try {
              region.clear();
            } catch (UnsupportedOperationException e) {
              for (Object key : region.keySet()) {
                region.remove(key);
              }
            }

            return null;
          }
        });
  }
 /*
  * (non-Javadoc)
  *
  * @see org.springframework.data.repository.CrudRepository#findOne(java.io.
  * Serializable)
  */
 @Override
 @SuppressWarnings("unchecked")
 public T findOne(ID id) {
   Object object = template.get(id);
   return (T) object;
 }
 /*
  * (non-Javadoc)
  *
  * @see org.springframework.data.repository.CrudRepository#save(S)
  */
 @Override
 public <U extends T> U save(U entity) {
   template.put(entityInformation.getId(entity), entity);
   return entity;
 }
 /*
  * (non-Javadoc)
  *
  * @see
  * org.springframework.data.gemfire.repository.GemfireRepository#save(org
  * .springframework.data.gemfire.repository.Wrapper)
  */
 @Override
 public T save(Wrapper<T, ID> wrapper) {
   return template.put(wrapper.getKey(), wrapper.getEntity());
 }
 /*
  * (non-Javadoc)
  *
  * @see org.springframework.data.repository.CrudRepository#delete(java.io.
  * Serializable)
  */
 @Override
 public void delete(ID id) {
   template.remove(id);
 }
 /*
  * (non-Javadoc)
  *
  * @see
  * org.springframework.data.repository.CrudRepository#delete(java.lang.Object
  * )
  */
 @Override
 public void delete(T entity) {
   template.remove(entityInformation.getId(entity));
 }
 /*
  * (non-Javadoc)
  *
  * @see org.springframework.data.repository.CrudRepository#count()
  */
 @Override
 public long count() {
   SelectResults<Integer> results =
       template.find("select count(*) from " + template.getRegion().getFullPath());
   return (long) results.iterator().next();
 }