DATADOC-47 - Adapted changes of DATACMNS-17.

This commit is contained in:
Oliver Gierke
2011-02-24 17:29:17 +00:00
parent 86cc313d80
commit 3caa88a2da
6 changed files with 120 additions and 149 deletions

View File

@@ -19,8 +19,7 @@ import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import org.springframework.data.repository.support.IdAware;
import org.springframework.data.repository.support.IsNewAware;
import org.springframework.data.repository.support.AbstractEntityMetadata;
import org.springframework.util.ReflectionUtils;
@@ -30,18 +29,20 @@ import org.springframework.util.ReflectionUtils;
*
* @author Oliver Gierke
*/
class MongoEntityInformation implements IsNewAware, IdAware {
class MongoEntityMetadata<T extends Object> extends AbstractEntityMetadata<T> {
private static final List<String> FIELD_NAMES = Arrays.asList("ID", "id", "_id");
private Field field;
/**
* Creates a new {@link MongoEntityInformation}.
* Creates a new {@link MongoEntityMetadata}.
*
* @param domainClass
*/
public MongoEntityInformation(Class<?> domainClass) {
public MongoEntityMetadata(Class<T> domainClass) {
super(domainClass);
for (String name : FIELD_NAMES) {
@@ -62,19 +63,6 @@ class MongoEntityInformation implements IsNewAware, IdAware {
}
/*
* (non-Javadoc)
*
* @see
* org.springframework.data.repository.support.IsNewAware#isNew(java.lang
* .Object)
*/
public boolean isNew(Object entity) {
return null == ReflectionUtils.getField(field, entity);
}
/**
* Returns the actual field name containing the id.
*

View File

@@ -20,142 +20,136 @@ import java.lang.reflect.Method;
import org.springframework.data.document.mongodb.MongoPropertyDescriptors.MongoPropertyDescriptor;
import org.springframework.data.document.mongodb.MongoTemplate;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.query.QueryLookupStrategy;
import org.springframework.data.repository.query.QueryLookupStrategy.Key;
import org.springframework.data.repository.query.RepositoryQuery;
import org.springframework.data.repository.support.EntityMetadata;
import org.springframework.data.repository.support.RepositoryFactoryBeanSupport;
import org.springframework.data.repository.support.RepositoryFactorySupport;
import org.springframework.data.repository.support.RepositorySupport;
import org.springframework.data.repository.util.ClassUtils;
import org.springframework.data.repository.support.RepositoryMetadata;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* {@link org.springframework.beans.factory.FactoryBean} to create {@link MongoRepository} instances.
*
* @author Oliver Gierke
*/
public class MongoRepositoryFactoryBean extends
RepositoryFactoryBeanSupport<MongoRepository<?, ?>> {
public class MongoRepositoryFactoryBean extends RepositoryFactoryBeanSupport<MongoRepository<?, ?>> {
private MongoTemplate template;
private MongoTemplate template;
/**
* Configures the {@link MongoTemplate} to be used.
*
* @param template the template to set
*/
public void setTemplate(MongoTemplate template) {
/**
* Configures the {@link MongoTemplate} to be used.
*
* @param template the template to set
*/
public void setTemplate(MongoTemplate template) {
this.template = template;
}
this.template = template;
}
/*
* (non-Javadoc)
*
* @see org.springframework.data.repository.support.RepositoryFactoryBeanSupport #createRepositoryFactory()
*/
@Override
protected RepositoryFactorySupport createRepositoryFactory() {
return new MongoRepositoryFactory(template);
}
/*
* (non-Javadoc)
*
* @see
* org.springframework.data.repository.support.RepositoryFactoryBeanSupport
* #createRepositoryFactory()
*/
@Override
protected RepositoryFactorySupport createRepositoryFactory() {
/*
* (non-Javadoc)
*
* @see org.springframework.data.repository.support.RepositoryFactoryBeanSupport #afterPropertiesSet()
*/
@Override
public void afterPropertiesSet() {
return new MongoRepositoryFactory(template);
}
super.afterPropertiesSet();
Assert.notNull(template, "MongoTemplate must not be null!");
}
/**
* Repository to create {@link MongoRepository} instances.
*
* @author Oliver Gierke
*/
public static class MongoRepositoryFactory extends RepositoryFactorySupport {
/*
* (non-Javadoc)
*
* @see
* org.springframework.data.repository.support.RepositoryFactoryBeanSupport
* #afterPropertiesSet()
*/
@Override
public void afterPropertiesSet() {
private final MongoTemplate template;
super.afterPropertiesSet();
Assert.notNull(template, "MongoTemplate must not be null!");
}
/**
* Repository to create {@link MongoRepository} instances.
*
* @author Oliver Gierke
*/
public static class MongoRepositoryFactory extends RepositoryFactorySupport {
private final MongoTemplate template;
/**
* Creates a new {@link MongoRepositoryFactory} fwith the given {@link MongoTemplate}.
*
* @param template
*/
/**
* Creates a new {@link MongoRepositoryFactory} fwith the given {@link MongoTemplate}.
*
* @param template
*/
public MongoRepositoryFactory(MongoTemplate template) {
this.template = template;
}
@Override
@SuppressWarnings("unchecked")
protected Object getTargetRepository(RepositoryMetadata metadata) {
EntityMetadata<Object> info = new MongoEntityMetadata<Object>((Class<Object>) metadata.getDomainClass());
return new SimpleMongoRepository<Object, Serializable>(info, template);
}
/*
* (non-Javadoc)
*
* @see org.springframework.data.repository.support.RepositoryFactorySupport#getRepositoryBaseClass()
*/
@Override
protected Class<?> getRepositoryBaseClass(Class<?> repositoryInterface) {
return SimpleMongoRepository.class;
}
@Override
protected <T, ID extends Serializable> RepositorySupport<T, ID> getTargetRepository(
Class<T> domainClass, Class<?> repositoryInterface) {
protected QueryLookupStrategy getQueryLookupStrategy(Key key) {
return new SimpleMongoRepository<T, ID>(domainClass, template);
}
return new MongoQueryLookupStrategy();
}
/**
* {@link QueryLookupStrategy} to create {@link PartTreeMongoQuery} instances.
*
* @author Oliver Gierke
*/
private class MongoQueryLookupStrategy implements QueryLookupStrategy {
@Override
@SuppressWarnings("rawtypes")
protected Class<? extends RepositorySupport> getRepositoryClass(Class<?> repositoryInterface) {
public RepositoryQuery resolveQuery(Method method) {
return SimpleMongoRepository.class;
}
MongoQueryMethod queryMethod = new MongoQueryMethod(method);
@Override
protected QueryLookupStrategy getQueryLookupStrategy(Key key) {
return new MongoQueryLookupStrategy();
}
/**
* {@link QueryLookupStrategy} to create {@link PartTreeMongoQuery} instances.
*
* @author Oliver Gierke
*/
private class MongoQueryLookupStrategy implements QueryLookupStrategy {
public RepositoryQuery resolveQuery(Method method) {
MongoQueryMethod queryMethod = new MongoQueryMethod(method);
if (queryMethod.hasAnnotatedQuery()) {
if (queryMethod.hasAnnotatedQuery()) {
return new StringBasedMongoQuery(queryMethod, template);
} else {
return new PartTreeMongoQuery(queryMethod, template);
}
}
}
/* (non-Javadoc)
* @see org.springframework.data.repository.support.RepositoryFactorySupport#validate(java.lang.Class, java.lang.Object)
*/
@Override
protected void validate(Class<? extends Repository<?, ?>> repositoryInterface, Object customImplementation) {
Class<?> idClass = ClassUtils.getIdClass(repositoryInterface);
if (!MongoPropertyDescriptor.SUPPORTED_ID_CLASSES.contains(idClass)) {
}
}
/*
* (non-Javadoc)
*
* @see org.springframework.data.repository.support.RepositoryFactorySupport#validate(java.lang.Class,
* java.lang.Object)
*/
@Override
protected void validate(RepositoryMetadata metadata, Object customImplementation) {
Class<?> idClass = metadata.getIdClass();
if (!MongoPropertyDescriptor.SUPPORTED_ID_CLASSES.contains(idClass)) {
throw new IllegalArgumentException(String.format("Unsupported id class! Only %s are supported!",
StringUtils.collectionToCommaDelimitedString(MongoPropertyDescriptor.SUPPORTED_ID_CLASSES)));
}
super.validate(repositoryInterface, customImplementation);
}
}
}
super.validate(metadata, customImplementation);
}
}
}

View File

@@ -31,8 +31,7 @@ import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.support.IsNewAware;
import org.springframework.data.repository.support.RepositorySupport;
import org.springframework.data.repository.support.EntityMetadata;
import org.springframework.util.Assert;
@@ -41,11 +40,10 @@ import org.springframework.util.Assert;
*
* @author Oliver Gierke
*/
public class SimpleMongoRepository<T, ID extends Serializable> extends
RepositorySupport<T, ID> implements PagingAndSortingRepository<T, ID> {
public class SimpleMongoRepository<T, ID extends Serializable> implements PagingAndSortingRepository<T, ID> {
private final MongoTemplate template;
private MongoEntityInformation entityInformation;
private final EntityMetadata<T> entityInformation;
/**
@@ -55,13 +53,16 @@ public class SimpleMongoRepository<T, ID extends Serializable> extends
* @param domainClass
* @param template
*/
public SimpleMongoRepository(Class<T> domainClass, MongoTemplate template) {
super(domainClass);
public SimpleMongoRepository(EntityMetadata<T> entityInformation, MongoTemplate template) {
Assert.notNull(entityInformation);
Assert.notNull(template);
this.entityInformation = entityInformation;
this.template = template;
createIsNewStrategy(domainClass);
}
private Class<T> getDomainClass() {
return entityInformation.getJavaType();
}
@@ -228,21 +229,4 @@ public class SimpleMongoRepository<T, ID extends Serializable> extends
return template.find(getCollectionName(getDomainClass()), query,
getDomainClass());
}
/*
* (non-Javadoc)
*
* @see org.springframework.data.repository.support.RepositorySupport#
* createIsNewStrategy(java.lang.Class)
*/
@Override
protected IsNewAware createIsNewStrategy(Class<?> domainClass) {
if (entityInformation == null) {
this.entityInformation = new MongoEntityInformation(domainClass);
}
return entityInformation;
}
}

View File

@@ -22,17 +22,17 @@ import org.junit.Test;
/**
* Unit test for {@link MongoEntityInformation}.
* Unit test for {@link MongoEntityMetadata}.
*
* @author Oliver Gierke
*/
public class MongoEntityInformationUnitTests {
public class MongoEntityMetadataUnitTests {
@Test
public void findsIdField() throws Exception {
MongoEntityInformation isNewAware =
new MongoEntityInformation(Person.class);
MongoEntityMetadata<Person> isNewAware =
new MongoEntityMetadata<Person>(Person.class);
Person person = new Person();
assertThat(isNewAware.isNew(person), is(true));
@@ -44,7 +44,7 @@ public class MongoEntityInformationUnitTests {
@Test(expected = IllegalArgumentException.class)
public void rejectsClassIfNoIdField() throws Exception {
new MongoEntityInformation(InvalidPerson.class);
new MongoEntityMetadata<InvalidPerson>(InvalidPerson.class);
}
class Person {

View File

@@ -20,7 +20,6 @@ import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.data.document.mongodb.MongoTemplate;
import org.springframework.data.document.mongodb.User;
import org.springframework.data.document.mongodb.repository.MongoRepositoryFactoryBean.MongoRepositoryFactory;
/**
@@ -40,7 +39,7 @@ public class MongoRepositoryFactoryUnitTests {
factory.getRepository(SampleRepository.class);
}
private interface SampleRepository extends MongoRepository<User, Long> {
private interface SampleRepository extends MongoRepository<Person, Long> {
}
}

View File

@@ -1,6 +1,12 @@
Spring Data Document Changelog
=============================================
Changes in version 1.0.0.M2 MongoDB
------------------------------------------------
Repository
* Adapted new metamodel API (DATADOC-47, DATACMNS-17)
Changes in version 1.0.0.M1 MongoDB (2011-02-14)
------------------------------------------------