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.drools.informer.domain.DomainModelSupportTest.java

private void setBooleanProperty(String property, Boolean value) {
    try {//from   w  ww .j ava 2s. co m
        logger.debug("Setting " + property);
        Class<?> propertyClass = PropertyUtils.getPropertyType(data, property);
        Object v = DomainModelSupport.answerToObject(Question.QuestionType.TYPE_BOOLEAN, value, propertyClass);
        PropertyUtils.setProperty(data, property, v);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.drools.informer.domain.DomainModelSupportTest.java

private void setDateProperty(String property, Date value) {
    try {/*from  w  ww .j  a v  a 2 s. com*/
        logger.debug("Setting " + property);
        Class<?> propertyClass = PropertyUtils.getPropertyType(data, property);
        Object v = DomainModelSupport.answerToObject(Question.QuestionType.TYPE_DATE, value, propertyClass);
        PropertyUtils.setProperty(data, property, v);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.drools.informer.domain.DomainModelSupportTest.java

private void setListProperty(String property, String value) {
    try {//from   w  ww. j a  v a2s. c o m
        logger.debug("Setting " + property);
        Class<?> propertyClass = PropertyUtils.getPropertyType(data, property);
        Object v = DomainModelSupport.answerToObject(Question.QuestionType.TYPE_LIST, value, propertyClass);
        PropertyUtils.setProperty(data, property, v);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.equilibriums.aop.utils.interceptor.convert.ConvertMethodArgumentInterceptor.java

private void convert(Object[] args, Integer fromArgumentIndex, String fromPropertyPath, Integer toArgumentIndex,
        String toPropertyPath) {// ww  w .ja  va2s .  co  m
    Object convertedValue = getConvertedValue(args, fromArgumentIndex, fromPropertyPath);

    if (StringUtils.isBlank(toPropertyPath)) {
        args[toArgumentIndex] = convertedValue;
    } else {
        try {
            PropertyUtils.setProperty(args[toArgumentIndex], toPropertyPath, convertedValue);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

From source file:org.equilibriums.aop.utils.interceptor.convert.ConvertMethodReturnValueInterceptor.java

private Object convert(Object returnValue, String fromPropertyPath, String toPropertyPath) {
    Object convertedValue = getConvertedValue(returnValue, fromPropertyPath);

    if (StringUtils.isBlank(toPropertyPath))
        return convertedValue;

    try {/*from   w ww  .j  a  v  a2 s  . co  m*/
        PropertyUtils.setProperty(returnValue, toPropertyPath, convertedValue);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    return returnValue;
}

From source file:org.exoplatform.container.xml.ObjectParam.java

private void populateBean() {
    Property prop = null;//ww  w  .  j av  a2 s  .  c  o m
    try {
        Class<?> clazz = ClassLoading.forName(type, this);
        object_ = clazz.newInstance();
        for (int i = 0; i < properties_.size(); i++) {
            prop = (Property) properties_.get(i);
            if (prop.name.endsWith("]")) {
                // arrary or list
                populateBeanInArray(object_, prop.name, prop.value);
            } else {
                Object valueBean = getValue(prop.value);
                PropertyUtils.setProperty(object_, prop.name, valueBean);
            }
        }
    } catch (Exception ex) {
        // if(prop != null) {
        // S ystem.out.println("Exception when try setting the prop.name " +
        // prop.name_ +
        // ", value prop.value " + prop.value_) ;
        // }
        LOG.error(ex.getLocalizedMessage(), ex);
    }
}

From source file:org.firebrandocm.dao.AbstractPersistenceFactory.java

private void createKeysIfNeeded(Set<Object> processed, Object... entities)
        throws InvocationTargetException, NoSuchMethodException, IllegalAccessException {
    for (Object entity : entities) {
        if (!processed.contains(entity)) {
            processed.add(entity);//from ww  w  .  j a va  2 s  .c o m
            String key = getKey(entity);
            if (key == null) {
                key = ObjectUtils.newTimeUuid().toString();
            }
            ClassMetadata<?> metadata = getClassMetadata(entity.getClass());
            PropertyUtils.setProperty(entity, metadata.getKeyProperty(), key);
            for (String mappedProperty : metadata.getMappedProperties()) {
                Object mappedEntity = PropertyUtils.getProperty(entity, mappedProperty);
                if (mappedEntity != null) {
                    if (Collection.class.isAssignableFrom(mappedEntity.getClass())) {
                        Collection<?> nestedEntities = (Collection) mappedEntity;
                        createKeysIfNeeded(processed, nestedEntities.toArray());
                    } else {
                        createKeysIfNeeded(processed, mappedEntity);
                    }
                }
            }
        }
    }
}

From source file:org.firebrandocm.dao.AbstractPersistenceFactory.java

/**
 * Instantiates null containers if an association is gonna take place on a mapped property
 *
 * @param metadata the class metadata for the affected entity
 * @param instance the entity/*w  ww  .  ja v  a 2s  .c  o  m*/
 * @param name     the mapped property
 * @param <T>      the type of entity
 */
protected <T> void instantiateContainersIfNecessary(ClassMetadata<?> metadata, T instance, String name)
        throws InvocationTargetException, NoSuchMethodException, IllegalAccessException,
        ClassNotFoundException {
    Object target = instance;
    String path = "";
    String[] properties = name.split("\\.");
    for (String property : properties) {
        path = StringUtils.isNotBlank(path) ? path + "." + property : property;
        if (metadata.isContainer(path)) {
            Object value = PropertyUtils.getProperty(target, property);
            if (value == null) {
                Class<?> typeClass = metadata.getPropertiesTypesMap().get(path);
                value = getInstance(typeClass);
                PropertyUtils.setProperty(target, property, value);
            }
            target = value;
        }
    }
}

From source file:org.firebrandocm.dao.impl.hector.HectorPersistenceFactory.java

/**
 * Loads a lazy property's value/*  ww w.ja v a 2  s.  c om*/
 *
 * @param metadata the entity metadata
 * @param self     the entity instance
 * @param proceed  the method being intercepted
 * @param m
 * @param args     the method arguments
 */
@Override
protected <T> void loadLazyPropertyIfNecessary(ClassMetadata<T> metadata, Object self, Method proceed, Method m,
        Object[] args) throws Exception {
    Object value = proceed.invoke(self, args);
    String key = getKey(self);
    if (key != null) { //key may be null if this is just a regular access to the property before the entity has been persisted and no key has been assigned
        SliceQuery<String, String, Object> query = getSliceQuery(metadata);
        query.setColumnFamily(metadata.getColumnFamily());
        query.setKey(key);
        String column = metadata.getLazyProperty(m);
        query.setColumnNames(column);
        List<HColumn<String, Object>> columns = query.execute().get().getColumns();
        HColumn<String, Object> mappedColumnValue = columns.size() == 1 ? columns.get(0) : null;
        if (mappedColumnValue != null && isEmptyContainerValue(value)) { //todo once a load attempt has been made we should not attempt again but we have no sessions...perhaps a weakreference map?
            Object propertyValue = loadProperty(metadata, column, mappedColumnValue);
            PropertyUtils.setProperty(self, column, propertyValue);
        }
    }
}

From source file:org.firebrandocm.dao.impl.hector.HectorPersistenceFactory.java

/**
 * Private helper that updates a simple column
 *
 * @param mutator  the operation mutator
 * @param key      the column key//from  w  ww  .java 2 s . c  o m
 * @param metadata the class metadata associated to the entity
 * @param property the property represented by this column
 * @param value    the value
 * @param entity   the entity the property belongs to
 */
private void updateSimpleColumn(Mutator<String> mutator, String key, ClassMetadata metadata, String property,
        Object value, Object entity)
        throws InvocationTargetException, NoSuchMethodException, IllegalAccessException, IOException {
    String colFamily = metadata.getColumnFamily();
    if (!metadata.isAssociationContainer(property)) { //association container properties are ignored since their embedded properties are flatten into the column family
        if (value == null) {
            if (!metadata.isContainer(property)) { //mapped properties are removed when loading a property with id that returns null
                fireColumnEvent(Event.Column.PRE_COLUMN_DELETION, entity, property, key, colFamily, null);
                mutator.addDeletion(key, colFamily, property, StringSerializer.get());
                log.debug(String.format("\tD: %s", property));
                fireColumnEvent(Event.Column.POST_COLUMN_DELETION, entity, property, key, colFamily, null);
            }
        } else {
            if (metadata.isCounterIncreaseProperty(property)) { //a counter property increase
                Long counterIncreaseValue = (Long) value;
                //if (counterIncreaseValue != 0) {
                String targetCounterProperty = metadata.getTargetCounterProperty(property);
                fireColumnEvent(Event.Column.PRE_COUNTER_MUTATION, entity, property, key, colFamily, null);
                mutator.incrementCounter(key, colFamily, targetCounterProperty, counterIncreaseValue);
                log.debug(String.format("C: %s increments to %d", targetCounterProperty, counterIncreaseValue));
                //once applied the increase the counter increase value gets reset to 0
                PropertyUtils.setProperty(entity, property, 0L);
                fireColumnEvent(Event.Column.POST_COUNTER_MUTATION, entity, property, key, colFamily, null);
                //}
            } else if (!metadata.isCounterProperty(property)) { //a regular column update, counter are ignored since they're just serialized
                HColumn<String, Object> column = createColumn(property, value, StringSerializer.get(),
                        new TypeConverterSerializer<Object>(value));
                fireColumnEvent(Event.Column.PRE_COLUMN_MUTATION, entity, property, key, colFamily, column);
                mutator.addInsertion(key, colFamily, column);
                log.debug(String.format("\tI: %s : %s ", property, value));
                fireColumnEvent(Event.Column.POST_COLUMN_MUTATION, entity, property, key, colFamily, column);
            }
        }
        //log.debug(String.format("batched mutation for colFamily: %s, key: %s, col: %s, val: %s", colFamily, key, property, value));
    }
}