Example usage for org.springframework.beans BeanWrapper setPropertyValue

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

Introduction

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

Prototype

void setPropertyValue(String propertyName, @Nullable Object value) throws BeansException;

Source Link

Document

Set the specified value as current property value.

Usage

From source file:com.aw.swing.mvp.grid.GridInfoProvider.java

/**
 * Get Object that will be used to get the data to be shown
 *
 * @return/*from   ww w  .  j  a va2  s.c om*/
 */
public ListProvider getListProvider(final Presenter presenter) {
    final GridInfoProvider gridInfoProvider = this;
    return new ListProvider() {
        public List getList(Object param) {
            List list = getValues((E) param);
            if (list == null) {
                list = new ArrayList();
            }
            if (!(presenter instanceof FindPresenter)) {
                Method method = null;
                try {
                    method = gridInfoProvider.getClass().getMethod("getValues", param.getClass());
                } catch (NoSuchMethodException e) {
                }
                if (method == null) {
                    method = ReflectionUtils.findMethodByName(gridInfoProvider.getClass(), "getValues");
                }
                DeleteableListField deleteableListField = AnnotationUtils.getAnnotation(method,
                        DeleteableListField.class);
                if (deleteableListField != null) {
                    if (!(list instanceof DeleteableList)) {
                        DeleteableList deleteableList = new DeleteableList();
                        deleteableList.init(list);
                        BeanWrapper bw = new BeanWrapperImpl(param);
                        bw.setPropertyValue(deleteableListField.value(), deleteableList);
                        return deleteableList;
                    }
                }
            }
            return list;
        }
    };
}

From source file:hu.petabyte.redflags.engine.util.MappingUtils.java

/**
 * Sets the value of a bean's property, using the given wrapper. The
 * property can be a deeper one, e.g. "a.b.c.d". If any of a, b or c
 * properties is null, the method will call an empty constructor for its
 * static type. If any error occurs, setDeepProperty will write it to the
 * LOG and returns false./* w ww  .  j a  v  a 2 s. c o  m*/
 *
 * @param wrapper
 *            Wrapper of the root object.
 * @param property
 *            The property needs to be set.
 * @param value
 *            The new value of the property.
 * @return True if property setting was successful, false on error.
 */
public static synchronized boolean setDeepProperty(BeanWrapper wrapper, String property, Object value) {
    try {
        // this will help calling a constructor:
        ExpressionParser parser = new SpelExpressionParser();

        // go thru property path elements:
        int offset = 0;
        while ((offset = property.indexOf(".", offset + 1)) > -1) {
            String currentProperty = property.substring(0, offset);

            // if current property is null:
            if (null == wrapper.getPropertyValue(currentProperty)) {
                String className = wrapper.getPropertyType(currentProperty).getName();

                // build up a constructor call:
                Expression exp = parser.parseExpression(String.format("new %s()", className));

                // LIMITATIONS:
                // 1) uses static type
                // 2) needs defined empty constructor

                // run it:
                Object newObject = exp.getValue();

                // and set the property:
                wrapper.setPropertyValue(currentProperty, newObject);
            }
        }

        // finally, set the destination property:
        wrapper.setPropertyValue(property, value);
        return true;
    } catch (Exception ex) {
        LOG.error("Could not set property '{}'", property);
        LOG.debug("Exception: ", ex);
        return false;
    }
}

From source file:nl.tue.gale.common.SpringManipulator.java

public <E> void setList(List<E> list) {
    BeanWrapper wrapper = new BeanWrapperImpl(bean);
    @SuppressWarnings("unchecked")
    List<E> beanList = new ArrayList<E>((List<E>) wrapper.getPropertyValue(property));
    beanList.addAll(list);/*w w w .  j av  a 2 s  .  c om*/
    wrapper.setPropertyValue(property, beanList);
}

From source file:nl.tue.gale.common.SpringManipulator.java

public <K, V> void setMap(Map<K, V> map) {
    BeanWrapper wrapper = new BeanWrapperImpl(bean);
    @SuppressWarnings("unchecked")
    Map<K, V> beanMap = new HashMap<K, V>((Map<K, V>) wrapper.getPropertyValue(property));
    beanMap.putAll(map);/* w  w w  . ja v a2s . c  o m*/
    wrapper.setPropertyValue(property, beanMap);
}

From source file:org.zilverline.web.TestCustomCollectionEditor.java

public void testCustomCollectionEditorWithString() {
    CollectionManager colMan = (CollectionManager) applicationContext.getBean("collectionMan");
    assertNotNull(colMan);// www.  j  ava2s. c  om
    assertTrue("collections must exist", colMan.getCollections().size() > 0);

    SearchForm sf = new SearchForm();
    BeanWrapper bw = new BeanWrapperImpl(sf);
    bw.registerCustomEditor(CollectionTriple[].class, "collections", new CustomCollectionEditor(colMan));
    try {
        bw.setPropertyValue("collections", "problems");
    } catch (BeansException ex) {
        fail("Should not throw BeansException: " + ex.getMessage());
    }
    CollectionTriple[] col3s = (CollectionTriple[]) bw.getPropertyValue("collections");
    assertEquals(colMan.getCollections().size(), col3s.length);
    assertEquals("testdata", col3s[0].getName());
}

From source file:org.zilverline.web.TestCustomCollectionEditor.java

public void testCustomCollectionEditorWithStringArrayOfOne() {
    CollectionManager colMan = (CollectionManager) applicationContext.getBean("collectionMan");
    assertNotNull(colMan);/*from   w  ww  .jav  a  2 s.  c  o  m*/
    assertTrue("collections must exist", colMan.getCollections().size() > 0);

    SearchForm sf = new SearchForm();
    BeanWrapper bw = new BeanWrapperImpl(sf);
    bw.registerCustomEditor(CollectionTriple[].class, "collections", new CustomCollectionEditor(colMan));
    try {
        bw.setPropertyValue("collections", new String[] { "problems" });
    } catch (BeansException ex) {
        fail("Should not throw BeansException: " + ex.getMessage());
    }
    CollectionTriple[] col3s = (CollectionTriple[]) bw.getPropertyValue("collections");
    assertEquals(colMan.getCollections().size(), col3s.length);
    assertEquals("testdata", col3s[0].getName());
}

From source file:org.zilverline.web.TestCustomCollectionEditor.java

public void testCustomCollectionEditorWithStringArrayOfTwo() {
    CollectionManager colMan = (CollectionManager) applicationContext.getBean("collectionMan");
    assertNotNull(colMan);//from   w  w  w.j a va 2s  .  co  m
    assertTrue("collections must exist", colMan.getCollections().size() > 0);

    SearchForm sf = new SearchForm();
    BeanWrapper bw = new BeanWrapperImpl(sf);
    bw.registerCustomEditor(CollectionTriple[].class, "collections", new CustomCollectionEditor(colMan));
    try {
        bw.setPropertyValue("collections", new String[] { "problems", "testdata" });
    } catch (BeansException ex) {
        fail("Should not throw BeansException: " + ex.getMessage());
    }
    CollectionTriple[] col3s = (CollectionTriple[]) bw.getPropertyValue("collections");
    assertEquals(colMan.getCollections().size(), col3s.length);
    assertEquals("testdata", col3s[0].getName());
}

From source file:org.sakaiproject.metaobj.utils.mvc.impl.BeanWrapperBase.java

public void setPropertyValue(String propertyName, Object value) throws BeansException {

    if (isNestedProperty(propertyName)) {
        BeanWrapper nestedBw = getBeanWrapperForPropertyPath(propertyName);
        nestedBw.setPropertyValue(getFinalPath(propertyName), value);
        return;//from   www .  j  a v  a 2  s  .c om
    } else {
        super.setPropertyValue(propertyName, value);
    }
}

From source file:org.zilverline.web.TestCustomCollectionEditor.java

public void testCustomCollectionEditorWithStrings() {
    CollectionManager colMan = (CollectionManager) applicationContext.getBean("collectionMan");
    assertNotNull(colMan);//w  ww  . j ava 2s  .  c o  m
    assertTrue("collections must exist", colMan.getCollections().size() > 0);
    log.debug("test");

    SearchForm sf = new SearchForm();
    BeanWrapper bw = new BeanWrapperImpl(sf);
    bw.registerCustomEditor(CollectionTriple[].class, "collections", new CustomCollectionEditor(colMan));
    try {
        bw.setPropertyValue("collections", "problems, testdata");
    } catch (BeansException ex) {
        fail("Should not throw BeansException: " + ex.getMessage());
    }
    CollectionTriple[] col3s = (CollectionTriple[]) bw.getPropertyValue("collections");
    log.debug(col3s);
    assertEquals(colMan.getCollections().size(), col3s.length);
    for (int i = 0; i < colMan.getCollections().size(); i++) {
        assertTrue(col3s[0].isSelected());
    }
}

From source file:org.iwethey.forums.web.user.UserController.java

/**
 * Toggle binary settings. Requires a request parameter of "toggle" 
 * that contains the name of the property to toggle.
 *///  w  w w.ja va2s .  co  m
public ModelAndView toggle(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    User user = (User) request.getAttribute(USER_ATTRIBUTE);

    String propName = RequestUtils.getStringParameter(request, "toggle", null);
    if (user != null && propName != null && !propName.equals("")) {
        BeanWrapper wrap = new BeanWrapperImpl(user);
        Boolean val = (Boolean) wrap.getPropertyValue(propName);

        wrap.setPropertyValue(propName, new Boolean(!val.booleanValue()));
        userManager.saveUserAttributes(user);
    }

    return new ModelAndView(new RedirectView(request.getHeader("referer")));
}