Bind existing data to dataset [message #653027] |
Mon, 07 February 2011 22:56  |
Eclipse User |
|
|
|
Hi,
I'm currently working on extracting .rptdesign file using java. My problem is some of the data which isn't binded to the dataset of its table cannot display its value expression. Only data which is binded to its table can display the value expression. I tried to set the data item's dataset but it seems like it reset all the info of the data.
This problem has driven me crazy because this is a rare case in google. Can anyone please help me on how to bind the existing data to its table's dataset, in order to display the value expression? Thanks!
Cass
|
|
|
Re: Bind existing data to dataset [message #653224 is a reply to message #653027] |
Tue, 08 February 2011 16:25   |
Eclipse User |
|
|
|
If you get the TableHandle call the the getTableBindings and add
computed columns to it using StructureFactory.createComputedColumn();.
Look at the attached example.
Jason
package DEAPI;
import java.io.IOException;
import java.util.ArrayList;
import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.report.model.api.CellHandle;
import org.eclipse.birt.report.model.api.DataItemHandle;
import org.eclipse.birt.report.model.api.DesignConfig;
import org.eclipse.birt.report.model.api.ElementFactory;
import org.eclipse.birt.report.model.api.IDesignEngine;
import org.eclipse.birt.report.model.api.IDesignEngineFactory;
import org.eclipse.birt.report.model.api.LabelHandle;
import org.eclipse.birt.report.model.api.OdaDataSetHandle;
import org.eclipse.birt.report.model.api.OdaDataSourceHandle;
import org.eclipse.birt.report.model.api.PropertyHandle;
import org.eclipse.birt.report.model.api.ReportDesignHandle;
import org.eclipse.birt.report.model.api.RowHandle;
import org.eclipse.birt.report.model.api.SessionHandle;
import org.eclipse.birt.report.model.api.StructureFactory;
import org.eclipse.birt.report.model.api.TableHandle;
import org.eclipse.birt.report.model.api.activity.SemanticException ;
import org.eclipse.birt.report.model.api.elements.structures.Comput edColumn;
import com.ibm.icu.util.ULocale;
/**
* Simple BIRT Design Engine API (DEAPI) demo.
*/
public class DECreateDynamicTable
{
ReportDesignHandle designHandle = null;
ElementFactory designFactory = null;
StructureFactory structFactory = null;
public static void main( String[] args )
{
try
{
DECreateDynamicTable de = new DECreateDynamicTable();
ArrayList al = new ArrayList();
al.add("OFFICECODE");
al.add("CITY");
al.add("COUNTRY");
de.buildReport(al, "From Offices" );
}
catch ( IOException e )
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch ( SemanticException e )
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
void buildDataSource( ) throws SemanticException
{
OdaDataSourceHandle dsHandle = designFactory.newOdaDataSource(
"Data Source", "org.eclipse.birt.report.data.oda.jdbc" );
dsHandle.setProperty( "odaDriverClass",
"org.eclipse.birt.report.data.oda.sampledb.Driver" );
dsHandle.setProperty( "odaURL", "jdbc:classicmodels:sampledb" );
dsHandle.setProperty( "odaUser", "ClassicModels" );
dsHandle.setProperty( "odaPassword", "" );
designHandle.getDataSources( ).add( dsHandle );
}
void buildDataSet(ArrayList cols, String fromClause ) throws
SemanticException
{
OdaDataSetHandle dsHandle = designFactory.newOdaDataSet( "ds",
"org.eclipse.birt.report.data.oda.jdbc.JdbcSelectDataSet" );
dsHandle.setDataSource( "Data Source" );
String qry = "Select ";
for( int i=0; i < cols.size(); i++){
qry += " " + cols.get(i);
if( i != (cols.size() -1) ){
qry += ",";
}
}
qry += " " + fromClause;
dsHandle.setQueryText( qry );
designHandle.getDataSets( ).add( dsHandle );
}
void buildReport(ArrayList cols, String fromClause ) throws
IOException, SemanticException
{
DesignConfig config = new DesignConfig( );
config.setBIRTHome("C:/birt/birt-runtime-2_5_1/birt-runtime-2_5_1/ReportEngine ");
IDesignEngine engine = null;
try{
Platform.startup( config );
IDesignEngineFactory factory = (IDesignEngineFactory) Platform
.createFactoryObject(
IDesignEngineFactory.EXTENSION_DESIGN_ENGINE_FACTORY );
engine = factory.createDesignEngine( config );
}catch( Exception ex){
ex.printStackTrace();
}
SessionHandle session = engine.newSessionHandle( ULocale.ENGLISH ) ;
try{
//open a design or a template
designHandle = session.createDesign();
designFactory = designHandle.getElementFactory( );
buildDataSource();
buildDataSet(cols, fromClause);
TableHandle table = designFactory.newTableItem( "table", cols.size() );
table.setWidth( "100%" );
table.setDataSet( designHandle.findDataSet( "ds" ) );
PropertyHandle computedSet = table.getColumnBindings( );
ComputedColumn cs1 = null;
for( int i=0; i < cols.size(); i++){
cs1 = StructureFactory.createComputedColumn();
cs1.setName((String)cols.get(i));
cs1.setExpression("dataSetRow[\"" + (String)cols.get(i) + "\"]");
computedSet.addItem(cs1);
}
// table header
RowHandle tableheader = (RowHandle) table.getHeader( ).get( 0 );
for( int i=0; i < cols.size(); i++){
LabelHandle label1 = designFactory.newLabel( (String)cols.get(i) );
label1.setText((String)cols.get(i));
CellHandle cell = (CellHandle) tableheader.getCells( ).get( i );
cell.getContent( ).add( label1 );
}
// table detail
RowHandle tabledetail = (RowHandle) table.getDetail( ).get( 0 );
for( int i=0; i < cols.size(); i++){
CellHandle cell = (CellHandle) tabledetail.getCells( ).get( i );
DataItemHandle data = designFactory.newDataItem(
"data_"+(String)cols.get(i) );
data.setResultSetColumn( (String)cols.get(i));
cell.getContent( ).add( data );
}
designHandle.getBody( ).add( table );
// Save the design and close it.
designHandle.saveAs( "output/desample/dynamic.rptdesign" ); //$NON-NLS-1$
designHandle.close( );
Platform.shutdown();
System.out.println("Finished");
}catch (Exception e){
e.printStackTrace();
}
}
}
On 2/7/2011 10:56 PM, cassandra wrote:
> Hi,
>
> I'm currently working on extracting .rptdesign file using java. My
> problem is some of the data which isn't binded to the dataset of its
> table cannot display its value expression. Only data which is binded to
> its table can display the value expression. I tried to set the data
> item's dataset but it seems like it reset all the info of the data.
>
> This problem has driven me crazy because this is a rare case in google.
> Can anyone please help me on how to bind the existing data to its
> table's dataset, in order to display the value expression? Thanks!
>
> Cass
|
|
|
|
|
Re: Bind existing data to dataset [message #653509 is a reply to message #653439] |
Wed, 09 February 2011 21:25  |
Eclipse User |
|
|
|
Jason,
The reason i want to bind the data to a dataset because the value expression can only be displayed if the data is binded to a dataset. Otherwise, the value expression will not be displayed in the output of my program.
Let says, a table is created and binded to A dataset. There are a few data in a table but some of the data is binded to B dataset but rest of the data are not binded to any dataset.
So, my program will read the .rpt file by running through all the content. When it detects that the data isn't binded to dataset, it'll bind them to A dataset, which is same as the table, in order to display the value expression.
Hope you get what i'm saying.
Cass
|
|
|
Powered by
FUDForum. Page generated in 0.03294 seconds