DATADOC-122 added a MongoCollectionsUtils to provide a method for preferred collection name

This commit is contained in:
Thomas Risberg
2011-05-18 17:24:41 -04:00
parent 6287fa425d
commit bf5fc0ff1f
6 changed files with 70 additions and 10 deletions

View File

@@ -0,0 +1,49 @@
/*
* Copyright 2011 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.data.document.mongodb;
/**
* Helper class featuring helper methods for working with MongoDb collections.
* <p/>
* <p>
* Mainly intended for internal use within the framework.
*
* @author Thomas Risberg
* @since 1.0
*/
public abstract class MongoCollectionUtils {
/**
* Private constructor to prevent instantiation.
*/
private MongoCollectionUtils() {
}
/**
* Obtains the collection name to use for the provided class
*
* @param entityClass
* The class to determine the preferred collection name for
* @return The preferred collection name
*/
public static String getPreferredCollectionName(Class<?> entityClass) {
return entityClass.getSimpleName();
}
}

View File

@@ -16,7 +16,6 @@
package org.springframework.data.document.mongodb;
import com.mongodb.CommandResult;
import com.mongodb.DB;
import com.mongodb.Mongo;
import org.apache.commons.logging.Log;

View File

@@ -604,7 +604,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
String collection = dbref.collection();
if ("".equals(collection)) {
collection = targetEntity.getType().getSimpleName().toLowerCase();
collection = targetEntity.getCollection();
}
String dbname = dbref.db();

View File

@@ -16,6 +16,7 @@
package org.springframework.data.document.mongodb.mapping;
import org.springframework.data.document.mongodb.MongoCollectionUtils;
import org.springframework.data.mapping.BasicPersistentEntity;
import org.springframework.data.mapping.model.MappingException;
import org.springframework.data.mapping.model.PersistentEntity;
@@ -46,7 +47,7 @@ public class BasicMongoPersistentEntity<T> extends BasicPersistentEntity<T, Mong
super(typeInformation);
Class<?> rawType = typeInformation.getType();
String fallback = rawType.getSimpleName().toLowerCase();
String fallback = MongoCollectionUtils.getPreferredCollectionName(rawType);
if (rawType.isAnnotationPresent(Document.class)) {
Document d = rawType.getAnnotation(Document.class);

View File

@@ -20,6 +20,7 @@ import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import org.springframework.data.document.mongodb.MongoCollectionUtils;
import org.springframework.data.mapping.AbstractMappingContext;
import org.springframework.data.mapping.BasicPersistentEntity;
import org.springframework.data.mapping.AbstractPersistentProperty;
@@ -103,7 +104,7 @@ public class SimpleMongoMappingContext extends
* @see org.springframework.data.document.mongodb.mapping.MongoPersistentEntity#getCollection()
*/
public String getCollection() {
return getType().getSimpleName();
return MongoCollectionUtils.getPreferredCollectionName(getType());
}
}
}

View File

@@ -40,8 +40,10 @@ import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.dao.DataAccessException;
import org.springframework.data.document.mongodb.CollectionCallback;
import org.springframework.data.document.mongodb.MongoCollectionUtils;
import org.springframework.data.document.mongodb.MongoDbUtils;
import org.springframework.data.document.mongodb.MongoTemplate;
import org.springframework.data.document.mongodb.convert.CustomConvertersUnitTests.Foo;
import org.springframework.data.document.mongodb.query.Criteria;
import org.springframework.data.document.mongodb.query.Query;
@@ -51,9 +53,17 @@ import org.springframework.data.document.mongodb.query.Query;
public class MappingTests {
private static final Log LOGGER = LogFactory.getLog(MongoDbUtils.class);
private final String[] collectionsToDrop = new String[] { "foobar", "person", "personmapproperty", "personpojo",
"personcustomidname", "personmultidimarrays", "personmulticollection", "personwithdbref", "personnullproperties",
"person1", "person2", "account" };
private final String[] collectionsToDrop = new String[] {
MongoCollectionUtils.getPreferredCollectionName(Person.class),
MongoCollectionUtils.getPreferredCollectionName(PersonMapProperty.class),
MongoCollectionUtils.getPreferredCollectionName(PersonPojo.class),
MongoCollectionUtils.getPreferredCollectionName(PersonCustomIdName.class),
MongoCollectionUtils.getPreferredCollectionName(PersonMultiDimArrays.class),
MongoCollectionUtils.getPreferredCollectionName(PersonMultiCollection.class),
MongoCollectionUtils.getPreferredCollectionName(PersonWithDbRef.class),
MongoCollectionUtils.getPreferredCollectionName(PersonNullProperties.class),
MongoCollectionUtils.getPreferredCollectionName(Account.class),
"foobar", "geolocation", "person1", "person2", "account" };
ApplicationContext applicationContext;
MongoTemplate template;
@@ -163,14 +173,14 @@ public class MappingTests {
Person p = new Person(123456789, "John", "Doe", 37, addr);
p.setAccounts(accounts);
template.insert("person", p);
template.insert("Person", p);
Account newAcct = new Account();
newAcct.setBalance(10000.00f);
template.insert("account", newAcct);
accounts.add(newAcct);
template.save("person", p);
template.save("Person", p);
assertNotNull(p.getId());
@@ -243,7 +253,7 @@ public class MappingTests {
DetectedCollectionWithIndex dcwi = new DetectedCollectionWithIndex("test");
template.insert(dcwi);
assertTrue(template.execute(DetectedCollectionWithIndex.class.getSimpleName().toLowerCase(),
assertTrue(template.execute(MongoCollectionUtils.getPreferredCollectionName(DetectedCollectionWithIndex.class),
new CollectionCallback<Boolean>() {
public Boolean doInCollection(DBCollection collection) throws MongoException, DataAccessException {
List<DBObject> indexes = collection.getIndexInfo();