From f29756e5f8ee56a0c359344f2b4bce0b8f0398d6 Mon Sep 17 00:00:00 2001 From: Jens Schauder Date: Mon, 1 Jul 2019 14:48:12 +0200 Subject: [PATCH] DATACMNS-1550 - Added RevisionType to RevisionMetadata. --- .../history/AnnotationRevisionMetadata.java | 30 ++++++++++++++++++- .../data/history/RevisionMetadata.java | 28 +++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/springframework/data/history/AnnotationRevisionMetadata.java b/src/main/java/org/springframework/data/history/AnnotationRevisionMetadata.java index aea4a5a86..d8cdb1fe2 100755 --- a/src/main/java/org/springframework/data/history/AnnotationRevisionMetadata.java +++ b/src/main/java/org/springframework/data/history/AnnotationRevisionMetadata.java @@ -40,10 +40,12 @@ public class AnnotationRevisionMetadata> implem private final Object entity; private final Lazy> revisionNumber; private final Lazy> revisionDate; + private final RevisionType revisionType; /** * Creates a new {@link AnnotationRevisionMetadata} inspecting the given entity for the given annotations. If no - * annotations will be provided these values will not be looked up from the entity and return {@literal null}. + * annotations will be provided these values will not be looked up from the entity and return {@literal null}. The + * revisionType will be set to {@literal unknown} * * @param entity must not be {@literal null}. * @param revisionNumberAnnotation must not be {@literal null}. @@ -52,13 +54,31 @@ public class AnnotationRevisionMetadata> implem public AnnotationRevisionMetadata(Object entity, Class revisionNumberAnnotation, Class revisionTimeStampAnnotation) { + this(entity, revisionNumberAnnotation, revisionTimeStampAnnotation, RevisionType.UNKNOWN); + } + + /** + * Creates a new {@link AnnotationRevisionMetadata} inspecting the given entity for the given annotations. If no + * annotations will be provided these values will not be looked up from the entity and return {@literal null}. + * + * @param entity must not be {@literal null}. + * @param revisionNumberAnnotation must not be {@literal null}. + * @param revisionTimeStampAnnotation must not be {@literal null}. + * @param revisionType Must not be {@literal null}. + * @since 2.2.0 + */ + public AnnotationRevisionMetadata(Object entity, Class revisionNumberAnnotation, + Class revisionTimeStampAnnotation, RevisionType revisionType) { + Assert.notNull(entity, "Entity must not be null!"); Assert.notNull(revisionNumberAnnotation, "Revision number annotation must not be null!"); Assert.notNull(revisionTimeStampAnnotation, "Revision time stamp annotation must not be null!"); + Assert.notNull(revisionType, "Revision Type must not be null!"); this.entity = entity; this.revisionNumber = detectAnnotation(entity, revisionNumberAnnotation); this.revisionDate = detectAnnotation(entity, revisionTimeStampAnnotation); + this.revisionType = revisionType; } /* @@ -77,6 +97,14 @@ public class AnnotationRevisionMetadata> implem return revisionDate.get().map(AnnotationRevisionMetadata::convertToInstant); } + /* + * (non-Javadoc) + * @see org.springframework.data.history.RevisionMetadata#getRevisionDate() + */ + public RevisionType getRevisionType() { + return revisionType; + } + /* * (non-Javadoc) * @see org.springframework.data.repository.history.RevisionMetadata#getDelegate() diff --git a/src/main/java/org/springframework/data/history/RevisionMetadata.java b/src/main/java/org/springframework/data/history/RevisionMetadata.java index b9332e772..ce5392c7d 100755 --- a/src/main/java/org/springframework/data/history/RevisionMetadata.java +++ b/src/main/java/org/springframework/data/history/RevisionMetadata.java @@ -71,4 +71,32 @@ public interface RevisionMetadata> { * @return */ T getDelegate(); + + /** + * Returns the {@link RevisionType} of this change. If the {@link RevisionType} cannot be determined this method + * returns {@link RevisionType#UNKNOWN}. + * + * @return Guaranteed to be not {@literal null}. + * @since 2.2.0 + */ + default RevisionType getRevisionType() { + return RevisionType.UNKNOWN; + } + + /** + * The type of a {@link Revision}. + * + * @author Jens Schauder + * @since 2.2.0 + */ + enum RevisionType { + /** Fallback when the type of a revision cannot be determined. */ + UNKNOWN, + /** Creation of an instance */ + INSERT, + /** Change of an instance */ + UPDATE, + /** Deletion of an instance */ + DELETE + } }