From ef7178c9fa12ebc29b4723c3208f5ede3499f9e7 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Tue, 1 Mar 2011 21:05:43 +0100 Subject: [PATCH] =?UTF-8?q?Extended=20=20ConvertingParameterAccessor=20to?= =?UTF-8?q?=20convert=20for=20getBindableValue(=E2=80=A6)=20as=20well.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added special handling for enums as the PropertyDescriptor otherwise fails to resolve the generic type. --- .../ConvertingParameterAccessor.java | 41 +++++++++++++++++-- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/ConvertingParameterAccessor.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/ConvertingParameterAccessor.java index b884d38d1..308d91abd 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/ConvertingParameterAccessor.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/ConvertingParameterAccessor.java @@ -71,6 +71,27 @@ public class ConvertingParameterAccessor implements ParameterAccessor { public Sort getSort() { return delegate.getSort(); } + + /* (non-Javadoc) + * @see org.springframework.data.repository.query.ParameterAccessor#getBindableParameter(int) + */ + public Object getBindableValue(int index) { + + return getConvertedValue(delegate.getBindableValue(index)); + } + + /** + * Converts the given value with the underlying {@link MongoWriter}. + * + * @param value + * @return + */ + private Object getConvertedValue(Object value) { + + DBObject result = new BasicDBObject(); + writer.write(value.getClass().isEnum() ? new EnumValueHolder((Enum) value) : new ValueHolder(value), result); + return result.get("value"); + } /** * Custom {@link Iterator} to convert items before returning them. @@ -106,9 +127,7 @@ public class ConvertingParameterAccessor implements ParameterAccessor { */ public Object next() { - DBObject result = new BasicDBObject(); - writer.write(new ValueHolder(delegate.next()), result); - return result.get("value"); + return getConvertedValue(delegate.next()); } /* @@ -141,4 +160,20 @@ public class ConvertingParameterAccessor implements ParameterAccessor { return value; } } + + private static class EnumValueHolder { + + private Enum value; + + public EnumValueHolder(Enum value) { + this.value = value; + } + + /** + * @return the value + */ + public Enum getValue() { + return value; + } + } }