Mutable BeanItem

Hi,

The current BeanItem wrapper only allows for a bean to be set in the constructor. This bean is immutable (marked final and “setBean(BT newBean)” method does not exist).

Unfortunately, our beans are JPA entities, and when we save the entity, a new instance is returned. There is no way currently to replace the bean in the existing BeanItem. We have to create a new BeanItem, with the new bean, and rebind all the controls.

I’ve been trying to extend BeanItem by creating a MutableBeanItem class which provides a “setBean(BT bean)” method, along the following lines (pseudocode):

public class MutableBeanItem<BT> extends BeanItem<BT> {

    public UpdatableBeanItem(BT bean) {
        super(bean);
    }

    public void setBean(BT newBean)  {
        // for every existing property, update it's value with the value from the new bean.
        // this will propogate the changes to the view... 
        final Collection<?> propertyIds = getItemPropertyIds();
        for (final Object propId : propertyIds) {
            final Property property =  getItemProperty(propId);
            property.setValue(
                        getValueFromNewBean(newBean, propery));      
        }

            // now replace the old bean with the new bean. Won't work as oldBean is final.
            // we would need to do this to ensure that the changes in the front end continue being propogated to the new bean
            BT oldBean = getBean();
            oldBean = newBean;
    }

    /** pseudocode: extract the property value from the new bean **/
    private ??? getValueFromNewBean(BT newBean, Property propery)) {

       reuturn Property.getValueExtractor().getValue(newBean);
    }
}

Does anyone know:

a) If what I’m trying to do is possible? I had thought that I could extend PropertysetItem instead of BeanItem, thus creating my own bean instance. However, I don’t want to have to implement all the existing BeanItem code (property listerners and so on…), plus I’d like to take advantage of the existing BeanItem implementation and eventual improvements in future releases.

b) if there’s a simpler way to achive what I’m doing?

Thanks,
Dave