use of org.springframework.data.mongodb.core.query.Collation in project spring-data-mongodb by spring-projects.
the class MongoTemplate method findDistinct.
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.MongoOperations#findDistinct(org.springframework.data.mongodb.core.query.Query, java.lang.String, java.lang.String, java.lang.Class, java.lang.Class)
*/
@Override
@SuppressWarnings("unchecked")
public <T> List<T> findDistinct(Query query, String field, String collectionName, Class<?> entityClass, Class<T> resultClass) {
Assert.notNull(query, "Query must not be null!");
Assert.notNull(field, "Field must not be null!");
Assert.notNull(collectionName, "CollectionName must not be null!");
Assert.notNull(entityClass, "EntityClass must not be null!");
Assert.notNull(resultClass, "ResultClass must not be null!");
MongoPersistentEntity<?> entity = entityClass != Object.class ? getPersistentEntity(entityClass) : null;
DistinctQueryContext distinctQueryContext = queryOperations.distinctQueryContext(query, field);
Document mappedQuery = distinctQueryContext.getMappedQuery(entity);
String mappedFieldName = distinctQueryContext.getMappedFieldName(entity);
Class<T> mongoDriverCompatibleType = distinctQueryContext.getDriverCompatibleClass(resultClass);
MongoIterable<?> result = execute(collectionName, (collection) -> {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(String.format("Executing findDistinct using query %s for field: %s in collection: %s", serializeToJsonSafely(mappedQuery), field, collectionName));
}
QueryCursorPreparer preparer = new QueryCursorPreparer(query, entityClass);
if (preparer.hasReadPreference()) {
collection = collection.withReadPreference(preparer.getReadPreference());
}
DistinctIterable<T> iterable = collection.distinct(mappedFieldName, mappedQuery, mongoDriverCompatibleType);
distinctQueryContext.applyCollation(entityClass, iterable::collation);
return iterable;
});
if (resultClass == Object.class || mongoDriverCompatibleType != resultClass) {
MongoConverter converter = getConverter();
DefaultDbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoDbFactory);
result = result.map((source) -> converter.mapValueToTargetType(source, distinctQueryContext.getMostSpecificConversionTargetType(resultClass, entityClass), dbRefResolver));
}
try {
return (List<T>) result.into(new ArrayList<>());
} catch (RuntimeException e) {
throw potentiallyConvertRuntimeException(e, exceptionTranslator);
}
}
use of org.springframework.data.mongodb.core.query.Collation in project spring-data-mongodb by spring-projects.
the class MongoParametersParameterAccessorUnitTests method shouldDetectCollation.
// DATAMONGO-1854
@Test
public void shouldDetectCollation() throws NoSuchMethodException, SecurityException {
Method method = PersonRepository.class.getMethod("findByFirstname", String.class, Collation.class);
MongoQueryMethod queryMethod = new MongoQueryMethod(method, metadata, factory, context);
Collation collation = Collation.of("en_US");
MongoParameterAccessor accessor = new MongoParametersParameterAccessor(queryMethod, new Object[] { "dalinar", collation });
assertThat(accessor.getCollation()).isEqualTo(collation);
}
use of org.springframework.data.mongodb.core.query.Collation in project spring-data-mongodb by spring-projects.
the class SimpleMongoRepositoryUnitTests method shouldAddDefaultCollationToFindWithSortForExampleIfPresent.
// DATAMONGO-1854
@Test
public void shouldAddDefaultCollationToFindWithSortForExampleIfPresent() {
Collation collation = Collation.of("en_US");
when(entityInformation.getCollation()).thenReturn(collation);
repository.findAll(Example.of(new TestDummy()), Sort.by("nothing"));
ArgumentCaptor<Query> query = ArgumentCaptor.forClass(Query.class);
verify(mongoOperations).find(query.capture(), any(), any());
assertThat(query.getValue().getCollation()).contains(collation);
}
use of org.springframework.data.mongodb.core.query.Collation in project spring-data-mongodb by spring-projects.
the class SimpleMongoRepositoryUnitTests method shouldAddDefaultCollationToCountForExampleIfPresent.
// DATAMONGO-1854
@Test
public void shouldAddDefaultCollationToCountForExampleIfPresent() {
Collation collation = Collation.of("en_US");
when(entityInformation.getCollation()).thenReturn(collation);
repository.count(Example.of(new TestDummy()));
ArgumentCaptor<Query> query = ArgumentCaptor.forClass(Query.class);
verify(mongoOperations).count(query.capture(), any(), any());
assertThat(query.getValue().getCollation()).contains(collation);
}
use of org.springframework.data.mongodb.core.query.Collation in project spring-data-mongodb by spring-projects.
the class AbstractMongoQueryUnitTests method shouldApplyCollationParameter.
// DATAMONGO-1854
@Test
void shouldApplyCollationParameter() {
Collation collation = Collation.of("en_US");
//
createQueryForMethod("findWithCollationParameterByFirstName", String.class, Collation.class).execute(new Object[] { "dalinar", collation });
ArgumentCaptor<Query> captor = ArgumentCaptor.forClass(Query.class);
verify(withQueryMock).matching(captor.capture());
assertThat(captor.getValue().getCollation()).contains(collation);
}
Aggregations