Search in sources :

Example 1 with DocumentReference

use of org.springframework.data.mongodb.core.mapping.DocumentReference in project spring-data-mongodb by spring-projects.

the class ReferenceLookupDelegateUnitTests method shouldComputePlainStringTargetCollection.

// GH-3842
@Test
void shouldComputePlainStringTargetCollection() {
    DocumentReference documentReference = mock(DocumentReference.class);
    MongoPersistentEntity entity = mock(MongoPersistentEntity.class);
    MongoPersistentProperty property = mock(MongoPersistentProperty.class);
    doReturn(entity).when(mappingContext).getRequiredPersistentEntity((Class) any());
    when(property.isDocumentReference()).thenReturn(true);
    when(property.getDocumentReference()).thenReturn(documentReference);
    when(documentReference.collection()).thenReturn("collection1");
    lookupDelegate.readReference(property, Collections.singletonList("one"), (referenceQuery, referenceCollection) -> {
        assertThat(referenceCollection.getCollection()).isEqualTo("collection1");
        return Collections.emptyList();
    }, entityReader);
}
Also used : MongoPersistentEntity(org.springframework.data.mongodb.core.mapping.MongoPersistentEntity) MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) DocumentReference(org.springframework.data.mongodb.core.mapping.DocumentReference) Test(org.junit.jupiter.api.Test)

Example 2 with DocumentReference

use of org.springframework.data.mongodb.core.mapping.DocumentReference in project spring-data-mongodb by spring-projects.

the class ReferenceLookupDelegate method computeFilter.

/**
 * Compute the query to retrieve linked documents.
 *
 * @param property must not be {@literal null}.
 * @param source must not be {@literal null}.
 * @param spELContext must not be {@literal null}.
 * @return never {@literal null}.
 */
@SuppressWarnings("unchecked")
DocumentReferenceQuery computeFilter(MongoPersistentProperty property, Object source, SpELContext spELContext) {
    DocumentReference documentReference = property.isDocumentReference() ? property.getDocumentReference() : ReferenceEmulatingDocumentReference.INSTANCE;
    String lookup = documentReference.lookup();
    Object value = DocumentReferenceSource.getTargetSource(source);
    Document sort = parseValueOrGet(documentReference.sort(), bindingContext(property, source, spELContext), Document::new);
    if (property.isCollectionLike() && (value instanceof Collection || value == null)) {
        if (value == null) {
            return new ListDocumentReferenceQuery(codec.decode(lookup, bindingContext(property, source, spELContext)), sort);
        }
        Collection<Object> objects = (Collection<Object>) value;
        if (objects.isEmpty()) {
            return new ListDocumentReferenceQuery(NO_RESULTS_PREDICATE, sort);
        }
        List<Document> ors = new ArrayList<>(objects.size());
        for (Object entry : objects) {
            Document decoded = codec.decode(lookup, bindingContext(property, entry, spELContext));
            ors.add(decoded);
        }
        return new ListDocumentReferenceQuery(new Document("$or", ors), sort);
    }
    if (property.isMap() && value instanceof Map) {
        Set<Entry<Object, Object>> entries = ((Map<Object, Object>) value).entrySet();
        if (entries.isEmpty()) {
            return new MapDocumentReferenceQuery(NO_RESULTS_PREDICATE, sort, Collections.emptyMap());
        }
        Map<Object, Document> filterMap = new LinkedHashMap<>(entries.size());
        for (Entry<Object, Object> entry : entries) {
            Document decoded = codec.decode(lookup, bindingContext(property, entry.getValue(), spELContext));
            filterMap.put(entry.getKey(), decoded);
        }
        return new MapDocumentReferenceQuery(new Document("$or", filterMap.values()), sort, filterMap);
    }
    return new SingleDocumentReferenceQuery(codec.decode(lookup, bindingContext(property, source, spELContext)), sort);
}
Also used : ArrayList(java.util.ArrayList) Document(org.bson.Document) LinkedHashMap(java.util.LinkedHashMap) Entry(java.util.Map.Entry) MongoCollection(com.mongodb.client.MongoCollection) Collection(java.util.Collection) ReferenceCollection(org.springframework.data.mongodb.core.convert.ReferenceResolver.ReferenceCollection) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) DocumentReference(org.springframework.data.mongodb.core.mapping.DocumentReference)

Example 3 with DocumentReference

use of org.springframework.data.mongodb.core.mapping.DocumentReference 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)

Aggregations

DocumentReference (org.springframework.data.mongodb.core.mapping.DocumentReference)3 Document (org.bson.Document)2 ReferenceCollection (org.springframework.data.mongodb.core.convert.ReferenceResolver.ReferenceCollection)2 DBRef (com.mongodb.DBRef)1 MongoCollection (com.mongodb.client.MongoCollection)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Test (org.junit.jupiter.api.Test)1 MongoPersistentEntity (org.springframework.data.mongodb.core.mapping.MongoPersistentEntity)1 MongoPersistentProperty (org.springframework.data.mongodb.core.mapping.MongoPersistentProperty)1 ParameterBindingContext (org.springframework.data.mongodb.util.json.ParameterBindingContext)1