Java Code Examples for org.apache.commons.beanutils.DynaBean

The following examples show how to use org.apache.commons.beanutils.DynaBean. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Override
public PaginatedListImpl<DynaBean> call() throws Exception {
	try {
		PaginatedListImpl<Recipient> recipientPaginatedList = recipientDao.getRecipients(companyID, columns, sqlStatementForData, sqlParametersForData, sortCriterion, sortedAscending, pageNumber, rownums);
		
		// Convert PaginatedListImpl of Recipient into PaginatedListImpl of DynaBean
		List<DynaBean> partialListOfDynaBeans = convertPaginatedListToDynaBean(recipientPaginatedList);
		
		return new PaginatedListImpl<>(partialListOfDynaBeans,
				recipientPaginatedList.getFullListSize(),
				recipientPaginatedList.getPageSize(),
				recipientPaginatedList.getPageNumber(),
				recipientPaginatedList.getSortCriterion(),
				recipientPaginatedList.getSortDirection());
	} catch (Exception e) {
		logger.error("Error executing RecipientBeanQueryWorker", e);
		error = e;
		return null;
	}
}
 
Example 2
Source Project: gemfirexd-oss   Source File: PlatformImplBase.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Creates the SQL for updating an object of the given type. If a concrete bean is given,
 * then a concrete update statement is created, otherwise an update statement usable in a
 * prepared statement is build.
 * 
 * @param model       The database model
 * @param dynaClass   The type
 * @param primaryKeys The primary keys
 * @param properties  The properties to write
 * @param oldBean     Contains column values to identify the rows to update (i.e. for the WHERE clause)
 * @param newBean     Contains the new column values to write
 * @return The SQL required to update the instance
 */
protected String createUpdateSql(Database model, SqlDynaClass dynaClass, SqlDynaProperty[] primaryKeys, SqlDynaProperty[] properties, DynaBean oldBean, DynaBean newBean)
{
    Table   table           = model.findTable(dynaClass.getTableName());
    HashMap oldColumnValues = toColumnValues(primaryKeys, oldBean);
    HashMap newColumnValues = toColumnValues(properties, newBean);

    if (primaryKeys.length == 0)
    {
        _log.info("Cannot update instances of type " + dynaClass + " because it has no primary keys");
        return null;
    }
    else
    {
        return _builder.getUpdateSql(table, oldColumnValues, newColumnValues, newBean == null);
    }
}
 
Example 3
Source Project: gemfirexd-oss   Source File: PlatformImplBase.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public String getUpdateSql(Database model, DynaBean dynaBean)
{
    SqlDynaClass      dynaClass      = model.getDynaClassFor(dynaBean);
    SqlDynaProperty[] primaryKeys    = dynaClass.getPrimaryKeyProperties();
    SqlDynaProperty[] nonPrimaryKeys = dynaClass.getNonPrimaryKeyProperties();

    if (primaryKeys.length == 0)
    {
        _log.info("Cannot update instances of type " + dynaClass + " because it has no primary keys");
        return null;
    }
    else
    {
        return createUpdateSql(model, dynaClass, primaryKeys, nonPrimaryKeys, dynaBean);
    }
}
 
Example 4
Source Project: gemfirexd-oss   Source File: DataWriter.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Writes the beans contained in the given iterator.
 * 
 * @param beans The beans iterator
 */
public void write(Iterator beans) throws DataWriterException
{
    while (beans.hasNext())
    {
        DynaBean bean = (DynaBean)beans.next();

        if (bean instanceof SqlDynaBean)
        {
            write((SqlDynaBean)bean);
        }
        else
        {
            _log.warn("Cannot write normal dyna beans (type: "+bean.getDynaClass().getName()+")");
        }
    }
}
 
Example 5
Source Project: gemfirexd-oss   Source File: PlatformImplBase.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Creates the SQL for updating an object of the given type. If a concrete bean is given,
 * then a concrete update statement is created, otherwise an update statement usable in a
 * prepared statement is build.
 * 
 * @param model       The database model
 * @param dynaClass   The type
 * @param primaryKeys The primary keys
 * @param properties  The properties to write
 * @param oldBean     Contains column values to identify the rows to update (i.e. for the WHERE clause)
 * @param newBean     Contains the new column values to write
 * @return The SQL required to update the instance
 */
protected String createUpdateSql(Database model, SqlDynaClass dynaClass, SqlDynaProperty[] primaryKeys, SqlDynaProperty[] properties, DynaBean oldBean, DynaBean newBean)
{
    Table   table           = model.findTable(dynaClass.getTableName());
    HashMap oldColumnValues = toColumnValues(primaryKeys, oldBean);
    HashMap newColumnValues = toColumnValues(properties, newBean);

    if (primaryKeys.length == 0)
    {
        _log.info("Cannot update instances of type " + dynaClass + " because it has no primary keys");
        return null;
    }
    else
    {
        return _builder.getUpdateSql(table, oldColumnValues, newColumnValues, newBean == null);
    }
}
 
Example 6
Source Project: gemfirexd-oss   Source File: DataToDatabaseSink.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Inserts the bean into the database or batch queue.
 * 
 * @param table The table
 * @param bean  The bean
 */
private void insertBeanIntoDatabase(Table table, DynaBean bean) throws DataSinkException
{
    if (_useBatchMode)
    {
        _batchQueue.add(bean);
        if (_batchQueue.size() >= _batchSize)
        {
            purgeBatchQueue();
        }
    }
    else
    {
        insertSingleBeanIntoDatabase(table, bean);
    }
}
 
Example 7
Source Project: gemfirexd-oss   Source File: TestAgainstLiveDatabaseBase.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Determines the value of the bean's property that has the given name. Depending on the
 * case-setting of the current builder, the case of teh name is considered or not. 
 * 
 * @param bean     The bean
 * @param propName The name of the property
 * @return The value
 */
protected Object getPropertyValue(DynaBean bean, String propName)
{
    if (getPlatform().isDelimitedIdentifierModeOn())
    {
        return bean.get(propName);
    }
    else
    {
        DynaProperty[] props = bean.getDynaClass().getDynaProperties();

        for (int idx = 0; idx < props.length; idx++)
        {
            if (propName.equalsIgnoreCase(props[idx].getName()))
            {
                return bean.get(props[idx].getName());
            }
        }
        throw new IllegalArgumentException("The bean has no property with the name "+propName);
    }
}
 
Example 8
Source Project: gemfirexd-oss   Source File: TestAgainstLiveDatabaseBase.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Compares the specified attribute value of the given bean with the expected object.
 * 
 * @param expected The expected object
 * @param bean     The bean
 * @param attrName The attribute name
 */
protected void assertEquals(Object expected, Object bean, String attrName)
{
    DynaBean dynaBean = (DynaBean)bean;
    Object   value    = dynaBean.get(attrName);

    if ((value instanceof byte[]) && !(expected instanceof byte[]) && (dynaBean instanceof SqlDynaBean))
    {
        SqlDynaClass dynaClass = (SqlDynaClass)((SqlDynaBean)dynaBean).getDynaClass();
        Column       column    = ((SqlDynaProperty)dynaClass.getDynaProperty(attrName)).getColumn();

        if (TypeMap.isBinaryType(column.getTypeCode()))
        {
            value = new BinaryObjectsHelper().deserialize((byte[])value);
        }
    }
    if (expected == null)
    {
        assertNull(value);
    }
    else
    {
        assertEquals(expected, value);
    }
}
 
Example 9
Source Project: gemfirexd-oss   Source File: TestAddColumn.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Tests the addition of a column with a default value.
 */
public void testAddColumnWithDefault()
{
    final String model1Xml = 
        "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
        "<database xmlns='" + DatabaseIO.DDLUTILS_NAMESPACE + "' name='roundtriptest'>\n"+
        "  <table name='roundtrip'>\n"+
        "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
        "  </table>\n"+
        "</database>";
    final String model2Xml = 
        "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
        "<database xmlns='" + DatabaseIO.DDLUTILS_NAMESPACE + "' name='roundtriptest'>\n"+
        "  <table name='roundtrip'>\n"+
        "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
        "    <column name='avalue' type='DOUBLE' default='2'/>\n"+
        "  </table>\n"+
        "</database>";

    createDatabase(model1Xml);

    insertRow("roundtrip", new Object[] { new Integer(1) });

    alterDatabase(model2Xml);

    assertEquals(getAdjustedModel(),
                 readModelFromDatabase("roundtriptest"));

    List   beans  = getRows("roundtrip");
    Object avalue = ((DynaBean)beans.get(0)).get("avalue");

    assertTrue((avalue == null) || new Double(2).equals(avalue));
}
 
Example 10
Source Project: gemfirexd-oss   Source File: TestDataReaderAndWriter.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Tests that attributes for which no column is defined, are ignored.
 */
public void testAttributeForUndefinedColumn() throws Exception
{
    Database model = readModel(
        "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
        "<database xmlns='" + DatabaseIO.DDLUTILS_NAMESPACE + "' name='test'>\n" +
        "  <table name='test'>\n"+
        "    <column name='id' type='INTEGER' primaryKey='true' required='true'/>\n"+
        "    <column name='value' type='VARCHAR' size='50' required='true'/>\n"+
        "  </table>\n"+
        "</database>");
    List beans = readBeans(
        model,
        "<data>\n"+
        "  <test id='1' value1='foo'/>\n"+
        "</data>");

    assertEquals(1, beans.size());

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

    assertEquals("test",
                 obj.getDynaClass().getName());
    assertEquals("1",
                 obj.get("id").toString());
    assertNull(obj.get("value"));
}
 
Example 11
Source Project: gemfirexd-oss   Source File: TestChangeColumn.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Tests removing the default value of a primary key column.
 */
public void testPKColumnRemoveDefault()
{
    final String model1Xml = 
        "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
        "<database xmlns='" + DatabaseIO.DDLUTILS_NAMESPACE + "' name='roundtriptest'>\n"+
        "  <table name='roundtrip'>\n"+
        "    <column name='pk' type='NUMERIC' size='12,2' primaryKey='true' required='true' default='2'/>\n"+
        "    <column name='avalue' type='INTEGER'/>\n"+
        "  </table>\n"+
        "</database>";
    final String model2Xml = 
        "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
        "<database xmlns='" + DatabaseIO.DDLUTILS_NAMESPACE + "' name='roundtriptest'>\n"+
        "  <table name='roundtrip'>\n"+
        "    <column name='pk' type='NUMERIC' size='12,2' primaryKey='true' required='true' default='2.20'/>\n"+
        "    <column name='avalue' type='INTEGER'/>\n"+
        "  </table>\n"+
        "</database>";

    createDatabase(model1Xml);

    insertRow("roundtrip", new Object[] { null, new Integer(1) });

    alterDatabase(model2Xml);

    assertEquals(getAdjustedModel(),
                 readModelFromDatabase("roundtriptest"));

    List     beans = getRows("roundtrip");
    DynaBean bean  = (DynaBean)beans.get(0);

    // Some DBs return the BigDecimal with the two digits scale, some don't
    assertTrue(bean.get("pk").equals(new BigDecimal("2")) || bean.get("pk").equals(new BigDecimal("2.00")));
    assertEquals(new Integer(1), bean, "avalue");
}
 
Example 12
Source Project: gemfirexd-oss   Source File: PlatformImplBase.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public String getInsertSql(Database model, DynaBean dynaBean)
{
    SqlDynaClass      dynaClass  = model.getDynaClassFor(dynaBean);
    SqlDynaProperty[] properties = dynaClass.getSqlDynaProperties();

    if (properties.length == 0)
    {
        _log.info("Cannot insert instances of type " + dynaClass + " because it has no properties");
        return null;
    }

    return createInsertSql(model, dynaClass, properties, dynaBean);
}
 
Example 13
Source Project: gemfirexd-oss   Source File: PlatformImplBase.java    License: Apache License 2.0 5 votes vote down vote up
/**
     * Returns all identity properties whose value were defined by the database and which
     * now need to be read back from the DB.
     * 
     * @param model     The database model
     * @param dynaClass The dyna class
     * @param bean      The bean
     * @return The columns
     */
    private Column[] getRelevantIdentityColumns(Database model, SqlDynaClass dynaClass, final DynaBean bean)
    {
        SqlDynaProperty[] properties = dynaClass.getSqlDynaProperties();

        Collection relevantProperties = CollectionUtils.select(Arrays.asList(properties), new Predicate() {
            public boolean evaluate(Object input) {
                SqlDynaProperty prop = (SqlDynaProperty)input;

                // we only want those identity columns that were really specified by the DB
                // if the platform allows specification of values for identity columns
                // in INSERT/UPDATE statements, then we need to filter the corresponding
                // columns out
                return prop.getColumn().isAutoIncrement() &&
// GemStone changes BEGIN
                       // allow using ALTER TABLE for identity columns
                       // after initial data loading
                       (!isAddIdentityUsingAlterTableOn() ||
                           !getPlatformInfo()
                               .isAddingIdentityUsingAlterTableSupported()) &&
                       (!isIdentityOverrideOn() ||
                           !getPlatformInfo().isIdentityOverrideAllowed() ||
                           (bean.get(prop.getName()) == null));
                       /* (original code)
                       (!isIdentityOverrideOn() || !getPlatformInfo().isIdentityOverrideAllowed() || (bean.get(prop.getName()) == null));
                       */
// GemStone changes END
            }
        });

        Column[] columns = new Column[relevantProperties.size()];
        int      idx     = 0;

        for (Iterator propIt = relevantProperties.iterator(); propIt.hasNext(); idx++)
        {
            columns[idx] = ((SqlDynaProperty)propIt.next()).getColumn();
        }
        return columns;
    }
 
Example 14
Source Project: gemfirexd-oss   Source File: TestChangeColumn.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Tests decreasing the size of a column.
 */
public void testColumnDecreaseSize()
{
    final String model1Xml = 
        "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
        "<database xmlns='" + DatabaseIO.DDLUTILS_NAMESPACE + "' name='roundtriptest'>\n"+
        "  <table name='roundtrip'>\n"+
        "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
        "    <column name='avalue' type='VARCHAR' size='32' required='true'/>\n"+
        "  </table>\n"+
        "</database>";
    final String model2Xml = 
        "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
        "<database xmlns='" + DatabaseIO.DDLUTILS_NAMESPACE + "' name='roundtriptest'>\n"+
        "  <table name='roundtrip'>\n"+
        "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
        "    <column name='avalue' type='VARCHAR' size='16' required='true'/>\n"+
        "  </table>\n"+
        "</database>";

    createDatabase(model1Xml);

    insertRow("roundtrip", new Object[] { new Integer(1), "12345678901234567890123456789012" });

    alterDatabase(model2Xml);

    assertEquals(getAdjustedModel(),
                 readModelFromDatabase("roundtriptest"));

    List     beans = getRows("roundtrip");
    DynaBean bean  = (DynaBean)beans.get(0);

    assertEquals(new Integer(1), bean, "pk");
    assertTrue("12345678901234567890123456789012".equals(bean.get("avalue")) || "1234567890123456".equals(bean.get("avalue")));
}
 
Example 15
Source Project: gemfirexd-oss   Source File: TestDynaSqlQueries.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Tests the insert method.
 */
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"));
}
 
Example 16
Source Project: gemfirexd-oss   Source File: PlatformImplBase.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void store(Database model, DynaBean dynaBean) throws DatabaseOperationException
{
    Connection connection = borrowConnection();

    try
    {
        store(connection, model, dynaBean);
    }
    finally
    {
        returnConnection(connection);
    }
}
 
Example 17
Source Project: gemfirexd-oss   Source File: PlatformImplBase.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public boolean exists(Database model, DynaBean dynaBean)
{
    Connection connection = borrowConnection();

    try
    {
        return exists(connection, model, dynaBean);
    }
    finally
    {
        returnConnection(connection);
    }
}
 
Example 18
Source Project: gemfirexd-oss   Source File: TestChangeColumn.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Tests removing the default value of a primary key column.
 */
public void testPKColumnRemoveDefault()
{
    final String model1Xml = 
        "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
        "<database xmlns='" + DatabaseIO.DDLUTILS_NAMESPACE + "' name='roundtriptest'>\n"+
        "  <table name='roundtrip'>\n"+
        "    <column name='pk' type='NUMERIC' size='12,2' primaryKey='true' required='true' default='2'/>\n"+
        "    <column name='avalue' type='INTEGER'/>\n"+
        "  </table>\n"+
        "</database>";
    final String model2Xml = 
        "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
        "<database xmlns='" + DatabaseIO.DDLUTILS_NAMESPACE + "' name='roundtriptest'>\n"+
        "  <table name='roundtrip'>\n"+
        "    <column name='pk' type='NUMERIC' size='12,2' primaryKey='true' required='true' default='2.20'/>\n"+
        "    <column name='avalue' type='INTEGER'/>\n"+
        "  </table>\n"+
        "</database>";

    createDatabase(model1Xml);

    insertRow("roundtrip", new Object[] { null, new Integer(1) });

    alterDatabase(model2Xml);

    assertEquals(getAdjustedModel(),
                 readModelFromDatabase("roundtriptest"));

    List     beans = getRows("roundtrip");
    DynaBean bean  = (DynaBean)beans.get(0);

    // Some DBs return the BigDecimal with the two digits scale, some don't
    assertTrue(bean.get("pk").equals(new BigDecimal("2")) || bean.get("pk").equals(new BigDecimal("2.00")));
    assertEquals(new Integer(1), bean, "avalue");
}
 
Example 19
Source Project: reflectutils   Source File: DynaBeanAdapter.java    License: Apache License 2.0 5 votes vote down vote up
public Class<?> getFieldType(Object obj, String name) {
    DynaClass dynaClass = ((DynaBean) obj).getDynaClass();
    DynaProperty dynaProperty = dynaClass.getDynaProperty(name);
    if (dynaProperty == null) {
        throw new FieldnameNotFoundException("DynaBean: Could not find this fieldName ("+name+") on the target object: " + obj, name, null);
    }
    return dynaProperty.getType();
}
 
Example 20
Source Project: gemfirexd-oss   Source File: PlatformImplBase.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void delete(Database model, DynaBean dynaBean) throws DatabaseOperationException
{
    Connection connection = borrowConnection();

    try
    {
        delete(connection, model, dynaBean);
    }
    finally
    {
        returnConnection(connection);
    }
}
 
Example 21
Source Project: gemfirexd-oss   Source File: TestDataReaderAndWriter.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Tests parsing when case sensitivity is turned on.
 */
public void testCaseSensitivityTurnedOn() throws Exception
{
    Database model = readModel(
        "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
        "<database xmlns='" + DatabaseIO.DDLUTILS_NAMESPACE + "' name='test'>\n" +
        "  <table name='Test'>\n"+
        "    <column name='Id' type='INTEGER' primaryKey='true' required='true'/>\n"+
        "    <column name='Value' type='VARCHAR' size='50' required='true'/>\n"+
        "  </table>\n"+
        "</database>");
    String testDataXml =
        "<data>\n"+
        "  <test Id='1' Value='foo'/>\n"+
        "  <Test Id='2' value='baz'/>\n"+
        "</data>";

    ArrayList  beans      = new ArrayList();
    DataReader dataReader = new DataReader();

    dataReader.setCaseSensitive(true);
    dataReader.setModel(model);
    dataReader.setSink(new TestDataSink(beans));
    dataReader.read(new StringReader(testDataXml));

    assertEquals(1, beans.size());

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

    assertEquals("Test",
                 obj.getDynaClass().getName());
    assertEquals("2",
                 obj.get("Id").toString());
    assertNull(obj.get("Value"));
}
 
Example 22
Source Project: gemfirexd-oss   Source File: TestChangeColumn.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Tests increasing the scale of a primary key column.
 */
public void testPKColumnIncreaseScale()
{
    final String model1Xml = 
        "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
        "<database xmlns='" + DatabaseIO.DDLUTILS_NAMESPACE + "' name='roundtriptest'>\n"+
        "  <table name='roundtrip'>\n"+
        "    <column name='pk' type='NUMERIC' size='10,3' primaryKey='true' required='true'/>\n"+
        "  </table>\n"+
        "</database>";
    final String model2Xml = 
        "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
        "<database xmlns='" + DatabaseIO.DDLUTILS_NAMESPACE + "' name='roundtriptest'>\n"+
        "  <table name='roundtrip'>\n"+
        "    <column name='pk' type='NUMERIC' size='10,5' primaryKey='true' required='true'/>\n"+
        "  </table>\n"+
        "</database>";

    createDatabase(model1Xml);

    insertRow("roundtrip", new Object[] { new BigDecimal("12345.123") });

    alterDatabase(model2Xml);

    assertEquals(getAdjustedModel(),
                 readModelFromDatabase("roundtriptest"));

    List     beans = getRows("roundtrip");
    DynaBean bean  = (DynaBean)beans.get(0);

    // Some DBs return the BigDecimal with the three digits scale, some don't
    assertTrue(bean.get("pk").equals(new BigDecimal("12345.123")) ||
               bean.get("pk").equals(new BigDecimal("12345.12300")));
}
 
Example 23
Source Project: gemfirexd-oss   Source File: TestDynaSqlQueries.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Tests the update method.
 */
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"));
}
 
Example 24
Source Project: gemfirexd-oss   Source File: DynaClassCache.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Returns the {@link SqlDynaClass} for the given bean.
 * 
 * @param dynaBean The bean
 * @return The dyna bean class
 */
public SqlDynaClass getDynaClass(DynaBean dynaBean) throws SqlDynaException
{
    DynaClass dynaClass = dynaBean.getDynaClass();

    if (dynaClass instanceof SqlDynaClass)
    {
        return (SqlDynaClass)dynaClass;
    }
    else
    {
        // TODO: we could autogenerate an SqlDynaClass here ?
        throw new SqlDynaException("The dyna bean is not an instance of a SqlDynaClass");
    }
}
 
Example 25
Source Project: gemfirexd-oss   Source File: PlatformImplBase.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public String getInsertSql(Database model, DynaBean dynaBean)
{
    SqlDynaClass      dynaClass  = model.getDynaClassFor(dynaBean);
    SqlDynaProperty[] properties = dynaClass.getSqlDynaProperties();

    if (properties.length == 0)
    {
        _log.info("Cannot insert instances of type " + dynaClass + " because it has no properties");
        return null;
    }

    return createInsertSql(model, dynaClass, properties, dynaBean);
}
 
Example 26
Source Project: gemfirexd-oss   Source File: TestAddColumn.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Tests the addition of a column with a default value.
 */
public void testAddColumnWithDefault()
{
    final String model1Xml = 
        "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
        "<database xmlns='" + DatabaseIO.DDLUTILS_NAMESPACE + "' name='roundtriptest'>\n"+
        "  <table name='roundtrip'>\n"+
        "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
        "  </table>\n"+
        "</database>";
    final String model2Xml = 
        "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
        "<database xmlns='" + DatabaseIO.DDLUTILS_NAMESPACE + "' name='roundtriptest'>\n"+
        "  <table name='roundtrip'>\n"+
        "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
        "    <column name='avalue' type='DOUBLE' default='2'/>\n"+
        "  </table>\n"+
        "</database>";

    createDatabase(model1Xml);

    insertRow("roundtrip", new Object[] { new Integer(1) });

    alterDatabase(model2Xml);

    assertEquals(getAdjustedModel(),
                 readModelFromDatabase("roundtriptest"));

    List   beans  = getRows("roundtrip");
    Object avalue = ((DynaBean)beans.get(0)).get("avalue");

    assertTrue((avalue == null) || new Double(2).equals(avalue));
}
 
Example 27
Source Project: gemfirexd-oss   Source File: PlatformImplBase.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public boolean exists(Database model, DynaBean dynaBean)
{
    Connection connection = borrowConnection();

    try
    {
        return exists(connection, model, dynaBean);
    }
    finally
    {
        returnConnection(connection);
    }
}
 
Example 28
Source Project: gemfirexd-oss   Source File: DataToDatabaseSink.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Updates the values of the columns constituting the indicated foreign key with the values
 * of the given identity.
 * 
 * @param bean     The bean whose columns shall be updated
 * @param fkName   The name of the foreign key
 * @param identity The target identity
 */
private void updateFKColumns(DynaBean bean, String fkName, Identity identity)
{
    Table      sourceTable = ((SqlDynaClass)bean.getDynaClass()).getTable();
    Table      targetTable = identity.getTable();
    ForeignKey fk          = null;

    for (int idx = 0; idx < sourceTable.getForeignKeyCount(); idx++)
    {
        ForeignKey curFk = sourceTable.getForeignKey(idx);

        if (curFk.getForeignTableName().equalsIgnoreCase(targetTable.getQualifiedName()))
        {
            if (fkName.equals(getFKName(sourceTable, curFk)))
            {
                fk = curFk;
                break;
            }
        }
    }
    if (fk != null)
    {
        for (int idx = 0; idx < fk.getReferenceCount(); idx++)
        {
            Reference curRef       = fk.getReference(idx);
            Column    sourceColumn = curRef.getLocalColumn();
            Column    targetColumn = curRef.getForeignColumn();

            bean.set(sourceColumn.getName(), identity.getColumnValue(targetColumn.getName()));
        }
    }
}
 
Example 29
Source Project: reflectutils   Source File: DynaBeanAdapter.java    License: Apache License 2.0 5 votes vote down vote up
public List<String> getPropertyNames(Object bean) {
    List<String> names = new ArrayList<String>();
    DynaProperty origDescriptors[] =
        ((DynaBean) bean).getDynaClass().getDynaProperties();
    for (DynaProperty dynaProperty : origDescriptors) {
        String name = dynaProperty.getName();
        names.add(name);
    }
    return names;
}
 
Example 30
Source Project: gemfirexd-oss   Source File: TestDynaSqlQueries.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Tests the store method.
 */
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"));
}