Difference between revisions of "Simple Execute (BIRT) 2.1"

From Eclipsepedia

Jump to: navigation, search
m (Improved Java coding)
Line 4: Line 4:
  
  
This example demonstrates using the Report Engine API(RE API) to run and render a report
+
This example demonstrates using the Report Engine API (RE API) to run and render a report
 
design to HTML.   
 
design to HTML.   
  
 
Add comments at the bottom of the example.
 
Add comments at the bottom of the example.
  
[[BIRT Report Engine API]] Return to the BIRT Report Engine API examples
+
[[Integration Examples (BIRT)|BIRT Report Engine API]]: Return to the BIRT Report Engine API examples
 +
 
 
== BIRT Version ==  
 
== BIRT Version ==  
 
BIRT 2.1.1
 
BIRT 2.1.1
Line 15: Line 16:
 
== Source ==
 
== Source ==
 
===ExecuteReport.java===
 
===ExecuteReport.java===
 
+
<source lang=Java>
 
+
import java.util.HashMap;
 
+
import java.util.logging.Level;
import java.util.HashMap;
+
import java.util.logging.Level;
+
 
   
 
   
import org.eclipse.birt.core.framework.Platform;
+
import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.report.engine.api.EngineConfig;
+
import org.eclipse.birt.report.engine.api.EngineConfig;
import org.eclipse.birt.report.engine.api.EngineConstants;
+
import org.eclipse.birt.report.engine.api.EngineConstants;
import org.eclipse.birt.report.engine.api.EngineException;
+
import org.eclipse.birt.report.engine.api.EngineException;
import org.eclipse.birt.report.engine.api.HTMLActionHandler;
+
import org.eclipse.birt.report.engine.api.HTMLActionHandler;
import org.eclipse.birt.report.engine.api.HTMLEmitterConfig;
+
import org.eclipse.birt.report.engine.api.HTMLEmitterConfig;
import org.eclipse.birt.report.engine.api.HTMLRenderContext;
+
import org.eclipse.birt.report.engine.api.HTMLRenderContext;
import org.eclipse.birt.report.engine.api.HTMLRenderOption;
+
import org.eclipse.birt.report.engine.api.HTMLRenderOption;
import org.eclipse.birt.report.engine.api.HTMLServerImageHandler;
+
import org.eclipse.birt.report.engine.api.HTMLServerImageHandler;
import org.eclipse.birt.report.engine.api.IReportEngine;
+
import org.eclipse.birt.report.engine.api.IReportEngine;
import org.eclipse.birt.report.engine.api.IReportEngineFactory;
+
import org.eclipse.birt.report.engine.api.IReportEngineFactory;
import org.eclipse.birt.report.engine.api.IReportRunnable;
+
import org.eclipse.birt.report.engine.api.IReportRunnable;
import org.eclipse.birt.report.engine.api.IRunAndRenderTask;
+
import org.eclipse.birt.report.engine.api.IRunAndRenderTask;
public class ExecuteReport {
+
 
   
 
   
static void executeReport() throws EngineException
+
public class ExecuteReport {
{
+
    static void executeReport() throws EngineException {
  HashMap<String, Integer> parameters = new HashMap<String, Integer>();
+
        final HashMap<String, Integer> PARAMETERS = new HashMap<String, Integer>();
+
       
  String name = "Top Count";
+
        final String NAME = "Top Count";
         Integer pvalue = new Integer(4);
+
         final Integer VALUE = new Integer(4);
  parameters.put(name, pvalue);
+
        PARAMETERS.put(NAME, VALUE);
 
+
       
  IReportEngine engine=null;
+
        IReportEngine engine = null;
  EngineConfig config = null;
+
        EngineConfig config = null;
  try{
+
       
 
+
        try {
  //Configure the Engine and start the Platform
+
       
  config = new EngineConfig( );
+
          // Configure the Engine and start the Platform
  config.setEngineHome( "C:/birt-runtime-2_1_1/birt-runtime-2_1_1/ReportEngine" );
+
          config = new EngineConfig();
  //set log config using ( null, Level ) if you do not want a log file
+
          config.setEngineHome("C:/birt-runtime-2_1_1/birt-runtime-2_1_1/ReportEngine");
  config.setLogConfig("c:/birt/logs", Level.FINE);
+
          // set log config using (null, Level) if you do not want a log file
 
+
          config.setLogConfig("C:/birt/logs", Level.FINE);
  Platform.startup( config );
+
         
  IReportEngineFactory factory = (IReportEngineFactory) Platform
+
          Platform.startup(config);
    .createFactoryObject( IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY );
+
          IReportEngineFactory factory = (IReportEngineFactory) Platform.
  engine = factory.createReportEngine( config );
+
                createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);
  engine.changeLogLevel( Level.WARNING );
+
          engine = factory.createReportEngine(config);
 
+
          engine.changeLogLevel(Level.WARNING);
  }catch( Exception ex){
+
       
  ex.printStackTrace();
+
        } catch(Exception ex) {
  }
+
          ex.printStackTrace();
 
+
        }
  //Configure the emitter to handle actions and images
+
       
  HTMLEmitterConfig emitterConfig = new HTMLEmitterConfig( );
+
        // Configure the emitter to handle actions and images
  emitterConfig.setActionHandler( new HTMLActionHandler( ) );
+
        final HTMLEmitterConfig EMITTER_CONFIGURATION = new HTMLEmitterConfig();
  HTMLServerImageHandler imageHandler = new HTMLServerImageHandler( );
+
        EMITTER_CONFIGURATION.setActionHandler(new HTMLActionHandler());
  emitterConfig.setImageHandler( imageHandler );
+
        HTMLServerImageHandler imageHandler = new HTMLServerImageHandler();
  config.getEmitterConfigs( ).put( "html", emitterConfig ); //$NON-NLS-1$
+
        EMITTER_CONFIGURATION.setImageHandler(imageHandler);
 
+
        config.getEmitterConfigs().put("html", EMITTER_CONFIGURATION); // $NON-NLS-1$
  IReportRunnable design = null;
+
       
 
+
        // Open the report design
  //Open the report design
+
        final IReportRunnable DESIGN =
  design = engine.openReportDesign("C:/test/2.1/executereport/test.rptdesign");  
+
              engine.openReportDesign("C:/test/2.1/executereport/test.rptdesign");  
 
+
       
  //Create task to run and render the report,
+
        // Create task to run and render the report:
  IRunAndRenderTask task = engine.createRunAndRenderTask(design);  
+
        final IRunAndRenderTask TASK = engine.createRunAndRenderTask(DESIGN);  
 
+
       
 
+
        // Set Render context to handle URL and image locations
  //Set Render context to handle url and image locataions
+
        HTMLRenderContext renderContext = new HTMLRenderContext();
  HTMLRenderContext renderContext = new HTMLRenderContext();
+
        // Set the Base URL for all actions
  //Set the Base URL for all actions
+
        renderContext.setBaseURL(<nowiki>"http:// localhost/"</nowiki>);
  renderContext.setBaseURL(<nowiki>"http://localhost/"</nowiki>);
+
        // Tell the Engine to prepend all images with this URL - Note this requires using the HTMLServerImageHandler
  //Tell the Engine to prepend all images with this URL - Note this requires using the HTMLServerImageHandler
+
        renderContext.setBaseImageURL(<nowiki>"http:// localhost/myimages"</nowiki>);
  renderContext.setBaseImageURL(<nowiki>"http://localhost/myimages"</nowiki>);
+
        // Tell the Engine where to write the images to
  //Tell the Engine where to write the images to
+
        renderContext.setImageDirectory("C:/xampplite/htdocs/myimages");
  renderContext.setImageDirectory("C:/xampplite/htdocs/myimages");
+
        // Tell the Engine what image formats are supported.  Note you must have SVG in the string  
  //Tell the Engine what image formats are supported.  Note you must have SVG in the string  
+
        // to render charts in SVG.
  //to render charts in SVG.
+
        renderContext.setSupportedImageFormats("JPG;PNG;BMP;SVG");
  renderContext.setSupportedImageFormats("JPG;PNG;BMP;SVG");
+
        final HashMap<String, HTMLRenderContext> CONTEXT =
  HashMap<String, HTMLRenderContext> contextMap = new HashMap<String, HTMLRenderContext>();
+
              new HashMap<String, HTMLRenderContext>();
  contextMap.put( EngineConstants.APPCONTEXT_HTML_RENDER_CONTEXT, renderContext );
+
        CONTEXT.put(EngineConstants.APPCONTEXT_HTML_RENDER_CONTEXT, renderContext);
  task.setAppContext( contextMap );
+
        TASK.setAppContext(CONTEXT);
  //Set parameters for the report
+
        // Set PARAMETERS for the report
  task.setParameterValues(parameters);
+
        TASK.setParameterValues(PARAMETERS);
  //Alternatively set each seperately
+
        // Alternatively set each separately
  //task.setParameterValue("Top Count", new Integer(12));
+
        // TASK.setParameterValue("Top Count", new Integer(12));
  task.validateParameters();
+
        TASK.validateParameters();
 
+
       
  //Add a scriptable object, which will allow the report developer to put
+
        // Add a scriptable object, which will allow the report developer to put
  //script in the report that references this Java object. eg in script  
+
        // script in the report that references this Java object, e.g. in script  
  //pFilter.myjavamethod()
+
        // pFilter.myjavamethod()
  //ProcessFilter pf = new ProcessFilter();
+
        // final ProcessFilter PF = new ProcessFilter();
  //task.addScriptableJavaObject("pFilter", pf);
+
        // TASK.addScriptableJavaObject("pFilter", PF);
 
+
       
  //Set rendering options - such as file or stream output,  
+
        // Set rendering options - such as file or stream output,  
  //output format, whether it is embeddable, etc
+
        // output format, whether it is embeddable, etc
  HTMLRenderOption options = new HTMLRenderOption();
+
        HTMLRenderOption options = new HTMLRenderOption();
 
+
       
  //Remove HTML and Body tags
+
        // Remove HTML and Body tags
  //options.setEmbeddable(true);
+
        // options.setEmbeddable(true);
 
+
       
  //Set ouptut location
+
        // Set output location
  options.setOutputFileName("C:/test/2.1/output.html");
+
        options.setOutputFileName("C:/test/2.1/output.html");
 
+
       
  //Set output format
+
        // Set output format
  options.setOutputFormat("html");
+
        options.setOutputFormat("html");
  task.setRenderOption(options);
+
        TASK.setRenderOption(options);
 
+
       
  //run the report and destroy the engine
+
        // run the report and destroy the engine
  //Note - If the program stays resident do not shutdown the Platform or the Engine
+
        // Note - If the program stays resident do not shutdown the Platform or the Engine
  task.run();
+
        TASK.run();
  task.close();
+
        TASK.close();
  engine.shutdown();
+
        engine.shutdown();
  Platform.shutdown();
+
        Platform.shutdown();
  System.out.println("Finished");
+
        System.out.println("Finished");
}  
+
    }
/**
+
  * @param args
+
  */
+
public static void main(String[] args) {
+
  try
+
  {
+
  executeReport( );
+
  }
+
  catch ( Exception e )
+
  {
+
  e.printStackTrace();
+
  }
+
}
+
 
+
}
+
 
+
  
 +
    /**
 +
    * @param ARGUMENTS
 +
    */
 +
    public static void main(final String[] ARGUMENTS) {
 +
        try {
 +
          executeReport();
 +
        } catch (final Exception EX) {
 +
          EX.printStackTrace();
 +
        }
 +
    }
 +
}
 +
</source>
 
----
 
----
 
BIRT 2.2
 
BIRT 2.2
public void executeReport() throws EngineException
+
 
{
+
<source lang=Java>
+
public void executeReport() throws EngineException {
IReportEngine engine=null;
+
   
EngineConfig config = null;
+
    IReportEngine engine = null;
+
    EngineConfig config = null;
try{
+
   
config = new EngineConfig( );
+
    try {
config.setBIRTHome("C:\\birt\\birt-runtime-2_2_1\\birt-runtime-2_2_1\\ReportEngine");
+
        config = new EngineConfig();        
config.setLogConfig("c:/temp/test", Level.FINEST);
+
        config.setBIRTHome("C:\\birt\\birt-runtime-2_2_1\\birt-runtime-2_2_1\\ReportEngine");
Platform.startup( config );
+
        config.setLogConfig("c:/temp/test", Level.FINEST);
IReportEngineFactory factory = (IReportEngineFactory) Platform
+
        Platform.startup(config);
.createFactoryObject( IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY );
+
        IReportEngineFactory factory = (IReportEngineFactory) Platform
engine = factory.createReportEngine( config );
+
        .createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);
+
        engine = factory.createReportEngine(config);      
IReportRunnable design = null;
+
   
//Open the report design
+
        IReportRunnable design = null;
design = engine.openReportDesign("Reports/customers.rptdesign");  
+
        //Open the report design
IRunAndRenderTask task = engine.createRunAndRenderTask(design);
+
        design = engine.openReportDesign("Reports/customers.rptdesign");  
//task.setParameterValue("Top Count", (new Integer(5)));
+
        IRunAndRenderTask task = engine.createRunAndRenderTask(design);      
//task.validateParameters();
+
        //task.setParameterValue("Top Count", (new Integer(5)));
+
        //task.validateParameters();
HTMLRenderOption options = new HTMLRenderOption();
+
             
options.setOutputFileName("output/resample/Parmdisp.html");
+
        HTMLRenderOption options = new HTMLRenderOption();      
options.setOutputFormat("html");
+
        options.setOutputFileName("output/resample/Parmdisp.html");
//options.setHtmlRtLFlag(false);
+
        options.setOutputFormat("html");
//options.setEmbeddable(false);
+
        //options.setHtmlRtLFlag(false);
//options.setImageDirectory("C:\\test\\images");
+
        //options.setEmbeddable(false);
+
        //options.setImageDirectory("C:\\test\\images");
//PDFRenderOption options = new PDFRenderOption();
+
   
//options.setOutputFileName("c:/temp/test.pdf");
+
        //PDFRenderOption options = new PDFRenderOption();
//options.setOutputFormat("pdf");
+
        //options.setOutputFileName("c:/temp/test.pdf");
+
        //options.setOutputFormat("pdf");
task.setRenderOption(options);
+
   
task.run();
+
        task.setRenderOption(options);
task.close();
+
        task.run();
engine.destroy();
+
        task.close();
}catch( Exception ex){
+
        engine.destroy();
ex.printStackTrace();
+
    } catch(final Exception EX) {
}
+
        EX.printStackTrace();
finally
+
    } finally {
{
+
      Platform.shutdown();
        Platform.shutdown( );
+
    }
}
+
}
 +
</source>
  
 
----
 
----
Line 196: Line 191:
 
In order to compile and run the sample code, you have to make sure that all the necessary libraries are in the classpath. I assume you are using eclipse as your development environment and that you created a java project where you created a java file containing the sample code above. Also you should have downloaded and installed the birt-runtime. (I guess you have because otherwise what did you set the engine home in the code above?)
 
In order to compile and run the sample code, you have to make sure that all the necessary libraries are in the classpath. I assume you are using eclipse as your development environment and that you created a java project where you created a java file containing the sample code above. Also you should have downloaded and installed the birt-runtime. (I guess you have because otherwise what did you set the engine home in the code above?)
  
First of all the question is which libraries do you have to add? The answer is quite easy: All the libraries in the birt-runtime-2_1_1/ReportEngine/lib directory. So all you have to do is to right click the project and choose the properties action to open the project property dialog. There select the "Java Build Path" prperty page. Go to the Libraries tab and add the above mentioned libraries using the "Add external jars ..." button.
+
First of all, the question is which libraries do you have to add? The answer is quite easy: All the libraries in the birt-runtime-2_1_1/ReportEngine/lib directory. So all you have to do is to right click the project and choose the properties action to open the project property dialog. There select the "Java Build Path" prperty page. Go to the Libraries tab and add the above mentioned libraries using the "Add external jars ..." button.
  
 
== Comments ==
 
== Comments ==

Revision as of 08:48, 12 October 2012

< To: Integration Examples (BIRT)

Contents

Integrating BIRT in a Simple Java Application

This example demonstrates using the Report Engine API (RE API) to run and render a report design to HTML.

Add comments at the bottom of the example.

BIRT Report Engine API: Return to the BIRT Report Engine API examples

BIRT Version

BIRT 2.1.1

Source

ExecuteReport.java

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;
 
public class ExecuteReport {
    static void executeReport() throws EngineException {
        final HashMap<String, Integer> PARAMETERS = new HashMap<String, Integer>();
 
        final String NAME = "Top Count";
        final Integer VALUE = new Integer(4);
        PARAMETERS.put(NAME, VALUE);
 
        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");
           // set log config using (null, Level) if you do not want a log file
           config.setLogConfig("C:/birt/logs", 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
        final HTMLEmitterConfig EMITTER_CONFIGURATION = new HTMLEmitterConfig();
        EMITTER_CONFIGURATION.setActionHandler(new HTMLActionHandler());
        HTMLServerImageHandler imageHandler = new HTMLServerImageHandler();
        EMITTER_CONFIGURATION.setImageHandler(imageHandler);
        config.getEmitterConfigs().put("html", EMITTER_CONFIGURATION); // $NON-NLS-1$
 
        // Open the report design
        final IReportRunnable DESIGN =
              engine.openReportDesign("C:/test/2.1/executereport/test.rptdesign"); 
 
        // Create task to run and render the report:
        final IRunAndRenderTask TASK = engine.createRunAndRenderTask(DESIGN); 
 
        // Set Render context to handle URL and image locations
        HTMLRenderContext renderContext = new HTMLRenderContext();
        // Set the Base URL for all actions
        renderContext.setBaseURL(<nowiki>"http:// localhost/"</nowiki>);
        // Tell the Engine to prepend all images with this URL - Note this requires using the HTMLServerImageHandler
        renderContext.setBaseImageURL(<nowiki>"http:// localhost/myimages"</nowiki>);
        // 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");
        final HashMap<String, HTMLRenderContext> CONTEXT =
              new HashMap<String, HTMLRenderContext>();
        CONTEXT.put(EngineConstants.APPCONTEXT_HTML_RENDER_CONTEXT, renderContext);
        TASK.setAppContext(CONTEXT);
        // Set PARAMETERS for the report
        TASK.setParameterValues(PARAMETERS);
        // Alternatively set each separately
        // TASK.setParameterValue("Top Count", new Integer(12));
        TASK.validateParameters();
 
        // Add a scriptable object, which will allow the report developer to put
        // script in the report that references this Java object, e.g. in script 
        // pFilter.myjavamethod()
        // final ProcessFilter PF = new ProcessFilter();
        // TASK.addScriptableJavaObject("pFilter", PF);
 
        // Set rendering options - such as file or stream output, 
        // output format, whether it is embeddable, etc
        HTMLRenderOption options = new HTMLRenderOption();
 
        // Remove HTML and Body tags
        // options.setEmbeddable(true);
 
        // Set output location
        options.setOutputFileName("C:/test/2.1/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");
    }
 
    /**
     * @param ARGUMENTS
     */
    public static void main(final String[] ARGUMENTS) {
        try {
           executeReport();
        } catch (final Exception EX) {
           EX.printStackTrace();
        }
    }
}

BIRT 2.2

public void executeReport() throws EngineException {
 
    IReportEngine engine = null;
    EngineConfig config = null;
 
    try {
        config = new EngineConfig();          
        config.setBIRTHome("C:\\birt\\birt-runtime-2_2_1\\birt-runtime-2_2_1\\ReportEngine");
        config.setLogConfig("c:/temp/test", Level.FINEST);
        Platform.startup(config);
        IReportEngineFactory factory = (IReportEngineFactory) Platform
        .createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);
        engine = factory.createReportEngine(config);       
 
        IReportRunnable design = null;
        //Open the report design
        design = engine.openReportDesign("Reports/customers.rptdesign"); 
        IRunAndRenderTask task = engine.createRunAndRenderTask(design);        
        //task.setParameterValue("Top Count", (new Integer(5)));
        //task.validateParameters();
 
        HTMLRenderOption options = new HTMLRenderOption();       
        options.setOutputFileName("output/resample/Parmdisp.html");
        options.setOutputFormat("html");
        //options.setHtmlRtLFlag(false);
        //options.setEmbeddable(false);
        //options.setImageDirectory("C:\\test\\images");
 
        //PDFRenderOption options = new PDFRenderOption();
        //options.setOutputFileName("c:/temp/test.pdf");
        //options.setOutputFormat("pdf");
 
        task.setRenderOption(options);
        task.run();
        task.close();
        engine.destroy();
    } catch(final Exception EX) {
        EX.printStackTrace();
    } finally {
       Platform.shutdown();
    }
}

In order to compile and run the sample code, you have to make sure that all the necessary libraries are in the classpath. I assume you are using eclipse as your development environment and that you created a java project where you created a java file containing the sample code above. Also you should have downloaded and installed the birt-runtime. (I guess you have because otherwise what did you set the engine home in the code above?)

First of all, the question is which libraries do you have to add? The answer is quite easy: All the libraries in the birt-runtime-2_1_1/ReportEngine/lib directory. So all you have to do is to right click the project and choose the properties action to open the project property dialog. There select the "Java Build Path" prperty page. Go to the Libraries tab and add the above mentioned libraries using the "Add external jars ..." button.

Comments

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