|
22 | 22 | import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
|
23 | 23 | import org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter;
|
24 | 24 | import org.springframework.data.elasticsearch.core.document.SearchDocumentResponse;
|
| 25 | +import org.springframework.data.elasticsearch.core.event.AfterSaveCallback; |
25 | 26 | import org.springframework.data.elasticsearch.core.event.BeforeConvertCallback;
|
26 | 27 | import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
|
27 | 28 | import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
|
|
45 | 46 | *
|
46 | 47 | * @author Sascha Woo
|
47 | 48 | * @author Peter-Josef Meisch
|
| 49 | + * @author Roman Puchkovskiy |
48 | 50 | */
|
49 | 51 | public abstract class AbstractElasticsearchTemplate implements ElasticsearchOperations, ApplicationContextAware {
|
50 | 52 |
|
@@ -117,8 +119,13 @@ public <T> T save(T entity, IndexCoordinates index) {
|
117 | 119 | Assert.notNull(entity, "entity must not be null");
|
118 | 120 | Assert.notNull(index, "index must not be null");
|
119 | 121 |
|
120 |
| - index(getIndexQuery(entity), index); |
121 |
| - return entity; |
| 122 | + IndexQuery query = getIndexQuery(entity); |
| 123 | + index(query, index); |
| 124 | + |
| 125 | + // suppressing because it's either entity itself or something of a correct type returned by an entity callback |
| 126 | + @SuppressWarnings("unchecked") |
| 127 | + T castResult = (T) query.getObject(); |
| 128 | + return castResult; |
122 | 129 | }
|
123 | 130 |
|
124 | 131 | @Override
|
@@ -151,7 +158,10 @@ public <T> Iterable<T> save(Iterable<T> entities, IndexCoordinates index) {
|
151 | 158 | });
|
152 | 159 | }
|
153 | 160 |
|
154 |
| - return entities; |
| 161 | + return indexQueries.stream() |
| 162 | + .map(IndexQuery::getObject) |
| 163 | + .map(entity -> (T) entity) |
| 164 | + .collect(Collectors.toList()); |
155 | 165 | }
|
156 | 166 |
|
157 | 167 | @Override
|
@@ -455,11 +465,39 @@ protected void maybeCallbackBeforeConvertWithQuery(Object query) {
|
455 | 465 | }
|
456 | 466 |
|
457 | 467 | // this can be called with either a List<IndexQuery> or a List<UpdateQuery>; these query classes
|
458 |
| - // don't have a common bas class, therefore the List<?> argument |
| 468 | + // don't have a common base class, therefore the List<?> argument |
459 | 469 | protected void maybeCallbackBeforeConvertWithQueries(List<?> queries) {
|
460 | 470 | queries.forEach(this::maybeCallbackBeforeConvertWithQuery);
|
461 | 471 | }
|
462 | 472 |
|
| 473 | + protected <T> T maybeCallbackAfterSave(T entity) { |
| 474 | + |
| 475 | + if (entityCallbacks != null) { |
| 476 | + return entityCallbacks.callback(AfterSaveCallback.class, entity); |
| 477 | + } |
| 478 | + |
| 479 | + return entity; |
| 480 | + } |
| 481 | + |
| 482 | + protected void maybeCallbackAfterSaveWithQuery(Object query) { |
| 483 | + |
| 484 | + if (query instanceof IndexQuery) { |
| 485 | + IndexQuery indexQuery = (IndexQuery) query; |
| 486 | + Object queryObject = indexQuery.getObject(); |
| 487 | + |
| 488 | + if (queryObject != null) { |
| 489 | + queryObject = maybeCallbackAfterSave(queryObject); |
| 490 | + indexQuery.setObject(queryObject); |
| 491 | + } |
| 492 | + } |
| 493 | + } |
| 494 | + |
| 495 | + // this can be called with either a List<IndexQuery> or a List<UpdateQuery>; these query classes |
| 496 | + // don't have a common base class, therefore the List<?> argument |
| 497 | + protected void maybeCallbackAfterSaveWithQueries(List<?> queries) { |
| 498 | + queries.forEach(this::maybeCallbackAfterSaveWithQuery); |
| 499 | + } |
| 500 | + |
463 | 501 | // endregion
|
464 | 502 |
|
465 | 503 | }
|
0 commit comments