Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package org.springframework.data.mongodb.core;

import static org.springframework.data.mongodb.core.query.Criteria.*;
import static org.springframework.data.mongodb.core.query.Criteria.where;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
Expand Down Expand Up @@ -101,11 +101,12 @@

/**
* Primary implementation of {@link MongoOperations}.
*
*
* @author Thomas Risberg
* @author Graeme Rocher
* @author Mark Pollack
* @author Oliver Gierke
* @author Amol Nayak
*/
public class MongoTemplate implements MongoOperations, ApplicationContextAware {

Expand Down Expand Up @@ -153,7 +154,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {

/**
* Constructor used for a basic template configuration
*
*
* @param mongo
* @param databaseName
*/
Expand All @@ -164,7 +165,7 @@ public MongoTemplate(Mongo mongo, String databaseName) {
/**
* Constructor used for a template configuration with user credentials in the form of
* {@link org.springframework.data.authentication.UserCredentials}
*
*
* @param mongo
* @param databaseName
* @param userCredentials
Expand All @@ -175,7 +176,7 @@ public MongoTemplate(Mongo mongo, String databaseName, UserCredentials userCrede

/**
* Constructor used for a basic template configuration
*
*
* @param mongoDbFactory
*/
public MongoTemplate(MongoDbFactory mongoDbFactory) {
Expand All @@ -184,7 +185,7 @@ public MongoTemplate(MongoDbFactory mongoDbFactory) {

/**
* Constructor used for a basic template configuration.
*
*
* @param mongoDbFactory
* @param mongoConverter
*/
Expand Down Expand Up @@ -212,7 +213,7 @@ public MongoTemplate(MongoDbFactory mongoDbFactory, MongoConverter mongoConverte
/**
* Configures the {@link WriteResultChecking} to be used with the template. Setting {@literal null} will reset the
* default of {@value #DEFAULT_WRITE_RESULT_CHECKING}.
*
*
* @param resultChecking
*/
public void setWriteResultChecking(WriteResultChecking resultChecking) {
Expand All @@ -221,7 +222,7 @@ public void setWriteResultChecking(WriteResultChecking resultChecking) {

/**
* Configures the {@link WriteConcern} to be used with the template.
*
*
* @param writeConcern
*/
public void setWriteConcern(WriteConcern writeConcern) {
Expand All @@ -230,7 +231,7 @@ public void setWriteConcern(WriteConcern writeConcern) {

/**
* Configures the {@link WriteConcernResolver} to be used with the template.
*
*
* @param writeConcernResolver
*/
public void setWriteConcernResolver(WriteConcernResolver writeConcernResolver) {
Expand All @@ -240,7 +241,7 @@ public void setWriteConcernResolver(WriteConcernResolver writeConcernResolver) {
/**
* Used by @{link {@link #prepareCollection(DBCollection)} to set the {@link ReadPreference} before any operations are
* performed.
*
*
* @param readPreference
*/
public void setReadPreference(ReadPreference readPreference) {
Expand All @@ -261,7 +262,7 @@ public void setApplicationContext(ApplicationContext applicationContext) throws

/**
* Returns the default {@link org.springframework.data.mongodb.core.core.convert.MongoConverter}.
*
*
* @return
*/
public MongoConverter getConverter() {
Expand Down Expand Up @@ -318,7 +319,7 @@ public void executeQuery(Query query, String collectionName, DocumentCallbackHan
/**
* Execute a MongoDB query and iterate over the query results on a per-document basis with a
* {@link DocumentCallbackHandler} using the provided CursorPreparer.
*
*
* @param query the query class that specifies the criteria used to find a record and also an optional fields
* specification, must not be {@literal null}.
* @param collectionName name of the collection to retrieve the objects from
Expand Down Expand Up @@ -598,7 +599,7 @@ protected void ensureNotIterable(Object o) {
/**
* Prepare the collection before any processing is done using it. This allows a convenient way to apply settings like
* slaveOk() etc. Can be overridden in sub-classes.
*
*
* @param collection
*/
protected void prepareCollection(DBCollection collection) {
Expand All @@ -610,7 +611,7 @@ protected void prepareCollection(DBCollection collection) {
/**
* Prepare the WriteConcern before any processing is done using it. This allows a convenient way to apply custom
* settings in sub-classes.
*
*
* @param writeConcern any WriteConcern already configured or null
* @return The prepared WriteConcern or null
*/
Expand Down Expand Up @@ -730,11 +731,13 @@ public Object doInCollection(DBCollection collection) throws MongoException, Dat
MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.INSERT, collectionName,
entityClass, dbDoc, null);
WriteConcern writeConcernToUse = prepareWriteConcern(mongoAction);
WriteResult wr;
if (writeConcernToUse == null) {
collection.insert(dbDoc);
wr = collection.insert(dbDoc);
} else {
collection.insert(dbDoc, writeConcernToUse);
wr = collection.insert(dbDoc, writeConcernToUse);
}
handleAnyWriteResultErrors(wr, dbDoc, "insert");
return dbDoc.get(ID);
}
});
Expand All @@ -753,11 +756,13 @@ public Void doInCollection(DBCollection collection) throws MongoException, DataA
MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.INSERT_LIST, collectionName, null,
null, null);
WriteConcern writeConcernToUse = prepareWriteConcern(mongoAction);
WriteResult wr;
if (writeConcernToUse == null) {
collection.insert(dbDocList);
wr = collection.insert(dbDocList);
} else {
collection.insert(dbDocList.toArray((DBObject[]) new BasicDBObject[dbDocList.size()]), writeConcernToUse);
wr = collection.insert(dbDocList.toArray((DBObject[]) new BasicDBObject[dbDocList.size()]), writeConcernToUse);
}
handleAnyWriteResultErrors(wr, null, "insert_list");
return null;
}
});
Expand All @@ -784,11 +789,13 @@ public Object doInCollection(DBCollection collection) throws MongoException, Dat
MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.SAVE, collectionName, entityClass,
dbDoc, null);
WriteConcern writeConcernToUse = prepareWriteConcern(mongoAction);
WriteResult wr;
if (writeConcernToUse == null) {
collection.save(dbDoc);
wr = collection.save(dbDoc);
} else {
collection.save(dbDoc, writeConcernToUse);
wr = collection.save(dbDoc, writeConcernToUse);
}
handleAnyWriteResultErrors(wr, dbDoc, "save");
return dbDoc.get(ID);
}
});
Expand Down Expand Up @@ -873,7 +880,7 @@ public void remove(Object object, String collection) {

/**
* Returns a {@link Query} for the given entity by its id.
*
*
* @param object must not be {@literal null}.
* @return
*/
Expand Down Expand Up @@ -1182,7 +1189,7 @@ protected <T> void maybeEmitEvent(MongoMappingEvent<T> event) {

/**
* Create the specified collection using the provided options
*
*
* @param collectionName
* @param collectionOptions
* @return the collection that was created
Expand All @@ -1204,7 +1211,7 @@ public DBCollection doInDB(DB db) throws MongoException, DataAccessException {
* Map the results of an ad-hoc query on the default MongoDB collection to an object using the template's converter
* <p/>
* The query document is specified as a standard DBObject and so is the fields specification.
*
*
* @param collectionName name of the collection to retrieve the objects from
* @param query the query document that specifies the criteria used to find a record
* @param fields the document that specifies the fields to be returned
Expand All @@ -1229,7 +1236,7 @@ protected <T> T doFindOne(String collectionName, DBObject query, DBObject fields
* The query document is specified as a standard DBObject and so is the fields specification.
* <p/>
* Can be overridden by subclasses.
*
*
* @param collectionName name of the collection to retrieve the objects from
* @param query the query document that specifies the criteria used to find a record
* @param fields the document that specifies the fields to be returned
Expand Down Expand Up @@ -1259,7 +1266,7 @@ protected <S, T> List<T> doFind(String collectionName, DBObject query, DBObject
* Map the results of an ad-hoc query on the default MongoDB collection to a List using the template's converter.
* <p/>
* The query document is specified as a standard DBObject and so is the fields specification.
*
*
* @param collectionName name of the collection to retrieve the objects from
* @param query the query document that specifies the criteria used to find a record
* @param fields the document that specifies the fields to be returned
Expand Down Expand Up @@ -1298,7 +1305,7 @@ protected DBObject convertToDbObject(CollectionOptions collectionOptions) {
* The first document that matches the query is returned and also removed from the collection in the database.
* <p/>
* The query document is specified as a standard DBObject and so is the fields specification.
*
*
* @param collectionName name of the collection to retrieve the objects from
* @param query the query document that specifies the criteria used to find a record
* @param entityClass the parameterized type of the returned list.
Expand Down Expand Up @@ -1345,7 +1352,7 @@ protected <T> T doFindAndModify(String collectionName, DBObject query, DBObject

/**
* Populates the id property of the saved object, if it's not set already.
*
*
* @param savedObject
* @param id
*/
Expand Down Expand Up @@ -1398,7 +1405,7 @@ private DBCollection getAndPrepareCollection(DB db, String collectionName) {
* <li>Execute the given {@link ConnectionCallback} for a {@link DBObject}.</li>
* <li>Apply the given {@link DbObjectCallback} to each of the {@link DBObject}s to obtain the result.</li>
* <ol>
*
*
* @param <T>
* @param collectionCallback the callback to retrieve the {@link DBObject} with
* @param objectCallback the {@link DbObjectCallback} to transform {@link DBObject}s into the actual domain type
Expand Down Expand Up @@ -1427,7 +1434,7 @@ private <T> T executeFindOneInternal(CollectionCallback<DBObject> collectionCall
* <li>Iterate over the {@link DBCursor} and applies the given {@link DbObjectCallback} to each of the
* {@link DBObject}s collecting the actual result {@link List}.</li>
* <ol>
*
*
* @param <T>
* @param collectionCallback the callback to retrieve the {@link DBCursor} with
* @param preparer the {@link CursorPreparer} to potentially modify the {@link DBCursor} before ireating over it
Expand Down Expand Up @@ -1521,9 +1528,19 @@ protected void handleAnyWriteResultErrors(WriteResult wr, DBObject query, String
String error = wr.getError();

if (error != null) {

String message = String.format("Execution of %s%s failed: %s", operation, query == null ? "" : "' using '"
String message;
if(operation.equals("insert") || operation.equals("save")) {
//assuming the insert operations will begin with insert string
message = String.format("Insert/Save for %s failed: %s", query,error);
}
else if(operation.equals("insert_list")) {
message = String.format("Insert list failed: %s", error);
}
else {
message = String.format("Execution of %s%s failed: %s", operation, query == null ? "" : "' using '"
+ query.toString() + "' query", error);
}


if (WriteResultChecking.EXCEPTION == this.writeResultChecking) {
throw new DataIntegrityViolationException(message);
Expand All @@ -1537,7 +1554,7 @@ protected void handleAnyWriteResultErrors(WriteResult wr, DBObject query, String
/**
* Tries to convert the given {@link RuntimeException} into a {@link DataAccessException} but returns the original
* exception if the conversation failed. Thus allows safe rethrowing of the return value.
*
*
* @param ex
* @return
*/
Expand All @@ -1557,7 +1574,7 @@ private static final MongoConverter getDefaultMongoConverter(MongoDbFactory fact
/**
* Simple {@link CollectionCallback} that takes a query {@link DBObject} plus an optional fields specification
* {@link DBObject} and executes that against the {@link DBCollection}.
*
*
* @author Oliver Gierke
* @author Thomas Risberg
*/
Expand Down Expand Up @@ -1590,7 +1607,7 @@ public DBObject doInCollection(DBCollection collection) throws MongoException, D
/**
* Simple {@link CollectionCallback} that takes a query {@link DBObject} plus an optional fields specification
* {@link DBObject} and executes that against the {@link DBCollection}.
*
*
* @author Oliver Gierke
* @author Thomas Risberg
*/
Expand Down Expand Up @@ -1621,7 +1638,7 @@ public DBCursor doInCollection(DBCollection collection) throws MongoException, D
/**
* Simple {@link CollectionCallback} that takes a query {@link DBObject} plus an optional fields specification
* {@link DBObject} and executes that against the {@link DBCollection}.
*
*
* @author Thomas Risberg
*/
private static class FindAndRemoveCallback implements CollectionCallback<DBObject> {
Expand Down Expand Up @@ -1666,7 +1683,7 @@ public DBObject doInCollection(DBCollection collection) throws MongoException, D

/**
* Simple internal callback to allow operations on a {@link DBObject}.
*
*
* @author Oliver Gierke
*/

Expand All @@ -1678,7 +1695,7 @@ private interface DbObjectCallback<T> {
/**
* Simple {@link DbObjectCallback} that will transform {@link DBObject} into the given target type using the given
* {@link MongoReader}.
*
*
* @author Oliver Gierke
*/
private class ReadDbObjectCallback<T> implements DbObjectCallback<T> {
Expand Down Expand Up @@ -1762,7 +1779,7 @@ public DBCursor prepare(DBCursor cursor) {
/**
* {@link DbObjectCallback} that assumes a {@link GeoResult} to be created, delegates actual content unmarshalling to
* a delegate and creates a {@link GeoResult} from the result.
*
*
* @author Oliver Gierke
*/
static class GeoNearResultDbObjectCallback<T> implements DbObjectCallback<GeoResult<T>> {
Expand All @@ -1773,7 +1790,7 @@ static class GeoNearResultDbObjectCallback<T> implements DbObjectCallback<GeoRes
/**
* Creates a new {@link GeoNearResultDbObjectCallback} using the given {@link DbObjectCallback} delegate for
* {@link GeoResult} content unmarshalling.
*
*
* @param delegate
*/
public GeoNearResultDbObjectCallback(DbObjectCallback<T> delegate, Metric metric) {
Expand Down
Loading