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:DynaBeansExampleV1.java

private static Object createMovieBean() throws Exception {

    // first create the properties
    DynaProperty properties[] = new DynaProperty[] { new DynaProperty("title", String.class),
            new DynaProperty("dateOfRelease", Date.class), new DynaProperty("keywords", String[].class),
            new DynaProperty("genre", Map.class), new DynaProperty("actors", List.class),
            new DynaProperty("director", DynaBean.class) };

    // next using the properties define the class
    DynaClass movieClass = new BasicDynaClass("movie", null, properties);

    // now, with the class, create a new instance
    DynaBean movieBean = movieClass.newInstance();

    // set its properties
    movieBean.set("title", "The Italian Job");
    movieBean.set("dateOfRelease", new GregorianCalendar(1969, 0, 1).getTime());
    movieBean.set("keywords", new String[] { "Italy", "Bank Robbery" });

    Map genre = new HashMap();
    genre.put("THR", "Thriller");

    movieBean.set("genre", genre);
    movieBean.set("genre", "ACT", "Action");

    DynaBean director = createPersonBean();
    director.set("name", "Peter Collinson");
    director.set("gender", new Integer(1));

    movieBean.set("director", director);

    return movieBean;
}

From source file:de.maklerpoint.office.LocalDatabase.DdlUtilsHelper.java

/**
 * /*from   w w w  . jav  a  2 s.  c  o m*/
 * @param dataSource
 * @param database
 * @param table
 * @param data
 */

public static void insertData(DataSource dataSource, Database database, String table, Object[][] data) {
    Platform platform = PlatformFactory.createNewPlatformInstance(dataSource);

    // "author" is a table of the model
    DynaBean tab = database.createDynaBeanFor(table, false);

    // "name" and "whatever" are columns of table "author"

    for (int i = 0; i < data.length; i++) {
        tab.set((String) data[i][0], data[i][1]);
    }

    platform.insert(database, tab);
}

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

/**
 *  dyna bean./* w  w w  . j  a va2s. c o  m*/
 * 
 * <h3>:</h3>
 * 
 * <blockquote>
 * 
 * <pre class="code">
 * 
 * Map{@code <String, Class<?>>} typeMap = new HashMap<>();
 * typeMap.put("address", java.util.Map.class);
 * typeMap.put("firstName", String.class);
 * typeMap.put("lastName", String.class);
 * 
 * Map{@code <String, Object>} valueMap = new HashMap<>();
 * valueMap.put("address", new HashMap());
 * valueMap.put("firstName", "Fred");
 * valueMap.put("lastName", "Flintstone");
 * 
 * DynaBean dynaBean = BeanUtil.newDynaBean(typeMap, valueMap);
 * LOGGER.debug(JsonUtil.format(dynaBean));
 * 
 * </pre>
 * 
 * <b>:</b>
 * 
 * <pre class="code">
 * {
 *         "lastName": "Flintstone",
 *         "address": {},
 *         "firstName": "Fred"
 *     }
 * </pre>
 * 
 * </blockquote>
 *
 * @param typeMap
 *            ??  map
 * @param valueMap
 *            ??   map
 * @return the dyna bean
 * @throws NullPointerException
 *              <code>typeMap </code>  <code>valueMap</code> null
 * @throws IllegalArgumentException
 *              <code>typeMap.size() != valueMap.size()</code>
 * @since 1.8.1 change name
 */
public static DynaBean newDynaBean(Map<String, Class<?>> typeMap, Map<String, Object> valueMap) {
    Validate.notNull(typeMap, "typeMap can't be null!");
    Validate.notNull(valueMap, "valueMap can't be null!");
    Validate.isTrue(typeMap.size() == valueMap.size(), "typeMap size:[%s] != valueMap size:[%s]",
            typeMap.size(), valueMap.size());

    //*********************************************************************************
    try {
        DynaClass dynaClass = new BasicDynaClass(null, null, toDynaPropertyArray(typeMap));

        DynaBean dynaBean = dynaClass.newInstance();
        for (Map.Entry<String, Object> entry : valueMap.entrySet()) {
            dynaBean.set(entry.getKey(), entry.getValue());
        }
        return dynaBean;
    } catch (IllegalAccessException | InstantiationException e) {
        LOGGER.error("", e);
        throw new BeanUtilException(e);
    }
}

From source file:net.sf.jrf.domain.PersistentObjectDynaClass.java

/** Transfers the properties from the bean to a new <code>PersistentObject</code>.  This
* method may be used for "stateful" and "stateless" contexts.  Stateful contexts maintain
* a copy of the <code>PersistentObject</code> for reference after properties of the bean has been updated,
* usually through a user interface. On the other hand, stateless contexts do not maintain a copy of the
* <code>PersistentObject</code> and rely on setting the <code>PersistentState</code> in the bean itself.
* In fact, this method  determines stateless status by examining the <code>PersistentState</code> in the bean.
* If the value is non-null, <code>PersistentState</code> of bean will be transferred to the
* <code>PersistentObject</code>.
* Otherwise the state of the <code>PersistentObject</code> will be <code>NewPersistentState</code>.
* <p>/*from   w w w. j  a v a2  s . c om*/
* Indexed and mapped properties will not be copied.  This functionality may be implemented
* in the future.
* @param poBean <code>DynaBean</code> instance to process that must return
* a <code>PersistentObjectDynaClass</code> instance from a call to <code>getDynaClass()</code>.
* @return a generated <code>PersistentObject</code> from the bean.
* @see #beanToPersistentObject(PersistentObjectDynaBean,PersistentObject)
*/
public static PersistentObject beanToPersistentObject(PersistentObjectDynaBean poBean) {
    PersistentObjectDynaClass c = poBean.getPersistentObjectDynaClass();
    PersistentObject aPO = c.newPersistentObject();
    DynaBean bean = (DynaBean) poBean;
    // If state is null, force to new:
    if (bean.get("persistentState") == null)
        bean.set("persistentState", new NewPersistentState());
    beanToPersistentObject(c, bean, aPO, false);
    return aPO;
}

From source file:net.sf.excelutils.ExcelParser.java

/**
 * get the value from context by the expression
 * /*from  w  w w  .  ja  v a 2  s.  com*/
 * @param expr
 * @param context data object
 * @return Object the value of the expr
 */
public static Object getValue(Object context, String expr) {
    Object value = null;
    try {
        value = PropertyUtils.getProperty(context, expr);
        // ?
        int index = expr.indexOf(INDEXED_DELIM);
        int index2 = expr.indexOf(INDEXED_DELIM2, index);
        if (index >= 0 && index2 > 0 && index2 > index) {
            // ?,?1[]
            if (expr.indexOf(INDEXED_DELIM, index2) >= 0) {
                DynaBean bean = new LazyDynaBean();
                bean.set("list_value", value);
                value = getValue(bean, "list_value" + expr.substring(index2 + 1));
            }
        }
    } catch (Exception e) {
        return null;
    }
    return value;
}

From source file:net.sf.jrf.domain.PersistentObjectDynaClass.java

/** Initializes a bean to the default settings of the <code>PersistentObject</code>.
* Any default values specified will be populated in the bean.
* @param poBean <code>PersistentObjectDynaBean</code> instance.
*//*from   ww  w  .j  a va 2s .  c o  m*/
public static void resetBean(PersistentObjectDynaBean poBean) {
    DynaProperty[] props = poBean.getPersistentObjectDynaClass().getDynaProperties();
    DynaBean bean = (DynaBean) poBean;
    for (int i = 0; i < props.length; i++) {
        PersistentObjectDynaProperty cp;
        if ((cp = PersistentObjectDynaProperty.getPOProperty(props[i])) != null) {
            if (cp.getName().equals("persistentState"))
                bean.set(cp.getName(), new NewPersistentState());
            else if (cp.isDbColumn() && cp.getDefaultValue() != null)
                bean.set(cp.getName(), cp.getDefaultValue());
        }
    }
}

From source file:net.sf.jrf.domain.PersistentObjectDynaClass.java

/** Updates bean from a given <code>PersistentObject</code>.
* @param aPO <code>PersistentObject</code> to read.
* @param poBean <code>PersistentObjectDynaBean</code> instance to update.
*//* w w  w . j  a  v a 2 s  . c om*/
public static void persistentObjectToBean(PersistentObject aPO, PersistentObjectDynaBean poBean) {
    DynaBean bean = (DynaBean) poBean;
    DynaProperty[] props = poBean.getPersistentObjectDynaClass().getDynaProperties();
    for (int i = 0; i < props.length; i++) {
        PersistentObjectDynaProperty cp;
        if ((cp = PersistentObjectDynaProperty.getPOProperty(props[i])) != null) {
            Object value = cp.get(aPO);
            bean.set(cp.getName(), value);
        }
    }
}

From source file:junit.mock.TestDynaPropertiesMO.java

private DynaBean createDynaBean() throws Exception {
    DynaProperty[] props = new DynaProperty[] { new DynaProperty("id", String.class),
            new DynaProperty("responsetime", Long.class) };
    BasicDynaClass dynaClass = new BasicDynaClass("requesttime", null, props);

    DynaBean bean = dynaClass.newInstance();
    bean.set("id", "12345");
    bean.set("responsetime", new Long(500));

    return bean;//from   w  w  w  .j av  a  2 s . c  o m
}

From source file:com.manning.junitbook.ch14.servlets.TestAdminServlet.java

/**
 * Creates a java.util.Collection of DynaBean objects to show in the JSP.
 * //from  w w w . ja  v a2 s . com
 * @return
 * @throws Exception
 */
private Collection<DynaBean> createCommandResult() throws Exception {
    List<DynaBean> results = new ArrayList<DynaBean>();

    DynaProperty[] props = new DynaProperty[] { new DynaProperty("id", String.class),
            new DynaProperty("responsetime", Long.class) };
    BasicDynaClass dynaClass = new BasicDynaClass("requesttime", null, props);

    DynaBean request1 = dynaClass.newInstance();
    request1.set("id", "12345");
    request1.set("responsetime", new Long(500));
    results.add(request1);

    DynaBean request2 = dynaClass.newInstance();
    request1.set("id", "56789");
    request1.set("responsetime", new Long(430));
    results.add(request2);

    return results;
}

From source file:com.ms.commons.summer.web.util.json.JsonBeanUtils.java

/**
 * Creates a JSONDynaBean from a JSONObject.
 *///www . jav  a2 s .c  o m
public static Object toBean(JSONObject jsonObject) {
    if (jsonObject == null || jsonObject.isNullObject()) {
        return null;
    }

    DynaBean dynaBean = null;

    Map props = JSONUtils.getProperties(jsonObject);
    dynaBean = JSONUtils.newDynaBean(jsonObject, new JsonConfig());
    for (Iterator entries = jsonObject.names().iterator(); entries.hasNext();) {
        String name = (String) entries.next();
        String key = JSONUtils.convertToJavaIdentifier(name, new JsonConfig());
        Class type = (Class) props.get(name);
        Object value = jsonObject.get(name);
        try {
            if (!JSONUtils.isNull(value)) {
                if (value instanceof JSONArray) {
                    dynaBean.set(key, JSONArray.toCollection((JSONArray) value));
                } else if (String.class.isAssignableFrom(type) || Boolean.class.isAssignableFrom(type)
                        || JSONUtils.isNumber(type) || Character.class.isAssignableFrom(type)
                        || JSONFunction.class.isAssignableFrom(type)) {
                    dynaBean.set(key, value);
                } else {
                    dynaBean.set(key, toBean((JSONObject) value));
                }
            } else {
                if (type.isPrimitive()) {
                    // assume assigned default value
                    log.warn("Tried to assign null value to " + key + ":" + type.getName());
                    dynaBean.set(key, JSONUtils.getMorpherRegistry().morph(type, null));
                } else {
                    dynaBean.set(key, null);
                }
            }
        } catch (JSONException jsone) {
            throw jsone;
        } catch (Exception e) {
            throw new JSONException("Error while setting property=" + name + " type" + type, e);
        }
    }

    return dynaBean;
}