Browse Source

Merge remote branch 'origin/master'

pull/1/head
Mark Pollack 15 years ago
parent
commit
d2d2238a57
  1. 43
      spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoTemplate.java

43
spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoTemplate.java

@ -17,9 +17,14 @@
package org.springframework.data.document.mongodb; package org.springframework.data.document.mongodb;
import java.beans.PropertyDescriptor;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.bson.types.ObjectId;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.PropertyAccessorFactory;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
import org.springframework.dao.DataRetrievalFailureException; import org.springframework.dao.DataRetrievalFailureException;
import org.springframework.dao.InvalidDataAccessApiUsageException; import org.springframework.dao.InvalidDataAccessApiUsageException;
@ -208,27 +213,30 @@ public class MongoTemplate extends AbstractDocumentStoreTemplate<DB> implements
} }
public void save(String collectionName, Object objectToSave) { public void save(String collectionName, Object objectToSave) {
BasicDBObject dbDoc = new BasicDBObject(); save(collectionName, objectToSave, this.mongoConverter);
this.mongoConverter.write(objectToSave, dbDoc);
saveDBObject(collectionName, dbDoc);
} }
public <T> void save(String collectionName, T objectToSave, MongoWriter<T> writer) { public <T> void save(String collectionName, T objectToSave, MongoWriter<T> writer) {
BasicDBObject dbDoc = new BasicDBObject(); BasicDBObject dbDoc = new BasicDBObject();
this.mongoConverter.write(objectToSave, dbDoc); writer.write(objectToSave, dbDoc);
saveDBObject(collectionName, dbDoc); Object _id = saveDBObject(collectionName, dbDoc);
populateIdIfNecessary(objectToSave, _id);
} }
protected void saveDBObject(String collectionName, BasicDBObject dbDoc) { protected Object saveDBObject(String collectionName, BasicDBObject dbDoc) {
if (dbDoc.keySet().size() > 0 ) { if (dbDoc.keySet().size() > 0 ) {
WriteResult wr = null; WriteResult wr = null;
try { try {
wr = getConnection().getCollection(collectionName).save(dbDoc); wr = getConnection().getCollection(collectionName).save(dbDoc);
return dbDoc.get("_id");
} catch (MongoException e) { } catch (MongoException e) {
throw new DataRetrievalFailureException(wr.getLastError().getErrorMessage(), e); throw new DataRetrievalFailureException(wr.getLastError().getErrorMessage(), e);
} }
} }
else {
return null;
}
} }
public <T> List<T> queryForCollection(Class<T> targetClass) { public <T> List<T> queryForCollection(Class<T> targetClass) {
@ -342,7 +350,26 @@ public class MongoTemplate extends AbstractDocumentStoreTemplate<DB> implements
return dbo; return dbo;
} }
private void populateIdIfNecessary(Object savedObject, Object id) {
//TODO Needs proper conversion support and should be integrated with reader implementation somehow
BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(savedObject);
PropertyDescriptor idPd = BeanUtils.getPropertyDescriptor(savedObject.getClass(), "id");
if (idPd == null) {
idPd = BeanUtils.getPropertyDescriptor(savedObject.getClass(), "_id");
}
if (idPd != null) {
Object v = bw.getPropertyValue(idPd.getName());
if (v == null) {
if (id instanceof ObjectId) {
bw.setPropertyValue(idPd.getName(), id.toString());
}
else if (id.getClass().isAssignableFrom(idPd.getPropertyType())) {
bw.setPropertyValue(idPd.getName(), id);
}
}
}
}
public void afterPropertiesSet() throws Exception { public void afterPropertiesSet() throws Exception {
if (this.getDefaultCollectionName() != null) { if (this.getDefaultCollectionName() != null) {

Loading…
Cancel
Save