BIRT Design API 学习
以下这个例子来自birt的官方教材,我没有改动任何的信息.
这个例子演示了从建立DataSource,然后建立DataSet,动态的根据输入数据输出reporttemplate.
关于Birt的API,在eclipse的helpcontent里面有,3.3支持新的基于topic的search,可以帮我们简化搜索的topic,其中有五个API(一共是5个):ReportObjectModelAPI,ReportEngineAPI,BirtReportScriptingAPI,OpenDataAccessAPI,DataEngineAPI.另外也提供详细的讲解每一个report的元素的意思.非常好的一份资料.
DECreateDynamicTable.java例子code:
importjava.io.IOException;
importjava.util.ArrayList;
importorg.eclipse.birt.core.framework.Platform;
importorg.eclipse.birt.report.model.api.CellHandle;
importorg.eclipse.birt.report.model.api.DataItemHandle;
importorg.eclipse.birt.report.model.api.DesignConfig;
importorg.eclipse.birt.report.model.api.ElementFactory;
importorg.eclipse.birt.report.model.api.IDesignEngine;
importorg.eclipse.birt.report.model.api.IDesignEngineFactory;
importorg.eclipse.birt.report.model.api.LabelHandle;
importorg.eclipse.birt.report.model.api.OdaDataSetHandle;
importorg.eclipse.birt.report.model.api.OdaDataSourceHandle;
importorg.eclipse.birt.report.model.api.PropertyHandle;
importorg.eclipse.birt.report.model.api.ReportDesignHandle;
importorg.eclipse.birt.report.model.api.RowHandle;
importorg.eclipse.birt.report.model.api.SessionHandle;
importorg.eclipse.birt.report.model.api.StructureFactory;
importorg.eclipse.birt.report.model.api.TableHandle;
importorg.eclipse.birt.report.model.api.activity.SemanticException;
importorg.eclipse.birt.report.model.api.elements.structures.ComputedColumn;
importcom.ibm.icu.util.ULocale;
/**
*DynamicTableBIRTDesignEngineAPI(DEAPI)demo.
*/
publicclassDECreateDynamicTable
{
ReportDesignHandledesignHandle=null;
ElementFactorydesignFactory=null;
StructureFactorystructFactory=null;
publicstaticvoidmain(String[]args)
{
try
{
DECreateDynamicTablede=newDECreateDynamicTable();
ArrayListal=newArrayList();
al.add("OFFICECODE");
al.add("CITY");
al.add("COUNTRY");
de.buildReport(al,"FromOffices");
}
catch(IOExceptione)
{
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
catch(SemanticExceptione)
{
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
voidbuildDataSource()throwsSemanticException
{
OdaDataSourceHandledsHandle=designFactory.newOdaDataSource(
"DataSource","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);
}
voidbuildDataSet(ArrayListcols,StringfromClause)throwsSemanticException
{
OdaDataSetHandledsHandle=designFactory.newOdaDataSet("ds",
"org.eclipse.birt.report.data.oda.jdbc.JdbcSelectDataSet");
dsHandle.setDataSource("DataSource");
Stringqry="Select";
for(inti=0;i<cols.size();i++){
qry+=""+cols.get(i);
if(i!=(cols.size()-1)){
qry+=",";
}
}
qry+=""+fromClause;
dsHandle.setQueryText(qry);
designHandle.getDataSets().add(dsHandle);
}
voidbuildReport(ArrayListcols,StringfromClause)throwsIOException,SemanticException
{
//ConfiguretheEngineandstartthePlatform
DesignConfigconfig=newDesignConfig();
config.setProperty("BIRT_HOME","C:/birt-runtime-2_1_1/birt-runtime-2_1_1/ReportEngine");
IDesignEngineengine=null;
try{
Platform.startup(config);
IDesignEngineFactoryfactory=(IDesignEngineFactory)Platform
.createFactoryObject(IDesignEngineFactory.EXTENSION_DESIGN_ENGINE_FACTORY);
engine=factory.createDesignEngine(config);
}catch(Exceptionex){
ex.printStackTrace();
}
SessionHandlesession=engine.newSessionHandle(ULocale.ENGLISH);
try{
//openadesignoratemplate
designHandle=session.openDesign("c:/tmp/testdeapi.rptdesign");
designFactory=designHandle.getElementFactory();
buildDataSource();
buildDataSet(cols,fromClause);
TableHandletable=designFactory.newTableItem("table",cols.size());
table.setWidth("100%");
table.setDataSet(designHandle.findDataSet("ds"));
PropertyHandlecomputedSet=table.getColumnBindings();
ComputedColumncs1=null;
for(inti=0;i<cols.size();i++){
cs1=StructureFactory.createComputedColumn();
cs1.setName((String)cols.get(i));
cs1.setExpression("dataSetRow[\""+(String)cols.get(i)+"\"]");
computedSet.addItem(cs1);
}
//tableheader
RowHandletableheader=(RowHandle)table.getHeader().get(0);
for(inti=0;i<cols.size();i++){
LabelHandlelabel1=designFactory.newLabel((String)cols.get(i));
label1.setText((String)cols.get(i));
CellHandlecell=(CellHandle)tableheader.getCells().get(i);
cell.getContent().add(label1);
}
//tabledetail
RowHandletabledetail=(RowHandle)table.getDetail().get(0);
for(inti=0;i<cols.size();i++){
CellHandlecell=(CellHandle)tabledetail.getCells().get(i);
DataItemHandledata=designFactory.newDataItem("data_"+(String)cols.get(i));
data.setResultSetColumn((String)cols.get(i));
cell.getContent().add(data);
}
designHandle.getBody().add(table);
//Savethedesignandcloseit.
designHandle.saveAs("c:/temp/sample.rptdesign");//$NON-NLS-1$
designHandle.close();
System.out.println("Finished");
}catch(Exceptione){
e.printStackTrace();
}
}
}
这个例子一共有四个函数:
1.Main函数:这个例子简单之处在与它可以直接的运行,只要你修改了
config.setProperty("BIRT_HOME","C:/birt-runtime-2_1_1/birt-runtime-2_1_1/ReportEngine");指向你自己的BirtRuntime解压后的ReportEngine目录.
designHandle=session.openDesign("c:/tmp/testdeapi.rptdesign");你可以从Birt里面建立一个新的Reporttemplate.然后指向这个report就可以了
designHandle.saveAs("c:/temp/sample.rptdesign");//$NON-NLS-1$指定一个你想保存的位置,c:/temp目录存在你才能够保存到c:/temp目录下.
2.buildDataSource函数把一个ReportDesignHandle的DataSource初始化,setProperties左边的String是不能变的,DataSource的名字可以随便取,取DataSet的时候要根据这个名字来取.
3.buildDataSet通过拼sql的方式,来buildDataSet,注意sql别拼错了.
4.buildReport注意element的初始化顺序.在所有的DataItem外面都是一层Cell,Cell外面才是row.这个例子使用的row来拼成table的,也可以用column来拼,相对应的数据处理也是一个column一个column的处理的了.