Skip to content

Commit 3e485e0

Browse files
christophstroblodrotbohm
authored andcommitted
DATAMONGO-1256 - MongoMappingEvents now expose the collection name they're issued for.
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.
1 parent 335c78f commit 3e485e0

18 files changed

+524
-104
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,8 @@ public CloseableIterator<T> doInCollection(DBCollection collection) throws Mongo
338338
DBCursor cursor = collection.find(mappedQuery, mappedFields);
339339
QueryCursorPreparer cursorPreparer = new QueryCursorPreparer(query, entityType);
340340

341-
ReadDbObjectCallback<T> readCallback = new ReadDbObjectCallback<T>(mongoConverter, entityType);
341+
ReadDbObjectCallback<T> readCallback = new ReadDbObjectCallback<T>(mongoConverter, entityType, collection
342+
.getName());
342343

343344
return new CloseableIterableCusorAdapter<T>(cursorPreparer.prepare(cursor), exceptionTranslator, readCallback);
344345
}
@@ -637,7 +638,7 @@ public <T> GeoResults<T> geoNear(NearQuery near, Class<T> entityClass, String co
637638
results = results == null ? Collections.emptyList() : results;
638639

639640
DbObjectCallback<GeoResult<T>> callback = new GeoNearResultDbObjectCallback<T>(new ReadDbObjectCallback<T>(
640-
mongoConverter, entityClass), near.getMetric());
641+
mongoConverter, entityClass, collectionName), near.getMetric());
641642
List<GeoResult<T>> result = new ArrayList<GeoResult<T>>(results.size());
642643

643644
int index = 0;
@@ -789,15 +790,15 @@ protected <T> void doInsert(String collectionName, T objectToSave, MongoWriter<T
789790

790791
initializeVersionProperty(objectToSave);
791792

792-
maybeEmitEvent(new BeforeConvertEvent<T>(objectToSave));
793+
maybeEmitEvent(new BeforeConvertEvent<T>(objectToSave, collectionName));
793794

794795
DBObject dbDoc = toDbObject(objectToSave, writer);
795796

796-
maybeEmitEvent(new BeforeSaveEvent<T>(objectToSave, dbDoc));
797+
maybeEmitEvent(new BeforeSaveEvent<T>(objectToSave, dbDoc, collectionName));
797798
Object id = insertDBObject(collectionName, dbDoc, objectToSave.getClass());
798799

799800
populateIdIfNecessary(objectToSave, id);
800-
maybeEmitEvent(new AfterSaveEvent<T>(objectToSave, dbDoc));
801+
maybeEmitEvent(new AfterSaveEvent<T>(objectToSave, dbDoc, collectionName));
801802
}
802803

803804
/**
@@ -885,18 +886,18 @@ protected <T> void doInsertBatch(String collectionName, Collection<? extends T>
885886
initializeVersionProperty(o);
886887
BasicDBObject dbDoc = new BasicDBObject();
887888

888-
maybeEmitEvent(new BeforeConvertEvent<T>(o));
889+
maybeEmitEvent(new BeforeConvertEvent<T>(o, collectionName));
889890
writer.write(o, dbDoc);
890891

891-
maybeEmitEvent(new BeforeSaveEvent<T>(o, dbDoc));
892+
maybeEmitEvent(new BeforeSaveEvent<T>(o, dbDoc, collectionName));
892893
dbObjectList.add(dbDoc);
893894
}
894895
List<ObjectId> ids = insertDBObjectList(collectionName, dbObjectList);
895896
int i = 0;
896897
for (T obj : batchToSave) {
897898
if (i < ids.size()) {
898899
populateIdIfNecessary(obj, ids.get(i));
899-
maybeEmitEvent(new AfterSaveEvent<T>(obj, dbObjectList.get(i)));
900+
maybeEmitEvent(new AfterSaveEvent<T>(obj, dbObjectList.get(i), collectionName));
900901
}
901902
i++;
902903
}
@@ -951,30 +952,30 @@ private <T> void doSaveVersioned(T objectToSave, MongoPersistentEntity<?> entity
951952

952953
BasicDBObject dbObject = new BasicDBObject();
953954

954-
maybeEmitEvent(new BeforeConvertEvent<T>(objectToSave));
955+
maybeEmitEvent(new BeforeConvertEvent<T>(objectToSave, collectionName));
955956
this.mongoConverter.write(objectToSave, dbObject);
956957

957-
maybeEmitEvent(new BeforeSaveEvent<T>(objectToSave, dbObject));
958+
maybeEmitEvent(new BeforeSaveEvent<T>(objectToSave, dbObject, collectionName));
958959
Update update = Update.fromDBObject(dbObject, ID_FIELD);
959960

960961
doUpdate(collectionName, query, update, objectToSave.getClass(), false, false);
961-
maybeEmitEvent(new AfterSaveEvent<T>(objectToSave, dbObject));
962+
maybeEmitEvent(new AfterSaveEvent<T>(objectToSave, dbObject, collectionName));
962963
}
963964
}
964965

965966
protected <T> void doSave(String collectionName, T objectToSave, MongoWriter<T> writer) {
966967

967968
assertUpdateableIdIfNotSet(objectToSave);
968969

969-
maybeEmitEvent(new BeforeConvertEvent<T>(objectToSave));
970+
maybeEmitEvent(new BeforeConvertEvent<T>(objectToSave, collectionName));
970971

971972
DBObject dbDoc = toDbObject(objectToSave, writer);
972973

973-
maybeEmitEvent(new BeforeSaveEvent<T>(objectToSave, dbDoc));
974+
maybeEmitEvent(new BeforeSaveEvent<T>(objectToSave, dbDoc, collectionName));
974975
Object id = saveDBObject(collectionName, dbDoc, objectToSave.getClass());
975976

976977
populateIdIfNecessary(objectToSave, id);
977-
maybeEmitEvent(new AfterSaveEvent<T>(objectToSave, dbDoc));
978+
maybeEmitEvent(new AfterSaveEvent<T>(objectToSave, dbDoc, collectionName));
978979
}
979980

980981
protected Object insertDBObject(final String collectionName, final DBObject dbDoc, final Class<?> entityClass) {
@@ -1266,7 +1267,7 @@ protected <T> WriteResult doRemove(final String collectionName, final Query quer
12661267
return execute(collectionName, new CollectionCallback<WriteResult>() {
12671268
public WriteResult doInCollection(DBCollection collection) throws MongoException, DataAccessException {
12681269

1269-
maybeEmitEvent(new BeforeDeleteEvent<T>(queryObject, entityClass));
1270+
maybeEmitEvent(new BeforeDeleteEvent<T>(queryObject, entityClass, collectionName));
12701271

12711272
DBObject dboq = queryMapper.getMappedObject(queryObject, entity);
12721273

@@ -1284,21 +1285,20 @@ public WriteResult doInCollection(DBCollection collection) throws MongoException
12841285

12851286
handleAnyWriteResultErrors(wr, dboq, MongoActionOperation.REMOVE);
12861287

1287-
maybeEmitEvent(new AfterDeleteEvent<T>(queryObject, entityClass));
1288+
maybeEmitEvent(new AfterDeleteEvent<T>(queryObject, entityClass, collectionName));
12881289

12891290
return wr;
12901291
}
12911292
});
12921293
}
12931294

12941295
public <T> List<T> findAll(Class<T> entityClass) {
1295-
return executeFindMultiInternal(new FindCallback(null), null, new ReadDbObjectCallback<T>(mongoConverter,
1296-
entityClass), determineCollectionName(entityClass));
1296+
return findAll(entityClass, determineCollectionName(entityClass));
12971297
}
12981298

12991299
public <T> List<T> findAll(Class<T> entityClass, String collectionName) {
13001300
return executeFindMultiInternal(new FindCallback(null), null, new ReadDbObjectCallback<T>(mongoConverter,
1301-
entityClass), collectionName);
1301+
entityClass, collectionName), collectionName);
13021302
}
13031303

13041304
public <T> MapReduceResults<T> mapReduce(String inputCollectionName, String mapFunction, String reduceFunction,
@@ -1343,7 +1343,7 @@ public <T> MapReduceResults<T> mapReduce(Query query, String inputCollectionName
13431343
}
13441344

13451345
List<T> mappedResults = new ArrayList<T>();
1346-
DbObjectCallback<T> callback = new ReadDbObjectCallback<T>(mongoConverter, entityClass);
1346+
DbObjectCallback<T> callback = new ReadDbObjectCallback<T>(mongoConverter, entityClass, inputCollectionName);
13471347

13481348
for (DBObject dbObject : mapReduceOutput.results()) {
13491349
mappedResults.add(callback.doWith(dbObject));
@@ -1404,7 +1404,7 @@ public <T> GroupByResults<T> group(Criteria criteria, String inputCollectionName
14041404
@SuppressWarnings("unchecked")
14051405
Iterable<DBObject> resultSet = (Iterable<DBObject>) commandResult.get("retval");
14061406
List<T> mappedResults = new ArrayList<T>();
1407-
DbObjectCallback<T> callback = new ReadDbObjectCallback<T>(mongoConverter, entityClass);
1407+
DbObjectCallback<T> callback = new ReadDbObjectCallback<T>(mongoConverter, entityClass, inputCollectionName);
14081408

14091409
for (DBObject dbObject : resultSet) {
14101410
mappedResults.add(callback.doWith(dbObject));
@@ -1506,7 +1506,8 @@ protected <O> AggregationResults<O> aggregate(Aggregation aggregation, String co
15061506
CommandResult commandResult = executeCommand(command, this.readPreference);
15071507
handleCommandError(commandResult, command);
15081508

1509-
return new AggregationResults<O>(returnPotentiallyMappedResults(outputType, commandResult), commandResult);
1509+
return new AggregationResults<O>(returnPotentiallyMappedResults(outputType, commandResult, collectionName),
1510+
commandResult);
15101511
}
15111512

15121513
/**
@@ -1516,15 +1517,16 @@ protected <O> AggregationResults<O> aggregate(Aggregation aggregation, String co
15161517
* @param commandResult
15171518
* @return
15181519
*/
1519-
private <O> List<O> returnPotentiallyMappedResults(Class<O> outputType, CommandResult commandResult) {
1520+
private <O> List<O> returnPotentiallyMappedResults(Class<O> outputType, CommandResult commandResult,
1521+
String collectionName) {
15201522

15211523
@SuppressWarnings("unchecked")
15221524
Iterable<DBObject> resultSet = (Iterable<DBObject>) commandResult.get("result");
15231525
if (resultSet == null) {
15241526
return Collections.emptyList();
15251527
}
15261528

1527-
DbObjectCallback<O> callback = new UnwrapAndReadDbObjectCallback<O>(mongoConverter, outputType);
1529+
DbObjectCallback<O> callback = new UnwrapAndReadDbObjectCallback<O>(mongoConverter, outputType, collectionName);
15281530

15291531
List<O> mappedResults = new ArrayList<O>();
15301532
for (DBObject dbObject : resultSet) {
@@ -1652,7 +1654,7 @@ protected <T> T doFindOne(String collectionName, DBObject query, DBObject fields
16521654
}
16531655

16541656
return executeFindOneInternal(new FindOneCallback(mappedQuery, mappedFields), new ReadDbObjectCallback<T>(
1655-
this.mongoConverter, entityClass), collectionName);
1657+
this.mongoConverter, entityClass, collectionName), collectionName);
16561658
}
16571659

16581660
/**
@@ -1667,7 +1669,7 @@ protected <T> T doFindOne(String collectionName, DBObject query, DBObject fields
16671669
*/
16681670
protected <T> List<T> doFind(String collectionName, DBObject query, DBObject fields, Class<T> entityClass) {
16691671
return doFind(collectionName, query, fields, entityClass, null, new ReadDbObjectCallback<T>(this.mongoConverter,
1670-
entityClass));
1672+
entityClass, collectionName));
16711673
}
16721674

16731675
/**
@@ -1686,7 +1688,7 @@ protected <T> List<T> doFind(String collectionName, DBObject query, DBObject fie
16861688
protected <T> List<T> doFind(String collectionName, DBObject query, DBObject fields, Class<T> entityClass,
16871689
CursorPreparer preparer) {
16881690
return doFind(collectionName, query, fields, entityClass, preparer, new ReadDbObjectCallback<T>(mongoConverter,
1689-
entityClass));
1691+
entityClass, collectionName));
16901692
}
16911693

16921694
protected <S, T> List<T> doFind(String collectionName, DBObject query, DBObject fields, Class<S> entityClass,
@@ -1742,7 +1744,7 @@ protected <T> T doFindAndRemove(String collectionName, DBObject query, DBObject
17421744
}
17431745
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityClass);
17441746
return executeFindOneInternal(new FindAndRemoveCallback(queryMapper.getMappedObject(query, entity), fields, sort),
1745-
new ReadDbObjectCallback<T>(readerToUse, entityClass), collectionName);
1747+
new ReadDbObjectCallback<T>(readerToUse, entityClass, collectionName), collectionName);
17461748
}
17471749

17481750
protected <T> T doFindAndModify(String collectionName, DBObject query, DBObject fields, DBObject sort,
@@ -1768,7 +1770,7 @@ protected <T> T doFindAndModify(String collectionName, DBObject query, DBObject
17681770
}
17691771

17701772
return executeFindOneInternal(new FindAndModifyCallback(mappedQuery, fields, sort, mappedUpdate, options),
1771-
new ReadDbObjectCallback<T>(readerToUse, entityClass), collectionName);
1773+
new ReadDbObjectCallback<T>(readerToUse, entityClass, collectionName), collectionName);
17721774
}
17731775

17741776
/**
@@ -2180,35 +2182,39 @@ static interface DbObjectCallback<T> {
21802182
* {@link MongoReader}.
21812183
*
21822184
* @author Oliver Gierke
2185+
* @author Christoph Strobl
21832186
*/
21842187
private class ReadDbObjectCallback<T> implements DbObjectCallback<T> {
21852188

21862189
private final EntityReader<? super T, DBObject> reader;
21872190
private final Class<T> type;
2191+
private final String collectionName;
2192+
2193+
public ReadDbObjectCallback(EntityReader<? super T, DBObject> reader, Class<T> type, String collectionName) {
21882194

2189-
public ReadDbObjectCallback(EntityReader<? super T, DBObject> reader, Class<T> type) {
21902195
Assert.notNull(reader);
21912196
Assert.notNull(type);
21922197
this.reader = reader;
21932198
this.type = type;
2199+
this.collectionName = collectionName;
21942200
}
21952201

21962202
public T doWith(DBObject object) {
21972203
if (null != object) {
2198-
maybeEmitEvent(new AfterLoadEvent<T>(object, type));
2204+
maybeEmitEvent(new AfterLoadEvent<T>(object, type, collectionName));
21992205
}
22002206
T source = reader.read(type, object);
22012207
if (null != source) {
2202-
maybeEmitEvent(new AfterConvertEvent<T>(object, source));
2208+
maybeEmitEvent(new AfterConvertEvent<T>(object, source, collectionName));
22032209
}
22042210
return source;
22052211
}
22062212
}
22072213

22082214
class UnwrapAndReadDbObjectCallback<T> extends ReadDbObjectCallback<T> {
22092215

2210-
public UnwrapAndReadDbObjectCallback(EntityReader<? super T, DBObject> reader, Class<T> type) {
2211-
super(reader, type);
2216+
public UnwrapAndReadDbObjectCallback(EntityReader<? super T, DBObject> reader, Class<T> type, String collectionName) {
2217+
super(reader, type, collectionName);
22122218
}
22132219

22142220
@Override

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/event/AbstractDeleteEvent.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013 by the original author(s).
2+
* Copyright 2013-2015 by the original author(s).
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@
2121
* Base class for delete events.
2222
*
2323
* @author Martin Baumgartner
24+
* @author Christoph Strobl
2425
*/
2526
public abstract class AbstractDeleteEvent<T> extends MongoMappingEvent<DBObject> {
2627

@@ -31,11 +32,25 @@ public abstract class AbstractDeleteEvent<T> extends MongoMappingEvent<DBObject>
3132
* Creates a new {@link AbstractDeleteEvent} for the given {@link DBObject} and type.
3233
*
3334
* @param dbo must not be {@literal null}.
34-
* @param type , possibly be {@literal null}.
35+
* @param type can be {@literal null}.
36+
* @deprecated since 1.8. Please use {@link #AbstractDeleteEvent(DBObject, Class, String)}
3537
*/
38+
@Deprecated
3639
public AbstractDeleteEvent(DBObject dbo, Class<T> type) {
40+
this(dbo, type, null);
41+
}
42+
43+
/**
44+
* Creates a new {@link AbstractDeleteEvent} for the given {@link DBObject} and type.
45+
*
46+
* @param dbo must not be {@literal null}.
47+
* @param type can be {@literal null}.
48+
* @param collectionName can be {@literal null}
49+
* @since 1.8
50+
*/
51+
public AbstractDeleteEvent(DBObject dbo, Class<T> type, String collectionName) {
3752

38-
super(dbo, dbo);
53+
super(dbo, dbo, collectionName);
3954
this.type = type;
4055
}
4156

0 commit comments

Comments
 (0)