Skip to content

Commit 80f26fb

Browse files
committed
Return audited immutable entities from save
Signed-off-by: Artur Kalimullin <kalimullin@gmail.com>
1 parent 6ac0c34 commit 80f26fb

13 files changed

Lines changed: 114 additions & 25 deletions

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
*
4949
* @author Michael Reiche
5050
* @author Emilien Bevierre
51+
* @author Artur Kalimullin
5152
*/
5253
@Stability.Internal
5354
public abstract class AbstractTemplateSupport {
@@ -191,16 +192,18 @@ CouchbasePersistentEntity couldBePersistentEntity(Class<?> entityClass) {
191192
return null;
192193
}
193194

194-
public <T> T applyResultBase(T entity, CouchbaseDocument converted, Object id, long cas,
195-
Object txResultHolder, CouchbaseResourceHolder holder) {
196-
ConvertingPropertyAccessor<Object> accessor = getPropertyAccessor(entity);
195+
@SuppressWarnings("unchecked")
196+
public <T> T applyResultBase(CouchbaseDocument converted, long cas, Object txResultHolder,
197+
CouchbaseResourceHolder holder) {
198+
Object entityToWrite = converted.getEntityToWrite();
199+
ConvertingPropertyAccessor<Object> accessor = getPropertyAccessor(entityToWrite);
197200

198201
CouchbasePersistentEntity<?> persistentEntity = converter.getMappingContext()
199-
.getRequiredPersistentEntity(entity.getClass());
202+
.getRequiredPersistentEntity(entityToWrite.getClass());
200203

201-
CouchbasePersistentProperty idProperty = persistentEntity.getIdProperty();
204+
CouchbasePersistentProperty idProperty = persistentEntity.getIdProperty();
202205
if (idProperty != null) {
203-
accessor.setProperty(idProperty, id);
206+
accessor.setProperty(idProperty, converted.getId());
204207
}
205208

206209
CouchbasePersistentProperty versionProperty = persistentEntity.getVersionProperty();

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
* @author Jorge Rodriguez Martin
4141
* @author Carlos Espinaco
4242
* @author Emilien Bevierre
43+
* @author Artur Kalimullin
4344
* @since 3.0
4445
*/
4546
class CouchbaseTemplateSupport extends AbstractTemplateSupport implements ApplicationContextAware, TemplateSupport {
@@ -59,6 +60,7 @@ public CouchbaseDocument encodeEntity(final Object entityToEncode) {
5960
Object maybeNewEntity = maybeCallBeforeConvert(entityToEncode, "");
6061
final CouchbaseDocument converted = new CouchbaseDocument();
6162
converter.write(maybeNewEntity, converted);
63+
converted.setEntityToWrite(maybeNewEntity);
6264
maybeCallAfterConvert(entityToEncode, converted, "");
6365
maybeEmitEvent(new BeforeSaveEvent<>(entityToEncode, converted));
6466
return converted;
@@ -77,9 +79,9 @@ public <T> T decodeEntity(Object id, byte[] source, Long cas, Instant expiryTime
7779
}
7880

7981
@Override
80-
public <T> T applyResult(T entity, CouchbaseDocument converted, Object id, long cas,
81-
Object txResultHolder, CouchbaseResourceHolder holder) {
82-
return applyResultBase(entity, converted, id, cas, txResultHolder, holder);
82+
public <T> T applyResult(CouchbaseDocument converted, long cas, Object txResultHolder,
83+
CouchbaseResourceHolder holder) {
84+
return applyResultBase(converted, cas, txResultHolder, holder);
8385
}
8486

8587
@Override

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
* @author Carlos Espinaco
3030
* @author Michael Reiche
3131
* @author Emilien Bevierre
32+
* @author Artur Kalimullin
3233
* @since 4.2
3334
*/
3435
public class NonReactiveSupportWrapper implements ReactiveTemplateSupport {
@@ -59,9 +60,9 @@ public <T> Mono<T> decodeEntity(Object id, byte[] source, Long cas, Instant expi
5960
}
6061

6162
@Override
62-
public <T> Mono<T> applyResult(T entity, CouchbaseDocument converted, Object id, Long cas,
63-
Object txResultHolder, CouchbaseResourceHolder holder) {
64-
return Mono.fromSupplier(() -> support.applyResult(entity, converted, id, cas, txResultHolder, holder));
63+
public <T> Mono<T> applyResult(CouchbaseDocument converted, Long cas, Object txResultHolder,
64+
CouchbaseResourceHolder holder) {
65+
return Mono.fromSupplier(() -> support.applyResult(converted, cas, txResultHolder, holder));
6566
}
6667

6768

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
* @author Carlos Espinaco
4242
* @author Michael Reiche
4343
* @author Emilien Bevierre
44+
* @author Artur Kalimullin
4445
* @since 4.2
4546
*/
4647
class ReactiveCouchbaseTemplateSupport extends AbstractTemplateSupport
@@ -61,6 +62,7 @@ public Mono<CouchbaseDocument> encodeEntity(final Object entityToEncode) {
6162
.flatMap(entity -> maybeCallBeforeConvert(entity, "")).map(maybeNewEntity -> {
6263
final CouchbaseDocument converted = new CouchbaseDocument();
6364
converter.write(maybeNewEntity, converted);
65+
converted.setEntityToWrite(maybeNewEntity);
6466
return converted;
6567
}).flatMap(converted -> maybeCallAfterConvert(entityToEncode, converted, "").thenReturn(converted))
6668
.doOnNext(converted -> maybeEmitEvent(new BeforeSaveEvent<>(entityToEncode, converted)));
@@ -88,9 +90,9 @@ public <T> Mono<T> decodeEntity(Object id, byte[] source, Long cas, Instant expi
8890
}
8991

9092
@Override
91-
public <T> Mono<T> applyResult(T entity, CouchbaseDocument converted, Object id, Long cas,
92-
Object txResultHolder, CouchbaseResourceHolder holder) {
93-
return Mono.fromSupplier(() -> applyResultBase(entity, converted, id, cas, txResultHolder, holder));
93+
public <T> Mono<T> applyResult(CouchbaseDocument converted, Long cas, Object txResultHolder,
94+
CouchbaseResourceHolder holder) {
95+
return Mono.fromSupplier(() -> this.applyResultBase(converted, cas, txResultHolder, holder));
9496
}
9597

9698
@Override

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
* @author Michael Reiche
4747
* @author Tigran Babloyan
4848
* @author Emilien Bevierre
49+
* @author Artur Kalimullin
4950
*/
5051
public class ReactiveInsertByIdOperationSupport implements ReactiveInsertByIdOperation {
5152

@@ -107,7 +108,7 @@ public Mono<T> one(T object) {
107108
return collection.reactive()
108109
.insert(converted.getId().toString(), converted.export(),
109110
buildOptions(pArgs.getOptions(), converted))
110-
.flatMap(result -> this.support.applyResult(object, converted, converted.getId(), result.cas(),
111+
.flatMap(result -> this.support.<T>applyResult(converted, result.cas(),
111112
null, null));
112113
} else {
113114
rejectInvalidTransactionalOptions();
@@ -120,7 +121,7 @@ public Mono<T> one(T object) {
120121
template.getCouchbaseClientFactory().getCluster().environment().transcoder()
121122
.encode(converted.export()).encoded(),
122123
new SpanWrapper(span))
123-
.flatMap(result -> this.support.applyResult(object, converted, converted.getId(), result.cas(),
124+
.flatMap(result -> this.support.applyResult(converted, result.cas(),
124125
null, null));
125126
}
126127
})).onErrorMap(throwable -> {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
* {@link ReactiveMutateInByIdOperation} implementations for Couchbase.
4545
*
4646
* @author Tigran Babloyan
47+
* @author Artur Kalimullin
4748
*/
4849
public class ReactiveMutateInByIdOperationSupport implements ReactiveMutateInByIdOperation {
4950

@@ -119,7 +120,7 @@ public Mono<T> one(T object) {
119120
.flatMap(collection -> collection.reactive()
120121
.mutateIn(converted.getId().toString(), getMutations(converted), buildMutateInOptions(pArgs.getOptions(), object, converted))
121122
.flatMap(
122-
result -> support.applyResult(object, converted, converted.getId(), result.cas(), null, null)));
123+
result -> support.<T>applyResult(converted, result.cas(), null, null)));
123124
});
124125

125126
return reactiveEntity.onErrorMap(throwable -> {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
* @author Michael Reiche
5050
* @author Tigran Babloyan
5151
* @author Emilien Bevierre
52+
* @author Artur Kalimullin
5253
*/
5354
public class ReactiveReplaceByIdOperationSupport implements ReactiveReplaceByIdOperation {
5455

@@ -110,7 +111,7 @@ public Mono<T> one(T object) {
110111
return collection.reactive()
111112
.replace(converted.getId().toString(), converted.export(),
112113
buildReplaceOptions(pArgs.getOptions(), object, converted))
113-
.flatMap(result -> support.applyResult(object, converted, converted.getId(), result.cas(), null,
114+
.flatMap(result -> support.<T>applyResult(converted, result.cas(), null,
114115
null));
115116
} else {
116117
rejectInvalidTransactionalOptions();
@@ -137,7 +138,7 @@ public Mono<T> one(T object) {
137138
return ctx.replace(getResult, template.getCouchbaseClientFactory().getCluster().environment()
138139
.transcoder().encode(converted.export()).encoded(), new SpanWrapper(span));
139140
}).flatMap(
140-
result -> support.applyResult(object, converted, converted.getId(), result.cas(), null, null));
141+
result -> support.<T>applyResult(converted, result.cas(), null, null));
141142
}
142143
})).onErrorMap(throwable -> {
143144
if (throwable instanceof RuntimeException) {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
*
3030
* @author Michael Reiche
3131
* @author Emilien Bevierre
32+
* @author Artur Kalimullin
3233
*/
3334
public interface ReactiveTemplateSupport {
3435

@@ -43,8 +44,8 @@ default <T> Mono<T> decodeEntity(Object id, byte[] source, Long cas, Instant exp
4344
collection, txResultHolder, holder);
4445
}
4546

46-
<T> Mono<T> applyResult(T entity, CouchbaseDocument converted, Object id, Long cas,
47-
Object txResultHolder, CouchbaseResourceHolder holder);
47+
<T> Mono<T> applyResult(CouchbaseDocument converted, Long cas, Object txResultHolder,
48+
CouchbaseResourceHolder holder);
4849

4950
Long getCas(Object entity);
5051

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
*
3939
* @author Michael Reiche
4040
* @author Tigran Babloyan
41+
* @author Artur Kalimullin
4142
*/
4243
public class ReactiveUpsertByIdOperationSupport implements ReactiveUpsertByIdOperation {
4344

@@ -99,7 +100,7 @@ public Mono<T> one(T object) {
99100
.flatMap(collection -> collection.reactive()
100101
.upsert(converted.getId().toString(), converted.export(), buildUpsertOptions(pArgs.getOptions(), converted))
101102
.flatMap(
102-
result -> support.applyResult(object, converted, converted.getId(), result.cas(), null, null)));
103+
result -> support.<T>applyResult(converted, result.cas(), null, null)));
103104
});
104105

105106
return reactiveEntity.onErrorMap(throwable -> {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
/**
2626
* @author Michael Reiche
2727
* @author Emilien Bevierre
28+
* @author Artur Kalimullin
2829
*/
2930
public interface TemplateSupport {
3031

@@ -40,8 +41,7 @@ default <T> T decodeEntity(Object id, byte[] source, Long cas, Instant expiryTim
4041
collection, txResultHolder, holder);
4142
}
4243

43-
<T> T applyResult(T entity, CouchbaseDocument converted, Object id, long cas, Object txResultHolder,
44-
CouchbaseResourceHolder holder);
44+
<T> T applyResult(CouchbaseDocument converted, long cas, Object txResultHolder, CouchbaseResourceHolder holder);
4545

4646
Long getCas(Object entity);
4747

0 commit comments

Comments
 (0)