Example usage for org.apache.commons.beanutils PropertyUtils setProperty

List of usage examples for org.apache.commons.beanutils PropertyUtils setProperty

Introduction

In this page you can find the example usage for org.apache.commons.beanutils PropertyUtils setProperty.

Prototype

public static void setProperty(Object bean, String name, Object value)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException 

Source Link

Document

Set the value of the specified property of the specified bean, no matter which property reference format is used, with no type conversions.

For more details see PropertyUtilsBean.

Usage

From source file:net.shopxx.entity.Goods.java

@Transient
public void setAttributeValue(Attribute attribute, String attributeValue) {
    if (attribute == null || attribute.getPropertyIndex() == null) {
        return;//from   www .  j av  a2  s. c  om
    }

    try {
        String propertyName = ATTRIBUTE_VALUE_PROPERTY_NAME_PREFIX + attribute.getPropertyIndex();
        PropertyUtils.setProperty(this, propertyName, attributeValue);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (InvocationTargetException e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (NoSuchMethodException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}

From source file:net.shopxx.entity.Goods.java

@Transient
public void removeAttributeValue() {
    for (int i = 0; i < ATTRIBUTE_VALUE_PROPERTY_COUNT; i++) {
        String propertyName = ATTRIBUTE_VALUE_PROPERTY_NAME_PREFIX + i;
        try {// w  w w .ja  v  a  2 s .  co  m
            PropertyUtils.setProperty(this, propertyName, null);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e.getMessage(), e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e.getMessage(), e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }
}

From source file:gr.abiss.calipso.domain.AbstractItem.java

public void setValue(Field.Name fieldName, Object value) {
    if (fieldName.getType() != 200) {
        try {/*  w w w.j a va  2  s  . com*/
            PropertyUtils.setProperty(this, fieldName.getText(), value);
        } catch (Exception e) {
            throw new RuntimeException("Could not set value for custom attribute " + fieldName, e);
        }
    }
}

From source file:com.sun.faces.config.ManagedBeanFactory.java

/**
 * <li><p>Call the property getter, if it exists.</p></li>
 * <p/>/*from   ww  w. j  a  v a  2 s . c om*/
 * <li><p>If the getter returns null or doesn't exist, create a
 * java.util.HashMap(), otherwise use the returned
 * java.util.Map.</p></li>
 * <p/>
 * <li><p>Add all entries defined by nested &lt;map-entry&gt;
 * elements in the order they are listed, converting key values
 * defined by nested &lt;key&gt; elements to the type defined by
 * &lt;key-class&gt; and entry values defined by nested
 * &lt;value&gt; elements to the type defined by
 * &lt;value-class&gt;. If &lt;key-class&gt; and/or
 * &lt;value-class&gt; are not defined, use the value as-is (i.e.,
 * as a java.lang.String). Add null for each &lt;null-value&gt;
 * element.</p></li>
 * <p/>
 * <li><p>If a new java.util.Map was created in step 2), set the
 * property by calling the setter method, or log an error if there
 * is no setter method.</p></li>
 */
private void setMapPropertiesIntoBean(Object bean, ManagedPropertyBean property) throws Exception {
    Map result = null;
    boolean getterIsNull = true;
    Class propertyType = null;
    String propertyName = property.getPropertyName();

    try {
        // see if there is a getter
        result = (Map) PropertyUtils.getProperty(bean, propertyName);
        getterIsNull = (null == result) ? true : false;

        propertyType = PropertyUtils.getPropertyType(bean, propertyName);
    } catch (NoSuchMethodException nsme) {
        // it's valid to not have a getter.
    }

    if (null != propertyType && !java.util.Map.class.isAssignableFrom(propertyType)) {
        throw new FacesException(Util.getExceptionMessageString(Util.MANAGED_BEAN_CANNOT_SET_MAP_PROPERTY_ID,
                new Object[] { propertyName, managedBean.getManagedBeanName() }));
    }

    if (getterIsNull) {
        result = new java.util.HashMap(property.getMapEntries().getMapEntries().length);
    }

    // at this point result contains the existing entries from the
    // bean, or no entries if the bean had no entries.  In any case,
    // we can proceed to add values from the config file.

    copyMapEntriesFromConfigToMap(property.getMapEntries(), result);

    if (getterIsNull) {
        PropertyUtils.setProperty(bean, propertyName, result);
    }

}

From source file:com.dp2345.entity.Shop.java

/**
 * //from  w ww .  j av a2  s  .com
 * 
 * @param storeAttribute
 *            
 * @param attributeValue
 *            
 */
@Transient
public void setAttributeValue(ShopAttribute storeAttribute, Object attributeValue) {
    if (storeAttribute != null) {
        if (attributeValue instanceof String && StringUtils.isEmpty((String) attributeValue)) {
            attributeValue = null;
        }
        if (storeAttribute.getType() == Type.name
                && (attributeValue instanceof String || attributeValue == null)) {
            setShopName((String) attributeValue);
        } else if (storeAttribute.getType() == Type.birth
                && (attributeValue instanceof Date || attributeValue == null)) {
            setBirth((Date) attributeValue);
        } else if (storeAttribute.getType() == Type.area
                && (attributeValue instanceof Area || attributeValue == null)) {
            setArea((Area) attributeValue);
        } else if (storeAttribute.getType() == Type.address
                && (attributeValue instanceof String || attributeValue == null)) {
            setAddress((String) attributeValue);
        } else if (storeAttribute.getType() == Type.zipCode
                && (attributeValue instanceof String || attributeValue == null)) {
            setZipCode((String) attributeValue);
        } else if (storeAttribute.getType() == Type.phone
                && (attributeValue instanceof String || attributeValue == null)) {
            setPhone((String) attributeValue);
        } else if (storeAttribute.getType() == Type.mobile
                && (attributeValue instanceof String || attributeValue == null)) {
            setMobile((String) attributeValue);
        } else if (storeAttribute.getType() == Type.checkbox
                && (attributeValue instanceof List || attributeValue == null)) {
            if (storeAttribute.getPropertyIndex() != null) {
                if (attributeValue == null || (storeAttribute.getOptions() != null
                        && storeAttribute.getOptions().containsAll((List<?>) attributeValue))) {
                    try {
                        String propertyName = ATTRIBUTE_VALUE_PROPERTY_NAME_PREFIX
                                + storeAttribute.getPropertyIndex();
                        PropertyUtils.setProperty(this, propertyName, JsonUtils.toJson(attributeValue));
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    } catch (NoSuchMethodException e) {
                        e.printStackTrace();
                    }
                }
            }
        } else {
            if (storeAttribute.getPropertyIndex() != null) {
                try {
                    String propertyName = ATTRIBUTE_VALUE_PROPERTY_NAME_PREFIX
                            + storeAttribute.getPropertyIndex();
                    PropertyUtils.setProperty(this, propertyName, attributeValue);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

From source file:com.dp2345.entity.Member.java

/**
 * /*from  w ww  .  ja va2 s.  c o m*/
 * 
 * @param memberAttribute
 *            
 * @param attributeValue
 *            
 */
@Transient
public void setAttributeValue(MemberAttribute memberAttribute, Object attributeValue) {
    if (memberAttribute != null) {
        if (attributeValue instanceof String && StringUtils.isEmpty((String) attributeValue)) {
            attributeValue = null;
        }
        if (memberAttribute.getType() == Type.name
                && (attributeValue instanceof String || attributeValue == null)) {
            setName((String) attributeValue);
        } else if (memberAttribute.getType() == Type.gender
                && (attributeValue instanceof Gender || attributeValue == null)) {
            setGender((Gender) attributeValue);
        } else if (memberAttribute.getType() == Type.birth
                && (attributeValue instanceof Date || attributeValue == null)) {
            setBirth((Date) attributeValue);
        } else if (memberAttribute.getType() == Type.area
                && (attributeValue instanceof Area || attributeValue == null)) {
            setArea((Area) attributeValue);
        } else if (memberAttribute.getType() == Type.address
                && (attributeValue instanceof String || attributeValue == null)) {
            setAddress((String) attributeValue);
        } else if (memberAttribute.getType() == Type.zipCode
                && (attributeValue instanceof String || attributeValue == null)) {
            setZipCode((String) attributeValue);
        } else if (memberAttribute.getType() == Type.phone
                && (attributeValue instanceof String || attributeValue == null)) {
            setPhone((String) attributeValue);
        } else if (memberAttribute.getType() == Type.mobile
                && (attributeValue instanceof String || attributeValue == null)) {
            setMobile((String) attributeValue);
        } else if (memberAttribute.getType() == Type.checkbox
                && (attributeValue instanceof List || attributeValue == null)) {
            if (memberAttribute.getPropertyIndex() != null) {
                if (attributeValue == null || (memberAttribute.getOptions() != null
                        && memberAttribute.getOptions().containsAll((List<?>) attributeValue))) {
                    try {
                        String propertyName = ATTRIBUTE_VALUE_PROPERTY_NAME_PREFIX
                                + memberAttribute.getPropertyIndex();
                        PropertyUtils.setProperty(this, propertyName, JsonUtils.toJson(attributeValue));
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    } catch (NoSuchMethodException e) {
                        e.printStackTrace();
                    }
                }
            }
        } else {
            if (memberAttribute.getPropertyIndex() != null) {
                try {
                    String propertyName = ATTRIBUTE_VALUE_PROPERTY_NAME_PREFIX
                            + memberAttribute.getPropertyIndex();
                    PropertyUtils.setProperty(this, propertyName, attributeValue);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

From source file:com.dp2345.entity.Shop.java

/**
 * // www .j  av  a2 s .c  o m
 */
@Transient
public void removeAttributeValue() {
    setBirth(null);
    setArea(null);
    setAddress(null);
    setZipCode(null);
    setPhone(null);
    setMobile(null);
    for (int i = 0; i < ATTRIBUTE_VALUE_PROPERTY_COUNT; i++) {
        String propertyName = ATTRIBUTE_VALUE_PROPERTY_NAME_PREFIX + i;
        try {
            PropertyUtils.setProperty(this, propertyName, null);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }
}

From source file:net.shopxx.entity.Merchant.java

/**
 * //from   w w  w. j a  va 2  s .  c om
 * 
 * @param merchantAttribute
 *            
 * @param attributeValue
 *            
 */
@Transient
public void setAttributeValue(MerchantAttribute merchantAttribute, Object attributeValue) {
    if (merchantAttribute != null) {
        if (attributeValue instanceof String && StringUtils.isEmpty((String) attributeValue)) {
            attributeValue = null;
        }
        if (merchantAttribute.getType() == Type.name
                && (attributeValue instanceof String || attributeValue == null)) {
            setName((String) attributeValue);
        } else if (merchantAttribute.getType() == Type.gender
                && (attributeValue instanceof Gender || attributeValue == null)) {
            setGender((Gender) attributeValue);
        } else if (merchantAttribute.getType() == Type.birth
                && (attributeValue instanceof Date || attributeValue == null)) {
            setBirth((Date) attributeValue);
        } else if (merchantAttribute.getType() == Type.area
                && (attributeValue instanceof Area || attributeValue == null)) {
            setArea((Area) attributeValue);
        } else if (merchantAttribute.getType() == Type.address
                && (attributeValue instanceof String || attributeValue == null)) {
            setAddress((String) attributeValue);
        } else if (merchantAttribute.getType() == Type.zipCode
                && (attributeValue instanceof String || attributeValue == null)) {
            setZipCode((String) attributeValue);
        } else if (merchantAttribute.getType() == Type.phone
                && (attributeValue instanceof String || attributeValue == null)) {
            setPhone((String) attributeValue);
        } else if (merchantAttribute.getType() == Type.mobile
                && (attributeValue instanceof String || attributeValue == null)) {
            setMobile((String) attributeValue);
        } else if (merchantAttribute.getType() == Type.checkbox
                && (attributeValue instanceof List || attributeValue == null)) {
            if (merchantAttribute.getPropertyIndex() != null) {
                if (attributeValue == null || (merchantAttribute.getOptions() != null
                        && merchantAttribute.getOptions().containsAll((List<?>) attributeValue))) {
                    try {
                        String propertyName = ATTRIBUTE_VALUE_PROPERTY_NAME_PREFIX
                                + merchantAttribute.getPropertyIndex();
                        PropertyUtils.setProperty(this, propertyName, JsonUtils.toJson(attributeValue));
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    } catch (NoSuchMethodException e) {
                        e.printStackTrace();
                    }
                }
            }
        } else {
            if (merchantAttribute.getPropertyIndex() != null) {
                try {
                    String propertyName = ATTRIBUTE_VALUE_PROPERTY_NAME_PREFIX
                            + merchantAttribute.getPropertyIndex();
                    PropertyUtils.setProperty(this, propertyName, attributeValue);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

From source file:com.dp2345.entity.Member.java

/**
 * //from w  w  w  .j  a  v a 2  s.  c om
 */
@Transient
public void removeAttributeValue() {
    setName(null);
    setGender(null);
    setBirth(null);
    setArea(null);
    setAddress(null);
    setZipCode(null);
    setPhone(null);
    setMobile(null);
    for (int i = 0; i < ATTRIBUTE_VALUE_PROPERTY_COUNT; i++) {
        String propertyName = ATTRIBUTE_VALUE_PROPERTY_NAME_PREFIX + i;
        try {
            PropertyUtils.setProperty(this, propertyName, null);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.sammyun.entity.Member.java

/**
 * // ww w  .  j  a v a  2  s.  c om
 * 
 * @param memberAttribute 
 * @param attributeValue 
 */
@Transient
public void setAttributeValue(MemberAttribute memberAttribute, Object attributeValue) {
    if (memberAttribute != null) {
        if (attributeValue instanceof String && StringUtils.isEmpty((String) attributeValue)) {
            attributeValue = null;
        } else if (memberAttribute.getType() == Type.gender
                && (attributeValue instanceof Gender || attributeValue == null)) {
            setGender((Gender) attributeValue);
        } else if (memberAttribute.getType() == Type.birth
                && (attributeValue instanceof Date || attributeValue == null)) {
            setBirth((Date) attributeValue);
        } else if (memberAttribute.getType() == Type.area
                && (attributeValue instanceof Area || attributeValue == null)) {
            setArea((Area) attributeValue);
        } else if (memberAttribute.getType() == Type.address
                && (attributeValue instanceof String || attributeValue == null)) {
            setAddress((String) attributeValue);
        } else if (memberAttribute.getType() == Type.zipCode
                && (attributeValue instanceof String || attributeValue == null)) {
            setZipCode((String) attributeValue);
        } else if (memberAttribute.getType() == Type.phone
                && (attributeValue instanceof String || attributeValue == null)) {
            setPhone((String) attributeValue);
        } else if (memberAttribute.getType() == Type.mobile
                && (attributeValue instanceof String || attributeValue == null)) {
            setMobile((String) attributeValue);
        } else if (memberAttribute.getType() == Type.checkbox
                && (attributeValue instanceof List || attributeValue == null)) {
            if (memberAttribute.getPropertyIndex() != null) {
                if (attributeValue == null || (memberAttribute.getOptions() != null
                        && memberAttribute.getOptions().containsAll((List<?>) attributeValue))) {
                    try {
                        String propertyName = ATTRIBUTE_VALUE_PROPERTY_NAME_PREFIX
                                + memberAttribute.getPropertyIndex();
                        PropertyUtils.setProperty(this, propertyName, JsonUtils.toJson(attributeValue));
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    } catch (NoSuchMethodException e) {
                        e.printStackTrace();
                    }
                }
            }
        } else {
            if (memberAttribute.getPropertyIndex() != null) {
                try {
                    String propertyName = ATTRIBUTE_VALUE_PROPERTY_NAME_PREFIX
                            + memberAttribute.getPropertyIndex();
                    PropertyUtils.setProperty(this, propertyName, attributeValue);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}