Skip to content

DATAMONGO-1256 - Provide a collectionName in MongoMappingEvents. #316

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 2 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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.8.0.BUILD-SNAPSHOT</version>
<version>1.8.0.DATAMONGO-1256-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand Down
4 changes: 2 additions & 2 deletions spring-data-mongodb-cross-store/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.8.0.BUILD-SNAPSHOT</version>
<version>1.8.0.DATAMONGO-1256-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down Expand Up @@ -48,7 +48,7 @@
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.8.0.BUILD-SNAPSHOT</version>
<version>1.8.0.DATAMONGO-1256-SNAPSHOT</version>
</dependency>

<dependency>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.8.0.BUILD-SNAPSHOT</version>
<version>1.8.0.DATAMONGO-1256-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-log4j/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.8.0.BUILD-SNAPSHOT</version>
<version>1.8.0.DATAMONGO-1256-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.8.0.BUILD-SNAPSHOT</version>
<version>1.8.0.DATAMONGO-1256-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,8 @@ public CloseableIterator<T> doInCollection(DBCollection collection) throws Mongo
DBCursor cursor = collection.find(mappedQuery, mappedFields);
QueryCursorPreparer cursorPreparer = new QueryCursorPreparer(query, entityType);

ReadDbObjectCallback<T> readCallback = new ReadDbObjectCallback<T>(mongoConverter, entityType);
ReadDbObjectCallback<T> readCallback = new ReadDbObjectCallback<T>(mongoConverter, entityType, collection
.getName());

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

DbObjectCallback<GeoResult<T>> callback = new GeoNearResultDbObjectCallback<T>(new ReadDbObjectCallback<T>(
mongoConverter, entityClass), near.getMetric());
mongoConverter, entityClass, collectionName), near.getMetric());
List<GeoResult<T>> result = new ArrayList<GeoResult<T>>(results.size());

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

initializeVersionProperty(objectToSave);

maybeEmitEvent(new BeforeConvertEvent<T>(objectToSave));
maybeEmitEvent(new BeforeConvertEvent<T>(objectToSave, collectionName));

DBObject dbDoc = toDbObject(objectToSave, writer);

maybeEmitEvent(new BeforeSaveEvent<T>(objectToSave, dbDoc));
maybeEmitEvent(new BeforeSaveEvent<T>(objectToSave, dbDoc, collectionName));
Object id = insertDBObject(collectionName, dbDoc, objectToSave.getClass());

populateIdIfNecessary(objectToSave, id);
maybeEmitEvent(new AfterSaveEvent<T>(objectToSave, dbDoc));
maybeEmitEvent(new AfterSaveEvent<T>(objectToSave, dbDoc, collectionName));
}

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

maybeEmitEvent(new BeforeConvertEvent<T>(o));
maybeEmitEvent(new BeforeConvertEvent<T>(o, collectionName));
writer.write(o, dbDoc);

maybeEmitEvent(new BeforeSaveEvent<T>(o, dbDoc));
maybeEmitEvent(new BeforeSaveEvent<T>(o, dbDoc, collectionName));
dbObjectList.add(dbDoc);
}
List<ObjectId> ids = insertDBObjectList(collectionName, dbObjectList);
int i = 0;
for (T obj : batchToSave) {
if (i < ids.size()) {
populateIdIfNecessary(obj, ids.get(i));
maybeEmitEvent(new AfterSaveEvent<T>(obj, dbObjectList.get(i)));
maybeEmitEvent(new AfterSaveEvent<T>(obj, dbObjectList.get(i), collectionName));
}
i++;
}
Expand Down Expand Up @@ -951,30 +952,30 @@ private <T> void doSaveVersioned(T objectToSave, MongoPersistentEntity<?> entity

BasicDBObject dbObject = new BasicDBObject();

maybeEmitEvent(new BeforeConvertEvent<T>(objectToSave));
maybeEmitEvent(new BeforeConvertEvent<T>(objectToSave, collectionName));
this.mongoConverter.write(objectToSave, dbObject);

maybeEmitEvent(new BeforeSaveEvent<T>(objectToSave, dbObject));
maybeEmitEvent(new BeforeSaveEvent<T>(objectToSave, dbObject, collectionName));
Update update = Update.fromDBObject(dbObject, ID_FIELD);

doUpdate(collectionName, query, update, objectToSave.getClass(), false, false);
maybeEmitEvent(new AfterSaveEvent<T>(objectToSave, dbObject));
maybeEmitEvent(new AfterSaveEvent<T>(objectToSave, dbObject, collectionName));
}
}

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

assertUpdateableIdIfNotSet(objectToSave);

maybeEmitEvent(new BeforeConvertEvent<T>(objectToSave));
maybeEmitEvent(new BeforeConvertEvent<T>(objectToSave, collectionName));

DBObject dbDoc = toDbObject(objectToSave, writer);

maybeEmitEvent(new BeforeSaveEvent<T>(objectToSave, dbDoc));
maybeEmitEvent(new BeforeSaveEvent<T>(objectToSave, dbDoc, collectionName));
Object id = saveDBObject(collectionName, dbDoc, objectToSave.getClass());

populateIdIfNecessary(objectToSave, id);
maybeEmitEvent(new AfterSaveEvent<T>(objectToSave, dbDoc));
maybeEmitEvent(new AfterSaveEvent<T>(objectToSave, dbDoc, collectionName));
}

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

maybeEmitEvent(new BeforeDeleteEvent<T>(queryObject, entityClass));
maybeEmitEvent(new BeforeDeleteEvent<T>(queryObject, entityClass, collectionName));

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

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

handleAnyWriteResultErrors(wr, dboq, MongoActionOperation.REMOVE);

maybeEmitEvent(new AfterDeleteEvent<T>(queryObject, entityClass));
maybeEmitEvent(new AfterDeleteEvent<T>(queryObject, entityClass, collectionName));

return wr;
}
});
}

public <T> List<T> findAll(Class<T> entityClass) {
return executeFindMultiInternal(new FindCallback(null), null, new ReadDbObjectCallback<T>(mongoConverter,
entityClass), determineCollectionName(entityClass));
return findAll(entityClass, determineCollectionName(entityClass));
}

public <T> List<T> findAll(Class<T> entityClass, String collectionName) {
return executeFindMultiInternal(new FindCallback(null), null, new ReadDbObjectCallback<T>(mongoConverter,
entityClass), collectionName);
entityClass, collectionName), collectionName);
}

public <T> MapReduceResults<T> mapReduce(String inputCollectionName, String mapFunction, String reduceFunction,
Expand Down Expand Up @@ -1343,7 +1343,7 @@ public <T> MapReduceResults<T> mapReduce(Query query, String inputCollectionName
}

List<T> mappedResults = new ArrayList<T>();
DbObjectCallback<T> callback = new ReadDbObjectCallback<T>(mongoConverter, entityClass);
DbObjectCallback<T> callback = new ReadDbObjectCallback<T>(mongoConverter, entityClass, inputCollectionName);

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

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

return new AggregationResults<O>(returnPotentiallyMappedResults(outputType, commandResult), commandResult);
return new AggregationResults<O>(returnPotentiallyMappedResults(outputType, commandResult, collectionName),
commandResult);
}

/**
Expand All @@ -1516,15 +1517,16 @@ protected <O> AggregationResults<O> aggregate(Aggregation aggregation, String co
* @param commandResult
* @return
*/
private <O> List<O> returnPotentiallyMappedResults(Class<O> outputType, CommandResult commandResult) {
private <O> List<O> returnPotentiallyMappedResults(Class<O> outputType, CommandResult commandResult,
String collectionName) {

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

DbObjectCallback<O> callback = new UnwrapAndReadDbObjectCallback<O>(mongoConverter, outputType);
DbObjectCallback<O> callback = new UnwrapAndReadDbObjectCallback<O>(mongoConverter, outputType, collectionName);

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

return executeFindOneInternal(new FindOneCallback(mappedQuery, mappedFields), new ReadDbObjectCallback<T>(
this.mongoConverter, entityClass), collectionName);
this.mongoConverter, entityClass, collectionName), collectionName);
}

/**
Expand All @@ -1667,7 +1669,7 @@ protected <T> T doFindOne(String collectionName, DBObject query, DBObject fields
*/
protected <T> List<T> doFind(String collectionName, DBObject query, DBObject fields, Class<T> entityClass) {
return doFind(collectionName, query, fields, entityClass, null, new ReadDbObjectCallback<T>(this.mongoConverter,
entityClass));
entityClass, collectionName));
}

/**
Expand All @@ -1686,7 +1688,7 @@ protected <T> List<T> doFind(String collectionName, DBObject query, DBObject fie
protected <T> List<T> doFind(String collectionName, DBObject query, DBObject fields, Class<T> entityClass,
CursorPreparer preparer) {
return doFind(collectionName, query, fields, entityClass, preparer, new ReadDbObjectCallback<T>(mongoConverter,
entityClass));
entityClass, collectionName));
}

protected <S, T> List<T> doFind(String collectionName, DBObject query, DBObject fields, Class<S> entityClass,
Expand Down Expand Up @@ -1742,7 +1744,7 @@ protected <T> T doFindAndRemove(String collectionName, DBObject query, DBObject
}
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityClass);
return executeFindOneInternal(new FindAndRemoveCallback(queryMapper.getMappedObject(query, entity), fields, sort),
new ReadDbObjectCallback<T>(readerToUse, entityClass), collectionName);
new ReadDbObjectCallback<T>(readerToUse, entityClass, collectionName), collectionName);
}

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

return executeFindOneInternal(new FindAndModifyCallback(mappedQuery, fields, sort, mappedUpdate, options),
new ReadDbObjectCallback<T>(readerToUse, entityClass), collectionName);
new ReadDbObjectCallback<T>(readerToUse, entityClass, collectionName), collectionName);
}

/**
Expand Down Expand Up @@ -2180,35 +2182,39 @@ static interface DbObjectCallback<T> {
* {@link MongoReader}.
*
* @author Oliver Gierke
* @author Christoph Strobl
*/
private class ReadDbObjectCallback<T> implements DbObjectCallback<T> {

private final EntityReader<? super T, DBObject> reader;
private final Class<T> type;
private final String collectionName;

public ReadDbObjectCallback(EntityReader<? super T, DBObject> reader, Class<T> type, String collectionName) {

public ReadDbObjectCallback(EntityReader<? super T, DBObject> reader, Class<T> 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<T>(object, type));
maybeEmitEvent(new AfterLoadEvent<T>(object, type, collectionName));
}
T source = reader.read(type, object);
if (null != source) {
maybeEmitEvent(new AfterConvertEvent<T>(object, source));
maybeEmitEvent(new AfterConvertEvent<T>(object, source, collectionName));
}
return source;
}
}

class UnwrapAndReadDbObjectCallback<T> extends ReadDbObjectCallback<T> {

public UnwrapAndReadDbObjectCallback(EntityReader<? super T, DBObject> reader, Class<T> type) {
super(reader, type);
public UnwrapAndReadDbObjectCallback(EntityReader<? super T, DBObject> reader, Class<T> type, String collectionName) {
super(reader, type, collectionName);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -21,6 +21,7 @@
* Base class for delete events.
*
* @author Martin Baumgartner
* @author Christoph Strobl
*/
public abstract class AbstractDeleteEvent<T> extends MongoMappingEvent<DBObject> {

Expand All @@ -31,11 +32,25 @@ public abstract class AbstractDeleteEvent<T> extends MongoMappingEvent<DBObject>
* 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<T> type) {
this(dbo, type, null);
}

/**
* 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<T> type, String collectionName) {

super(dbo, dbo);
super(dbo, dbo, collectionName);
this.type = type;
}

Expand Down
Loading