Skip to content

Commit 9af0782

Browse files
jorgerodmikereiche
andcommitted
Add support for reactive auditing and ReactiveEntityCallbacks.
Add support for reactive auditing and ReactiveEntityCallbacks. Also, adapt CouchbaseAuditingRegistrar for support AuditingEntityCallback. Closes #955. Original pull request: #1102. Co-authored-by: Carlos Espinado <carlosemart> Co-authored-by: mikereiche <[email protected]>
1 parent 5a04711 commit 9af0782

34 files changed

+1031
-97
lines changed

src/main/java/org/springframework/data/couchbase/config/BeanNames.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors
2+
* Copyright 2012-2021 the original author or authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@
2424
* @author Michael Nitschinger
2525
* @author Simon Baslé
2626
* @author Michael Reiche
27+
* @author Jorge Rodríguez Martín
2728
*/
2829
public class BeanNames {
2930

@@ -53,4 +54,9 @@ public class BeanNames {
5354
* The name for the bean that will handle audit trail marking of entities.
5455
*/
5556
public static final String COUCHBASE_AUDITING_HANDLER = "couchbaseAuditingHandler";
57+
58+
/**
59+
* The name for the bean that will handle reactive audit trail marking of entities.
60+
*/
61+
public static final String REACTIVE_COUCHBASE_AUDITING_HANDLER = "reactiveCouchbaseAuditingHandler";
5662
}

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors
2+
* Copyright 2012-2021 the original author or authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -180,4 +180,8 @@ private void prepareIndexCreator(final ApplicationContext context) {
180180
}
181181
}
182182
}
183+
184+
TemplateSupport support() {
185+
return templateSupport;
186+
}
183187
}

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

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors
2+
/*
3+
* Copyright 2012-2021 the original author or authors
34
*
45
* Licensed under the Apache License, Version 2.0 (the "License");
56
* you may not use this file except in compliance with the License.
@@ -45,9 +46,10 @@
4546
* @author Michael Nitschinger
4647
* @author Michael Reiche
4748
* @author Jorge Rodriguez Martin
49+
* @author Carlos Espinaco
4850
* @since 3.0
4951
*/
50-
class CouchbaseTemplateSupport implements ApplicationContextAware {
52+
class CouchbaseTemplateSupport implements ApplicationContextAware, TemplateSupport {
5153

5254
private static final Logger LOG = LoggerFactory.getLogger(CouchbaseTemplateSupport.class);
5355

@@ -63,6 +65,7 @@ public CouchbaseTemplateSupport(final CouchbaseConverter converter, final Transl
6365
this.translationService = translationService;
6466
}
6567

68+
@Override
6669
public CouchbaseDocument encodeEntity(final Object entityToEncode) {
6770
maybeEmitEvent(new BeforeConvertEvent<>(entityToEncode));
6871
Object maybeNewEntity = maybeCallBeforeConvert(entityToEncode, "");
@@ -73,6 +76,7 @@ public CouchbaseDocument encodeEntity(final Object entityToEncode) {
7376
return converted;
7477
}
7578

79+
@Override
7680
public <T> T decodeEntity(String id, String source, long cas, Class<T> entityClass) {
7781
final CouchbaseDocument converted = new CouchbaseDocument(id);
7882
converted.setId(id);
@@ -90,6 +94,7 @@ public <T> T decodeEntity(String id, String source, long cas, Class<T> entityCla
9094
return accessor.getBean();
9195
}
9296

97+
@Override
9398
public Object applyUpdatedCas(final Object entity, final long cas) {
9499
final ConvertingPropertyAccessor<Object> accessor = getPropertyAccessor(entity);
95100
final CouchbasePersistentEntity<?> persistentEntity = mappingContext.getRequiredPersistentEntity(entity.getClass());
@@ -102,6 +107,7 @@ public Object applyUpdatedCas(final Object entity, final long cas) {
102107
return entity;
103108
}
104109

110+
@Override
105111
public Object applyUpdatedId(final Object entity, Object id) {
106112
final ConvertingPropertyAccessor<Object> accessor = getPropertyAccessor(entity);
107113
final CouchbasePersistentEntity<?> persistentEntity = mappingContext.getRequiredPersistentEntity(entity.getClass());
@@ -114,6 +120,7 @@ public Object applyUpdatedId(final Object entity, Object id) {
114120
return entity;
115121
}
116122

123+
@Override
117124
public long getCas(final Object entity) {
118125
final ConvertingPropertyAccessor<Object> accessor = getPropertyAccessor(entity);
119126
final CouchbasePersistentEntity<?> persistentEntity = mappingContext.getRequiredPersistentEntity(entity.getClass());
@@ -129,6 +136,7 @@ public long getCas(final Object entity) {
129136
return cas;
130137
}
131138

139+
@Override
132140
public String getJavaNameForEntity(final Class<?> clazz) {
133141
final CouchbasePersistentEntity<?> persistentEntity = mappingContext.getRequiredPersistentEntity(clazz);
134142
MappingCouchbaseEntityInformation<?, Object> info = new MappingCouchbaseEntityInformation<>(persistentEntity);

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors
2+
* Copyright 2012-2021 the original author or authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -53,7 +53,7 @@ static class ExecutableFindByAnalyticsSupport<T> implements ExecutableFindByAnal
5353
this.domainType = domainType;
5454
this.query = query;
5555
this.reactiveSupport = new ReactiveFindByAnalyticsSupport<>(template.reactive(), domainType, query,
56-
scanConsistency);
56+
scanConsistency, new NonReactiveSupportWrapper(template.support()));
5757
this.scanConsistency = scanConsistency;
5858
}
5959

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors
2+
* Copyright 2012-2021 the original author or authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -48,7 +48,7 @@ static class ExecutableFindByIdSupport<T> implements ExecutableFindById<T> {
4848
this.domainType = domainType;
4949
this.collection = collection;
5050
this.fields = fields;
51-
this.reactiveSupport = new ReactiveFindByIdSupport<>(template.reactive(), domainType, collection, fields);
51+
this.reactiveSupport = new ReactiveFindByIdSupport<>(template.reactive(), domainType, collection, fields, new NonReactiveSupportWrapper(template.support()));
5252
}
5353

5454
@Override

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors
2+
* Copyright 2012-2021 the original author or authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -65,7 +65,7 @@ static class ExecutableFindByQuerySupport<T> implements ExecutableFindByQuery<T>
6565
this.returnType = returnType;
6666
this.query = query;
6767
this.reactiveSupport = new ReactiveFindByQuerySupport<T>(template.reactive(), domainType, returnType, query,
68-
scanConsistency, collection, distinctFields);
68+
scanConsistency, collection, distinctFields, new NonReactiveSupportWrapper(template.support()));
6969
this.scanConsistency = scanConsistency;
7070
this.collection = collection;
7171
this.distinctFields = distinctFields;

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors
2+
* Copyright 2012-2021 the original author or authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -48,7 +48,7 @@ static class ExecutableFindFromReplicasByIdSupport<T> implements ExecutableFindF
4848
this.collection = collection;
4949
this.returnType = returnType;
5050
this.reactiveSupport = new ReactiveFindFromReplicasByIdSupport<>(template.reactive(), domainType, returnType,
51-
collection);
51+
collection, new NonReactiveSupportWrapper(template.support()));
5252
}
5353

5454
@Override

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors
2+
* Copyright 2012-2021 the original author or authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -62,7 +62,7 @@ static class ExecutableInsertByIdSupport<T> implements ExecutableInsertById<T> {
6262
this.durabilityLevel = durabilityLevel;
6363
this.expiry = expiry;
6464
this.reactiveSupport = new ReactiveInsertByIdSupport<>(template.reactive(), domainType, collection, persistTo,
65-
replicateTo, durabilityLevel, expiry);
65+
replicateTo, durabilityLevel, expiry, new NonReactiveSupportWrapper(template.support()));
6666
}
6767

6868
@Override

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors
2+
* Copyright 2012-2021 the original author or authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -62,7 +62,7 @@ static class ExecutableReplaceByIdSupport<T> implements ExecutableReplaceById<T>
6262
this.durabilityLevel = durabilityLevel;
6363
this.expiry = expiry;
6464
this.reactiveSupport = new ReactiveReplaceByIdSupport<>(template.reactive(),
65-
domainType, collection, persistTo, replicateTo, durabilityLevel, expiry);
65+
domainType, collection, persistTo, replicateTo, durabilityLevel, expiry, new NonReactiveSupportWrapper(template.support()));
6666
}
6767

6868
@Override

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors
2+
* Copyright 2012-2021 the original author or authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -62,7 +62,7 @@ static class ExecutableUpsertByIdSupport<T> implements ExecutableUpsertById<T> {
6262
this.durabilityLevel = durabilityLevel;
6363
this.expiry = expiry;
6464
this.reactiveSupport = new ReactiveUpsertByIdSupport<>(template.reactive(),
65-
domainType, collection, persistTo, replicateTo, durabilityLevel, expiry);
65+
domainType, collection, persistTo, replicateTo, durabilityLevel, expiry, new NonReactiveSupportWrapper(template.support()));
6666
}
6767

6868
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.couchbase.core;
17+
18+
import org.springframework.data.couchbase.core.mapping.CouchbaseDocument;
19+
20+
import reactor.core.publisher.Mono;
21+
22+
/**
23+
* Wrapper of {@link TemplateSupport} methods to adapt them to {@link ReactiveTemplateSupport}.
24+
*
25+
* @author Carlos Espinaco
26+
* @since 4.2
27+
*/
28+
public class NonReactiveSupportWrapper implements ReactiveTemplateSupport {
29+
30+
private final TemplateSupport support;
31+
32+
public NonReactiveSupportWrapper(TemplateSupport support) {
33+
this.support = support;
34+
}
35+
36+
@Override
37+
public Mono<CouchbaseDocument> encodeEntity(Object entityToEncode) {
38+
return Mono.fromSupplier(() -> support.encodeEntity(entityToEncode));
39+
}
40+
41+
@Override
42+
public <T> Mono<T> decodeEntity(String id, String source, long cas, Class<T> entityClass) {
43+
return Mono.fromSupplier(() -> support.decodeEntity(id, source, cas, entityClass));
44+
}
45+
46+
@Override
47+
public Mono<Object> applyUpdatedCas(Object entity, long cas) {
48+
return Mono.fromSupplier(() -> support.applyUpdatedCas(entity, cas));
49+
}
50+
51+
@Override
52+
public Mono<Object> applyUpdatedId(Object entity, Object id) {
53+
return Mono.fromSupplier(() -> support.applyUpdatedId(entity, id));
54+
}
55+
56+
@Override
57+
public Long getCas(Object entity) {
58+
return support.getCas(entity);
59+
}
60+
61+
@Override
62+
public String getJavaNameForEntity(Class<?> clazz) {
63+
return support.getJavaNameForEntity(clazz);
64+
}
65+
}

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors
2+
* Copyright 2012-2021 the original author or authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,13 +34,14 @@
3434
* @author Michael Nitschinger
3535
* @author Michael Reiche
3636
* @author Jorge Rodriguez Martin
37+
* @author Carlos Espinaco
3738
*/
3839
public class ReactiveCouchbaseTemplate implements ReactiveCouchbaseOperations, ApplicationContextAware {
3940

4041
private final CouchbaseClientFactory clientFactory;
4142
private final CouchbaseConverter converter;
4243
private final PersistenceExceptionTranslator exceptionTranslator;
43-
private final CouchbaseTemplateSupport templateSupport;
44+
private final ReactiveCouchbaseTemplateSupport templateSupport;
4445

4546
public ReactiveCouchbaseTemplate(final CouchbaseClientFactory clientFactory, final CouchbaseConverter converter) {
4647
this(clientFactory, converter, new JacksonTranslationService());
@@ -51,7 +52,7 @@ public ReactiveCouchbaseTemplate(final CouchbaseClientFactory clientFactory, fin
5152
this.clientFactory = clientFactory;
5253
this.converter = converter;
5354
this.exceptionTranslator = clientFactory.getExceptionTranslator();
54-
this.templateSupport = new CouchbaseTemplateSupport(converter, translationService);
55+
this.templateSupport = new ReactiveCouchbaseTemplateSupport(converter, translationService);
5556
}
5657

5758
@Override
@@ -134,7 +135,7 @@ public CouchbaseConverter getConverter() {
134135
return converter;
135136
}
136137

137-
CouchbaseTemplateSupport support() {
138+
ReactiveTemplateSupport support() {
138139
return templateSupport;
139140
}
140141

0 commit comments

Comments
 (0)