Jump to: navigation, search

Difference between revisions of "Execute Modified Report"

 
(Replacing page with '== Execute Modified Report == Category:BIRT Category:BIRT Example OLD')
Line 1: Line 1:
== Execute Modifed Report  ==
+
== Execute Modified 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.
 
  
[[BIRT Design Engine API]] Return to the BIRT Design Engine API examples
+
[[Category:BIRT]]
 
+
[[Category:BIRT Example OLD]]
== 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.
+
 
+
----
+

Revision as of 13:08, 9 April 2007

Execute Modified Report