Example usage for org.apache.commons.beanutils PropertyUtilsBean getMappedProperty

List of usage examples for org.apache.commons.beanutils PropertyUtilsBean getMappedProperty

Introduction

In this page you can find the example usage for org.apache.commons.beanutils PropertyUtilsBean getMappedProperty.

Prototype

public Object getMappedProperty(Object bean, String name)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException 

Source Link

Document

Return the value of the specified mapped property of the specified bean, with no type conversions.

Usage

From source file:org.opencms.workplace.CmsWidgetDialogParameter.java

/**
 * Create a new Widget parameter based on a given object's property.<p>
 * /*from w  w w .ja va2  s.c om*/
 * @param base the base object to map the parameter to / from
 * @param property the base object property to map the parameter to / from
 * @param htmlName the form id name to use in the generated HTML
 * @param defaultValue the default value to use for this parameter
 * @param dialogPage the dialog page to use the widget on
 * @param widget the widget used for this paramete
 * @param minOccurs the required minimum numer of occurences of this parameter
 * @param maxOccurs the maximum allowed numer of occurences of this parameter
 */
public CmsWidgetDialogParameter(Object base, String property, String htmlName, String defaultValue,
        String dialogPage, I_CmsWidget widget, int minOccurs, int maxOccurs) {

    if (htmlName == null) {
        htmlName = property;
    }

    if ((base instanceof List) || (base instanceof SortedMap)) {

        // this is a list, use custom list mappings
        init(null, defaultValue, htmlName, widget, dialogPage, 0, MAX_OCCURENCES, 0);

        m_baseObject = null;
        m_baseObjectProperty = null;
        m_baseCollection = base;

    } else {

        // generic object:use reflection to map object properties
        init(null, defaultValue, htmlName, widget, dialogPage, minOccurs, maxOccurs, 0);

        m_baseObject = base;
        m_baseObjectProperty = property;
        m_baseCollection = null;

        PropertyUtilsBean bean = new PropertyUtilsBean();

        Object value = null;
        // make sure the base object has the requested property
        if (!bean.isReadable(m_baseObject, m_baseObjectProperty)
                || !bean.isWriteable(m_baseObject, m_baseObjectProperty)) {
            try {
                // check if this is a mapped property
                value = bean.getMappedProperty(m_baseObject, m_baseObjectProperty);
            } catch (Exception e) {
                throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_NO_PROPERTY_2,
                        base.getClass().getName(), property));
            }
        }

        try {
            if (value == null) {
                // may have been read already as a mapped property
                value = bean.getNestedProperty(m_baseObject, m_baseObjectProperty);
            }
        } catch (Exception e) {
            throw new CmsRuntimeException(
                    Messages.get().container(Messages.ERR_PROPERTY_READ_2, property, base.getClass().getName()),
                    e);
        }

        if (value != null) {
            if ((value instanceof List) || (value instanceof SortedMap)) {
                m_baseCollection = value;
                m_minOccurs = 0;
                m_maxOccurs = MAX_OCCURENCES;
            } else {
                m_defaultValue = String.valueOf(value);
                m_value = m_defaultValue;
                if ((m_minOccurs == 0) && !m_value.equals(defaultValue)) {
                    // if value is different from default ensure this widget is displayed
                    m_minOccurs = 1;
                }
            }
        }
    }
}