DATADOC-78: Added code to MongoTemplate to check for overriden collection name when saving an entity without specifying a collection. Removed SLF4J from source code and reverted to commons-logging API for internal logging.

This commit is contained in:
Jon Brisbin
2011-04-04 15:14:42 -05:00
committed by J. Brisbin
parent b25e0c432e
commit 3fdd43bef8
16 changed files with 124 additions and 199 deletions

View File

@@ -82,45 +82,6 @@
<version>1.6.1</version>
</dependency>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<exclusions>
<exclusion>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
</exclusion>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
</exclusions>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>jsr250-api</artifactId>

View File

@@ -26,45 +26,6 @@
<artifactId>spring-web</artifactId>
</dependency>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<exclusions>
<exclusion>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
</exclusion>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
</exclusions>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>jsr250-api</artifactId>
@@ -95,34 +56,16 @@
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework.version}</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework.version}</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework.version}</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>

View File

@@ -103,12 +103,6 @@
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework.version}</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
@@ -169,6 +163,7 @@
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>

View File

@@ -2,7 +2,6 @@ package org.springframework.data.document.persistence.test;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.data.persistence.document.mongo.MongoDocumentBacking;
//@DocumentEntity
public class Resume {

View File

@@ -62,45 +62,6 @@
</exclusions>
</dependency>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<exclusions>
<exclusion>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
</exclusion>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
</exclusions>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-mongodb</artifactId>

View File

@@ -57,6 +57,8 @@ import org.springframework.data.document.mongodb.convert.MappingMongoConverter;
import org.springframework.data.document.mongodb.convert.MongoConverter;
import org.springframework.data.document.mongodb.convert.SimpleMongoConverter;
import org.springframework.data.document.mongodb.index.IndexDefinition;
import org.springframework.data.document.mongodb.mapping.Document;
import org.springframework.data.document.mongodb.mapping.MongoPersistentEntity;
import org.springframework.data.document.mongodb.mapping.event.AfterConvertEvent;
import org.springframework.data.document.mongodb.mapping.event.AfterLoadEvent;
import org.springframework.data.document.mongodb.mapping.event.AfterSaveEvent;
@@ -65,6 +67,7 @@ import org.springframework.data.document.mongodb.mapping.event.BeforeSaveEvent;
import org.springframework.data.document.mongodb.mapping.event.MongoMappingEvent;
import org.springframework.data.document.mongodb.query.Query;
import org.springframework.data.document.mongodb.query.Update;
import org.springframework.data.mapping.model.MappingContext;
import org.springframework.jca.cci.core.ConnectionCallback;
import org.springframework.util.Assert;
@@ -505,11 +508,19 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
// Find methods that take a Query to express the query and that return a List of objects.
public <T> List<T> find(Query query, Class<T> targetClass) {
return find(getDefaultCollectionName(), query, targetClass);
String collName = getEntityCollection(targetClass);
if (null == collName) {
collName = getRequiredDefaultCollectionName();
}
return find(collName, query, targetClass);
}
public <T> List<T> find(Query query, Class<T> targetClass, MongoReader<T> reader) {
return find(getDefaultCollectionName(), query, targetClass, reader);
String collName = getEntityCollection(targetClass);
if (null == collName) {
collName = getRequiredDefaultCollectionName();
}
return find(collName, query, targetClass, reader);
}
public <T> List<T> find(String collectionName, final Query query, Class<T> targetClass) {
@@ -574,7 +585,11 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
* @see org.springframework.data.document.mongodb.MongoOperations#insert(java.lang.Object)
*/
public void insert(Object objectToSave) {
insert(getRequiredDefaultCollectionName(), objectToSave);
String collName = getEntityCollection(objectToSave);
if (null == collName) {
collName = getRequiredDefaultCollectionName();
}
insert(collName, objectToSave);
}
/* (non-Javadoc)
@@ -588,7 +603,11 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
* @see org.springframework.data.document.mongodb.MongoOperations#insert(T, org.springframework.data.document.mongodb.MongoWriter)
*/
public <T> void insert(T objectToSave, MongoWriter<T> writer) {
insert(getDefaultCollectionName(), objectToSave, writer);
String collName = getEntityCollection(objectToSave);
if (null == collName) {
collName = getDefaultCollectionName();
}
insert(collName, objectToSave, writer);
}
/* (non-Javadoc)
@@ -1084,6 +1103,36 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
return name;
}
private String getEntityCollection(Object obj) {
if (null != obj) {
return getEntityCollection(obj.getClass());
}
return null;
}
private String getEntityCollection(Class<?> clazz) {
if (mongoConverter instanceof MappingMongoConverter) {
MappingContext ctx = ((MappingMongoConverter) mongoConverter).getMappingContext();
MongoPersistentEntity entity = (MongoPersistentEntity) ctx.getPersistentEntity(clazz);
if (null != entity) {
return entity.getCollection();
}
}
// Entity hasn't yet been added, try and figure it out anyway
if (clazz.isAnnotationPresent(Document.class)) {
Document doc = clazz.getAnnotation(Document.class);
if (!"".equals(doc.collection())) {
return doc.collection();
}
// Default to simple name
return clazz.getSimpleName().toLowerCase();
}
return null;
}
/**
* Checks and handles any errors.
* <p/>

View File

@@ -41,7 +41,13 @@ public class MongoPersistentEntity<T> extends BasicPersistentEntity<T> {
*/
public MongoPersistentEntity(MappingContext mappingContext, TypeInformation typeInformation) {
super(mappingContext, typeInformation);
this.collection = typeInformation.getType().getSimpleName();
this.collection = typeInformation.getType().getSimpleName().toLowerCase();
if (typeInformation.getType().isAnnotationPresent(Document.class)) {
Document d = typeInformation.getType().getAnnotation(Document.class);
if (!"".equals(d.collection())) {
this.collection = d.collection();
}
}
}
/**

View File

@@ -26,8 +26,8 @@ import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.MongoException;
import com.mongodb.util.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.dao.DataAccessException;
import org.springframework.data.document.mongodb.CollectionCallback;
@@ -50,7 +50,7 @@ import org.springframework.util.Assert;
*/
public class MongoPersistentEntityIndexCreator implements ApplicationListener<MappingContextEvent> {
private static final Logger log = LoggerFactory.getLogger(MongoPersistentEntityIndexCreator.class);
private static final Log log = LogFactory.getLog(MongoPersistentEntityIndexCreator.class);
private Set<Class<?>> classesSeen = Collections.newSetFromMap(new ConcurrentHashMap<Class<?>, Boolean>());

View File

@@ -17,8 +17,8 @@
package org.springframework.data.document.mongodb.mapping.event;
import com.mongodb.DBObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
@@ -27,7 +27,7 @@ import org.springframework.context.ApplicationListener;
*/
public abstract class AbstractMappingEventListener<T extends ApplicationEvent, E> implements ApplicationListener<T> {
protected final Logger log = LoggerFactory.getLogger(getClass());
protected final Log log = LogFactory.getLog(getClass());
@SuppressWarnings({"unchecked"})
public void onApplicationEvent(T appEvent) {

View File

@@ -21,8 +21,8 @@ import java.util.Collection;
import java.util.Iterator;
import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.data.document.mongodb.query.Criteria;
import org.springframework.data.document.mongodb.query.CriteriaDefinition;
import org.springframework.data.document.mongodb.query.Query;
@@ -42,7 +42,7 @@ import org.springframework.data.repository.query.parser.PartTree;
*/
class MongoQueryCreator extends AbstractQueryCreator<Query, Query> {
private static final Logger LOG = LoggerFactory.getLogger(MongoQueryCreator.class);
private static final Log LOG = LogFactory.getLog(MongoQueryCreator.class);
/**

View File

@@ -18,8 +18,8 @@ package org.springframework.data.document.mongodb.repository;
import java.io.Serializable;
import java.lang.reflect.Method;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.data.document.mongodb.MongoOperations;
import org.springframework.data.document.mongodb.MongoPropertyDescriptors.MongoPropertyDescriptor;
import org.springframework.data.document.mongodb.MongoTemplate;
@@ -55,8 +55,7 @@ public class MongoRepositoryFactoryBean<T extends MongoRepository<S, ID>, S, ID
/**
* Configures the {@link MongoTemplate} to be used.
*
* @param template
* the template to set
* @param template the template to set
*/
public void setTemplate(MongoTemplate template) {
@@ -274,7 +273,7 @@ public class MongoRepositoryFactoryBean<T extends MongoRepository<S, ID>, S, ID
*/
private static class IndexEnsuringQueryCreationListener implements QueryCreationListener<PartTreeMongoQuery> {
private static final Logger LOG = LoggerFactory.getLogger(IndexEnsuringQueryCreationListener.class);
private static final Log LOG = LogFactory.getLog(IndexEnsuringQueryCreationListener.class);
private final MongoOperations operations;
public IndexEnsuringQueryCreationListener(MongoOperations operations) {

View File

@@ -18,9 +18,9 @@ package org.springframework.data.document.mongodb.repository;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.bson.types.ObjectId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.document.mongodb.MongoTemplate;
import org.springframework.data.document.mongodb.query.BasicQuery;
import org.springframework.data.document.mongodb.query.Query;
@@ -33,7 +33,7 @@ import org.springframework.data.document.mongodb.query.Query;
public class StringBasedMongoQuery extends AbstractMongoQuery {
private static final Pattern PLACEHOLDER = Pattern.compile("\\?(\\d+)");
private static final Logger LOG = LoggerFactory.getLogger(StringBasedMongoQuery.class);
private static final Log LOG = LogFactory.getLog(StringBasedMongoQuery.class);
private final String query;
private final String fieldSpec;
@@ -71,7 +71,9 @@ public class StringBasedMongoQuery extends AbstractMongoQuery {
query = new BasicQuery(queryString);
}
LOG.debug("Created query {}", query.getQueryObject());
if (LOG.isDebugEnabled()) {
LOG.debug(String.format("Created query {}", query.getQueryObject()));
}
return query;
}

View File

@@ -21,7 +21,7 @@ import org.bson.types.ObjectId;
/**
* @author Jon Brisbin <jbrisbin@vmware.com>
*/
@Document
@Document(collection = "places")
public class Location {
private ObjectId id;

View File

@@ -17,8 +17,8 @@
package org.springframework.data.document.mongodb.mapping;
import com.mongodb.DBObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.data.document.mongodb.mapping.event.AbstractMappingEventListener;
/**
@@ -26,7 +26,7 @@ import org.springframework.data.document.mongodb.mapping.event.AbstractMappingEv
*/
public class MappingEventsListener<MongoMappingEvent> extends AbstractMappingEventListener {
private Logger log = LoggerFactory.getLogger(getClass());
private Log log = LogFactory.getLog(getClass());
@Override
public void onBeforeConvert(Object source) {

View File

@@ -24,11 +24,12 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.mongodb.DB;
import com.mongodb.Mongo;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.document.mongodb.MongoDbUtils;
@@ -41,7 +42,8 @@ import org.springframework.data.document.mongodb.query.Query;
*/
public class MappingTests {
private static final Logger LOGGER = LoggerFactory.getLogger(MongoDbUtils.class);
private static final Log LOGGER = LogFactory.getLog(MongoDbUtils.class);
private final String[] collectionsToDrop = new String[]{"person", "personmapproperty", "personpojo", "personcustomidname", "account"};
ApplicationContext applicationContext;
MongoTemplate template;
@@ -50,7 +52,10 @@ public class MappingTests {
@Before
public void setUp() throws Exception {
Mongo mongo = new Mongo();
mongo.getDB("database").getCollection("person").drop();
DB db = mongo.getDB("database");
for (String coll : collectionsToDrop) {
db.getCollection(coll).drop();
}
applicationContext = new ClassPathXmlApplicationContext("/mapping.xml");
template = applicationContext.getBean(MongoTemplate.class);
mappingContext = applicationContext.getBean(MongoMappingContext.class);
@@ -58,6 +63,9 @@ public class MappingTests {
@Test
public void testPersonPojo() throws Exception {
// POJOs aren't auto-detected, have to add manually
mappingContext.addPersistentEntity(PersonPojo.class);
LOGGER.info("about to create new personpojo");
PersonPojo p = new PersonPojo(12345, "Person", "Pojo");
LOGGER.info("about to insert");
@@ -73,6 +81,9 @@ public class MappingTests {
@Test
public void testPersonWithCustomIdName() {
// POJOs aren't auto-detected, have to add manually
mappingContext.addPersistentEntity(PersonCustomIdName.class);
PersonCustomIdName p = new PersonCustomIdName(123456, "Custom", "Id");
template.insert(p);
@@ -163,15 +174,15 @@ public class MappingTests {
}
@Test
public void testPrimitives() {
public void testPrimitivesAndCustomCollectionName() {
Location loc = new Location(
new double[]{1.0, 2.0},
new int[]{1, 2, 3, 4},
new float[]{1.0f, 2.0f}
);
template.insert("locations", loc);
template.insert(loc);
List<Location> result = template.find("locations", new Query(Criteria.where("_id").is(loc.getId())), Location.class);
List<Location> result = template.find("places", new Query(Criteria.where("_id").is(loc.getId())), Location.class);
assertThat(result.size(), is(1));
}

View File

@@ -33,5 +33,4 @@ Import-Template:
org.apache.commons.collections15.*;version="[4.0.0,5.0.0)",
org.apache.commons.logging.*;version="[1.1.1, 2.0.0)",
org.bson.*;version="0",
org.slf4j.*;version="[1.5.0,1.6.0)",
org.w3c.dom.*;version="0"