#31 - Polishing.

Tweaked implementation to not only lookup revision but also the entity in the given revision. Additional formatting and metadata polishing.
This commit is contained in:
Oliver Gierke
2015-05-14 18:18:18 +02:00
parent 6f076b5609
commit c14a3c1116
3 changed files with 65 additions and 33 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012 the original author or authors.
* Copyright 2012-2015 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.
@@ -27,10 +27,18 @@ import org.springframework.data.repository.history.RevisionRepository;
* go.
*
* @author Oliver Gierke
* @author Michael Igler
*/
@NoRepositoryBean
public interface EnversRevisionRepository<T, ID extends Serializable, N extends Number & Comparable<N>>
extends RevisionRepository<T, ID, N>, JpaRepository<T, ID> {
public interface EnversRevisionRepository<T, ID extends Serializable, N extends Number & Comparable<N>> extends
RevisionRepository<T, ID, N>, JpaRepository<T, ID> {
/**
* Returns the entity with the given ID in the given revision number.
*
* @param id must not be {@literal null}.
* @param revisionNumber must not be {@literal null}.
* @return
*/
Revision<N, T> findRevision(ID id, N revisionNumber);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012 the original author or authors.
* Copyright 2012-2015 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.
@@ -16,7 +16,12 @@
package org.springframework.data.envers.repository.support;
import java.io.Serializable;
import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.persistence.EntityManager;
@@ -36,7 +41,6 @@ import org.springframework.data.history.Revisions;
import org.springframework.data.jpa.repository.support.JpaEntityInformation;
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
import org.springframework.data.repository.core.EntityInformation;
import org.springframework.data.repository.history.RevisionRepository;
import org.springframework.data.repository.history.support.RevisionEntityInformation;
import org.springframework.util.Assert;
@@ -47,7 +51,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 RevisionRepository<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;
@@ -62,7 +67,7 @@ public class EnversRevisionRepositoryImpl<T, ID extends Serializable, N extends
* @param entityManager must not be {@literal null}.
*/
public EnversRevisionRepositoryImpl(JpaEntityInformation<T, ?> entityInformation,
RevisionEntityInformation revisionEntityInformation, EntityManager entityManager) {
RevisionEntityInformation revisionEntityInformation, EntityManager entityManager) {
super(entityInformation, entityManager);
@@ -98,12 +103,17 @@ public class EnversRevisionRepositoryImpl<T, ID extends Serializable, N extends
return new Revision<N, T>(metadata, reader.find(type, id, latestRevision));
}
@SuppressWarnings("unchecked")
/*
* (non-Javadoc)
* @see org.springframework.data.envers.repository.support.EnversRevisionRepository#findRevision(java.io.Serializable, java.lang.Number)
*/
@Override
public Revision<N, T> findRevision(ID id, N revisionNumber) {
AuditReader reader = AuditReaderFactory.get(entityManager);
Assert.notNull(id, "Identifier must not be null!");
Assert.notNull(revisionNumber, "Revision number must not be null!");
return getEntityForRevision(revisionNumber, id, reader);
return getEntityForRevision(revisionNumber, id, AuditReaderFactory.get(entityManager));
}
/*
@@ -133,7 +143,7 @@ public class EnversRevisionRepositoryImpl<T, ID extends Serializable, N extends
List<Number> revisionNumbers = reader.getRevisions(type, id);
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();
@@ -145,7 +155,6 @@ public class EnversRevisionRepositoryImpl<T, ID extends Serializable, N extends
return new PageImpl<Revision<N, T>>(revisions.getContent(), pageable, revisionNumbers.size());
}
/**
* Returns the entities in the given revisions for the entitiy with the given id.
*
@@ -179,15 +188,15 @@ public class EnversRevisionRepositoryImpl<T, ID extends Serializable, N extends
* @param reader
* @return
*/
@SuppressWarnings("unchecked")
private Revision<N, T> getEntityForRevision(N revisionNumber, ID id, AuditReader reader) {
Class<?> revisionEntityClass = revisionEntityInformation.getRevisionEntityClass();
Class<?> type = revisionEntityInformation.getRevisionEntityClass();
T revision = (T) reader.findRevision(revisionEntityClass, revisionNumber);
T revision = (T) reader.findRevision(type, revisionNumber);
Object entity = reader.find(entityInformation.getJavaType(), id, revisionNumber);
RevisionMetadata<N> metadata = (RevisionMetadata<N>) getRevisionMetadata(revision);
return new Revision<N, T>(metadata, revision);
return new Revision<N, T>((RevisionMetadata<N>) getRevisionMetadata(revision), (T) entity);
}
@SuppressWarnings("unchecked")

View File

@@ -20,8 +20,9 @@ import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Iterator;
import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -50,8 +51,6 @@ public class RepositoryIntegrationTest {
@Autowired LicenseRepository licenseRepository;
@Autowired CountryRepository countryRepository;
@Autowired EnversRevisionRepository enversRevisionRepository;
@Before
public void setUp() {
licenseRepository.deleteAll();
@@ -93,22 +92,38 @@ public class RepositoryIntegrationTest {
Page<Revision<Integer, License>> revisions = licenseRepository.findRevisions(license.id, new PageRequest(0, 10));
Revisions<Integer, License> wrapper = new Revisions<Integer, License>(revisions.getContent());
assertThat(wrapper.getLatestRevision(), is(revision));
List<Revision<Integer, Country>> revisionsDe = countryRepository.findRevisions(de.id).getContent();
for (Revision<Integer, Country> revisionDe: revisionsDe) {
System.out.println("revisionDe.getRevisionNumber(): " + revisionDe.getRevisionNumber());
System.out.println("revisionDe.getEntity().name: " + revisionDe.getEntity().name);
}
Revision<Integer, Country> originalCountryRevision = enversRevisionRepository.findRevision(de.id, 2);
Country originalCountry = originalCountryRevision.getEntity();
assertThat(originalCountry.name, is("Deutschland"));
}
@Test
public void returnsEmptyRevisionsForUnrevisionedEntity() {
assertThat(countryRepository.findRevisions(100L).getContent(), is(hasSize(0)));
}
/**
* @see #31
*/
@Test
public void returnsParticularRevisionForAnEntity() {
Country de = new Country();
de.code = "de";
de.name = "Deutschland";
countryRepository.save(de);
de.name = "Germany";
countryRepository.save(de);
Revisions<Integer, Country> revisions = countryRepository.findRevisions(de.id);
assertThat(revisions, is(Matchers.<Revision<Integer, Country>> iterableWithSize(2)));
Iterator<Revision<Integer, Country>> iterator = revisions.iterator();
Revision<Integer, Country> first = iterator.next();
Revision<Integer, Country> second = iterator.next();
assertThat(countryRepository.findRevision(de.id, first.getRevisionNumber()).getEntity().name, is("Deutschland"));
assertThat(countryRepository.findRevision(de.id, second.getRevisionNumber()).getEntity().name, is("Germany"));
}
}