use of org.springframework.data.mongodb.core.convert.ReferenceLoader.DocumentReferenceQuery in project spring-data-mongodb by spring-projects.
the class ReferenceLookupDelegate method readReference.
/**
* Read the reference expressed by the given property.
*
* @param property the reference defining property. Must not be {@literal null}. THe
* @param source the source value identifying to the referenced entity. Must not be {@literal null}.
* @param lookupFunction to execute a lookup query. Must not be {@literal null}.
* @param entityReader the callback to convert raw source values into actual domain types. Must not be
* {@literal null}.
* @return can be {@literal null}.
*/
@Nullable
public Object readReference(MongoPersistentProperty property, Object source, LookupFunction lookupFunction, MongoEntityReader entityReader) {
Object value = source instanceof DocumentReferenceSource ? ((DocumentReferenceSource) source).getTargetSource() : source;
DocumentReferenceQuery filter = computeFilter(property, source, spELContext);
ReferenceCollection referenceCollection = computeReferenceContext(property, value, spELContext);
Iterable<Document> result = lookupFunction.apply(filter, referenceCollection);
if (property.isCollectionLike()) {
return entityReader.read(result, property.getTypeInformation());
}
if (!result.iterator().hasNext()) {
return null;
}
Object resultValue = result.iterator().next();
return resultValue != null ? entityReader.read(resultValue, property.getTypeInformation()) : null;
}
Aggregations