16
16
17
17
package org .springframework .data .couchbase .core ;
18
18
19
+ import org .springframework .data .couchbase .core .mapping .event .AfterSaveEvent ;
20
+ import org .springframework .data .couchbase .core .mapping .event .ReactiveAfterSaveEvent ;
21
+ import reactor .core .publisher .Mono ;
22
+
19
23
import org .slf4j .Logger ;
20
24
import org .slf4j .LoggerFactory ;
21
25
import org .springframework .beans .BeansException ;
26
30
import org .springframework .data .couchbase .core .mapping .CouchbaseDocument ;
27
31
import org .springframework .data .couchbase .core .mapping .CouchbasePersistentEntity ;
28
32
import org .springframework .data .couchbase .core .mapping .CouchbasePersistentProperty ;
29
- import org .springframework .data .couchbase .core .mapping .event .BeforeConvertEvent ;
30
- import org .springframework .data .couchbase .core .mapping .event .BeforeSaveEvent ;
31
33
import org .springframework .data .couchbase .core .mapping .event .CouchbaseMappingEvent ;
32
34
import org .springframework .data .couchbase .core .mapping .event .ReactiveAfterConvertCallback ;
33
35
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 ;
34
38
import org .springframework .data .couchbase .repository .support .MappingCouchbaseEntityInformation ;
35
39
import org .springframework .data .mapping .PersistentPropertyAccessor ;
36
40
import org .springframework .data .mapping .callback .EntityCallbacks ;
37
41
import org .springframework .data .mapping .callback .ReactiveEntityCallbacks ;
38
42
import org .springframework .data .mapping .context .MappingContext ;
39
43
import org .springframework .data .mapping .model .ConvertingPropertyAccessor ;
40
44
import org .springframework .util .Assert ;
41
- import reactor .core .publisher .Mono ;
42
45
43
46
/**
44
47
* Internal encode/decode support for {@link ReactiveCouchbaseTemplate}.
@@ -65,16 +68,13 @@ public ReactiveCouchbaseTemplateSupport(final CouchbaseConverter converter,
65
68
66
69
@ Override
67
70
public Mono <CouchbaseDocument > encodeEntity (final Object entityToEncode ) {
68
- return Mono .just (entityToEncode )
69
- .doOnNext (entity -> maybeEmitEvent (new BeforeConvertEvent <>(entity )))
70
- .flatMap (entity -> maybeCallBeforeConvert (entity , "" ))
71
- .map (maybeNewEntity -> {
71
+ return Mono .just (entityToEncode ).doOnNext (entity -> maybeEmitEvent (new ReactiveBeforeConvertEvent <>(entity )))
72
+ .flatMap (entity -> maybeCallBeforeConvert (entity , "" )).map (maybeNewEntity -> {
72
73
final CouchbaseDocument converted = new CouchbaseDocument ();
73
74
converter .write (maybeNewEntity , converted );
74
75
return converted ;
75
- })
76
- .flatMap (converted -> maybeCallAfterConvert (entityToEncode , converted , "" ).thenReturn (converted ))
77
- .doOnNext (converted -> maybeEmitEvent (new BeforeSaveEvent <>(entityToEncode , converted )));
76
+ }).flatMap (converted -> maybeCallAfterConvert (entityToEncode , converted , "" ).thenReturn (converted ))
77
+ .doOnNext (converted -> maybeEmitEvent (new ReactiveBeforeSaveEvent <>(entityToEncode , converted )));
78
78
}
79
79
80
80
@ Override
@@ -98,25 +98,31 @@ public <T> Mono<T> decodeEntity(String id, String source, long cas, Class<T> ent
98
98
}
99
99
100
100
@ Override
101
- public Mono <Object > applyUpdatedCas (final Object entity , final long cas ) {
101
+ public Mono <Object > applyUpdatedCas (final Object entity , CouchbaseDocument converted , final long cas ) {
102
102
return Mono .fromSupplier (() -> {
103
+ Object returnValue ;
103
104
final ConvertingPropertyAccessor <Object > accessor = getPropertyAccessor (entity );
104
- final CouchbasePersistentEntity <?> persistentEntity = mappingContext .getRequiredPersistentEntity (entity .getClass ());
105
+ final CouchbasePersistentEntity <?> persistentEntity = mappingContext
106
+ .getRequiredPersistentEntity (entity .getClass ());
105
107
final CouchbasePersistentProperty versionProperty = persistentEntity .getVersionProperty ();
106
108
107
109
if (versionProperty != null ) {
108
110
accessor .setProperty (versionProperty , cas );
109
- return accessor .getBean ();
111
+ returnValue = accessor .getBean ();
112
+ } else {
113
+ returnValue = entity ;
110
114
}
111
- return entity ;
115
+ maybeEmitEvent (new ReactiveAfterSaveEvent (returnValue , converted ));
116
+ return returnValue ;
112
117
});
113
118
}
114
119
115
120
@ Override
116
121
public Mono <Object > applyUpdatedId (final Object entity , Object id ) {
117
122
return Mono .fromSupplier (() -> {
118
123
final ConvertingPropertyAccessor <Object > accessor = getPropertyAccessor (entity );
119
- final CouchbasePersistentEntity <?> persistentEntity = mappingContext .getRequiredPersistentEntity (entity .getClass ());
124
+ final CouchbasePersistentEntity <?> persistentEntity = mappingContext
125
+ .getRequiredPersistentEntity (entity .getClass ());
120
126
final CouchbasePersistentProperty idProperty = persistentEntity .getIdProperty ();
121
127
122
128
if (idProperty != null ) {
@@ -130,8 +136,7 @@ public Mono<Object> applyUpdatedId(final Object entity, Object id) {
130
136
@ Override
131
137
public Long getCas (final Object entity ) {
132
138
final ConvertingPropertyAccessor <Object > accessor = getPropertyAccessor (entity );
133
- final CouchbasePersistentEntity <?> persistentEntity = mappingContext
134
- .getRequiredPersistentEntity (entity .getClass ());
139
+ final CouchbasePersistentEntity <?> persistentEntity = mappingContext .getRequiredPersistentEntity (entity .getClass ());
135
140
final CouchbasePersistentProperty versionProperty = persistentEntity .getVersionProperty ();
136
141
137
142
long cas = 0 ;
@@ -166,9 +171,9 @@ public void setApplicationContext(ApplicationContext applicationContext) throws
166
171
}
167
172
168
173
/**
169
- * Set the {@link ReactiveEntityCallbacks} instance to use when invoking {@link
170
- * org.springframework.data.mapping.callback.ReactiveEntityCallbacks callbacks} like the {@link
171
- * ReactiveBeforeConvertCallback}.
174
+ * Set the {@link ReactiveEntityCallbacks} instance to use when invoking
175
+ * {@link org.springframework.data.mapping.callback.ReactiveEntityCallbacks callbacks} like the
176
+ * {@link ReactiveBeforeConvertCallback}.
172
177
* <p/>
173
178
* Overrides potentially existing {@link EntityCallbacks}.
174
179
*
@@ -180,7 +185,7 @@ public void setReactiveEntityCallbacks(ReactiveEntityCallbacks reactiveEntityCal
180
185
this .reactiveEntityCallbacks = reactiveEntityCallbacks ;
181
186
}
182
187
183
- void maybeEmitEvent (CouchbaseMappingEvent <?> event ) {
188
+ public void maybeEmitEvent (CouchbaseMappingEvent <?> event ) {
184
189
if (canPublishEvent ()) {
185
190
try {
186
191
this .applicationContext .publishEvent (event );
0 commit comments