Jump to: navigation, search

Java - Execute Modified Report (BIRT)

Revision as of 23:23, 24 July 2012 by Curmudgeon99.gmail.com (Talk | contribs) (Cleaned up the annoying formatting.)

< To: Integration Examples (BIRT)

Execute Modifed Report

This example opens an existing report and adds a data source, data set and a table. The example also uses the Report Engine API (RE API) to execute the modified report. 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.

Add comments at the bottom of the example.

Moved from the old examples area BIRT Design Engine API


ExecuteModifedReport .java

import java.util.ArrayList;
import java.util.HashMap;
import java.util.logging.Level; 

import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.report.engine.api.EngineConfig;
import org.eclipse.birt.report.engine.api.EngineConstants;
import org.eclipse.birt.report.engine.api.EngineException;
import org.eclipse.birt.report.engine.api.HTMLActionHandler;
import org.eclipse.birt.report.engine.api.HTMLEmitterConfig;
import org.eclipse.birt.report.engine.api.HTMLRenderContext;
import org.eclipse.birt.report.engine.api.HTMLRenderOption;
import org.eclipse.birt.report.engine.api.HTMLServerImageHandler;
import org.eclipse.birt.report.engine.api.IReportEngine;
import org.eclipse.birt.report.engine.api.IReportEngineFactory;
import org.eclipse.birt.report.engine.api.IReportRunnable;
import org.eclipse.birt.report.engine.api.IRunAndRenderTask;
import org.eclipse.birt.report.model.api.CellHandle;
import org.eclipse.birt.report.model.api.DataItemHandle;
import org.eclipse.birt.report.model.api.ElementFactory;
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.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;

public class ExecuteModifedReport {

   public void runReport() throws EngineException
       IReportEngine engine=null;
       EngineConfig config = null;
           //Configure the Engine and start the Platform
           config = new EngineConfig( );
           config.setEngineHome( "C:/birt-runtime-2_1_1/birt-runtime-2_1_1/ReportEngine" );
           config.setLogConfig(null, Level.FINE);
           Platform.startup( config );
           IReportEngineFactory factory = (IReportEngineFactory) Platform.createFactoryObject( IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY );
           engine = factory.createReportEngine( config );
           engine.changeLogLevel( Level.WARNING );
       catch( Exception ex )
       //Configure the emitter to handle actions and images
       HTMLEmitterConfig emitterConfig = new HTMLEmitterConfig( );
       emitterConfig.setActionHandler( new HTMLActionHandler( ) );
       HTMLServerImageHandler imageHandler = new HTMLServerImageHandler( );
       emitterConfig.setImageHandler( imageHandler );
       config.getEmitterConfigs( ).put( "html", emitterConfig ); //$NON-NLS-1$
       IReportRunnable design = null;

       //Open the report design 
       design = engine.openReportDesign("C:/tmp/testde.rptdesign"); 
       ReportDesignHandle report = (ReportDesignHandle) design.getDesignHandle( );
       buildReport( report ); 
       //Create task to run and render the report,
       IRunAndRenderTask task = engine.createRunAndRenderTask(design); 
       //Set Render context to handle url and image locataions
       HTMLRenderContext renderContext = new HTMLRenderContext();
       //Set the Base URL for all actions
       //Tell the Engine to prepend all images with this URL - Note this requires using the HTMLServerImageHandler
       //Tell the Engine where to write the images to
       //Tell the Engine what image formats are supported.  Note you must have SVG in the string 
       //to render charts in SVG.
       HashMap contextMap = new HashMap();
       contextMap.put( EngineConstants.APPCONTEXT_HTML_RENDER_CONTEXT, renderContext );
       task.setAppContext( contextMap );
       //Set parameters for the report
       //Alternatively set each seperately
       //task.setParameterValue("Top Count", new Integer(12));
       HTMLRenderOption options = new HTMLRenderOption();
       //Set ouptut location
       //Set output format
       //run the report and destroy the engine
       //Note - If the program stays resident do not shutdown the Platform or the Engine

   public void buildReport(ReportDesignHandle designHandle)
           ElementFactory designFactory = designHandle.getElementFactory( );
           buildDataSource(designFactory, designHandle);
           ArrayList cols = new ArrayList();
           buildDataSet(cols, "From Offices", designFactory, designHandle);

           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.setExpression("dataSetRow[\"" + (String)cols.get(i) + "\"]");

           // 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) );	
               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 );
       catch(Exception e)

   void buildDataSource( ElementFactory designFactory, ReportDesignHandle designHandle ) 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, ElementFactory designFactory, ReportDesignHandle designHandle ) 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 );

      * @param args
   public static void main(String[] args) 
           ExecuteModifedReport ex = new ExecuteModifedReport( );
       catch ( Exception e )


Please enter comments below by selecting the edit icon to the right. You will need a Bugzilla account to add comments.

This example adds data source, data set, and elements. To modify existing elements use the find methods of the ReportDesignHandle. For example to modify an existing Data Source use code similar to:

ReportDesignHandle report = (ReportDesignHandle) design.getDesignHandle();
OdaDataSourceHandle dsHandle = (OdaDataSourceHandle) report.findDataSource( "Data Source" );

dsHandle.setProperty( "odaDriverClass","mydriverclass" );
dsHandle.setProperty( "odaURL", "mydriverurl" );
dsHandle.setProperty( "odaUser", "user" );
dsHandle.setProperty( "odaPassword", "pass" );

Please note that the Class name in the above example code is misspelled and should be ExecuteModifiedReport instead of ExecuteModifedReport.