#55 - Added support for changing the sort direction when accessing revisions.
We now make use of the RevisionSort type introduced in Spring Data Commons [0] to determine the order of the revisions to be read. [0] https://jira.spring.io/browse/DATACMNS-888
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2015 the original author or authors.
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -37,6 +37,7 @@ import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.history.AnnotationRevisionMetadata;
|
||||
import org.springframework.data.history.Revision;
|
||||
import org.springframework.data.history.RevisionMetadata;
|
||||
import org.springframework.data.history.RevisionSort;
|
||||
import org.springframework.data.history.Revisions;
|
||||
import org.springframework.data.jpa.repository.support.JpaEntityInformation;
|
||||
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
|
||||
@@ -51,8 +52,8 @@ import org.springframework.util.Assert;
|
||||
* @author Philipp Huegelmeyer
|
||||
* @author Michael Igler
|
||||
*/
|
||||
public class EnversRevisionRepositoryImpl<T, ID extends Serializable, N extends Number & Comparable<N>> extends
|
||||
SimpleJpaRepository<T, ID> implements EnversRevisionRepository<T, ID, N> {
|
||||
public class EnversRevisionRepositoryImpl<T, ID extends Serializable, N extends Number & Comparable<N>>
|
||||
extends SimpleJpaRepository<T, ID> implements EnversRevisionRepository<T, ID, N> {
|
||||
|
||||
private final EntityInformation<T, ?> entityInformation;
|
||||
private final RevisionEntityInformation revisionEntityInformation;
|
||||
@@ -127,8 +128,8 @@ public class EnversRevisionRepositoryImpl<T, ID extends Serializable, N extends
|
||||
AuditReader reader = AuditReaderFactory.get(entityManager);
|
||||
List<? extends Number> revisionNumbers = reader.getRevisions(type, id);
|
||||
|
||||
return revisionNumbers.isEmpty() ? new Revisions<N, T>(Collections.EMPTY_LIST) : getEntitiesForRevisions(
|
||||
(List<N>) revisionNumbers, id, reader);
|
||||
return revisionNumbers.isEmpty() ? new Revisions<N, T>(Collections.EMPTY_LIST)
|
||||
: getEntitiesForRevisions((List<N>) revisionNumbers, id, reader);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -141,9 +142,14 @@ public class EnversRevisionRepositoryImpl<T, ID extends Serializable, N extends
|
||||
Class<T> type = entityInformation.getJavaType();
|
||||
AuditReader reader = AuditReaderFactory.get(entityManager);
|
||||
List<Number> revisionNumbers = reader.getRevisions(type, id);
|
||||
boolean isDescending = RevisionSort.getRevisionDirection(pageable.getSort()).isDescending();
|
||||
|
||||
if (isDescending) {
|
||||
Collections.reverse(revisionNumbers);
|
||||
}
|
||||
|
||||
if (pageable.getOffset() > revisionNumbers.size()) {
|
||||
return new PageImpl<Revision<N, T>>(Collections.<Revision<N, T>> emptyList(), pageable, 0);
|
||||
return new PageImpl<Revision<N, T>>(Collections.<Revision<N, T>>emptyList(), pageable, 0);
|
||||
}
|
||||
|
||||
int upperBound = pageable.getOffset() + pageable.getPageSize();
|
||||
@@ -152,6 +158,8 @@ public class EnversRevisionRepositoryImpl<T, ID extends Serializable, N extends
|
||||
List<? extends Number> subList = revisionNumbers.subList(pageable.getOffset(), upperBound);
|
||||
Revisions<N, T> revisions = getEntitiesForRevisions((List<N>) subList, id, reader);
|
||||
|
||||
revisions = isDescending ? revisions.reverse() : revisions;
|
||||
|
||||
return new PageImpl<Revision<N, T>>(revisions.getContent(), pageable, revisionNumbers.size());
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2014 the original author or authors.
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -21,6 +21,7 @@ import static org.junit.Assert.*;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.Before;
|
||||
@@ -35,6 +36,7 @@ import org.springframework.data.envers.sample.CountryRepository;
|
||||
import org.springframework.data.envers.sample.License;
|
||||
import org.springframework.data.envers.sample.LicenseRepository;
|
||||
import org.springframework.data.history.Revision;
|
||||
import org.springframework.data.history.RevisionSort;
|
||||
import org.springframework.data.history.Revisions;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
@@ -117,7 +119,7 @@ public class RepositoryIntegrationTests {
|
||||
|
||||
Revisions<Integer, Country> revisions = countryRepository.findRevisions(de.id);
|
||||
|
||||
assertThat(revisions, is(Matchers.<Revision<Integer, Country>> iterableWithSize(2)));
|
||||
assertThat(revisions, is(Matchers.<Revision<Integer, Country>>iterableWithSize(2)));
|
||||
|
||||
Iterator<Revision<Integer, Country>> iterator = revisions.iterator();
|
||||
Revision<Integer, Country> first = iterator.next();
|
||||
@@ -126,4 +128,28 @@ public class RepositoryIntegrationTests {
|
||||
assertThat(countryRepository.findRevision(de.id, first.getRevisionNumber()).getEntity().name, is("Deutschland"));
|
||||
assertThat(countryRepository.findRevision(de.id, second.getRevisionNumber()).getEntity().name, is("Germany"));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see #55
|
||||
*/
|
||||
@Test
|
||||
public void considersRevisionNumberSortOrder() {
|
||||
|
||||
Country de = new Country();
|
||||
de.code = "de";
|
||||
de.name = "Deutschland";
|
||||
|
||||
countryRepository.save(de);
|
||||
|
||||
de.name = "Germany";
|
||||
|
||||
countryRepository.save(de);
|
||||
|
||||
List<Revision<Integer, Country>> content = countryRepository
|
||||
.findRevisions(de.id, new PageRequest(0, 10, RevisionSort.desc())).getContent();
|
||||
|
||||
assertThat(content, hasSize(2));
|
||||
assertThat(content.get(0).getRevisionNumber(), is(2));
|
||||
assertThat(content.get(1).getRevisionNumber(), is(1));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user