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

Java - Execute Modified Report (BIRT)

< 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

Source

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;
        	
       try
       {
           //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 )
       {
           ex.printStackTrace();
       }
        
       //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
       renderContext.setBaseURL("baseurl");
       //Tell the Engine to prepend all images with this URL - Note this requires using the HTMLServerImageHandler
       renderContext.setBaseImageURL("urltoimages");
       //Tell the Engine where to write the images to
       renderContext.setImageDirectory("C:/xampplite/htdocs/myimages");
       //Tell the Engine what image formats are supported.  Note you must have SVG in the string 
       //to render charts in SVG.
       renderContext.setSupportedImageFormats("JPG;PNG;BMP;SVG");
       HashMap contextMap = new HashMap();
       contextMap.put( EngineConstants.APPCONTEXT_HTML_RENDER_CONTEXT, renderContext );
       task.setAppContext( contextMap );
       //Set parameters for the report
       //task.setParameterValues(parameters);
       //Alternatively set each seperately
       //task.setParameterValue("Top Count", new Integer(12));
       //task.validateParameters();
        
       HTMLRenderOption options = new HTMLRenderOption();
        
       //Set ouptut location
       options.setOutputFileName("C:/tmp/output.html");
        
       //Set output format
       options.setOutputFormat("html");
       task.setRenderOption(options);
        
       //run the report and destroy the engine
       //Note - If the program stays resident do not shutdown the Platform or the Engine
       task.run();
       task.close();
       engine.shutdown();
       Platform.shutdown();
       System.out.println("Finished"); 
   }	
  
   public void buildReport(ReportDesignHandle designHandle)
   {
       try
       {
           ElementFactory designFactory = designHandle.getElementFactory( );
           buildDataSource(designFactory, designHandle);
           
           ArrayList cols = new ArrayList();
           cols.add("OFFICECODE");
           cols.add("CITY");
           cols.add("COUNTRY");
  
           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.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 );
       }
       catch(Exception e)
       {
           e.printStackTrace();
       }
   }
  
   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) 
   {
       try
       {
           ExecuteModifedReport ex = new ExecuteModifedReport( );
           ex.runReport();
       }
       catch ( Exception e )
       {
           e.printStackTrace();
       }
   }
}

Comments

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.

Copyright © Eclipse Foundation, Inc. All Rights Reserved.