Example usage for org.apache.commons.beanutils BeanUtilsBean populate

List of usage examples for org.apache.commons.beanutils BeanUtilsBean populate

Introduction

In this page you can find the example usage for org.apache.commons.beanutils BeanUtilsBean populate.

Prototype

public void populate(Object bean, Map properties) 

Source Link

Usage

From source file:BeanUtilsExampleV4.java

  public static void main(String args[]) throws Exception {
    BeanUtilsExampleV4 diff = new BeanUtilsExampleV4();
    Actor actor = diff.prepareData();//from   w  w  w.jav  a2  s . c o m

    ConvertUtilsBean convertUtilsBean = new ConvertUtilsBean();
    convertUtilsBean.deregister(String.class);
    convertUtilsBean.register(new MyStringConverter(), String.class);

    convertUtilsBean.deregister(Long.class);
    convertUtilsBean.register(new MyLongConverter(), Long.class);
    convertUtilsBean.register(new MyLongConverter(), Long.TYPE);

    BeanUtilsBean beanUtilsBean =
      new BeanUtilsBean(convertUtilsBean, new PropertyUtilsBean());

    System.err.println("==== Values before calling describe ==== ");
    System.err.println("By PropertyUtils: " +
      PropertyUtils.getProperty(actor, "name"));
    System.err.println("By BeanUtils: " +
      beanUtilsBean.getProperty(actor, "name"));
    System.err.println(beanUtilsBean.getProperty(actor, "worth"));

    Map describedData = beanUtilsBean.describe(actor);

    // check the map
    System.err.println("==== Values in Map ==== ");
    System.err.println(describedData.get("name"));
    System.err.println(describedData.get("worth"));

    // create a new Actor Bean
    Actor newActor = new Actor();
    beanUtilsBean.populate(newActor, describedData);

    System.err.println("==== Values after calling populate ==== ");
    System.err.println(beanUtilsBean.getProperty(newActor, "name"));
    System.err.println(beanUtilsBean.getProperty(newActor, "worth"));

}

From source file:com.formkiq.core.form.bean.FormFieldMapper.java

/**
 * Map {@link FormJSON} to {@link Object} by value key.
 * @param source {@link FormJSON}/*from  w w w. j a  va 2 s. c  om*/
 * @param dest {@link Object}
 * @throws ReflectiveOperationException ReflectiveOperationException
 */
public static void mapByValue(final FormJSON source, final Object dest) throws ReflectiveOperationException {
    Map<String, String> values = ObjectBuilder.getValueKeyMap(source);
    BeanUtilsBean bean = ObjectBuilder.getBeanUtils();
    bean.populate(dest, values);
}

From source file:com.formkiq.core.form.bean.ObjectBuilder.java

/**
 * Transfers a {@link FormJSON} to an object.
 * @param <T> Type of object/* w  w w  .  ja  v a2  s. c o  m*/
 * @param form {@link FormJSON}
 * @param additional {@link Map} additional key/values
 * @param t T
 * @return T
 */
public static <T> T buildObject(final FormJSON form, final T t, final Map<String, String> additional) {

    Map<String, String> valueMap = getValueKeyMap(form);

    try {
        BeanUtilsBean bean = getBeanUtils();
        bean.populate(t, valueMap);

        if (additional != null) {
            bean.populate(t, additional);
        }

    } catch (IllegalAccessException | InvocationTargetException e) {
        throw new RuntimeException(e);
    }

    return t;
}

From source file:com.formkiq.core.dao.AbstractDaoImpl.java

/**
 * Transform results from a AliasToEntityMapResultTransformer
 * to an actual class./* ww  w .j a va2s.  co m*/
 * Better than using AliasToBeanResultTransformer
 * because it supports nested classes.
 * @param <T> Type of class
 * @param maps {@link List}
 * @param clazz {@link Class}
 * @return {@link List}
 */
protected <T> List<T> transformMapListToObject(final List<Map<String, Object>> maps, final Class<T> clazz) {

    BeanUtilsBean utils = ObjectBuilder.getBeanUtils();

    List<T> list = new ArrayList<>();

    try {

        for (Map<String, Object> map : maps) {
            T bean = clazz.newInstance();

            utils.populate(bean, map);
            list.add(bean);
        }

        return list;

    } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
        throw new RuntimeException(e);
    }
}

From source file:io.milton.servlet.DefaultMiltonConfigurator.java

@Override
public HttpManager configure(Config config) throws ServletException {

    log.info("Listing all config parameters:");
    Map<String, String> props = new HashMap<String, String>();
    for (String s : config.getInitParameterNames()) {
        String val = config.getInitParameter(s);
        log.info(" " + s + " = " + val);
        if (!s.contains(".") && !s.contains("_")) {
            props.put(s, val);
        }/*from www  . ja  v a 2 s. c o  m*/
    }

    String authHandlers = config.getInitParameter("authenticationHandlers");
    if (authHandlers != null) {
        props.remove("authenticationHandlers"); // so the bub doesnt try to set it
        initAuthHandlers(authHandlers);
    }

    String extraAuthHandlers = config.getInitParameter("extraAuthenticationHandlers");
    if (extraAuthHandlers != null) {
        props.remove("extraAuthenticationHandlers"); // so the bub doesnt try to set it
        initExtraAuthHandlers(extraAuthHandlers);
    }

    String resourceFactoryClassName = config.getInitParameter("resource.factory.class");
    if (resourceFactoryClassName != null) {
        ResourceFactory rf = instantiate(resourceFactoryClassName);
        builder.setMainResourceFactory(rf);
    } else {
        log.warn("No custom ResourceFactory class name provided in resource.factory.class");
    }

    String responseHandlerClassName = config.getInitParameter("response.handler.class");
    if (responseHandlerClassName != null) {
        WebDavResponseHandler davResponseHandler = instantiate(responseHandlerClassName);
        builder.setWebdavResponseHandler(davResponseHandler);
    }
    List<Filter> filters = null;
    List<String> params = config.getInitParameterNames();
    for (String paramName : params) {
        if (paramName.startsWith("filter_")) {
            String filterClass = config.getInitParameter(paramName);
            Filter f = instantiate(filterClass);
            if (filters == null) {
                filters = new ArrayList<Filter>();
            }
            filters.add(f);
        }
    }
    if (filters != null) {
        builder.setFilters(filters);
    }

    try {
        ConvertUtilsBean2 convertUtilsBean = new ConvertUtilsBean2();
        BeanUtilsBean bub = new BeanUtilsBean(convertUtilsBean);
        bub.populate(builder, props);
    } catch (IllegalAccessException e) {
        throw new ServletException(e);
    } catch (InvocationTargetException e) {
        throw new ServletException(e);
    }

    ResourceFactory rf = builder.getMainResourceFactory();
    //      if (rf instanceof AnnotationResourceFactory) {
    //         AnnotationResourceFactory arf = (AnnotationResourceFactory) rf;
    //         arf.setContextPath("/");
    //         if (arf.getViewResolver() == null) {
    //            ViewResolver viewResolver = new JspViewResolver(config.getServletContext());
    //            arf.setViewResolver(viewResolver);
    //         }
    //      }

    build();
    initables = new ArrayList<Initable>();

    checkAddInitable(initables, builder.getAuthenticationHandlers());
    checkAddInitable(initables, builder.getMainResourceFactory());
    checkAddInitable(initables, builder.getWebdavResponseHandler());
    checkAddInitable(initables, builder.getFilters());

    for (Initable i : initables) {
        i.init(config, httpManager);
    }
    return httpManager;
}

From source file:org.mule.module.wsdlproc.WSDLProcModule.java

public Object createArgumentObject(Map<String, Object> inputParams)
        throws InstantiationException, IllegalAccessException, IllegalArgumentException,
        InvocationTargetException, SecurityException, NoSuchMethodException {
    Object inputObject = type.newInstance();

    BeanUtilsBean beanUtilsBean = new BeanUtilsBean(new ConvertUtilsBean() {//This is to make BeanUtils handle Enums
        @Override/*from  www  .j  a v  a 2s.  c om*/
        public Object convert(String value, Class clazz) {
            if (clazz.isEnum()) {
                return Enum.valueOf(clazz, camelCaseToEnumFormat(value));
            } else {
                return super.convert(value, clazz);
            }
        }
    });

    beanUtilsBean.populate(inputObject, inputParams);
    //BeanUtils.populateWithoutFail(inputObject, inputParams, false);
    return inputObject;

}

From source file:org.seasar.struts.bean.SuppressPropertyUtilsBeanTest.java

public void testBeanUtilsBean_default_populate() throws Exception {
    BeanUtilsBean beanUtils = new BeanUtilsBean();
    Person person = new Person();
    Map properties = new HashMap();
    properties.put("fullName.firstName", "aaa");
    properties.put("fullName.lastName", "bbb");
    properties.put("address.street", "ccc");
    properties.put("age", "20");
    beanUtils.populate(person, properties);
    assertEquals("aaa", person.getFullName().getFirstName());
    assertEquals("bbb", person.getFullName().getLastName());
    assertEquals("ccc", person.getAddress().getStreet());
    assertEquals("20", person.getAge());
}

From source file:org.seasar.struts.bean.SuppressPropertyUtilsBeanTest.java

public void testBeanUtilsBean_suppressed_populate() throws Exception {
    List classes = new ArrayList();
    classes.add(FullName.class);
    BeanUtilsBean beanUtils = new BeanUtilsBean(new ConvertUtilsBean(), new SuppressPropertyUtilsBean(classes));
    Person person = new Person();
    Map properties = new HashMap();
    properties.put("fullName.firstName", "aaa");
    properties.put("fullName.lastName", "bbb");
    properties.put("address.street", "ccc");
    properties.put("age", "20");
    beanUtils.populate(person, properties);
    assertNull(person.getFullName().getFirstName());
    assertNull(person.getFullName().getLastName());
    assertEquals("ccc", person.getAddress().getStreet());
    assertEquals("20", person.getAge());
}