Skip to content

Commit ed3d923

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 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 ed3d923

27 files changed

+112
-344
lines changed

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

Lines changed: 2 additions & 3 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
package org.springframework.data.couchbase.core;
1818

1919
import org.springframework.data.couchbase.core.mapping.event.AfterSaveEvent;
20-
import org.springframework.data.couchbase.core.mapping.event.ReactiveAfterSaveEvent;
20+
import org.springframework.data.couchbase.core.mapping.event.BeforeConvertEvent;
21+
import org.springframework.data.couchbase.core.mapping.event.BeforeSaveEvent;
2122
import reactor.core.publisher.Mono;
2223

2324
import org.slf4j.Logger;
@@ -33,8 +34,6 @@
3334
import org.springframework.data.couchbase.core.mapping.event.CouchbaseMappingEvent;
3435
import org.springframework.data.couchbase.core.mapping.event.ReactiveAfterConvertCallback;
3536
import org.springframework.data.couchbase.core.mapping.event.ReactiveBeforeConvertCallback;
36-
import org.springframework.data.couchbase.core.mapping.event.ReactiveBeforeConvertEvent;
37-
import org.springframework.data.couchbase.core.mapping.event.ReactiveBeforeSaveEvent;
3837
import org.springframework.data.couchbase.repository.support.MappingCouchbaseEntityInformation;
3938
import org.springframework.data.mapping.PersistentPropertyAccessor;
4039
import org.springframework.data.mapping.callback.EntityCallbacks;
@@ -68,13 +67,13 @@ public ReactiveCouchbaseTemplateSupport(final CouchbaseConverter converter,
6867

6968
@Override
7069
public Mono<CouchbaseDocument> encodeEntity(final Object entityToEncode) {
71-
return Mono.just(entityToEncode).doOnNext(entity -> maybeEmitEvent(new ReactiveBeforeConvertEvent<>(entity)))
70+
return Mono.just(entityToEncode).doOnNext(entity -> maybeEmitEvent(new BeforeConvertEvent<>(entity)))
7271
.flatMap(entity -> maybeCallBeforeConvert(entity, "")).map(maybeNewEntity -> {
7372
final CouchbaseDocument converted = new CouchbaseDocument();
7473
converter.write(maybeNewEntity, converted);
7574
return converted;
7675
}).flatMap(converted -> maybeCallAfterConvert(entityToEncode, converted, "").thenReturn(converted))
77-
.doOnNext(converted -> maybeEmitEvent(new ReactiveBeforeSaveEvent<>(entityToEncode, converted)));
76+
.doOnNext(converted -> maybeEmitEvent(new BeforeSaveEvent<>(entityToEncode, converted)));
7877
}
7978

8079
@Override
@@ -112,7 +111,7 @@ public Mono<Object> applyUpdatedCas(final Object entity, CouchbaseDocument conve
112111
} else {
113112
returnValue = entity;
114113
}
115-
maybeEmitEvent(new ReactiveAfterSaveEvent(returnValue, converted));
114+
maybeEmitEvent(new AfterSaveEvent(returnValue, converted));
116115
return returnValue;
117116
});
118117
}

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

Lines changed: 7 additions & 7 deletions
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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
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;
18+
import org.springframework.data.couchbase.core.mapping.event.AfterDeleteEvent;
19+
import org.springframework.data.couchbase.core.mapping.event.BeforeDeleteEvent;
2020
import reactor.core.publisher.Flux;
2121
import reactor.core.publisher.Mono;
2222

@@ -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 -> {
76-
template.support().maybeEmitEvent(new ReactiveBeforeDeleteEvent<>(r));
76+
template.support().maybeEmitEvent(new BeforeDeleteEvent<>(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 AfterDeleteEvent<>(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

Lines changed: 7 additions & 6 deletions
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

Lines changed: 9 additions & 9 deletions
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

Lines changed: 5 additions & 5 deletions
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

Lines changed: 7 additions & 7 deletions
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

Lines changed: 3 additions & 4 deletions
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

Lines changed: 13 additions & 21 deletions
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/ReactiveAfterDeleteEvent.java

Lines changed: 0 additions & 28 deletions
This file was deleted.

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

Lines changed: 0 additions & 30 deletions
This file was deleted.

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

Lines changed: 3 additions & 2 deletions
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
*

0 commit comments

Comments
 (0)