Browse Source

DATAMONGO-474 - Fixed criteria mapping for MongoTemplate.group(…).

The criteria object handed to the group object needs to be mapped correctly to map complex values. Improved error handling on the way.
1.0.x
Oliver Gierke 14 years ago
parent
commit
134e7762a7
  1. 58
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java

58
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java

@ -998,22 +998,10 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
LOGGER.debug("Executing MapReduce on collection [" + command.getInput() + "], mapFunction [" + mapFunc LOGGER.debug("Executing MapReduce on collection [" + command.getInput() + "], mapFunction [" + mapFunc
+ "], reduceFunction [" + reduceFunc + "]"); + "], reduceFunction [" + reduceFunc + "]");
} }
CommandResult commandResult = null;
try { CommandResult commandResult = command.getOutputType() == MapReduceCommand.OutputType.INLINE ? executeCommand(
if (command.getOutputType() == MapReduceCommand.OutputType.INLINE) { commandObject, getDb().getOptions()) : executeCommand(commandObject);
commandResult = executeCommand(commandObject, getDb().getOptions()); handleCommandError(commandResult, commandObject);
} else {
commandResult = executeCommand(commandObject);
}
commandResult.throwOnError();
} catch (RuntimeException ex) {
this.potentiallyConvertRuntimeException(ex);
}
String error = commandResult.getErrorMessage();
if (error != null) {
throw new InvalidDataAccessApiUsageException("Command execution failed: Error [" + error + "], Command = "
+ commandObject);
}
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
LOGGER.debug(String.format("MapReduce command result = [%s]", LOGGER.debug(String.format("MapReduce command result = [%s]",
@ -1044,7 +1032,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
if (criteria == null) { if (criteria == null) {
dbo.put("cond", null); dbo.put("cond", null);
} else { } else {
dbo.put("cond", criteria.getCriteriaObject()); dbo.put("cond", mapper.getMappedObject(criteria.getCriteriaObject(), null));
} }
// If initial document was a JavaScript string, potentially loaded by Spring's Resource abstraction, load it and // If initial document was a JavaScript string, potentially loaded by Spring's Resource abstraction, load it and
// convert to DBObject // convert to DBObject
@ -1073,18 +1061,9 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
LOGGER.debug(String.format("Executing Group with DBObject [%s]", LOGGER.debug(String.format("Executing Group with DBObject [%s]",
SerializationUtils.serializeToJsonSafely(commandObject))); SerializationUtils.serializeToJsonSafely(commandObject)));
} }
CommandResult commandResult = null;
try { CommandResult commandResult = executeCommand(commandObject, getDb().getOptions());
commandResult = executeCommand(commandObject, getDb().getOptions()); handleCommandError(commandResult, commandObject);
commandResult.throwOnError();
} catch (RuntimeException ex) {
this.potentiallyConvertRuntimeException(ex);
}
String error = commandResult.getErrorMessage();
if (error != null) {
throw new InvalidDataAccessApiUsageException("Command execution failed: Error [" + error + "], Command = "
+ commandObject);
}
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Group command result = [" + commandResult + "]"); LOGGER.debug("Group command result = [" + commandResult + "]");
@ -1556,6 +1535,27 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
return resolved == null ? ex : resolved; return resolved == null ? ex : resolved;
} }
/**
* Inspects the given {@link CommandResult} for erros and potentially throws an
* {@link InvalidDataAccessApiUsageException} for that error.
*
* @param result must not be {@literal null}.
* @param source must not be {@literal null}.
*/
private void handleCommandError(CommandResult result, DBObject source) {
try {
result.throwOnError();
} catch (MongoException ex) {
String error = result.getErrorMessage();
error = error == null ? "NO MESSAGE" : error;
throw new InvalidDataAccessApiUsageException("Command execution failed: Error [" + error + "], Command = "
+ source, ex);
}
}
private static final MongoConverter getDefaultMongoConverter(MongoDbFactory factory) { private static final MongoConverter getDefaultMongoConverter(MongoDbFactory factory) {
MappingMongoConverter converter = new MappingMongoConverter(factory, new MongoMappingContext()); MappingMongoConverter converter = new MappingMongoConverter(factory, new MongoMappingContext());
converter.afterPropertiesSet(); converter.afterPropertiesSet();

Loading…
Cancel
Save