Back Forum Reply New

Conditionally Copying Bean Properties

I am looking for an easy way to copy properties from one bean to another.
I know there are several possible approaches, generally using BeanUtils.
I also thought the DataBinder should be an option, but can't find an easy way to generate the necessary PropertyValues required for the bind method.

However, I have two additional requirements.
1)  Only certain properties may be copied, based on the state of the target bean.
2)  After the copy, I want to know which, if any, properties have been copied.

The point of this is to use the approach in both the Controller and Model layers.

In the Controller to prevent properties from being overlayed in domain objects.
(I realize i can set allowed/disallowed properties in the initBinder method.  I was hoping for a more general approach)

In the Model layer to prevent one or more properties (conditionally) from being updated based on the status of the domain object.  Also to know if any property has changed on the domain Object in order to know if it needs to be persisted.
A Listener approach would be fine to be notified of the property changes.

Anyone have any ideas?

did you take a look at the DataBinder tests?  It has examples of using the DataBinder.

org..validation.DataBinderTests

Have a look at the org..beans.BeanUtils.copyProperties method. You can also specify properties to be excluded there.

Hope that helps,
Andreas

I realize that the BeanUtils class can provide this function, but it doesn't tell me what properties have changed.
All the methods in that class are static, and don't return changed properties.

What I would really like is a Class that has a listener defined, or has a method that can return all the Properties that have changed.

I see. But maybe you could use the source of aforementioned method to implement your requirement. Then you wouldn't need to start from scratch (if there is no ready solution available, that is).

Regards,
Andreas

Probably too complex approach, but you could create an AOP advice that is applied to the setters.  This advice would in turn use the standard JavaBean Bound properties support like, like PropertyChangeListeners.   

The advantage is that the target bean does not need to know it is being 'watched' or needs to manipulate a PropertyChangeSupport member.

An alternative approach was used in the book quotro Springquot; where an Introduction  was used to create an isModified() interface that could be applied to an object.  But, there the mixin just set a flag, it did not publish a PropertyChangeEvent.

I didn't intend to suggest adding the functionality to the beans themselves. I envisioned a utility class like the one in BeanUtils.

It should be possible to check both beans' states and copy only unequal property values and reporting the processed properties. I remember implementing a similar functionality once (though it is a while ago and I do not have the sources anymore).

Regards,
Andreas

I definitely would want to do this as a utility class, as I want the code to be reusable.
AOP around a BeanUtil like copy function would probably work, although as pointed out, might be a little complicated.
I might give that one a try.

Thanks for the advice.
¥
Back Forum Reply New