Search in sources :

Example 1 with ReferenceCollection

use of org.springframework.data.mongodb.core.convert.ReferenceResolver.ReferenceCollection in project spring-data-mongodb by spring-projects.

the class ReferenceLookupDelegate method computeReferenceContext.

private ReferenceCollection computeReferenceContext(MongoPersistentProperty property, Object value, SpELContext spELContext) {
    // Use the first value as a reference for others in case of collection like
    if (value instanceof Iterable) {
        Iterator<?> iterator = ((Iterable<?>) value).iterator();
        value = iterator.hasNext() ? iterator.next() : new Document();
    }
    // handle DBRef value
    if (value instanceof DBRef) {
        return ReferenceCollection.fromDBRef((DBRef) value);
    }
    String collection = mappingContext.getRequiredPersistentEntity(property.getAssociationTargetType()).getCollection();
    if (value instanceof Document) {
        Document documentPointer = (Document) value;
        if (property.isDocumentReference()) {
            ParameterBindingContext bindingContext = bindingContext(property, value, spELContext);
            DocumentReference documentReference = property.getDocumentReference();
            String targetDatabase = parseValueOrGet(documentReference.db(), bindingContext, () -> documentPointer.get("db", String.class));
            String targetCollection = parseValueOrGet(documentReference.collection(), bindingContext, () -> documentPointer.get("collection", collection));
            return new ReferenceCollection(targetDatabase, targetCollection);
        }
        return new ReferenceCollection(documentPointer.getString("db"), documentPointer.get("collection", collection));
    }
    if (property.isDocumentReference()) {
        ParameterBindingContext bindingContext = bindingContext(property, value, spELContext);
        DocumentReference documentReference = property.getDocumentReference();
        String targetDatabase = parseValueOrGet(documentReference.db(), bindingContext, () -> null);
        String targetCollection = parseValueOrGet(documentReference.collection(), bindingContext, () -> collection);
        return new ReferenceCollection(targetDatabase, targetCollection);
    }
    return new ReferenceCollection(null, collection);
}
Also used : ParameterBindingContext(org.springframework.data.mongodb.util.json.ParameterBindingContext) DBRef(com.mongodb.DBRef) Document(org.bson.Document) ReferenceCollection(org.springframework.data.mongodb.core.convert.ReferenceResolver.ReferenceCollection) DocumentReference(org.springframework.data.mongodb.core.mapping.DocumentReference)

Example 2 with ReferenceCollection

use of org.springframework.data.mongodb.core.convert.ReferenceResolver.ReferenceCollection 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)2 ReferenceCollection (org.springframework.data.mongodb.core.convert.ReferenceResolver.ReferenceCollection)2 DBRef (com.mongodb.DBRef)1 DocumentReferenceQuery (org.springframework.data.mongodb.core.convert.ReferenceLoader.DocumentReferenceQuery)1 DocumentReference (org.springframework.data.mongodb.core.mapping.DocumentReference)1 ParameterBindingContext (org.springframework.data.mongodb.util.json.ParameterBindingContext)1 Nullable (org.springframework.lang.Nullable)1