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);
}
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);
}
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);
}
Aggregations