Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Data Extract
Revision as of 23:56, 18 July 2006 by Unnamed Poltroon (Talk)
Data Extract
This example demonstrate using the RE API to run a report and extract the results from the rptdocument.
Add comments at the bottom of the example.
BIRT Report Engine API Return to the BIRT Report Engine API examples
Source
import java.util.ArrayList; import java.util.logging.Level; import org.eclipse.birt.core.framework.Platform; import org.eclipse.birt.data.engine.core.DataException; import org.eclipse.birt.report.engine.api.EngineConfig; import org.eclipse.birt.report.engine.api.EngineException; import org.eclipse.birt.report.engine.api.IDataExtractionTask; import org.eclipse.birt.report.engine.api.IDataIterator; import org.eclipse.birt.report.engine.api.IExtractionResults; import org.eclipse.birt.report.engine.api.IReportDocument; 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.IResultMetaData; import org.eclipse.birt.report.engine.api.IResultSetItem; import org.eclipse.birt.report.engine.api.IRunTask; public class DataExtract { static void executeReport() throws EngineException { IReportEngine engine=null; EngineConfig config = null; try{ config = new EngineConfig( ); config.setEngineHome( "C:/birt-runtime-2_1_0/birt-runtime-2_1_0/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(); } //open the report design IReportRunnable design = null; design = engine.openReportDesign("C:/test/2.1/toc/TOCTest.rptdesign"); //Create task to run the report - use the task to run the report, IRunTask task = engine.createRunTask(design); //Run the report and create the rptdocument task.run("c:/test/2.1/toc/TOCTest.rptdocument"); //Open the rptdocument IReportDocument rptdoc = engine.openReportDocument("c:/test/2.1/toc/TOCTest.rptdocument"); //Create the data extraction task IDataExtractionTask iDataExtract = engine.createDataExtractionTask(rptdoc); /** * returns the metadata corresponding to the data stored in the report * document. Could specify a component. */ ArrayList resultSetList = (ArrayList)iDataExtract.getResultSetList( ); //Get the first result set. Note this is a table elemenent IResultSetItem resultItem = (IResultSetItem)resultSetList.get( 0 ); //Set the name of the element you want to retrieve. //This will usually be ELEMENT_something if you do not name your elements. //If you name a table for example "MyTable" this will be the resultset name String dispName = resultItem.getResultSetName( ); iDataExtract.selectResultSet( dispName ); IExtractionResults iExtractResults = iDataExtract.extract(); IDataIterator iData = null; //Iterate the results try{ if ( iExtractResults != null ) { iData = iExtractResults.nextResultIterator( ); //Get metadata on retrieved results IResultMetaData irmd = iData.getResultMetaData(); if ( iData != null ){ int colCount = irmd.getColumnCount(); System.out.println("Cloumn Count =" + colCount ); for( int j=0; j< colCount; j++){ System.out.println("Cloumn Name =" + irmd.getColumnName(j) ); System.out.println("Cloumn Type =" + irmd.getColumnTypeName(j) ); } while ( iData.next( ) ) { //Just disply the first two columns Object objColumn1; Object objColumn2; try{ objColumn1 = iData.getValue(0); }catch(DataException e){ objColumn1 = new String(""); } try{ objColumn2 = iData.getValue(1); }catch(DataException e){ objColumn2 = new String(""); } System.out.println( objColumn1 + " , " + objColumn2 ); } iData.close(); } } }catch( Exception e){ e.printStackTrace(); } //close the task and showdown the engine and Platform //Note - If the program stays resident do not shutdown the Platform or the Engine iDataExtract.close(); engine.shutdown(); Platform.shutdown(); System.out.println("Finished"); } public static void main(String[] args) { try { executeReport( ); } catch ( Exception e ) { e.printStackTrace(); } } }