Programming using the BIRT Reporting APIs : Programming the structure of a report design : Accessing a data source and data set with the API : How to change the data source for a data set
 
How to change the data source for a data set
The code sample in Listing 12‑25 shows how to check for a particular data source and data set in a report design and changes the data source for the data set. The code finds the data source and data set by name. Alternatively, use the getDataSets( ) and getDataSources( ) methods. Then, use the technique for iterating over the contents of a slot handle. The variable, design, is a ReportDesignHandle object.
Listing 12‑25 Modifying a data set
// Find the data set by name.
DataSetHandle ds = design.findDataSet( "Customers" );
 
// Find the data source by name.
DataSourceHandle dso = design.findDataSource( "EuropeSales" );
 
// Check for the existence of the data set and data source.
if ( (dso == null) || ( ds == null ) )
{
System.err.println( "EuropeSales or Customers not found" );
return;
}
 
// Change the data source of the data set.
try {
ds.setDataSource( "EuropeSales" );
} catch ( SemanticException e1 )
{
e1.printStackTrace( );
}
Changing the data set binding of a report item
Call the report item’s setDataSet( ) method to set or change the data set used by a report item. If the application changes the data set used by a report item, ensure that the new data set supplies all the data bindings that the contents of the report item require. If necessary, change the references to data bindings in data elements, text elements, and scripting methods. If the data bindings in the old data set do not match the names or data types of the fields that the new data set provides, the application or a report developer must resolve the data bindings before generating a report from the modified report design.
Use the ReportItemHandle method, columnBindingsIterator( ), to iterate over the column bindings that the report item uses. The items in the list are of type ComputedColumnHandle. This class provides methods to access the name, expression, and data type of the column binding.
To access the data set column and expression that a data item uses, call the methods, getResultSetColumn( ) and getResultSetExpression( ). Then, compare the data type and name with the result-set columns that the data set returns.