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:net.sf.juffrou.reflect.spring.BeanWrapperGenericsTests.java

@Test
public void testGenericMapElementWithKeyType() {
    GenericBean<?> gb = new GenericBean<Object>();
    gb.setLongMap(new HashMap<Long, Integer>());
    BeanWrapper bw = new JuffrouSpringBeanWrapper(gb);
    bw.setPropertyValue("longMap[4]", "5");
    assertEquals("5", gb.getLongMap().get(new Long("4")));
    assertEquals("5", bw.getPropertyValue("longMap[4]"));
}

From source file:org.crank.javax.faces.component.MenuRenderer.java

@SuppressWarnings("unchecked")
Object getCurrentSelectedValues(FacesContext context, UIComponent component) {

    if (component instanceof UISelectMany) {
        UISelectMany select = (UISelectMany) component;
        Object value = select.getValue();
        if (value instanceof Collection) {

            Collection<?> list = (Collection) value;
            int size = list.size();
            if (size > 0) {
                String[] values = new String[list.size()];
                int index = 0;
                for (Object valueObject : list) {
                    BeanWrapper wrapper = new BeanWrapperImpl(valueObject);
                    String propertyValue = wrapper.getPropertyValue("id").toString();
                    values[index] = propertyValue;
                }/*from  www .  j  a va  2s. c  o  m*/
                return values;
            } else {
                return ((Collection) value).toArray();
            }
        } else if (value != null && !value.getClass().isArray()) {
            logger.warning("The UISelectMany value should be an array or a collection type, the actual type is "
                    + value.getClass().getName());
        }

        return value;
    }

    UISelectOne select = (UISelectOne) component;
    Object returnObject;
    if (null != (returnObject = select.getValue())) {
        String[] ret = new String[1];
        BeanWrapper wrapper = new BeanWrapperImpl(returnObject);
        String propertyValue = wrapper.getPropertyValue("id").toString();
        ret[0] = propertyValue;
        return ret;
    }
    return null;

}

From source file:net.sf.juffrou.reflect.spring.BeanWrapperGenericsTests.java

@Test
public void testGenericListOfArraysWithElementConversion() throws MalformedURLException {
    GenericBean<String> gb = new GenericBean<String>();
    ArrayList<String[]> list = new ArrayList<String[]>();
    list.add(new String[] { "str1", "str2" });
    gb.setListOfArrays(list);//from  ww  w. j a  v a 2 s.c o  m
    BeanWrapper bw = new JuffrouSpringBeanWrapper(gb);
    bw.registerCustomEditor(String.class, new StringTrimmerEditor(false));
    bw.setPropertyValue("listOfArrays[0][1]", "str3 ");
    assertEquals("str3", bw.getPropertyValue("listOfArrays[0][1]"));
    assertEquals("str3", gb.getListOfArrays().get(0)[1]);
}

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

private void generateJavaBeanObject(JsonGenerator json, String key, Object bean,
        PropertyEditorRegistrar registrar) throws JsonGenerationException, IOException {
    if (key != null) {
        json.writeFieldName(key);//from  w  w w  .ja v  a  2 s.  co  m
    }
    if (bean == null) {
        json.writeNull();
        return;
    }
    BeanWrapper wrapper = getPropertyAccessor(bean, registrar);
    PropertyDescriptor[] props = wrapper.getPropertyDescriptors();
    json.writeStartObject();
    for (PropertyDescriptor prop : props) {
        String name = prop.getName();
        if (this.getJavaBeanIgnoreProperties() != null && this.getJavaBeanIgnoreProperties().contains(name)) {
            continue;
        }
        if (wrapper.isReadableProperty(name)) {
            Object propVal = wrapper.getPropertyValue(name);
            if (propVal != null) {

                // test for SerializeIgnore
                Method getter = prop.getReadMethod();
                if (getter != null && getter.isAnnotationPresent(SerializeIgnore.class)) {
                    continue;
                }

                if (getPropertySerializerRegistrar() != null) {
                    propVal = getPropertySerializerRegistrar().serializeProperty(name, propVal.getClass(), bean,
                            propVal);
                } else {
                    // Spring does not apply PropertyEditors on read methods, so manually handle
                    PropertyEditor editor = wrapper.findCustomEditor(null, name);
                    if (editor != null) {
                        editor.setValue(propVal);
                        propVal = editor.getAsText();
                    }
                }
                if (propVal instanceof Enum<?> || getJavaBeanTreatAsStringValues() != null
                        && getJavaBeanTreatAsStringValues().contains(propVal.getClass())) {
                    propVal = propVal.toString();
                }
                writeJsonValue(json, name, propVal, registrar);
            }
        }
    }
    json.writeEndObject();
}

From source file:org.lightadmin.core.persistence.support.DynamicDomainObjectMerger.java

/**
 * Merges the given target object into the source one.
 *
 * @param from       can be {@literal null}.
 * @param target     can be {@literal null}.
 * @param nullPolicy how to handle {@literal null} values in the source object.
 *//*  ww w.  j  a  v a  2s.c  om*/
@Override
public void merge(final Object from, final Object target, final NullHandlingPolicy nullPolicy) {
    if (from == null || target == null) {
        return;
    }

    final BeanWrapper fromWrapper = beanWrapper(from);
    final BeanWrapper targetWrapper = beanWrapper(target);

    final DomainTypeAdministrationConfiguration domainTypeAdministrationConfiguration = configuration
            .forManagedDomainType(target.getClass());
    final PersistentEntity<?, ?> entity = domainTypeAdministrationConfiguration.getPersistentEntity();

    entity.doWithProperties(new SimplePropertyHandler() {
        @Override
        public void doWithPersistentProperty(PersistentProperty<?> persistentProperty) {
            Object sourceValue = fromWrapper.getPropertyValue(persistentProperty.getName());
            Object targetValue = targetWrapper.getPropertyValue(persistentProperty.getName());

            if (entity.isIdProperty(persistentProperty)) {
                return;
            }

            if (nullSafeEquals(sourceValue, targetValue)) {
                return;
            }

            if (propertyIsHiddenInFormView(persistentProperty, domainTypeAdministrationConfiguration)) {
                return;
            }

            if (nullPolicy == APPLY_NULLS || sourceValue != null) {
                targetWrapper.setPropertyValue(persistentProperty.getName(), sourceValue);
            }
        }
    });

    entity.doWithAssociations(new SimpleAssociationHandler() {
        @Override
        @SuppressWarnings("unchecked")
        public void doWithAssociation(Association<? extends PersistentProperty<?>> association) {
            PersistentProperty<?> persistentProperty = association.getInverse();

            Object fromValue = fromWrapper.getPropertyValue(persistentProperty.getName());
            Object targetValue = targetWrapper.getPropertyValue(persistentProperty.getName());

            if (propertyIsHiddenInFormView(persistentProperty, domainTypeAdministrationConfiguration)) {
                return;
            }

            if ((fromValue == null && nullPolicy == APPLY_NULLS)) {
                targetWrapper.setPropertyValue(persistentProperty.getName(), fromValue);
            }

            if (persistentProperty.isCollectionLike()) {
                Collection<Object> sourceCollection = (Collection) fromValue;
                Collection<Object> targetCollection = (Collection) targetValue;

                Collection<Object> candidatesForAddition = candidatesForAddition(sourceCollection,
                        targetCollection, persistentProperty);
                Collection<Object> candidatesForRemoval = candidatesForRemoval(sourceCollection,
                        targetCollection, persistentProperty);

                removeReferencedItems(targetCollection, candidatesForRemoval);

                addReferencedItems(targetCollection, candidatesForAddition);

                return;
            }

            if (!nullSafeEquals(fromValue, targetWrapper.getPropertyValue(persistentProperty.getName()))) {
                targetWrapper.setPropertyValue(persistentProperty.getName(), fromValue);
            }
        }
    });
}

From source file:org.jdal.swing.ListTableModel.java

/**
 * Get a primary key of entity in the list
 * @param row row of model/*from  ww w  .  j a va 2s.  c  om*/
 * @return the primary key of model, if any
 */
private Object getPrimaryKey(Object row) {
    if (BeanUtils.getPropertyDescriptor(modelClass, id) == null)
        return row;

    BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(row);
    return wrapper.getPropertyValue(id);
}

From source file:org.jdal.swing.ListTableModel.java

private Object getCellValue(int rowIndex, int columnIndex) {
    BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(list.get(rowIndex));
    return bw.getPropertyValue(columnNames.get(columnIndex));
}

From source file:org.brushingbits.jnap.common.bean.cloning.BeanCloner.java

private Object cloneBean(Object bean, Class<?> type) {
    BeanWrapper source = PropertyAccessorFactory.forBeanPropertyAccess(bean);
    BeanWrapper copy = PropertyAccessorFactory.forBeanPropertyAccess(BeanUtils.instantiate(type));

    // keep instance for circular and multiple references
    context.setAsVisited(bean);//ww w  .  j a va 2s .c  o m
    alreadyCloned.put(bean, copy.getWrappedInstance());

    PropertyDescriptor[] beanProperties = copy.getPropertyDescriptors();
    for (PropertyDescriptor propertyDescriptor : beanProperties) {
        String name = propertyDescriptor.getName();
        context.pushPath(name);
        if (copy.isReadableProperty(name) && copy.isWritableProperty(name)) {
            Object value = source.getPropertyValue(name);
            copy.setPropertyValue(name, clone(value));
        }
        context.popPath();
    }
    Object beanCopy = copy.getWrappedInstance();
    source = null;
    copy = null;
    return beanCopy;
}

From source file:com.aw.swing.mvp.binding.component.support.ColumnInfo.java

/**
 * Set the value to the specific cell//from   ww w  .  j ava  2s.co m
 *
 * @param object
 */
public void setValue(Object object, Object value, int row) {
    // At this moment in the case of Object[] only the first element could be updated.
    if (object instanceof Object[]) {
        ((Object[]) object)[0] = value;
    } else {
        if (value instanceof Boolean) {
            value = ((Boolean) value).booleanValue() ? valueTrue : valueFalse;
        }
        BeanWrapper bwRow = getRow(object);
        Object oldValue = null;
        if (existChangeValueListener() || existVetoableChangeListener()) {
            oldValue = bwRow.getPropertyValue(fieldName);
        }
        if (formatter == DateFormatter.DATE_FORMATTER) {
            value = BndIJTextField.getValidDate((String) value);
        }
        try {
            if (existVetoableChangeListener()) {
                Object convertedValue = getConvertedValue(object, fieldName, value);
                vetoableChangeListener.vetoableChange(object, oldValue, convertedValue);
            }
            bwRow.setPropertyValue(fieldName, value);
            value = bwRow.getPropertyValue(fieldName);
            executeChangeValueListener(object, value, oldValue);
        } catch (AWBusinessException e) {
            bwRow.setPropertyValue(fieldName, oldValue);
            showErrorMsg(e);
            return;
        }
    }
}

From source file:com.emc.ecs.sync.service.SyncJobService.java

protected void copyProperties(Object source, Object target, String... ignoredProperties) {
    List<String> ignoredList = Collections.emptyList();
    if (ignoredProperties != null)
        ignoredList = Arrays.asList(ignoredProperties);
    BeanWrapper wSource = new BeanWrapperImpl(source), wTarget = new BeanWrapperImpl(target);
    wSource.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat(ISO_8601_FORMAT), true));
    wTarget.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat(ISO_8601_FORMAT), true));
    for (PropertyDescriptor descriptor : wSource.getPropertyDescriptors()) {
        if (ignoredList.contains(descriptor.getName()))
            continue;
        if (!wSource.isReadableProperty(descriptor.getName()))
            continue;
        if (wTarget.isWritableProperty(descriptor.getName())) {

            // property is readable from source, writeable on target, and not in the list of ignored props
            Object value = wSource.getPropertyValue(descriptor.getName());
            if (value != null)
                wTarget.setPropertyValue(descriptor.getName(), value);
        }//w  ww  .  jav  a2s. c  om
    }
}