diff --git a/spring-data-mongodb-cross-store/src/main/java/org/springframework/data/mongodb/crossstore/MongoChangeSetPersister.java b/spring-data-mongodb-cross-store/src/main/java/org/springframework/data/mongodb/crossstore/MongoChangeSetPersister.java index 2333ee91a..3bb4ce601 100644 --- a/spring-data-mongodb-cross-store/src/main/java/org/springframework/data/mongodb/crossstore/MongoChangeSetPersister.java +++ b/spring-data-mongodb-cross-store/src/main/java/org/springframework/data/mongodb/crossstore/MongoChangeSetPersister.java @@ -1,5 +1,5 @@ /* - * Copyright 2011-2013 the original author or authors. + * Copyright 2011-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -41,17 +41,13 @@ import com.mongodb.MongoException; public class MongoChangeSetPersister implements ChangeSetPersister { private static final String ENTITY_CLASS = "_entity_class"; - private static final String ENTITY_ID = "_entity_id"; - private static final String ENTITY_FIELD_NAME = "_entity_field_name"; - private static final String ENTITY_FIELD_CLASS = "_entity_field_class"; protected final Logger log = LoggerFactory.getLogger(getClass()); private MongoTemplate mongoTemplate; - private EntityManagerFactory entityManagerFactory; public void setMongoTemplate(MongoTemplate mongoTemplate) { @@ -113,12 +109,14 @@ public class MongoChangeSetPersister implements ChangeSetPersister { * @see org.springframework.data.crossstore.ChangeSetPersister#getPersistentId(org.springframework.data.crossstore.ChangeSetBacked, org.springframework.data.crossstore.ChangeSet) */ public Object getPersistentId(ChangeSetBacked entity, ChangeSet cs) throws DataAccessException { + log.debug("getPersistentId called on " + entity); + if (entityManagerFactory == null) { throw new DataAccessResourceFailureException("EntityManagerFactory cannot be null"); } - Object o = entityManagerFactory.getPersistenceUnitUtil().getIdentifier(entity); - return o; + + return entityManagerFactory.getPersistenceUnitUtil().getIdentifier(entity); } /* diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MappingMongoConverterParser.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MappingMongoConverterParser.java index 438c2f175..46491f6a3 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MappingMongoConverterParser.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MappingMongoConverterParser.java @@ -1,5 +1,5 @@ /* - * Copyright 2011-2013 the original author or authors. + * Copyright 2011-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -75,7 +75,7 @@ import org.w3c.dom.Element; public class MappingMongoConverterParser implements BeanDefinitionParser { private static final String BASE_PACKAGE = "base-package"; - private static final boolean jsr303Present = ClassUtils.isPresent("javax.validation.Validator", + private static final boolean JSR_303_PRESENT = ClassUtils.isPresent("javax.validation.Validator", MappingMongoConverterParser.class.getClassLoader()); /* (non-Javadoc) @@ -166,7 +166,7 @@ public class MappingMongoConverterParser implements BeanDefinitionParser { private RuntimeBeanReference getValidator(Object source, ParserContext parserContext) { - if (!jsr303Present) { + if (!JSR_303_PRESENT) { return null; } @@ -195,7 +195,8 @@ public class MappingMongoConverterParser implements BeanDefinitionParser { BeanDefinitionBuilder mappingContextBuilder = BeanDefinitionBuilder .genericBeanDefinition(MongoMappingContext.class); - Set classesToAdd = getInititalEntityClasses(element, mappingContextBuilder); + Set classesToAdd = getInititalEntityClasses(element); + if (classesToAdd != null) { mappingContextBuilder.addPropertyValue("initialEntitySet", classesToAdd); } @@ -262,7 +263,7 @@ public class MappingMongoConverterParser implements BeanDefinitionParser { return null; } - private static Set getInititalEntityClasses(Element element, BeanDefinitionBuilder builder) { + private static Set getInititalEntityClasses(Element element) { String basePackage = element.getAttribute(BASE_PACKAGE); diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoAdminOperations.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoAdminOperations.java index da18a4b21..293ef14c5 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoAdminOperations.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoAdminOperations.java @@ -1,16 +1,34 @@ +/* + * Copyright 2011-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.springframework.data.mongodb.core; import org.springframework.jmx.export.annotation.ManagedOperation; +/** + * @author Mark Pollack + * @author Oliver Gierke + */ public interface MongoAdminOperations { @ManagedOperation - public abstract void dropDatabase(String databaseName); + void dropDatabase(String databaseName); @ManagedOperation - public abstract void createDatabase(String databaseName); + void createDatabase(String databaseName); @ManagedOperation - public abstract String getDatabaseStats(String databaseName); - -} \ No newline at end of file + String getDatabaseStats(String databaseName); +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOptionsFactoryBean.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOptionsFactoryBean.java index feea11b35..63a107574 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOptionsFactoryBean.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOptionsFactoryBean.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2013 the original author or authors. + * Copyright 2010-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -30,27 +30,28 @@ import com.mongodb.MongoOptions; * @author Mike Saavedra * @author Thomas Darimont */ +@SuppressWarnings("deprecation") public class MongoOptionsFactoryBean implements FactoryBean, InitializingBean { - @SuppressWarnings("deprecation")// - private final MongoOptions MONGO_OPTIONS = new MongoOptions(); - - private int connectionsPerHost = MONGO_OPTIONS.connectionsPerHost; - - private int threadsAllowedToBlockForConnectionMultiplier = MONGO_OPTIONS.threadsAllowedToBlockForConnectionMultiplier; - private int maxWaitTime = MONGO_OPTIONS.maxWaitTime; - private int connectTimeout = MONGO_OPTIONS.connectTimeout; - private int socketTimeout = MONGO_OPTIONS.socketTimeout; - private boolean socketKeepAlive = MONGO_OPTIONS.socketKeepAlive; - private boolean autoConnectRetry = MONGO_OPTIONS.autoConnectRetry; - private long maxAutoConnectRetryTime = MONGO_OPTIONS.maxAutoConnectRetryTime; - private int writeNumber; - private int writeTimeout; - private boolean writeFsync; - @SuppressWarnings("deprecation") private boolean slaveOk = MONGO_OPTIONS.slaveOk; + private static final MongoOptions DEFAULT_MONGO_OPTIONS = new MongoOptions(); + + private int connectionsPerHost = DEFAULT_MONGO_OPTIONS.connectionsPerHost; + private int threadsAllowedToBlockForConnectionMultiplier = DEFAULT_MONGO_OPTIONS.threadsAllowedToBlockForConnectionMultiplier; + private int maxWaitTime = DEFAULT_MONGO_OPTIONS.maxWaitTime; + private int connectTimeout = DEFAULT_MONGO_OPTIONS.connectTimeout; + private int socketTimeout = DEFAULT_MONGO_OPTIONS.socketTimeout; + private boolean socketKeepAlive = DEFAULT_MONGO_OPTIONS.socketKeepAlive; + private boolean autoConnectRetry = DEFAULT_MONGO_OPTIONS.autoConnectRetry; + private long maxAutoConnectRetryTime = DEFAULT_MONGO_OPTIONS.maxAutoConnectRetryTime; + private int writeNumber = DEFAULT_MONGO_OPTIONS.w; + private int writeTimeout = DEFAULT_MONGO_OPTIONS.wtimeout; + private boolean writeFsync = DEFAULT_MONGO_OPTIONS.fsync; + private boolean slaveOk = DEFAULT_MONGO_OPTIONS.slaveOk; private boolean ssl; private SSLSocketFactory sslSocketFactory; + private MongoOptions options; + /** * Configures the maximum number of connections allowed per host until we will block. * @@ -197,24 +198,28 @@ public class MongoOptionsFactoryBean implements FactoryBean, Initi * (non-Javadoc) * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet() */ - @SuppressWarnings("deprecation") public void afterPropertiesSet() { - MONGO_OPTIONS.connectionsPerHost = connectionsPerHost; - MONGO_OPTIONS.threadsAllowedToBlockForConnectionMultiplier = threadsAllowedToBlockForConnectionMultiplier; - MONGO_OPTIONS.maxWaitTime = maxWaitTime; - MONGO_OPTIONS.connectTimeout = connectTimeout; - MONGO_OPTIONS.socketTimeout = socketTimeout; - MONGO_OPTIONS.socketKeepAlive = socketKeepAlive; - MONGO_OPTIONS.autoConnectRetry = autoConnectRetry; - MONGO_OPTIONS.maxAutoConnectRetryTime = maxAutoConnectRetryTime; - MONGO_OPTIONS.slaveOk = slaveOk; - MONGO_OPTIONS.w = writeNumber; - MONGO_OPTIONS.wtimeout = writeTimeout; - MONGO_OPTIONS.fsync = writeFsync; + MongoOptions options = new MongoOptions(); + + options.connectionsPerHost = connectionsPerHost; + options.threadsAllowedToBlockForConnectionMultiplier = threadsAllowedToBlockForConnectionMultiplier; + options.maxWaitTime = maxWaitTime; + options.connectTimeout = connectTimeout; + options.socketTimeout = socketTimeout; + options.socketKeepAlive = socketKeepAlive; + options.autoConnectRetry = autoConnectRetry; + options.maxAutoConnectRetryTime = maxAutoConnectRetryTime; + options.slaveOk = slaveOk; + options.w = writeNumber; + options.wtimeout = writeTimeout; + options.fsync = writeFsync; + if (ssl) { - MONGO_OPTIONS.setSocketFactory(sslSocketFactory != null ? sslSocketFactory : SSLSocketFactory.getDefault()); + options.setSocketFactory(sslSocketFactory != null ? sslSocketFactory : SSLSocketFactory.getDefault()); } + + this.options = options; } /* @@ -222,7 +227,7 @@ public class MongoOptionsFactoryBean implements FactoryBean, Initi * @see org.springframework.beans.factory.FactoryBean#getObject() */ public MongoOptions getObject() { - return MONGO_OPTIONS; + return this.options; } /* diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java index 3100d0812..39550cd09 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java @@ -375,7 +375,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { DBObject fieldsObject = query.getFieldsObject(); if (LOGGER.isDebugEnabled()) { - LOGGER.debug(String.format("Executing query: %s sort: %s fields: %s in collection: $s", + LOGGER.debug(String.format("Executing query: %s sort: %s fields: %s in collection: %s", serializeToJsonSafely(queryObject), sortObject, fieldsObject, collectionName)); } @@ -1181,6 +1181,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { public MapReduceResults mapReduce(Query query, String inputCollectionName, String mapFunction, String reduceFunction, MapReduceOptions mapReduceOptions, Class entityClass) { + String mapFunc = replaceWithResourceIfNecessary(mapFunction); String reduceFunc = replaceWithResourceIfNecessary(reduceFunction); DBCollection inputCollection = getCollection(inputCollectionName); @@ -1205,12 +1206,12 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { MapReduceOutput mapReduceOutput = new MapReduceOutput(inputCollection, commandObject, commandResult); List mappedResults = new ArrayList(); DbObjectCallback callback = new ReadDbObjectCallback(mongoConverter, entityClass); + for (DBObject dbObject : mapReduceOutput.results()) { mappedResults.add(callback.doWith(dbObject)); } - MapReduceResults mapReduceResult = new MapReduceResults(mappedResults, commandResult); - return mapReduceResult; + return new MapReduceResults(mappedResults, commandResult); } public GroupByResults group(String inputCollectionName, GroupBy groupBy, Class entityClass) { @@ -1264,15 +1265,14 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { @SuppressWarnings("unchecked") Iterable resultSet = (Iterable) commandResult.get("retval"); - List mappedResults = new ArrayList(); DbObjectCallback callback = new ReadDbObjectCallback(mongoConverter, entityClass); + for (DBObject dbObject : resultSet) { mappedResults.add(callback.doWith(dbObject)); } - GroupByResults groupByResult = new GroupByResults(mappedResults, commandResult); - return groupByResult; + return new GroupByResults(mappedResults, commandResult); } @Override diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ExposedFields.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ExposedFields.java index 03d7df561..764398d22 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ExposedFields.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ExposedFields.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 the original author or authors. + * Copyright 2013-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -32,7 +32,7 @@ import org.springframework.util.CompositeIterator; * @author Thomas Darimont * @since 1.3 */ -public class ExposedFields implements Iterable { +public final class ExposedFields implements Iterable { private static final List NO_FIELDS = Collections.emptyList(); private static final ExposedFields EMPTY = new ExposedFields(NO_FIELDS, NO_FIELDS); diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/Fields.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/Fields.java index b729cce4f..dc7ee6615 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/Fields.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/Fields.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 the original author or authors. + * Copyright 2013-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -32,13 +32,13 @@ import org.springframework.util.StringUtils; * @author Oliver Gierke * @since 1.3 */ -public class Fields implements Iterable { +public final class Fields implements Iterable { private static final String AMBIGUOUS_EXCEPTION = "Found two fields both using '%s' as name: %s and %s! Please " + "customize your field definitions to get to unique field names!"; - public static String UNDERSCORE_ID = "_id"; - public static String UNDERSCORE_ID_REF = "$_id"; + public static final String UNDERSCORE_ID = "_id"; + public static final String UNDERSCORE_ID_REF = "$_id"; private final List fields; @@ -203,7 +203,7 @@ public class Fields implements Iterable { Assert.hasText(nameToSet, "AggregationField name must not be null or empty!"); if (target == null && name.contains(".")) { - this.name = nameToSet.substring(nameToSet.indexOf(".") + 1); + this.name = nameToSet.substring(nameToSet.indexOf('.') + 1); this.target = nameToSet; } else { this.name = nameToSet; diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/GroupOperation.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/GroupOperation.java index 521a2bf85..dd1009e4d 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/GroupOperation.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/GroupOperation.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 the original author or authors. + * Copyright 2013-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -99,7 +99,7 @@ public class GroupOperation implements FieldsExposingAggregationOperation { * * @author Thomas Darimont */ - public class GroupOperationBuilder { + public static final class GroupOperationBuilder { private final GroupOperation groupOperation; private final Operation operation; diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ProjectionOperation.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ProjectionOperation.java index a00944262..cf8093cee 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ProjectionOperation.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ProjectionOperation.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 the original author or authors. + * Copyright 2013-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -252,7 +252,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation { public ExpressionProjectionOperationBuilder(Object value, ProjectionOperation operation, Object[] parameters) { super(value, operation); - this.params = parameters; + this.params = parameters.clone(); } /* @@ -294,7 +294,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation { Assert.notNull(parameters, "Parameters must not be null!"); this.expression = expression; - this.params = parameters; + this.params = parameters.clone(); } /* @@ -316,6 +316,9 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation { */ public static class ProjectionOperationBuilder extends AbstractProjectionOperationBuilder { + private static final String NUMBER_NOT_NULL = "Number must not be null!"; + private static final String FIELD_REFERENCE_NOT_NULL = "Field reference must not be null!"; + private final String name; private final ProjectionOperation operation; private final OperationProjection previousProjection; @@ -382,7 +385,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation { */ public ProjectionOperationBuilder plus(Number number) { - Assert.notNull(number, "Number must not be null!"); + Assert.notNull(number, NUMBER_NOT_NULL); return project("add", number); } @@ -419,7 +422,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation { */ public ProjectionOperationBuilder minus(String fieldReference) { - Assert.notNull(fieldReference, "Field reference must not be null!"); + Assert.notNull(fieldReference, FIELD_REFERENCE_NOT_NULL); return project("subtract", Fields.field(fieldReference)); } @@ -431,7 +434,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation { */ public ProjectionOperationBuilder multiply(Number number) { - Assert.notNull(number, "Number must not be null!"); + Assert.notNull(number, NUMBER_NOT_NULL); return project("multiply", number); } @@ -444,7 +447,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation { */ public ProjectionOperationBuilder multiply(String fieldReference) { - Assert.notNull(fieldReference, "Field reference must not be null!"); + Assert.notNull(fieldReference, FIELD_REFERENCE_NOT_NULL); return project("multiply", Fields.field(fieldReference)); } @@ -456,7 +459,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation { */ public ProjectionOperationBuilder divide(Number number) { - Assert.notNull(number, "Number must not be null!"); + Assert.notNull(number, FIELD_REFERENCE_NOT_NULL); Assert.isTrue(Math.abs(number.intValue()) != 0, "Number must not be zero!"); return project("divide", number); } @@ -470,7 +473,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation { */ public ProjectionOperationBuilder divide(String fieldReference) { - Assert.notNull(fieldReference, "Field reference must not be null!"); + Assert.notNull(fieldReference, FIELD_REFERENCE_NOT_NULL); return project("divide", Fields.field(fieldReference)); } @@ -483,7 +486,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation { */ public ProjectionOperationBuilder mod(Number number) { - Assert.notNull(number, "Number must not be null!"); + Assert.notNull(number, NUMBER_NOT_NULL); Assert.isTrue(Math.abs(number.intValue()) != 0, "Number must not be zero!"); return project("mod", number); } @@ -497,7 +500,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation { */ public ProjectionOperationBuilder mod(String fieldReference) { - Assert.notNull(fieldReference, "Field reference must not be null!"); + Assert.notNull(fieldReference, FIELD_REFERENCE_NOT_NULL); return project("mod", Fields.field(fieldReference)); } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/DBObjectPropertyAccessor.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/DBObjectPropertyAccessor.java index 85485a141..0a18d29b5 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/DBObjectPropertyAccessor.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/DBObjectPropertyAccessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 the original author or authors. + * Copyright 2012-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -31,18 +31,30 @@ import com.mongodb.DBObject; */ class DBObjectPropertyAccessor extends MapAccessor { - static MapAccessor INSTANCE = new DBObjectPropertyAccessor(); + static final MapAccessor INSTANCE = new DBObjectPropertyAccessor(); + /* + * (non-Javadoc) + * @see org.springframework.context.expression.MapAccessor#getSpecificTargetClasses() + */ @Override public Class[] getSpecificTargetClasses() { return new Class[] { DBObject.class }; } + /* + * (non-Javadoc) + * @see org.springframework.context.expression.MapAccessor#canRead(org.springframework.expression.EvaluationContext, java.lang.Object, java.lang.String) + */ @Override public boolean canRead(EvaluationContext context, Object target, String name) { return true; } + /* + * (non-Javadoc) + * @see org.springframework.context.expression.MapAccessor#read(org.springframework.expression.EvaluationContext, java.lang.Object, java.lang.String) + */ @Override @SuppressWarnings("unchecked") public TypedValue read(EvaluationContext context, Object target, String name) { @@ -52,4 +64,4 @@ class DBObjectPropertyAccessor extends MapAccessor { Object value = source.get(name); return value == null ? TypedValue.NULL : new TypedValue(value); } -} \ No newline at end of file +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java index 0d5e398df..b4701f137 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2011-2013 by the original author(s). + * Copyright 2011-2014 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. @@ -74,7 +74,7 @@ import com.mongodb.DBRef; */ public class MappingMongoConverter extends AbstractMongoConverter implements ApplicationContextAware { - protected static final Logger log = LoggerFactory.getLogger(MappingMongoConverter.class); + protected static final Logger LOGGER = LoggerFactory.getLogger(MappingMongoConverter.class); protected final MappingContext, MongoPersistentProperty> mappingContext; protected final SpelExpressionParser spelExpressionParser = new SpelExpressionParser(); @@ -668,7 +668,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App */ protected void addCustomTypeKeyIfNecessary(TypeInformation type, Object value, DBObject dbObject) { - TypeInformation actualType = type != null ? type.getActualType() : type; + TypeInformation actualType = type != null ? type.getActualType() : null; Class reference = actualType == null ? Object.class : actualType.getType(); boolean notTheSameClass = !value.getClass().equals(reference); diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexCreator.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexCreator.java index 88ec0d3c8..3ab078b23 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexCreator.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexCreator.java @@ -1,5 +1,5 @@ /* - * Copyright 2011-2013 the original author or authors. + * Copyright 2011-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -50,7 +50,7 @@ import com.mongodb.util.JSON; public class MongoPersistentEntityIndexCreator implements ApplicationListener, MongoPersistentProperty>> { - private static final Logger log = LoggerFactory.getLogger(MongoPersistentEntityIndexCreator.class); + private static final Logger LOGGER = LoggerFactory.getLogger(MongoPersistentEntityIndexCreator.class); private final Map, Boolean> classesSeen = new ConcurrentHashMap, Boolean>(); private final MongoDbFactory mongoDbFactory; @@ -97,8 +97,8 @@ public class MongoPersistentEntityIndexCreator implements protected void checkForIndexes(final MongoPersistentEntity entity) { final Class type = entity.getType(); if (!classesSeen.containsKey(type)) { - if (log.isDebugEnabled()) { - log.debug("Analyzing class " + type + " for index information."); + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("Analyzing class " + type + " for index information."); } // Make sure indexes get created @@ -112,8 +112,8 @@ public class MongoPersistentEntityIndexCreator implements ensureIndex(indexColl, index.name(), definition, index.unique(), index.dropDups(), index.sparse(), index.background(), index.expireAfterSeconds()); - if (log.isDebugEnabled()) { - log.debug("Created compound index " + index); + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("Created compound index " + index); } } } @@ -133,8 +133,8 @@ public class MongoPersistentEntityIndexCreator implements } else { if (!name.equals(field.getName()) && index.unique() && !index.sparse()) { // Names don't match, and sparse is not true. This situation will generate an error on the server. - if (log.isWarnEnabled()) { - log.warn("The index name " + name + " doesn't match this property name: " + field.getName() + if (LOGGER.isWarnEnabled()) { + LOGGER.warn("The index name " + name + " doesn't match this property name: " + field.getName() + ". Setting sparse=true on this index will prevent errors when inserting documents."); } } @@ -147,8 +147,8 @@ public class MongoPersistentEntityIndexCreator implements ensureIndex(collection, name, definition, index.unique(), index.dropDups(), index.sparse(), index.background(), index.expireAfterSeconds()); - if (log.isDebugEnabled()) { - log.debug("Created property index " + index); + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("Created property index " + index); } } else if (field.isAnnotationPresent(GeoSpatialIndexed.class)) { @@ -165,8 +165,8 @@ public class MongoPersistentEntityIndexCreator implements mongoDbFactory.getDb().getCollection(collection) .ensureIndex(indexObject.getIndexKeys(), indexObject.getIndexOptions()); - if (log.isDebugEnabled()) { - log.debug(String.format("Created %s for entity %s in collection %s! ", indexObject, entity.getType(), + if (LOGGER.isDebugEnabled()) { + LOGGER.debug(String.format("Created %s for entity %s in collection %s! ", indexObject, entity.getType(), collection)); } } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapreduce/MapReduceCounts.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapreduce/MapReduceCounts.java index befcbf873..d47d9e2d8 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapreduce/MapReduceCounts.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapreduce/MapReduceCounts.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2012 the original author or authors. + * Copyright 2010-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,7 +23,7 @@ package org.springframework.data.mongodb.core.mapreduce; */ public class MapReduceCounts { - public static MapReduceCounts NONE = new MapReduceCounts(-1, -1, -1); + public static final MapReduceCounts NONE = new MapReduceCounts(-1, -1, -1); private final long inputCount; private final long emitCount; diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Criteria.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Criteria.java index dbce295e8..786b0cc84 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Criteria.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Criteria.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2013 the original author or authors. + * Copyright 2010-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -52,11 +52,8 @@ public class Criteria implements CriteriaDefinition { private static final Object NOT_SET = new Object(); private String key; - private List criteriaChain; - private LinkedHashMap criteria = new LinkedHashMap(); - private Object isValue = NOT_SET; public Criteria() { @@ -101,13 +98,16 @@ public class Criteria implements CriteriaDefinition { * @return */ public Criteria is(Object o) { - if (isValue != NOT_SET) { + + if (!isValue.equals(NOT_SET)) { throw new InvalidMongoDbApiUsageException( "Multiple 'is' values declared. You need to use 'and' with multiple criteria"); } + if (lastOperatorWasNot()) { throw new InvalidMongoDbApiUsageException("Invalid query: 'not' can't be used with 'is' - use 'ne' instead."); } + this.isValue = o; return this; } @@ -475,8 +475,10 @@ public class Criteria implements CriteriaDefinition { } protected DBObject getSingleCriteriaObject() { + DBObject dbo = new BasicDBObject(); boolean not = false; + for (String k : this.criteria.keySet()) { Object value = this.criteria.get(k); if (not) { @@ -494,12 +496,14 @@ public class Criteria implements CriteriaDefinition { } DBObject queryCriteria = new BasicDBObject(); - if (isValue != NOT_SET) { + + if (!NOT_SET.equals(isValue)) { queryCriteria.put(this.key, this.isValue); queryCriteria.putAll(dbo); } else { queryCriteria.put(this.key, dbo); } + return queryCriteria; } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/NearQuery.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/NearQuery.java index c000d4e32..495744198 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/NearQuery.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/NearQuery.java @@ -1,5 +1,5 @@ /* - * Copyright 2011-2013 the original author or authors. + * Copyright 2011-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -32,7 +32,7 @@ import com.mongodb.DBObject; * @author Oliver Gierke * @author Thomas Darimont */ -public class NearQuery { +public final class NearQuery { private final Point point; private Query query; diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Query.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Query.java index 98814033b..172861e13 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Query.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Query.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2013 the original author or authors. + * Copyright 2010-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,6 +23,7 @@ import java.util.Collections; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; import java.util.Set; import org.springframework.data.domain.Pageable; @@ -41,10 +42,10 @@ import com.mongodb.DBObject; */ public class Query { - private final static String RESTRICTED_TYPES_KEY = "_$RESTRICTED_TYPES"; + private static final String RESTRICTED_TYPES_KEY = "_$RESTRICTED_TYPES"; private final Set> restrictedTypes = new HashSet>(); - private LinkedHashMap criteria = new LinkedHashMap(); + private final Map criteria = new LinkedHashMap(); private Field fieldSpec; private Sort sort; private int skip; @@ -197,6 +198,7 @@ public class Query { public DBObject getQueryObject() { DBObject dbo = new BasicDBObject(); + for (String k : criteria.keySet()) { CriteriaDefinition c = criteria.get(k); DBObject cl = c.getCriteriaObject(); @@ -211,24 +213,19 @@ public class Query { } public DBObject getFieldsObject() { - if (this.fieldSpec == null) { - return null; - } - return fieldSpec.getFieldsObject(); + return this.fieldSpec == null ? null : fieldSpec.getFieldsObject(); } public DBObject getSortObject() { - if (this.sort == null && this.sort == null) { + if (this.sort == null) { return null; } DBObject dbo = new BasicDBObject(); - if (this.sort != null) { - for (org.springframework.data.domain.Sort.Order order : this.sort) { - dbo.put(order.getProperty(), order.isAscending() ? 1 : -1); - } + for (org.springframework.data.domain.Sort.Order order : this.sort) { + dbo.put(order.getProperty(), order.isAscending() ? 1 : -1); } return dbo; diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/AbstractMongoQuery.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/AbstractMongoQuery.java index 88e4dc3cb..190c83b82 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/AbstractMongoQuery.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/AbstractMongoQuery.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2013 the original author or authors. + * Copyright 2010-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -153,7 +153,7 @@ public abstract class AbstractMongoQuery implements RepositoryQuery { * * @author Oliver Gierke */ - class CollectionExecution extends Execution { + final class CollectionExecution extends Execution { private final Pageable pageable; @@ -176,7 +176,7 @@ public abstract class AbstractMongoQuery implements RepositoryQuery { * * @author Oliver Gierke */ - class PagedExecution extends Execution { + final class PagedExecution extends Execution { private final Pageable pageable; @@ -213,7 +213,7 @@ public abstract class AbstractMongoQuery implements RepositoryQuery { * * @author Oliver Gierke */ - class SingleEntityExecution extends Execution { + final class SingleEntityExecution extends Execution { private final boolean countProjection; @@ -239,7 +239,7 @@ public abstract class AbstractMongoQuery implements RepositoryQuery { * * @author Oliver Gierke */ - class GeoNearExecution extends Execution { + final class GeoNearExecution extends Execution { private final MongoParameterAccessor accessor;