Java Code Examples for org.apache.commons.beanutils.DynaProperty

The following examples show how to use org.apache.commons.beanutils.DynaProperty. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: gemfirexd-oss   Source File: SqlDynaClass.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Initializes the primary key and non primary key property arrays.
 */
protected void initPrimaryKeys()
{
    List           pkProps    = new ArrayList();
    List           nonPkProps = new ArrayList();
    DynaProperty[] properties = getDynaProperties();

    for (int idx = 0; idx < properties.length; idx++)
    {
        if (properties[idx] instanceof SqlDynaProperty)
        {
            SqlDynaProperty sqlProperty = (SqlDynaProperty)properties[idx];

            if (sqlProperty.isPrimaryKey())
            {
                pkProps.add(sqlProperty);
            }
            else
            {
                nonPkProps.add(sqlProperty);
            }
        }
    }
    _primaryKeyProperties    = (SqlDynaProperty[])pkProps.toArray(new SqlDynaProperty[pkProps.size()]);
    _nonPrimaryKeyProperties = (SqlDynaProperty[])nonPkProps.toArray(new SqlDynaProperty[nonPkProps.size()]);
}
 
Example 2
Source Project: gemfirexd-oss   Source File: SqlDynaBean.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public String toString()
{
    StringBuilder   result = new StringBuilder();
    DynaClass      type   = getDynaClass();
    DynaProperty[] props  = type.getDynaProperties();

    result.append(type.getName());
    result.append(": ");
    for (int idx = 0; idx < props.length; idx++)
    {
        if (idx > 0)
        {
            result.append(", ");
        }
        result.append(props[idx].getName());
        result.append(" = ");
        result.append(get(props[idx].getName()));
    }
    return result.toString();
}
 
Example 3
Source Project: gemfirexd-oss   Source File: TestAgainstLiveDatabaseBase.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Determines the value of the bean's property that has the given name. Depending on the
 * case-setting of the current builder, the case of teh name is considered or not. 
 * 
 * @param bean     The bean
 * @param propName The name of the property
 * @return The value
 */
protected Object getPropertyValue(DynaBean bean, String propName)
{
    if (getPlatform().isDelimitedIdentifierModeOn())
    {
        return bean.get(propName);
    }
    else
    {
        DynaProperty[] props = bean.getDynaClass().getDynaProperties();

        for (int idx = 0; idx < props.length; idx++)
        {
            if (propName.equalsIgnoreCase(props[idx].getName()))
            {
                return bean.get(props[idx].getName());
            }
        }
        throw new IllegalArgumentException("The bean has no property with the name "+propName);
    }
}
 
Example 4
Source Project: gemfirexd-oss   Source File: SqlDynaClass.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Initializes the primary key and non primary key property arrays.
 */
protected void initPrimaryKeys()
{
    List           pkProps    = new ArrayList();
    List           nonPkProps = new ArrayList();
    DynaProperty[] properties = getDynaProperties();

    for (int idx = 0; idx < properties.length; idx++)
    {
        if (properties[idx] instanceof SqlDynaProperty)
        {
            SqlDynaProperty sqlProperty = (SqlDynaProperty)properties[idx];

            if (sqlProperty.isPrimaryKey())
            {
                pkProps.add(sqlProperty);
            }
            else
            {
                nonPkProps.add(sqlProperty);
            }
        }
    }
    _primaryKeyProperties    = (SqlDynaProperty[])pkProps.toArray(new SqlDynaProperty[pkProps.size()]);
    _nonPrimaryKeyProperties = (SqlDynaProperty[])nonPkProps.toArray(new SqlDynaProperty[nonPkProps.size()]);
}
 
Example 5
Source Project: gemfirexd-oss   Source File: SqlDynaBean.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public String toString()
{
    StringBuilder   result = new StringBuilder();
    DynaClass      type   = getDynaClass();
    DynaProperty[] props  = type.getDynaProperties();

    result.append(type.getName());
    result.append(": ");
    for (int idx = 0; idx < props.length; idx++)
    {
        if (idx > 0)
        {
            result.append(", ");
        }
        result.append(props[idx].getName());
        result.append(" = ");
        result.append(get(props[idx].getName()));
    }
    return result.toString();
}
 
Example 6
Source Project: gemfirexd-oss   Source File: TestAgainstLiveDatabaseBase.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Determines the value of the bean's property that has the given name. Depending on the
 * case-setting of the current builder, the case of teh name is considered or not. 
 * 
 * @param bean     The bean
 * @param propName The name of the property
 * @return The value
 */
protected Object getPropertyValue(DynaBean bean, String propName)
{
    if (getPlatform().isDelimitedIdentifierModeOn())
    {
        return bean.get(propName);
    }
    else
    {
        DynaProperty[] props = bean.getDynaClass().getDynaProperties();

        for (int idx = 0; idx < props.length; idx++)
        {
            if (propName.equalsIgnoreCase(props[idx].getName()))
            {
                return bean.get(props[idx].getName());
            }
        }
        throw new IllegalArgumentException("The bean has no property with the name "+propName);
    }
}
 
Example 7
Source Project: kfs   Source File: ObjectUtil.java    License: GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * build a map of business object with its specified property names and corresponding values
 * 
 * @param businessObject the given business object
 * @param the specified fields that need to be included in the return map
 * @return the map of business object with its property names and values
 */
public static Map<String, Object> buildPropertyMap(Object object, List<String> keyFields) {
    DynaClass dynaClass = WrapDynaClass.createDynaClass(object.getClass());
    DynaProperty[] properties = dynaClass.getDynaProperties();
    Map<String, Object> propertyMap = new LinkedHashMap<String, Object>();

    for (DynaProperty property : properties) {
        String propertyName = property.getName();

        if (PropertyUtils.isReadable(object, propertyName) && keyFields.contains(propertyName)) {
            try {
                Object propertyValue = PropertyUtils.getProperty(object, propertyName);

                if (propertyValue != null && !StringUtils.isEmpty(propertyValue.toString())) {
                    propertyMap.put(propertyName, propertyValue);
                }
            }
            catch (Exception e) {
                LOG.info(e);
            }
        }
    }
    return propertyMap;
}
 
Example 8
Source Project: kfs   Source File: ObjectUtil.java    License: GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * determine if the source object has a field with null as its value
 * 
 * @param sourceObject the source object
 */
public static boolean hasNullValueField(Object sourceObject) {
    DynaClass dynaClass = WrapDynaClass.createDynaClass(sourceObject.getClass());
    DynaProperty[] properties = dynaClass.getDynaProperties();

    for (DynaProperty property : properties) {
        String propertyName = property.getName();

        if (PropertyUtils.isReadable(sourceObject, propertyName)) {
            try {
                Object propertyValue = PropertyUtils.getProperty(sourceObject, propertyName);
                if (propertyValue == null) {
                    return true;
                }
            }
            catch (Exception e) {
                LOG.info(e);
                return false;
            }
        }
    }
    return false;
}
 
Example 9
Source Project: kfs   Source File: OJBUtility.java    License: GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * This method builds a map of business object with its property names and values
 * 
 * @param businessObject the given business object
 * @return the map of business object with its property names and values
 */
public static LinkedHashMap buildPropertyMap(Object businessObject) {
    DynaClass dynaClass = WrapDynaClass.createDynaClass(businessObject.getClass());
    DynaProperty[] properties = dynaClass.getDynaProperties();
    LinkedHashMap propertyMap = new LinkedHashMap();

    try {
        for (int numOfProperty = 0; numOfProperty < properties.length; numOfProperty++) {
            String propertyName = properties[numOfProperty].getName();
            if (PropertyUtils.isWriteable(businessObject, propertyName)) {
                Object propertyValue = PropertyUtils.getProperty(businessObject, propertyName);
                propertyMap.put(propertyName, propertyValue);
            }
        }
    }
    catch (Exception e) {
        LOG.error("OJBUtility.buildPropertyMap()" + e);
    }
    return propertyMap;
}
 
Example 10
@Override
public DynaProperty[] getDynaProperties()
{
    if (LOG.isTraceEnabled())
    {
        LOG.trace("getDynaProperties()");
    }

    final Iterator<String> keys = configuration.getKeys();
    final List<DynaProperty> properties = new ArrayList<>();
    while (keys.hasNext())
    {
        final String key = keys.next();
        final DynaProperty property = getDynaProperty(key);
        properties.add(property);
    }

    final DynaProperty[] propertyArray = new DynaProperty[properties.size()];
    properties.toArray(propertyArray);
    if (LOG.isDebugEnabled())
    {
        LOG.debug("Found " + properties.size() + " properties.");
    }

    return propertyArray;
}
 
Example 11
Source Project: commons-configuration   Source File: MultiWrapDynaBean.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new instance of {@code MultiWrapDynaBean} and initializes it
 * with the given collections of beans to be wrapped.
 *
 * @param beans the wrapped beans
 */
public MultiWrapDynaBean(final Collection<?> beans)
{
    propsToBeans = new HashMap<>();
    final Collection<DynaClass> beanClasses =
            new ArrayList<>(beans.size());

    for (final Object bean : beans)
    {
        final DynaBean dynaBean = createDynaBean(bean);
        final DynaClass beanClass = dynaBean.getDynaClass();
        for (final DynaProperty prop : beanClass.getDynaProperties())
        {
            // ensure an order of properties
            if (!propsToBeans.containsKey(prop.getName()))
            {
                propsToBeans.put(prop.getName(), dynaBean);
            }
        }
        beanClasses.add(beanClass);
    }

    dynaClass = new MultiWrapDynaClass(beanClasses);
}
 
Example 12
Source Project: commons-jxpath   Source File: DynaBeanPropertyPointer.java    License: Apache License 2.0 6 votes vote down vote up
public String[] getPropertyNames() {
    /* @todo do something about the sorting - LIKE WHAT? - MJB */
    if (names == null) {
        DynaClass dynaClass = dynaBean.getDynaClass();
        DynaProperty[] dynaProperties = dynaClass.getDynaProperties();
        ArrayList properties = new ArrayList(dynaProperties.length);
        for (int i = 0; i < dynaProperties.length; i++) {
            String name = dynaProperties[i].getName();
            if (!CLASS.equals(name)) {
                properties.add(name);
            }
        }
        names = (String[]) properties.toArray(new String[properties.size()]);
        Arrays.sort(names);
    }
    return names;
}
 
Example 13
Source Project: commons-jxpath   Source File: DynaBeanPropertyPointer.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Convert a value to the appropriate property type.
 * @param value to convert
 * @param element whether this should be a collection element.
 * @return conversion result
 */
private Object convert(Object value, boolean element) {
    DynaClass dynaClass = dynaBean.getDynaClass();
    DynaProperty property = dynaClass.getDynaProperty(getPropertyName());
    Class type = property.getType();
    if (element) {
        if (type.isArray()) {
            type = type.getComponentType();
        }
        else {
            return value; // No need to convert
        }
    }

    try {
        return TypeUtils.convert(value, type);
    }
    catch (Exception ex) {
        String string = value == null ? "null" : value.getClass().getName();
        throw new JXPathTypeConversionException(
                "Cannot convert value of class " + string + " to type "
                        + type, ex);
    }
}
 
Example 14
Source Project: gemfirexd-oss   Source File: SqlDynaClass.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Returns the properties of this dyna class.
 * 
 * @return The properties
 */
public SqlDynaProperty[] getSqlDynaProperties()
{
    DynaProperty[]    props  = getDynaProperties();
    SqlDynaProperty[] result = new SqlDynaProperty[props.length];

    System.arraycopy(props, 0, result, 0, props.length);
    return result;
}
 
Example 15
Source Project: gemfirexd-oss   Source File: SqlDynaBean.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public boolean equals(Object obj)
{
    if (obj instanceof SqlDynaBean)
    {
        SqlDynaBean other     = (SqlDynaBean)obj;
        DynaClass   dynaClass = getDynaClass();

        if (dynaClass.equals(other.getDynaClass()))
        {
            DynaProperty[] props = dynaClass.getDynaProperties();

            for (int idx = 0; idx < props.length; idx++)
            {
                Object value      = get(props[idx].getName());
                Object otherValue = other.get(props[idx].getName());

                if (value == null)
                {
                    if (otherValue != null)
                    {
                        return false;
                    }
                }
                else
                {
                    return value.equals(otherValue);
                }
            }
            return true;
        }
    }
    return false;
}
 
Example 16
Source Project: gemfirexd-oss   Source File: SqlDynaClass.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Returns the properties of this dyna class.
 * 
 * @return The properties
 */
public SqlDynaProperty[] getSqlDynaProperties()
{
    DynaProperty[]    props  = getDynaProperties();
    SqlDynaProperty[] result = new SqlDynaProperty[props.length];

    System.arraycopy(props, 0, result, 0, props.length);
    return result;
}
 
Example 17
Source Project: gemfirexd-oss   Source File: SqlDynaBean.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public boolean equals(Object obj)
{
    if (obj instanceof SqlDynaBean)
    {
        SqlDynaBean other     = (SqlDynaBean)obj;
        DynaClass   dynaClass = getDynaClass();

        if (dynaClass.equals(other.getDynaClass()))
        {
            DynaProperty[] props = dynaClass.getDynaProperties();

            for (int idx = 0; idx < props.length; idx++)
            {
                Object value      = get(props[idx].getName());
                Object otherValue = other.get(props[idx].getName());

                if (value == null)
                {
                    if (otherValue != null)
                    {
                        return false;
                    }
                }
                else
                {
                    return value.equals(otherValue);
                }
            }
            return true;
        }
    }
    return false;
}
 
Example 18
Source Project: reflectutils   Source File: DynaBeanAdapter.java    License: Apache License 2.0 5 votes vote down vote up
public Class<?> getFieldType(Object obj, String name) {
    DynaClass dynaClass = ((DynaBean) obj).getDynaClass();
    DynaProperty dynaProperty = dynaClass.getDynaProperty(name);
    if (dynaProperty == null) {
        throw new FieldnameNotFoundException("DynaBean: Could not find this fieldName ("+name+") on the target object: " + obj, name, null);
    }
    return dynaProperty.getType();
}
 
Example 19
Source Project: reflectutils   Source File: DynaBeanAdapter.java    License: Apache License 2.0 5 votes vote down vote up
public Map<String, Object> getFieldValues(Object obj, FieldsFilter filter) {
    Map<String, Object> values = new HashMap<String, Object>();
    DynaProperty[] descriptors =
        ((DynaBean) obj).getDynaClass().getDynaProperties();
    for (int i = 0; i < descriptors.length; i++) {
        String name = descriptors[i].getName();
        // cannot filter the values for dynabeans -AZ
        Object o = getSimpleValue(obj, name);
        values.put(name, o);
    }
    return values;
}
 
Example 20
Source Project: reflectutils   Source File: DynaBeanAdapter.java    License: Apache License 2.0 5 votes vote down vote up
public Object getSimpleValue(Object obj, String name) {
    DynaProperty descriptor =
        ((DynaBean) obj).getDynaClass().getDynaProperty(name);
    if (descriptor == null) {
        throw new FieldnameNotFoundException(name);
    }
    Object value = (((DynaBean) obj).get(name));
    return value;
}
 
Example 21
Source Project: reflectutils   Source File: DynaBeanAdapter.java    License: Apache License 2.0 5 votes vote down vote up
public Object getIndexedValue(Object obj, String name, int index) {
    DynaProperty descriptor =
        ((DynaBean) obj).getDynaClass().getDynaProperty(name);
    if (descriptor == null) {
        throw new FieldnameNotFoundException(name);
    }
    Object value = ((DynaBean) obj).get(name, index);
    return value;
}
 
Example 22
Source Project: reflectutils   Source File: DynaBeanAdapter.java    License: Apache License 2.0 5 votes vote down vote up
public Object getMappedValue(Object obj, String name, String key) {
    DynaProperty descriptor =
        ((DynaBean) obj).getDynaClass().getDynaProperty(name);
    if (descriptor == null) {
        throw new FieldnameNotFoundException(name);
    }
    Object value = ((DynaBean) obj).get(name, key);
    return value;
}
 
Example 23
Source Project: reflectutils   Source File: DynaBeanAdapter.java    License: Apache License 2.0 5 votes vote down vote up
public void setIndexedValue(Object obj, String name, int index, Object value) {
    DynaProperty descriptor =
        ((DynaBean) obj).getDynaClass().getDynaProperty(name);
    if (descriptor == null) {
        throw new FieldnameNotFoundException(name);
    }
    ((DynaBean) obj).set(name, index, value);
}
 
Example 24
Source Project: reflectutils   Source File: DynaBeanAdapter.java    License: Apache License 2.0 5 votes vote down vote up
public void setMappedValue(Object obj, String name, String key, Object value) {
    DynaProperty descriptor =
        ((DynaBean) obj).getDynaClass().getDynaProperty(name);
    if (descriptor == null) {
        throw new FieldnameNotFoundException(name);
    }
    ((DynaBean) obj).set(name, key, value);
}
 
Example 25
Source Project: reflectutils   Source File: DynaBeanAdapter.java    License: Apache License 2.0 5 votes vote down vote up
public void setSimpleValue(Object obj, String name, Object value) {
    DynaProperty descriptor =
        ((DynaBean) obj).getDynaClass().getDynaProperty(name);
    if (descriptor == null) {
        throw new FieldnameNotFoundException(name);
    }
    ((DynaBean) obj).set(name, value);
}
 
Example 26
Source Project: reflectutils   Source File: DynaBeanAdapter.java    License: Apache License 2.0 5 votes vote down vote up
public List<String> getPropertyNames(Object bean) {
    List<String> names = new ArrayList<String>();
    DynaProperty origDescriptors[] =
        ((DynaBean) bean).getDynaClass().getDynaProperties();
    for (DynaProperty dynaProperty : origDescriptors) {
        String name = dynaProperty.getName();
        names.add(name);
    }
    return names;
}
 
Example 27
Source Project: astor   Source File: BeanListUnivariateImpl.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
  *  Creates a {@link org.apache.commons.beanutils.DynaBean} with a 
  *  {@link org.apache.commons.beanutils.DynaProperty} named 
  *  <code>propertyName,</code> sets the value of the property to <code>v</code>
  *  and adds the DynaBean to the underlying list.
  *
  */
public void addValue(double v)  {
    DynaProperty[] props = new DynaProperty[] {
            new DynaProperty(propertyName, Double.class)
    };
    BasicDynaClass dynaClass = new BasicDynaClass(null, null, props);
    DynaBean dynaBean = null;
    try {
        dynaBean = dynaClass.newInstance();
    } catch (Exception ex) {              // InstantiationException, IllegalAccessException
        throw new RuntimeException(ex);   // should never happen
    }
	dynaBean.set(propertyName, Double.valueOf(v));
	addObject(dynaBean);
}
 
Example 28
Source Project: astor   Source File: BeanListUnivariateImpl.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
  *  Creates a {@link org.apache.commons.beanutils.DynaBean} with a 
  *  {@link org.apache.commons.beanutils.DynaProperty} named 
  *  <code>propertyName,</code> sets the value of the property to <code>v</code>
  *  and adds the DynaBean to the underlying list.
  *
  */
public void addValue(double v)  {
    DynaProperty[] props = new DynaProperty[] {
            new DynaProperty(propertyName, Double.class)
    };
    BasicDynaClass dynaClass = new BasicDynaClass(null, null, props);
    DynaBean dynaBean = null;
    try {
        dynaBean = dynaClass.newInstance();
    } catch (Exception ex) {              // InstantiationException, IllegalAccessException
        throw new RuntimeException(ex);   // should never happen
    }
	dynaBean.set(propertyName, Double.valueOf(v));
	addObject(dynaBean);
}
 
Example 29
Source Project: astor   Source File: BeanListUnivariateImpl.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
  *  Creates a {@link org.apache.commons.beanutils.DynaBean} with a 
  *  {@link org.apache.commons.beanutils.DynaProperty} named 
  *  <code>propertyName,</code> sets the value of the property to <code>v</code>
  *  and adds the DynaBean to the underlying list.
  *
  */
public void addValue(double v)  {
    DynaProperty[] props = new DynaProperty[] {
            new DynaProperty(propertyName, Double.class)
    };
    BasicDynaClass dynaClass = new BasicDynaClass(null, null, props);
    DynaBean dynaBean = null;
    try {
        dynaBean = dynaClass.newInstance();
    } catch (Exception ex) {              // InstantiationException, IllegalAccessException
        throw new RuntimeException(ex);   // should never happen
    }
	dynaBean.set(propertyName, Double.valueOf(v));
	addObject(dynaBean);
}
 
Example 30
Source Project: astor   Source File: BeanListUnivariateImpl.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
  *  Creates a {@link org.apache.commons.beanutils.DynaBean} with a 
  *  {@link org.apache.commons.beanutils.DynaProperty} named 
  *  <code>propertyName,</code> sets the value of the property to <code>v</code>
  *  and adds the DynaBean to the underlying list.
  *
  */
public void addValue(double v)  {
    DynaProperty[] props = new DynaProperty[] {
            new DynaProperty(propertyName, Double.class)
    };
    BasicDynaClass dynaClass = new BasicDynaClass(null, null, props);
    DynaBean dynaBean = null;
    try {
        dynaBean = dynaClass.newInstance();
    } catch (Exception ex) {              // InstantiationException, IllegalAccessException
        throw new RuntimeException(ex);   // should never happen
    }
	dynaBean.set(propertyName, Double.valueOf(v));
	addObject(dynaBean);
}