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:org.specrunner.plugins.core.objects.AbstractPluginObject.java

/**
 * If there is some plugin of the object type specified in field, use the
 * plugin object version./*from w  w w .j  a  va  2 s .c  om*/
 * 
 * @param instance
 *            The object instance.
 * @param f
 *            The field metadata.
 * @param value
 *            The value to be set.
 * @throws Exception
 *             On set errors.
 */
protected void setEntity(Object instance, Field f, Object value) throws Exception {
    if (UtilLog.LOG.isDebugEnabled()) {
        UtilLog.LOG.debug("LOOKUP(" + f.getSpecificType() + ")");
    }
    Object obj = SRServices.getObjectManager().get(f.getSpecificType(), String.valueOf(value));
    if (UtilLog.LOG.isDebugEnabled()) {
        UtilLog.LOG.debug("FOUND(" + obj + ")");
    }
    PropertyUtils.setProperty(instance, f.getFullName(), obj);
}

From source file:org.specrunner.plugins.core.objects.AbstractPluginObject.java

/**
 * Sets an object to a field.//from   ww w . ja  v a  2  s  .  co  m
 * 
 * @param instance
 *            The object instance.
 * @param f
 *            The field information.
 * @param value
 *            The value to be set.
 * @throws Exception
 *             On setting errors.
 */
protected void setObject(Object instance, Field f, Object value) throws Exception {
    if (UtilLog.LOG.isDebugEnabled()) {
        UtilLog.LOG.debug("OBJECT(" + f.getSpecificType() + ")");
    }
    PropertyUtils.setProperty(instance, f.getFullName(), value);
}

From source file:org.springside.modules.orm.hibernate.HibernateUtils.java

/**
 * ?ID?,???.//from w  ww .  j  a  v  a2 s  .c  o  m
 * 
 * ??????id,??????id?????.
 * ???id??,??id??.
 * ?ID, ??cascade-save-or-update??.
 * 
 * @param srcObjects ??,.
 * @param checkedIds  ?,ID.
 * @param clazz  ?
 * @param idName ??
 */
public static <T, ID> void mergeByCheckedIds(final Collection<T> srcObjects, final Collection<ID> checkedIds,
        final Class<T> clazz, final String idName) {

    //?
    Assert.notNull(srcObjects, "scrObjects?");
    Assert.hasText(idName, "idName?");
    Assert.notNull(clazz, "clazz?");

    //?, ???.
    if (checkedIds == null) {
        srcObjects.clear();
        return;
    }

    //????,id?ID?,.
    //?,???id,?id???id.
    Iterator<T> srcIterator = srcObjects.iterator();
    try {

        while (srcIterator.hasNext()) {
            T element = srcIterator.next();
            Object id;
            id = PropertyUtils.getProperty(element, idName);

            if (!checkedIds.contains(id)) {
                srcIterator.remove();
            } else {
                checkedIds.remove(id);
            }
        }

        //ID??id????,,id??.
        for (ID id : checkedIds) {
            T obj = clazz.newInstance();
            PropertyUtils.setProperty(obj, idName, id);
            srcObjects.add(obj);
        }
    } catch (Exception e) {
        throw ReflectionUtils.convertReflectionExceptionToUnchecked(e);
    }
}

From source file:org.squale.jraf.provider.persistence.hibernate.BlobHelper.java

/**
 * Creation d'un BLOB Oracle vide//  w  ww.  j  av a2s.  c  o  m
 * @param s session de persistance
 * @param bo business object
 * @param property nom de la propriete
 * @return 
 * @throws JrafPersistenceException
 */
private final static void createEmptyBlob(Session s, Object bo, String property)
        throws JrafPersistenceException {

    try {
        // on fixe le BLOB vide
        PropertyUtils.setProperty(bo, property, oracle.sql.BLOB.empty_lob());
        // on sauve le BO
        s.save(bo);
        s.flush();
        s.connection().commit();

    } catch (Exception e) {
        // on transforme l'exception
        log.error("Erreur lors de l'ecriture du BLOB vide", e);
        throw new JrafPersistenceException("Erreur lors de l'ecriture du BLOB vide", e);
    }
}

From source file:org.squale.welcom.struts.bean.JCryptable.java

/**
 * Crypte les attributs crypts d'un Bean Date de cration : (07/10/2002 15:57:23)
 * /*from  w  w  w . j a  v  a 2s .c o m*/
 * @param conn la connection
 * @throws JCryptableException exception pouvant etre levee
 */
public void crypte(final java.sql.Connection conn) throws JCryptableException {
    try {
        final int nbAttr = cryptableAttr.length;

        // Pour chaque attr  crypter, appelle la procdure stocke de cryptage auprs de la BDD
        for (int i = 0; i < nbAttr; i++) {
            final String attr = cryptableAttr[i];

            final String value = (String) PropertyUtils.getProperty(this, attr);

            if ((value != null) && (value.length() > 0)) {
                final CallableStatement cs = conn.prepareCall("{? = call crypte_FCT(?)}");
                cs.registerOutParameter(1, java.sql.Types.VARCHAR);
                cs.setString(2, value);

                cs.executeUpdate();
                PropertyUtils.setProperty(this, attr, cs.getString(1));
                cs.close();
            }
        }
    } catch (final SQLException ex) {
        throw new JCryptableException(ex.getMessage());
    } catch (final IllegalAccessException ex) {
        throw new JCryptableException(ex.getMessage());
    } catch (final InvocationTargetException ex) {
        throw new JCryptableException(ex.getMessage());
    } catch (final NoSuchMethodException ex) {
        throw new JCryptableException(ex.getMessage());
    }
}

From source file:org.squale.welcom.struts.bean.JCryptable.java

/**
 * Decrypte les attributs crypts d'un Bean Date de cration : (07/10/2002 15:57:13)
 * //w  w w .  j  a v  a 2  s  .  co  m
 * @param conn la connection
 * @throws JCryptableException exception pouvant etre levee
 */
public void decrypte(final java.sql.Connection conn) throws JCryptableException {
    try {
        if (cryptableAttr != null) {
            final int nbAttr = cryptableAttr.length;

            // Pour chaque attr crypt, appelle la procdure stocke de dcryptage auprs de la BDD
            for (int i = 0; i < nbAttr; i++) {
                final String attr = cryptableAttr[i];

                final String value = (String) PropertyUtils.getProperty(this, attr);

                if ((value != null) && (value.length() > 0)) {
                    final CallableStatement cs = conn.prepareCall("{? = call decrypte_FCT(?)}");
                    cs.registerOutParameter(1, java.sql.Types.VARCHAR);
                    cs.setString(2, value);

                    cs.executeUpdate();
                    PropertyUtils.setProperty(this, attr, cs.getString(1));
                    cs.close();
                }
            }
        }
    } catch (final SQLException ex) {
        throw new JCryptableException(ex.getMessage());
    } catch (final IllegalAccessException ex) {
        throw new JCryptableException(ex.getMessage());
    } catch (final InvocationTargetException ex) {
        throw new JCryptableException(ex.getMessage());
    } catch (final NoSuchMethodException ex) {
        throw new JCryptableException(ex.getMessage());
    }
}

From source file:org.squale.welcom.taglib.field.util.LayoutUtils.java

/**
 * Get the property 'property' of the bean 'bean' Handle classic exception
 * /*ww w  .ja  va2  s .  c o  m*/
 * @param bean le bean
 * @param property le nom de la property
 * @param value la value a setter
 * @throws JspException exception pouvant etre levee
 */
public static void setProperty(final Object bean, final String property, final Object value)
        throws JspException {
    if (property != null) {
        try {
            PropertyUtils.setProperty(bean, property, value);
        } catch (final IllegalAccessException e) {
            throw new JspException(messages.getMessage("getter.access", property, name));
        } catch (final InvocationTargetException e) {
            final Throwable t = e.getTargetException();
            throw new JspException(messages.getMessage("getter.result", property, t.toString()));
        } catch (final NoSuchMethodException e) {
            throw new JspException(messages.getMessage("getter.method", property, name));
        }
    }
}

From source file:org.sylvani.bot.util.SessionPropertyModel.java

@Override
public void setObject(T object, ISession session) {
    Object target = session.getAttribute(name);
    try {//from   w w w  .  ja  va 2  s  .c o  m
        PropertyUtils.setProperty(target, name, object);
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:org.talend.commons.utils.data.reflection.ReflectUtils.java

public static void setValue(Object object, String property, Object value) {
    try {//from  w  w  w  .jav a  2 s .  co  m
        PropertyUtils.setProperty(object, property, value);
    } catch (IllegalAccessException e) {
        throw new ReflectionPropertyException(object != null ? object.getClass() : null, property, false, e);
    } catch (InvocationTargetException e) {
        throw new ReflectionPropertyException(object != null ? object.getClass() : null, property, false, e);
    } catch (NoSuchMethodException e) {
        throw new ReflectionPropertyException(object != null ? object.getClass() : null, property, false, e);
    } catch (IllegalArgumentException e) {
        throw new ReflectionPropertyException(object != null ? object.getClass() : null, property, false, e);
    }
}

From source file:org.thiesen.helenaorm.HelenaDAO.java

private T applyColumns(final String key, final Iterable<Column> slice) {
    try {/*w  w  w . j  av a  2  s.c  om*/
        final T newInstance = _clz.newInstance();

        PropertyUtils.setProperty(newInstance, _keyPropertyDescriptor.getName(),
                _typeConverter.convertByteArrayToValueObject(
                        _keyPropertyDescriptor.getReadMethod().getReturnType(),
                        _typeConverter.stringToBytes(key)));

        for (final Column c : slice) {
            final String name = _typeConverter.bytesToString(c.name);
            if (PropertyUtils.isWriteable(newInstance, name)) {
                final PropertyDescriptor propertyDescriptor = PropertyUtils.getPropertyDescriptor(newInstance,
                        name);
                final Class<?> returnType = propertyDescriptor.getReadMethod().getReturnType();
                PropertyUtils.setProperty(newInstance, name,
                        _typeConverter.convertByteArrayToValueObject(returnType, c.value));
            }
        }

        return newInstance;

    } catch (final InstantiationException e) {
        throw new HelenaRuntimeException("Could not instanciate " + _clz.getName(), e);
    } catch (final IllegalAccessException e) {
        throw new HelenaRuntimeException("Could not instanciate " + _clz.getName(), e);
    } catch (final InvocationTargetException e) {
        throw new HelenaRuntimeException(e);
    } catch (final NoSuchMethodException e) {
        throw new HelenaRuntimeException(e);
    }
}