|
65 | 65 | import org.springframework.util.CollectionUtils;
|
66 | 66 | import org.springframework.util.ObjectUtils;
|
67 | 67 |
|
| 68 | +import javax.print.Doc; |
| 69 | + |
68 | 70 | /**
|
69 | 71 | * Elasticsearch specific {@link org.springframework.data.convert.EntityConverter} implementation based on domain type
|
70 | 72 | * {@link ElasticsearchPersistentEntity metadata}.
|
@@ -333,46 +335,52 @@ private <R> R readEntity(ElasticsearchPersistentEntity<?> entity, Map<String, Ob
|
333 | 335 | return instance;
|
334 | 336 | }
|
335 | 337 |
|
| 338 | + Document document = (source instanceof Document) ? (Document) source : null; |
| 339 | + |
336 | 340 | ElasticsearchPropertyValueProvider valueProvider = new ElasticsearchPropertyValueProvider(accessor, evaluator);
|
337 |
| - R result = readProperties(targetEntity, instance, valueProvider); |
338 |
| - |
339 |
| - if (source instanceof Document document) { |
340 |
| - if (document.hasId()) { |
341 |
| - ElasticsearchPersistentProperty idProperty = targetEntity.getIdProperty(); |
342 |
| - PersistentPropertyAccessor<R> propertyAccessor = new ConvertingPropertyAccessor<>( |
343 |
| - targetEntity.getPropertyAccessor(result), conversionService); |
344 |
| - // Only deal with String because ES generated Ids are strings ! |
345 |
| - if (idProperty != null && idProperty.isReadable() && idProperty.getType().isAssignableFrom(String.class)) { |
346 |
| - propertyAccessor.setProperty(idProperty, document.getId()); |
| 341 | + try { |
| 342 | + R result = readProperties(targetEntity, instance, valueProvider); |
| 343 | + |
| 344 | + if (document != null) { |
| 345 | + if (document.hasId()) { |
| 346 | + ElasticsearchPersistentProperty idProperty = targetEntity.getIdProperty(); |
| 347 | + PersistentPropertyAccessor<R> propertyAccessor = new ConvertingPropertyAccessor<>( |
| 348 | + targetEntity.getPropertyAccessor(result), conversionService); |
| 349 | + // Only deal with String because ES generated Ids are strings ! |
| 350 | + if (idProperty != null && idProperty.isReadable() && idProperty.getType().isAssignableFrom(String.class)) { |
| 351 | + propertyAccessor.setProperty(idProperty, document.getId()); |
| 352 | + } |
347 | 353 | }
|
348 |
| - } |
349 | 354 |
|
350 |
| - if (document.hasVersion()) { |
351 |
| - long version = document.getVersion(); |
352 |
| - ElasticsearchPersistentProperty versionProperty = targetEntity.getVersionProperty(); |
353 |
| - // Only deal with Long because ES versions are longs ! |
354 |
| - if (versionProperty != null && versionProperty.getType().isAssignableFrom(Long.class)) { |
355 |
| - // check that a version was actually returned in the response, -1 would indicate that |
356 |
| - // a search didn't request the version ids in the response, which would be an issue |
357 |
| - Assert.isTrue(version != -1, "Version in response is -1"); |
358 |
| - targetEntity.getPropertyAccessor(result).setProperty(versionProperty, version); |
| 355 | + if (document.hasVersion()) { |
| 356 | + long version = document.getVersion(); |
| 357 | + ElasticsearchPersistentProperty versionProperty = targetEntity.getVersionProperty(); |
| 358 | + // Only deal with Long because ES versions are longs ! |
| 359 | + if (versionProperty != null && versionProperty.getType().isAssignableFrom(Long.class)) { |
| 360 | + // check that a version was actually returned in the response, -1 would indicate that |
| 361 | + // a search didn't request the version ids in the response, which would be an issue |
| 362 | + Assert.isTrue(version != -1, "Version in response is -1"); |
| 363 | + targetEntity.getPropertyAccessor(result).setProperty(versionProperty, version); |
| 364 | + } |
359 | 365 | }
|
360 |
| - } |
361 | 366 |
|
362 |
| - if (targetEntity.hasSeqNoPrimaryTermProperty() && document.hasSeqNo() && document.hasPrimaryTerm()) { |
363 |
| - if (isAssignedSeqNo(document.getSeqNo()) && isAssignedPrimaryTerm(document.getPrimaryTerm())) { |
364 |
| - SeqNoPrimaryTerm seqNoPrimaryTerm = new SeqNoPrimaryTerm(document.getSeqNo(), document.getPrimaryTerm()); |
365 |
| - ElasticsearchPersistentProperty property = targetEntity.getRequiredSeqNoPrimaryTermProperty(); |
366 |
| - targetEntity.getPropertyAccessor(result).setProperty(property, seqNoPrimaryTerm); |
| 367 | + if (targetEntity.hasSeqNoPrimaryTermProperty() && document.hasSeqNo() && document.hasPrimaryTerm()) { |
| 368 | + if (isAssignedSeqNo(document.getSeqNo()) && isAssignedPrimaryTerm(document.getPrimaryTerm())) { |
| 369 | + SeqNoPrimaryTerm seqNoPrimaryTerm = new SeqNoPrimaryTerm(document.getSeqNo(), document.getPrimaryTerm()); |
| 370 | + ElasticsearchPersistentProperty property = targetEntity.getRequiredSeqNoPrimaryTermProperty(); |
| 371 | + targetEntity.getPropertyAccessor(result).setProperty(property, seqNoPrimaryTerm); |
| 372 | + } |
367 | 373 | }
|
368 | 374 | }
|
369 |
| - } |
370 | 375 |
|
371 |
| - if (source instanceof SearchDocument searchDocument) { |
372 |
| - populateScriptFields(targetEntity, result, searchDocument); |
| 376 | + if (source instanceof SearchDocument searchDocument) { |
| 377 | + populateScriptFields(targetEntity, result, searchDocument); |
| 378 | + } |
| 379 | + return result; |
| 380 | + } catch (ConversionException e) { |
| 381 | + String documentId = (document != null && document.hasId()) ? document.getId() : null; |
| 382 | + throw new MappingConversionException(documentId, e); |
373 | 383 | }
|
374 |
| - |
375 |
| - return result; |
376 | 384 | }
|
377 | 385 |
|
378 | 386 | private ParameterValueProvider<ElasticsearchPersistentProperty> getParameterProvider(
|
@@ -510,11 +518,9 @@ private Object propertyConverterRead(ElasticsearchPersistentProperty property, O
|
510 | 518 | }
|
511 | 519 |
|
512 | 520 | if (source instanceof List<?> list) {
|
513 |
| - source = list.stream().map(it -> convertOnRead(propertyValueConverter, it)) |
514 |
| - .collect(Collectors.toList()); |
| 521 | + source = list.stream().map(it -> convertOnRead(propertyValueConverter, it)).collect(Collectors.toList()); |
515 | 522 | } else if (source instanceof Set<?> set) {
|
516 |
| - source = set.stream().map(it -> convertOnRead(propertyValueConverter, it)) |
517 |
| - .collect(Collectors.toSet()); |
| 523 | + source = set.stream().map(it -> convertOnRead(propertyValueConverter, it)).collect(Collectors.toSet()); |
518 | 524 | } else {
|
519 | 525 | source = convertOnRead(propertyValueConverter, source);
|
520 | 526 | }
|
|
0 commit comments