#47 - Populate the RevisionType with information from Envers.
For a given revision r the revision type may now be accessed using
r.getMetadata().getRevisionType()
Original pull request: #195.
This commit is contained in:
committed by
Mark Paluch
parent
90b8fcd0b3
commit
ee0613ed4d
@@ -16,6 +16,7 @@
|
||||
package org.springframework.data.envers.repository.support;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
import lombok.Value;
|
||||
@@ -40,9 +41,15 @@ import org.springframework.data.history.RevisionMetadata;
|
||||
* @author Jens Schauder
|
||||
*/
|
||||
@Value
|
||||
@AllArgsConstructor
|
||||
public class DefaultRevisionMetadata implements RevisionMetadata<Integer> {
|
||||
|
||||
private final @NonNull @Getter(AccessLevel.NONE) DefaultRevisionEntity entity;
|
||||
private final RevisionType revisionType;
|
||||
|
||||
public DefaultRevisionMetadata(DefaultRevisionEntity entity) {
|
||||
this(entity, RevisionType.UNKNOWN);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
@@ -79,6 +86,9 @@ public class DefaultRevisionMetadata implements RevisionMetadata<Integer> {
|
||||
return (T) entity;
|
||||
}
|
||||
|
||||
public RevisionType getRevisionType() {
|
||||
return revisionType;
|
||||
}
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
|
||||
@@ -90,12 +100,7 @@ public class DefaultRevisionMetadata implements RevisionMetadata<Integer> {
|
||||
}
|
||||
DefaultRevisionMetadata that = (DefaultRevisionMetadata) o;
|
||||
return getRevisionNumber().equals(that.getRevisionNumber())
|
||||
&& getRevisionInstant().equals(that.getRevisionInstant());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
|
||||
return Objects.hash(getRevisionNumber(), getRevisionInstant());
|
||||
&& getRevisionInstant().equals(that.getRevisionInstant())
|
||||
&& revisionType.equals(that.getRevisionType());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,6 +45,8 @@ import org.springframework.data.repository.history.support.RevisionEntityInforma
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import static org.springframework.data.history.RevisionMetadata.RevisionType.*;
|
||||
|
||||
/**
|
||||
* Repository implementation using Hibernate Envers to implement revision specific query methods.
|
||||
*
|
||||
@@ -181,7 +183,7 @@ public class EnversRevisionRepositoryImpl<T, ID, N extends Number & Comparable<N
|
||||
|
||||
private final T entity;
|
||||
private final Object metadata;
|
||||
private final RevisionType revisionType;
|
||||
private final RevisionMetadata.RevisionType revisionType;
|
||||
|
||||
QueryResult(Object[] data) {
|
||||
|
||||
@@ -196,13 +198,23 @@ public class EnversRevisionRepositoryImpl<T, ID, N extends Number & Comparable<N
|
||||
|
||||
entity = (T) data[0];
|
||||
metadata = data[1];
|
||||
revisionType = (RevisionType) data[2];
|
||||
revisionType = convertRevisionType(data[2]);
|
||||
}
|
||||
|
||||
private RevisionMetadata.RevisionType convertRevisionType(Object datum) {
|
||||
|
||||
switch ((RevisionType) datum){
|
||||
case ADD:return INSERT;
|
||||
case MOD:return UPDATE;
|
||||
case DEL:return DELETE;
|
||||
default:return UNKNOWN;
|
||||
}
|
||||
}
|
||||
|
||||
RevisionMetadata<N> createRevisionMetadata() {
|
||||
|
||||
return metadata instanceof DefaultRevisionEntity //
|
||||
? (RevisionMetadata<N>) new DefaultRevisionMetadata((DefaultRevisionEntity) metadata) //
|
||||
? (RevisionMetadata<N>) new DefaultRevisionMetadata((DefaultRevisionEntity) metadata, revisionType) //
|
||||
: new AnnotationRevisionMetadata<>(metadata, RevisionNumber.class, RevisionTimestamp.class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
package org.springframework.data.envers.repository.support;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.springframework.data.history.RevisionMetadata.RevisionType.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
@@ -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.RevisionMetadata;
|
||||
import org.springframework.data.history.RevisionSort;
|
||||
import org.springframework.data.history.Revisions;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
@@ -192,6 +194,32 @@ public class RepositoryIntegrationTests {
|
||||
.containsExactly(null, null);
|
||||
}
|
||||
|
||||
@Test // #47
|
||||
public void includesCorrectRevisionType() {
|
||||
|
||||
Country de = new Country();
|
||||
de.code = "de";
|
||||
de.name = "Deutschland";
|
||||
|
||||
countryRepository.save(de);
|
||||
|
||||
de.name = "Bundes Republik Deutschland";
|
||||
|
||||
countryRepository.save(de);
|
||||
|
||||
countryRepository.delete(de);
|
||||
|
||||
Revisions<Integer, Country> revisions = countryRepository.findRevisions(de.id);
|
||||
|
||||
assertThat(revisions) //
|
||||
.extracting(r -> r.getMetadata().getRevisionType()) //
|
||||
.containsExactly( //
|
||||
INSERT, //
|
||||
UPDATE, //
|
||||
DELETE //
|
||||
);
|
||||
}
|
||||
|
||||
@Test // #146
|
||||
public void shortCircuitingWhenOffsetIsToLarge() {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user