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:org.agnitas.web.UserFormEditAction.java

public List<DynaBean> getUserFromList(HttpServletRequest request)
        throws IllegalAccessException, InstantiationException {
    ApplicationContext aContext = getWebApplicationContext();
    JdbcTemplate aTemplate = new JdbcTemplate((DataSource) aContext.getBean("dataSource"));

    String sqlStatement = "SELECT form_id, formname, description FROM userform_tbl WHERE company_id="
            + AgnUtils.getCompanyID(request) + " ORDER BY formname";

    List<Map> tmpList = aTemplate.queryForList(sqlStatement);

    DynaProperty[] properties = new DynaProperty[] { new DynaProperty("formid", Long.class),
            new DynaProperty("formname", String.class), new DynaProperty("description", String.class) };

    if (AgnUtils.isOracleDB()) {
        properties = new DynaProperty[] { new DynaProperty("formid", BigDecimal.class),
                new DynaProperty("formname", String.class), new DynaProperty("description", String.class) };
    }//from  w  w  w.  ja  v  a  2 s  .c o m

    BasicDynaClass dynaClass = new BasicDynaClass("userform", null, properties);

    List<DynaBean> result = new ArrayList<DynaBean>();
    for (Map row : tmpList) {
        DynaBean newBean = dynaClass.newInstance();
        newBean.set("formid", row.get("FORM_ID"));
        newBean.set("formname", row.get("FORMNAME"));
        newBean.set("description", row.get("DESCRIPTION"));
        result.add(newBean);
    }
    return result;
}

From source file:org.apache.action2.legacy.DynaBeanPropertyAccessor.java

public void setProperty(Map context, Object target, Object name, Object value) throws OgnlException {

    if (target instanceof DynaBean && name != null) {
        DynaBean bean = (DynaBean) target;
        String key = name.toString();
        bean.set(key, value);
    }/*from   w w w .  j av a2s  .co  m*/
}

From source file:org.apache.ddlutils.dynabean.TestDynaSqlQueries.java

/**
 * Tests insertion & reading of auto-increment columns.
 *//*  www  .jav  a  2s.  c o  m*/
public void testAutoIncrement() throws Exception {
    // we need special catering for Sybase which does not support identity for INTEGER columns
    final String modelXml;

    if (SybasePlatform.DATABASENAME.equals(getPlatform().getName())) {
        modelXml = "<?xml version='1.0' encoding='ISO-8859-1'?>\n" + "<database xmlns='"
                + DatabaseIO.DDLUTILS_NAMESPACE + "' name='ddlutils'>\n" + "  <table name='TestTable'>\n"
                + "    <column name='TheId' type='NUMERIC' size='12,0' primaryKey='true' required='true' autoIncrement='true'/>\n"
                + "    <column name='TheText' type='VARCHAR' size='15'/>\n" + "  </table>\n" + "</database>";
    } else {
        modelXml = "<?xml version='1.0' encoding='ISO-8859-1'?>\n" + "<database xmlns='"
                + DatabaseIO.DDLUTILS_NAMESPACE + "' name='ddlutils'>\n" + "  <table name='TestTable'>\n"
                + "    <column name='TheId' type='INTEGER' primaryKey='true' required='true' autoIncrement='true'/>\n"
                + "    <column name='TheText' type='VARCHAR' size='15'/>\n" + "  </table>\n" + "</database>";
    }

    createDatabase(modelXml);

    // we're inserting the rows manually via beans since we do want to
    // check the back-reading of the auto-increment columns
    SqlDynaClass dynaClass = getModel().getDynaClassFor("TestTable");
    DynaBean bean = null;
    Object id1 = null;
    Object id2 = null;
    Object id3 = null;

    bean = dynaClass.newInstance();
    bean.set("TheText", "Text 1");
    getPlatform().insert(getModel(), bean);
    if (getPlatformInfo().isLastIdentityValueReadable()) {
        // we cannot know the value for sure (though it usually will be 1)
        id1 = getPropertyValue(bean, "TheId");
        assertNotNull(id1);
    }
    bean = dynaClass.newInstance();
    bean.set("TheText", "Text 2");
    getPlatform().insert(getModel(), bean);
    if (getPlatformInfo().isLastIdentityValueReadable()) {
        // we cannot know the value for sure (though it usually will be 2)
        id2 = getPropertyValue(bean, "TheId");
        assertNotNull(id2);
    }
    bean = dynaClass.newInstance();
    bean.set("TheText", "Text 3");
    getPlatform().insert(getModel(), bean);
    if (getPlatformInfo().isLastIdentityValueReadable()) {
        // we cannot know the value for sure (though it usually will be 3)
        id3 = getPropertyValue(bean, "TheId");
        assertNotNull(id3);
    }

    List beans = getPlatform().fetch(getModel(), "SELECT * FROM " + asIdentifier("TestTable"),
            new Table[] { getModel().getTable(0) });

    assertEquals(3, beans.size());

    bean = (DynaBean) beans.get(0);
    if (getPlatformInfo().isLastIdentityValueReadable()) {
        assertEquals(id1, getPropertyValue(bean, "TheId"));
    } else {
        assertNotNull(getPropertyValue(bean, "TheId"));
    }
    assertEquals("Text 1", getPropertyValue(bean, "TheText"));

    bean = (DynaBean) beans.get(1);
    if (getPlatformInfo().isLastIdentityValueReadable()) {
        assertEquals(id2, getPropertyValue(bean, "TheId"));
    } else {
        assertNotNull(getPropertyValue(bean, "TheId"));
    }
    assertEquals("Text 2", getPropertyValue(bean, "TheText"));

    bean = (DynaBean) beans.get(2);
    if (getPlatformInfo().isLastIdentityValueReadable()) {
        assertEquals(id3, getPropertyValue(bean, "TheId"));
    } else {
        assertNotNull(getPropertyValue(bean, "TheId"));
    }
    assertEquals("Text 3", getPropertyValue(bean, "TheText"));
}

From source file:org.apache.ddlutils.dynabean.TestDynaSqlQueries.java

/**
 * Tests the insert method./*from   www  .  jav a2s  .c  o m*/
 */
public void testInsertSingle() throws Exception {
    createDatabase("<?xml version='1.0' encoding='ISO-8859-1'?>\n" + "<database xmlns='"
            + DatabaseIO.DDLUTILS_NAMESPACE + "' name='ddlutils'>\n" + "  <table name='TestTable'>\n"
            + "    <column name='TheId' type='INTEGER' primaryKey='true' required='true'/>\n"
            + "    <column name='TheText' type='VARCHAR' size='15'/>\n" + "  </table>\n" + "</database>");

    SqlDynaClass dynaClass = SqlDynaClass.newInstance(getModel().getTable(0));
    DynaBean dynaBean = new SqlDynaBean(dynaClass);

    dynaBean.set("TheId", new Integer(1));
    dynaBean.set("TheText", "Text 1");

    getPlatform().insert(getModel(), dynaBean);

    List beans = getPlatform().fetch(getModel(), "SELECT * FROM " + asIdentifier("TestTable"),
            new Table[] { getModel().getTable(0) });

    assertEquals(1, beans.size());

    DynaBean bean = (DynaBean) beans.get(0);

    assertEquals(new Integer(1), getPropertyValue(bean, "TheId"));
    assertEquals("Text 1", getPropertyValue(bean, "TheText"));
}

From source file:org.apache.ddlutils.dynabean.TestDynaSqlQueries.java

/**
 * Tests the insert method.//www .j a  v a 2s . c om
 */
public void testInsertMultiple() throws Exception {
    createDatabase("<?xml version='1.0' encoding='ISO-8859-1'?>\n" + "<database xmlns='"
            + DatabaseIO.DDLUTILS_NAMESPACE + "' name='ddlutils'>\n" + "  <table name='TestTable'>\n"
            + "    <column name='TheId' type='INTEGER' primaryKey='true' required='true'/>\n"
            + "    <column name='TheText' type='VARCHAR' size='15'/>\n" + "  </table>\n" + "</database>");

    SqlDynaClass dynaClass = SqlDynaClass.newInstance(getModel().getTable(0));
    DynaBean dynaBean1 = new SqlDynaBean(dynaClass);
    DynaBean dynaBean2 = new SqlDynaBean(dynaClass);
    DynaBean dynaBean3 = new SqlDynaBean(dynaClass);

    dynaBean1.set("TheId", new Integer(1));
    dynaBean1.set("TheText", "Text 1");
    dynaBean2.set("TheId", new Integer(2));
    dynaBean2.set("TheText", "Text 2");
    dynaBean3.set("TheId", new Integer(3));
    dynaBean3.set("TheText", "Text 3");

    List dynaBeans = new ArrayList();

    dynaBeans.add(dynaBean1);
    dynaBeans.add(dynaBean2);
    dynaBeans.add(dynaBean3);

    getPlatform().insert(getModel(), dynaBeans);

    List beans = getPlatform().fetch(getModel(), "SELECT * FROM " + asIdentifier("TestTable"),
            new Table[] { getModel().getTable(0) });

    assertEquals(3, beans.size());

    DynaBean bean = (DynaBean) beans.get(0);

    assertEquals(new Integer(1), getPropertyValue(bean, "TheId"));
    assertEquals("Text 1", getPropertyValue(bean, "TheText"));

    bean = (DynaBean) beans.get(1);

    assertEquals(new Integer(2), getPropertyValue(bean, "TheId"));
    assertEquals("Text 2", getPropertyValue(bean, "TheText"));

    bean = (DynaBean) beans.get(2);

    assertEquals(new Integer(3), getPropertyValue(bean, "TheId"));
    assertEquals("Text 3", getPropertyValue(bean, "TheText"));
}

From source file:org.apache.ddlutils.dynabean.TestDynaSqlQueries.java

/**
 * Tests the update method./*w  w w  .j ava 2 s .  c  o m*/
 */
public void testUpdate() throws Exception {
    createDatabase("<?xml version='1.0' encoding='ISO-8859-1'?>\n" + "<database xmlns='"
            + DatabaseIO.DDLUTILS_NAMESPACE + "' name='ddlutils'>\n" + "  <table name='TestTable'>\n"
            + "    <column name='TheId' type='INTEGER' primaryKey='true' required='true'/>\n"
            + "    <column name='TheText' type='VARCHAR' size='15'/>\n" + "  </table>\n" + "</database>");

    insertData("<?xml version='1.0' encoding='ISO-8859-1'?>\n" + "<data>\n"
            + "  <TestTable TheId='1' TheText='Text 1'/>\n" + "</data>");

    SqlDynaClass dynaClass = SqlDynaClass.newInstance(getModel().getTable(0));
    DynaBean dynaBean = new SqlDynaBean(dynaClass);

    dynaBean.set("TheId", new Integer(1));
    dynaBean.set("TheText", "Text 10");

    getPlatform().update(getModel(), dynaBean);

    List beans = getPlatform().fetch(getModel(), "SELECT * FROM " + asIdentifier("TestTable"),
            new Table[] { getModel().getTable(0) });

    assertEquals(1, beans.size());

    DynaBean bean = (DynaBean) beans.get(0);

    assertEquals(new Integer(1), getPropertyValue(bean, "TheId"));
    assertEquals("Text 10", getPropertyValue(bean, "TheText"));
}

From source file:org.apache.ddlutils.dynabean.TestDynaSqlQueries.java

/**
 * Tests the exists method.//w  w w  . ja  v  a  2s  .c o  m
 */
public void testExists() throws Exception {
    createDatabase("<?xml version='1.0' encoding='ISO-8859-1'?>\n" + "<database xmlns='"
            + DatabaseIO.DDLUTILS_NAMESPACE + "' name='ddlutils'>\n" + "  <table name='TestTable'>\n"
            + "    <column name='TheId' type='INTEGER' primaryKey='true' required='true'/>\n"
            + "    <column name='TheText' type='VARCHAR' size='15'/>\n" + "  </table>\n" + "</database>");

    insertData("<?xml version='1.0' encoding='ISO-8859-1'?>\n" + "<data>\n"
            + "  <TestTable TheId='1' TheText='Text 1'/>\n" + "  <TestTable TheId='3' TheText='Text 3'/>\n"
            + "</data>");

    SqlDynaClass dynaClass = SqlDynaClass.newInstance(getModel().getTable(0));
    DynaBean dynaBean1 = new SqlDynaBean(dynaClass);
    DynaBean dynaBean2 = new SqlDynaBean(dynaClass);
    DynaBean dynaBean3 = new SqlDynaBean(dynaClass);

    dynaBean1.set("TheId", new Integer(1));
    dynaBean1.set("TheText", "Text 1");
    dynaBean2.set("TheId", new Integer(2));
    dynaBean2.set("TheText", "Text 2");
    dynaBean3.set("TheId", new Integer(3));
    dynaBean3.set("TheText", "Text 30");

    assertTrue(getPlatform().exists(getModel(), dynaBean1));
    assertFalse(getPlatform().exists(getModel(), dynaBean2));
    assertTrue(getPlatform().exists(getModel(), dynaBean3));
}

From source file:org.apache.ddlutils.dynabean.TestDynaSqlQueries.java

/**
 * Tests the store method.//from  w  w  w.  jav  a2  s  . c  o m
 */
public void testStoreNew() throws Exception {
    createDatabase("<?xml version='1.0' encoding='ISO-8859-1'?>\n" + "<database xmlns='"
            + DatabaseIO.DDLUTILS_NAMESPACE + "' name='ddlutils'>\n" + "  <table name='TestTable'>\n"
            + "    <column name='TheId' type='INTEGER' primaryKey='true' required='true'/>\n"
            + "    <column name='TheText' type='VARCHAR' size='15'/>\n" + "  </table>\n" + "</database>");

    SqlDynaClass dynaClass = SqlDynaClass.newInstance(getModel().getTable(0));
    DynaBean dynaBean = new SqlDynaBean(dynaClass);

    dynaBean.set("TheId", new Integer(1));
    dynaBean.set("TheText", "Text 1");

    getPlatform().store(getModel(), dynaBean);

    List beans = getPlatform().fetch(getModel(), "SELECT * FROM " + asIdentifier("TestTable"),
            new Table[] { getModel().getTable(0) });

    assertEquals(1, beans.size());

    DynaBean bean = (DynaBean) beans.get(0);

    assertEquals(new Integer(1), getPropertyValue(bean, "TheId"));
    assertEquals("Text 1", getPropertyValue(bean, "TheText"));
}

From source file:org.apache.ddlutils.dynabean.TestDynaSqlQueries.java

/**
 * Tests the store method./*from  w w w .j av a 2  s.c om*/
 */
public void testStoreExisting() throws Exception {
    createDatabase("<?xml version='1.0' encoding='ISO-8859-1'?>\n" + "<database xmlns='"
            + DatabaseIO.DDLUTILS_NAMESPACE + "' name='ddlutils'>\n" + "  <table name='TestTable'>\n"
            + "    <column name='TheId' type='INTEGER' primaryKey='true' required='true'/>\n"
            + "    <column name='TheText' type='VARCHAR' size='15'/>\n" + "  </table>\n" + "</database>");

    insertData("<?xml version='1.0' encoding='ISO-8859-1'?>\n" + "<data>\n"
            + "  <TestTable TheId='1' TheText='Text 1'/>\n" + "</data>");

    SqlDynaClass dynaClass = SqlDynaClass.newInstance(getModel().getTable(0));
    DynaBean dynaBean = new SqlDynaBean(dynaClass);

    dynaBean.set("TheId", new Integer(1));
    dynaBean.set("TheText", "Text 10");

    getPlatform().store(getModel(), dynaBean);

    List beans = getPlatform().fetch(getModel(), "SELECT * FROM " + asIdentifier("TestTable"),
            new Table[] { getModel().getTable(0) });

    assertEquals(1, beans.size());

    DynaBean bean = (DynaBean) beans.get(0);

    assertEquals(new Integer(1), getPropertyValue(bean, "TheId"));
    assertEquals("Text 10", getPropertyValue(bean, "TheText"));
}

From source file:org.apache.ddlutils.io.DataToDatabaseSink.java

/**
 * Directly inserts the given bean into the database.
 * /*from  www  .j  a  v  a  2  s .c  o  m*/
 * @param table The table of the bean
 * @param bean  The bean
 */
private void insertSingleBeanIntoDatabase(Table table, DynaBean bean) throws DataSinkException {
    try {
        boolean needTwoStepInsert = false;
        ForeignKey selfRefFk = null;

        if (!_platform.isIdentityOverrideOn() && _tablesWithSelfIdentityReference.contains(table)) {
            selfRefFk = table.getSelfReferencingForeignKey();

            // in case of a self-reference (fk points to the very row that we're inserting)
            // and (at least) one of the pk columns is an identity column, we first need
            // to insert the row with the fk columns set to null
            Identity pkIdentity = buildIdentityFromPKs(table, bean);
            Identity fkIdentity = buildIdentityFromFK(table, selfRefFk, bean);

            if (pkIdentity.equals(fkIdentity)) {
                if (_tablesWithRequiredSelfReference.contains(table)) {
                    throw new DataSinkException(
                            "Can only insert rows with fk pointing to themselves when all fk columns can be NULL (row pk is "
                                    + pkIdentity + ")");
                } else {
                    needTwoStepInsert = true;
                }
            }
        }

        if (needTwoStepInsert) {
            // we first insert the bean without the fk, then in the second step we update the bean
            // with the row with the identity pk values
            ArrayList fkValues = new ArrayList();

            for (int idx = 0; idx < selfRefFk.getReferenceCount(); idx++) {
                String columnName = selfRefFk.getReference(idx).getLocalColumnName();

                fkValues.add(bean.get(columnName));
                bean.set(columnName, null);
            }
            _platform.insert(_connection, _model, bean);
            for (int idx = 0; idx < selfRefFk.getReferenceCount(); idx++) {
                bean.set(selfRefFk.getReference(idx).getLocalColumnName(), fkValues.get(idx));
            }
            _platform.update(_connection, _model, bean);
        } else {
            _platform.insert(_connection, _model, bean);
        }
        if (!_connection.getAutoCommit()) {
            _connection.commit();
        }
    } catch (Exception ex) {
        if (_haltOnErrors) {
            _platform.returnConnection(_connection);
            throw new DataSinkException(ex);
        } else {
            _log.warn("Exception while inserting a row into the database", ex);
        }
    }
}