From 9c9904686be0fea395e34bc5f5ee4fb623070bbe Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Wed, 9 Oct 2013 15:45:12 +0200 Subject: [PATCH] DATACMNS-293 - Further refinements in soft delete support. Improved the template methods in AbstractSoftDeleteQueryAugmentor to accommodate more complex scenarios (see DATAJPA-307). Added DirectFieldAccessFallbackBeanWrapper from SD JPA to use it for looking up property values. --- .../AbstractSoftDeleteQueryAugmentor.java | 47 +++++++------------ .../AnnotationBasedQueryAugmentor.java | 12 +++-- .../support/RepositoryFactorySupport.java | 4 +- 3 files changed, 28 insertions(+), 35 deletions(-) diff --git a/src/main/java/org/springframework/data/repository/augment/AbstractSoftDeleteQueryAugmentor.java b/src/main/java/org/springframework/data/repository/augment/AbstractSoftDeleteQueryAugmentor.java index 38096fd88..149ded907 100644 --- a/src/main/java/org/springframework/data/repository/augment/AbstractSoftDeleteQueryAugmentor.java +++ b/src/main/java/org/springframework/data/repository/augment/AbstractSoftDeleteQueryAugmentor.java @@ -15,16 +15,14 @@ */ package org.springframework.data.repository.augment; -import org.springframework.beans.ConfigurablePropertyAccessor; -import org.springframework.beans.MutablePropertyValues; -import org.springframework.beans.PropertyAccessor; +import org.springframework.beans.BeanWrapper; import org.springframework.data.repository.SoftDelete; -import org.springframework.validation.DataBinder; +import org.springframework.data.util.DirectFieldAccessFallbackBeanWrapper; /** * Base class to implement a {@link QueryAugmentor} to soft-delete entities. * - * @since 1.6 + * @since 1.7 * @author Oliver Gierke */ public abstract class AbstractSoftDeleteQueryAugmentor, N extends QueryContext, U extends UpdateContext> @@ -48,26 +46,32 @@ public abstract class AbstractSoftDeleteQueryAugmentor return context; } - CustomDataBinder binder = new CustomDataBinder(entity); - binder.initDirectFieldAccess(); + BeanWrapper wrapper = createBeanWrapper(context); - Object currentValue = binder.getPropertyAccessor().getPropertyValue(property); + Object currentValue = wrapper.getPropertyValue(property); Object nextValue = annotation.flagMode().toDeletedValue(currentValue); if (nextValue == null) { return context; } - MutablePropertyValues values = new MutablePropertyValues(); - values.add(property, nextValue); - - binder.bind(values); - + wrapper.setPropertyValue(property, nextValue); updateDeletedState(entity, context); return null; } + /** + * Creates a new {@link BeanWrapper} for the given {@link UpdateContext}. Defaults to a + * {@link DirectFieldAccessFallbackBeanWrapper}. + * + * @param context will never be {@literal null}. + * @return + */ + protected BeanWrapper createBeanWrapper(U context) { + return new DirectFieldAccessFallbackBeanWrapper(context.getEntity()); + } + /** * Update the entity using the API exposed in the given {@link UpdateContext}. * @@ -75,21 +79,4 @@ public abstract class AbstractSoftDeleteQueryAugmentor * @param context will never be {@literal null}. */ public abstract void updateDeletedState(Object entity, U context); - - /** - * Custom {@link DataBinder} to expose the {@link PropertyAccessor} used. - * - * @author Oliver Gierke - */ - private static class CustomDataBinder extends DataBinder { - - public CustomDataBinder(Object target) { - super(target); - } - - @Override - public ConfigurablePropertyAccessor getPropertyAccessor() { - return super.getPropertyAccessor(); - } - } } diff --git a/src/main/java/org/springframework/data/repository/augment/AnnotationBasedQueryAugmentor.java b/src/main/java/org/springframework/data/repository/augment/AnnotationBasedQueryAugmentor.java index 62405cdf1..4a7c0b185 100644 --- a/src/main/java/org/springframework/data/repository/augment/AnnotationBasedQueryAugmentor.java +++ b/src/main/java/org/springframework/data/repository/augment/AnnotationBasedQueryAugmentor.java @@ -109,13 +109,17 @@ public abstract class AnnotationBasedQueryAugmentor[]) proxy.getProxiedInterfaces()); + return Arrays.asList(proxy.getProxiedInterfaces()); } return Collections.> singletonList(getMethodInvocation().getThis().getClass());