Example usage for org.apache.commons.beanutils DynaBean set

List of usage examples for org.apache.commons.beanutils DynaBean set

Introduction

In this page you can find the example usage for org.apache.commons.beanutils DynaBean set.

Prototype

public void set(String name, Object value);

Source Link

Document

Set the value of a simple property with the specified name.

Usage

From source file:es.caib.zkib.jxpath.ri.model.dynabeans.TestDynaBeanFactory.java

/**
 *//* w  w w .  ja va 2s.co m*/
public boolean createObject(JXPathContext context, Pointer pointer, Object parent, String name, int index) {
    if (name.equals("nestedBean")) {
        ((DynaBean) parent).set("nestedBean", new NestedTestBean("newName"));
        return true;
    } else if (name.equals("beans")) {
        DynaBean bean = (DynaBean) parent;
        Object beans[] = (Object[]) bean.get("beans");
        if (beans == null || index >= beans.length) {
            beans = new NestedTestBean[index + 1];
            bean.set("beans", beans);
        }
        beans[index] = new NestedTestBean("newName");
        return true;
    } else if (name.equals("integers")) {
        DynaBean bean = (DynaBean) parent;
        bean.set("integers", index, new Integer(0));
        return true;
    }
    return false;
}

From source file:net.rrm.ehour.persistence.dbvalidator.DerbyDbValidator.java

private void updateVersion(Platform platform, Database database) {
    DynaBean configuration = database.createDynaBeanFor("CONFIGURATION", false);
    configuration.set("config_key", "version");
    platform.delete(database, configuration);

    configuration.set("config_value", requiredDbVersion);

    platform.insert(database, configuration);
}

From source file:com.feilong.commons.core.bean.BeanUtilTest.java

/**
 * Demo dyna beans./*ww w .java 2s. co m*/
 *
 * @throws Exception
 *             the exception
 */
@Test
public void demoDynaBeans() throws Exception {

    log.debug(StringUtils.center(" demoDynaBeans ", 40, "="));

    // creating a DynaBean  
    DynaProperty[] dynaBeanProperties = new DynaProperty[] { //DynaProperty  
            new DynaProperty("name", String.class), new DynaProperty("inPrice", Double.class),
            new DynaProperty("outPrice", Double.class), };
    BasicDynaClass cargoClass = new BasicDynaClass("Cargo", BasicDynaBean.class, dynaBeanProperties); //BasicDynaClass  BasicDynaBean  
    DynaBean cargo = cargoClass.newInstance();//DynaBean  

    // accessing a DynaBean  
    cargo.set("name", "Instant Noodles");
    cargo.set("inPrice", new Double(21.3));
    cargo.set("outPrice", new Double(23.8));
    log.debug("name: " + cargo.get("name"));
    log.debug("inPrice: " + cargo.get("inPrice"));
    log.debug("outPrice: " + cargo.get("outPrice"));
}

From source file:io.coala.experimental.dynabean.DynaBeanTest.java

@Test
public void testDynaBeans() throws Exception {
    // for usage, see
    // http://commons.apache.org/proper/commons-beanutils/javadocs/v1.9.2/apidocs/org/apache/commons/beanutils/package-summary.html#package_description

    final DynaBean dynaBean = new LazyDynaBean(); // Create LazyDynaBean
    final MutableDynaClass dynaClass = (MutableDynaClass) dynaBean.getDynaClass(); // get DynaClass

    dynaClass.add("amount", java.lang.Integer.class); // add property
    dynaClass.add("myMap", java.util.TreeMap.class); // add mapped property

    final DynaBean employee = dynaClass.newInstance();

    // TODO experiment with Jackson's AnnotationIntrospector to annotate
    // DynaBean#get(...) method with @JsonAnyGetter and #set(...) method
    // with @JsonAnySetter

    employee.set("address", new HashMap<String, String>());
    employee.set("activeEmployee", Boolean.FALSE);
    employee.set("firstName", "Fred");
    employee.set("lastName", "Flintstone");

    LOG.trace("Employee: " + JsonUtil.toPrettyJSON(employee));

    // set all <activeEmployee> attribute values to <true>
    final BeanPropertyValueChangeClosure closure = new BeanPropertyValueChangeClosure("activeEmployee",
            Boolean.TRUE);//from  www  . ja  va 2  s. c  o  m

    final Collection<?> employees = Collections.singleton(employee);
    LOG.trace("Mutated employees: " + JsonUtil.toPrettyJSON(employees));

    // update the Collection
    CollectionUtils.forAllDo(employees, closure);

    // filter for beans with <activeEmployee> set to <false>
    final BeanPropertyValueEqualsPredicate predicate = new BeanPropertyValueEqualsPredicate("lastName",
            "Flintstone");

    // filter the Collection
    CollectionUtils.filter(employees, predicate);
    LOG.trace("Filtered employees: " + JsonUtil.toPrettyJSON(employees));
}

From source file:com.feilong.core.bean.BeanUtilTest.java

@Test
public void testLazyDynaBean() {
    DynaBean dynaBean = new LazyDynaBean();

    dynaBean.set("first", "1"); //simple
    dynaBean.set("num", "second", "2"); //map
    dynaBean.set("num", "third", "3"); //map
    dynaBean.set("name", 0, "one"); //indexed
    dynaBean.set("name", 1, "two"); //indexed
    LOGGER.debug(JsonUtil.format(dynaBean));
}

From source file:com.emergya.persistenceGeo.dao.impl.PostgisDBManagementDaoHibernateImpl.java

@Override
public boolean createLayerTable(String tableName, int srsCode, GeometryType geometryType) {
    List<DynaBean> columns = new ArrayList<DynaBean>();
    DynaBean fieldId = new LazyDynaBean();
    fieldId.set("name", "id");
    fieldId.set("type", "serial");
    fieldId.set("primaryKey", true);

    columns.add(fieldId);//  w  w w .  jav a2 s  .c o m

    DynaBean fieldDesc = new LazyDynaBean();
    fieldDesc.set("name", "descripcion");
    fieldDesc.set("type", "character varying(512)");
    fieldDesc.set("primaryKey", false);
    columns.add(fieldDesc);

    return createLayerTable(tableName, columns, srsCode, geometryType);
}

From source file:net.mlw.vlh.adapter.jdbc.dynabean.DefaultDynaBeanAdapter.java

public List processResultSet(String name, ResultSet result, int numberPerPage, ValueListInfo info)
        throws SQLException {
    List list = new ArrayList();

    ResultSetDynaClass rsdc = new ResultSetDynaClass(result, false, isUseName());
    BasicDynaClass bdc = new BasicDynaClass(name, BasicDynaBean.class, rsdc.getDynaProperties());

    int rowIndex = 0;
    for (Iterator rows = rsdc.iterator(); rows.hasNext() && rowIndex < numberPerPage; rowIndex++) {
        try {/*w ww . j a v  a2  s. c  om*/
            DynaBean oldRow = (DynaBean) rows.next();
            DynaBean newRow = bdc.newInstance();

            DynaProperty[] properties = oldRow.getDynaClass().getDynaProperties();
            for (int i = 0, length = properties.length; i < length; i++) {
                String propertyName = properties[i].getName();
                Object value = oldRow.get(propertyName);
                newRow.set(propertyName, value);
            }

            list.add(newRow);
        } catch (Exception e) {
            LOGGER.error(e);
        }
    }

    return list;
}

From source file:kenh.xscript.database.beans.ResultSetBean.java

/**
 * Use result set to initial a bean./*from   w  ww  . j a v  a2 s . c  o  m*/
 * 
 * @param rs
 * @param includeFieldName
 * @throws SQLException
 * @throws IllegalAccessException
 * @throws InstantiationException
 */
public ResultSetBean(ResultSet rs, boolean includeFieldName)
        throws SQLException, IllegalAccessException, InstantiationException {
    include_field_name = includeFieldName;

    LazyDynaClass beanClass = new LazyDynaClass();

    ResultSetMetaData m = rs.getMetaData();
    for (int i = 1; i <= m.getColumnCount(); i++) {
        Column c = new Column();

        try {
            c.catalogName = m.getCatalogName(i);
        } catch (SQLException e) {
        }
        try {
            c.className = m.getColumnClassName(i);
        } catch (SQLException e) {
        }
        try {
            c.displaySize = m.getColumnDisplaySize(i);
        } catch (SQLException e) {
        }
        try {
            c.label = m.getColumnLabel(i);
        } catch (SQLException e) {
        }
        try {
            c.name = m.getColumnName(i);
        } catch (SQLException e) {
        }
        try {
            c.type = m.getColumnType(i);
        } catch (SQLException e) {
        }
        try {
            c.typeName = m.getColumnTypeName(i);
        } catch (SQLException e) {
        }
        try {
            c.precision = m.getPrecision(i);
        } catch (SQLException e) {
        }
        try {
            c.scale = m.getScale(i);
        } catch (SQLException e) {
        }
        try {
            c.schemaName = m.getSchemaName(i);
        } catch (SQLException e) {
        }
        try {
            c.tableName = m.getTableName(i);
        } catch (SQLException e) {
        }

        beanClass.add(m.getColumnLabel(i).toLowerCase());
        beanClass.add("" + i);

        cols.add(c);
    }

    DynaBean colBean = beanClass.newInstance();
    int i = 1;
    for (Column col : cols) {
        String field = col.getLabel().toLowerCase();
        colBean.set(field, col.getLabel());
        colBean.set("" + i, col.getLabel());
        i++;
    }

    if (include_field_name)
        rows.add(colBean);

    while (rs.next()) {
        DynaBean bean = beanClass.newInstance();
        i = 1;
        for (Column c : cols) {
            String field = c.getLabel().toLowerCase();
            Object obj = rs.getObject(field);
            bean.set(field, obj);
            bean.set("" + i, obj);
            i++;
        }
        rows.add(bean);
    }

}

From source file:com.hr.struts.controller.Employee.java

public ActionForward searchEmployee(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    this.service.setEmployees(getEmployees(connexion(request, response)));
    ArrayList results;//from   w ww . j  av a 2s .com
    DynaBean searchForm = (DynaBean) form;

    // Perform employee search based on the criteria entered. 
    String name = (String) searchForm.get("name");
    String ssNum = (String) searchForm.get("ssNum");
    String phone = (String) searchForm.get("phone");
    if (name != null && name.trim().length() > 0) {
        results = service.searchByName(name);
    } else if (ssNum != null && ssNum.trim().length() > 0) {
        results = service.searchBySsNum(ssNum.trim());
    } else {
        results = service.searchByPhone(phone.trim());
    }

    // Place search results in EmployeeSearchForm bean for access in the JSP. 
    searchForm.set("results", results);

    // Forward control to this Action's input page.
    return mapping.getInputForward();
}

From source file:com.hr.struts.controller.Employee.java

public ActionForward showEmployees(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    ArrayList results;//from  w w  w . j  a v a2  s  .c  o m
    this.service.setEmployees(getEmployees(connexion(request, response)));
    DynaBean showForm = (DynaBean) form;

    // Perform the show all the employees function.
    results = service.findAll();

    // Cible par defaut
    String cible = new String("succes");

    // Cible en cas d'echec
    if (results == null) {
        cible = new String("echec");
        ActionMessages errors = new ActionMessages();
        errors.add(null, new ActionMessage("error.show.employees.notfound"));
        // Signalement des erreurs a la page d'origine
        if (!errors.isEmpty()) {
            saveErrors(request, errors);
        }
    } else {
        // Place search results in EmployeesShowForm for access by JSP.
        showForm.set("results", results);
    }
    // Transmission a la vue appropriee
    return (mapping.findForward(cible));
}