Skip to content
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

Add dedicated RevisionSort to easily capture the desire to sort by revision number [DATACMNS-888] #1346

Closed
spring-projects-issues opened this issue Jul 26, 2016 · 1 comment
Assignees
Labels
type: enhancement

Comments

@spring-projects-issues
Copy link

@spring-projects-issues spring-projects-issues commented Jul 26, 2016

Oliver Drotbohm opened DATACMNS-888 and commented


Reference URL: spring-projects/spring-data-envers#55

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Sep 9, 2016

Matthias Müller commented

Just as a hint/suggestion, it would also be possible to sort by any number of attributes.
A implementation would look like this:

public Page<Revision<N, T>> findRevisions(ID id, Pageable pageable) {
	Class<T> type = entityInformation.getJavaType();
	AuditReader reader = AuditReaderFactory.get(entityManager);
	List<Number> allRevisionNumbers = reader.getRevisions(type, id);
	if (pageable.getOffset() > allRevisionNumbers.size()) {
		return new PageImpl<Revision<N, T>>(Collections.<Revision<N, T>> emptyList(), pageable, 0);
	}
	AuditQuery query = reader.createQuery().forRevisionsOfEntity(type, type.getName(), false, true);
	query.addProjection(AuditEntity.revisionNumber());
	query.add(AuditEntity.id().eq(id));
	query.setFirstResult(pageable.getOffset());
	query.setMaxResults(pageable.getPageSize());
	if (null == pageable.getSort()) {
		query.addOrder(AuditEntity.revisionNumber().asc());
	} else {
		for (Sort.Order order : pageable.getSort()) {
			AuditProperty<?> property = "revisionNumber".equals(order.getProperty())
				? AuditEntity.id() : AuditEntity.property(order.getProperty());
			boolean asc = Direction.ASC.equals(order.getDirection());
			query.addOrder(asc ? property.asc() : property.desc());
		}
	}
	List<N> revisionNumbers = query.getResultList();
	List<Revision<N, T>> content = new ArrayList<Revision<N, T>>();
	for (N revisionNumber : revisionNumbers) {
		content.add(findRevision(id, revisionNumber));
	}
	return new PageImpl<Revision<N, T>>(content, pageable, allRevisionNumbers.size());
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement
Projects
None yet
Development

No branches or pull requests

2 participants