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

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

Introduction

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

Prototype

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

Source Link

Document

Set the value of the specified simple property of the specified bean, with no type conversions.

For more details see PropertyUtilsBean.

Usage

From source file:de.cbb.mplayer.mapping.MappingUtil.java

private static void mapFieldToEntity(Field field, Object presenter, Object entity) {
    try {/*from www .ja  v  a 2 s  .  c om*/
        if (!Control.class.isAssignableFrom(field.getType()))
            return;
    } catch (Exception ex) {
        log.debug("mapFieldToEntity failed", ex);
    }
    try {
        String fieldname = field.getName();
        field.setAccessible(true);
        Object value1 = null;

        ValueWrapper wrapper = MappingFactory.buildValueWrapper(presenter, field, value1, entity, fieldname);
        if (wrapper == null)
            return;
        value1 = wrapper.getValue();

        PropertyUtils.setSimpleProperty(entity, fieldname, value1);
    } catch (Exception ex) {
        log.debug("Unmapped control: " + field.getName() + " [" + ex.toString() + "]");
    }
}

From source file:de.iritgo.simplelife.bean.BeanTools.java

/**
 * Copy all attributes from the given map to the given bean
 * /*from  w w  w .  java 2 s.c  o  m*/
 * @param map The map The map
 * @param object The bean The bean
 */
static public void copyMap2Bean(Map<String, Object> map, Object object) {
    for (String name : map.keySet()) {
        if (PropertyUtils.isWriteable(object, name)) {
            try {
                PropertyUtils.setSimpleProperty(object, name, map.get(name));
            } catch (Exception ignore) {
            }
        }
    }
}

From source file:net.sourceforge.fenixedu.applicationTier.Servico.manager.TransferDomainObjectProperty.java

@Atomic
public static void run(DomainObject srcObject, DomainObject dstObject, String slotName)
        throws FenixServiceException {
    check(RolePredicates.MANAGER_PREDICATE);
    try {/*from  w  ww. j av  a 2s .c o m*/
        Object srcProperty = PropertyUtils.getSimpleProperty(srcObject, slotName);

        if (srcProperty != null && srcProperty instanceof Collection) {
            Collection srcCollection = (Collection) srcProperty;

            Object dstProperty = PropertyUtils.getSimpleProperty(dstObject, slotName);
            if (dstProperty instanceof Collection) {
                Collection dstCollection = (Collection) dstProperty;
                dstCollection.addAll(srcCollection);
            }

        } else {
            PropertyUtils.setSimpleProperty(dstObject, slotName, srcProperty);
        }
    } catch (InvocationTargetException e) {
        if (e.getTargetException() != null) {
            if (e.getTargetException() instanceof WriteOnReadError) {
                throw ((WriteOnReadError) e.getTargetException());
            }
            throw new FenixServiceException(e.getTargetException());
        }
        throw new FenixServiceException(e);
    } catch (IllegalAccessException e) {
        throw new FenixServiceException(e);
    } catch (NoSuchMethodException e) {
        throw new FenixServiceException(e);
    }

}

From source file:com.rolex.explore.beanutils.service.BeanUtilsSpecificService.java

public void exploreBeanUtil() {

    SampleBean bean = new SampleBean();

    String property1 = "name";

    String property2 = "currentAddress.city";

    String property3 = "previousAddresses[0].city";

    String property4 = "previousAddresses[3].city";

    String property5 = "vehicleLicenseModel(R60)";

    Place place1 = new Place("Sentosa", "Singapore");
    Place place2 = new Place("Colombo", "Sri Lanka");
    List<Place> places = new ArrayList<Place>();
    places.add(place1);/*www.j a v  a2 s  .c o  m*/
    places.add(place2);

    String property6 = "yearlyPlacesVisited(2000)";

    String property7 = "placesVisited";

    String property8 = "placesVisited[0]";

    TourismAward award = new TourismAward("World Award Committee", "USA");

    String property9 = "yearlyPlacesVisited(2000)[0].tourismAwards[0]";

    try {
        PropertyUtils.setSimpleProperty(bean, property1, "Rolex Rolex");
        String value1 = (String) PropertyUtils.getSimpleProperty(bean, property1);
        System.out.println("###Reverse1:   " + value1);

        PropertyUtils.setNestedProperty(bean, property2, "Hoffman Estates");
        String value2 = (String) PropertyUtils.getNestedProperty(bean, property2);
        System.out.println("###Reverse2:   " + value2);

        PropertyUtils.setNestedProperty(bean, property3, "Schaumburg");
        String value3 = (String) PropertyUtils.getNestedProperty(bean, property3);
        System.out.println("###Reverse3:   " + value3);

        PropertyUtils.setNestedProperty(bean, property4, "Des Plaines");
        String value4 = (String) PropertyUtils.getNestedProperty(bean, property4);
        System.out.println("###Reverse4:   " + value4);

        Address[] arrayValue1 = (Address[]) PropertyUtils.getSimpleProperty(bean, "previousAddresses");
        System.out.println("###ReverseArray:   " + Arrays.toString(arrayValue1));

        PropertyUtils.setMappedProperty(bean, property5, "Sonata");
        String value5 = (String) PropertyUtils.getMappedProperty(bean, property5);
        System.out.println("###Reverse5:   " + value5);

        PropertyUtils.setMappedProperty(bean, property6, places);
        List<Place> value6 = (List<Place>) PropertyUtils.getMappedProperty(bean, property6);
        System.out.println("###Reverse6:   " + value6.get(0));

        PropertyUtils.setSimpleProperty(bean, property7, places);
        List<Place> value7 = (List<Place>) PropertyUtils.getSimpleProperty(bean, property7);
        System.out.println("###Reverse7:   " + value7.get(0));

        PropertyUtils.setIndexedProperty(bean, property8, place2);
        Place value8 = (Place) PropertyUtils.getIndexedProperty(bean, property8);
        System.out.println("###Reverse8:   " + value8);

        PropertyUtils.setNestedProperty(bean, property9, award);
        TourismAward value9 = (TourismAward) PropertyUtils.getNestedProperty(bean, property9);
        System.out.println("###Reverse8:   " + value8);

    } 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:de.stefanteitge.kwery.internal.Entity.java

@Override
public <T> T beanify(Class<T> clazz) throws KweryException {
    try {//www .ja v a  2s .c  om
        T bean = clazz.newInstance();

        for (String column : getTable().getColumns()) {
            PropertyUtils.setSimpleProperty(bean, column, getValue(column));
        }

        return bean;
    } catch (InstantiationException e) {
        throw new KweryException("Beanify failed", e);
    } catch (IllegalAccessException e) {
        throw new KweryException("Beanify failed", e);
    } catch (NoSuchMethodException e) {
        throw new KweryException("Beanify failed", e);
    } catch (InvocationTargetException e) {
        throw new KweryException("Beanify failed", e);
    }
}

From source file:com.jythonui.server.RUtils.java

public static <T extends RMap> void toEProperties(String[] prop, Object dest, T sou) {
    for (String key : prop) {
        String val = sou.getAttr(key);
        try {//from  w  w  w .  java2s  .c  om
            PropertyUtils.setSimpleProperty(dest, key, val);
        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
            errorMess(L(), IErrorCode.ERRORCODE111, ILogMess.BEANCANNOTSETPROPERTY, key, val);
        }
    }
}

From source file:com.eviware.soapui.support.ModelItemPropertyEditorModel.java

public void setEditorText(String text) {
    try {/*from   ww w  .ja v  a 2  s. com*/
        PropertyUtils.setSimpleProperty(modelItem, propertyName, text);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.qrmedia.commons.persistence.hibernate.clone.property.CloneablePropertyCloner.java

@Override
protected <T> boolean cloneNonNullValue(Object source, Object target, String propertyName, Object propertyValue,
        HibernateEntityGraphCloner entityGraphCloner) throws IllegalArgumentException {

    if (propertyValue instanceof Cloneable) {

        try {//w  ww .ja v a 2  s .  c  o  m
            PropertyUtils.setSimpleProperty(target, propertyName, clone((Cloneable) propertyValue));
            return true;
        } catch (Exception exception) {
            throw new IllegalArgumentException("Unable to set property '" + propertyName + "' on " + target
                    + " due to " + exception.getClass().getSimpleName() + ": " + exception.getMessage());
        }

    }

    // other properties can't be handled by this cloner
    return false;
}

From source file:fr.mtlx.odm.ClassAssistant.java

public void setIdentifier(Object object, Name value) {
    String identifier = metadata.getIdentifierPropertyName();

    try {/*from w ww. ja  v  a 2s . c  om*/
        PropertyUtils.setSimpleProperty(object, identifier, value);
    } catch (Exception e) {
        throw new UnsupportedOperationException(e);
    }
}

From source file:net.orpiske.ssps.common.db.SimpleRsHandler.java

@Override
public T handle(ResultSet rs) throws SQLException {

    // No records to handle :O
    if (!rs.next()) {
        return null;
    }/*  w ww.ja  v  a 2  s .co  m*/

    ResultSetMetaData meta = rs.getMetaData();

    for (int i = 1; i <= meta.getColumnCount(); i++) {
        Object value = rs.getObject(i);
        String name = meta.getColumnName(i);

        try {
            /*
             * We convert the column name to a more appropriate and java like name 
             * because some columns are usually named as some_thing whereas Java 
             * properties are named someThing. This call does this conversion.
             */
            String javaProperty = NameConverter.sqlToProperty(name);

            PropertyUtils.setSimpleProperty(dto, javaProperty, value);
        } catch (Exception e) {
            throw new SQLException("Unable to set property " + name + " for bean" + dto.getClass(), e);
        }
    }

    return dto;
}