Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "Java - Build Dynamic Table (BIRT)"

m (Clean up annoying bad formatting.)
 
(One intermediate revision by one other user not shown)
Line 83: Line 83:
 
   */
 
   */
 
   
 
   
public class DECreateDynamicTable
+
public class DECreateDynamicTable
{
+
{
ReportDesignHandle designHandle = null;
+
    ReportDesignHandle designHandle = null;
ElementFactory designFactory = null;
+
    ElementFactory designFactory = null;
StructureFactory structFactory = null;
+
    StructureFactory structFactory = null;
 
    
 
    
public static void main( String[] args )
+
    public static void main( String[] args )
{
+
    {
try
+
        try
{
+
        {
DECreateDynamicTable de = new DECreateDynamicTable();
+
            DECreateDynamicTable de = new DECreateDynamicTable();
ArrayList al = new ArrayList();
+
            List al = new ArrayList();
al.add("OFFICECODE");
+
            al.add("OFFICECODE");
al.add("CITY");
+
            al.add("CITY");
al.add("COUNTRY");
+
            al.add("COUNTRY");
+
 
de.buildReport(al, "From Offices" );
+
            de.buildReport(al, "From Offices" );
}
+
        }
catch ( IOException e )
+
        catch( IOException e )
{
+
        {
// TODO Auto-generated catch block
+
            e.printStackTrace();
e.printStackTrace();
+
        }
}
+
        catch( SemanticException e )
catch ( SemanticException e )
+
        {
{
+
            e.printStackTrace();
// 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 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
+
{
+
+
+
//Configure the Engine and start the Platform
+
DesignConfig config = new DesignConfig( );
+
+
config.setProperty("BIRT_HOME", "C:/birt-runtime-2_1_1/birt-runtime-2_1_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 ) ;
+
 
   
 
   
 +
    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
 +
    {
 +
        //Configure the Engine and start the Platform
 +
        DesignConfig config = new DesignConfig( );
 
   
 
   
try{
+
        config.setProperty("BIRT_HOME", "C:/birt-runtime-2_1_1/birt-runtime-2_1_1/ReportEngine");
//open a design or a template
+
        IDesignEngine engine = null;
designHandle = session.openDesign("c:/tmp/testdeapi.rptdesign");
+
 
 +
        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.openDesign("c:/tmp/testdeapi.rptdesign");
 
   
 
   
designFactory = designHandle.getElementFactory( );
+
            designFactory = designHandle.getElementFactory( );
 
    
 
    
buildDataSource();
+
            buildDataSource();
  buildDataSet(cols, fromClause);
+
            buildDataSet(cols, fromClause);
 
    
 
    
TableHandle table = designFactory.newTableItem( "table", cols.size() );
+
            TableHandle table = designFactory.newTableItem( "table", cols.size() );
table.setWidth( "100%" );
+
            table.setWidth( "100%" );
  table.setDataSet( designHandle.findDataSet( "ds" ) );
+
            table.setDataSet( designHandle.findDataSet( "ds" ) );
 
+
 
+
            PropertyHandle computedSet = table.getColumnBindings( );  
    PropertyHandle computedSet = table.getColumnBindings( );  
+
            ComputedColumn  cs1 = null;
ComputedColumn  cs1 = null;
+
 
+
            for( int i=0; i < cols.size(); i++)
for( int i=0; i < cols.size(); i++){
+
            {
cs1 = StructureFactory.createComputedColumn();
+
                cs1 = StructureFactory.createComputedColumn();
  cs1.setName((String)cols.get(i));
+
                cs1.setName((String)cols.get(i));
cs1.setExpression("dataSetRow[\"" + (String)cols.get(i) + "\"]");
+
                cs1.setExpression("dataSetRow[\"" + (String)cols.get(i) + "\"]");
computedSet.addItem(cs1);
+
                computedSet.addItem(cs1);
}
+
            }
+
 
+
            // table header
// table header
+
            RowHandle tableheader = (RowHandle) table.getHeader( ).get( 0 );
RowHandle tableheader = (RowHandle) table.getHeader( ).get( 0 );
+
 
+
            for( int i=0; i < cols.size(); i++ )
 
+
            {
  for( int i=0; i < cols.size(); i++){
+
                LabelHandle label1 = designFactory.newLabel( (String)cols.get(i) );
  LabelHandle label1 = designFactory.newLabel( (String)cols.get(i) );
+
                label1.setText((String)cols.get(i));
  label1.setText((String)cols.get(i));
+
                CellHandle cell = (CellHandle) tableheader.getCells( ).get( i );
  CellHandle cell = (CellHandle) tableheader.getCells( ).get( i );
+
                cell.getContent( ).add( label1 );
  cell.getContent( ).add( label1 );
+
            }
}
+
 
  
 
  
  // table detail
+
            // table detail
  RowHandle tabledetail = (RowHandle) table.getDetail( ).get( 0 );
+
            RowHandle tabledetail = (RowHandle) table.getDetail( ).get( 0 );
  for( int i=0; i < cols.size(); i++){
+
 
CellHandle cell = (CellHandle) tabledetail.getCells( ).get( i );
+
            for( int i=0; i < cols.size(); i++ )
  DataItemHandle data = designFactory.newDataItem( "data_"+(String)cols.get(i) );
+
            {
  data.setResultSetColumn( (String)cols.get(i));
+
                CellHandle cell = (CellHandle) tabledetail.getCells( ).get( i );
    cell.getContent( ).add( data );
+
                DataItemHandle data = designFactory.newDataItem( "data_"+(String)cols.get(i) );
  }
+
                data.setResultSetColumn( (String)cols.get(i));
 +
                cell.getContent( ).add( data );
 +
            }
 
    
 
    
  designHandle.getBody( ).add( table );
+
            designHandle.getBody( ).add( table );
 
    
 
    
// Save the design and close it.  
+
            // Save the design and close it.  
+
            designHandle.saveAs( "c:/temp/sample.rptdesign" ); //$NON-NLS-1$
designHandle.saveAs( "c:/temp/sample.rptdesign" ); //$NON-NLS-1$
+
            designHandle.close( );
designHandle.close( );
+
            System.out.println("Finished");
  System.out.println("Finished");
+
        }
}catch (Exception e){
+
        catch (Exception e)
e.printStackTrace();
+
        {
}
+
            e.printStackTrace();
+
        }
}
+
    }
 
   }
 
   }
 
----
 
----
  
 
== Comments ==
 
== Comments ==
Please enter comments below by selecting the edit icon to the right.
+
Hello,
You will need a Bugzilla account to add comments.
+
 
 +
thank you for this great example.
 +
 
 +
I would like to utilize it with other oda-drivers.
 +
 
 +
At the moment i have problems, because I got a java.lang.NullPointerException.
 +
In that part it occured:
 +
OdaDataSourceHandle dsHandle = designFactory.newOdaDataSource(
 +
"Data Source", "org.eclipse.birt.report.data.oda.jdbc" );
 +
dsHandle.setProperty( "odaDriverClass",
 +
"org.eclipse.birt.report.data.oda.sampledb.Driver" );
 +
 
 +
I think, the oda-driver have to deployed in a special way.
 +
 
 +
Please could you give us a hint, how to use it with further oda extensions. Like that one:
 +
http://anthonysdevshare.blogspot.com/2008/12/extending-birt-google-spreadsheet-oda.html
 +
 
 +
There are a lot of people, who would like to know that,too:
 +
http://www.birt-exchange.org/forum/deploying-integrating-birt-report-engine-applications/16382-integrate-custom-oda-driver-java-program.html
 +
 
 +
Please help us :)
 +
 
 +
Bye
 +
Martin
  
 
----
 
----

Latest revision as of 12:42, 25 July 2012

< To: Integration Examples (BIRT)

Java - Build a Dynamic Table

This example opens an existing report and adds a data source, data set and a table. The table columns are dynamically created based on an ArrayList that is passed in. The columns must exist in the query that is passed in.

Imported from old example area.

Modifying the Source

Prior to compiling the source, modify these three lines:

  • config.setProperty("BIRT_HOME", "C:/birt-runtime-2_1_1/birt-runtime-2_1_1/ReportEngine");
  • designHandle = session.openDesign("c:/tmp/testdeapi.rptdesign");
  • designHandle.saveAs( "c:/temp/sample.rptdesign" ); //$NON-NLS-1$

Edit the first line to point to the actual location where you installed the BIRT runtime. The second line must point to an existing report. The last line is the location where the report will be written to. The directory must exist prior to saving the report.

Compiling the Example

When running this example from within Eclipse, the Java Build Path for the project must contain all of the libraries in the BIRT_HOME/lib directory as well as the JRE System Library.

Here is a complete list of these libraries:

  • chartengineapi.jar
  • com.ibm.icu_3.4.4.1.jar
  • commons-cli-1.0.jar
  • commons-codec-1.3.jar
  • coreapi.jar
  • dataadapterapi.jar
  • dteapi.jar
  • engineapi.jar
  • flute.jar
  • js.jar
  • modelapi.jar
  • org.eclipse.emf.common_2.2.0.v200606051102.jar
  • org.eclipse.emf.ecore.xmi_2.2.0.v200606051102.jar
  • org.eclipse.emf.ecore_2.2.0.v200606051102.jar
  • sac.jar
  • scriptapi.jar

The example should now run successfully.

Add comments at the bottom of the example.

BIRT Design Engine API Return to the BIRT Design Engine API examples

Source

DECreateDynamicTable.java

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.ComputedColumn;

import com.ibm.icu.util.ULocale;

/**
 * Dynamic Table 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();
           List al = new ArrayList();
           al.add("OFFICECODE");
           al.add("CITY");
           al.add("COUNTRY");
           de.buildReport(al, "From Offices" );
       }
       catch( IOException e )
       {
           e.printStackTrace();
       }
       catch( SemanticException e )
       {
           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
   {
       //Configure the Engine and start the Platform
       DesignConfig config = new DesignConfig( );

       config.setProperty("BIRT_HOME", "C:/birt-runtime-2_1_1/birt-runtime-2_1_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.openDesign("c:/tmp/testdeapi.rptdesign");

           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( "c:/temp/sample.rptdesign" ); //$NON-NLS-1$
           designHandle.close( );
           System.out.println("Finished");
       }
       catch (Exception e)
       {
           e.printStackTrace();
       }		
    }
 }

Comments

Hello,

thank you for this great example.

I would like to utilize it with other oda-drivers.

At the moment i have problems, because I got a java.lang.NullPointerException. In that part it occured: OdaDataSourceHandle dsHandle = designFactory.newOdaDataSource( "Data Source", "org.eclipse.birt.report.data.oda.jdbc" ); dsHandle.setProperty( "odaDriverClass", "org.eclipse.birt.report.data.oda.sampledb.Driver" );

I think, the oda-driver have to deployed in a special way.

Please could you give us a hint, how to use it with further oda extensions. Like that one: http://anthonysdevshare.blogspot.com/2008/12/extending-birt-google-spreadsheet-oda.html

There are a lot of people, who would like to know that,too: http://www.birt-exchange.org/forum/deploying-integrating-birt-report-engine-applications/16382-integrate-custom-oda-driver-java-program.html

Please help us :)

Bye Martin


Copyright © Eclipse Foundation, Inc. All Rights Reserved.