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

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

Introduction

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

Prototype

public static void populate(Object bean, Map properties) 

Source Link

Usage

From source file:BeanUtilsExampleV3.java

  public static void main(String args[]) throws Exception {
    BeanUtilsExampleV3 diff = new BeanUtilsExampleV3();
    Actor actor = diff.prepareData();//from  www.j  a v  a  2 s  . c o m

    Map describedData = BeanUtils.describe(actor);

    // check the map
    System.err.println(describedData.get("name"));

    // change this value
    describedData.put("name", "Robert Redford");

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

    System.err.println(BeanUtils.getProperty(newActor, "name"));

}

From source file:com.amar.web.demo.utils.ServletUtil.java

@SuppressWarnings("rawtypes")
public static <T> T request2Bean(HttpServletRequest request, Class<T> beanClass) {
    try {/*from  w ww . j a va2  s  .  com*/
        T bean = beanClass.newInstance();
        Map map = request.getParameterMap();
        BeanUtils.populate(bean, map);
        return bean;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:ch.cyclops.publish.ParseQueryResult.java

public static List map(List<Map> list, Class clazz) {
    List<Object> mapped = new ArrayList<>();

    // iterate and map those objects
    if (list != null) {
        for (Map map : list) {
            try {
                Object bean = clazz.newInstance();

                // map HashMap to POJO
                BeanUtils.populate(bean, map);

                // add it to list of mapped CDRs
                mapped.add(bean);/*from  ww w. j a  va  2  s  . com*/

            } catch (Exception ignored) {
                return null;
            }
        }
    }

    return mapped;
}

From source file:ch.icclab.cyclops.util.BeanList.java

public static List populate(List<Map> list, Class clazz) {
    List<Object> mapped = new ArrayList<>();

    // iterate and map those objects
    if (list != null) {
        for (Map map : list) {
            try {
                Object bean = clazz.newInstance();

                // map HashMap to POJO
                BeanUtils.populate(bean, map);

                // add it to list of mapped CDRs
                mapped.add(bean);/*from   w w w. ja  v  a 2s. com*/

            } catch (Exception ignored) {
                return null;
            }
        }
    }

    return mapped;
}

From source file:com.github.ibole.infrastructure.common.utils.BeanUtil.java

public static <T> List<T> resolveMapListToObjectList(List<Map<String, ?>> list, Class<T> clazz)
        throws IllegalAccessException, InstantiationException, InvocationTargetException {
    List<T> resultList = new ArrayList<>();
    for (Map<String, ?> map : list) {
        T bean = clazz.newInstance();/*from  w  ww. java  2 s . c om*/
        BeanUtils.populate(bean, map);
        resultList.add(bean);
    }
    return resultList;
}

From source file:br.com.softgraf.model.command.CadastrarFornecedor.java

@Override
public String execute(HttpServletRequest request, HttpServletResponse response) {

    Fornecedor fornecedor = new Fornecedor();

    try {//from   w  w w  . j av a 2 s .  co  m
        BeanUtils.populate(fornecedor, request.getParameterMap());

        System.out.println(fornecedor.toString());

        if (fornecedor.isValid()) {
            DAO<Fornecedor> fornecedorDao = new DAOImpl<Fornecedor>(Fornecedor.class,
                    (Session) request.getAttribute(HibernateUtil.HIBERNATE_SESSION));
            fornecedorDao.salvar(fornecedor);
            request.setAttribute("mensagem", "Fornecedor gravado com sucesso: " + fornecedor.getNome());
        } else {
            System.out.println("falta dados");
            request.setAttribute("mensagem", "Preencha os campos obrigatrios");
        }

    } catch (IllegalAccessException e) {
        request.setAttribute("mensagem", "Problemas com preenchimento do Bean");
        e.printStackTrace();

    } catch (InvocationTargetException e) {
        request.setAttribute("mensagem", "Problemas com preenchimento do Bean");
        e.printStackTrace();
    }

    return "cadastra_fornecedor.jsp";
}

From source file:controler.category.AddCategoryServlet.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    Category category = new Category();
    CategoryDAO categoryDAO = new CategoryDAO();
    try {// w  w w . j a  va 2 s . co m
        BeanUtils.populate(category, request.getParameterMap());
        if (categoryDAO.connect()) {
            int rows = categoryDAO.addCategory(category);
        }

    } catch (IllegalAccessException ex) {
        Logger.getLogger(AddCategoryServlet.class.getName()).log(Level.SEVERE, null, ex);
    } catch (InvocationTargetException ex) {
        Logger.getLogger(AddCategoryServlet.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        categoryDAO.disconnect();
        response.sendRedirect(request.getSession().getServletContext().getContextPath() + "/AdminViewCategory");

    }

}

From source file:com.nec.sydney.beans.exportroot.NSBeanUtil.java

public static void setProperties(Object bean, String[] valueArray) throws Exception {
    if (bean == null || valueArray == null || valueArray.length == 0) {
        return;/* ww  w. j a v a 2 s .  c  o m*/
    }
    Map map = new TreeMap();
    for (int i = 0; i < valueArray.length; i++) {
        String keyAndValue = valueArray[i];
        if (keyAndValue == null || keyAndValue.indexOf(KEY_VALUE_SPLITER) == -1) {
            continue;
        } else {
            String[] tmpArray = valueArray[i].split(KEY_VALUE_SPLITER, 2);
            String key = tmpArray[0].trim();
            String value = tmpArray[1];
            map.put(key, value);
        }
    }
    if (map.size() > 0) {
        BeanUtils.populate(bean, map);
    }
    return;
}

From source file:com.base2.kagura.rest.model.ResponseBase.java

public ResponseBase(Map<String, Object> result)
        throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
    BeanUtils.populate(this, result);
}

From source file:com.rodaxsoft.junit.mailgun.MemberDetailsConverter.java

@Override
public <T> T convert(Class<T> type, Object value) {

    MemberDetails details = null;// w w  w. ja v  a 2s .c om

    if (value instanceof JSONObject) {

        JSONObject obj = (JSONObject) value;

        details = new MemberDetails();

        try {
            BeanUtils.populate(details, obj);
        } catch (IllegalAccessException | InvocationTargetException e) {
            ContextedRuntimeException cre;
            cre = new ContextedRuntimeException("Bean population error", e);
            cre.addContextValue("value", obj);
            throw cre;
        }

    }

    return type.cast(details);
}