From 3e485e0a88f026d48634ccc1ab1b223e41b386e7 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Wed, 5 Aug 2015 19:50:14 +0200 Subject: [PATCH] DATAMONGO-1256 - MongoMappingEvents now expose the collection name they're issued for. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We now directly expose the collection name via MongoMappingEvent.getCollectionName(). Therefore we added new constructors to all the events, deprecating the previous ones. Several overloads have been added to MongoEventListener, deprecating previous API. We’ll call the deprecated from the new ones until their removal. Original pull request: #316. --- .../data/mongodb/core/MongoTemplate.java | 74 +++++---- .../mapping/event/AbstractDeleteEvent.java | 21 ++- .../event/AbstractMongoEventListener.java | 156 ++++++++++++++++-- .../core/mapping/event/AfterConvertEvent.java | 28 +++- .../core/mapping/event/AfterDeleteEvent.java | 19 ++- .../core/mapping/event/AfterLoadEvent.java | 21 ++- .../core/mapping/event/AfterSaveEvent.java | 25 ++- .../mapping/event/BeforeConvertEvent.java | 23 ++- .../core/mapping/event/BeforeDeleteEvent.java | 19 ++- .../core/mapping/event/BeforeSaveEvent.java | 25 ++- .../core/mapping/event/MongoMappingEvent.java | 51 +++++- .../config/AuditingIntegrationTests.java | 4 +- ...nwrapAndReadDbObjectCallbackUnitTests.java | 2 +- .../AbstractMongoEventListenerUnitTests.java | 29 ++-- .../event/ApplicationContextEventTests.java | 83 +++++++++- .../event/AuditingEventListenerUnitTests.java | 4 +- .../event/PersonBeforeSaveListener.java | 6 +- .../event/SimpleMappingEventListener.java | 42 +++-- 18 files changed, 526 insertions(+), 106 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java index ec50e0e85..3b8d51078 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java @@ -338,7 +338,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { DBCursor cursor = collection.find(mappedQuery, mappedFields); QueryCursorPreparer cursorPreparer = new QueryCursorPreparer(query, entityType); - ReadDbObjectCallback readCallback = new ReadDbObjectCallback(mongoConverter, entityType); + ReadDbObjectCallback readCallback = new ReadDbObjectCallback(mongoConverter, entityType, collection + .getName()); return new CloseableIterableCusorAdapter(cursorPreparer.prepare(cursor), exceptionTranslator, readCallback); } @@ -637,7 +638,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { results = results == null ? Collections.emptyList() : results; DbObjectCallback> callback = new GeoNearResultDbObjectCallback(new ReadDbObjectCallback( - mongoConverter, entityClass), near.getMetric()); + mongoConverter, entityClass, collectionName), near.getMetric()); List> result = new ArrayList>(results.size()); int index = 0; @@ -789,15 +790,15 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { initializeVersionProperty(objectToSave); - maybeEmitEvent(new BeforeConvertEvent(objectToSave)); + maybeEmitEvent(new BeforeConvertEvent(objectToSave, collectionName)); DBObject dbDoc = toDbObject(objectToSave, writer); - maybeEmitEvent(new BeforeSaveEvent(objectToSave, dbDoc)); + maybeEmitEvent(new BeforeSaveEvent(objectToSave, dbDoc, collectionName)); Object id = insertDBObject(collectionName, dbDoc, objectToSave.getClass()); populateIdIfNecessary(objectToSave, id); - maybeEmitEvent(new AfterSaveEvent(objectToSave, dbDoc)); + maybeEmitEvent(new AfterSaveEvent(objectToSave, dbDoc, collectionName)); } /** @@ -885,10 +886,10 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { initializeVersionProperty(o); BasicDBObject dbDoc = new BasicDBObject(); - maybeEmitEvent(new BeforeConvertEvent(o)); + maybeEmitEvent(new BeforeConvertEvent(o, collectionName)); writer.write(o, dbDoc); - maybeEmitEvent(new BeforeSaveEvent(o, dbDoc)); + maybeEmitEvent(new BeforeSaveEvent(o, dbDoc, collectionName)); dbObjectList.add(dbDoc); } List ids = insertDBObjectList(collectionName, dbObjectList); @@ -896,7 +897,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { for (T obj : batchToSave) { if (i < ids.size()) { populateIdIfNecessary(obj, ids.get(i)); - maybeEmitEvent(new AfterSaveEvent(obj, dbObjectList.get(i))); + maybeEmitEvent(new AfterSaveEvent(obj, dbObjectList.get(i), collectionName)); } i++; } @@ -951,14 +952,14 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { BasicDBObject dbObject = new BasicDBObject(); - maybeEmitEvent(new BeforeConvertEvent(objectToSave)); + maybeEmitEvent(new BeforeConvertEvent(objectToSave, collectionName)); this.mongoConverter.write(objectToSave, dbObject); - maybeEmitEvent(new BeforeSaveEvent(objectToSave, dbObject)); + maybeEmitEvent(new BeforeSaveEvent(objectToSave, dbObject, collectionName)); Update update = Update.fromDBObject(dbObject, ID_FIELD); doUpdate(collectionName, query, update, objectToSave.getClass(), false, false); - maybeEmitEvent(new AfterSaveEvent(objectToSave, dbObject)); + maybeEmitEvent(new AfterSaveEvent(objectToSave, dbObject, collectionName)); } } @@ -966,15 +967,15 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { assertUpdateableIdIfNotSet(objectToSave); - maybeEmitEvent(new BeforeConvertEvent(objectToSave)); + maybeEmitEvent(new BeforeConvertEvent(objectToSave, collectionName)); DBObject dbDoc = toDbObject(objectToSave, writer); - maybeEmitEvent(new BeforeSaveEvent(objectToSave, dbDoc)); + maybeEmitEvent(new BeforeSaveEvent(objectToSave, dbDoc, collectionName)); Object id = saveDBObject(collectionName, dbDoc, objectToSave.getClass()); populateIdIfNecessary(objectToSave, id); - maybeEmitEvent(new AfterSaveEvent(objectToSave, dbDoc)); + maybeEmitEvent(new AfterSaveEvent(objectToSave, dbDoc, collectionName)); } protected Object insertDBObject(final String collectionName, final DBObject dbDoc, final Class entityClass) { @@ -1266,7 +1267,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { return execute(collectionName, new CollectionCallback() { public WriteResult doInCollection(DBCollection collection) throws MongoException, DataAccessException { - maybeEmitEvent(new BeforeDeleteEvent(queryObject, entityClass)); + maybeEmitEvent(new BeforeDeleteEvent(queryObject, entityClass, collectionName)); DBObject dboq = queryMapper.getMappedObject(queryObject, entity); @@ -1284,7 +1285,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { handleAnyWriteResultErrors(wr, dboq, MongoActionOperation.REMOVE); - maybeEmitEvent(new AfterDeleteEvent(queryObject, entityClass)); + maybeEmitEvent(new AfterDeleteEvent(queryObject, entityClass, collectionName)); return wr; } @@ -1292,13 +1293,12 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { } public List findAll(Class entityClass) { - return executeFindMultiInternal(new FindCallback(null), null, new ReadDbObjectCallback(mongoConverter, - entityClass), determineCollectionName(entityClass)); + return findAll(entityClass, determineCollectionName(entityClass)); } public List findAll(Class entityClass, String collectionName) { return executeFindMultiInternal(new FindCallback(null), null, new ReadDbObjectCallback(mongoConverter, - entityClass), collectionName); + entityClass, collectionName), collectionName); } public MapReduceResults mapReduce(String inputCollectionName, String mapFunction, String reduceFunction, @@ -1343,7 +1343,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { } List mappedResults = new ArrayList(); - DbObjectCallback callback = new ReadDbObjectCallback(mongoConverter, entityClass); + DbObjectCallback callback = new ReadDbObjectCallback(mongoConverter, entityClass, inputCollectionName); for (DBObject dbObject : mapReduceOutput.results()) { mappedResults.add(callback.doWith(dbObject)); @@ -1404,7 +1404,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { @SuppressWarnings("unchecked") Iterable resultSet = (Iterable) commandResult.get("retval"); List mappedResults = new ArrayList(); - DbObjectCallback callback = new ReadDbObjectCallback(mongoConverter, entityClass); + DbObjectCallback callback = new ReadDbObjectCallback(mongoConverter, entityClass, inputCollectionName); for (DBObject dbObject : resultSet) { mappedResults.add(callback.doWith(dbObject)); @@ -1506,7 +1506,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { CommandResult commandResult = executeCommand(command, this.readPreference); handleCommandError(commandResult, command); - return new AggregationResults(returnPotentiallyMappedResults(outputType, commandResult), commandResult); + return new AggregationResults(returnPotentiallyMappedResults(outputType, commandResult, collectionName), + commandResult); } /** @@ -1516,7 +1517,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { * @param commandResult * @return */ - private List returnPotentiallyMappedResults(Class outputType, CommandResult commandResult) { + private List returnPotentiallyMappedResults(Class outputType, CommandResult commandResult, + String collectionName) { @SuppressWarnings("unchecked") Iterable resultSet = (Iterable) commandResult.get("result"); @@ -1524,7 +1526,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { return Collections.emptyList(); } - DbObjectCallback callback = new UnwrapAndReadDbObjectCallback(mongoConverter, outputType); + DbObjectCallback callback = new UnwrapAndReadDbObjectCallback(mongoConverter, outputType, collectionName); List mappedResults = new ArrayList(); for (DBObject dbObject : resultSet) { @@ -1652,7 +1654,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { } return executeFindOneInternal(new FindOneCallback(mappedQuery, mappedFields), new ReadDbObjectCallback( - this.mongoConverter, entityClass), collectionName); + this.mongoConverter, entityClass, collectionName), collectionName); } /** @@ -1667,7 +1669,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { */ protected List doFind(String collectionName, DBObject query, DBObject fields, Class entityClass) { return doFind(collectionName, query, fields, entityClass, null, new ReadDbObjectCallback(this.mongoConverter, - entityClass)); + entityClass, collectionName)); } /** @@ -1686,7 +1688,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { protected List doFind(String collectionName, DBObject query, DBObject fields, Class entityClass, CursorPreparer preparer) { return doFind(collectionName, query, fields, entityClass, preparer, new ReadDbObjectCallback(mongoConverter, - entityClass)); + entityClass, collectionName)); } protected List doFind(String collectionName, DBObject query, DBObject fields, Class entityClass, @@ -1742,7 +1744,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { } MongoPersistentEntity entity = mappingContext.getPersistentEntity(entityClass); return executeFindOneInternal(new FindAndRemoveCallback(queryMapper.getMappedObject(query, entity), fields, sort), - new ReadDbObjectCallback(readerToUse, entityClass), collectionName); + new ReadDbObjectCallback(readerToUse, entityClass, collectionName), collectionName); } protected T doFindAndModify(String collectionName, DBObject query, DBObject fields, DBObject sort, @@ -1768,7 +1770,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { } return executeFindOneInternal(new FindAndModifyCallback(mappedQuery, fields, sort, mappedUpdate, options), - new ReadDbObjectCallback(readerToUse, entityClass), collectionName); + new ReadDbObjectCallback(readerToUse, entityClass, collectionName), collectionName); } /** @@ -2180,26 +2182,30 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { * {@link MongoReader}. * * @author Oliver Gierke + * @author Christoph Strobl */ private class ReadDbObjectCallback implements DbObjectCallback { private final EntityReader reader; private final Class type; + private final String collectionName; + + public ReadDbObjectCallback(EntityReader reader, Class type, String collectionName) { - public ReadDbObjectCallback(EntityReader reader, Class type) { Assert.notNull(reader); Assert.notNull(type); this.reader = reader; this.type = type; + this.collectionName = collectionName; } public T doWith(DBObject object) { if (null != object) { - maybeEmitEvent(new AfterLoadEvent(object, type)); + maybeEmitEvent(new AfterLoadEvent(object, type, collectionName)); } T source = reader.read(type, object); if (null != source) { - maybeEmitEvent(new AfterConvertEvent(object, source)); + maybeEmitEvent(new AfterConvertEvent(object, source, collectionName)); } return source; } @@ -2207,8 +2213,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { class UnwrapAndReadDbObjectCallback extends ReadDbObjectCallback { - public UnwrapAndReadDbObjectCallback(EntityReader reader, Class type) { - super(reader, type); + public UnwrapAndReadDbObjectCallback(EntityReader reader, Class type, String collectionName) { + super(reader, type, collectionName); } @Override diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/event/AbstractDeleteEvent.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/event/AbstractDeleteEvent.java index 1e1dadbef..b5f41a166 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/event/AbstractDeleteEvent.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/event/AbstractDeleteEvent.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 by the original author(s). + * Copyright 2013-2015 by the original author(s). * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,6 +21,7 @@ import com.mongodb.DBObject; * Base class for delete events. * * @author Martin Baumgartner + * @author Christoph Strobl */ public abstract class AbstractDeleteEvent extends MongoMappingEvent { @@ -31,11 +32,25 @@ public abstract class AbstractDeleteEvent extends MongoMappingEvent * Creates a new {@link AbstractDeleteEvent} for the given {@link DBObject} and type. * * @param dbo must not be {@literal null}. - * @param type , possibly be {@literal null}. + * @param type can be {@literal null}. + * @deprecated since 1.8. Please use {@link #AbstractDeleteEvent(DBObject, Class, String)} */ + @Deprecated public AbstractDeleteEvent(DBObject dbo, Class type) { + this(dbo, type, null); + } - super(dbo, dbo); + /** + * Creates a new {@link AbstractDeleteEvent} for the given {@link DBObject} and type. + * + * @param dbo must not be {@literal null}. + * @param type can be {@literal null}. + * @param collectionName can be {@literal null} + * @since 1.8 + */ + public AbstractDeleteEvent(DBObject dbo, Class type, String collectionName) { + + super(dbo, dbo, collectionName); this.type = type; } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/event/AbstractMongoEventListener.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/event/AbstractMongoEventListener.java index ab1c6fa20..59e329b23 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/event/AbstractMongoEventListener.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/event/AbstractMongoEventListener.java @@ -1,5 +1,5 @@ /* - * Copyright 2011-2013 by the original author(s). + * Copyright 2011-2015 by the original author(s). * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,6 +28,7 @@ import com.mongodb.DBObject; * @author Jon Brisbin * @author Oliver Gierke * @author Martin Baumgartner + * @author Christoph Strobl */ public abstract class AbstractMongoEventListener implements ApplicationListener> { @@ -46,14 +47,14 @@ public abstract class AbstractMongoEventListener implements ApplicationListen * (non-Javadoc) * @see org.springframework.context.ApplicationListener#onApplicationEvent(org.springframework.context.ApplicationEvent) */ - @SuppressWarnings("rawtypes") + @SuppressWarnings({ "rawtypes", "unchecked" }) public void onApplicationEvent(MongoMappingEvent event) { if (event instanceof AfterLoadEvent) { AfterLoadEvent afterLoadEvent = (AfterLoadEvent) event; if (domainClass.isAssignableFrom(afterLoadEvent.getType())) { - onAfterLoad(event.getDBObject()); + onAfterLoad((AfterLoadEvent) event); } return; @@ -65,74 +66,203 @@ public abstract class AbstractMongoEventListener implements ApplicationListen if (eventDomainType != null && domainClass.isAssignableFrom(eventDomainType)) { if (event instanceof BeforeDeleteEvent) { - onBeforeDelete(event.getDBObject()); + onBeforeDelete((BeforeDeleteEvent) event); } if (event instanceof AfterDeleteEvent) { - onAfterDelete(event.getDBObject()); + onAfterDelete((AfterDeleteEvent) event); } } return; } - @SuppressWarnings("unchecked") - E source = (E) event.getSource(); - // Check for matching domain type and invoke callbacks - if (source != null && !domainClass.isAssignableFrom(source.getClass())) { + if (event.getSource() != null && !domainClass.isAssignableFrom(event.getSource().getClass())) { return; } + E source = (E) event.getSource(); + if (event instanceof BeforeConvertEvent) { - onBeforeConvert(source); + onBeforeConvert((BeforeConvertEvent) event); } else if (event instanceof BeforeSaveEvent) { - onBeforeSave(source, event.getDBObject()); + onBeforeSave((BeforeSaveEvent) event); } else if (event instanceof AfterSaveEvent) { - onAfterSave(source, event.getDBObject()); + onAfterSave((AfterSaveEvent) event); } else if (event instanceof AfterConvertEvent) { - onAfterConvert(event.getDBObject(), source); + onAfterConvert((AfterConvertEvent) event); } } + /** + * Captures source element before conversion. + * + * @param source will never be {@literal null}. + * @deprecated since 1.8. Please use {@link #onBeforeConvert(BeforeConvertEvent)}. + */ + @Deprecated public void onBeforeConvert(E source) { + if (LOG.isDebugEnabled()) { LOG.debug("onBeforeConvert({})", source); } } + /** + * Captures {@link BeforeConvertEvent}. + * + * @param event never {@literal null}. + * @since 1.8 + */ + public void onBeforeConvert(BeforeConvertEvent event) { + onBeforeConvert(event.getSource()); + } + + /** + * Captures source element and {@link com.mongodb.DBObject} representation before save. + * + * @param source will never be {@literal null}. + * @param dbo can be {@literal null}. + * @deprecated since 1.8. Please use {@link #onBeforeSave(BeforeSaveEvent)}. + */ + @Deprecated public void onBeforeSave(E source, DBObject dbo) { + if (LOG.isDebugEnabled()) { LOG.debug("onBeforeSave({}, {})", source, dbo); } } + /** + * Captures {@link BeforeSaveEvent}. + * + * @param event will never be {@literal null}. + * @since 1.8 + */ + public void onBeforeSave(BeforeSaveEvent event) { + onBeforeSave(event.getSource(), event.getDBObject()); + } + + /** + * Captures source element and {@link com.mongodb.DBObject} representation after save. + * + * @param source will never be {@literal null}. + * @param dbo can be {@literal null}. + * @deprecated since 1.8. Please use {@link #onAfterSave(AfterSaveEvent)}. + */ + @Deprecated public void onAfterSave(E source, DBObject dbo) { + if (LOG.isDebugEnabled()) { LOG.debug("onAfterSave({}, {})", source, dbo); } } + /** + * Captures {@link AfterSaveEvent}. + * + * @param event will never be {@literal null}. + * @since 1.8 + */ + public void onAfterSave(AfterSaveEvent event) { + onAfterSave(event.getSource(), event.getDBObject()); + } + + /** + * Captures raw {@link com.mongodb.DBObject} when read from MongoDB. + * + * @param dbo can be {@literal null}. + * @deprecated since 1.8. Please use {@link #onAfterLoad(AfterLoadEvent)}. + */ + @Deprecated public void onAfterLoad(DBObject dbo) { + if (LOG.isDebugEnabled()) { LOG.debug("onAfterLoad({})", dbo); } } + /** + * Captures {@link AfterLoadEvent}. + * + * @param event will never be {@literal null}. + * @since 1.8 + */ + public void onAfterLoad(AfterLoadEvent event) { + onAfterLoad(event.getDBObject()); + } + + /** + * Captures raw {@link com.mongodb.DBObject} and converted domain type after conversion. + * + * @param dbo can be {@literal null}. + * @param source will never be {@literal null}. + * @deprecated since 1.8. Please use {@link #onAfterConvert(AfterConvertEvent)}. + */ + @Deprecated public void onAfterConvert(DBObject dbo, E source) { + if (LOG.isDebugEnabled()) { LOG.debug("onAfterConvert({}, {})", dbo, source); } } + /** + * Captures {@link AfterConvertEvent}. + * + * @param event will never be {@literal null}. + * @since 1.8 + */ + public void onAfterConvert(AfterConvertEvent event) { + onAfterConvert(event.getDBObject(), event.getSource()); + + } + + /** + * Captures {@link com.mongodb.DBObject} after delete. + * + * @param dbo can be {@literal null}. + * @deprecated since 1.8. Please use {@link #onAfterDelete(AfterDeleteEvent)}. + */ + @Deprecated public void onAfterDelete(DBObject dbo) { + if (LOG.isDebugEnabled()) { LOG.debug("onAfterDelete({})", dbo); } } + /** + * Captures {@link AfterDeleteEvent}. + * + * @param event will never be {@literal null}. + * @since 1.8 + */ + public void onAfterDelete(AfterDeleteEvent event) { + onAfterDelete(event.getDBObject()); + } + + /** + * Capture {@link com.mongodb.DBObject} before delete. + * + * @param dbo can be {@literal null}. + * @deprecated since 1.8. Please use {@link #onBeforeDelete(BeforeDeleteEvent)}. + */ + @Deprecated public void onBeforeDelete(DBObject dbo) { + if (LOG.isDebugEnabled()) { LOG.debug("onBeforeDelete({})", dbo); } } + + /** + * Capture {@link BeforeDeleteEvent}. + * + * @param event will never be {@literal null}. + * @since 1.8 + */ + public void onBeforeDelete(BeforeDeleteEvent event) { + onBeforeDelete(event.getDBObject()); + } } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/event/AfterConvertEvent.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/event/AfterConvertEvent.java index 7f6a0fa26..2ac7a55d8 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/event/AfterConvertEvent.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/event/AfterConvertEvent.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011 by the original author(s). + * Copyright (c) 2011-2015 by the original author(s). * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,20 +13,42 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.springframework.data.mongodb.core.mapping.event; import com.mongodb.DBObject; /** + * {@link MongoMappingEvent} thrown after convert of a document. + * * @author Jon Brisbin + * @author Christoph Strobl */ public class AfterConvertEvent extends MongoMappingEvent { private static final long serialVersionUID = 1L; + /** + * Creates new {@link AfterConvertEvent}. + * + * @param dbo can be {@literal null}. + * @param source must not be {@literal null}. + * @deprecated since 1.8. Please use {@link #AfterConvertEvent(DBObject, Object, String)}. + */ + @Deprecated public AfterConvertEvent(DBObject dbo, E source) { - super(source, dbo); + this(dbo, source, null); + } + + /** + * Creates new {@link AfterConvertEvent}. + * + * @param dbo can be {@literal null}. + * @param source must not be {@literal null}. + * @param collectionName can be {@literal null}. + * @since 1.8 + */ + public AfterConvertEvent(DBObject dbo, E source, String collectionName) { + super(source, dbo, collectionName); } } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/event/AfterDeleteEvent.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/event/AfterDeleteEvent.java index f6495fc85..ccbcdbd43 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/event/AfterDeleteEvent.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/event/AfterDeleteEvent.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 by the original author(s). + * Copyright 2013-2015 by the original author(s). * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,6 +22,7 @@ import com.mongodb.DBObject; * will be the query document after it has been mapped onto the domain type handled. * * @author Martin Baumgartner + * @author Christoph Strobl */ public class AfterDeleteEvent extends AbstractDeleteEvent { @@ -32,8 +33,22 @@ public class AfterDeleteEvent extends AbstractDeleteEvent { * * @param dbo must not be {@literal null}. * @param type can be {@literal null}. + * @deprecated since 1.8. Please use {@link #AfterDeleteEvent(DBObject, Class, String)}. */ + @Deprecated public AfterDeleteEvent(DBObject dbo, Class type) { - super(dbo, type); + this(dbo, type, null); + } + + /** + * Creates a new {@link AfterDeleteEvent} for the given {@link DBObject}, type and collectionName. + * + * @param dbo must not be {@literal null}. + * @param type can be {@literal null}. + * @param collectionName can be {@literal null}. + * @since 1.8 + */ + public AfterDeleteEvent(DBObject dbo, Class type, String collectionName) { + super(dbo, type, collectionName); } } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/event/AfterLoadEvent.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/event/AfterLoadEvent.java index 7d23f048e..2876b243e 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/event/AfterLoadEvent.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/event/AfterLoadEvent.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011 by the original author(s). + * Copyright (c) 2011-2015 by the original author(s). * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,6 +26,7 @@ import com.mongodb.DBObject; * @author Oliver Gierke * @author Jon Brisbin * @author Christoph Leiter + * @author Christoph Strobl */ public class AfterLoadEvent extends MongoMappingEvent { @@ -36,11 +37,25 @@ public class AfterLoadEvent extends MongoMappingEvent { * Creates a new {@link AfterLoadEvent} for the given {@link DBObject} and type. * * @param dbo must not be {@literal null}. - * @param type must not be {@literal null}. + * @param type can be {@literal null}. + * @deprecated since 1.8. Please use {@link #AfterLoadEvent(DBObject, Class, String)}. */ + @Deprecated public AfterLoadEvent(DBObject dbo, Class type) { + this(dbo, type, null); + } - super(dbo, dbo); + /** + * Creates a new {@link AfterLoadEvent} for the given {@link DBObject}, type and collectionName. + * + * @param dbo must not be {@literal null}. + * @param type must not be {@literal null}. + * @param collectionName can be {@literal null}. + * @since 1.8 + */ + public AfterLoadEvent(DBObject dbo, Class type, String collectionName) { + + super(dbo, dbo, collectionName); Assert.notNull(type, "Type must not be null!"); this.type = type; diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/event/AfterSaveEvent.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/event/AfterSaveEvent.java index 18aec0974..1b892b64c 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/event/AfterSaveEvent.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/event/AfterSaveEvent.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011 by the original author(s). + * Copyright (c) 2011-2015 by the original author(s). * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,14 +19,37 @@ package org.springframework.data.mongodb.core.mapping.event; import com.mongodb.DBObject; /** + * {@link MongoMappingEvent} triggered after save of a document. + * * @author Jon Brisbin + * @author Christoph Strobl */ public class AfterSaveEvent extends MongoMappingEvent { private static final long serialVersionUID = 1L; + /** + * Creates new {@link AfterSaveEvent} + * + * @param source must not be {@literal null}. + * @param dbo can be {@literal null}. + * @deprecated since 1.8. Please use {@link #AfterSaveEvent(Object, DBObject, String)}. + */ + @Deprecated public AfterSaveEvent(E source, DBObject dbo) { super(source, dbo); } + /** + * Creates new {@link AfterSaveEvent}. + * + * @param source must not be {@literal null}. + * @param dbo can be {@literal null}. + * @param collectionName can be {@literal null}. + * @since 1.8 + */ + public AfterSaveEvent(E source, DBObject dbo, String collectionName) { + super(source, dbo, collectionName); + } + } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/event/BeforeConvertEvent.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/event/BeforeConvertEvent.java index cbd12aace..d7c60aa23 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/event/BeforeConvertEvent.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/event/BeforeConvertEvent.java @@ -1,5 +1,5 @@ /* - * Copyright 2011-2012 the original author or authors. + * Copyright 2011-2015 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. @@ -20,12 +20,31 @@ package org.springframework.data.mongodb.core.mapping.event; * * @author Jon Brisbin * @author Oliver Gierke + * @author Christoph Strobl */ public class BeforeConvertEvent extends MongoMappingEvent { private static final long serialVersionUID = 252614269008845243L; + /** + * Creates new {@link BeforeConvertEvent}. + * + * @param source must not be {@literal null}. + * @deprecated since 1.8. Please use {@link #BeforeConvertEvent(Object, String)} + */ + @Deprecated public BeforeConvertEvent(T source) { - super(source, null); + this(source, null); + } + + /** + * Creates new {@link BeforeConvertEvent}. + * + * @param source must not be {@literal null}. + * @param collectionName can be {@literal null}. + * @since 1.8 + */ + public BeforeConvertEvent(T source, String collectionName) { + super(source, null, collectionName); } } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/event/BeforeDeleteEvent.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/event/BeforeDeleteEvent.java index ce1848947..743ea48d5 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/event/BeforeDeleteEvent.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/event/BeforeDeleteEvent.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 by the original author(s). + * Copyright 2013-2015 by the original author(s). * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,6 +22,7 @@ import com.mongodb.DBObject; * document before being mapped based on the domain class handled. * * @author Martin Baumgartner + * @author Christoph Strobl */ public class BeforeDeleteEvent extends AbstractDeleteEvent { @@ -32,8 +33,22 @@ public class BeforeDeleteEvent extends AbstractDeleteEvent { * * @param dbo must not be {@literal null}. * @param type can be {@literal null}. + * @deprecated since 1.8. Please use {@link #BeforeDeleteEvent(DBObject, Class, String)}. */ + @Deprecated public BeforeDeleteEvent(DBObject dbo, Class type) { - super(dbo, type); + this(dbo, type, null); + } + + /** + * Creates a new {@link BeforeDeleteEvent} for the given {@link DBObject}, type and collectionName. + * + * @param dbo must not be {@literal null}. + * @param type can be {@literal null}. + * @param collectionName can be {@literal null} + * @since 1.8 + */ + public BeforeDeleteEvent(DBObject dbo, Class type, String collectionName) { + super(dbo, type, collectionName); } } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/event/BeforeSaveEvent.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/event/BeforeSaveEvent.java index 0a70e01df..b55b52818 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/event/BeforeSaveEvent.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/event/BeforeSaveEvent.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011 by the original author(s). + * Copyright (c) 2011-2015 by the original author(s). * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,14 +19,37 @@ package org.springframework.data.mongodb.core.mapping.event; import com.mongodb.DBObject; /** + * {@link MongoMappingEvent} triggered before save of a document. + * * @author Jon Brisbin + * @author Christoph Strobl */ public class BeforeSaveEvent extends MongoMappingEvent { private static final long serialVersionUID = 1L; + /** + * Creates new {@link BeforeSaveEvent}. + * + * @param source must not be {@literal null}. + * @param dbo can be {@literal null}. + * @deprecated since 1.8. Please use {@link #BeforeSaveEvent(Object, DBObject, String)}. + */ + @Deprecated public BeforeSaveEvent(E source, DBObject dbo) { super(source, dbo); } + /** + * Creates new {@link BeforeSaveEvent}. + * + * @param source must not be {@literal null}. + * @param dbo can be {@literal null}. + * @param collectionName can be {@literal null}. + * @since 1.8 + */ + public BeforeSaveEvent(E source, DBObject dbo, String collectionName) { + super(source, dbo, collectionName); + } + } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/event/MongoMappingEvent.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/event/MongoMappingEvent.java index 9189e932e..d286aea54 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/event/MongoMappingEvent.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/event/MongoMappingEvent.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011 by the original author(s). + * Copyright (c) 2011-2015 by the original author(s). * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,26 +16,69 @@ package org.springframework.data.mongodb.core.mapping.event; -import com.mongodb.DBObject; import org.springframework.context.ApplicationEvent; +import com.mongodb.DBObject; + /** + * Base {@link ApplicationEvent} triggered by Spring Data MongoDB. + * * @author Jon Brisbin + * @author Christoph Strobl */ public class MongoMappingEvent extends ApplicationEvent { private static final long serialVersionUID = 1L; private final DBObject dbo; + private final String collectionName; + /** + * Creates new {@link MongoMappingEvent}. + * + * @param source must not be {@literal null}. + * @param dbo can be {@literal null}. + * @deprecated since 1.8. Please use {@link #MongoMappingEvent(Object, DBObject, String)}. + */ + @Deprecated public MongoMappingEvent(T source, DBObject dbo) { - super(source); - this.dbo = dbo; + this(source, dbo, null); } + /** + * Creates new {@link MongoMappingEvent}. + * + * @param source must not be {@literal null}. + * @param dbo can be {@literal null}. + * @param collectionName can be {@literal null}. + */ + public MongoMappingEvent(T source, DBObject dbo, String collectionName) { + + super(source); + this.dbo = dbo; + this.collectionName = collectionName; + } + + /** + * @return {@literal null} if not set. + */ public DBObject getDBObject() { return dbo; } + /** + * Get the collection the event refers to. + * + * @return {@literal null} if not set. + * @since 1.8 + */ + public String getCollectionName() { + return collectionName; + } + + /* + * (non-Javadoc) + * @see java.util.EventObject#getSource() + */ @SuppressWarnings({ "unchecked" }) @Override public T getSource() { diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/AuditingIntegrationTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/AuditingIntegrationTests.java index dc1d7e60f..47cee10e5 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/AuditingIntegrationTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/AuditingIntegrationTests.java @@ -47,7 +47,7 @@ public class AuditingIntegrationTests { mappingContext.getPersistentEntity(Entity.class); Entity entity = new Entity(); - BeforeConvertEvent event = new BeforeConvertEvent(entity); + BeforeConvertEvent event = new BeforeConvertEvent(entity, "collection-1"); context.publishEvent(event); assertThat(entity.created, is(notNullValue())); @@ -55,7 +55,7 @@ public class AuditingIntegrationTests { Thread.sleep(10); entity.id = 1L; - event = new BeforeConvertEvent(entity); + event = new BeforeConvertEvent(entity, "collection-1"); context.publishEvent(event); assertThat(entity.created, is(notNullValue())); diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/UnwrapAndReadDbObjectCallbackUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/UnwrapAndReadDbObjectCallbackUnitTests.java index 4163e5c51..9e8dceb6a 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/UnwrapAndReadDbObjectCallbackUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/UnwrapAndReadDbObjectCallbackUnitTests.java @@ -50,7 +50,7 @@ public class UnwrapAndReadDbObjectCallbackUnitTests { MappingMongoConverter converter = new MappingMongoConverter(new DefaultDbRefResolver(factory), new MongoMappingContext()); - this.callback = template.new UnwrapAndReadDbObjectCallback(converter, Target.class); + this.callback = template.new UnwrapAndReadDbObjectCallback(converter, Target.class, "collection-1"); } @Test diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/AbstractMongoEventListenerUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/AbstractMongoEventListenerUnitTests.java index fcba44928..675984bc7 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/AbstractMongoEventListenerUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/AbstractMongoEventListenerUnitTests.java @@ -39,7 +39,7 @@ public class AbstractMongoEventListenerUnitTests { @Test public void invokesCallbackForEventForPerson() { - MongoMappingEvent event = new BeforeConvertEvent(new Person("Dave", "Matthews")); + MongoMappingEvent event = new BeforeConvertEvent(new Person("Dave", "Matthews"), "collection-1"); SamplePersonEventListener listener = new SamplePersonEventListener(); listener.onApplicationEvent(event); assertThat(listener.invokedOnBeforeConvert, is(true)); @@ -54,11 +54,11 @@ public class AbstractMongoEventListenerUnitTests { SamplePersonEventListener listener = new SamplePersonEventListener(); context.addApplicationListener(listener); - context.publishEvent(new BeforeConvertEvent(new Person("Dave", "Matthews"))); + context.publishEvent(new BeforeConvertEvent(new Person("Dave", "Matthews"), "collection-1")); assertThat(listener.invokedOnBeforeConvert, is(true)); listener.invokedOnBeforeConvert = false; - context.publishEvent(new BeforeConvertEvent("Test")); + context.publishEvent(new BeforeConvertEvent("Test", "collection-1")); assertThat(listener.invokedOnBeforeConvert, is(false)); context.close(); @@ -71,7 +71,7 @@ public class AbstractMongoEventListenerUnitTests { public void afterLoadEffectGetsHandledCorrectly() { SamplePersonEventListener listener = new SamplePersonEventListener(); - listener.onApplicationEvent(new AfterLoadEvent(new BasicDBObject(), Person.class)); + listener.onApplicationEvent(new AfterLoadEvent(new BasicDBObject(), Person.class, "collection-1")); assertThat(listener.invokedOnAfterLoad, is(true)); } @@ -83,8 +83,8 @@ public class AbstractMongoEventListenerUnitTests { SamplePersonEventListener personListener = new SamplePersonEventListener(); SampleAccountEventListener accountListener = new SampleAccountEventListener(); - personListener.onApplicationEvent(new AfterLoadEvent(new BasicDBObject(), Person.class)); - accountListener.onApplicationEvent(new AfterLoadEvent(new BasicDBObject(), Person.class)); + personListener.onApplicationEvent(new AfterLoadEvent(new BasicDBObject(), Person.class, "collection-1")); + accountListener.onApplicationEvent(new AfterLoadEvent(new BasicDBObject(), Person.class, "collection-1")); assertThat(personListener.invokedOnAfterLoad, is(true)); assertThat(accountListener.invokedOnAfterLoad, is(false)); @@ -98,8 +98,8 @@ public class AbstractMongoEventListenerUnitTests { SamplePersonEventListener personListener = new SamplePersonEventListener(); SampleContactEventListener contactListener = new SampleContactEventListener(); - personListener.onApplicationEvent(new AfterLoadEvent(new BasicDBObject(), Person.class)); - contactListener.onApplicationEvent(new AfterLoadEvent(new BasicDBObject(), Person.class)); + personListener.onApplicationEvent(new AfterLoadEvent(new BasicDBObject(), Person.class, "collection-1")); + contactListener.onApplicationEvent(new AfterLoadEvent(new BasicDBObject(), Person.class, "collection-1")); assertThat(personListener.invokedOnAfterLoad, is(true)); assertThat(contactListener.invokedOnAfterLoad, is(true)); @@ -113,8 +113,8 @@ public class AbstractMongoEventListenerUnitTests { SamplePersonEventListener personListener = new SamplePersonEventListener(); SampleContactEventListener contactListener = new SampleContactEventListener(); - personListener.onApplicationEvent(new AfterLoadEvent(new BasicDBObject(), Contact.class)); - contactListener.onApplicationEvent(new AfterLoadEvent(new BasicDBObject(), Contact.class)); + personListener.onApplicationEvent(new AfterLoadEvent(new BasicDBObject(), Contact.class, "collection-1")); + contactListener.onApplicationEvent(new AfterLoadEvent(new BasicDBObject(), Contact.class, "collection-1")); assertThat(personListener.invokedOnAfterLoad, is(false)); assertThat(contactListener.invokedOnAfterLoad, is(true)); @@ -137,7 +137,7 @@ public class AbstractMongoEventListenerUnitTests { @Test public void invokeContactCallbackForPersonEvent() { - MongoMappingEvent event = new BeforeDeleteEvent(new BasicDBObject(), Person.class); + MongoMappingEvent event = new BeforeDeleteEvent(new BasicDBObject(), Person.class, "collection-1"); SampleContactEventListener listener = new SampleContactEventListener(); listener.onApplicationEvent(event); @@ -150,7 +150,7 @@ public class AbstractMongoEventListenerUnitTests { @Test public void invokePersonCallbackForPersonEvent() { - MongoMappingEvent event = new BeforeDeleteEvent(new BasicDBObject(), Person.class); + MongoMappingEvent event = new BeforeDeleteEvent(new BasicDBObject(), Person.class, "collection-1"); SamplePersonEventListener listener = new SamplePersonEventListener(); listener.onApplicationEvent(event); @@ -163,7 +163,8 @@ public class AbstractMongoEventListenerUnitTests { @Test public void dontInvokePersonCallbackForAccountEvent() { - MongoMappingEvent event = new BeforeDeleteEvent(new BasicDBObject(), Account.class); + MongoMappingEvent event = new BeforeDeleteEvent(new BasicDBObject(), Account.class, + "collection-1"); SamplePersonEventListener listener = new SamplePersonEventListener(); listener.onApplicationEvent(event); @@ -176,7 +177,7 @@ public class AbstractMongoEventListenerUnitTests { @Test public void donInvokePersonCallbackForUntypedEvent() { - MongoMappingEvent event = new BeforeDeleteEvent(new BasicDBObject(), null); + MongoMappingEvent event = new BeforeDeleteEvent(new BasicDBObject(), null, "collection-1"); SamplePersonEventListener listener = new SamplePersonEventListener(); listener.onApplicationEvent(event); diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/ApplicationContextEventTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/ApplicationContextEventTests.java index a7f163e6b..2e03229a3 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/ApplicationContextEventTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/ApplicationContextEventTests.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011 by the original author(s). + * Copyright (c) 2011-2015 by the original author(s). * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,7 +15,10 @@ */ package org.springframework.data.mongodb.core.mapping.event; +import static org.hamcrest.core.Is.*; import static org.junit.Assert.*; +import static org.springframework.data.mongodb.core.query.Criteria.*; +import static org.springframework.data.mongodb.core.query.Query.*; import java.net.UnknownHostException; @@ -26,6 +29,7 @@ import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.aggregation.Aggregation; import org.springframework.data.mongodb.core.mapping.PersonPojoStringId; import com.mongodb.DB; @@ -38,10 +42,13 @@ import com.mongodb.WriteConcern; * Integration test for Mapping Events. * * @author Mark Pollack + * @author Christoph Strobl */ public class ApplicationContextEventTests { - private final String[] collectionsToDrop = new String[] { "personPojoStringId" }; + private static final String COLLECTION_NAME = "personPojoStringId"; + + private final String[] collectionsToDrop = new String[] { COLLECTION_NAME }; private ApplicationContext applicationContext; private MongoTemplate template; @@ -60,6 +67,7 @@ public class ApplicationContextEventTests { } private void cleanDb() throws UnknownHostException { + Mongo mongo = new MongoClient(); DB db = mongo.getDB("database"); for (String coll : collectionsToDrop) { @@ -90,6 +98,9 @@ public class ApplicationContextEventTests { assertEquals(1, simpleMappingEventListener.onBeforeSaveEvents.size()); assertEquals(1, simpleMappingEventListener.onAfterSaveEvents.size()); + assertEquals(COLLECTION_NAME, simpleMappingEventListener.onBeforeSaveEvents.get(0).getCollectionName()); + assertEquals(COLLECTION_NAME, simpleMappingEventListener.onAfterSaveEvents.get(0).getCollectionName()); + Assert.assertTrue(personBeforeSaveListener.seenEvents.get(0) instanceof BeforeSaveEvent); Assert.assertTrue(afterSaveListener.seenEvents.get(0) instanceof AfterSaveEvent); @@ -106,7 +117,75 @@ public class ApplicationContextEventTests { dbo = beforeSaveEvent.getDBObject(); comparePersonAndDbo(p, p2, dbo); + } + /** + * @see DATAMONGO-1256 + */ + @Test + public void loadAndConvertEvents() { + + SimpleMappingEventListener simpleMappingEventListener = applicationContext + .getBean(SimpleMappingEventListener.class); + + PersonPojoStringId entity = new PersonPojoStringId("1", "Text"); + template.insert(entity); + + template.findOne(query(where("id").is(entity.getId())), PersonPojoStringId.class); + + assertThat(simpleMappingEventListener.onAfterLoadEvents.size(), is(1)); + assertThat(simpleMappingEventListener.onAfterLoadEvents.get(0).getCollectionName(), is(COLLECTION_NAME)); + + assertThat(simpleMappingEventListener.onBeforeConvertEvents.size(), is(1)); + assertThat(simpleMappingEventListener.onBeforeConvertEvents.get(0).getCollectionName(), is(COLLECTION_NAME)); + + assertThat(simpleMappingEventListener.onAfterConvertEvents.size(), is(1)); + assertThat(simpleMappingEventListener.onAfterConvertEvents.get(0).getCollectionName(), is(COLLECTION_NAME)); + } + + /** + * @see DATAMONGO-1256 + */ + @Test + public void loadEventsOnAggregation() { + + SimpleMappingEventListener simpleMappingEventListener = applicationContext + .getBean(SimpleMappingEventListener.class); + + template.insert(new PersonPojoStringId("1", "Text")); + + template.aggregate(Aggregation.newAggregation(Aggregation.project("text")), PersonPojoStringId.class, + PersonPojoStringId.class); + + assertThat(simpleMappingEventListener.onAfterLoadEvents.size(), is(1)); + assertThat(simpleMappingEventListener.onAfterLoadEvents.get(0).getCollectionName(), is(COLLECTION_NAME)); + + assertThat(simpleMappingEventListener.onBeforeConvertEvents.size(), is(1)); + assertThat(simpleMappingEventListener.onBeforeConvertEvents.get(0).getCollectionName(), is(COLLECTION_NAME)); + + assertThat(simpleMappingEventListener.onAfterConvertEvents.size(), is(1)); + assertThat(simpleMappingEventListener.onAfterConvertEvents.get(0).getCollectionName(), is(COLLECTION_NAME)); + } + + /** + * @see DATAMONGO-1256 + */ + @Test + public void deleteEvents() { + + SimpleMappingEventListener simpleMappingEventListener = applicationContext + .getBean(SimpleMappingEventListener.class); + + PersonPojoStringId entity = new PersonPojoStringId("1", "Text"); + template.insert(entity); + + template.remove(entity); + + assertThat(simpleMappingEventListener.onBeforeDeleteEvents.size(), is(1)); + assertThat(simpleMappingEventListener.onBeforeDeleteEvents.get(0).getCollectionName(), is(COLLECTION_NAME)); + + assertThat(simpleMappingEventListener.onAfterDeleteEvents.size(), is(1)); + assertThat(simpleMappingEventListener.onAfterDeleteEvents.get(0).getCollectionName(), is(COLLECTION_NAME)); } private void comparePersonAndDbo(PersonPojoStringId p, PersonPojoStringId p2, DBObject dbo) { diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/AuditingEventListenerUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/AuditingEventListenerUnitTests.java index d9cf51925..9387a2449 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/AuditingEventListenerUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/AuditingEventListenerUnitTests.java @@ -78,7 +78,7 @@ public class AuditingEventListenerUnitTests { public void triggersCreationMarkForObjectWithEmptyId() { Sample sample = new Sample(); - listener.onApplicationEvent(new BeforeConvertEvent(sample)); + listener.onApplicationEvent(new BeforeConvertEvent(sample, "collection-1")); verify(handler, times(1)).markCreated(sample); verify(handler, times(0)).markModified(any(Sample.class)); @@ -92,7 +92,7 @@ public class AuditingEventListenerUnitTests { Sample sample = new Sample(); sample.id = "id"; - listener.onApplicationEvent(new BeforeConvertEvent(sample)); + listener.onApplicationEvent(new BeforeConvertEvent(sample, "collection-1")); verify(handler, times(0)).markCreated(any(Sample.class)); verify(handler, times(1)).markModified(sample); diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/PersonBeforeSaveListener.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/PersonBeforeSaveListener.java index beabaf860..eda34e756 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/PersonBeforeSaveListener.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/PersonBeforeSaveListener.java @@ -21,8 +21,6 @@ import java.util.List; import org.springframework.context.ApplicationEvent; import org.springframework.data.mongodb.core.mapping.PersonPojoStringId; -import com.mongodb.DBObject; - public class PersonBeforeSaveListener extends AbstractMongoEventListener { public final List seenEvents = new ArrayList(); @@ -32,7 +30,7 @@ public class PersonBeforeSaveListener extends AbstractMongoEventListener(source, dbo)); + public void onBeforeSave(BeforeSaveEvent event) { + seenEvents.add(event); } } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/SimpleMappingEventListener.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/SimpleMappingEventListener.java index 4545676ee..c73727ea2 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/SimpleMappingEventListener.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/SimpleMappingEventListener.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011 by the original author(s). + * Copyright (c) 2011-2015 by the original author(s). * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,8 +17,12 @@ package org.springframework.data.mongodb.core.mapping.event; import java.util.ArrayList; -import com.mongodb.DBObject; - +/** + * @author Mark Pollak + * @author Oliver Gierke + * @author Christoph Leiter + * @author Christoph Strobl + */ public class SimpleMappingEventListener extends AbstractMongoEventListener { public final ArrayList> onBeforeConvertEvents = new ArrayList>(); @@ -26,29 +30,41 @@ public class SimpleMappingEventListener extends AbstractMongoEventListener> onAfterSaveEvents = new ArrayList>(); public final ArrayList> onAfterLoadEvents = new ArrayList>(); public final ArrayList> onAfterConvertEvents = new ArrayList>(); + public final ArrayList> onBeforeDeleteEvents = new ArrayList>(); + public final ArrayList> onAfterDeleteEvents = new ArrayList>(); @Override - public void onBeforeConvert(Object source) { - onBeforeConvertEvents.add(new BeforeConvertEvent(source)); + public void onBeforeConvert(BeforeConvertEvent event) { + onBeforeConvertEvents.add(event); } @Override - public void onBeforeSave(Object source, DBObject dbo) { - onBeforeSaveEvents.add(new BeforeSaveEvent(source, dbo)); + public void onBeforeSave(BeforeSaveEvent event) { + onBeforeSaveEvents.add(event); } @Override - public void onAfterSave(Object source, DBObject dbo) { - onAfterSaveEvents.add(new AfterSaveEvent(source, dbo)); + public void onAfterSave(AfterSaveEvent event) { + onAfterSaveEvents.add(event); } @Override - public void onAfterLoad(DBObject dbo) { - onAfterLoadEvents.add(new AfterLoadEvent(dbo, Object.class)); + public void onAfterLoad(AfterLoadEvent event) { + onAfterLoadEvents.add(event); } @Override - public void onAfterConvert(DBObject dbo, Object source) { - onAfterConvertEvents.add(new AfterConvertEvent(dbo, source)); + public void onAfterConvert(AfterConvertEvent event) { + onAfterConvertEvents.add(event); + } + + @Override + public void onAfterDelete(AfterDeleteEvent event) { + onAfterDeleteEvents.add(event); + } + + @Override + public void onBeforeDelete(BeforeDeleteEvent event) { + onBeforeDeleteEvents.add(event); } }