Skip to content

Commit 2aba7f2

Browse files
committed
Add missing hooks for callbacks.
1) the previous change (#955) to CouchbaseAuditingRegister intended to register AuditingEntityCallback instead of AuditingEventListener but only changed the name that AuditingEventListener was registered with. Since AuditingEventListener does exactly the same thing as AuditingEntityCallback, it behaved as those AuditingEntityCallback was being used. 2) The two mechanisms - EntityCallback and EventListener both do more-or-less the same thing. EventListener is intendended to be read-only, therefore EntityCallback is necessary and should be used for modifying operations such as markAudited which modifies @createdby, @CreatedDate, @LastModifiedBy, @LostModifiedDate. Eventually EventListener will be deprecated. 3) Although the ReactiveAuditingEntityCallback mechanism is triggered only for reactive operations and the non-reactive AuditingEntityCallback is triggered only for non-reactive operations, the events are triggered for both. Since there was a desire to have distinct callbacks for reactive/non-reactive operations, I also made distinct events for reactive/non-reactive operations. However both the reactive and non-reactive event mechanisms are triggered for either reactive and non-reactive operations, so it is necessary for the mechanism to determine if it should act on the event. 4) Although there is an AbstractCouchbaseEventListener that is extended by LoggingEventListener and ValidatingEventListener, AuditingEventListener does not extend it (maybe it should). Closes #1074.
2 parents 383d169 + 2393b0e commit 2aba7f2

19 files changed

+99
-113
lines changed

src/main/java/org/springframework/data/couchbase/core/CouchbaseTemplateSupport.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package org.springframework.data.couchbase.core;
1919

20+
import org.slf4j.Logger;
21+
import org.slf4j.LoggerFactory;
2022
import org.springframework.beans.BeansException;
2123
import org.springframework.context.ApplicationContext;
2224
import org.springframework.context.ApplicationContextAware;
@@ -38,9 +40,6 @@
3840
import org.springframework.data.mapping.model.ConvertingPropertyAccessor;
3941
import org.springframework.util.Assert;
4042

41-
import org.slf4j.Logger;
42-
import org.slf4j.LoggerFactory;
43-
4443
/**
4544
* Internal encode/decode support for CouchbaseTemplate.
4645
*

src/main/java/org/springframework/data/couchbase/core/NonReactiveSupportWrapper.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
*/
1616
package org.springframework.data.couchbase.core;
1717

18-
import org.springframework.data.couchbase.core.mapping.CouchbaseDocument;
18+
import reactor.core.publisher.Mono;
1919

20+
import org.springframework.data.couchbase.core.mapping.CouchbaseDocument;
2021
import org.springframework.data.couchbase.core.mapping.event.CouchbaseMappingEvent;
21-
import reactor.core.publisher.Mono;
2222

2323
/**
2424
* Wrapper of {@link TemplateSupport} methods to adapt them to {@link ReactiveTemplateSupport}.

src/main/java/org/springframework/data/couchbase/core/ReactiveCouchbaseTemplateSupport.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.data.couchbase.core;
1818

19-
import org.springframework.data.couchbase.core.mapping.event.AfterSaveEvent;
20-
import org.springframework.data.couchbase.core.mapping.event.ReactiveAfterSaveEvent;
2119
import reactor.core.publisher.Mono;
2220

2321
import org.slf4j.Logger;
@@ -32,6 +30,7 @@
3230
import org.springframework.data.couchbase.core.mapping.CouchbasePersistentProperty;
3331
import org.springframework.data.couchbase.core.mapping.event.CouchbaseMappingEvent;
3432
import org.springframework.data.couchbase.core.mapping.event.ReactiveAfterConvertCallback;
33+
import org.springframework.data.couchbase.core.mapping.event.ReactiveAfterSaveEvent;
3534
import org.springframework.data.couchbase.core.mapping.event.ReactiveBeforeConvertCallback;
3635
import org.springframework.data.couchbase.core.mapping.event.ReactiveBeforeConvertEvent;
3736
import org.springframework.data.couchbase.core.mapping.event.ReactiveBeforeSaveEvent;

src/main/java/org/springframework/data/couchbase/core/ReactiveInsertByIdOperationSupport.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,14 @@ static class ReactiveInsertByIdSupport<T> implements ReactiveInsertById<T> {
7878

7979
@Override
8080
public Mono<T> one(T object) {
81-
PseudoArgs<InsertOptions> pArgs = new PseudoArgs(template, scope, collection,
82-
options != null ? options : InsertOptions.insertOptions());
83-
LOG.trace("statement: {} scope: {} collection: {} options: {}", "insertById", pArgs.getScope(),
84-
pArgs.getCollection(), pArgs.getOptions());
81+
PseudoArgs<InsertOptions> pArgs = new PseudoArgs(template, scope, collection,
82+
options != null ? options : InsertOptions.insertOptions());
83+
LOG.trace("statement: {} scope: {} collection: {} options: {}", "insertById", pArgs.getScope(),
84+
pArgs.getCollection(), pArgs.getOptions());
8585
return Mono.just(object).flatMap(support::encodeEntity)
86-
.flatMap(converted -> template.getCouchbaseClientFactory().withScope(pArgs.getScope())
87-
.getCollection(pArgs.getCollection()).reactive()
88-
.insert(converted.getId(), converted.export(), buildOptions(pArgs.getOptions(), converted))
86+
.flatMap(converted -> template.getCouchbaseClientFactory().withScope(pArgs.getScope())
87+
.getCollection(pArgs.getCollection()).reactive()
88+
.insert(converted.getId(), converted.export(), buildOptions(pArgs.getOptions(), converted))
8989
.flatMap(result -> support.applyUpdatedId(object, converted.getId())
9090
.flatMap(updatedObject -> support.applyUpdatedCas(updatedObject, converted, result.cas()))))
9191
.onErrorMap(throwable -> {

src/main/java/org/springframework/data/couchbase/core/ReactiveRemoveByIdOperationSupport.java

+15-15
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
*/
1616
package org.springframework.data.couchbase.core;
1717

18-
import org.springframework.data.couchbase.core.mapping.event.ReactiveAfterDeleteEvent;
19-
import org.springframework.data.couchbase.core.mapping.event.ReactiveBeforeDeleteEvent;
2018
import reactor.core.publisher.Flux;
2119
import reactor.core.publisher.Mono;
2220

2321
import java.util.Collection;
2422

23+
import org.springframework.data.couchbase.core.mapping.event.ReactiveAfterDeleteEvent;
24+
import org.springframework.data.couchbase.core.mapping.event.ReactiveBeforeDeleteEvent;
2525
import org.springframework.data.couchbase.core.support.PseudoArgs;
2626
import org.springframework.util.Assert;
2727

@@ -70,22 +70,22 @@ static class ReactiveRemoveByIdSupport implements ReactiveRemoveById {
7070

7171
@Override
7272
public Mono<RemoveResult> one(final String id) {
73-
PseudoArgs<RemoveOptions> pArgs = new PseudoArgs(template, scope, collection,
74-
options != null ? options : RemoveOptions.removeOptions());
73+
PseudoArgs<RemoveOptions> pArgs = new PseudoArgs(template, scope, collection,
74+
options != null ? options : RemoveOptions.removeOptions());
7575
return Mono.just(id).map(r -> {
7676
template.support().maybeEmitEvent(new ReactiveBeforeDeleteEvent<>(r));
7777
return r;
78-
}).flatMap(docId -> template.getCouchbaseClientFactory().withScope(pArgs.getScope())
79-
.getCollection(pArgs.getCollection()).reactive().remove(id, buildRemoveOptions(pArgs.getOptions())).map(r -> {
80-
template.support().maybeEmitEvent(new ReactiveAfterDeleteEvent<>(r));
81-
return RemoveResult.from(docId, r);
82-
})).onErrorMap(throwable -> {
83-
if (throwable instanceof RuntimeException) {
84-
return template.potentiallyConvertRuntimeException((RuntimeException) throwable);
85-
} else {
86-
return throwable;
87-
}
88-
});
78+
}).flatMap(docId -> template.getCouchbaseClientFactory().withScope(pArgs.getScope())
79+
.getCollection(pArgs.getCollection()).reactive().remove(id, buildRemoveOptions(pArgs.getOptions())).map(r -> {
80+
template.support().maybeEmitEvent(new ReactiveAfterDeleteEvent<>(r));
81+
return RemoveResult.from(docId, r);
82+
})).onErrorMap(throwable -> {
83+
if (throwable instanceof RuntimeException) {
84+
return template.potentiallyConvertRuntimeException((RuntimeException) throwable);
85+
} else {
86+
return throwable;
87+
}
88+
});
8989
}
9090

9191
@Override

src/main/java/org/springframework/data/couchbase/core/ReactiveReplaceByIdOperationSupport.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,14 @@ static class ReactiveReplaceByIdSupport<T> implements ReactiveReplaceById<T> {
7878

7979
@Override
8080
public Mono<T> one(T object) {
81-
PseudoArgs<ReplaceOptions> pArgs = new PseudoArgs<>(template, scope, collection,
82-
options != null ? options : ReplaceOptions.replaceOptions());
83-
LOG.trace("statement: {} pArgs: {}", "replaceById", pArgs);
81+
PseudoArgs<ReplaceOptions> pArgs = new PseudoArgs<>(template, scope, collection,
82+
options != null ? options : ReplaceOptions.replaceOptions());
83+
LOG.trace("statement: {} pArgs: {}", "replaceById", pArgs);
8484
return Mono.just(object).flatMap(support::encodeEntity)
85-
.flatMap(converted -> template.getCouchbaseClientFactory().withScope(pArgs.getScope())
86-
.getCollection(pArgs.getCollection()).reactive()
87-
.replace(converted.getId(), converted.export(), buildReplaceOptions(pArgs.getOptions(), object, converted))
85+
.flatMap(converted -> template.getCouchbaseClientFactory().withScope(pArgs.getScope())
86+
.getCollection(pArgs.getCollection()).reactive()
87+
.replace(converted.getId(), converted.export(),
88+
buildReplaceOptions(pArgs.getOptions(), object, converted))
8889
.flatMap(result -> support.applyUpdatedCas(object, converted, result.cas())))
8990
.onErrorMap(throwable -> {
9091
if (throwable instanceof RuntimeException) {

src/main/java/org/springframework/data/couchbase/core/ReactiveTemplateSupport.java

+9-9
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,24 @@
1515
*/
1616
package org.springframework.data.couchbase.core;
1717

18-
import org.springframework.data.couchbase.core.mapping.CouchbaseDocument;
18+
import reactor.core.publisher.Mono;
1919

20+
import org.springframework.data.couchbase.core.mapping.CouchbaseDocument;
2021
import org.springframework.data.couchbase.core.mapping.event.CouchbaseMappingEvent;
21-
import reactor.core.publisher.Mono;
2222

2323
public interface ReactiveTemplateSupport {
2424

25-
Mono<CouchbaseDocument> encodeEntity(Object entityToEncode);
25+
Mono<CouchbaseDocument> encodeEntity(Object entityToEncode);
2626

27-
<T> Mono<T> decodeEntity(String id, String source, long cas, Class<T> entityClass);
27+
<T> Mono<T> decodeEntity(String id, String source, long cas, Class<T> entityClass);
2828

29-
<T> Mono<T> applyUpdatedCas(T entity, CouchbaseDocument converted, long cas);
29+
<T> Mono<T> applyUpdatedCas(T entity, CouchbaseDocument converted, long cas);
3030

31-
<T> Mono<T> applyUpdatedId(T entity, Object id);
31+
<T> Mono<T> applyUpdatedId(T entity, Object id);
3232

33-
Long getCas(Object entity);
33+
Long getCas(Object entity);
3434

35-
String getJavaNameForEntity(Class<?> clazz);
35+
String getJavaNameForEntity(Class<?> clazz);
3636

37-
void maybeEmitEvent(CouchbaseMappingEvent<?> event);
37+
void maybeEmitEvent(CouchbaseMappingEvent<?> event);
3838
}

src/main/java/org/springframework/data/couchbase/core/ReactiveUpsertByIdOperationSupport.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,12 @@ static class ReactiveUpsertByIdSupport<T> implements ReactiveUpsertById<T> {
7575

7676
@Override
7777
public Mono<T> one(T object) {
78-
PseudoArgs<UpsertOptions> pArgs = new PseudoArgs<>(template, scope, collection,
79-
options != null ? options : UpsertOptions.upsertOptions());
78+
PseudoArgs<UpsertOptions> pArgs = new PseudoArgs<>(template, scope, collection,
79+
options != null ? options : UpsertOptions.upsertOptions());
8080
return Mono.just(object).flatMap(support::encodeEntity)
81-
.flatMap(converted -> template.getCouchbaseClientFactory().withScope(pArgs.getScope())
82-
.getCollection(pArgs.getCollection()).reactive()
83-
.upsert(converted.getId(), converted.export(), buildUpsertOptions(pArgs.getOptions(), converted))
81+
.flatMap(converted -> template.getCouchbaseClientFactory().withScope(pArgs.getScope())
82+
.getCollection(pArgs.getCollection()).reactive()
83+
.upsert(converted.getId(), converted.export(), buildUpsertOptions(pArgs.getOptions(), converted))
8484
.flatMap(result -> support.applyUpdatedId(object, converted.getId())
8585
.flatMap(updatedObject -> support.applyUpdatedCas(updatedObject, converted, result.cas()))))
8686
.onErrorMap(throwable -> {

src/main/java/org/springframework/data/couchbase/core/TemplateSupport.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@
2020

2121
public interface TemplateSupport {
2222

23-
CouchbaseDocument encodeEntity(Object entityToEncode);
23+
CouchbaseDocument encodeEntity(Object entityToEncode);
2424

25-
<T> T decodeEntity(String id, String source, long cas, Class<T> entityClass);
25+
<T> T decodeEntity(String id, String source, long cas, Class<T> entityClass);
2626

27-
<T> T applyUpdatedCas(T entity, CouchbaseDocument converted, long cas);
27+
<T> T applyUpdatedCas(T entity, CouchbaseDocument converted, long cas);
2828

29-
<T> T applyUpdatedId(T entity, Object id);
29+
<T> T applyUpdatedId(T entity, Object id);
3030

31-
long getCas(Object entity);
31+
long getCas(Object entity);
3232

33-
String getJavaNameForEntity(Class<?> clazz);
33+
String getJavaNameForEntity(Class<?> clazz);
3434

35-
void maybeEmitEvent(CouchbaseMappingEvent<?> event);
35+
void maybeEmitEvent(CouchbaseMappingEvent<?> event);
3636
}

src/main/java/org/springframework/data/couchbase/core/mapping/event/AuditingEntityCallback.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,8 @@ public AuditingEntityCallback(ObjectFactory<IsNewAwareAuditingHandler> auditingH
5454
*/
5555
@Override
5656
public Object onBeforeConvert(Object entity, String collection) {
57-
//LOG.debug("onBeforeConvert " + entity);
58-
return entity; // markAudited called in AuditingEventListener.onApplicationEvent()
59-
// auditingHandlerFactory.getObject().markAudited(entity);
57+
LOG.trace("onBeforeConvert {}", entity);
58+
return auditingHandlerFactory.getObject().markAudited(entity);
6059
}
6160

6261
/*
@@ -65,7 +64,7 @@ public Object onBeforeConvert(Object entity, String collection) {
6564
*/
6665
@Override
6766
public Object onAfterConvert(Object entity, CouchbaseDocument document, String collection) {
68-
//LOG.debug("onAfterConvert " + document);
67+
LOG.trace("onAfterConvert {}", document);
6968
return entity;
7069
}
7170

src/main/java/org/springframework/data/couchbase/core/mapping/event/AuditingEventListener.java

+13-21
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.data.couchbase.core.mapping.event;
1818

19-
import java.util.Optional;
20-
2119
import org.slf4j.Logger;
2220
import org.slf4j.LoggerFactory;
2321
import org.springframework.beans.factory.ObjectFactory;
@@ -62,26 +60,20 @@ public AuditingEventListener(ObjectFactory<IsNewAwareAuditingHandler> auditingHa
6260
*/
6361
@Override
6462
public void onApplicationEvent(CouchbaseMappingEvent<Object> event) {
65-
if (event instanceof BeforeConvertEvent) {
66-
Optional.ofNullable(event.getSource())//
67-
.ifPresent(it -> auditingHandlerFactory.getObject().markAudited(it));
68-
// LOG.info(event.getClass().getSimpleName() + " " + event);
69-
}
70-
if (event instanceof BeforeSaveEvent) {
71-
// LOG.info(event.getClass().getSimpleName() + " " + event);
72-
}
73-
if (event instanceof AfterSaveEvent) {
74-
// LOG.info(event.getClass().getSimpleName() + " " + event);
75-
}
76-
if (event instanceof BeforeDeleteEvent) {
77-
// LOG.info(event.getClass().getSimpleName() + " " + event);
78-
}
79-
if (event instanceof AfterDeleteEvent) {
80-
// LOG.info(event.getClass().getSimpleName() + " " + event);
81-
}
8263
if (!event.getClass().getSimpleName().startsWith("Reactive")) {
83-
if (LOG.isDebugEnabled()) {
84-
LOG.debug(event.getClass().getSimpleName() + " " + event.getSource());
64+
LOG.trace("{} {}", event.getClass().getSimpleName(), event.getSource());
65+
if (event instanceof BeforeConvertEvent) {
66+
// markAudited called in AuditingEventCallback.onBeforeConvert()
67+
// Optional.ofNullable(event.getSource())//
68+
// .ifPresent(it -> auditingHandlerFactory.getObject().markAudited(it));
69+
} else if (event instanceof BeforeSaveEvent) {
70+
;
71+
} else if (event instanceof AfterSaveEvent) {
72+
;
73+
} else if (event instanceof BeforeDeleteEvent) {
74+
;
75+
} else if (event instanceof AfterDeleteEvent) {
76+
;
8577
}
8678
}
8779
}

src/main/java/org/springframework/data/couchbase/core/mapping/event/ReactiveAuditingEntityCallback.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public ReactiveAuditingEntityCallback(final ObjectFactory<ReactiveIsNewAwareAudi
6363
*/
6464
@Override
6565
public Publisher<Object> onBeforeConvert(final Object entity, final String collection) {
66-
LOG.debug("onBeforeConvert " + entity.toString());
66+
LOG.trace("onBeforeConvert {}", entity.toString());
6767
return this.auditingHandlerFactory.getObject().markAudited(entity);
6868
}
6969

@@ -76,9 +76,10 @@ public Publisher<Object> onBeforeConvert(final Object entity, final String colle
7676
*/
7777
@Override
7878
public Publisher<Object> onAfterConvert(Object entity, CouchbaseDocument document, String collection) {
79-
LOG.debug("onAfterConvert " + document.toString());
79+
LOG.trace("onAfterConvert {}", document.toString());
8080
return Mono.just(entity);
8181
}
82+
8283
/*
8384
* (non-Javadoc)
8485
*

src/main/java/org/springframework/data/couchbase/core/mapping/event/ReactiveAuditingEventListener.java

+13-22
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,11 @@
1616

1717
package org.springframework.data.couchbase.core.mapping.event;
1818

19-
import java.util.Optional;
20-
2119
import org.slf4j.Logger;
2220
import org.slf4j.LoggerFactory;
2321
import org.springframework.beans.factory.ObjectFactory;
2422
import org.springframework.context.ApplicationListener;
2523
import org.springframework.data.auditing.AuditingHandler;
26-
import org.springframework.data.auditing.IsNewAwareAuditingHandler;
2724
import org.springframework.data.auditing.ReactiveIsNewAwareAuditingHandler;
2825
import org.springframework.data.mapping.context.MappingContext;
2926
import org.springframework.util.Assert;
@@ -60,26 +57,20 @@ public ReactiveAuditingEventListener(ObjectFactory<ReactiveIsNewAwareAuditingHan
6057
*/
6158
@Override
6259
public void onApplicationEvent(CouchbaseMappingEvent<Object> event) {
63-
if (event instanceof ReactiveBeforeConvertEvent) {
64-
Optional.ofNullable(event.getSource())//
65-
.ifPresent(it -> auditingHandlerFactory.getObject().markAudited(it));
66-
// LOG.info(event.getClass().getSimpleName() + " " + event);
67-
}
68-
if (event instanceof ReactiveBeforeSaveEvent) {
69-
// LOG.info(event.getClass().getSimpleName() + " " + event);
70-
}
71-
if (event instanceof ReactiveAfterSaveEvent) {
72-
// LOG.info(event.getClass().getSimpleName() + " " + event);
73-
}
74-
if (event instanceof ReactiveBeforeDeleteEvent) {
75-
// LOG.info(event.getClass().getSimpleName() + " " + event);
76-
}
77-
if (event instanceof ReactiveAfterDeleteEvent) {
78-
// LOG.info(event.getClass().getSimpleName() + " " + event);
79-
}
8060
if (event.getClass().getSimpleName().startsWith("Reactive")) {
81-
if (LOG.isDebugEnabled()) {
82-
LOG.debug(event.getClass().getSimpleName() + " " + event.getSource());
61+
LOG.trace("{} {}", event.getClass().getSimpleName(), event.getSource());
62+
if (event instanceof ReactiveBeforeConvertEvent) {
63+
// markAudited called by ReactiveAuditingEntityCallback
64+
// Optional.ofNullable(event.getSource())
65+
// .ifPresent(it -> auditingHandlerFactory.getObject().markAudited(it));
66+
} else if (event instanceof ReactiveBeforeSaveEvent) {
67+
;
68+
} else if (event instanceof ReactiveAfterSaveEvent) {
69+
;
70+
} else if (event instanceof ReactiveBeforeDeleteEvent) {
71+
;
72+
} else if (event instanceof ReactiveAfterDeleteEvent) {
73+
;
8374
}
8475
}
8576
}

0 commit comments

Comments
 (0)