From 38ca06bd4903b33a2d1f8535da40e68e2b5ab7c2 Mon Sep 17 00:00:00 2001 From: Thomas Risberg Date: Fri, 3 Dec 2010 09:33:27 -0500 Subject: [PATCH 01/12] DATADOC-23 removed DocumentSource and its subclasses --- .../document/couchdb/CouchDocumentSource.java | 8 ----- .../data/document/couchdb/CouchTemplate.java | 6 ++-- .../data/document/DocumentSource.java | 29 ------------------- .../document/mongodb/MongoDocumentSource.java | 24 --------------- 4 files changed, 2 insertions(+), 65 deletions(-) delete mode 100644 spring-data-couchdb/src/main/java/org/springframework/data/document/couchdb/CouchDocumentSource.java delete mode 100644 spring-data-document-core/src/main/java/org/springframework/data/document/DocumentSource.java delete mode 100644 spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoDocumentSource.java diff --git a/spring-data-couchdb/src/main/java/org/springframework/data/document/couchdb/CouchDocumentSource.java b/spring-data-couchdb/src/main/java/org/springframework/data/document/couchdb/CouchDocumentSource.java deleted file mode 100644 index e6c44d5d1..000000000 --- a/spring-data-couchdb/src/main/java/org/springframework/data/document/couchdb/CouchDocumentSource.java +++ /dev/null @@ -1,8 +0,0 @@ -package org.springframework.data.document.couchdb; - -import org.jcouchdb.document.BaseDocument; -import org.springframework.data.document.DocumentSource; - -public interface CouchDocumentSource extends DocumentSource { - -} diff --git a/spring-data-couchdb/src/main/java/org/springframework/data/document/couchdb/CouchTemplate.java b/spring-data-couchdb/src/main/java/org/springframework/data/document/couchdb/CouchTemplate.java index ca4bcc7ba..0e646ab02 100644 --- a/spring-data-couchdb/src/main/java/org/springframework/data/document/couchdb/CouchTemplate.java +++ b/spring-data-couchdb/src/main/java/org/springframework/data/document/couchdb/CouchTemplate.java @@ -20,7 +20,6 @@ package org.springframework.data.document.couchdb; import org.jcouchdb.db.Database; import org.jcouchdb.document.BaseDocument; import org.springframework.data.document.AbstractDocumentStoreTemplate; -import org.springframework.data.document.DocumentSource; public class CouchTemplate extends AbstractDocumentStoreTemplate { @@ -40,9 +39,8 @@ import org.springframework.data.document.DocumentSource; this.database = database; } - public void save(DocumentSource documentSource) { - BaseDocument d = documentSource.getDocument(); - getConnection().createDocument(d); + public void save(BaseDocument document) { + getConnection().createDocument(document); } @Override diff --git a/spring-data-document-core/src/main/java/org/springframework/data/document/DocumentSource.java b/spring-data-document-core/src/main/java/org/springframework/data/document/DocumentSource.java deleted file mode 100644 index 87dcaea5d..000000000 --- a/spring-data-document-core/src/main/java/org/springframework/data/document/DocumentSource.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright 2010 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; - -/** - * Class used to map a business object to an object providing the source data for a Document. - * - * @author Thomas Risberg - * @since 1.0 - */ -public interface DocumentSource { - - D getDocument(); - -} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoDocumentSource.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoDocumentSource.java deleted file mode 100644 index 22fd66be6..000000000 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoDocumentSource.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright 2010 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; - -import org.springframework.data.document.DocumentSource; - -import com.mongodb.DBObject; - -public interface MongoDocumentSource extends DocumentSource { - -} From 7599a2d0b74e1293e1fc110186de93cea982d35f Mon Sep 17 00:00:00 2001 From: Mark Pollack Date: Fri, 3 Dec 2010 10:50:52 -0500 Subject: [PATCH 02/12] Add initial README --- README.md | 146 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 000000000..0696d6d6e --- /dev/null +++ b/README.md @@ -0,0 +1,146 @@ +Spring Data - Document +====================== + +The primary goal of the [Spring Data](http://www.springsource.org/spring-data) project is to make it easier to build Spring-powered applications that use new data access technologies such as non-relational databases, map-reduce frameworks, and cloud based data services. +As the name implies, the **Document** modules provides integration with document databases such as [MongoDB](http://www.mongodb.org/) and [CouchDB](http://couchdb.apache.org/). + +Getting Help +------------ + +Read the main project [website](http://www.springsource.org/spring-data)) and the [User Guide](http://static.springsource.org/spring-data/datastore-keyvalue/snapshot-site/reference/html/) (A work in progress). + +At this point your best bet is to look at the Look at the [JavaDocs](http://static.springsource.org/spring-data/data-document/docs/1.0.0.BUILD-SNAPSHOT/spring-data-mongodb/apidocs/) for MongoDB integration and corresponding and source code. For more detailed questions, use the [forum](http://forum.springsource.org/forumdisplay.php?f=80). If you are new to Spring as well as to Spring Data, look for information about [Spring projects](http://www.springsource.org/projects). + +Quick Start +----------- + +## Redis + +For those in a hurry: + + +* Download the jar through Maven: + + + org.springframework.data + spring-data-document + 1.0.0-BUILD-SNAPSHOT + + + + + spring-maven-snapshot + true + Springframework Maven SNAPSHOT Repository + http://maven.springframework.org/snapshot + + + +* The MongoTemplate is the central support class for Mongo database operations. It supports +** Basic POJO mapping support to and from BSON +** Connection Affinity callback +** Exception translation into Spring's + +Future plans are to support optional logging and/or exception throwing based on WriteResult return value, common map-reduce operations, GridFS operations. A simple API for partial document updates is also planned. + +* To simplify the creation of Data Repositories a generic Repository interface and base class is provided. Furthermore, Spring will automatically create a Repository implementation for you that add implementations of finder methods you specify on an interface. For example the Repository interface is + +public interface Repository { + + T save(T entity); + + List save(Iterable entities); + + T findById(ID id); + + boolean exists(ID id); + + List findAll(); + + Long count(); + + void delete(T entity); + + void delete(Iterable entities); + + void deleteAll(); +} + +and there is a placeholder interface called MongoRepository that will in future add more Mongo specific methods. + +public interface MongoRepository extends + Repository { + +} + +You can use the provided implementation class SimpleMongoRepository for basic data access. You can also extend the MongoRepository interface and supply your own finder methods that follow simple naming conventions so they can be converted into queries. For example, given a Person class with first and last name properties + +public interface PersonRepository extends MongoRepository { + + List findByLastname(String lastname); + + + List findByFirstnameLike(String firstname); +} + +You can have Spring automatically generate the implemention as shown below + + + + + + + + + + + + + + + + + +This will register an object in the container named PersonRepository. You can use it as shown below + +@Service +public class MyService { + + @Autowired + PersonRepository repository; + + + @Test + public void doWork() { + + repository.deleteAll(); + + Person person = new Person(); + person.setFirstname("Oliver"); + person.setLastname("Gierke"); + person = repository.save(person); + + List lastNameResults = repository.findByLastname("Gierke"); + + List firstNameResults = repository.findByFirstnameLike("Oli*"); + + } +} + + +## CouchDB + +TBD + + +Contributing to Spring Data +--------------------------- + +Here are some ways for you to get involved in the community: + +* Get involved with the Spring community on the Spring Community Forums. Please help out on the [forum](http://forum.springsource.org/forumdisplay.php?f=80) by responding to questions and joining the debate. +* Create [JIRA](https://jira.springframework.org/browse/DATADOC) tickets for bugs and new features and comment and vote on the ones that you are interested in. +* Github is for social coding: if you want to write code, we encourage contributions through pull requests from [forks of this repository](http://help.github.com/forking/). If you want to contribute code this way, please reference a JIRA ticket as well covering the specific issue you are addressing. +* Watch for upcoming articles on Spring by [subscribing](http://www.springsource.org/node/feed) to springframework.org + +Before we accept a non-trivial patch or pull request we will need you to sign the [contributor's agreement](https://support.springsource.com/spring_committer_signup). Signing the contributor's agreement does not grant anyone commit rights to the main repository, but it does mean that we can accept your contributions, and you will get an author credit if we do. Active contributors might be asked to join the core team, and given the ability to merge pull requests. From 6858f8c609d75c04635c8e7ae81b933149c8103d Mon Sep 17 00:00:00 2001 From: Mark Pollack Date: Fri, 3 Dec 2010 10:53:38 -0500 Subject: [PATCH 03/12] README updates. --- README.md | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 0696d6d6e..262c86e4f 100644 --- a/README.md +++ b/README.md @@ -45,26 +45,26 @@ Future plans are to support optional logging and/or exception throwing based on * To simplify the creation of Data Repositories a generic Repository interface and base class is provided. Furthermore, Spring will automatically create a Repository implementation for you that add implementations of finder methods you specify on an interface. For example the Repository interface is -public interface Repository { + public interface Repository { - T save(T entity); + T save(T entity); - List save(Iterable entities); + List save(Iterable entities); - T findById(ID id); + T findById(ID id); - boolean exists(ID id); + boolean exists(ID id); - List findAll(); + List findAll(); - Long count(); + Long count(); - void delete(T entity); + void delete(T entity); - void delete(Iterable entities); + void delete(Iterable entities); - void deleteAll(); -} + void deleteAll(); + } and there is a placeholder interface called MongoRepository that will in future add more Mongo specific methods. From d592028890f467f658b4cc52c8c95af49c065d01 Mon Sep 17 00:00:00 2001 From: Mark Pollack Date: Fri, 3 Dec 2010 10:58:41 -0500 Subject: [PATCH 04/12] README updates. --- README.md | 71 ++++++++++++++++++++++++++----------------------------- 1 file changed, 34 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index 262c86e4f..5fe5d486b 100644 --- a/README.md +++ b/README.md @@ -68,64 +68,61 @@ Future plans are to support optional logging and/or exception throwing based on and there is a placeholder interface called MongoRepository that will in future add more Mongo specific methods. -public interface MongoRepository extends + public interface MongoRepository extends Repository { - -} + } You can use the provided implementation class SimpleMongoRepository for basic data access. You can also extend the MongoRepository interface and supply your own finder methods that follow simple naming conventions so they can be converted into queries. For example, given a Person class with first and last name properties -public interface PersonRepository extends MongoRepository { + public interface PersonRepository extends MongoRepository { - List findByLastname(String lastname); + List findByLastname(String lastname); - - List findByFirstnameLike(String firstname); -} + List findByFirstnameLike(String firstname); + } You can have Spring automatically generate the implemention as shown below - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + This will register an object in the container named PersonRepository. You can use it as shown below -@Service -public class MyService { + @Service + public class MyService { - @Autowired - PersonRepository repository; + @Autowired + PersonRepository repository; - @Test - public void doWork() { + public void doWork() { - repository.deleteAll(); + repository.deleteAll(); - Person person = new Person(); - person.setFirstname("Oliver"); - person.setLastname("Gierke"); - person = repository.save(person); + Person person = new Person(); + person.setFirstname("Oliver"); + person.setLastname("Gierke"); + person = repository.save(person); - List lastNameResults = repository.findByLastname("Gierke"); + List lastNameResults = repository.findByLastname("Gierke"); - List firstNameResults = repository.findByFirstnameLike("Oli*"); + List firstNameResults = repository.findByFirstnameLike("Oli*"); + } } -} ## CouchDB From 60f7dfb5c75a2ae157d703762ac1167a3a3a4b60 Mon Sep 17 00:00:00 2001 From: Mark Pollack Date: Fri, 3 Dec 2010 10:59:39 -0500 Subject: [PATCH 05/12] README updates. --- README.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 5fe5d486b..7f6dc4d6d 100644 --- a/README.md +++ b/README.md @@ -49,21 +49,21 @@ Future plans are to support optional logging and/or exception throwing based on T save(T entity); - List save(Iterable entities); + List save(Iterable entities); - T findById(ID id); + T findById(ID id); - boolean exists(ID id); + boolean exists(ID id); - List findAll(); + List findAll(); - Long count(); + Long count(); - void delete(T entity); + void delete(T entity); - void delete(Iterable entities); + void delete(Iterable entities); - void deleteAll(); + void deleteAll(); } and there is a placeholder interface called MongoRepository that will in future add more Mongo specific methods. From 8732b957de60a0b91e8cf9657f8c97a3a68fc4d1 Mon Sep 17 00:00:00 2001 From: Mark Pollack Date: Fri, 3 Dec 2010 11:01:12 -0500 Subject: [PATCH 06/12] README updates. --- README.md | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 7f6dc4d6d..08bd975bf 100644 --- a/README.md +++ b/README.md @@ -7,14 +7,16 @@ As the name implies, the **Document** modules provides integration with document Getting Help ------------ -Read the main project [website](http://www.springsource.org/spring-data)) and the [User Guide](http://static.springsource.org/spring-data/datastore-keyvalue/snapshot-site/reference/html/) (A work in progress). - At this point your best bet is to look at the Look at the [JavaDocs](http://static.springsource.org/spring-data/data-document/docs/1.0.0.BUILD-SNAPSHOT/spring-data-mongodb/apidocs/) for MongoDB integration and corresponding and source code. For more detailed questions, use the [forum](http://forum.springsource.org/forumdisplay.php?f=80). If you are new to Spring as well as to Spring Data, look for information about [Spring projects](http://www.springsource.org/projects). +The [User Guide](http://static.springsource.org/spring-data/datastore-keyvalue/snapshot-site/reference/html/) (A work in progress). + + + Quick Start ----------- -## Redis +## MongoDB For those in a hurry: @@ -45,7 +47,7 @@ Future plans are to support optional logging and/or exception throwing based on * To simplify the creation of Data Repositories a generic Repository interface and base class is provided. Furthermore, Spring will automatically create a Repository implementation for you that add implementations of finder methods you specify on an interface. For example the Repository interface is - public interface Repository { + public interface Repository { T save(T entity); @@ -64,7 +66,7 @@ Future plans are to support optional logging and/or exception throwing based on void delete(Iterable entities); void deleteAll(); - } + } and there is a placeholder interface called MongoRepository that will in future add more Mongo specific methods. From 3bafb886622d73807f030d04d9038823e1342f96 Mon Sep 17 00:00:00 2001 From: Mark Pollack Date: Fri, 3 Dec 2010 11:03:06 -0500 Subject: [PATCH 07/12] README updates. --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 08bd975bf..8bf49ad24 100644 --- a/README.md +++ b/README.md @@ -39,15 +39,15 @@ For those in a hurry: * The MongoTemplate is the central support class for Mongo database operations. It supports -** Basic POJO mapping support to and from BSON -** Connection Affinity callback -** Exception translation into Spring's + * Basic POJO mapping support to and from BSON + * Connection Affinity callback + * Exception translation into Spring's Future plans are to support optional logging and/or exception throwing based on WriteResult return value, common map-reduce operations, GridFS operations. A simple API for partial document updates is also planned. * To simplify the creation of Data Repositories a generic Repository interface and base class is provided. Furthermore, Spring will automatically create a Repository implementation for you that add implementations of finder methods you specify on an interface. For example the Repository interface is - public interface Repository { + public interface Repository { T save(T entity); @@ -66,7 +66,7 @@ Future plans are to support optional logging and/or exception throwing based on void delete(Iterable entities); void deleteAll(); - } + } and there is a placeholder interface called MongoRepository that will in future add more Mongo specific methods. From 1429a12fe4cec46baa2d31f1b20394e28e8860f4 Mon Sep 17 00:00:00 2001 From: Mark Pollack Date: Fri, 3 Dec 2010 11:07:04 -0500 Subject: [PATCH 08/12] README updates. --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 8bf49ad24..befba0317 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,7 @@ Future plans are to support optional logging and/or exception throwing based on * To simplify the creation of Data Repositories a generic Repository interface and base class is provided. Furthermore, Spring will automatically create a Repository implementation for you that add implementations of finder methods you specify on an interface. For example the Repository interface is + public interface Repository { T save(T entity); From 2b0c85b235d7ee8d4915e419961a2b4850d6839e Mon Sep 17 00:00:00 2001 From: Mark Pollack Date: Fri, 3 Dec 2010 11:07:40 -0500 Subject: [PATCH 09/12] README updates. --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index befba0317..9a1420175 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ Future plans are to support optional logging and/or exception throwing based on * To simplify the creation of Data Repositories a generic Repository interface and base class is provided. Furthermore, Spring will automatically create a Repository implementation for you that add implementations of finder methods you specify on an interface. For example the Repository interface is - public interface Repository { +'public interface Repository {' T save(T entity); @@ -67,7 +67,7 @@ Future plans are to support optional logging and/or exception throwing based on void delete(Iterable entities); void deleteAll(); - } +'}' and there is a placeholder interface called MongoRepository that will in future add more Mongo specific methods. From 01775d288eac8f4bd66be63417c59ae2af8ee9a6 Mon Sep 17 00:00:00 2001 From: Mark Pollack Date: Fri, 3 Dec 2010 11:08:30 -0500 Subject: [PATCH 10/12] README updates. --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9a1420175..b5e926f82 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ Future plans are to support optional logging and/or exception throwing based on * To simplify the creation of Data Repositories a generic Repository interface and base class is provided. Furthermore, Spring will automatically create a Repository implementation for you that add implementations of finder methods you specify on an interface. For example the Repository interface is -'public interface Repository {' +`public interface Repository {` T save(T entity); @@ -67,7 +67,7 @@ Future plans are to support optional logging and/or exception throwing based on void delete(Iterable entities); void deleteAll(); -'}' +`}` and there is a placeholder interface called MongoRepository that will in future add more Mongo specific methods. From 3cb857ef0561f7c5808ca75f166d65fbe82f412a Mon Sep 17 00:00:00 2001 From: Mark Pollack Date: Fri, 3 Dec 2010 11:57:07 -0500 Subject: [PATCH 11/12] README updates. --- README.md | 49 +++++++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index b5e926f82..acd07de4e 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Getting Help At this point your best bet is to look at the Look at the [JavaDocs](http://static.springsource.org/spring-data/data-document/docs/1.0.0.BUILD-SNAPSHOT/spring-data-mongodb/apidocs/) for MongoDB integration and corresponding and source code. For more detailed questions, use the [forum](http://forum.springsource.org/forumdisplay.php?f=80). If you are new to Spring as well as to Spring Data, look for information about [Spring projects](http://www.springsource.org/projects). -The [User Guide](http://static.springsource.org/spring-data/datastore-keyvalue/snapshot-site/reference/html/) (A work in progress). +The [User Guide](http://static.springsource.org/spring-data/data-document/docs/1.0.0.BUILD-SNAPSHOT/reference/html/) (A work in progress). @@ -37,45 +37,54 @@ For those in a hurry: http://maven.springframework.org/snapshot +### MongoTemplate +MongoTemplate is the central support class for Mongo database operations. It provides -* The MongoTemplate is the central support class for Mongo database operations. It supports - * Basic POJO mapping support to and from BSON - * Connection Affinity callback - * Exception translation into Spring's +* Basic POJO mapping support to and from BSON +* Connection Affinity callback +* Exception translation into Spring's [technology agnostic DAO exception hierarchy](http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/dao.html#dao-exceptions). Future plans are to support optional logging and/or exception throwing based on WriteResult return value, common map-reduce operations, GridFS operations. A simple API for partial document updates is also planned. -* To simplify the creation of Data Repositories a generic Repository interface and base class is provided. Furthermore, Spring will automatically create a Repository implementation for you that add implementations of finder methods you specify on an interface. For example the Repository interface is +### Easy Data Repository generation - -`public interface Repository {` +To simplify the creation of Data Repositories a generic Repository interface and default implementation is provided. Furthermore, Spring will automatically create a Repository implementation for you that adds implementations of finder methods you specify on an interface. - T save(T entity); +The Repository interface is - List save(Iterable entities); + public interface Repository { - T findById(ID id); + T save(T entity); - boolean exists(ID id); + List save(Iterable entities); - List findAll(); + T findById(ID id); - Long count(); + boolean exists(ID id); - void delete(T entity); + List findAll(); - void delete(Iterable entities); + Long count(); - void deleteAll(); -`}` + void delete(T entity); -and there is a placeholder interface called MongoRepository that will in future add more Mongo specific methods. + void delete(Iterable entities); + + void deleteAll(); + } + + +The MongoRepository extends Repository and will in future add more Mongo specific methods. public interface MongoRepository extends Repository { } -You can use the provided implementation class SimpleMongoRepository for basic data access. You can also extend the MongoRepository interface and supply your own finder methods that follow simple naming conventions so they can be converted into queries. For example, given a Person class with first and last name properties +SimpleMongoRepository is the out of the box implementation of the MongoRepository you can use for basid CRUD operations. + +To go beyond basic CRUD, extend the MongoRepository interface and supply your own finder methods that follow simple naming conventions such that they can be easily converted into queries. + +For example, given a Person class with first and last name properties, a PersonRepository interface that can query for Person by last name and when the first name matches a regular expression is shown below public interface PersonRepository extends MongoRepository { From 9500bcbe29de7824088c3b12c6b52fe398bb96ae Mon Sep 17 00:00:00 2001 From: Thomas Risberg Date: Fri, 3 Dec 2010 14:49:27 -0500 Subject: [PATCH 12/12] added a simple UpdateBuilder implementation --- .../data/document/mongodb/UpdateBuilder.java | 46 ++++++++++++++++++ .../document/mongodb/UpdateBuilderTests.java | 47 +++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/UpdateBuilder.java create mode 100644 spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/UpdateBuilderTests.java diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/UpdateBuilder.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/UpdateBuilder.java new file mode 100644 index 000000000..f96d7691d --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/UpdateBuilder.java @@ -0,0 +1,46 @@ +/* + * Copyright 2010 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; + +import java.util.Collections; +import java.util.LinkedHashMap; + +import com.mongodb.BasicDBObject; +import com.mongodb.DBObject; + +public class UpdateBuilder { + + private LinkedHashMap criteria = new LinkedHashMap(); + + public UpdateBuilder set(String key, Object value) { + criteria.put("$set", Collections.singletonMap(key, value)); + return this; + } + + public UpdateBuilder inc(String key, long inc) { + criteria.put("$inc", Collections.singletonMap(key, inc)); + return this; + } + + public DBObject build() { + DBObject dbo = new BasicDBObject(); + for (String k : criteria.keySet()) { + dbo.put(k, criteria.get(k)); + } + return dbo; + } + +} diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/UpdateBuilderTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/UpdateBuilderTests.java new file mode 100644 index 000000000..630227380 --- /dev/null +++ b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/UpdateBuilderTests.java @@ -0,0 +1,47 @@ +/* + * Copyright 2010 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; + +import org.junit.Assert; +import org.junit.Test; + +public class UpdateBuilderTests { + + @Test + public void testSetUpdate() { + UpdateBuilder ub = new UpdateBuilder() + .set("directory", "/Users/Test/Desktop"); + Assert.assertEquals("{ \"$set\" : { \"directory\" : \"/Users/Test/Desktop\"}}", ub.build().toString()); + } + + @Test + public void testIncUpdate() { + UpdateBuilder ub = new UpdateBuilder() + .inc("size", 1); + Assert.assertEquals("{ \"$inc\" : { \"size\" : 1}}", ub.build().toString()); + } + + @Test + public void testIncAndSetUpdate() { + UpdateBuilder ub = new UpdateBuilder() + .inc("size", 1) + .set("directory", "/Users/Test/Desktop"); + Assert.assertEquals("{ \"$inc\" : { \"size\" : 1} , \"$set\" : { \"directory\" : \"/Users/Test/Desktop\"}}", + ub.build().toString()); + } + + +}