@ -25,12 +25,10 @@ import java.util.ArrayList;
import java.util.Arrays ;
import java.util.Arrays ;
import java.util.Collections ;
import java.util.Collections ;
import java.util.HashMap ;
import java.util.HashMap ;
import java.util.Iterator ;
import java.util.List ;
import java.util.List ;
import java.util.Map ;
import java.util.Map ;
import java.util.Set ;
import java.util.Set ;
import com.mongodb.BasicDBList ;
import com.mongodb.BasicDBObject ;
import com.mongodb.BasicDBObject ;
import com.mongodb.CommandResult ;
import com.mongodb.CommandResult ;
import com.mongodb.DB ;
import com.mongodb.DB ;
@ -45,7 +43,6 @@ import com.mongodb.util.JSON;
import org.apache.commons.logging.Log ;
import org.apache.commons.logging.Log ;
import org.apache.commons.logging.LogFactory ;
import org.apache.commons.logging.LogFactory ;
import org.bson.types.ObjectId ;
import org.bson.types.ObjectId ;
import org.springframework.beans.factory.InitializingBean ;
import org.springframework.context.ApplicationEventPublisher ;
import org.springframework.context.ApplicationEventPublisher ;
import org.springframework.context.ApplicationEventPublisherAware ;
import org.springframework.context.ApplicationEventPublisherAware ;
import org.springframework.core.convert.ConversionFailedException ;
import org.springframework.core.convert.ConversionFailedException ;
@ -83,7 +80,7 @@ import org.springframework.util.Assert;
* @author Mark Pollack
* @author Mark Pollack
* @author Oliver Gierke
* @author Oliver Gierke
* /
* /
public class MongoTemplate implements InitializingBean , MongoOperations , ApplicationEventPublisherAware {
public class MongoTemplate implements MongoOperations , ApplicationEventPublisherAware {
private static final Log LOGGER = LogFactory . getLog ( MongoTemplate . class ) ;
private static final Log LOGGER = LogFactory . getLog ( MongoTemplate . class ) ;
@ -107,7 +104,6 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
private final MongoExceptionTranslator exceptionTranslator = new MongoExceptionTranslator ( ) ;
private final MongoExceptionTranslator exceptionTranslator = new MongoExceptionTranslator ( ) ;
private final QueryMapper mapper ;
private final QueryMapper mapper ;
private String defaultCollectionName ;
private String databaseName ;
private String databaseName ;
private String username ;
private String username ;
private String password ;
private String password ;
@ -120,34 +116,22 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
* @param databaseName
* @param databaseName
* /
* /
public MongoTemplate ( Mongo mongo , String databaseName ) {
public MongoTemplate ( Mongo mongo , String databaseName ) {
this ( mongo , databaseName , null , null , null , null ) ;
this ( mongo , databaseName , null , null , null ) ;
}
}
/ * *
/ * *
* Constructor used for a basic template configuration with a default collection name
* Constructor used for a template configuration with a custom { @link org . springframework . data . document . mongodb . convert . MongoConverter }
*
*
* @param mongo
* @param mongo
* @param databaseName
* @param databaseName
* @param defaultCollectionName
* /
public MongoTemplate ( Mongo mongo , String databaseName , String defaultCollectionName ) {
this ( mongo , databaseName , defaultCollectionName , null , null , null ) ;
}
/ * *
* Constructor used for a template configuration with a default collection name and a custom { @link org . springframework . data . document . mongodb . convert . MongoConverter }
*
* @param mongo
* @param databaseName
* @param defaultCollectionName
* @param mongoConverter
* @param mongoConverter
* /
* /
public MongoTemplate ( Mongo mongo , String databaseName , String defaultCollectionName , MongoConverter mongoConverter ) {
public MongoTemplate ( Mongo mongo , String databaseName , MongoConverter mongoConverter ) {
this ( mongo , databaseName , defaultCollectionName , mongoConverter , null , null ) ;
this ( mongo , databaseName , mongoConverter , null , null ) ;
}
}
/ * *
/ * *
* Constructor used for a template configuration with a default collection name and a custom { @link MongoConverter }
* Constructor used for a template configuration with a custom { @link MongoConverter }
* and with a specific { @link com . mongodb . WriteConcern } to be used for all database write operations
* and with a specific { @link com . mongodb . WriteConcern } to be used for all database write operations
*
*
* @param mongo
* @param mongo
@ -158,12 +142,11 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
* @param writeResultChecking
* @param writeResultChecking
* /
* /
@SuppressWarnings ( { "unchecked" } )
@SuppressWarnings ( { "unchecked" } )
MongoTemplate ( Mongo mongo , String databaseName , String defaultCollectionName , MongoConverter mongoConverter , WriteConcern writeConcern , WriteResultChecking writeResultChecking ) {
MongoTemplate ( Mongo mongo , String databaseName , MongoConverter mongoConverter , WriteConcern writeConcern , WriteResultChecking writeResultChecking ) {
Assert . notNull ( mongo ) ;
Assert . notNull ( mongo ) ;
Assert . notNull ( databaseName ) ;
Assert . notNull ( databaseName ) ;
this . defaultCollectionName = defaultCollectionName ;
this . mongo = mongo ;
this . mongo = mongo ;
this . databaseName = databaseName ;
this . databaseName = databaseName ;
this . writeConcern = writeConcern ;
this . writeConcern = writeConcern ;
@ -211,15 +194,6 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
this . password = password ;
this . password = password ;
}
}
/ * *
* Sets the name of the default collection to be used .
*
* @param defaultCollectionName
* /
public void setDefaultCollectionName ( String defaultCollectionName ) {
this . defaultCollectionName = defaultCollectionName ;
}
/ * *
/ * *
* Sets the database name to be used .
* Sets the database name to be used .
*
*
@ -242,20 +216,8 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
/ * ( non - Javadoc )
/ * ( non - Javadoc )
* @see org . springframework . data . document . mongodb . MongoOperations # getDefaultCollectionName ( )
* @see org . springframework . data . document . mongodb . MongoOperations # getDefaultCollectionName ( )
* /
* /
public String getDefaultCollectionName ( ) {
public String getCollectionName ( Class < ? > clazz ) {
return defaultCollectionName ;
return this . determineCollectionName ( clazz ) ;
}
/ * ( non - Javadoc )
* @see org . springframework . data . document . mongodb . MongoOperations # getDefaultCollection ( )
* /
public DBCollection getDefaultCollection ( ) {
return execute ( new DbCallback < DBCollection > ( ) {
public DBCollection doInDB ( DB db ) throws MongoException , DataAccessException {
return db . getCollection ( getDefaultCollectionName ( ) ) ;
}
} ) ;
}
}
/ * ( non - Javadoc )
/ * ( non - Javadoc )
@ -305,8 +267,8 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
/ * ( non - Javadoc )
/ * ( non - Javadoc )
* @see org . springframework . data . document . mongodb . MongoOperations # execute ( org . springframework . data . document . mongodb . CollectionCallback )
* @see org . springframework . data . document . mongodb . MongoOperations # execute ( org . springframework . data . document . mongodb . CollectionCallback )
* /
* /
public < T > T execute ( CollectionCallback < T > callback ) {
public < T > T execute ( Class < ? > entityClass , C ollectionCallback < T > callback ) {
return execute ( getDefaultCollectionName ( ) , callback ) ;
return execute ( determineCollectionName ( entityClass ) , callback ) ;
}
}
/ * ( non - Javadoc )
/ * ( non - Javadoc )
@ -452,8 +414,8 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
// Indexing methods
// Indexing methods
public void ensureIndex ( IndexDefinition indexDefinition ) {
public void ensureIndex ( Class < ? > entityClass , IndexDefinition indexDefinition ) {
ensureIndex ( getDefaultCollectionName ( ) , indexDefinition ) ;
ensureIndex ( determineCollectionName ( entityClass ) , indexDefinition ) ;
}
}
public void ensureIndex ( String collectionName , final IndexDefinition indexDefinition ) {
public void ensureIndex ( String collectionName , final IndexDefinition indexDefinition ) {
@ -473,12 +435,12 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
// Find methods that take a Query to express the query and that return a single object.
// Find methods that take a Query to express the query and that return a single object.
public < T > T findOne ( Query query , Class < T > targetClass ) {
public < T > T findOne ( Query query , Class < T > targetClass ) {
return findOne ( getEntityCollection ( targetClass ) , query , targetClass ) ;
return findOne ( determineCollectionName ( targetClass ) , query , targetClass ) ;
}
}
public < T > T findOne ( Query query , Class < T > targetClass ,
public < T > T findOne ( Query query , Class < T > targetClass ,
MongoReader < T > reader ) {
MongoReader < T > reader ) {
return findOne ( getEntityCollection ( targetClass ) , query , targetClass , reader ) ;
return findOne ( determineCollectionName ( targetClass ) , query , targetClass , reader ) ;
}
}
public < T > T findOne ( String collectionName , Query query ,
public < T > T findOne ( String collectionName , Query query ,
@ -494,11 +456,11 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
// Find methods that take a Query to express the query and that return a List of objects.
// Find methods that take a Query to express the query and that return a List of objects.
public < T > List < T > find ( Query query , Class < T > targetClass ) {
public < T > List < T > find ( Query query , Class < T > targetClass ) {
return find ( getEntityCollection ( targetClass ) , query , targetClass ) ;
return find ( determineCollectionName ( targetClass ) , query , targetClass ) ;
}
}
public < T > List < T > find ( Query query , Class < T > targetClass , MongoReader < T > reader ) {
public < T > List < T > find ( Query query , Class < T > targetClass , MongoReader < T > reader ) {
return find ( getEntityCollection ( targetClass ) , query , targetClass , reader ) ;
return find ( determineCollectionName ( targetClass ) , query , targetClass , reader ) ;
}
}
public < T > List < T > find ( String collectionName , final Query query , Class < T > targetClass ) {
public < T > List < T > find ( String collectionName , final Query query , Class < T > targetClass ) {
@ -541,12 +503,12 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
// also removed from the collection in the database.
// also removed from the collection in the database.
public < T > T findAndRemove ( Query query , Class < T > targetClass ) {
public < T > T findAndRemove ( Query query , Class < T > targetClass ) {
return findAndRemove ( getEntityCollection ( targetClass ) , query , targetClass ) ;
return findAndRemove ( determineCollectionName ( targetClass ) , query , targetClass ) ;
}
}
public < T > T findAndRemove ( Query query , Class < T > targetClass ,
public < T > T findAndRemove ( Query query , Class < T > targetClass ,
MongoReader < T > reader ) {
MongoReader < T > reader ) {
return findAndRemove ( getEntityCollection ( targetClass ) , query , targetClass , reader ) ;
return findAndRemove ( determineCollectionName ( targetClass ) , query , targetClass , reader ) ;
}
}
public < T > T findAndRemove ( String collectionName , Query query ,
public < T > T findAndRemove ( String collectionName , Query query ,
@ -563,7 +525,7 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
* @see org . springframework . data . document . mongodb . MongoOperations # insert ( java . lang . Object )
* @see org . springframework . data . document . mongodb . MongoOperations # insert ( java . lang . Object )
* /
* /
public void insert ( Object objectToSave ) {
public void insert ( Object objectToSave ) {
insert ( getEntityCollection ( objectToSave ) , objectToSave ) ;
insert ( determineEntityCollectionName ( objectToSave ) , objectToSave ) ;
}
}
/ * ( non - Javadoc )
/ * ( non - Javadoc )
@ -577,7 +539,7 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
* @see org . springframework . data . document . mongodb . MongoOperations # insert ( T , org . springframework . data . document . mongodb . MongoWriter )
* @see org . springframework . data . document . mongodb . MongoOperations # insert ( T , org . springframework . data . document . mongodb . MongoWriter )
* /
* /
public < T > void insert ( T objectToSave , MongoWriter < T > writer ) {
public < T > void insert ( T objectToSave , MongoWriter < T > writer ) {
insert ( getEntityCollection ( objectToSave ) , objectToSave , writer ) ;
insert ( determineEntityCollectionName ( objectToSave ) , objectToSave , writer ) ;
}
}
/ * ( non - Javadoc )
/ * ( non - Javadoc )
@ -619,7 +581,11 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
for ( Object o : listToSave ) {
for ( Object o : listToSave ) {
MongoPersistentEntity < ? > entity = mappingContext . getPersistentEntity ( o . getClass ( ) ) ;
MongoPersistentEntity < ? > entity = mappingContext . getPersistentEntity ( o . getClass ( ) ) ;
String collection = entity = = null ? getDefaultCollectionName ( ) : entity . getCollection ( ) ;
if ( entity = = null ) {
throw new InvalidDataAccessApiUsageException ( "No Persitent Entity information found for the class " +
o . getClass ( ) . getName ( ) ) ;
}
String collection = entity . getCollection ( ) ;
List < Object > objList = objs . get ( collection ) ;
List < Object > objList = objs . get ( collection ) ;
if ( null = = objList ) {
if ( null = = objList ) {
@ -666,7 +632,7 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
* @see org . springframework . data . document . mongodb . MongoOperations # save ( java . lang . Object )
* @see org . springframework . data . document . mongodb . MongoOperations # save ( java . lang . Object )
* /
* /
public void save ( Object objectToSave ) {
public void save ( Object objectToSave ) {
save ( getEntityCollection ( objectToSave ) , objectToSave ) ;
save ( determineEntityCollectionName ( objectToSave ) , objectToSave ) ;
}
}
/ * ( non - Javadoc )
/ * ( non - Javadoc )
@ -680,7 +646,7 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
* @see org . springframework . data . document . mongodb . MongoOperations # save ( T , org . springframework . data . document . mongodb . MongoWriter )
* @see org . springframework . data . document . mongodb . MongoOperations # save ( T , org . springframework . data . document . mongodb . MongoWriter )
* /
* /
public < T > void save ( T objectToSave , MongoWriter < T > writer ) {
public < T > void save ( T objectToSave , MongoWriter < T > writer ) {
save ( getEntityCollection ( objectToSave ) , objectToSave , writer ) ;
save ( determineEntityCollectionName ( objectToSave ) , objectToSave , writer ) ;
}
}
/ * ( non - Javadoc )
/ * ( non - Javadoc )
@ -808,8 +774,8 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
/ * ( non - Javadoc )
/ * ( non - Javadoc )
* @see org . springframework . data . document . mongodb . MongoOperations # updateFirst ( com . mongodb . DBObject , com . mongodb . DBObject )
* @see org . springframework . data . document . mongodb . MongoOperations # updateFirst ( com . mongodb . DBObject , com . mongodb . DBObject )
* /
* /
public WriteResult updateFirst ( Query query , Update update ) {
public WriteResult updateFirst ( Class < ? > entityClass , Query query , Update update ) {
return updateFirst ( getRequiredDefaultCollectionName ( ) , query , update ) ;
return updateFirst ( determineCollectionName ( entityClass ) , query , update ) ;
}
}
/ * ( non - Javadoc )
/ * ( non - Javadoc )
@ -838,8 +804,8 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
/ * ( non - Javadoc )
/ * ( non - Javadoc )
* @see org . springframework . data . document . mongodb . MongoOperations # updateMulti ( com . mongodb . DBObject , com . mongodb . DBObject )
* @see org . springframework . data . document . mongodb . MongoOperations # updateMulti ( com . mongodb . DBObject , com . mongodb . DBObject )
* /
* /
public WriteResult updateMulti ( Query query , Update update ) {
public WriteResult updateMulti ( Class < ? > entityClass , Query query , Update update ) {
return updateMulti ( getRequiredDefaultCollectionName ( ) , query , update ) ;
return updateMulti ( determineCollectionName ( entityClass ) , query , update ) ;
}
}
/ * ( non - Javadoc )
/ * ( non - Javadoc )
@ -878,7 +844,7 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
public < T > void remove ( Query query , Class < T > targetClass ) {
public < T > void remove ( Query query , Class < T > targetClass ) {
Assert . notNull ( query ) ;
Assert . notNull ( query ) ;
remove ( getEntityCollection ( targetClass ) , query , targetClass ) ;
remove ( determineCollectionName ( targetClass ) , query , targetClass ) ;
}
}
private PersistentEntity < ? > getPersistentEntity ( Class < ? > type ) {
private PersistentEntity < ? > getPersistentEntity ( Class < ? > type ) {
@ -922,7 +888,7 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
* /
* /
public < T > List < T > getCollection ( Class < T > targetClass ) {
public < T > List < T > getCollection ( Class < T > targetClass ) {
return executeEach ( new FindCallback ( null ) , null , new ReadDbObjectCallback < T > ( mongoConverter , targetClass ) ,
return executeEach ( new FindCallback ( null ) , null , new ReadDbObjectCallback < T > ( mongoConverter , targetClass ) ,
getDefaultCollectionName ( ) ) ;
determineCollectionName ( targetClass ) ) ;
}
}
public < T > List < T > getCollection ( String collectionName , Class < T > targetClass ) {
public < T > List < T > getCollection ( String collectionName , Class < T > targetClass ) {
@ -1233,30 +1199,24 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
return newValue ;
return newValue ;
}
}
private String getRequiredDefaultCollectionName ( ) {
private < T > String determineEntityCollectionName ( T obj ) {
String name = getDefaultCollectionName ( ) ;
if ( name = = null ) {
throw new IllegalStateException (
"No 'defaultCollection' or 'defaultCollectionName' specified. Check configuration of MongoTemplate." ) ;
}
return name ;
}
private < T > String getEntityCollection ( T obj ) {
if ( null ! = obj ) {
if ( null ! = obj ) {
return getEntityCollection ( obj . getClass ( ) ) ;
return determineCollectionName ( obj . getClass ( ) ) ;
}
}
return null ;
return null ;
}
}
private String getEntityCollection ( Class < ? > clazz ) {
private String determineCollectionName ( Class < ? > clazz ) {
if ( clazz = = null ) {
if ( clazz = = null ) {
return getDefaultCollectionName ( ) ;
throw new InvalidDataAccessApiUsageException ( "No class parameter provided, entity collection can't be determined for " + clazz ) ;
}
}
MongoPersistentEntity < ? > entity = mappingContext . getPersistentEntity ( clazz ) ;
MongoPersistentEntity < ? > entity = mappingContext . getPersistentEntity ( clazz ) ;
if ( entity = = null ) {
throw new InvalidDataAccessApiUsageException ( "No Persitent Entity information found for the class " + clazz . getName ( ) ) ;
}
return entity . getCollection ( ) ;
return entity . getCollection ( ) ;
}
}
@ -1309,18 +1269,6 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
converter . setDefaultDatabase ( databaseName ) ;
converter . setDefaultDatabase ( databaseName ) ;
}
}
/ *
* ( non - Javadoc )
* @see org . springframework . beans . factory . InitializingBean # afterPropertiesSet ( )
* /
public void afterPropertiesSet ( ) {
if ( this . getDefaultCollectionName ( ) ! = null ) {
if ( ! collectionExists ( getDefaultCollectionName ( ) ) ) {
createCollection ( getDefaultCollectionName ( ) , null ) ;
}
}
}
/ * *
/ * *
* Simple { @link CollectionCallback } that takes a query { @link DBObject } plus an optional fields specification
* Simple { @link CollectionCallback } that takes a query { @link DBObject } plus an optional fields specification