Generate Report Programatically using Design API & flat file as data source [message #164718] |
Wed, 09 August 2006 06:34 |
Eclipse User |
|
|
|
Originally posted by: rd.xxxxxxx.com
Hi all,
I want to automate the report generation programmatically.
I have generated a report using UI & flat file as data source.
I came to know that for generating report by Flat file as a data source,
the text file must be comma delimited in which column headings & values
are comma separated. So I passed a text file as input which stores a HTML
table in comma delimited format.
I used Table view for the same & I got the desired report.
Now I have to generate the same report programmatically in which the input
will be the same text file & desired column names as report parameters.
Some Environmental details you will want to know ;
birt-runtime 2.1.0
Eclipse version: Eclipse 3.2
ODA driver for accessing flat file data sources i.e.
org.eclipse.birt.report.data.oda.flatfile
When I execute the program I get only column headings.
but not the content of it.
So, can anybody guide me how to fetch the text file & disply its content?
I am expecting the report format as :
First Name Last Name
xyz pqr
abc lmn
I'm attching my sample program here.
package bt;
import java.io.IOException;
import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.report.engine.api.EngineConfig;
import org.eclipse.birt.report.model.api.CellHandle;
import org.eclipse.birt.report.model.api.DataSourceHandle;
import org.eclipse.birt.report.model.api.DataSetHandle;
import org.eclipse.birt.report.model.api.DataSetParameterHandle;
import org.eclipse.birt.report.model.api.DesignConfig;
import org.eclipse.birt.report.model.api.DesignElementHandle;
import org.eclipse.birt.report.model.api.DesignEngine;
import org.eclipse.birt.report.model.api.ElementFactory;
import org.eclipse.birt.report.model.api.GridHandle;
import org.eclipse.birt.report.model.api.ImageHandle;
import org.eclipse.birt.report.model.api.LabelHandle;
import org.eclipse.birt.report.model.api.ReportDesignHandle;
import org.eclipse.birt.report.model.api.RowHandle;
import org.eclipse.birt.report.model.api.SessionHandle;
import org.eclipse.birt.report.model.api.TableHandle;
import org.eclipse.birt.report.model.api.activity.SemanticException ;
import org.eclipse.birt.report.model.elements.ReportDesign;
import org.eclipse.birt.report.model.api.ColumnHandle;
import org.eclipse.birt.report.model.api.DataItemHandle;
import org.eclipse.birt.report.model.api.ElementDetailHandle;
import org.eclipse.birt.report.model.api.IDesignEngine;
import org.eclipse.birt.report.model.api.IDesignEngineFactory;
import org.eclipse.birt.report.model.api.ListHandle;
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.ResultSetColumnHandle;
import org.eclipse.birt.report.model.api.SimpleValueHandle;
import org.eclipse.birt.report.model.api.TableHandle;
import java.util.Iterator;
import org.eclipse.birt.report.model.api.elements.DesignChoiceConst ants;
import org.eclipse.birt.report.model.api.elements.table.*;
import org.eclipse.birt.report.model.api.ColumnBandData.*;
import org.eclipse.birt.report.model.api.SlotHandle;
import org.eclipse.birt.report.model.api.core.UserPropertyDefn;
import org.eclipse.birt.report.model.core.Structure;
import org.eclipse.birt.report.model.api.StructureHandle;
import
org.eclipse.birt.report.model.api.elements.structures.DataSe tParameter;
import
org.eclipse.birt.report.model.api.elements.structures.Result SetColumn;
import org.eclipse.birt.report.model.api.StructureFactory;
import java.lang.Object.*;
import
org.eclipse.birt.report.model.api.elements.structures.Comput edColumn;
import org.eclipse.birt.report.model.api.TableGroupHandle;
import com.ibm.icu.util.ULocale;
public class sample {
static void createReport()throws IOException, SemanticException
{
ReportDesignHandle designHandle = null;
SessionHandle sessionHandle = null;
ComputedColumn cs1, cs2, cs3 = null;
DesignConfig dc = new DesignConfig();
dc.setConfigurationVariable("BIRT_HOME",
"C:\\birtruntime\\birt-runtime-2_1_0\\ReportEngine");
IDesignEngine engine = null;
try {
Platform.startup(dc);
IDesignEngineFactory factory = (IDesignEngineFactory)
Platform.createFactoryObject(IDesignEngineFactory.EXTENSION_ DESIGN_ENGINE_FACTORY);
engine = factory.createDesignEngine(dc);
} catch (Exception ex) {
ex.printStackTrace();
}
sessionHandle = new DesignEngine( dc ).newSessionHandle( ULocale.ENGLISH );
// Create a new report design.
ReportDesignHandle design = sessionHandle.createDesign( );
// The element factory creates instances of the various BIRT elements.
ElementFactory factory = design.getElementFactory( );
// Create a simple master page
DesignElementHandle element = ((ElementFactory)
factory).newSimpleMasterPage( "Page Master" );
design.getMasterPages( ).add( element );
OdaDataSourceHandle odaDS = ((ElementFactory)
factory).newOdaDataSource("Data
Source","org.eclipse.datatools.connectivity.oda.flatfile");
System.out.println("data source :" + odaDS.toString());
odaDS.setDisplayName("Data Source");
odaDS.setProperty("HOME", "C:\\files");
odaDS.setProperty("CHARSET", "UTF-8");
odaDS.setProperty("INCLTYPELINE", "NO");
//design.getDataSources( ).add( odaDS );
SlotHandle lDSH = design.getDataSources();
int liS1 = lDSH.getCount();
lDSH.add(odaDS);
lDSH = design.getDataSources();
int liS2 = lDSH.getCount();
//dataset
OdaDataSetHandle dataSetHandle = ((ElementFactory)
factory).newOdaDataSet("Data
Set","org.eclipse.datatools.connectivity.oda.flatfile.dataSet ");
System.out.println("data handle : " + dataSetHandle.toString());
//query text
//output.txt contains comma separated column headings & their values
dataSetHandle.setQueryText ("select First Name, Last Name from
output.txt");
dataSetHandle.setDataSource ("Data Source");
SlotHandle lDSetH = design.getDataSets();
lDSetH.add(dataSetHandle);
ListHandle lMain = ((ElementFactory) factory).newList( null
);
design.getBody( ).add( lMain );
PropertyHandle lPH =
dataSetHandle.getPropertyHandle(DataSetHandle.RESULT_SET_PRO P);
lPH.setStringValue(null);
ResultSetColumn lColA =
StructureFactory.createResultSetColumn();
lColA.setColumnName("First Name");
lColA.setDataType("String");
lColA.setPosition(new Integer(0));
lColA.setNativeDataType(new Integer(12));
lPH.addItem(lColA);
ResultSetColumn lColB =
StructureFactory.createResultSetColumn();
lColB.setColumnName("Last Name");
lColB.setDataType("String");
lColB.setPosition(new Integer(1));
lColB.setNativeDataType(new Integer(13));
lPH.addItem(lColB);
Iterator it =
dataSetHandle.resultSetIterator();
Object lO = null;
while( it.hasNext()) {
lO = it.next();
ResultSetColumnHandle lRS =
(ResultSetColumnHandle )lO;
}
// Create lable
//lMain.setDataSet(dataSetHandle);
//String lsOnRow = lMain.getOnRow();
LabelHandle lHeader = ((ElementFactory) factory).newLabel( null );
lMain.getHeader().add(lHeader);
lHeader.setText( "Header...." ); //$NON-NLS-1$
GridHandle grid = ((ElementFactory) factory).newGridItem("My Grid" , 3 ,1);
grid.setDataSet(dataSetHandle);
grid.setWidth("100%");
design.getBody().add(grid);
//First Grid Row
RowHandle row = (RowHandle)grid.getRows().get(0);
// Cell (1st Row)
CellHandle cell = (CellHandle) row.getCells().get(0);
TableHandle tableHandle = ((ElementFactory) factory).newTableItem("My
Table" ,3);//$NON-NLS-1$
tableHandle.setWidth("100%");
tableHandle.setDataSet(dataSetHandle);
cell.getContent( ).add( tableHandle );
tableHandle.setProperty( TableHandle.DATA_SET_PROP, "Data Set");
PropertyHandle computedSet = tableHandle.getColumnBindings( );
cs1 = StructureFactory.createComputedColumn();
cs1.setName("First Name");//$NON-NLS-1$
cs1.setExpression("row[\"First Name\"]");//$NON-NLS-1$
cs1.setDataType("string");
//cs1.setAggregateOn("MyGroup");
computedSet.addItem(cs1);
cs2 = StructureFactory.createComputedColumn();
cs2.setName("Last Name");//$NON-NLS-1$
cs2.setExpression("row[\"Last Name\"]");//$NON-NLS-1$
cs1.setDataType("string");
computedSet.addItem(cs2);
// 1
//Header
RowHandle header = (RowHandle) tableHandle.getHeader( ).get( 0 );
CellHandle tcell = (CellHandle) header.getCells( ).get( 0 );
LabelHandle label = ((ElementFactory) factory).newLabel( null );
label.setText( "First Name" );//$NON-NLS-1$
//label.setStyleName( "Label" );//$NON-NLS-1$
tcell.getContent( ).add( label );
tcell = (CellHandle) header.getCells( ).get( 1 );
label = ((ElementFactory) factory).newLabel( null );
label.setText( "Last Name" );//$NON-NLS-1$
//label.setStyleName( "Label" );//$NON-NLS-1$
tcell.getContent( ).add( label );
tcell = (CellHandle) header.getCells( ).get( 2 );
// 3
//Detail
RowHandle detail = (RowHandle) tableHandle.getDetail( ).get( 0 );
DataItemHandle data =((ElementFactory) factory).newDataItem( null );
tcell = (CellHandle) detail.getCells( ).get( 1 );
data = ((ElementFactory) factory).newDataItem( null );
//data.setStyleName( "Data" );//$NON-NLS-1$
data.setResultSetColumn( cs1.getName( ) );
tcell.getContent( ).add( data );
tcell = (CellHandle) detail.getCells( ).get( 2 );
data = ((ElementFactory) factory).newDataItem( null );
//data.setStyleName( "Data" );//$NON-NLS-1$
data.setResultSetColumn( cs2.getName( ) );
tcell.getContent( ).add( data );
System.out.println("hi");
//cell.getContent( ).add( tableHandle );
System.out.println("bye");
design.saveAs( "f:/reports/sample1" +".rptdesign" );
//design.saveAs("f:/reports/x6.rptdesign ");
design.close( );
//sessionHandle.closeAll(true);
System.out.println("Finished!");
}
public static void main(String[] args) {
try
{
createReport( );
}
catch ( Exception e )
{
e.printStackTrace();
}
}
}
|
|
|
Powered by
FUDForum. Page generated in 1.76648 seconds