DATAMONGO-534 - GridFsTemplate supports queries with sorting.
Upgraded to Mongo Java Driver 2.11.3 to be able to forward the sorting options contained in a Query to eventually be able to return sorted results when querying for files.
This commit is contained in:
@@ -158,7 +158,15 @@ public class GridFsTemplate implements GridFsOperations, ResourcePatternResolver
|
||||
* @see org.springframework.data.mongodb.gridfs.GridFsOperations#find(com.mongodb.DBObject)
|
||||
*/
|
||||
public List<GridFSDBFile> find(Query query) {
|
||||
return getGridFs().find(getMappedQuery(query));
|
||||
|
||||
if (query == null) {
|
||||
return getGridFs().find((DBObject) null);
|
||||
}
|
||||
|
||||
DBObject queryObject = getMappedQuery(query.getQueryObject());
|
||||
DBObject sortObject = getMappedQuery(query.getSortObject());
|
||||
|
||||
return getGridFs().find(queryObject, sortObject);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -221,7 +229,11 @@ public class GridFsTemplate implements GridFsOperations, ResourcePatternResolver
|
||||
}
|
||||
|
||||
private DBObject getMappedQuery(Query query) {
|
||||
return query == null ? null : queryMapper.getMappedObject(query.getQueryObject(), null);
|
||||
return query == null ? null : getMappedQuery(query.getQueryObject());
|
||||
}
|
||||
|
||||
private DBObject getMappedQuery(DBObject query) {
|
||||
return query == null ? null : queryMapper.getMappedObject(query, null);
|
||||
}
|
||||
|
||||
private GridFS getGridFs() {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2011-2012 the original author or authors.
|
||||
* Copyright 2011-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -15,7 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.mongodb.gridfs;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.data.mongodb.core.query.Query.*;
|
||||
import static org.springframework.data.mongodb.gridfs.GridFsCriteria.*;
|
||||
@@ -29,6 +29,9 @@ import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.domain.Sort.Direction;
|
||||
import org.springframework.data.mongodb.core.query.Query;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@@ -49,8 +52,7 @@ public class GridFsTemplateIIntegrationTests {
|
||||
|
||||
Resource resource = new ClassPathResource("gridfs/gridfs.xml");
|
||||
|
||||
@Autowired
|
||||
GridFsOperations operations;
|
||||
@Autowired GridFsOperations operations;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
@@ -127,6 +129,39 @@ public class GridFsTemplateIIntegrationTests {
|
||||
assertThat(resources[0].getContentType(), is(reference.getContentType()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAMONGO-534
|
||||
*/
|
||||
@Test
|
||||
public void considersSortWhenQueryingFiles() throws IOException {
|
||||
|
||||
GridFSFile second = operations.store(resource.getInputStream(), "foo.xml");
|
||||
GridFSFile third = operations.store(resource.getInputStream(), "foobar.xml");
|
||||
GridFSFile first = operations.store(resource.getInputStream(), "bar.xml");
|
||||
|
||||
Query query = new Query().with(new Sort(Direction.ASC, "filename"));
|
||||
|
||||
List<GridFSDBFile> result = operations.find(query);
|
||||
assertThat(result, hasSize(3));
|
||||
assertSame(result.get(0), first);
|
||||
assertSame(result.get(1), second);
|
||||
assertSame(result.get(2), third);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAMONGO-534
|
||||
*/
|
||||
@Test
|
||||
public void queryingWithNullQueryReturnsAllFiles() throws IOException {
|
||||
|
||||
GridFSFile reference = operations.store(resource.getInputStream(), "foo.xml");
|
||||
|
||||
List<GridFSDBFile> result = operations.find(null);
|
||||
|
||||
assertThat(result, hasSize(1));
|
||||
assertSame(result.get(0), reference);
|
||||
}
|
||||
|
||||
private static void assertSame(GridFSFile left, GridFSFile right) {
|
||||
|
||||
assertThat(left.getId(), is(right.getId()));
|
||||
|
||||
Reference in New Issue
Block a user