DATACOUCH-383 - RxJavaCouchbaseTemplate should return generated id when saving.

Seems we fixed this for the non-Rx version, but not the RxJava version.  Followed
same basic strategy which was used for version, added a simple test.
This commit is contained in:
David Kelly
2019-07-01 12:44:42 -07:00
parent 712348ab63
commit 98a4999f37
4 changed files with 151 additions and 3 deletions

View File

@@ -228,9 +228,20 @@ public class RxJavaCouchbaseTemplate implements RxJavaCouchbaseOperations {
break;
}
return persistFunction.call(toJsonDocument(objectToPersist), persistTo, replicateTo)
.flatMap(storedDoc -> Observable.just(storedDoc != null && storedDoc.cas() != 0
? setVersion(objectToPersist, storedDoc.cas())
: objectToPersist))
.flatMap(storedDoc -> {
if (storedDoc != null) {
if (storedDoc.cas() != 0) {
setVersion(objectToPersist, storedDoc.cas());
}
// Only set the id if the objectToPersist doesn't have it. That only
// happens when you have generated ids, and you are first persisting the
// document.
if (storedDoc.id() != null && getId(objectToPersist) == null) {
setId(objectToPersist, storedDoc.id());
}
}
return Observable.just(objectToPersist);
})
.onErrorResumeNext(e -> {
if (e instanceof DocumentAlreadyExistsException) {
throw new OptimisticLockingFailureException(persistType.springDataOperationName +
@@ -256,6 +267,10 @@ public class RxJavaCouchbaseTemplate implements RxJavaCouchbaseOperations {
return mappingContext.getRequiredPersistentEntity(object.getClass()).getVersionProperty();
}
private<T> CouchbasePersistentProperty idProperty(T object) {
return mappingContext.getRequiredPersistentEntity(object.getClass()).getIdProperty();
}
private <T> Long getVersion(T object) {
CouchbasePersistentProperty versionProperty = versionProperty(object);
@@ -278,6 +293,24 @@ public class RxJavaCouchbaseTemplate implements RxJavaCouchbaseOperations {
return accessor.getBean();
}
private<T> String getId(T object) {
CouchbasePersistentProperty idProperty = idProperty(object);
return idProperty == null
? null //
: getPropertyAccessor(object).getProperty(idProperty, String.class);
}
private<T> T setId(T object, String id) {
CouchbasePersistentProperty idProperty = idProperty(object);
if (idProperty == null) {
return object;
}
final ConvertingPropertyAccessor<T> accessor = getPropertyAccessor(object);
accessor.setProperty(idProperty, id);
return accessor.getBean();
}
private <T> Observable<T> doRemove(T objectToRemove, final PersistTo persistTo, final ReplicateTo replicateTo) {
if(objectToRemove instanceof String) {
return client.remove((String) objectToRemove, persistTo, replicateTo)