diff --git a/.classpath b/.classpath index 9d42e65e7..16f01e2ee 100644 --- a/.classpath +++ b/.classpath @@ -1,32 +1,7 @@ + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file + + + + + diff --git a/.project b/.project index 68bed8f8a..3764216a6 100644 --- a/.project +++ b/.project @@ -1,13 +1,23 @@ + - datastore-document - - - - - org.eclipse.jdt.core.javabuilder - - - - org.eclipse.jdt.core.javanature - - \ No newline at end of file + datastore-document + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.maven.ide.eclipse.maven2Builder + + + + + + org.maven.ide.eclipse.maven2Nature + org.eclipse.jdt.core.javanature + + diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs index cdaeaf82f..89e4fdb41 100644 --- a/.settings/org.eclipse.jdt.core.prefs +++ b/.settings/org.eclipse.jdt.core.prefs @@ -1,5 +1,6 @@ -#Wed Jul 14 14:36:01 EDT 2010 -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 +#Wed Aug 25 00:54:37 EDT 2010 eclipse.preferences.version=1 -org.eclipse.jdt.core.compiler.source=1.5 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 org.eclipse.jdt.core.compiler.compliance=1.5 +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.source=1.5 diff --git a/.settings/org.maven.ide.eclipse.prefs b/.settings/org.maven.ide.eclipse.prefs new file mode 100644 index 000000000..6bb697c7a --- /dev/null +++ b/.settings/org.maven.ide.eclipse.prefs @@ -0,0 +1,9 @@ +#Wed Aug 25 00:54:36 EDT 2010 +activeProfiles= +eclipse.preferences.version=1 +fullBuildGoals=process-test-resources +includeModules=false +resolveWorkspaceProjects=true +resourceFilterGoals=process-resources resources\:testResources +skipCompilerPlugin=true +version=1 diff --git a/pom.xml b/pom.xml index 3a7cb824b..06bd88aab 100644 --- a/pom.xml +++ b/pom.xml @@ -62,11 +62,16 @@ ${slf4j.version} - + org.mongodb mongo-java-driver - 1.4 + 2.1 + + + com.google.code.jcouchdb + jcouchdb + 0.11.0-1 @@ -114,11 +119,13 @@ ${spring.version} + diff --git a/src/main/java/org/springframework/datastore/document/AbstractDocumentStoreTemplate.java b/src/main/java/org/springframework/datastore/document/AbstractDocumentStoreTemplate.java new file mode 100644 index 000000000..010ada079 --- /dev/null +++ b/src/main/java/org/springframework/datastore/document/AbstractDocumentStoreTemplate.java @@ -0,0 +1,34 @@ +/* + * 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; + + +public abstract class AbstractDocumentStoreTemplate { + + + public abstract DocumentStoreConnectionFactory getDocumentStoreConnectionFactory(); + + public T execute(DocumentStoreConnectionCallback action) { + try { + return action.doInConnection(getDocumentStoreConnectionFactory().getConnection()); + } + catch (Exception e) { + throw new UncategorizedDocumentStoreException("Failure executing using datastore connection", e); + } + } + +} diff --git a/src/main/java/org/springframework/datastore/document/DocumentSource.java b/src/main/java/org/springframework/datastore/document/DocumentSource.java index abbcf0351..eaada09a8 100644 --- a/src/main/java/org/springframework/datastore/document/DocumentSource.java +++ b/src/main/java/org/springframework/datastore/document/DocumentSource.java @@ -22,8 +22,8 @@ package org.springframework.datastore.document; * @author Thomas Risberg * @since 1.0 */ -public interface DocumentSource { +public interface DocumentSource { - D getDocument(T source); + D getDocument(); } diff --git a/src/main/java/org/springframework/datastore/document/DocumentStoreConnectionCallback.java b/src/main/java/org/springframework/datastore/document/DocumentStoreConnectionCallback.java new file mode 100644 index 000000000..0c2faa23d --- /dev/null +++ b/src/main/java/org/springframework/datastore/document/DocumentStoreConnectionCallback.java @@ -0,0 +1,22 @@ +/* + * 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; + +public interface DocumentStoreConnectionCallback { + + T doInConnection(C con) throws Exception; +} diff --git a/src/main/java/org/springframework/datastore/document/DocumentStoreConnectionFactory.java b/src/main/java/org/springframework/datastore/document/DocumentStoreConnectionFactory.java new file mode 100644 index 000000000..fd90b5883 --- /dev/null +++ b/src/main/java/org/springframework/datastore/document/DocumentStoreConnectionFactory.java @@ -0,0 +1,21 @@ +/* + * 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; + +public interface DocumentStoreConnectionFactory { + C getConnection(); +} diff --git a/src/main/java/org/springframework/datastore/document/UncategorizedDocumentStoreException.java b/src/main/java/org/springframework/datastore/document/UncategorizedDocumentStoreException.java new file mode 100644 index 000000000..af80155a3 --- /dev/null +++ b/src/main/java/org/springframework/datastore/document/UncategorizedDocumentStoreException.java @@ -0,0 +1,27 @@ +/* + * 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; + +import org.springframework.dao.UncategorizedDataAccessException; + +public class UncategorizedDocumentStoreException extends UncategorizedDataAccessException { + + public UncategorizedDocumentStoreException(String msg, Throwable cause) { + super(msg, cause); + } + +} diff --git a/src/main/java/org/springframework/datastore/document/couchdb/CannotGetCouchDbConnectionException.java b/src/main/java/org/springframework/datastore/document/couchdb/CannotGetCouchDbConnectionException.java new file mode 100644 index 000000000..246a9abed --- /dev/null +++ b/src/main/java/org/springframework/datastore/document/couchdb/CannotGetCouchDbConnectionException.java @@ -0,0 +1,11 @@ +package org.springframework.datastore.document.couchdb; + +import org.springframework.dao.DataAccessResourceFailureException; + +public class CannotGetCouchDbConnectionException extends DataAccessResourceFailureException { + + public CannotGetCouchDbConnectionException(String msg, Throwable cause) { + super(msg, cause); + } + +} diff --git a/src/main/java/org/springframework/datastore/document/couchdb/CouchDbConnectionFactory.java b/src/main/java/org/springframework/datastore/document/couchdb/CouchDbConnectionFactory.java new file mode 100644 index 000000000..89ee47019 --- /dev/null +++ b/src/main/java/org/springframework/datastore/document/couchdb/CouchDbConnectionFactory.java @@ -0,0 +1,123 @@ +/* + * 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. + */ + +/* + * 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.couchdb; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.jcouchdb.db.Database; + +import org.springframework.beans.factory.InitializingBean; +import org.springframework.datastore.document.DocumentStoreConnectionFactory; + + +/** + * Convenient factory for configuring CouchDB. + * + * @author Thomas Risberg + * @since 1.0 + */ +public class CouchDbConnectionFactory implements DocumentStoreConnectionFactory, InitializingBean { + + /** + * Logger, available to subclasses. + */ + protected final Log logger = LogFactory.getLog(getClass()); + + private Database database; + private String host; + private String databaseName; + + public CouchDbConnectionFactory() { + super(); + } + + public CouchDbConnectionFactory(String host, String databaseName) { + super(); + this.host = host; + this.databaseName = databaseName; + } + + public CouchDbConnectionFactory(Database database) { + super(); + this.database = database; + } + + public void setDatabase(Database database) { + this.database = database; + } + + public void setDatabaseName(String databaseName) { + this.databaseName = databaseName; + } + + public void setHost(String host) { + this.host = host; + } + + public boolean isSingleton() { + return false; + } + + public void afterPropertiesSet() throws Exception { + // apply defaults - convenient when used to configure for tests + // in an application context + if (database == null) { + if (databaseName == null) { + logger.warn("Property databaseName not specified. Using default name 'test'"); + databaseName = "test"; + } + if (host == null) { + logger.warn("Property host not specified. Using default 'localhost'"); + database = new Database(host, databaseName); + } + database = new Database(host, databaseName); + } + else { + logger.info("Using provided database configuration"); + } + } + + public Database getConnection() { + synchronized (this){ + if (database == null) { + try { + afterPropertiesSet(); + } catch (Exception e) { + throw new CannotGetCouchDbConnectionException("Unable to connect to CouchDB", e); + } + } + } + return database; + } + +} diff --git a/src/main/java/org/springframework/datastore/document/couchdb/CouchDbDocumentSource.java b/src/main/java/org/springframework/datastore/document/couchdb/CouchDbDocumentSource.java new file mode 100644 index 000000000..89adfd2f8 --- /dev/null +++ b/src/main/java/org/springframework/datastore/document/couchdb/CouchDbDocumentSource.java @@ -0,0 +1,8 @@ +package org.springframework.datastore.document.couchdb; + +import org.jcouchdb.document.BaseDocument; +import org.springframework.datastore.document.DocumentSource; + +public interface CouchDbDocumentSource extends DocumentSource { + +} diff --git a/src/main/java/org/springframework/datastore/document/couchdb/CouchDbTemplate.java b/src/main/java/org/springframework/datastore/document/couchdb/CouchDbTemplate.java new file mode 100644 index 000000000..b80948c5a --- /dev/null +++ b/src/main/java/org/springframework/datastore/document/couchdb/CouchDbTemplate.java @@ -0,0 +1,55 @@ +/* + * 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.couchdb; + + +import org.jcouchdb.db.Database; +import org.jcouchdb.document.BaseDocument; +import org.springframework.datastore.document.AbstractDocumentStoreTemplate; +import org.springframework.datastore.document.DocumentSource; +import org.springframework.datastore.document.DocumentStoreConnectionFactory; + + public class CouchDbTemplate extends AbstractDocumentStoreTemplate { + + private DocumentStoreConnectionFactory connectionFactory; + + public CouchDbTemplate() { + super(); + } + + public CouchDbTemplate(String host, String databaseName) { + super(); + connectionFactory = new CouchDbConnectionFactory(host, databaseName); + } + + public CouchDbTemplate(CouchDbConnectionFactory mcf) { + super(); + connectionFactory = mcf; + } + + public void save(DocumentSource documentSource) { + BaseDocument d = documentSource.getDocument(); + getDocumentStoreConnectionFactory().getConnection().createDocument(d); + } + + @Override + public DocumentStoreConnectionFactory getDocumentStoreConnectionFactory() { + return connectionFactory; + } + + +} diff --git a/src/main/java/org/springframework/datastore/document/mongodb/CannotGetMongoDbConnectionException.java b/src/main/java/org/springframework/datastore/document/mongodb/CannotGetMongoDbConnectionException.java new file mode 100644 index 000000000..e6046e6db --- /dev/null +++ b/src/main/java/org/springframework/datastore/document/mongodb/CannotGetMongoDbConnectionException.java @@ -0,0 +1,11 @@ +package org.springframework.datastore.document.mongodb; + +import org.springframework.dao.DataAccessResourceFailureException; + +public class CannotGetMongoDbConnectionException extends DataAccessResourceFailureException { + + public CannotGetMongoDbConnectionException(String msg, Throwable cause) { + super(msg, cause); + } + +} diff --git a/src/main/java/org/springframework/datastore/document/mongodb/MongoBeanPropertyDocumentMapper.java b/src/main/java/org/springframework/datastore/document/mongodb/MongoBeanPropertyDocumentMapper.java new file mode 100644 index 000000000..31756cca9 --- /dev/null +++ b/src/main/java/org/springframework/datastore/document/mongodb/MongoBeanPropertyDocumentMapper.java @@ -0,0 +1,139 @@ +/* + * 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.beans.PropertyDescriptor; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.beans.BeanUtils; +import org.springframework.beans.BeanWrapper; +import org.springframework.beans.NotWritablePropertyException; +import org.springframework.beans.PropertyAccessorFactory; +import org.springframework.beans.TypeMismatchException; +import org.springframework.dao.DataRetrievalFailureException; +import org.springframework.datastore.document.DocumentMapper; +import org.springframework.util.Assert; + +import com.mongodb.DBObject; + +/** + * Class used to map properties of a Document to the corresponding properties of a business object. + * + * @author Thomas Risberg + * @since 1.0 + */ +public class MongoBeanPropertyDocumentMapper implements DocumentMapper { + + /** Logger available to subclasses */ + protected final Log logger = LogFactory.getLog(getClass()); + + /** The class we are mapping to */ + private Class mappedClass; + + /** Map of the fields we provide mapping for */ + private Map mappedFields; + + /** Set of bean properties we provide mapping for */ + private Set mappedProperties; + + + public MongoBeanPropertyDocumentMapper(Class mappedClass) { + initialize(mappedClass); + } + + + public T mapDocument(DBObject document) { + Assert.state(this.mappedClass != null, "Mapped class was not specified"); + T mappedObject = BeanUtils.instantiate(this.mappedClass); + BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(mappedObject); + initBeanWrapper(bw); + + Set keys = document.keySet(); + for (String key : keys) { + String keyToUse = ("_id".equals(key) ? "id" : key); + PropertyDescriptor pd = this.mappedFields.get(keyToUse); + if (pd != null) { + try { + Object value = document.get(key); + try { + bw.setPropertyValue(pd.getName(), value); + } + catch (TypeMismatchException e) { + logger.warn("Intercepted TypeMismatchException for " + key + "' with value " + value + + " when setting property '" + pd.getName() + "' of type " + pd.getPropertyType() + + " on object: " + mappedObject); + } + } + catch (NotWritablePropertyException ex) { + throw new DataRetrievalFailureException( + "Unable to map key " + key + " to property " + pd.getName(), ex); + } + } + } + + return mappedObject; + } + + /** + * Get the class that we are mapping to. + */ + public final Class getMappedClass() { + return this.mappedClass; + } + + /** + * Initialize the mapping metadata for the given class. + * @param mappedClass the mapped class. + */ + protected void initialize(Class mappedClass) { + this.mappedClass = mappedClass; + this.mappedFields = new HashMap(); + this.mappedProperties = new HashSet(); + 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; + } + +}