From e6066d60c015e6c8e41bfa7485b950fe6469c741 Mon Sep 17 00:00:00 2001 From: John Blum Date: Wed, 16 Apr 2014 21:11:54 -0700 Subject: [PATCH] Declared and implemented the findAll(:Sort) method from the SDC's PagingAndSortingRepository abstraction in SDG given GemFire supports ORDER BY in Queries. --- .../gemfire/repository/GemfireRepository.java | 19 +++++++++- .../support/SimpleGemfireRepository.java | 37 +++++++++++++++++- .../GemfireRepositoryFactoryUnitTests.java | 38 ++++++++++++++----- 3 files changed, 81 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/springframework/data/gemfire/repository/GemfireRepository.java b/src/main/java/org/springframework/data/gemfire/repository/GemfireRepository.java index ec4b935c..6e9d0551 100644 --- a/src/main/java/org/springframework/data/gemfire/repository/GemfireRepository.java +++ b/src/main/java/org/springframework/data/gemfire/repository/GemfireRepository.java @@ -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. - * + *

* @author Oliver Gierke + * @author John Blum + * @see java.io.Serializable + * @see org.springframework.data.repository.CrudRepository */ +@SuppressWarnings("unused") public interface GemfireRepository extends CrudRepository { + /** + * Returns all entities sorted by the given options. + *

+ * @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 findAll(Sort sort); + T save(Wrapper wrapper); + } diff --git a/src/main/java/org/springframework/data/gemfire/repository/support/SimpleGemfireRepository.java b/src/main/java/org/springframework/data/gemfire/repository/support/SimpleGemfireRepository.java index 0c4ae16e..25f2da5d 100644 --- a/src/main/java/org/springframework/data/gemfire/repository/support/SimpleGemfireRepository.java +++ b/src/main/java/org/springframework/data/gemfire/repository/support/SimpleGemfireRepository.java @@ -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 implements Gemf @Override public Collection findAll() { SelectResults results = template.find("select * from " + template.getRegion().getFullPath()); - return (Collection)results.asList(); + return results.asList(); } /* * (non-Javadoc) - * + * @see org.springframework.data.gemfire.repository.GemfireRepository.sor(:org.springframework.data.domain.Sort) + */ + @Override + public Iterable findAll(final Sort sort) { + SelectResults 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) diff --git a/src/test/java/org/springframework/data/gemfire/repository/support/GemfireRepositoryFactoryUnitTests.java b/src/test/java/org/springframework/data/gemfire/repository/support/GemfireRepositoryFactoryUnitTests.java index 92a1b80d..a2a90a66 100644 --- a/src/test/java/org/springframework/data/gemfire/repository/support/GemfireRepositoryFactoryUnitTests.java +++ b/src/test/java/org/springframework/data/gemfire/repository/support/GemfireRepositoryFactoryUnitTests.java @@ -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}. - * + *

* @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 { - } + + interface SamplePagingInterface extends Repository { + Page findAll(Pageable pageable); + } + + interface SampleSortingInterface extends Repository { + Iterable findAll(Sort sort); + } + }