Search in sources :

Example 1 with DocumentReferenceQuery

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;
}
Also used : DocumentReferenceQuery(org.springframework.data.mongodb.core.convert.ReferenceLoader.DocumentReferenceQuery) Document(org.bson.Document) ReferenceCollection(org.springframework.data.mongodb.core.convert.ReferenceResolver.ReferenceCollection) Nullable(org.springframework.lang.Nullable)

Aggregations

Document (org.bson.Document)1 DocumentReferenceQuery (org.springframework.data.mongodb.core.convert.ReferenceLoader.DocumentReferenceQuery)1 ReferenceCollection (org.springframework.data.mongodb.core.convert.ReferenceResolver.ReferenceCollection)1 Nullable (org.springframework.lang.Nullable)1