Example usage for org.springframework.beans BeanWrapper getPropertyValue

List of usage examples for org.springframework.beans BeanWrapper getPropertyValue

Introduction

In this page you can find the example usage for org.springframework.beans BeanWrapper getPropertyValue.

Prototype

@Nullable
Object getPropertyValue(String propertyName) throws BeansException;

Source Link

Document

Get the current value of the specified property.

Usage

From source file:com.fmguler.ven.Ven.java

private boolean isObjectNew(Object object) throws VenException {
    BeanWrapper beanWrapper = new BeanWrapperImpl(object);
    Object objectId = beanWrapper.getPropertyValue("id");
    if (objectId == null)
        return true;
    if (!(objectId instanceof Integer))
        throw new VenException(VenException.EC_GENERATOR_OBJECT_ID_TYPE_INVALID);
    return ((Integer) objectId).intValue() == 0;
}

From source file:com.senacor.core.manager.BaseManagerImpl.java

public List<T> find(final T t) {
    Session session = getSession();//from w  w w .j  a v  a 2 s.  c o  m
    Criteria criteria = session.createCriteria(boClass);
    criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    PropertyDescriptor[] descriptors = BeanUtils.getPropertyDescriptors(boClass);
    BeanWrapper beanWrapper = new BeanWrapperImpl(t);
    for (PropertyDescriptor descriptor : descriptors) {
        if (!descriptor.getName().equals("class") && !descriptor.getName().equals("id")) {
            Object value = beanWrapper.getPropertyValue(descriptor.getName());
            if (value != null) {
                criteria.add(Restrictions.eq(descriptor.getName(), value));
            }
        }
    }
    criteria.addOrder(Order.asc("id"));
    return criteria.list();
}

From source file:org.jdto.spring.BeanWrapperBeanModifier.java

/**
 * Read a property value using the property path by invoking a spring {@link BeanWrapper}
 * @param propertyPath/* w ww.  ja va 2s .  com*/
 * @param instance
 * @return the property value found on the property path applied to the provided instance.
 */
@Override
public Object doReadPropertyValue(String propertyPath, Object instance) {
    BeanWrapper beanWrapper = PropertyAccessorFactory.forBeanPropertyAccess(instance);

    //check if it's safe to write the property.
    if (!beanWrapper.isReadableProperty(propertyPath)) {
        return null;
    }

    return beanWrapper.getPropertyValue(propertyPath);
}

From source file:com.xyxy.platform.modules.core.web.taglib.BSAbstractMultiCheckedElementTag.java

/**
 * Copy & Paste, .//from   w  ww . j a  va  2  s .  c  om
 */
private void writeMapEntry(TagWriter tagWriter, String valueProperty, String labelProperty, Map.Entry entry,
        int itemIndex) throws JspException {

    Object mapKey = entry.getKey();
    Object mapValue = entry.getValue();
    BeanWrapper mapKeyWrapper = PropertyAccessorFactory.forBeanPropertyAccess(mapKey);
    BeanWrapper mapValueWrapper = PropertyAccessorFactory.forBeanPropertyAccess(mapValue);
    Object renderValue = (valueProperty != null ? mapKeyWrapper.getPropertyValue(valueProperty)
            : mapKey.toString());
    Object renderLabel = (labelProperty != null ? mapValueWrapper.getPropertyValue(labelProperty)
            : mapValue.toString());
    writeElementTag(tagWriter, mapKey, renderValue, renderLabel, itemIndex);
}

From source file:ar.com.zauber.commons.conversion.setters.CollectionGetAndAddFieldSetterStrategy.java

/** @see FieldSetterStrategy#setProperty(BeanWrapper, String, Object) */
public final void setProperty(final BeanWrapper bean, final String targetName, final Object value) {
    if (!(value instanceof Iterable<?>)) {
        throw new IllegalStateException(
                "The source object must be iterable." + value.getClass().getCanonicalName() + " given.");
    }/*ww w  .  j av a2 s  . c o  m*/
    final Iterable<?> sourceCollection = (Iterable<?>) value;
    final Object t = bean.getPropertyValue(targetName);
    if (!(t instanceof Collection<?>)) {
        throw new IllegalStateException(
                "The target object must be a collection. " + t.getClass().getCanonicalName() + " given.");
    }
    final Collection<Object> targetCollection = (Collection<Object>) t;
    for (final Object o : sourceCollection) {
        targetCollection.add(o);
    }
}

From source file:org.hdiv.web.servlet.tags.form.OptionWriterHDIV.java

/**
 * Renders the inner '<code>option</code>' tags using the supplied {@link Collection} of
 * objects as the source. The value of the {@link #valueProperty} field is used
 * when rendering the '<code>value</code>' of the '<code>option</code>' and the value of the
 * {@link #labelProperty} property is used when rendering the label.
 *///www .j a v a  2s.  c o  m
private void doRenderFromCollection(Collection optionCollection, TagWriter tagWriter) throws JspException {
    for (Object item : optionCollection) {
        BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(item);
        Object value;
        if (this.valueProperty != null) {
            value = wrapper.getPropertyValue(this.valueProperty);
        } else if (item instanceof Enum) {
            value = ((Enum<?>) item).name();
        } else {
            value = item;
        }
        Object label = (this.labelProperty != null ? wrapper.getPropertyValue(this.labelProperty) : item);
        renderOption(tagWriter, item, value, label);
    }
}

From source file:com.yosanai.java.aws.console.DefaultAWSConnectionProvider.java

protected String getInstanceDetail(String property, Instance instance) throws Exception {
    String ret = null;/* w ww .j  a  va2  s  .c om*/
    BeanWrapper beanWrapper = new BeanWrapperImpl(instance);
    Object value = beanWrapper.getPropertyValue(property);
    if (null != value) {
        ret = value.toString();
    }
    return ret;
}

From source file:net.solarnetwork.web.support.SimpleCsvView.java

private void writeCSV(ICsvMapWriter writer, String[] fields, Object row) throws IOException {
    if (row instanceof Map) {
        @SuppressWarnings("unchecked")
        Map<String, ?> map = (Map<String, ?>) row;
        writer.write(map, fields);//www. ja  v  a  2s  . c om
    } else if (row != null) {
        Map<String, Object> map = new HashMap<String, Object>(fields.length);

        // use bean properties
        if (getPropertySerializerRegistrar() != null) {
            // try whole-bean serialization first
            row = getPropertySerializerRegistrar().serializeProperty("row", row.getClass(), row, row);
            if (row == null) {
                return;
            }
        }

        BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(row);
        for (String name : fields) {
            Object val = wrapper.getPropertyValue(name);
            if (val != null) {
                if (getPropertySerializerRegistrar() != null) {
                    val = getPropertySerializerRegistrar().serializeProperty(name, val.getClass(), row, val);
                } else {
                    // Spring does not apply PropertyEditors on read methods, so manually handle
                    PropertyEditor editor = wrapper.findCustomEditor(null, name);
                    if (editor != null) {
                        editor.setValue(val);
                        val = editor.getAsText();
                    }
                }
                if (val instanceof Enum<?> || getJavaBeanTreatAsStringValues() != null
                        && getJavaBeanTreatAsStringValues().contains(val.getClass())) {
                    val = val.toString();
                }
                if (val != null) {
                    map.put(name, val);
                }
            }
        }

        writer.write(map, fields);
    }
}

From source file:net.solarnetwork.web.support.SimpleCsvHttpMessageConverter.java

private void writeCSV(ICsvMapWriter writer, String[] fields, Object row) throws IOException {
    if (row instanceof Map) {
        @SuppressWarnings("unchecked")
        Map<String, ?> map = (Map<String, ?>) row;
        writer.write(map, fields);//w ww  . ja  v a 2s.c  o m
    } else if (row != null) {
        Map<String, Object> map = new HashMap<String, Object>(fields.length);

        // use bean properties
        if (propertySerializerRegistrar != null) {
            // try whole-bean serialization first
            row = propertySerializerRegistrar.serializeProperty("row", row.getClass(), row, row);
            if (row == null) {
                return;
            }
        }

        if (row instanceof Map) {
            Map<String, ?> rowMap = cast(row);
            for (Map.Entry<String, ?> me : rowMap.entrySet()) {
                Object val = getRowPropertyValue(row, me.getKey(), me.getValue(), null);
                if (val != null) {
                    map.put(me.getKey(), val);
                }
            }
        } else {
            BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(row);
            for (String name : fields) {
                Object val = wrapper.getPropertyValue(name);
                val = getRowPropertyValue(row, name, val, wrapper);
                if (val != null) {
                    map.put(name, val);
                }
            }
        }

        writer.write(map, fields);
    }
}

From source file:de.escalon.hypermedia.spring.SpringActionDescriptor.java

/**
 * Gets input parameter info which is part of the URL mapping, be it request parameters, path variables or request
 * body attributes.// ww w. j a  va  2s.  com
 *
 * @param name
 *         to retrieve
 * @return parameter descriptor or null
 */
@Override
public ActionInputParameter getActionInputParameter(String name) {
    ActionInputParameter ret = requestParams.get(name);
    if (ret == null) {
        ret = pathVariables.get(name);
    }
    if (ret == null) {
        for (ActionInputParameter annotatedParameter : getInputParameters()) {
            // TODO create ActionInputParameter for bean property at property path
            // TODO field access in addition to bean?
            PropertyDescriptor pd = getPropertyDescriptorForPropertyPath(name,
                    annotatedParameter.getParameterType());
            if (pd != null) {
                if (pd.getWriteMethod() != null) {

                    Object callValue = annotatedParameter.getValue();
                    Object propertyValue = null;
                    if (callValue != null) {
                        BeanWrapper beanWrapper = PropertyAccessorFactory.forBeanPropertyAccess(callValue);
                        propertyValue = beanWrapper.getPropertyValue(name);
                    }
                    ret = new SpringActionInputParameter(new MethodParameter(pd.getWriteMethod(), 0),
                            propertyValue);
                }
                break;
            }
        }
    }
    return ret;
}