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:com.khubla.cbean.serializer.impl.json.JSONDateFieldSerializer.java

@Override
public void deserialize(Object o, Field field, String value) throws SerializerException {
    try {/*from  w ww  . j  a  v a2  s  .  com*/
        final Long l = Long.parseLong(value);
        final Date date = new Date(l);
        PropertyUtils.setProperty(o, field.getName(), date);
    } catch (final Exception e) {
        throw new SerializerException(e);
    }
}

From source file:it.geosolutions.geoserver.jms.impl.utils.BeanUtils.java

/**
 * This is a 'smart' (perform checks for some special cases) update function which should be used to copy of the properties for objects
 * of the catalog and configuration.//ww w . j  a  va2 s.  c  om
 * 
 * @param <T> the type of the bean to update
 * @param info the bean instance to update
 * @param properties the list of string of properties to update
 * @param values the list of new values to update
 * 
 * @throws IllegalAccessException
 * @throws InvocationTargetException
 * @throws NoSuchMethodException
 */
public static <T> void smartUpdate(final T info, final List<String> properties, final List<Object> values)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    final Iterator<String> itPropertyName = properties.iterator();
    final Iterator<Object> itValue = values.iterator();
    while (itPropertyName.hasNext() && itValue.hasNext()) {
        String propertyName = itPropertyName.next();
        final Object value = itValue.next();

        PropertyDescriptor pd = PropertyUtils.getPropertyDescriptor(info, propertyName);
        // return null if there is no such descriptor
        if (pd == null) {
            // this is a special case used by the NamespaceInfoImpl setURI
            // the propertyName coming from the ModificationProxy is set to 'uRI'
            // lets set it to uri
            propertyName = propertyName.toUpperCase();
            pd = PropertyUtils.getPropertyDescriptor(info, propertyName);
            if (pd == null) {
                return;
            }
        }
        if (pd.getWriteMethod() != null) {
            PropertyUtils.setProperty(info, propertyName, value);
        } else {
            // T interface do not declare setter method for this property
            // lets use getter methods to get the property reference
            final Object property = PropertyUtils.getProperty(info, propertyName);

            // check type of property to apply new value
            if (Collection.class.isAssignableFrom(pd.getPropertyType())) {
                final Collection<?> liveCollection = (Collection<?>) property;
                liveCollection.clear();
                liveCollection.addAll((Collection) value);
            } else if (Map.class.isAssignableFrom(pd.getPropertyType())) {
                final Map<?, ?> liveMap = (Map<?, ?>) property;
                liveMap.clear();
                liveMap.putAll((Map) value);
            } else {
                if (CatalogUtils.LOGGER.isLoggable(java.util.logging.Level.SEVERE))
                    CatalogUtils.LOGGER.severe("Skipping unwritable property " + propertyName
                            + " with property type " + pd.getPropertyType());
            }
        }
    }
}

From source file:com.infinities.skyport.diff.PatchUtil.java

private static <T, E> void patch(List<Diff<?>> diffs, T destination) throws IllegalAccessException,
        InvocationTargetException, NoSuchMethodException, SecurityException, ClassNotFoundException {
    for (Diff<?> diff : diffs) {
        PropertyUtils.setProperty(destination, diff.getFieldName(), diff.getRight());
    }//  ww  w  . j a  v a  2  s. c  om
}

From source file:com.blackbear.flatworm.PropertyUtilsMappingStrategy.java

@Override
public void mapBean(Object bean, String beanName, String property, Object value,
        Map<String, ConversionOption> conv) throws FlatwormConversionException {
    try {// w  ww. j  ava2 s  . c o m
        ConversionOption option = conv.get("append");
        if (option != null && "true".equalsIgnoreCase(option.getValue())) {
            Object currentValue = PropertyUtils.getProperty(bean, property);
            if (currentValue != null)
                value = currentValue.toString() + value;
        }
        PropertyUtils.setProperty(bean, property, value);
    } catch (IllegalAccessException e) {
        log.error("While running set property method for " + beanName + "." + property + "with value '" + value
                + "'", e);
        throw new FlatwormConversionException("Setting field " + beanName + "." + property);
    } catch (InvocationTargetException e) {
        log.error("While running set property method for " + beanName + "." + property + "with value '" + value
                + "'", e);
        throw new FlatwormConversionException("Setting field " + beanName + "." + property);
    } catch (NoSuchMethodException e) {
        log.error("While running set property method for " + beanName + "." + property + "with value '" + value
                + "'", e);
        throw new FlatwormConversionException("Setting field " + beanName + "." + property);
    }
}

From source file:com.mycollab.db.persistence.service.DefaultCrudService.java

@Override
public Integer saveWithSession(T record, String username) {
    if (!StringUtils.isBlank(username)) {
        try {//from w w  w. j  av  a  2  s . c o m
            PropertyUtils.setProperty(record, "createduser", username);
        } catch (Exception e) {
        }
    }

    try {
        PropertyUtils.setProperty(record, "createdtime", new GregorianCalendar().getTime());
        PropertyUtils.setProperty(record, "lastupdatedtime", new GregorianCalendar().getTime());
    } catch (Exception e) {
    }

    getCrudMapper().insertAndReturnKey(record);
    try {
        return (Integer) PropertyUtils.getProperty(record, "id");
    } catch (Exception e) {
        return 0;
    }
}

From source file:com.nec.nsgui.action.cifs.CommonUtil.java

static public void setNoContentMsgInObj(Object obj, String protertyName, MessageResources msgResources,
        HttpServletRequest request) throws Exception {
    Object objValue;/* ww w.j  a  v  a 2s  .  c  om*/
    try {
        objValue = PropertyUtils.getProperty(obj, protertyName);
    } catch (Exception e) {
        throw e;
    }
    if (objValue.toString().equals("")) {
        PropertyUtils.setProperty(obj, protertyName,
                msgResources.getMessage(request.getLocale(), "cifs.shareDetial.nocontent"));

    }
}

From source file:com.esofthead.mycollab.core.persistence.service.DefaultCrudService.java

@Override
public int saveWithSession(T record, String username) {
    if (!StringUtils.isBlank(username)) {
        try {/*from   ww w.  j  av  a  2  s .c  om*/
            PropertyUtils.setProperty(record, "createduser", username);
        } catch (Exception e) {

        }
    }

    try {
        PropertyUtils.setProperty(record, "createdtime", new GregorianCalendar().getTime());
        PropertyUtils.setProperty(record, "lastupdatedtime", new GregorianCalendar().getTime());
    } catch (Exception e) {
    }

    getCrudMapper().insertAndReturnKey(record);
    try {
        return (Integer) PropertyUtils.getProperty(record, "id");
    } catch (Exception e) {
        return 0;
    }
}

From source file:com.tobedevoured.modelcitizen.template.JavaBeanTemplate.java

public <T> T set(T model, String property, Object value) throws BlueprintTemplateException {
    try {//from  www.jav  a  2 s  . c o m
        PropertyUtils.setProperty(model, property, value);
    } catch (IllegalAccessException propertyException) {
        throw new BlueprintTemplateException(propertyException);
    } catch (InvocationTargetException propertyException) {
        throw new BlueprintTemplateException(propertyException);
    } catch (NoSuchMethodException propertyException) {
        throw new BlueprintTemplateException(propertyException);
    }

    return model;
}

From source file:com.eviware.soapui.model.propertyexpansion.MutablePropertyExpansionImpl.java

public void update() throws Exception {
    String rep = toString();/* w w  w  . j  a va  2  s .c  o  m*/

    // not changed
    if (stringRep.equals(rep))
        return;

    Object obj = PropertyUtils.getProperty(container, propertyName);
    if (obj == null)
        throw new Exception("property value is null");

    String str = obj.toString();
    int ix = str.indexOf(stringRep);
    if (ix == -1)
        throw new Exception("property expansion [" + stringRep + "] not found for update");

    while (ix != -1) {
        str = str.substring(0, ix) + rep + str.substring(ix + stringRep.length());
        ix = str.indexOf(stringRep, ix + rep.length());
    }

    PropertyUtils.setProperty(container, propertyName, str);

    stringRep = rep;
}

From source file:com.tonbeller.wcf.convert.SelectSingleConverter.java

protected void updateModelReference(Formatter fmt, Element elem, Object bean)
        throws FormatException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
    String model = SelectSingle.getModelReference(elem);
    if (model.length() == 0)
        return;// w  w w.  j  a v a  2s. c om

    String type = SelectSingle.getType(elem);
    String formatString = SelectSingle.getFormatString(elem);
    FormatHandler parser = fmt.getHandler(type);
    if (parser == null)
        throw new FormatException("no handler found for type: " + type);

    Element item = SelectSingle.getSelectedItem(elem);
    if (item == null)
        return;

    String valueString = Item.getValue(item);
    Object value = parser.parse(valueString, formatString);
    PropertyUtils.setProperty(bean, model, value);
}