Skip to content

DATAMONGO-1185 - Provide lifecycle events when using QueryDSL #324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>
Expand All @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data.build</groupId>
<artifactId>spring-data-parent</artifactId>
<version>1.8.0.BUILD-SNAPSHOT</version>
<version>1.7.0.RELEASE</version>
</parent>

<modules>
Expand All @@ -28,7 +28,7 @@
<properties>
<project.type>multi</project.type>
<dist.id>spring-data-mongodb</dist.id>
<springdata.commons>1.12.0.BUILD-SNAPSHOT</springdata.commons>
<springdata.commons>1.11.0.RELEASE</springdata.commons>
<mongo>2.13.0</mongo>
<mongo.osgi>2.13.0</mongo.osgi>
</properties>
Expand Down Expand Up @@ -156,8 +156,8 @@

<repositories>
<repository>
<id>spring-libs-snapshot</id>
<url>https://repo.spring.io/libs-snapshot</url>
<id>spring-libs-release</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@

import java.lang.annotation.Annotation;
import java.util.Set;

import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;

import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.repository.support.MongoRepositoryFactory;
import org.springframework.data.repository.cdi.CdiRepositoryBean;
Expand All @@ -33,10 +32,12 @@
*
* @author Oliver Gierke
* @author Mark Paluch
* @author Jordi Llach
*/
public class MongoRepositoryBean<T> extends CdiRepositoryBean<T> {

private final Bean<MongoOperations> operations;
private final ApplicationEventPublisher eventPublisher;

/**
* Creates a new {@link MongoRepositoryBean}.
Expand All @@ -47,14 +48,18 @@ public class MongoRepositoryBean<T> extends CdiRepositoryBean<T> {
* @param beanManager must not be {@literal null}.
* @param detector detector for the custom {@link org.springframework.data.repository.Repository} implementations
* {@link CustomRepositoryImplementationDetector}, can be {@literal null}.
* @param eventPublisher must not be {@literal null}.
*/
public MongoRepositoryBean(Bean<MongoOperations> operations, Set<Annotation> qualifiers, Class<T> repositoryType,
BeanManager beanManager, CustomRepositoryImplementationDetector detector) {
BeanManager beanManager, CustomRepositoryImplementationDetector detector,
ApplicationEventPublisher eventPublisher) {

super(qualifiers, repositoryType, beanManager, detector);

Assert.notNull(operations);
Assert.notNull(eventPublisher);
this.operations = operations;
this.eventPublisher = eventPublisher;
}

/*
Expand All @@ -65,7 +70,7 @@ public MongoRepositoryBean(Bean<MongoOperations> operations, Set<Annotation> qua
protected T create(CreationalContext<T> creationalContext, Class<T> repositoryType, Object customImplementation) {

MongoOperations mongoOperations = getDependencyInstance(operations, MongoOperations.class);
MongoRepositoryFactory factory = new MongoRepositoryFactory(mongoOperations);
MongoRepositoryFactory factory = new MongoRepositoryFactory(mongoOperations, eventPublisher);

return factory.getRepository(repositoryType, customImplementation);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.enterprise.event.Observes;
import javax.enterprise.inject.UnsatisfiedResolutionException;
import javax.enterprise.inject.spi.AfterBeanDiscovery;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.ProcessBean;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.repository.cdi.CdiRepositoryBean;
import org.springframework.data.repository.cdi.CdiRepositoryExtensionSupport;
Expand All @@ -41,6 +41,7 @@
*
* @author Oliver Gierke
* @author Mark Paluch
* @author Jordi Llach
*/
public class MongoRepositoryExtension extends CdiRepositoryExtensionSupport {

Expand Down Expand Up @@ -112,6 +113,20 @@ private <T> CdiRepositoryBean<T> createRepositoryBean(Class<T> repositoryType, S

// Construct and return the repository bean.
return new MongoRepositoryBean<T>(mongoOperations, qualifiers, repositoryType, beanManager,
getCustomImplementationDetector());
getCustomImplementationDetector(),
new NullCdiApplicationEvenPublisher());
}
}


private final class NullCdiApplicationEvenPublisher
implements ApplicationEventPublisher {

@Override
public void publishEvent(ApplicationEvent event) {
// NOTHING TO DO HERE ... as stated in ApplicationEventPublisher you need an ApplicationContext
// to enable the publishing of all persistence events such as {@link AfterLoadEvent}, {@link AfterSaveEvent}, etc.
// BUT it could be a good idea to check CDI Events
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@
*/
package org.springframework.data.mongodb.repository.support;

import static org.springframework.data.querydsl.QueryDslUtils.*;

import java.io.Serializable;
import java.lang.reflect.Method;

import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.mapping.model.MappingException;
import org.springframework.data.mongodb.core.MongoOperations;
Expand All @@ -31,6 +29,7 @@
import org.springframework.data.mongodb.repository.query.PartTreeMongoQuery;
import org.springframework.data.mongodb.repository.query.StringBasedMongoQuery;
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
import static org.springframework.data.querydsl.QueryDslUtils.QUERY_DSL_PRESENT;
import org.springframework.data.repository.core.NamedQueries;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.repository.core.RepositoryMetadata;
Expand All @@ -47,25 +46,29 @@
*
* @author Oliver Gierke
* @author Thomas Darimont
* @author Jordi Llach
*/
public class MongoRepositoryFactory extends RepositoryFactorySupport {

private static final SpelExpressionParser EXPRESSION_PARSER = new SpelExpressionParser();

private final MongoOperations mongoOperations;
private final MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext;
private final ApplicationEventPublisher eventPublisher;

/**
* Creates a new {@link MongoRepositoryFactory} with the given {@link MongoOperations}.
*
* @param mongoOperations must not be {@literal null}.
* @param eventPublisher must not be {@literal null}.
*/
public MongoRepositoryFactory(MongoOperations mongoOperations) {

public MongoRepositoryFactory(MongoOperations mongoOperations, ApplicationEventPublisher eventPublisher) {
Assert.notNull(mongoOperations);
Assert.notNull(eventPublisher);

this.mongoOperations = mongoOperations;
this.mappingContext = mongoOperations.getConverter().getMappingContext();
this.eventPublisher = eventPublisher;
}

/*
Expand All @@ -89,7 +92,7 @@ protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
protected Object getTargetRepository(RepositoryInformation information) {

MongoEntityInformation<?, Serializable> entityInformation = getEntityInformation(information.getDomainType());
return getTargetRepositoryViaReflection(information, entityInformation, mongoOperations);
return getTargetRepositoryViaReflection(information, entityInformation, mongoOperations, eventPublisher);
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
package org.springframework.data.mongodb.repository.support;

import java.io.Serializable;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.repository.MongoRepository;
Expand All @@ -29,13 +32,16 @@
* {@link org.springframework.beans.factory.FactoryBean} to create {@link MongoRepository} instances.
*
* @author Oliver Gierke
* @author Jordi Llach
*/
public class MongoRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extends Serializable> extends
RepositoryFactoryBeanSupport<T, S, ID> {
public class MongoRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extends Serializable>
extends RepositoryFactoryBeanSupport<T, S, ID>
implements ApplicationContextAware {

private MongoOperations operations;
private boolean createIndexesForQueryMethods = false;
private boolean mappingContextConfigured = false;
private ApplicationEventPublisher eventPublisher;

/**
* Configures the {@link MongoOperations} to be used.
Expand Down Expand Up @@ -65,6 +71,15 @@ protected void setMappingContext(MappingContext<?, ?> mappingContext) {
super.setMappingContext(mappingContext);
this.mappingContextConfigured = true;
}

/*
* (non-Javadoc)
* @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.eventPublisher = applicationContext;
}

/*
* (non-Javadoc)
Expand Down Expand Up @@ -92,7 +107,7 @@ protected final RepositoryFactorySupport createRepositoryFactory() {
* @return
*/
protected RepositoryFactorySupport getFactoryInstance(MongoOperations operations) {
return new MongoRepositoryFactory(operations);
return new MongoRepositoryFactory(operations, eventPublisher);
}

/*
Expand All @@ -106,10 +121,11 @@ protected RepositoryFactorySupport getFactoryInstance(MongoOperations operations
public void afterPropertiesSet() {

super.afterPropertiesSet();
Assert.notNull(operations, "MongoTemplate must not be null!");
Assert.notNull(operations, "MongoTemplate must not be null!");
Assert.notNull(eventPublisher, "ApplicationContext must not be null");

if (!mappingContextConfigured) {
setMappingContext(operations.getConverter().getMappingContext());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@
*/
package org.springframework.data.mongodb.repository.support;

import com.mysema.query.mongodb.MongodbQuery;
import com.mysema.query.types.EntityPath;
import com.mysema.query.types.Expression;
import com.mysema.query.types.OrderSpecifier;
import com.mysema.query.types.Predicate;
import com.mysema.query.types.path.PathBuilder;
import java.io.Serializable;
import java.util.List;

import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
Expand All @@ -34,21 +40,16 @@
import org.springframework.data.repository.core.EntityMetadata;
import org.springframework.util.Assert;

import com.mysema.query.mongodb.MongodbQuery;
import com.mysema.query.types.EntityPath;
import com.mysema.query.types.Expression;
import com.mysema.query.types.OrderSpecifier;
import com.mysema.query.types.Predicate;
import com.mysema.query.types.path.PathBuilder;

/**
* Special QueryDsl based repository implementation that allows execution {@link Predicate}s in various forms.
*
* @author Oliver Gierke
* @author Thomas Darimont
* @author Jordi Llach
*/
public class QueryDslMongoRepository<T, ID extends Serializable> extends SimpleMongoRepository<T, ID> implements
QueryDslPredicateExecutor<T> {
public class QueryDslMongoRepository<T, ID extends Serializable>
extends SimpleMongoRepository<T, ID>
implements QueryDslPredicateExecutor<T> {

private final PathBuilder<T> builder;
private final EntityInformation<T, ID> entityInformation;
Expand All @@ -60,9 +61,11 @@ public class QueryDslMongoRepository<T, ID extends Serializable> extends SimpleM
*
* @param entityInformation must not be {@literal null}.
* @param mongoOperations must not be {@literal null}.
* @param eventPublisher must not be {@literal null}.
*/
public QueryDslMongoRepository(MongoEntityInformation<T, ID> entityInformation, MongoOperations mongoOperations) {
this(entityInformation, mongoOperations, SimpleEntityPathResolver.INSTANCE);
public QueryDslMongoRepository(MongoEntityInformation<T, ID> entityInformation, MongoOperations mongoOperations,
ApplicationEventPublisher eventPublisher) {
this(entityInformation, mongoOperations, SimpleEntityPathResolver.INSTANCE, eventPublisher);
}

/**
Expand All @@ -72,11 +75,12 @@ public QueryDslMongoRepository(MongoEntityInformation<T, ID> entityInformation,
* @param entityInformation must not be {@literal null}.
* @param mongoOperations must not be {@literal null}.
* @param resolver must not be {@literal null}.
* @param eventPublisher must not be {@literal null}.
*/
public QueryDslMongoRepository(MongoEntityInformation<T, ID> entityInformation, MongoOperations mongoOperations,
EntityPathResolver resolver) {
EntityPathResolver resolver, ApplicationEventPublisher eventPublisher) {

super(entityInformation, mongoOperations);
super(entityInformation, mongoOperations, eventPublisher);

Assert.notNull(resolver);
EntityPath<T> path = resolver.createPath(entityInformation.getJavaType());
Expand Down Expand Up @@ -200,7 +204,7 @@ private MongodbQuery<T> createQueryFor(Predicate predicate) {
* @return
*/
private MongodbQuery<T> createQuery() {
return new SpringDataMongodbQuery<T>(mongoOperations, entityInformation.getJavaType());
return new SpringDataMongodbQuery<T>(mongoOperations, entityInformation.getJavaType(), eventPublisher);
}

/**
Expand Down
Loading