@@ -338,7 +338,8 @@ public CloseableIterator<T> doInCollection(DBCollection collection) throws Mongo
338
338
DBCursor cursor = collection .find (mappedQuery , mappedFields );
339
339
QueryCursorPreparer cursorPreparer = new QueryCursorPreparer (query , entityType );
340
340
341
- ReadDbObjectCallback <T > readCallback = new ReadDbObjectCallback <T >(mongoConverter , entityType );
341
+ ReadDbObjectCallback <T > readCallback = new ReadDbObjectCallback <T >(mongoConverter , entityType , collection
342
+ .getName ());
342
343
343
344
return new CloseableIterableCusorAdapter <T >(cursorPreparer .prepare (cursor ), exceptionTranslator , readCallback );
344
345
}
@@ -637,7 +638,7 @@ public <T> GeoResults<T> geoNear(NearQuery near, Class<T> entityClass, String co
637
638
results = results == null ? Collections .emptyList () : results ;
638
639
639
640
DbObjectCallback <GeoResult <T >> callback = new GeoNearResultDbObjectCallback <T >(new ReadDbObjectCallback <T >(
640
- mongoConverter , entityClass ), near .getMetric ());
641
+ mongoConverter , entityClass , collectionName ), near .getMetric ());
641
642
List <GeoResult <T >> result = new ArrayList <GeoResult <T >>(results .size ());
642
643
643
644
int index = 0 ;
@@ -789,15 +790,15 @@ protected <T> void doInsert(String collectionName, T objectToSave, MongoWriter<T
789
790
790
791
initializeVersionProperty (objectToSave );
791
792
792
- maybeEmitEvent (new BeforeConvertEvent <T >(objectToSave ));
793
+ maybeEmitEvent (new BeforeConvertEvent <T >(objectToSave , collectionName ));
793
794
794
795
DBObject dbDoc = toDbObject (objectToSave , writer );
795
796
796
- maybeEmitEvent (new BeforeSaveEvent <T >(objectToSave , dbDoc ));
797
+ maybeEmitEvent (new BeforeSaveEvent <T >(objectToSave , dbDoc , collectionName ));
797
798
Object id = insertDBObject (collectionName , dbDoc , objectToSave .getClass ());
798
799
799
800
populateIdIfNecessary (objectToSave , id );
800
- maybeEmitEvent (new AfterSaveEvent <T >(objectToSave , dbDoc ));
801
+ maybeEmitEvent (new AfterSaveEvent <T >(objectToSave , dbDoc , collectionName ));
801
802
}
802
803
803
804
/**
@@ -885,18 +886,18 @@ protected <T> void doInsertBatch(String collectionName, Collection<? extends T>
885
886
initializeVersionProperty (o );
886
887
BasicDBObject dbDoc = new BasicDBObject ();
887
888
888
- maybeEmitEvent (new BeforeConvertEvent <T >(o ));
889
+ maybeEmitEvent (new BeforeConvertEvent <T >(o , collectionName ));
889
890
writer .write (o , dbDoc );
890
891
891
- maybeEmitEvent (new BeforeSaveEvent <T >(o , dbDoc ));
892
+ maybeEmitEvent (new BeforeSaveEvent <T >(o , dbDoc , collectionName ));
892
893
dbObjectList .add (dbDoc );
893
894
}
894
895
List <ObjectId > ids = insertDBObjectList (collectionName , dbObjectList );
895
896
int i = 0 ;
896
897
for (T obj : batchToSave ) {
897
898
if (i < ids .size ()) {
898
899
populateIdIfNecessary (obj , ids .get (i ));
899
- maybeEmitEvent (new AfterSaveEvent <T >(obj , dbObjectList .get (i )));
900
+ maybeEmitEvent (new AfterSaveEvent <T >(obj , dbObjectList .get (i ), collectionName ));
900
901
}
901
902
i ++;
902
903
}
@@ -951,30 +952,30 @@ private <T> void doSaveVersioned(T objectToSave, MongoPersistentEntity<?> entity
951
952
952
953
BasicDBObject dbObject = new BasicDBObject ();
953
954
954
- maybeEmitEvent (new BeforeConvertEvent <T >(objectToSave ));
955
+ maybeEmitEvent (new BeforeConvertEvent <T >(objectToSave , collectionName ));
955
956
this .mongoConverter .write (objectToSave , dbObject );
956
957
957
- maybeEmitEvent (new BeforeSaveEvent <T >(objectToSave , dbObject ));
958
+ maybeEmitEvent (new BeforeSaveEvent <T >(objectToSave , dbObject , collectionName ));
958
959
Update update = Update .fromDBObject (dbObject , ID_FIELD );
959
960
960
961
doUpdate (collectionName , query , update , objectToSave .getClass (), false , false );
961
- maybeEmitEvent (new AfterSaveEvent <T >(objectToSave , dbObject ));
962
+ maybeEmitEvent (new AfterSaveEvent <T >(objectToSave , dbObject , collectionName ));
962
963
}
963
964
}
964
965
965
966
protected <T > void doSave (String collectionName , T objectToSave , MongoWriter <T > writer ) {
966
967
967
968
assertUpdateableIdIfNotSet (objectToSave );
968
969
969
- maybeEmitEvent (new BeforeConvertEvent <T >(objectToSave ));
970
+ maybeEmitEvent (new BeforeConvertEvent <T >(objectToSave , collectionName ));
970
971
971
972
DBObject dbDoc = toDbObject (objectToSave , writer );
972
973
973
- maybeEmitEvent (new BeforeSaveEvent <T >(objectToSave , dbDoc ));
974
+ maybeEmitEvent (new BeforeSaveEvent <T >(objectToSave , dbDoc , collectionName ));
974
975
Object id = saveDBObject (collectionName , dbDoc , objectToSave .getClass ());
975
976
976
977
populateIdIfNecessary (objectToSave , id );
977
- maybeEmitEvent (new AfterSaveEvent <T >(objectToSave , dbDoc ));
978
+ maybeEmitEvent (new AfterSaveEvent <T >(objectToSave , dbDoc , collectionName ));
978
979
}
979
980
980
981
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
1266
1267
return execute (collectionName , new CollectionCallback <WriteResult >() {
1267
1268
public WriteResult doInCollection (DBCollection collection ) throws MongoException , DataAccessException {
1268
1269
1269
- maybeEmitEvent (new BeforeDeleteEvent <T >(queryObject , entityClass ));
1270
+ maybeEmitEvent (new BeforeDeleteEvent <T >(queryObject , entityClass , collectionName ));
1270
1271
1271
1272
DBObject dboq = queryMapper .getMappedObject (queryObject , entity );
1272
1273
@@ -1284,21 +1285,20 @@ public WriteResult doInCollection(DBCollection collection) throws MongoException
1284
1285
1285
1286
handleAnyWriteResultErrors (wr , dboq , MongoActionOperation .REMOVE );
1286
1287
1287
- maybeEmitEvent (new AfterDeleteEvent <T >(queryObject , entityClass ));
1288
+ maybeEmitEvent (new AfterDeleteEvent <T >(queryObject , entityClass , collectionName ));
1288
1289
1289
1290
return wr ;
1290
1291
}
1291
1292
});
1292
1293
}
1293
1294
1294
1295
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 ));
1297
1297
}
1298
1298
1299
1299
public <T > List <T > findAll (Class <T > entityClass , String collectionName ) {
1300
1300
return executeFindMultiInternal (new FindCallback (null ), null , new ReadDbObjectCallback <T >(mongoConverter ,
1301
- entityClass ), collectionName );
1301
+ entityClass , collectionName ), collectionName );
1302
1302
}
1303
1303
1304
1304
public <T > MapReduceResults <T > mapReduce (String inputCollectionName , String mapFunction , String reduceFunction ,
@@ -1343,7 +1343,7 @@ public <T> MapReduceResults<T> mapReduce(Query query, String inputCollectionName
1343
1343
}
1344
1344
1345
1345
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 );
1347
1347
1348
1348
for (DBObject dbObject : mapReduceOutput .results ()) {
1349
1349
mappedResults .add (callback .doWith (dbObject ));
@@ -1404,7 +1404,7 @@ public <T> GroupByResults<T> group(Criteria criteria, String inputCollectionName
1404
1404
@ SuppressWarnings ("unchecked" )
1405
1405
Iterable <DBObject > resultSet = (Iterable <DBObject >) commandResult .get ("retval" );
1406
1406
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 );
1408
1408
1409
1409
for (DBObject dbObject : resultSet ) {
1410
1410
mappedResults .add (callback .doWith (dbObject ));
@@ -1506,7 +1506,8 @@ protected <O> AggregationResults<O> aggregate(Aggregation aggregation, String co
1506
1506
CommandResult commandResult = executeCommand (command , this .readPreference );
1507
1507
handleCommandError (commandResult , command );
1508
1508
1509
- return new AggregationResults <O >(returnPotentiallyMappedResults (outputType , commandResult ), commandResult );
1509
+ return new AggregationResults <O >(returnPotentiallyMappedResults (outputType , commandResult , collectionName ),
1510
+ commandResult );
1510
1511
}
1511
1512
1512
1513
/**
@@ -1516,15 +1517,16 @@ protected <O> AggregationResults<O> aggregate(Aggregation aggregation, String co
1516
1517
* @param commandResult
1517
1518
* @return
1518
1519
*/
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 ) {
1520
1522
1521
1523
@ SuppressWarnings ("unchecked" )
1522
1524
Iterable <DBObject > resultSet = (Iterable <DBObject >) commandResult .get ("result" );
1523
1525
if (resultSet == null ) {
1524
1526
return Collections .emptyList ();
1525
1527
}
1526
1528
1527
- DbObjectCallback <O > callback = new UnwrapAndReadDbObjectCallback <O >(mongoConverter , outputType );
1529
+ DbObjectCallback <O > callback = new UnwrapAndReadDbObjectCallback <O >(mongoConverter , outputType , collectionName );
1528
1530
1529
1531
List <O > mappedResults = new ArrayList <O >();
1530
1532
for (DBObject dbObject : resultSet ) {
@@ -1652,7 +1654,7 @@ protected <T> T doFindOne(String collectionName, DBObject query, DBObject fields
1652
1654
}
1653
1655
1654
1656
return executeFindOneInternal (new FindOneCallback (mappedQuery , mappedFields ), new ReadDbObjectCallback <T >(
1655
- this .mongoConverter , entityClass ), collectionName );
1657
+ this .mongoConverter , entityClass , collectionName ), collectionName );
1656
1658
}
1657
1659
1658
1660
/**
@@ -1667,7 +1669,7 @@ protected <T> T doFindOne(String collectionName, DBObject query, DBObject fields
1667
1669
*/
1668
1670
protected <T > List <T > doFind (String collectionName , DBObject query , DBObject fields , Class <T > entityClass ) {
1669
1671
return doFind (collectionName , query , fields , entityClass , null , new ReadDbObjectCallback <T >(this .mongoConverter ,
1670
- entityClass ));
1672
+ entityClass , collectionName ));
1671
1673
}
1672
1674
1673
1675
/**
@@ -1686,7 +1688,7 @@ protected <T> List<T> doFind(String collectionName, DBObject query, DBObject fie
1686
1688
protected <T > List <T > doFind (String collectionName , DBObject query , DBObject fields , Class <T > entityClass ,
1687
1689
CursorPreparer preparer ) {
1688
1690
return doFind (collectionName , query , fields , entityClass , preparer , new ReadDbObjectCallback <T >(mongoConverter ,
1689
- entityClass ));
1691
+ entityClass , collectionName ));
1690
1692
}
1691
1693
1692
1694
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
1742
1744
}
1743
1745
MongoPersistentEntity <?> entity = mappingContext .getPersistentEntity (entityClass );
1744
1746
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 );
1746
1748
}
1747
1749
1748
1750
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
1768
1770
}
1769
1771
1770
1772
return executeFindOneInternal (new FindAndModifyCallback (mappedQuery , fields , sort , mappedUpdate , options ),
1771
- new ReadDbObjectCallback <T >(readerToUse , entityClass ), collectionName );
1773
+ new ReadDbObjectCallback <T >(readerToUse , entityClass , collectionName ), collectionName );
1772
1774
}
1773
1775
1774
1776
/**
@@ -2180,35 +2182,39 @@ static interface DbObjectCallback<T> {
2180
2182
* {@link MongoReader}.
2181
2183
*
2182
2184
* @author Oliver Gierke
2185
+ * @author Christoph Strobl
2183
2186
*/
2184
2187
private class ReadDbObjectCallback <T > implements DbObjectCallback <T > {
2185
2188
2186
2189
private final EntityReader <? super T , DBObject > reader ;
2187
2190
private final Class <T > type ;
2191
+ private final String collectionName ;
2192
+
2193
+ public ReadDbObjectCallback (EntityReader <? super T , DBObject > reader , Class <T > type , String collectionName ) {
2188
2194
2189
- public ReadDbObjectCallback (EntityReader <? super T , DBObject > reader , Class <T > type ) {
2190
2195
Assert .notNull (reader );
2191
2196
Assert .notNull (type );
2192
2197
this .reader = reader ;
2193
2198
this .type = type ;
2199
+ this .collectionName = collectionName ;
2194
2200
}
2195
2201
2196
2202
public T doWith (DBObject object ) {
2197
2203
if (null != object ) {
2198
- maybeEmitEvent (new AfterLoadEvent <T >(object , type ));
2204
+ maybeEmitEvent (new AfterLoadEvent <T >(object , type , collectionName ));
2199
2205
}
2200
2206
T source = reader .read (type , object );
2201
2207
if (null != source ) {
2202
- maybeEmitEvent (new AfterConvertEvent <T >(object , source ));
2208
+ maybeEmitEvent (new AfterConvertEvent <T >(object , source , collectionName ));
2203
2209
}
2204
2210
return source ;
2205
2211
}
2206
2212
}
2207
2213
2208
2214
class UnwrapAndReadDbObjectCallback <T > extends ReadDbObjectCallback <T > {
2209
2215
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 );
2212
2218
}
2213
2219
2214
2220
@ Override
0 commit comments