();
+ PropertyDescriptor[] pds = BeanUtils.getPropertyDescriptors(mappedClass);
+ for (PropertyDescriptor pd : pds) {
+ if (pd.getWriteMethod() != null) {
+ this.mappedFields.put(pd.getName(), pd);
+ this.mappedProperties.add(pd.getName());
+ }
+ }
+ }
+
+ /**
+ * Initialize the given BeanWrapper to be used for row mapping.
+ * To be called for each row.
+ * The default implementation is empty. Can be overridden in subclasses.
+ * @param bw the BeanWrapper to initialize
+ */
+ protected void initBeanWrapper(BeanWrapper bw) {
+ }
+
+ /**
+ * Static factory method to create a new MongoBeanPropertyDocumentMapper
+ * (with the mapped class specified only once).
+ * @param mappedClass the class that document should be mapped to
+ */
+ public static MongoBeanPropertyDocumentMapper newInstance(Class mappedClass) {
+ MongoBeanPropertyDocumentMapper newInstance = new MongoBeanPropertyDocumentMapper(mappedClass);
+ return newInstance;
+ }
+
+}
diff --git a/src/main/java/org/springframework/datastore/document/mongodb/MongoConnectionFactory.java b/src/main/java/org/springframework/datastore/document/mongodb/MongoConnectionFactory.java
index c6bda7a03..96a0fdc37 100644
--- a/src/main/java/org/springframework/datastore/document/mongodb/MongoConnectionFactory.java
+++ b/src/main/java/org/springframework/datastore/document/mongodb/MongoConnectionFactory.java
@@ -14,13 +14,29 @@
* limitations under the License.
*/
+/*
+ * 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.datastore.document.mongodb;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean;
-import org.springframework.datastore.core.DatastoreConnectionFactory;
+import org.springframework.datastore.document.DocumentStoreConnectionFactory;
import org.springframework.util.Assert;
import com.mongodb.DB;
@@ -32,7 +48,7 @@ import com.mongodb.Mongo;
* @author Thomas Risberg
* @since 1.0
*/
-public class MongoConnectionFactory implements DatastoreConnectionFactory, InitializingBean {
+public class MongoConnectionFactory implements DocumentStoreConnectionFactory, InitializingBean {
/**
* Logger, available to subclasses.
diff --git a/src/main/java/org/springframework/datastore/document/mongodb/MongoDatastoreTemplate.java b/src/main/java/org/springframework/datastore/document/mongodb/MongoDatastoreTemplate.java
deleted file mode 100644
index 4fc0406a5..000000000
--- a/src/main/java/org/springframework/datastore/document/mongodb/MongoDatastoreTemplate.java
+++ /dev/null
@@ -1,45 +0,0 @@
-package org.springframework.datastore.document.mongodb;
-
-
-import java.util.List;
-
-import org.springframework.data.core.DataMapper;
-import org.springframework.data.core.QueryDefinition;
-import org.springframework.datastore.core.AbstractDatastoreTemplate;
-
-import com.mongodb.DB;
-import com.mongodb.Mongo;
-
-public class MongoDatastoreTemplate extends AbstractDatastoreTemplate {
-
- private Mongo mongo;
-
-
- public MongoDatastoreTemplate() {
- super();
- }
-
- public MongoDatastoreTemplate(Mongo mongo, String databaseName) {
- super();
- this.mongo = mongo;
- setDatastoreConnectionFactory(new MongoConnectionFactory(mongo, databaseName));
- }
-
-
- public Mongo getMongo() {
- return mongo;
- }
-
- public void setMongo(Mongo mongo) {
- this.mongo = mongo;
- }
-
-
- @Override
- public List query(QueryDefinition arg0, DataMapper arg1) {
- return null;
- }
-
-
-
-}
diff --git a/src/main/java/org/springframework/datastore/document/mongodb/MongoDbDocumentSource.java b/src/main/java/org/springframework/datastore/document/mongodb/MongoDbDocumentSource.java
new file mode 100644
index 000000000..8aac07f3d
--- /dev/null
+++ b/src/main/java/org/springframework/datastore/document/mongodb/MongoDbDocumentSource.java
@@ -0,0 +1,9 @@
+package org.springframework.datastore.document.mongodb;
+
+import org.springframework.datastore.document.DocumentSource;
+
+import com.mongodb.DBObject;
+
+public interface MongoDbDocumentSource extends DocumentSource {
+
+}
diff --git a/src/main/java/org/springframework/datastore/document/mongodb/MongoTemplate.java b/src/main/java/org/springframework/datastore/document/mongodb/MongoTemplate.java
new file mode 100644
index 000000000..4c83d1940
--- /dev/null
+++ b/src/main/java/org/springframework/datastore/document/mongodb/MongoTemplate.java
@@ -0,0 +1,109 @@
+/*
+ * 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.datastore.document.mongodb;
+
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.springframework.dao.DataRetrievalFailureException;
+import org.springframework.dao.InvalidDataAccessApiUsageException;
+import org.springframework.datastore.document.AbstractDocumentStoreTemplate;
+import org.springframework.datastore.document.DocumentMapper;
+import org.springframework.datastore.document.DocumentSource;
+import org.springframework.datastore.document.DocumentStoreConnectionFactory;
+
+import com.mongodb.CommandResult;
+import com.mongodb.DB;
+import com.mongodb.DBCollection;
+import com.mongodb.DBObject;
+import com.mongodb.Mongo;
+import com.mongodb.MongoException;
+import com.mongodb.WriteResult;
+
+public class MongoTemplate extends AbstractDocumentStoreTemplate {
+
+ private DocumentStoreConnectionFactory connectionFactory;
+
+ public MongoTemplate() {
+ super();
+ }
+
+ public MongoTemplate(Mongo mongo, String databaseName) {
+ super();
+ connectionFactory = new MongoConnectionFactory(mongo, databaseName);
+ }
+
+ public MongoTemplate(MongoConnectionFactory mcf) {
+ super();
+ connectionFactory = mcf;
+ }
+
+ @Override
+ public DocumentStoreConnectionFactory getDocumentStoreConnectionFactory() {
+ return connectionFactory;
+ }
+
+
+ public void execute(DocumentSource command) {
+ CommandResult cr = getDocumentStoreConnectionFactory()
+ .getConnection()
+ .command(command.getDocument());
+ System.out.println("! " + cr.getErrorMessage());
+ }
+
+ public void createCollection(String collectionName, DocumentSource documentSource) {
+ DB db = getDocumentStoreConnectionFactory().getConnection();
+ try {
+ db.createCollection(collectionName, documentSource.getDocument());
+ } catch (MongoException e) {
+ throw new InvalidDataAccessApiUsageException("Error creating collection " + collectionName + ": " + e.getMessage(), e);
+ }
+ }
+
+ public void dropCollection(String collectionName) {
+ getDocumentStoreConnectionFactory()
+ .getConnection()
+ .getCollection(collectionName)
+ .drop();
+ }
+
+ public void save(String collectionName, DocumentSource documentSource) {
+ DBObject dbDoc = documentSource.getDocument();
+ WriteResult wr = null;
+ try {
+ wr = getDocumentStoreConnectionFactory()
+ .getConnection()
+ .getCollection(collectionName)
+ .save(dbDoc);
+ } catch (MongoException e) {
+ throw new DataRetrievalFailureException(wr.getLastError().getErrorMessage(), e);
+ }
+ }
+
+ public List queryForCollection(String collectionName, DocumentMapper mapper) {
+ List results = new ArrayList();
+ DBCollection collection = getDocumentStoreConnectionFactory()
+ .getConnection()
+ .getCollection(collectionName);
+ for (DBObject dbo : collection.find()) {
+ results.add(mapper.mapDocument(dbo));
+ }
+ return results;
+ }
+
+}