Skip to content

pageable sort by property #381

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.history.AnnotationRevisionMetadata;
import org.springframework.data.history.Revision;
import org.springframework.data.history.RevisionMetadata;
Expand All @@ -58,6 +59,7 @@
* @author Julien Millau
* @author Mark Paluch
* @author Sander Bylemans
* @author Niklas Loechte
*/
@Transactional(readOnly = true)
public class EnversRevisionRepositoryImpl<T, ID, N extends Number & Comparable<N>>
Expand Down Expand Up @@ -141,18 +143,31 @@ public Revisions<N, T> findRevisions(ID id) {
return Revisions.of(revisionList);
}


private AuditOrder mapRevisionSort(RevisionSort revisionSort) {
return RevisionSort.getRevisionDirection(revisionSort).isDescending() //
? AuditEntity.revisionNumber().desc() //
: AuditEntity.revisionNumber().asc();
}

private AuditOrder mapPropertySort(Sort sort) {
return sort.stream().findFirst().map(order -> order.getDirection().isAscending() ?
AuditEntity.property(order.getProperty()).asc() :
AuditEntity.property(order.getProperty()).desc())
.orElse(AuditEntity.revisionNumber().asc());
}

@SuppressWarnings("unchecked")
public Page<Revision<N, T>> findRevisions(ID id, Pageable pageable) {

AuditOrder sorting = RevisionSort.getRevisionDirection(pageable.getSort()).isDescending() //
? AuditEntity.revisionNumber().desc() //
: AuditEntity.revisionNumber().asc();
AuditOrder orderMapped = (pageable.getSort() instanceof RevisionSort) ?
mapRevisionSort((RevisionSort) pageable.getSort()) :
mapPropertySort(pageable.getSort());

List<Object[]> resultList = createBaseQuery(id) //
.addOrder(sorting) //
.setFirstResult((int) pageable.getOffset()) //
.setMaxResults(pageable.getPageSize()) //
.getResultList();
.addOrder(orderMapped) //
.setFirstResult((int) pageable.getOffset()) //
.setMaxResults(pageable.getPageSize()) //
.getResultList();

Long count = (Long) createBaseQuery(id) //
.addProjection(AuditEntity.revisionNumber().count()).getSingleResult();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static org.assertj.core.api.Assertions.*;
import static org.springframework.data.history.RevisionMetadata.RevisionType.*;

import java.time.Instant;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
Expand All @@ -31,6 +32,9 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.envers.Config;
import org.springframework.data.envers.sample.Country;
import org.springframework.data.envers.sample.CountryRepository;
Expand Down Expand Up @@ -245,6 +249,23 @@ void paginationWithEmptyResult() {
check(23L, 0, 0, 0);
}


@Test
void testSort_pageableByProperty() {
Country de = new Country();
de.code = "de";
de.name = "Deutschland";
de.timestamp = Instant.parse("2000-01-01T00:00:00Z");
countryRepository.save(de);
de.timestamp = Instant.parse("2000-01-04T00:01:00Z");
countryRepository.save(de);
de.timestamp = Instant.parse("2000-01-04T00:00:00Z");
countryRepository.save(de);

assertThat(countryRepository.findRevisions(de.id, PageRequest.of(0, 3, Sort.by("timestamp"))).map(Revision::getEntity).map(country -> country.timestamp).getContent())
.isSortedAccordingTo(Instant::compareTo);
}

void check(Long id, int page, int expectedSize, int expectedTotalSize) {

Page<Revision<Integer, Country>> revisions = countryRepository.findRevisions(id, PageRequest.of(page, 1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.springframework.data.envers.sample;

import java.time.Instant;

import javax.persistence.Entity;

import lombok.ToString;
Expand All @@ -32,5 +34,8 @@
public class Country extends AbstractEntity {

public String code;

public Instant timestamp;

public String name;
}