Declared and implemented the findAll(:Sort) method from the SDC's PagingAndSortingRepository abstraction in SDG given GemFire supports ORDER BY in Queries.

This commit is contained in:
John Blum
2014-04-16 21:11:54 -07:00
parent 7c354f59d6
commit e6066d60c0
3 changed files with 81 additions and 13 deletions

View File

@@ -17,14 +17,31 @@ package org.springframework.data.gemfire.repository;
import java.io.Serializable;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.CrudRepository;
/**
* Gemfire-specific extension of the {@link CrudRepository} interface.
*
* <p/>
* @author Oliver Gierke
* @author John Blum
* @see java.io.Serializable
* @see org.springframework.data.repository.CrudRepository
*/
@SuppressWarnings("unused")
public interface GemfireRepository<T, ID extends Serializable> extends CrudRepository<T, ID> {
/**
* Returns all entities sorted by the given options.
* <p/>
* @param sort the Spring Data Commons Sort type defining the ordering criteria.
* @return all entities sorted by the given options.
* @see org.springframework.data.repository.PagingAndSortingRepository#findAll(org.springframework.data.domain.Sort)
* @see org.springframework.data.domain.Sort
* @see java.lang.Iterable
*/
Iterable<T> findAll(Sort sort);
T save(Wrapper<T, ID> wrapper);
}

View File

@@ -5,6 +5,7 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.springframework.data.domain.Sort;
import org.springframework.data.gemfire.GemfireCallback;
import org.springframework.data.gemfire.GemfireTemplate;
import org.springframework.data.gemfire.repository.GemfireRepository;
@@ -101,12 +102,44 @@ public class SimpleGemfireRepository<T, ID extends Serializable> implements Gemf
@Override
public Collection<T> findAll() {
SelectResults<T> results = template.find("select * from " + template.getRegion().getFullPath());
return (Collection<T>)results.asList();
return results.asList();
}
/*
* (non-Javadoc)
*
* @see org.springframework.data.gemfire.repository.GemfireRepository.sor(:org.springframework.data.domain.Sort)
*/
@Override
public Iterable<T> findAll(final Sort sort) {
SelectResults<T> selectResults = template.find(String.format("SELECT * FROM %1$s%2$s",
template.getRegion().getFullPath(), toString(sort)));
return selectResults.asList();
}
/*
* (non-Javadoc)
*
* @see org.springframework.data.domain.Sort
*/
private String toString(final Sort sort) {
if (sort != null) {
StringBuilder orderClause = new StringBuilder(" ORDER BY ");
int count = 0;
for (Sort.Order order : sort) {
orderClause.append(count++ > 0 ? ", " : "");
orderClause.append(String.format("%1$s %2$s", order.getProperty(), order.getDirection()));
}
return orderClause.toString();
}
return "";
}
/*
* (non-Javadoc)
*
* @see
* org.springframework.data.repository.CrudRepository#findAll(java.lang.
* Iterable)

View File

@@ -15,8 +15,8 @@
*/
package org.springframework.data.gemfire.repository.support;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.List;
@@ -26,32 +26,39 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.gemfire.mapping.GemfireMappingContext;
import org.springframework.data.gemfire.repository.sample.Person;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.Repository;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.RegionAttributes;
/**
* Unit tests for {@link GemfireRepositoryFactory}.
*
* <p/>
* @author Oliver Gierke
* @author John Blum
* @see org.springframework.data.gemfire.repository.support.GemfireRepositoryFactory
*/
@RunWith(MockitoJUnitRunner.class)
@SuppressWarnings("unused")
public class GemfireRepositoryFactoryUnitTests {
@Mock
Region<?, ?> region;
private Region<?, ?> region;
@Mock
@SuppressWarnings("rawtypes")
RegionAttributes attributes;
private RegionAttributes attributes;
/**
* @see SGF-112
* @link https://jira.spring.io/browse/SGF-112
*/
@Test
@Test(expected = IllegalStateException.class)
@SuppressWarnings("unchecked")
public void rejectsInterfacesExtendingPagingAndSortingRepository() {
@@ -67,12 +74,23 @@ public class GemfireRepositoryFactoryUnitTests {
try {
factory.getRepository(SampleInterface.class);
} catch (IllegalStateException e) {
assertThat(e.getMessage(), Matchers.startsWith("Pagination is not supported by Gemfire repositories!"));
//factory.getRepository(SamplePagingInterface.class);
//factory.getRepository(SampleSortingInterface.class);
} catch (IllegalStateException expected) {
assertThat(expected.getMessage(), Matchers.startsWith("Pagination is not supported by Gemfire repositories!"));
throw expected;
}
}
interface SampleInterface extends PagingAndSortingRepository<Person, Long> {
}
interface SamplePagingInterface extends Repository<Person, Long> {
Page<Person> findAll(Pageable pageable);
}
interface SampleSortingInterface extends Repository<Person, Long> {
Iterable<Person> findAll(Sort sort);
}
}