Skip to content

Commit

Permalink
Add an overloaded findAllVersions that accepts a sort
Browse files Browse the repository at this point in the history
- for backward compatibility

Fixes #663
  • Loading branch information
paulcwarren committed Nov 5, 2021
1 parent 56a3a2a commit 53a63e4
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.Serializable;
import java.util.List;

import org.springframework.data.domain.Sort;
import org.springframework.data.rest.core.annotation.RestResource;

public interface LockingAndVersioningRepository<T, ID extends Serializable> {
Expand Down Expand Up @@ -107,6 +108,16 @@ public interface LockingAndVersioningRepository<T, ID extends Serializable> {
*/
<S extends T> List<S> findAllVersions(S entity);

/**
* Returns a sorted list of all versions for the given entity
*
* @param <S> the type of entity
* @param entity the entity to find versions for
* @param sort the sort to apply
* @return list of entity versions
*/
<S extends T> List<S> findAllVersions(S entity, Sort sort);

/**
* Deletes a given entity version. The entity must be the head of the version list.
*
Expand Down Expand Up @@ -144,4 +155,5 @@ public interface LockingAndVersioningRepository<T, ID extends Serializable> {
* @return the working copy if it exists, or null
*/
<S extends T> S findWorkingCopy(S entity);

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
import org.apache.commons.text.StringSubstitutor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.content.commons.utils.BeanUtils;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.core.EntityInformation;
import org.springframework.security.core.Authentication;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.versions.AncestorId;
import org.springframework.versions.AncestorRootId;
import org.springframework.versions.LockOwner;
Expand Down Expand Up @@ -316,7 +318,33 @@ public <S extends T> List<S> findAllVersionsLatest(Class<S> entityClass) {
@Override
public <S extends T> List<S> findAllVersions(S entity) {

String sql = "select t from ${entityClass} t where t.${ancestorRootId} = " + getAncestralRootId(entity) + " order by t.${id} desc";
return this.findAllVersions(entity, Sort.unsorted());
}

@Override
public <S extends T> List<S> findAllVersions(S entity, Sort sort) {

StringBuilder builder = new StringBuilder();
if (sort.isSorted()) {
builder.append("order by ");

int i=0;
sort.forEach((property) -> {
if (i > 0) {
builder.append(",");
}
builder.append("t.");
builder.append(property.getProperty());
builder.append(" ");
builder.append(property.getDirection());
});
}

String sql = "select t from ${entityClass} t where t.${ancestorRootId} = " + getAncestralRootId(entity);

if (StringUtils.hasText(builder)) {
sql = sql + " " + builder.toString();
}

StringSubstitutor sub = new StringSubstitutor(getAttributeMap(entity.getClass()));
sql = sub.replace(sql);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import org.springframework.context.annotation.Import;
import org.springframework.core.io.ClassPathResource;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
Expand Down Expand Up @@ -406,8 +408,14 @@ public class JpaLockingAndVersioningRepositoryImplIT {
e2v2 = repo.unlock(e2v2);
});

It("should return the version series", () -> {
List<TestEntity> results = repo.findAllVersions(e1, Sort.by(Order.desc("id")));
assertThat(results.size(), is(2));
assertThat(results, Matchers.hasItems(hasProperty("xid", is(e1.getXid())), hasProperty("xid", is(e1v11.getXid()))));
});

It("should return the ordered version series", () -> {
List<TestEntity> results = repo.findAllVersions(e1);
List<TestEntity> results = repo.findAllVersions(e1, Sort.by(Order.desc("id")));
assertThat(results.size(), is(2));
assertThat(results, Matchers.hasItems(hasProperty("xid", is(e1.getXid())), hasProperty("xid", is(e1v11.getXid()))));

Expand Down

0 comments on commit 53a63e4

Please sign in to comment.