diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoOperations.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoOperations.java index b75586fcc..2ee666e59 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoOperations.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoOperations.java @@ -18,11 +18,10 @@ package org.springframework.data.document.mongodb; import java.util.List; import java.util.Set; -import org.springframework.data.document.mongodb.builder.Query; +import org.springframework.data.document.mongodb.builder.QueryDefinition; import com.mongodb.DBCollection; import com.mongodb.DBObject; -import com.mongodb.WriteConcern; /** * Interface that specifies a basic set of MongoDB operations. Implemented by {@link MongoTemplate}. @@ -397,14 +396,14 @@ public interface MongoOperations { * {@see MongoConverter}. Unless configured otherwise, an * instance of SimpleMongoConverter will be used. * - * The query is specified as a {@link Query} which can be created either using the {@link BasicQuery} or the more - * feature rich {@link QuerySpec}. + * The query is specified as a {@link QueryDefinition} which can be created either using the {@link BasicQuery} or the more + * feature rich {@link Query}. * * @param query the query class that specifies the criteria used to find a record and also an optional fields specification * @param targetClass the parameterized type of the returned list. * @return the List of converted objects */ - List query(Query query, Class targetClass); + List find(QueryDefinition query, Class targetClass); /** * Map the results of an ad-hoc query on the default MongoDB collection to a List of the specified type. @@ -413,15 +412,15 @@ public interface MongoOperations { * {@see MongoConverter}. Unless configured otherwise, an * instance of SimpleMongoConverter will be used. * - * The query is specified as a {@link Query} which can be created either using the {@link BasicQuery} or the more - * feature rich {@link QuerySpec}. + * The query is specified as a {@link QueryDefinition} which can be created either using the {@link BasicQuery} or the more + * feature rich {@link Query}. * * @param query the query class that specifies the criteria used to find a record and also an optional fields specification * @param targetClass the parameterized type of the returned list. * @param reader the MongoReader to convert from DBObject to an object. * @return the List of converted objects */ - List query(Query query, Class targetClass, + List find(QueryDefinition query, Class targetClass, MongoReader reader); /** @@ -431,15 +430,15 @@ public interface MongoOperations { * {@see MongoConverter}. Unless configured otherwise, an * instance of SimpleMongoConverter will be used. * - * The query is specified as a {@link Query} which can be created either using the {@link BasicQuery} or the more - * feature rich {@link QuerySpec}. + * The query is specified as a {@link QueryDefinition} which can be created either using the {@link BasicQuery} or the more + * feature rich {@link Query}. * * @param collectionName name of the collection to retrieve the objects from * @param query the query class that specifies the criteria used to find a record and also an optional fields specification * @param targetClass the parameterized type of the returned list. * @return the List of converted objects */ - List query(String collectionName, Query query, + List find(String collectionName, QueryDefinition query, Class targetClass); /** @@ -449,8 +448,8 @@ public interface MongoOperations { * {@see MongoConverter}. Unless configured otherwise, an * instance of SimpleMongoConverter will be used. * - * The query is specified as a {@link Query} which can be created either using the {@link BasicQuery} or the more - * feature rich {@link QuerySpec}. + * The query is specified as a {@link QueryDefinition} which can be created either using the {@link BasicQuery} or the more + * feature rich {@link Query}. * * @param collectionName name of the collection to retrieve the objects from * @param query the query class that specifies the criteria used to find a record and also an optional fields specification @@ -458,27 +457,9 @@ public interface MongoOperations { * @param reader the MongoReader to convert from DBObject to an object. * @return the List of converted objects */ - List query(String collectionName, Query query, + List find(String collectionName, QueryDefinition query, Class targetClass, MongoReader reader); - /** - * Map the results of an ad-hoc query on the default MongoDB collection to a List of the specified type. - * - * The object is converted from the MongoDB native representation using an instance of - * {@see MongoConverter}. Unless configured otherwise, an - * instance of SimpleMongoConverter will be used. - * - * The query document is specified as a standard DBObject. - * - * NOTE: A generic criteria API will be introduced in a future release. You can see the - * inconsequential project for an example of how that - * may look. - * - * @param query the query document that specifies the criteria used to find a record - * @param targetClass the parameterized type of the returned list. - * @return the List of converted objects - */ - List find(DBObject query, Class targetClass); /** * Map the results of an ad-hoc query on the default MongoDB collection to a List of the specified type. @@ -487,184 +468,16 @@ public interface MongoOperations { * {@see MongoConverter}. Unless configured otherwise, an * instance of SimpleMongoConverter will be used. * - * The query document is specified as a standard DBObject. + * The query is specified as a {@link QueryDefinition} which can be created either using the {@link BasicQuery} or the more + * feature rich {@link Query}. * - * @param query the query document that specifies the criteria used to find a record + * @param collectionName name of the collection to retrieve the objects from + * @param query the query class that specifies the criteria used to find a record and also an optional fields specification * @param targetClass the parameterized type of the returned list. * @param preparer allows for customization of the DBCursor used when iterating over the result set, * (apply limits, skips and so on). * @return the List of converted objects. */ - List find(DBObject query, Class targetClass, - CursorPreparer preparer); - - /** - * Map the results of an ad-hoc query on the default MongoDB collection to a List using the provided MongoReader - * - * The query document is specified as a standard DBObject. - * - * @param query the query document that specifies the criteria used to find a record - * @param targetClass the parameterized type of the returned list. - * @param reader the MongoReader to convert from DBObject to an object. - * @return the List of converted objects. - */ - List find(DBObject query, Class targetClass, - MongoReader reader); + List find(String collectionName, QueryDefinition query, Class targetClass, CursorPreparer preparer); - /** - * Map the results of an ad-hoc query on the default MongoDB collection to a List of the specified type. - * - * The object is converted from the MongoDB native representation using an instance of - * {@see MongoConverter}. Unless configured otherwise, an - * instance of SimpleMongoConverter will be used. - * - * The query document is specified as a standard DBObject. - * - * NOTE: A generic criteria API will be introduced in a future release. You can see the - * inconsequential project for an example of how that - * may look. - * - * @param collectionName name of the collection to retrieve the objects from - * @param query the query document that specifies the criteria used to find a record - * @param targetClass the parameterized type of the returned list. - * @return the List of converted objects - */ - List find(String collectionName, DBObject query, Class targetClass); - - /** - * Map the results of an ad-hoc query on the default MongoDB collection to a List of the specified type. - * - * The object is converted from the MongoDB native representation using an instance of - * {@see MongoConverter}. Unless configured otherwise, an - * instance of SimpleMongoConverter will be used. - * - * The query document is specified as a standard DBObject. - * - * @param collectionName name of the collection to retrieve the objects from - * @param query the query document that specifies the criteria used to find a record - * @param targetClass the parameterized type of the returned list. - * @param preparer allows for customization of the DBCursor used when iterating over the result set, - * (apply limits, skips and so on). - * @return the List of converted objects. - */ - List find(String collectionName, DBObject query, Class targetClass, CursorPreparer preparer); - - /** - * Map the results of an ad-hoc query on the default MongoDB collection to a List using the provided MongoReader - * - * The query document is specified as a standard DBObject. - * - * @param collectionName name of the collection to retrieve the objects from - * @param query the query document that specifies the criteria used to find a record - * @param targetClass the parameterized type of the returned list. - * @param reader the MongoReader to convert from DBObject to an object. - * @return the List of converted objects. - */ - List find(String collectionName, DBObject query, Class targetClass, MongoReader reader); - - /** - * Map the results of an ad-hoc query on the default MongoDB collection to a List of the specified type. - * - * The object is converted from the MongoDB native representation using an instance of - * {@see MongoConverter}. Unless configured otherwise, an - * instance of SimpleMongoConverter will be used. - * - * The query document is specified as a standard DBObject and so is the fields specification. - * - * NOTE: A generic criteria API will be introduced in a future release. You can see the - * inconsequential project for an example of how that - * may look. - * - * @param query the query document that specifies the criteria used to find a record - * @param targetClass the parameterized type of the returned list. - * @return the List of converted objects - */ - List find(DBObject query, DBObject fields, Class targetClass); - - /** - * Map the results of an ad-hoc query on the default MongoDB collection to a List of the specified type. - * - * The object is converted from the MongoDB native representation using an instance of - * {@see MongoConverter}. Unless configured otherwise, an - * instance of SimpleMongoConverter will be used. - * - * The query document is specified as a standard DBObject and so is the fields specification. - * - * @param query the query document that specifies the criteria used to find a record - * @param fields the document that specifies the fields to be returned - * @param targetClass the parameterized type of the returned list. - * @param preparer allows for customization of the DBCursor used when iterating over the result set, - * (apply limits, skips and so on). - * @return the List of converted objects. - */ - List find(DBObject query, DBObject fields, Class targetClass, - CursorPreparer preparer); - - /** - * Map the results of an ad-hoc query on the default MongoDB collection to a List using the provided MongoReader - * - * The query document is specified as a standard DBObject and so is the fields specification. - * - * @param query the query document that specifies the criteria used to find a record - * @param fields the document that specifies the fields to be returned - * @param targetClass the parameterized type of the returned list. - * @param reader the MongoReader to convert from DBObject to an object. - * @return the List of converted objects. - */ - List find(DBObject query, DBObject fields, Class targetClass, - MongoReader reader); - - /** - * Map the results of an ad-hoc query on the default MongoDB collection to a List of the specified type. - * - * The object is converted from the MongoDB native representation using an instance of - * {@see MongoConverter}. Unless configured otherwise, an - * instance of SimpleMongoConverter will be used. - * - * The query document is specified as a standard DBObject and so is the fields specification. - * - * NOTE: A generic criteria API will be introduced in a future release. You can see the - * inconsequential project for an example of how that - * may look. - * - * @param collectionName name of the collection to retrieve the objects from - * @param query the query document that specifies the criteria used to find a record - * @param fields the document that specifies the fields to be returned - * @param targetClass the parameterized type of the returned list. - * @return the List of converted objects - */ - List find(String collectionName, DBObject fields, DBObject query, Class targetClass); - - /** - * Map the results of an ad-hoc query on the default MongoDB collection to a List of the specified type. - * - * The object is converted from the MongoDB native representation using an instance of - * {@see MongoConverter}. Unless configured otherwise, an - * instance of SimpleMongoConverter will be used. - * - * The query document is specified as a standard DBObject and so is the fields specification. - * - * @param collectionName name of the collection to retrieve the objects from - * @param query the query document that specifies the criteria used to find a record - * @param fields the document that specifies the fields to be returned - * @param targetClass the parameterized type of the returned list. - * @param preparer allows for customization of the DBCursor used when iterating over the result set, - * (apply limits, skips and so on). - * @return the List of converted objects. - */ - List find(String collectionName, DBObject fields, DBObject query, Class targetClass, CursorPreparer preparer); - - /** - * Map the results of an ad-hoc query on the default MongoDB collection to a List using the provided MongoReader - * - * The query document is specified as a standard DBObject and so is the fields specification. - * - * @param collectionName name of the collection to retrieve the objects from - * @param query the query document that specifies the criteria used to find a record - * @param fields the document that specifies the fields to be returned - * @param targetClass the parameterized type of the returned list. - * @param reader the MongoReader to convert from DBObject to an object. - * @return the List of converted objects. - */ - List find(String collectionName, DBObject fields, DBObject query, Class targetClass, MongoReader reader); } \ No newline at end of file diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoTemplate.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoTemplate.java index 6b38ca93f..d01949db1 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoTemplate.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoTemplate.java @@ -28,7 +28,7 @@ import org.springframework.beans.factory.InitializingBean; import org.springframework.dao.DataAccessException; import org.springframework.dao.InvalidDataAccessApiUsageException; import org.springframework.data.document.mongodb.MongoPropertyDescriptors.MongoPropertyDescriptor; -import org.springframework.data.document.mongodb.builder.Query; +import org.springframework.data.document.mongodb.builder.QueryDefinition; import org.springframework.jca.cci.core.ConnectionCallback; import org.springframework.util.Assert; @@ -635,17 +635,11 @@ public class MongoTemplate implements InitializingBean, MongoOperations { getDefaultCollectionName()); } - /* (non-Javadoc) - * @see org.springframework.data.document.mongodb.MongoOperations#getCollection(java.lang.String, java.lang.Class) - */ public List getCollection(String collectionName, Class targetClass) { return executeEach(new FindCallback(null), null, new ReadDbObjectCallback(mongoConverter, targetClass), collectionName); } - /* (non-Javadoc) - * @see org.springframework.data.document.mongodb.MongoOperations#getCollectionNames() - */ public Set getCollectionNames() { return execute(new DbCallback>() { public Set doInDB(DB db) throws MongoException, DataAccessException { @@ -654,131 +648,71 @@ public class MongoTemplate implements InitializingBean, MongoOperations { }); } - /* (non-Javadoc) - * @see org.springframework.data.document.mongodb.MongoOperations#getCollection(java.lang.String, java.lang.Class, org.springframework.data.document.mongodb.MongoReader) - */ public List getCollection(String collectionName, Class targetClass, MongoReader reader) { return executeEach(new FindCallback(null), null, new ReadDbObjectCallback(reader, targetClass), collectionName); } - // Queries that take JavaScript to express the query. + // Find methods that take a QueryDefinition to express the query. - /* (non-Javadoc) - * @see org.springframework.data.document.mongodb.MongoOperations#queryUsingJavaScript(java.lang.String, java.lang.Class) - */ - public List query(Query query, Class targetClass) { - return query(getDefaultCollectionName(), query, targetClass); // + public List find(QueryDefinition query, Class targetClass) { + return find(getDefaultCollectionName(), query, targetClass); // } - /* (non-Javadoc) - * @see org.springframework.data.document.mongodb.MongoOperations#queryUsingJavaScript(java.lang.String, java.lang.Class, org.springframework.data.document.mongodb.MongoReader) - */ - public List query(Query query, Class targetClass, MongoReader reader) { - return query(getDefaultCollectionName(), query, targetClass, reader); + public List find(QueryDefinition query, Class targetClass, MongoReader reader) { + return find(getDefaultCollectionName(), query, targetClass, reader); } - /* (non-Javadoc) - * @see org.springframework.data.document.mongodb.MongoOperations#queryUsingJavaScript(java.lang.String, java.lang.String, java.lang.Class) - */ - public List query(String collectionName, Query query, Class targetClass) { - return find(collectionName, query.getQueryObject(), query.getFieldsObject(), targetClass); // + public List find(String collectionName, QueryDefinition query, Class targetClass) { + return doFind(collectionName, query.getQueryObject(), query.getFieldsObject(), targetClass, (CursorPreparer) null); } - /* (non-Javadoc) - * @see org.springframework.data.document.mongodb.MongoOperations#queryUsingJavaScript(java.lang.String, java.lang.String, java.lang.Class, org.springframework.data.document.mongodb.MongoReader) - */ - public List query(String collectionName, Query query, Class targetClass, MongoReader reader) { - return find(collectionName, query.getQueryObject(), query.getFieldsObject(), targetClass, reader); + public List find(String collectionName, QueryDefinition query, Class targetClass, MongoReader reader) { + return doFind(collectionName, query.getQueryObject(), query.getFieldsObject(), targetClass, reader); } - - // Find methods that take DBObject to express the query - - /* (non-Javadoc) - * @see org.springframework.data.document.mongodb.MongoOperations#query(com.mongodb.DBObject, java.lang.Class) - */ - public List find(DBObject query, Class targetClass) { - return find(query, null, targetClass); // - } - - /* (non-Javadoc) - * @see org.springframework.data.document.mongodb.MongoOperations#query(com.mongodb.DBObject, java.lang.Class, org.springframework.data.document.mongodb.CursorPreparer) - */ - public List find(DBObject query, Class targetClass, CursorPreparer preparer) { - return find(query, null, targetClass, preparer); // + public List find(String collectionName, QueryDefinition query, + Class targetClass, CursorPreparer preparer) { + return doFind(collectionName, query.getQueryObject(), query.getFieldsObject(), targetClass, preparer); } - /* (non-Javadoc) - * @see org.springframework.data.document.mongodb.MongoOperations#query(com.mongodb.DBObject, java.lang.Class, org.springframework.data.document.mongodb.MongoReader) + /** + * Map the results of an ad-hoc query on the default MongoDB collection to a List of the specified type. + * + * The object is converted from the MongoDB native representation using an instance of + * {@see MongoConverter}. Unless configured otherwise, an + * instance of SimpleMongoConverter will be used. + * + * The query document is specified as a standard DBObject and so is the fields specification. + * + * Can be overridden by subclasses. + * + * @param collectionName name of the collection to retrieve the objects from + * @param query the query document that specifies the criteria used to find a record + * @param fields the document that specifies the fields to be returned + * @param targetClass the parameterized type of the returned list. + * @param preparer allows for customization of the DBCursor used when iterating over the result set, + * (apply limits, skips and so on). + * @return the List of converted objects. */ - public List find(DBObject query, Class targetClass, MongoReader reader) { - return find(query, null, targetClass, reader); - } - - /* (non-Javadoc) - * @see org.springframework.data.document.mongodb.MongoOperations#query(java.lang.String, com.mongodb.DBObject, java.lang.Class) - */ - public List find(String collectionName, DBObject query, Class targetClass) { - return find(collectionName, query, null, targetClass); - } - - /* (non-Javadoc) - * @see org.springframework.data.document.mongodb.MongoOperations#query(java.lang.String, com.mongodb.DBObject, java.lang.Class, org.springframework.data.document.mongodb.CursorPreparer) - */ - public List find(String collectionName, DBObject query, Class targetClass, CursorPreparer preparer) { - return find(collectionName, query, null, targetClass, preparer); - } - - /* (non-Javadoc) - * @see org.springframework.data.document.mongodb.MongoOperations#query(java.lang.String, com.mongodb.DBObject, java.lang.Class, org.springframework.data.document.mongodb.MongoReader) - */ - public List find(String collectionName, DBObject query, Class targetClass, MongoReader reader) { - return find(collectionName, query, null, targetClass, reader); - } - - // Find methods that take DBObject to express the query and a DBObject to express the fields specification - - /* (non-Javadoc) - * @see org.springframework.data.document.mongodb.MongoOperations#query(com.mongodb.DBObject, java.lang.Class) - */ - public List find(DBObject query, DBObject fields, Class targetClass) { - return find(getDefaultCollectionName(), query, fields, targetClass); // - } - - /* (non-Javadoc) - * @see org.springframework.data.document.mongodb.MongoOperations#query(com.mongodb.DBObject, java.lang.Class, org.springframework.data.document.mongodb.CursorPreparer) - */ - public List find(DBObject query, DBObject fields, Class targetClass, CursorPreparer preparer) { - return find(getDefaultCollectionName(), query, fields, targetClass, preparer); // - } - - /* (non-Javadoc) - * @see org.springframework.data.document.mongodb.MongoOperations#query(com.mongodb.DBObject, java.lang.Class, org.springframework.data.document.mongodb.MongoReader) - */ - public List find(DBObject query, DBObject fields, Class targetClass, MongoReader reader) { - return find(getDefaultCollectionName(), query, fields, targetClass, reader); - } - - /* (non-Javadoc) - * @see org.springframework.data.document.mongodb.MongoOperations#query(java.lang.String, com.mongodb.DBObject, java.lang.Class) - */ - public List find(String collectionName, DBObject query, DBObject fields, Class targetClass) { - return find(collectionName, query, fields, targetClass, (CursorPreparer) null); - } - - /* (non-Javadoc) - * @see org.springframework.data.document.mongodb.MongoOperations#query(java.lang.String, com.mongodb.DBObject, java.lang.Class, org.springframework.data.document.mongodb.CursorPreparer) - */ - public List find(String collectionName, DBObject query, DBObject fields, Class targetClass, CursorPreparer preparer) { + protected List doFind(String collectionName, DBObject query, DBObject fields, Class targetClass, CursorPreparer preparer) { return executeEach(new FindCallback(query, fields), preparer, new ReadDbObjectCallback(mongoConverter, targetClass), collectionName); } - /* (non-Javadoc) - * @see org.springframework.data.document.mongodb.MongoOperations#query(java.lang.String, com.mongodb.DBObject, java.lang.Class, org.springframework.data.document.mongodb.MongoReader) + /** + * Map the results of an ad-hoc query on the default MongoDB collection to a List using the provided MongoReader + * + * The query document is specified as a standard DBObject and so is the fields specification. + * + * @param collectionName name of the collection to retrieve the objects from + * @param query the query document that specifies the criteria used to find a record + * @param fields the document that specifies the fields to be returned + * @param targetClass the parameterized type of the returned list. + * @param reader the MongoReader to convert from DBObject to an object. + * @return the List of converted objects. */ - public List find(String collectionName, DBObject query, DBObject fields, Class targetClass, MongoReader reader) { + protected List doFind(String collectionName, DBObject query, DBObject fields, Class targetClass, MongoReader reader) { return executeEach(new FindCallback(query, fields), null, new ReadDbObjectCallback(reader, targetClass), collectionName); } @@ -919,4 +853,5 @@ public class MongoTemplate implements InitializingBean, MongoOperations { return reader.read(type, object); } } + } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/BasicQuery.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/BasicQuery.java index f27b7f141..2fda08181 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/BasicQuery.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/BasicQuery.java @@ -18,7 +18,7 @@ package org.springframework.data.document.mongodb.builder; import com.mongodb.DBObject; import com.mongodb.util.JSON; -public class BasicQuery implements Query { +public class BasicQuery implements QueryDefinition { private DBObject queryObject = null; diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/BasicUpdate.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/BasicUpdate.java index e7cc94fa1..bb7dba149 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/BasicUpdate.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/BasicUpdate.java @@ -18,7 +18,7 @@ package org.springframework.data.document.mongodb.builder; import com.mongodb.DBObject; import com.mongodb.util.JSON; -public class BasicUpdate implements Update { +public class BasicUpdate implements UpdateDefinition { private DBObject updateObject = null; diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/Criteria.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/Criteria.java index 90720858f..61df52c46 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/Criteria.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/Criteria.java @@ -15,10 +15,147 @@ */ package org.springframework.data.document.mongodb.builder; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; + +import org.springframework.data.document.InvalidDocumentStoreApiUsageException; + +import com.mongodb.BasicDBObject; import com.mongodb.DBObject; -public interface Criteria { +public class Criteria implements CriteriaDefinition { + + private Query qb = null; + + private LinkedHashMap criteria = new LinkedHashMap(); - DBObject getCriteriaObject(String key); + private Object isValue = null; + + + public Criteria(Query qb) { + super(); + this.qb = qb; + } -} \ No newline at end of file + + public Criteria and(String key) { + return qb.find(key); + } + + + public Criteria is(Object o) { + if (isValue != null) { + throw new InvalidDocumentStoreApiUsageException("Multiple 'is' values declared."); + } + this.isValue = o; + return this; + } + + public Criteria lt(Object o) { + criteria.put("$lt", o); + return this; + } + + public Criteria lte(Object o) { + criteria.put("$lte", o); + return this; + } + + public Criteria gt(Object o) { + criteria.put("$gt", o); + return this; + } + + public Criteria gte(Object o) { + criteria.put("$gte", o); + return this; + } + + public Criteria in(Object... o) { + criteria.put("$in", o); + return this; + } + + public Criteria nin(Object... o) { + criteria.put("$min", o); + return this; + } + + public Criteria mod(Number value, Number remainder) { + List l = new ArrayList(); + l.add(value); + l.add(remainder); + criteria.put("$mod", l); + return this; + } + + public Criteria all(Object o) { + criteria.put("$is", o); + return this; + } + + public Criteria size(Object o) { + criteria.put("$is", o); + return this; + } + + public Criteria exists(boolean b) { + return this; + } + + public Criteria type(int t) { + return this; + } + + public Criteria not() { + criteria.put("$not", null); + return this; + } + + public Criteria regExp(String re) { + return this; + } + + public void or(List queries) { + criteria.put("$or", queries); + } + + public Query build() { + return qb; + } + + /* (non-Javadoc) + * @see org.springframework.datastore.document.mongodb.query.Criteria#getCriteriaObject(java.lang.String) + */ + public DBObject getCriteriaObject(String key) { + DBObject dbo = new BasicDBObject(); + boolean not = false; + for (String k : criteria.keySet()) { + if (not) { + DBObject notDbo = new BasicDBObject(); + notDbo.put(k, criteria.get(k)); + dbo.put("$not", notDbo); + not = false; + } + else { + if ("$not".equals(k)) { + not = true; + } + else { + dbo.put(k, criteria.get(k)); + } + } + } + DBObject queryCriteria = new BasicDBObject(); + if (isValue != null) { + queryCriteria.put(key, isValue); + queryCriteria.putAll(dbo); + } + else { + queryCriteria.put(key, dbo); + } + return queryCriteria; + } + +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/CriteriaDefinition.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/CriteriaDefinition.java new file mode 100644 index 000000000..8a926dbe7 --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/CriteriaDefinition.java @@ -0,0 +1,24 @@ +/* + * Copyright 2010-2011 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.document.mongodb.builder; + +import com.mongodb.DBObject; + +public interface CriteriaDefinition { + + DBObject getCriteriaObject(String key); + +} \ No newline at end of file diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/CriteriaSpec.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/CriteriaSpec.java deleted file mode 100644 index e122f2400..000000000 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/CriteriaSpec.java +++ /dev/null @@ -1,161 +0,0 @@ -/* - * Copyright 2010-2011 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.data.document.mongodb.builder; - -import java.util.ArrayList; -import java.util.LinkedHashMap; -import java.util.List; - -import org.springframework.data.document.InvalidDocumentStoreApiUsageException; - -import com.mongodb.BasicDBObject; -import com.mongodb.DBObject; - -public class CriteriaSpec implements Criteria { - - private QuerySpec qb = null; - - private LinkedHashMap criteria = new LinkedHashMap(); - - private Object isValue = null; - - - public CriteriaSpec(QuerySpec qb) { - super(); - this.qb = qb; - } - - - public CriteriaSpec and(String key) { - return qb.find(key); - } - - - public CriteriaSpec is(Object o) { - if (isValue != null) { - throw new InvalidDocumentStoreApiUsageException("Multiple 'is' values declared."); - } - this.isValue = o; - return this; - } - - public CriteriaSpec lt(Object o) { - criteria.put("$lt", o); - return this; - } - - public CriteriaSpec lte(Object o) { - criteria.put("$lte", o); - return this; - } - - public CriteriaSpec gt(Object o) { - criteria.put("$gt", o); - return this; - } - - public CriteriaSpec gte(Object o) { - criteria.put("$gte", o); - return this; - } - - public CriteriaSpec in(Object... o) { - criteria.put("$in", o); - return this; - } - - public CriteriaSpec nin(Object... o) { - criteria.put("$min", o); - return this; - } - - public CriteriaSpec mod(Number value, Number remainder) { - List l = new ArrayList(); - l.add(value); - l.add(remainder); - criteria.put("$mod", l); - return this; - } - - public CriteriaSpec all(Object o) { - criteria.put("$is", o); - return this; - } - - public CriteriaSpec size(Object o) { - criteria.put("$is", o); - return this; - } - - public CriteriaSpec exists(boolean b) { - return this; - } - - public CriteriaSpec type(int t) { - return this; - } - - public CriteriaSpec not() { - criteria.put("$not", null); - return this; - } - - public CriteriaSpec regExp(String re) { - return this; - } - - public void or(List queries) { - criteria.put("$or", queries); - } - - public Query build() { - return qb.build(); - } - - /* (non-Javadoc) - * @see org.springframework.datastore.document.mongodb.query.Criteria#getCriteriaObject(java.lang.String) - */ - public DBObject getCriteriaObject(String key) { - DBObject dbo = new BasicDBObject(); - boolean not = false; - for (String k : criteria.keySet()) { - if (not) { - DBObject notDbo = new BasicDBObject(); - notDbo.put(k, criteria.get(k)); - dbo.put("$not", notDbo); - not = false; - } - else { - if ("$not".equals(k)) { - not = true; - } - else { - dbo.put(k, criteria.get(k)); - } - } - } - DBObject queryCriteria = new BasicDBObject(); - if (isValue != null) { - queryCriteria.put(key, isValue); - queryCriteria.putAll(dbo); - } - else { - queryCriteria.put(key, dbo); - } - return queryCriteria; - } - -} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/FieldSpec.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/Field.java similarity index 87% rename from spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/FieldSpec.java rename to spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/Field.java index db0b3572a..899bc1584 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/FieldSpec.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/Field.java @@ -22,28 +22,28 @@ import com.mongodb.BasicDBObject; import com.mongodb.DBObject; -public class FieldSpec { +public class Field { private Map criteria = new HashMap(); private Map slices = new HashMap(); - public FieldSpec include(String key) { + public Field include(String key) { criteria.put(key, Integer.valueOf(1)); return this; } - public FieldSpec exclude(String key) { + public Field exclude(String key) { criteria.put(key, Integer.valueOf(0)); return this; } - public FieldSpec slice(String key, int size) { + public Field slice(String key, int size) { slices.put(key, Integer.valueOf(size)); return this; } - public FieldSpec slice(String key, int offset, int size) { + public Field slice(String key, int offset, int size) { slices.put(key, new Integer[] {Integer.valueOf(offset), Integer.valueOf(size)}); return this; } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/OrCriteriaSpec.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/OrCriteria.java similarity index 86% rename from spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/OrCriteriaSpec.java rename to spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/OrCriteria.java index 281c482c4..41e69a34c 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/OrCriteriaSpec.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/OrCriteria.java @@ -20,11 +20,11 @@ import org.bson.types.BasicBSONList; import com.mongodb.BasicDBObject; import com.mongodb.DBObject; -public class OrCriteriaSpec implements Criteria { +public class OrCriteria implements CriteriaDefinition { - Query[] queries = null; + QueryDefinition[] queries = null; - public OrCriteriaSpec(Query[] queries) { + public OrCriteria(QueryDefinition[] queries) { super(); this.queries = queries; } @@ -36,7 +36,7 @@ public class OrCriteriaSpec implements Criteria { public DBObject getCriteriaObject(String key) { DBObject dbo = new BasicDBObject(); BasicBSONList l = new BasicBSONList(); - for (Query q : queries) { + for (QueryDefinition q : queries) { l.add(q.getQueryObject()); } dbo.put(key, l); diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/Query.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/Query.java index f383f0b10..74c05ef41 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/Query.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/Query.java @@ -15,14 +15,71 @@ */ package org.springframework.data.document.mongodb.builder; +import java.util.LinkedHashMap; + +import com.mongodb.BasicDBObject; import com.mongodb.DBObject; - -public interface Query { +public class Query implements QueryDefinition { - DBObject getQueryObject(); - - DBObject getFieldsObject(); + private LinkedHashMap criteria = new LinkedHashMap(); - int getLimit(); + private Field fieldSpec; + + private int limit; + + public static Criteria newQuery(String key) { + return new Query().find(key); + } + + public Criteria find(String key) { + Criteria c = new Criteria(this); + this.criteria.put(key, c); + return c; + } + + public Query or(QueryDefinition... queries) { + this.criteria.put("$or", new OrCriteria(queries)); + return this; + } + + public Field fields() { + synchronized (this) { + if (fieldSpec == null) { + this.fieldSpec = new Field(); + } + } + return this.fieldSpec; + } + + public Query limit(int limit) { + this.limit = limit; + return this; + } + + public QueryDefinition build() { + return this; + } + + public DBObject getQueryObject() { + DBObject dbo = new BasicDBObject(); + for (String k : criteria.keySet()) { + CriteriaDefinition c = criteria.get(k); + DBObject cl = c.getCriteriaObject(k); + dbo.putAll(cl); + } + return dbo; + } + + public DBObject getFieldsObject() { + if (this.fieldSpec == null) { + return null; + } + return fieldSpec.getFieldsObject(); + } + + public int getLimit() { + return this.limit; + } + } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/BasicSort.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/QueryDefinition.java similarity index 63% rename from spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/BasicSort.java rename to spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/QueryDefinition.java index 2356e6d35..1ba084df8 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/BasicSort.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/QueryDefinition.java @@ -1,5 +1,5 @@ /* - * Copyright 2011 the original author or authors. + * Copyright 2010-2011 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. @@ -16,22 +16,13 @@ package org.springframework.data.document.mongodb.builder; import com.mongodb.DBObject; -import com.mongodb.util.JSON; -public class BasicSort implements Sort { - private DBObject sortObject; +public interface QueryDefinition { - public BasicSort(String sortString) { - this.sortObject = (DBObject) JSON.parse(sortString); - } + DBObject getQueryObject(); + + DBObject getFieldsObject(); - public BasicSort(DBObject sortObject) { - this.sortObject = sortObject; - } - - public DBObject getSortObject() { - return this.sortObject; - } - + int getLimit(); } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/QuerySpec.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/QuerySpec.java deleted file mode 100644 index 07e60e9b0..000000000 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/QuerySpec.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright 2010-2011 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.data.document.mongodb.builder; - -import java.util.LinkedHashMap; - -import com.mongodb.BasicDBObject; -import com.mongodb.DBObject; - -public class QuerySpec implements Query { - - private LinkedHashMap criteria = new LinkedHashMap(); - - private FieldSpec fieldSpec; - - private int limit; - - public CriteriaSpec find(String key) { - CriteriaSpec c = new CriteriaSpec(this); - this.criteria.put(key, c); - return c; - } - - public QuerySpec or(Query... queries) { - this.criteria.put("$or", new OrCriteriaSpec(queries)); - return this; - } - - public FieldSpec fields() { - synchronized (this) { - if (fieldSpec == null) { - this.fieldSpec = new FieldSpec(); - } - } - return this.fieldSpec; - } - - public QuerySpec limit(int limit) { - this.limit = limit; - return this; - } - - public Query build() { - return this; - } - - public DBObject getQueryObject() { - DBObject dbo = new BasicDBObject(); - for (String k : criteria.keySet()) { - Criteria c = criteria.get(k); - DBObject cl = c.getCriteriaObject(k); - dbo.putAll(cl); - } - return dbo; - } - - public DBObject getFieldsObject() { - if (this.fieldSpec == null) { - return null; - } - return fieldSpec.getFieldsObject(); - } - - public int getLimit() { - return this.limit; - } - -} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/Sort.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/Sort.java index bf655815e..35ffa686e 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/Sort.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/Sort.java @@ -1,5 +1,5 @@ /* - * Copyright 2011 the original author or authors. + * Copyright 2010-2011 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,10 +15,32 @@ */ package org.springframework.data.document.mongodb.builder; +import java.util.HashMap; +import java.util.Map; + +import com.mongodb.BasicDBObject; import com.mongodb.DBObject; -public interface Sort { +public class Sort { - DBObject getSortObject(); + public enum Order { + ASCENDING, DESCENDING + } + + private Map criteria = new HashMap(); + + public Sort on(String key, Order order) { + criteria.put(key, order); + return this; + } + + public DBObject getSortObject() { + DBObject dbo = new BasicDBObject(); + for (String k : criteria.keySet()) { + dbo.put(k, (criteria.get(k).equals(Order.ASCENDING) ? 1 : -1)); + } + return dbo; + } + } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/Update.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/Update.java index c15fcd949..8c1e0aa3b 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/Update.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/Update.java @@ -15,10 +15,85 @@ */ package org.springframework.data.document.mongodb.builder; +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedHashMap; + +import com.mongodb.BasicDBObject; import com.mongodb.DBObject; +public class Update implements UpdateDefinition { -public interface Update { + public enum Position { + LAST, FIRST + } + + private HashMap criteria = new LinkedHashMap(); - DBObject getUpdateObject(); + public Update set(String key, Object value) { + criteria.put("$set", Collections.singletonMap(key, value)); + return this; + } + + public Update unset(String key) { + criteria.put("$unset", Collections.singletonMap(key, 1)); + return this; + } + + public Update inc(String key, long inc) { + criteria.put("$inc", Collections.singletonMap(key, inc)); + return this; + } + + public Update push(String key, Object value) { + criteria.put("$push", Collections.singletonMap(key, value)); + return this; + } + + public Update pushAll(String key, Object[] values) { + DBObject keyValue = new BasicDBObject(); + keyValue.put(key, values); + criteria.put("$pushAll", keyValue); + return this; + } + + public Update addToSet(String key, Object value) { + criteria.put("$addToSet", Collections.singletonMap(key, value)); + return this; + } + + public Update pop(String key, Position pos) { + criteria.put("$pop", Collections.singletonMap(key, (pos == Position.FIRST ? -1 : 1))); + return this; + } + + public Update pull(String key, Object value) { + criteria.put("$pull", Collections.singletonMap(key, value)); + return this; + } + + public Update pullAll(String key, Object[] values) { + DBObject keyValue = new BasicDBObject(); + keyValue.put(key, values); + criteria.put("$pullAll", keyValue); + return this; + } + + public Update rename(String oldName, String newName) { + criteria.put("$rename", Collections.singletonMap(oldName, newName)); + return this; + } + + public UpdateDefinition build() { + return this; + } + + public DBObject getUpdateObject() { + DBObject dbo = new BasicDBObject(); + for (String k : criteria.keySet()) { + dbo.put(k, criteria.get(k)); + } + return dbo; + } + } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/SortSpec.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/UpdateDefinition.java similarity index 54% rename from spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/SortSpec.java rename to spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/UpdateDefinition.java index 94157d570..7ccff6389 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/SortSpec.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/UpdateDefinition.java @@ -15,36 +15,10 @@ */ package org.springframework.data.document.mongodb.builder; -import java.util.HashMap; -import java.util.Map; - -import com.mongodb.BasicDBObject; import com.mongodb.DBObject; -public class SortSpec implements Sort { +public interface UpdateDefinition { - public enum SortOrder { - ASCENDING, DESCENDING - } - - private Map criteria = new HashMap(); - - public SortSpec on(String key, SortOrder order) { - criteria.put(key, order); - return this; - } - - public Sort build() { - return this; - } - - public DBObject getSortObject() { - DBObject dbo = new BasicDBObject(); - for (String k : criteria.keySet()) { - dbo.put(k, (criteria.get(k).equals(SortOrder.ASCENDING) ? 1 : -1)); - } - return dbo; - } - + DBObject getUpdateObject(); } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/UpdateSpec.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/UpdateSpec.java deleted file mode 100644 index 80c4f89a6..000000000 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/UpdateSpec.java +++ /dev/null @@ -1,99 +0,0 @@ -/* - * Copyright 2010-2011 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.data.document.mongodb.builder; - -import java.util.Collections; -import java.util.HashMap; -import java.util.LinkedHashMap; - -import com.mongodb.BasicDBObject; -import com.mongodb.DBObject; - -public class UpdateSpec implements Update { - - public enum Position { - LAST, FIRST - } - - private HashMap criteria = new LinkedHashMap(); - - public UpdateSpec set(String key, Object value) { - criteria.put("$set", Collections.singletonMap(key, value)); - return this; - } - - public UpdateSpec unset(String key) { - criteria.put("$unset", Collections.singletonMap(key, 1)); - return this; - } - - public UpdateSpec inc(String key, long inc) { - criteria.put("$inc", Collections.singletonMap(key, inc)); - return this; - } - - public UpdateSpec push(String key, Object value) { - criteria.put("$push", Collections.singletonMap(key, value)); - return this; - } - - public UpdateSpec pushAll(String key, Object[] values) { - DBObject keyValue = new BasicDBObject(); - keyValue.put(key, values); - criteria.put("$pushAll", keyValue); - return this; - } - - public UpdateSpec addToSet(String key, Object value) { - criteria.put("$addToSet", Collections.singletonMap(key, value)); - return this; - } - - public UpdateSpec pop(String key, Position pos) { - criteria.put("$pop", Collections.singletonMap(key, (pos == Position.FIRST ? -1 : 1))); - return this; - } - - public UpdateSpec pull(String key, Object value) { - criteria.put("$pull", Collections.singletonMap(key, value)); - return this; - } - - public UpdateSpec pullAll(String key, Object[] values) { - DBObject keyValue = new BasicDBObject(); - keyValue.put(key, values); - criteria.put("$pullAll", keyValue); - return this; - } - - public UpdateSpec rename(String oldName, String newName) { - criteria.put("$rename", Collections.singletonMap(oldName, newName)); - return this; - } - - public Update build() { - return this; - } - - public DBObject getUpdateObject() { - DBObject dbo = new BasicDBObject(); - for (String k : criteria.keySet()) { - dbo.put(k, criteria.get(k)); - } - return dbo; - } - -} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/MongoQuery.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/MongoQuery.java index a195587eb..089806504 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/MongoQuery.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/MongoQuery.java @@ -21,7 +21,7 @@ import java.util.List; import org.springframework.data.document.mongodb.CollectionCallback; import org.springframework.data.document.mongodb.MongoTemplate; -import org.springframework.data.document.mongodb.builder.QuerySpec; +import org.springframework.data.document.mongodb.builder.Query; import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.Pageable; import org.springframework.data.repository.query.QueryMethod; @@ -76,7 +76,7 @@ public class MongoQuery implements RepositoryQuery { SimpleParameterAccessor accessor = new SimpleParameterAccessor(method.getParameters(), parameters); - QuerySpec spec = new QuerySpec(); + Query spec = new Query(); MongoQueryCreator creator = new MongoQueryCreator(spec, tree, accessor, @@ -95,12 +95,12 @@ public class MongoQuery implements RepositoryQuery { private abstract class Execution { - abstract Object execute(QuerySpec query); + abstract Object execute(Query query); - protected List readCollection(QuerySpec query) { + protected List readCollection(Query query) { - return template.query(query.build(), method.getDomainClass()); + return template.find(query.build(), method.getDomainClass()); } } @@ -119,7 +119,7 @@ public class MongoQuery implements RepositoryQuery { * #execute(com.mongodb.DBObject) */ @Override - public Object execute(QuerySpec query) { + public Object execute(Query query) { return readCollection(query); } @@ -159,12 +159,12 @@ public class MongoQuery implements RepositoryQuery { */ @Override @SuppressWarnings({ "rawtypes", "unchecked" }) - Object execute(QuerySpec query) { + Object execute(Query query) { int count = getCollectionCursor(query.getQueryObject()).count(); List result = - template.query(applyPagination(query, pageable), + template.find(applyPagination(query, pageable), method.getDomainClass()); return new PageImpl(result, pageable, count); @@ -198,7 +198,7 @@ public class MongoQuery implements RepositoryQuery { * #execute(com.mongodb.DBObject) */ @Override - Object execute(QuerySpec query) { + Object execute(Query query) { List result = readCollection(query); return result.isEmpty() ? null : result.get(0); diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/MongoQueryCreator.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/MongoQueryCreator.java index 36f54eeac..63f7e4dfa 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/MongoQueryCreator.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/MongoQueryCreator.java @@ -21,10 +21,10 @@ import java.util.regex.Pattern; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.data.document.mongodb.MongoConverter; +import org.springframework.data.document.mongodb.builder.CriteriaDefinition; import org.springframework.data.document.mongodb.builder.Criteria; -import org.springframework.data.document.mongodb.builder.CriteriaSpec; +import org.springframework.data.document.mongodb.builder.QueryDefinition; import org.springframework.data.document.mongodb.builder.Query; -import org.springframework.data.document.mongodb.builder.QuerySpec; import org.springframework.data.domain.Sort; import org.springframework.data.repository.query.SimpleParameterAccessor; import org.springframework.data.repository.query.SimpleParameterAccessor.BindableParameterIterator; @@ -42,11 +42,11 @@ import com.mongodb.DBObject; * * @author Oliver Gierke */ -class MongoQueryCreator extends AbstractQueryCreator { +class MongoQueryCreator extends AbstractQueryCreator { private static final Log LOG = LogFactory.getLog(MongoQueryCreator.class); private final MongoConverter converter; - private final QuerySpec querySpec; + private final Query querySpec; /** @@ -56,7 +56,7 @@ class MongoQueryCreator extends AbstractQueryCreator { * @param tree * @param accessor */ - public MongoQueryCreator(QuerySpec querySpec, PartTree tree, + public MongoQueryCreator(Query querySpec, PartTree tree, SimpleParameterAccessor accessor, MongoConverter converter) { super(tree, accessor); @@ -75,7 +75,7 @@ class MongoQueryCreator extends AbstractQueryCreator { * .data.repository.query.SimpleParameterAccessor.BindableParameterIterator) */ @Override - protected CriteriaSpec create(Part part, BindableParameterIterator iterator) { + protected Criteria create(Part part, BindableParameterIterator iterator) { return from(part.getType(), querySpec.find(part.getProperty().toDotPath()), iterator); @@ -92,7 +92,7 @@ class MongoQueryCreator extends AbstractQueryCreator { * .data.repository.query.SimpleParameterAccessor.BindableParameterIterator) */ @Override - protected CriteriaSpec and(Part part, CriteriaSpec base, + protected Criteria and(Part part, Criteria base, BindableParameterIterator iterator) { return from(part.getType(), base.and(part.getProperty().toDotPath()), @@ -108,7 +108,7 @@ class MongoQueryCreator extends AbstractQueryCreator { * #or(java.lang.Object, java.lang.Object) */ @Override - protected CriteriaSpec or(CriteriaSpec base, CriteriaSpec criteria) { + protected Criteria or(Criteria base, Criteria criteria) { base.or(Arrays.asList(criteria.build())); return base; @@ -123,7 +123,7 @@ class MongoQueryCreator extends AbstractQueryCreator { * #complete(java.lang.Object, org.springframework.data.domain.Sort) */ @Override - protected Void complete(CriteriaSpec criteria, Sort sort) { + protected Void complete(Criteria criteria, Sort sort) { Query query = criteria.build(); @@ -136,14 +136,14 @@ class MongoQueryCreator extends AbstractQueryCreator { /** - * Populates the given {@link Criteria} depending on the {@link Type} given. + * Populates the given {@link CriteriaDefinition} depending on the {@link Type} given. * * @param type * @param criteria * @param parameters * @return */ - private CriteriaSpec from(Type type, CriteriaSpec criteria, + private Criteria from(Type type, Criteria criteria, BindableParameterIterator parameters) { switch (type) { diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/QueryUtils.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/QueryUtils.java index d2411d152..fb66a6480 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/QueryUtils.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/QueryUtils.java @@ -15,7 +15,7 @@ */ package org.springframework.data.document.mongodb.repository; -import org.springframework.data.document.mongodb.builder.QuerySpec; +import org.springframework.data.document.mongodb.builder.Query; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; @@ -36,14 +36,14 @@ abstract class QueryUtils { /** - * Applies the given {@link Pageable} to the given {@link QuerySpec}. Will + * Applies the given {@link Pageable} to the given {@link Query}. Will * do nothing if {@link Pageable} is {@literal null}. * * @param spec * @param pageable * @return */ - public static QuerySpec applyPagination(QuerySpec spec, Pageable pageable) { + public static Query applyPagination(Query spec, Pageable pageable) { if (pageable == null) { return spec; @@ -57,14 +57,14 @@ abstract class QueryUtils { /** - * Applies the given {@link Sort} to the {@link QuerySpec}. Will do nothing + * Applies the given {@link Sort} to the {@link Query}. Will do nothing * if {@link Sort} is {@literal null}. * * @param spec * @param sort * @return */ - public static QuerySpec applySorting(QuerySpec spec, Sort sort) { + public static Query applySorting(Query spec, Sort sort) { if (sort == null) { return spec; diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/SimpleMongoRepository.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/SimpleMongoRepository.java index 4dd7c4cf3..9fe46b4da 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/SimpleMongoRepository.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/SimpleMongoRepository.java @@ -22,7 +22,7 @@ import java.util.List; import org.bson.types.ObjectId; import org.springframework.data.document.mongodb.MongoConverter; import org.springframework.data.document.mongodb.MongoTemplate; -import org.springframework.data.document.mongodb.builder.QuerySpec; +import org.springframework.data.document.mongodb.builder.Query; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.Pageable; @@ -109,8 +109,8 @@ public class SimpleMongoRepository extends ObjectId objectId = converter.convertObjectId(id); List result = - template.query( - new QuerySpec().find("_id").is(objectId).build(), + template.find( + new Query().find("_id").is(objectId).build(), getDomainClass()); return result.isEmpty() ? null : result.get(0); } @@ -202,10 +202,10 @@ public class SimpleMongoRepository extends public Page findAll(final Pageable pageable) { Long count = count(); - QuerySpec spec = new QuerySpec(); + Query spec = new Query(); List list = - template.query(QueryUtils.applyPagination(spec, pageable), + template.find(QueryUtils.applyPagination(spec, pageable), getDomainClass()); return new PageImpl(list, pageable, count); @@ -221,8 +221,8 @@ public class SimpleMongoRepository extends */ public List findAll(final Sort sort) { - QuerySpec query = QueryUtils.applySorting(new QuerySpec(), sort); - return template.query(query, getDomainClass()); + Query query = QueryUtils.applySorting(new Query(), sort); + return template.find(query, getDomainClass()); } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/MongoTemplateTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/MongoTemplateTests.java index 21bbfa3fa..609147320 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/MongoTemplateTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/MongoTemplateTests.java @@ -24,6 +24,7 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.document.mongodb.builder.Query; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @@ -54,7 +55,7 @@ public class MongoTemplateTests { MongoConverter converter = template.getConverter(); - List result = template.find(QueryBuilder.start("_id").is(converter.convertObjectId(person.getId())).get(), Person.class); + List result = template.find(Query.newQuery("_id").is(converter.convertObjectId(person.getId())).build(), Person.class); assertThat(result.size(), is(1)); assertThat(result, hasItem(person)); } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/analytics/MvcAnalyticsTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/analytics/MvcAnalyticsTests.java index 2b938e12a..f026d358f 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/analytics/MvcAnalyticsTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/analytics/MvcAnalyticsTests.java @@ -88,7 +88,7 @@ public class MvcAnalyticsTests { for (DBObject dbo : mongoTemplate.getCollection("counters").find(query)) { System.out.println(dbo); } - List counters = mongoTemplate.query("counters", new BasicQuery("{ 'name' : 'SignUpController'} "), ControllerCounter.class); + List counters = mongoTemplate.find("counters", new BasicQuery("{ 'name' : 'SignUpController'} "), ControllerCounter.class); for (ControllerCounter controllerCounter : counters) { System.out.println(controllerCounter); } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/builder/QueryTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/builder/QueryTests.java index 5012a946a..a412a3d14 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/builder/QueryTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/builder/QueryTests.java @@ -22,7 +22,7 @@ public class QueryTests { @Test public void testSimpleQuery() { - QuerySpec q = new QuerySpec(); + Query q = new Query(); q.find("name").is("Thomas"); q.find("age").lt(80); String expected = "{ \"name\" : \"Thomas\" , \"age\" : { \"$lt\" : 80}}"; @@ -31,7 +31,7 @@ public class QueryTests { @Test public void testQueryWithNot() { - QuerySpec q = new QuerySpec(); + Query q = new Query(); q.find("name").is("Thomas"); q.find("age").not().mod(10, 0); String expected = "{ \"name\" : \"Thomas\" , \"age\" : { \"$not\" : { \"$mod\" : [ 10 , 0]}}}"; @@ -40,10 +40,10 @@ public class QueryTests { @Test public void testOrQuery() { - QuerySpec q = new QuerySpec();; + Query q = new Query();; q.or( - new QuerySpec().find("name").is("Sven").and("age").lt(50).build(), - new QuerySpec().find("age").lt(50).build(), + new Query().find("name").is("Sven").and("age").lt(50).build(), + new Query().find("age").lt(50).build(), new BasicQuery("{'name' : 'Thomas'}") ); String expected = "{ \"$or\" : [ { \"name\" : \"Sven\" , \"age\" : { \"$lt\" : 50}} , { \"age\" : { \"$lt\" : 50}} , { \"name\" : \"Thomas\"}]}"; @@ -52,7 +52,7 @@ public class QueryTests { @Test public void testQueryWithLimit() { - QuerySpec q = new QuerySpec(); + Query q = new Query(); q.find("name").gte("M").lte("T").and("age").not().gt(22); q.limit(50); String expected = "{ \"name\" : { \"$gte\" : \"M\" , \"$lte\" : \"T\"} , \"age\" : { \"$not\" : { \"$gt\" : 22}}}"; @@ -62,7 +62,7 @@ public class QueryTests { @Test public void testQueryWithFieldsAndSlice() { - QuerySpec q = new QuerySpec(); + Query q = new Query(); q.find("name").gte("M").lte("T").and("age").not().gt(22); q.fields().exclude("address").include("name").slice("orders", 10); diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/builder/SortTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/builder/SortTests.java index bad997c7e..0f6766d75 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/builder/SortTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/builder/SortTests.java @@ -17,20 +17,20 @@ package org.springframework.data.document.mongodb.builder; import org.junit.Assert; import org.junit.Test; -import org.springframework.data.document.mongodb.builder.SortSpec.SortOrder; +import org.springframework.data.document.mongodb.builder.Sort.Order; public class SortTests { @Test public void testWithSortAscending() { - SortSpec s = new SortSpec().on("name", SortOrder.ASCENDING); - Assert.assertEquals("{ \"name\" : 1}", s.build().getSortObject().toString()); + Sort s = new Sort().on("name", Order.ASCENDING); + Assert.assertEquals("{ \"name\" : 1}", s.getSortObject().toString()); } @Test public void testWithSortDescending() { - SortSpec s = new SortSpec().on("name", SortOrder.DESCENDING); - Assert.assertEquals("{ \"name\" : -1}", s.build().getSortObject().toString()); + Sort s = new Sort().on("name", Order.DESCENDING); + Assert.assertEquals("{ \"name\" : -1}", s.getSortObject().toString()); } } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/builder/UpdateTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/builder/UpdateTests.java index 247f13aac..30b9bae25 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/builder/UpdateTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/builder/UpdateTests.java @@ -20,28 +20,28 @@ import java.util.Map; import org.junit.Assert; import org.junit.Test; -import org.springframework.data.document.mongodb.builder.SortSpec.SortOrder; -import org.springframework.data.document.mongodb.builder.UpdateSpec; +import org.springframework.data.document.mongodb.builder.Sort.Order; +import org.springframework.data.document.mongodb.builder.Update; public class UpdateTests { @Test public void testSet() { - UpdateSpec u = new UpdateSpec() + Update u = new Update() .set("directory", "/Users/Test/Desktop"); Assert.assertEquals("{ \"$set\" : { \"directory\" : \"/Users/Test/Desktop\"}}", u.build().getUpdateObject().toString()); } @Test public void testInc() { - UpdateSpec u = new UpdateSpec() + Update u = new Update() .inc("size", 1); Assert.assertEquals("{ \"$inc\" : { \"size\" : 1}}", u.build().getUpdateObject().toString()); } @Test public void testIncAndSet() { - UpdateSpec u = new UpdateSpec() + Update u = new Update() .inc("size", 1) .set("directory", "/Users/Test/Desktop"); Assert.assertEquals("{ \"$inc\" : { \"size\" : 1} , \"$set\" : { \"directory\" : \"/Users/Test/Desktop\"}}", @@ -50,7 +50,7 @@ public class UpdateTests { @Test public void testUnset() { - UpdateSpec u = new UpdateSpec() + Update u = new Update() .unset("directory"); Assert.assertEquals("{ \"$unset\" : { \"directory\" : 1}}", u.build().getUpdateObject().toString()); } @@ -59,7 +59,7 @@ public class UpdateTests { public void testPush() { Map m = new HashMap(); m.put("name", "Sven"); - UpdateSpec u = new UpdateSpec() + Update u = new Update() .push("authors", m); Assert.assertEquals("{ \"$push\" : { \"authors\" : { \"name\" : \"Sven\"}}}", u.build().getUpdateObject().toString()); } @@ -70,7 +70,7 @@ public class UpdateTests { m1.put("name", "Sven"); Map m2 = new HashMap(); m2.put("name", "Maria"); - UpdateSpec u = new UpdateSpec() + Update u = new Update() .pushAll("authors", new Object[] {m1, m2}); Assert.assertEquals("{ \"$pushAll\" : { \"authors\" : [ { \"name\" : \"Sven\"} , { \"name\" : \"Maria\"}]}}", u.build().getUpdateObject().toString()); } @@ -79,18 +79,18 @@ public class UpdateTests { public void testAddToSet() { Map m = new HashMap(); m.put("name", "Sven"); - UpdateSpec u = new UpdateSpec() + Update u = new Update() .addToSet("authors", m); Assert.assertEquals("{ \"$addToSet\" : { \"authors\" : { \"name\" : \"Sven\"}}}", u.build().getUpdateObject().toString()); } @Test public void testPop() { - UpdateSpec u = new UpdateSpec() - .pop("authors", UpdateSpec.Position.FIRST); + Update u = new Update() + .pop("authors", Update.Position.FIRST); Assert.assertEquals("{ \"$pop\" : { \"authors\" : -1}}", u.build().getUpdateObject().toString()); - u = new UpdateSpec() - .pop("authors", UpdateSpec.Position.LAST); + u = new Update() + .pop("authors", Update.Position.LAST); Assert.assertEquals("{ \"$pop\" : { \"authors\" : 1}}", u.build().getUpdateObject().toString()); } @@ -98,7 +98,7 @@ public class UpdateTests { public void testPull() { Map m = new HashMap(); m.put("name", "Sven"); - UpdateSpec u = new UpdateSpec() + Update u = new Update() .pull("authors", m); Assert.assertEquals("{ \"$pull\" : { \"authors\" : { \"name\" : \"Sven\"}}}", u.build().getUpdateObject().toString()); } @@ -109,14 +109,14 @@ public class UpdateTests { m1.put("name", "Sven"); Map m2 = new HashMap(); m2.put("name", "Maria"); - UpdateSpec u = new UpdateSpec() + Update u = new Update() .pullAll("authors", new Object[] {m1, m2}); Assert.assertEquals("{ \"$pullAll\" : { \"authors\" : [ { \"name\" : \"Sven\"} , { \"name\" : \"Maria\"}]}}", u.build().getUpdateObject().toString()); } @Test public void testRename() { - UpdateSpec u = new UpdateSpec() + Update u = new Update() .rename("directory", "folder"); Assert.assertEquals("{ \"$rename\" : { \"directory\" : \"folder\"}}", u.build().getUpdateObject().toString()); } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/repository/MongoQueryCreatorUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/repository/MongoQueryCreatorUnitTests.java index 9a10cf72b..6634bfce1 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/repository/MongoQueryCreatorUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/repository/MongoQueryCreatorUnitTests.java @@ -25,7 +25,7 @@ import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; import org.springframework.data.document.mongodb.MongoConverter; import org.springframework.data.document.mongodb.Person; -import org.springframework.data.document.mongodb.builder.QuerySpec; +import org.springframework.data.document.mongodb.builder.Query; import org.springframework.data.repository.query.Parameters; import org.springframework.data.repository.query.SimpleParameterAccessor; import org.springframework.data.repository.query.parser.PartTree; @@ -64,7 +64,7 @@ public class MongoQueryCreatorUnitTests { PartTree tree = new PartTree("findByFirstName", Person.class); MongoQueryCreator creator = - new MongoQueryCreator(new QuerySpec(), tree, + new MongoQueryCreator(new Query(), tree, new SimpleParameterAccessor(new Parameters( findByFirstname), new Object[] { "Oliver" }), converter); @@ -72,7 +72,7 @@ public class MongoQueryCreatorUnitTests { creator.createQuery(); creator = - new MongoQueryCreator(new QuerySpec(), new PartTree( + new MongoQueryCreator(new Query(), new PartTree( "findByFirstNameAndFriend", Person.class), new SimpleParameterAccessor(new Parameters( findByFirstnameAndFriend), new Object[] {