renaming, refactoring, adding MongoBeanPropertyDocumentSource
This commit is contained in:
@@ -3,6 +3,6 @@ package org.springframework.datastore.document.couchdb;
|
||||
import org.jcouchdb.document.BaseDocument;
|
||||
import org.springframework.datastore.document.DocumentSource;
|
||||
|
||||
public interface CouchDbDocumentSource extends DocumentSource<BaseDocument> {
|
||||
public interface CouchDocumentSource extends DocumentSource<BaseDocument> {
|
||||
|
||||
}
|
||||
@@ -23,20 +23,20 @@ import org.springframework.datastore.document.AbstractDocumentStoreTemplate;
|
||||
import org.springframework.datastore.document.DocumentSource;
|
||||
import org.springframework.datastore.document.DocumentStoreConnectionFactory;
|
||||
|
||||
public class CouchDbTemplate extends AbstractDocumentStoreTemplate<Database> {
|
||||
public class CouchTemplate extends AbstractDocumentStoreTemplate<Database> {
|
||||
|
||||
private DocumentStoreConnectionFactory<Database> connectionFactory;
|
||||
|
||||
public CouchDbTemplate() {
|
||||
public CouchTemplate() {
|
||||
super();
|
||||
}
|
||||
|
||||
public CouchDbTemplate(String host, String databaseName) {
|
||||
public CouchTemplate(String host, String databaseName) {
|
||||
super();
|
||||
connectionFactory = new CouchDbConnectionFactory(host, databaseName);
|
||||
}
|
||||
|
||||
public CouchDbTemplate(CouchDbConnectionFactory mcf) {
|
||||
public CouchTemplate(CouchDbConnectionFactory mcf) {
|
||||
super();
|
||||
connectionFactory = mcf;
|
||||
}
|
||||
@@ -24,6 +24,7 @@ import java.util.Set;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.bson.types.ObjectId;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.BeanWrapper;
|
||||
import org.springframework.beans.NotWritablePropertyException;
|
||||
@@ -75,7 +76,12 @@ public class MongoBeanPropertyDocumentMapper<T> implements DocumentMapper<DBObje
|
||||
try {
|
||||
Object value = document.get(key);
|
||||
try {
|
||||
bw.setPropertyValue(pd.getName(), value);
|
||||
if (value instanceof ObjectId) {
|
||||
bw.setPropertyValue(pd.getName(), ((ObjectId)value).toString());
|
||||
}
|
||||
else {
|
||||
bw.setPropertyValue(pd.getName(), value);
|
||||
}
|
||||
}
|
||||
catch (TypeMismatchException e) {
|
||||
logger.warn("Intercepted TypeMismatchException for " + key + "' with value " + value +
|
||||
|
||||
@@ -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.
|
||||
*/
|
||||
|
||||
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.NotReadablePropertyException;
|
||||
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.datastore.document.DocumentSource;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import com.mongodb.BasicDBObject;
|
||||
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 MongoBeanPropertyDocumentSource implements DocumentSource<DBObject> {
|
||||
|
||||
/** Logger available to subclasses */
|
||||
protected final Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
/** The class we are mapping to */
|
||||
private Object source;
|
||||
|
||||
/** The class we are mapping to */
|
||||
private Class<?> mappedClass;
|
||||
|
||||
/** Map of the fields we provide mapping for */
|
||||
private Map<String, PropertyDescriptor> mappedFields;
|
||||
|
||||
/** Set of bean properties we provide mapping for */
|
||||
private Set<String> mappedProperties;
|
||||
|
||||
|
||||
public MongoBeanPropertyDocumentSource(Object source) {
|
||||
initialize(source);
|
||||
}
|
||||
|
||||
|
||||
public DBObject getDocument() {
|
||||
BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this.source);
|
||||
DBObject dbo = new BasicDBObject();
|
||||
for (String key : this.mappedFields.keySet()) {
|
||||
String keyToUse = ("id".equals(key) ? "_id" : key);
|
||||
PropertyDescriptor pd = this.mappedFields.get(key);
|
||||
if (pd != null) {
|
||||
try {
|
||||
Object value = bw.getPropertyValue(key);
|
||||
if (value instanceof Enum) {
|
||||
dbo.put(keyToUse, ((Enum)value).name());
|
||||
}
|
||||
else {
|
||||
dbo.put(keyToUse, value);
|
||||
}
|
||||
}
|
||||
catch (NotReadablePropertyException ex) {
|
||||
throw new DataRetrievalFailureException(
|
||||
"Unable to map property " + pd.getName() + " to key " + key, ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
return dbo;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Initialize the mapping metadata for the given class.
|
||||
* @param mappedClass the mapped class.
|
||||
*/
|
||||
protected void initialize(Object source) {
|
||||
this.source = source;
|
||||
this.mappedClass = source.getClass();
|
||||
this.mappedFields = new HashMap<String, PropertyDescriptor>();
|
||||
this.mappedProperties = new HashSet<String>();
|
||||
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.
|
||||
* <p>The default implementation is empty. Can be overridden in subclasses.
|
||||
* @param bw the BeanWrapper to initialize
|
||||
*/
|
||||
protected void initBeanWrapper(BeanWrapper bw) {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -48,7 +48,7 @@ import com.mongodb.Mongo;
|
||||
* @author Thomas Risberg
|
||||
* @since 1.0
|
||||
*/
|
||||
public class MongoConnectionFactory implements DocumentStoreConnectionFactory<DB>, InitializingBean {
|
||||
public class MongoDbConnectionFactory implements DocumentStoreConnectionFactory<DB>, InitializingBean {
|
||||
|
||||
/**
|
||||
* Logger, available to subclasses.
|
||||
@@ -58,16 +58,16 @@ public class MongoConnectionFactory implements DocumentStoreConnectionFactory<DB
|
||||
private Mongo mongo;
|
||||
private String databaseName;
|
||||
|
||||
public MongoConnectionFactory() {
|
||||
public MongoDbConnectionFactory() {
|
||||
super();
|
||||
}
|
||||
|
||||
public MongoConnectionFactory(String databaseName) {
|
||||
public MongoDbConnectionFactory(String databaseName) {
|
||||
super();
|
||||
this.databaseName = databaseName;
|
||||
}
|
||||
|
||||
public MongoConnectionFactory(Mongo mongo, String databaseName) {
|
||||
public MongoDbConnectionFactory(Mongo mongo, String databaseName) {
|
||||
super();
|
||||
this.mongo = mongo;
|
||||
this.databaseName = databaseName;
|
||||
@@ -38,7 +38,7 @@ public class MongoDbFactoryBean implements FactoryBean<DB>, InitializingBean {
|
||||
*/
|
||||
protected final Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
private MongoConnectionFactory mcf = new MongoConnectionFactory();
|
||||
private MongoDbConnectionFactory mcf = new MongoDbConnectionFactory();
|
||||
|
||||
public void setMongo(Mongo mongo) {
|
||||
this.mcf.setMongo(mongo);
|
||||
|
||||
@@ -4,6 +4,6 @@ import org.springframework.datastore.document.DocumentSource;
|
||||
|
||||
import com.mongodb.DBObject;
|
||||
|
||||
public interface MongoDbDocumentSource extends DocumentSource<DBObject> {
|
||||
public interface MongoDocumentSource extends DocumentSource<DBObject> {
|
||||
|
||||
}
|
||||
@@ -45,10 +45,10 @@ public class MongoTemplate extends AbstractDocumentStoreTemplate<DB> {
|
||||
|
||||
public MongoTemplate(Mongo mongo, String databaseName) {
|
||||
super();
|
||||
connectionFactory = new MongoConnectionFactory(mongo, databaseName);
|
||||
connectionFactory = new MongoDbConnectionFactory(mongo, databaseName);
|
||||
}
|
||||
|
||||
public MongoTemplate(MongoConnectionFactory mcf) {
|
||||
public MongoTemplate(MongoDbConnectionFactory mcf) {
|
||||
super();
|
||||
connectionFactory = mcf;
|
||||
}
|
||||
@@ -82,6 +82,11 @@ public class MongoTemplate extends AbstractDocumentStoreTemplate<DB> {
|
||||
.drop();
|
||||
}
|
||||
|
||||
public void saveObject(String collectionName, Object source) {
|
||||
MongoBeanPropertyDocumentSource docSrc = new MongoBeanPropertyDocumentSource(source);
|
||||
save(collectionName, docSrc);
|
||||
}
|
||||
|
||||
public void save(String collectionName, DocumentSource<DBObject> documentSource) {
|
||||
DBObject dbDoc = documentSource.getDocument();
|
||||
WriteResult wr = null;
|
||||
@@ -95,6 +100,11 @@ public class MongoTemplate extends AbstractDocumentStoreTemplate<DB> {
|
||||
}
|
||||
}
|
||||
|
||||
public <T> List<T> queryForCollection(String collectionName, Class<T> targetClass) {
|
||||
DocumentMapper<DBObject, T> mapper = MongoBeanPropertyDocumentMapper.newInstance(targetClass);
|
||||
return queryForCollection(collectionName, mapper);
|
||||
}
|
||||
|
||||
public <T> List<T> queryForCollection(String collectionName, DocumentMapper<DBObject, T> mapper) {
|
||||
List<T> results = new ArrayList<T>();
|
||||
DBCollection collection = getDocumentStoreConnectionFactory()
|
||||
|
||||
Reference in New Issue
Block a user