Browse Source

renaming, refactoring, adding MongoBeanPropertyDocumentSource

pull/1/head
Thomas Risberg 16 years ago
parent
commit
7f05a7e212
  1. 2
      src/main/java/org/springframework/datastore/document/couchdb/CouchDocumentSource.java
  2. 8
      src/main/java/org/springframework/datastore/document/couchdb/CouchTemplate.java
  3. 8
      src/main/java/org/springframework/datastore/document/mongodb/MongoBeanPropertyDocumentMapper.java
  4. 123
      src/main/java/org/springframework/datastore/document/mongodb/MongoBeanPropertyDocumentSource.java
  5. 8
      src/main/java/org/springframework/datastore/document/mongodb/MongoDbConnectionFactory.java
  6. 2
      src/main/java/org/springframework/datastore/document/mongodb/MongoDbFactoryBean.java
  7. 2
      src/main/java/org/springframework/datastore/document/mongodb/MongoDocumentSource.java
  8. 14
      src/main/java/org/springframework/datastore/document/mongodb/MongoTemplate.java

2
src/main/java/org/springframework/datastore/document/couchdb/CouchDbDocumentSource.java → src/main/java/org/springframework/datastore/document/couchdb/CouchDocumentSource.java

@ -3,6 +3,6 @@ package org.springframework.datastore.document.couchdb;
import org.jcouchdb.document.BaseDocument; import org.jcouchdb.document.BaseDocument;
import org.springframework.datastore.document.DocumentSource; import org.springframework.datastore.document.DocumentSource;
public interface CouchDbDocumentSource extends DocumentSource<BaseDocument> { public interface CouchDocumentSource extends DocumentSource<BaseDocument> {
} }

8
src/main/java/org/springframework/datastore/document/couchdb/CouchDbTemplate.java → src/main/java/org/springframework/datastore/document/couchdb/CouchTemplate.java

@ -23,20 +23,20 @@ import org.springframework.datastore.document.AbstractDocumentStoreTemplate;
import org.springframework.datastore.document.DocumentSource; import org.springframework.datastore.document.DocumentSource;
import org.springframework.datastore.document.DocumentStoreConnectionFactory; import org.springframework.datastore.document.DocumentStoreConnectionFactory;
public class CouchDbTemplate extends AbstractDocumentStoreTemplate<Database> { public class CouchTemplate extends AbstractDocumentStoreTemplate<Database> {
private DocumentStoreConnectionFactory<Database> connectionFactory; private DocumentStoreConnectionFactory<Database> connectionFactory;
public CouchDbTemplate() { public CouchTemplate() {
super(); super();
} }
public CouchDbTemplate(String host, String databaseName) { public CouchTemplate(String host, String databaseName) {
super(); super();
connectionFactory = new CouchDbConnectionFactory(host, databaseName); connectionFactory = new CouchDbConnectionFactory(host, databaseName);
} }
public CouchDbTemplate(CouchDbConnectionFactory mcf) { public CouchTemplate(CouchDbConnectionFactory mcf) {
super(); super();
connectionFactory = mcf; connectionFactory = mcf;
} }

8
src/main/java/org/springframework/datastore/document/mongodb/MongoBeanPropertyDocumentMapper.java

@ -24,6 +24,7 @@ import java.util.Set;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.bson.types.ObjectId;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper; import org.springframework.beans.BeanWrapper;
import org.springframework.beans.NotWritablePropertyException; import org.springframework.beans.NotWritablePropertyException;
@ -75,7 +76,12 @@ public class MongoBeanPropertyDocumentMapper<T> implements DocumentMapper<DBObje
try { try {
Object value = document.get(key); Object value = document.get(key);
try { 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) { catch (TypeMismatchException e) {
logger.warn("Intercepted TypeMismatchException for " + key + "' with value " + value + logger.warn("Intercepted TypeMismatchException for " + key + "' with value " + value +

123
src/main/java/org/springframework/datastore/document/mongodb/MongoBeanPropertyDocumentSource.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.
*/
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) {
}
}

8
src/main/java/org/springframework/datastore/document/mongodb/MongoConnectionFactory.java → src/main/java/org/springframework/datastore/document/mongodb/MongoDbConnectionFactory.java

@ -48,7 +48,7 @@ import com.mongodb.Mongo;
* @author Thomas Risberg * @author Thomas Risberg
* @since 1.0 * @since 1.0
*/ */
public class MongoConnectionFactory implements DocumentStoreConnectionFactory<DB>, InitializingBean { public class MongoDbConnectionFactory implements DocumentStoreConnectionFactory<DB>, InitializingBean {
/** /**
* Logger, available to subclasses. * Logger, available to subclasses.
@ -58,16 +58,16 @@ public class MongoConnectionFactory implements DocumentStoreConnectionFactory<DB
private Mongo mongo; private Mongo mongo;
private String databaseName; private String databaseName;
public MongoConnectionFactory() { public MongoDbConnectionFactory() {
super(); super();
} }
public MongoConnectionFactory(String databaseName) { public MongoDbConnectionFactory(String databaseName) {
super(); super();
this.databaseName = databaseName; this.databaseName = databaseName;
} }
public MongoConnectionFactory(Mongo mongo, String databaseName) { public MongoDbConnectionFactory(Mongo mongo, String databaseName) {
super(); super();
this.mongo = mongo; this.mongo = mongo;
this.databaseName = databaseName; this.databaseName = databaseName;

2
src/main/java/org/springframework/datastore/document/mongodb/MongoDbFactoryBean.java

@ -38,7 +38,7 @@ public class MongoDbFactoryBean implements FactoryBean<DB>, InitializingBean {
*/ */
protected final Log logger = LogFactory.getLog(getClass()); protected final Log logger = LogFactory.getLog(getClass());
private MongoConnectionFactory mcf = new MongoConnectionFactory(); private MongoDbConnectionFactory mcf = new MongoDbConnectionFactory();
public void setMongo(Mongo mongo) { public void setMongo(Mongo mongo) {
this.mcf.setMongo(mongo); this.mcf.setMongo(mongo);

2
src/main/java/org/springframework/datastore/document/mongodb/MongoDbDocumentSource.java → src/main/java/org/springframework/datastore/document/mongodb/MongoDocumentSource.java

@ -4,6 +4,6 @@ import org.springframework.datastore.document.DocumentSource;
import com.mongodb.DBObject; import com.mongodb.DBObject;
public interface MongoDbDocumentSource extends DocumentSource<DBObject> { public interface MongoDocumentSource extends DocumentSource<DBObject> {
} }

14
src/main/java/org/springframework/datastore/document/mongodb/MongoTemplate.java

@ -45,10 +45,10 @@ public class MongoTemplate extends AbstractDocumentStoreTemplate<DB> {
public MongoTemplate(Mongo mongo, String databaseName) { public MongoTemplate(Mongo mongo, String databaseName) {
super(); super();
connectionFactory = new MongoConnectionFactory(mongo, databaseName); connectionFactory = new MongoDbConnectionFactory(mongo, databaseName);
} }
public MongoTemplate(MongoConnectionFactory mcf) { public MongoTemplate(MongoDbConnectionFactory mcf) {
super(); super();
connectionFactory = mcf; connectionFactory = mcf;
} }
@ -82,6 +82,11 @@ public class MongoTemplate extends AbstractDocumentStoreTemplate<DB> {
.drop(); .drop();
} }
public void saveObject(String collectionName, Object source) {
MongoBeanPropertyDocumentSource docSrc = new MongoBeanPropertyDocumentSource(source);
save(collectionName, docSrc);
}
public void save(String collectionName, DocumentSource<DBObject> documentSource) { public void save(String collectionName, DocumentSource<DBObject> documentSource) {
DBObject dbDoc = documentSource.getDocument(); DBObject dbDoc = documentSource.getDocument();
WriteResult wr = null; 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) { public <T> List<T> queryForCollection(String collectionName, DocumentMapper<DBObject, T> mapper) {
List<T> results = new ArrayList<T>(); List<T> results = new ArrayList<T>();
DBCollection collection = getDocumentStoreConnectionFactory() DBCollection collection = getDocumentStoreConnectionFactory()

Loading…
Cancel
Save