如何以编程方式设置 BIRT 报告的数据源?

2024-02-21

我有一个连接到我们的测试数据库的 BIRT 报告。在生产环境中,我想提供一个由容器通过 jndi 提供的数据源。

如何以编程方式为给定报告设置数据源?

    ...
    IReportRunnable design = birtEngine.openReportDesign ( new File ( properties.getProperty ( "reportPath" ), report + ".rptdesign" ).getAbsolutePath () );
    IRunAndRenderTask task = birtEngine.createRunAndRenderTask ( design );

    PDFRenderOption options = new PDFRenderOption ();
    options.setOutputFormat ( PDFRenderOption.OUTPUT_FORMAT_PDF );
    options.setOutputStream ( out );
    task.setRenderOption ( options );
    for ( Entry<String, Object> entry : parameters.entrySet () )
    {
        task.setParameterValue ( entry.getKey (), entry.getValue () );
    }

    task.run ();
    task.close ();
    ...

我想我必须修改design但另一方面task有一个方法setDataSource但这看起来有点像我必须提供一些 xml dom 元素。


查看以下代码,您可能会在运行时提供数据源方面获得一些帮助。

对于我的要求,它工作得很好。

我从某个网站得到这个不记得了。

import java.io.IOException;
import java.util.ArrayList; 

import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.report.model.api.CellHandle;
import org.eclipse.birt.report.model.api.DataItemHandle;
import org.eclipse.birt.report.model.api.DesignConfig; 
import org.eclipse.birt.report.model.api.ElementFactory;
import org.eclipse.birt.report.model.api.IDesignEngine;
import org.eclipse.birt.report.model.api.IDesignEngineFactory;
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.SessionHandle;
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;

import com.ibm.icu.util.ULocale;

/**
 * Dynamic Table BIRT Design Engine API (DEAPI) demo.
 */

public class DECreateDynamicTable
{
    ReportDesignHandle designHandle = null;
    ElementFactory designFactory = null;
    StructureFactory structFactory = null;  

    public static void main( String[] args )
    {
        try
        {
            DECreateDynamicTable de = new DECreateDynamicTable();
            ArrayList al = new ArrayList();
            al.add("USERNAME");
            al.add("COUNTRY");
            de.buildReport(al, "From GTM_REPORT_APP_USER" );
        }
        catch ( IOException e )
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch ( SemanticException e )
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    void buildDataSource( ) throws SemanticException
    {

        OdaDataSourceHandle dsHandle = designFactory.newOdaDataSource(
                "Data Source", "org.eclipse.birt.report.data.oda.jdbc" );
        dsHandle.setProperty( "odaDriverClass",
                "oracle.jdbc.driver.OracleDriver" );
        dsHandle.setProperty( "odaURL", "jdbc:oracle:thin:@xeon:1521:ora9i" );
        dsHandle.setProperty( "odaUser", "AIMS_GTMNE" );
        dsHandle.setProperty( "odaPassword", "AIMS_GTMNE" );

        designHandle.getDataSources( ).add( dsHandle );

    }

    void buildDataSet(ArrayList cols, String fromClause ) 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 );



    }
    void buildReport(ArrayList cols, String fromClause ) throws IOException, SemanticException
    {


        //Configure the Engine and start the Platform
        DesignConfig config = new DesignConfig( );

        config.setProperty("BIRT_HOME", "D:/Softwares/Frame Works - APIs-Tools/birt-runtime-2_6_1/birt-runtime-2_6_1/ReportEngine");

        IDesignEngine engine = null;
        try{


            Platform.startup( config );
            IDesignEngineFactory factory = (IDesignEngineFactory) Platform.createFactoryObject( IDesignEngineFactory.EXTENSION_DESIGN_ENGINE_FACTORY );
            engine = factory.createDesignEngine( config );

        }catch( Exception ex){
            ex.printStackTrace();
        }       


        SessionHandle session = engine.newSessionHandle( ULocale.ENGLISH ) ;



        try{
            //open a design or a template

            designHandle = session.openDesign("D:/tempBirtReport/test.rptdesign");

            designFactory = designHandle.getElementFactory( );

            buildDataSource();
            buildDataSet(cols, fromClause);

            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 );

            // Save the design and close it. 

            designHandle.saveAs( "D:/tempBirtReport/test.rptdesign" ); //$NON-NLS-1$
            designHandle.close( );
            System.out.println("Finished");
        }catch (Exception e){
            e.printStackTrace();
        }       

    }
 }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何以编程方式设置 BIRT 报告的数据源? 的相关文章

随机推荐

  • Laravel 中的 VUE2 组件注册

    Laravel 5 8 和 VUE 一起工作得非常好 但是我的 app js 变得很大 例如 我有以下 app js window Vue require vue window Vue component Comp1 require com
  • 带有 Bluebeam 的 JavaScript 库

    我正在寻找一些信息 参考 示例以及如何在 Bluebeam 中使用 javascript Bluebeam表示可以参考以下AcroForm文档 https www adobe com content dam acom en devnet a
  • 使用 Angular-ui Bootstrap 设置预输入选项

    使用 typeahead 我尝试设置几个选项 这些选项可作为标准 Bootstrap 使用预输入选项 http twitter github io bootstrap javascript html typeahead 使用以下代码 我可以
  • Nasm - 按值和地址访问结构元素

    我最近开始在 NASM 程序集中编码 我的问题是我不知道如何以正确的方式访问结构元素 我已经在这个网站和谷歌上搜索了解决方案 但我看到的每个地方人们都有不同的说法 我的程序崩溃了 我感觉问题出在访问结构上 查看示例代码时 STRUC Tes
  • CoreBluetooth 无法读取固件修订字符串

    我正在尝试检索外围设备的固件修订字符串 当通过应用程序 LightBlue 询问我的外围设备时 我可以查看设备信息 其中包括 制造商名称字符串 固件修订字符串 但是 在我的代码中 我无法发现固件修订字符串的特征 我已经尝试过以下 UUID
  • 不要使用 Proguard 优化特定的类路径

    我尝试在我的 Android 应用程序中实现亚马逊应用内购买 亚马逊人文档 https developer amazon com public apis earn in app purchasing docs code obfuscatio
  • 帮助 Kohana 3 ORM 加快一点速度

    我注意到 当我开始使用它们时 Kohana 3 ORM 会为每个模型运行 显示完整列 SHOW FULL COLUMNS FROM mytable 此查询可能需要几个时钟周期才能执行 在 Kohana 分析器中 它实际上是我当前应用程序中运
  • 避免时间序列一维图中的项目标签重叠 (JFreeChart)

    我正在尝试找到生成带有标签的一维时间图的最佳策略 目前看起来是这样的 问题是 有些时间跨度几乎没有发生什么 而另一些时间跨度则密度很高 我想要实现的是扭曲时间轴以便在密集区域中标签间隔开以避免重叠 或者 我可以想象显示没有轴扭曲的实际数据点
  • JavaScript 时间戳到 Python 日期时间转换

    为了在 JavaScript 中获取时间戳 我们使用 var ts new Date getTime 将其转换为Python的正确方法是什么datetime到目前为止我使用以下代码 gt gt gt jsts 1335205804950 g
  • D 项目的 CMake 或 Waf

    我们正在寻找足够的构建工具 用于用 D 语言 使用 Qt 工具包 编写桌面 GUI 应用程序 由多个本机库组成 使用第 3 方 C lib 它必须在 Linux 本机开发 和 Mac 以及 Windows 上构建 我们可能会采用代码 块 h
  • 按照约定使用接口拦截器进行 Unity 注册会导致“[type] 不可拦截”异常

    我想将所有实现特定接口的类注册到 Unity 中WithMappings FromMatchingInterface习俗 此外 我希望使用接口拦截行为来拦截所有已注册的对象 问题是 Unity 还会注册具体类之间的映射 当解析这些类时 会抛
  • e(fx)clipse javafx的导入无法解析

    嘿 我刚刚为 eclipse 安装了 e fx clipse 插件并创建了一个新的 JavaFX 项目 问题是所有 javafx 导入都无法解析 即使库似乎位于构建路径中 以下是一些屏幕截图 可以向您展示我的意思 有人知道我做错了什么吗 通
  • 如何使用“godoc”生成 HTML 文档?

    我编写了一个小型 go 程序 我想从源代码生成独立的 HTML 文档 无需 godoc 服务器即可查看 但我找不到任何方法来实现它 如果有人可以帮助我 我将不胜感激 可以通过以下方式生成更好的形式 godoc url http localh
  • Travis-CI 跳过部署,尽管已标记“提交”

    我对 Travis CI 还很陌生 但我使用他们的文档找到了解决方法 然而 部署到 GitHub 版本对我来说不起作用 我的 travis yml文件看起来像这样 language java branches only master not
  • 窗口关闭时停止模式(Cocoa)

    我当前正在使用以下代码显示模式窗口 NSApplication sharedApplication runModalForWindow mainWindow 但是 当我关闭此窗口时 其他窗口仍然处于非活动状态 我如何运行stopModal使
  • 在哪里可以找到 MATLAB 的形式语法?

    我想编写一个词法分析器生成器 将 MATLAB 语言的基本子集转换为 C C 等 为了帮助我做到这一点 我想找到一个包含 MATLAB 形式语法的文档 花了一些时间调查这一点 Mathworks 似乎没有提供这一点 有谁知道我在哪里可以找到
  • 如何在 iOS 中验证美国或加拿大的邮政编码?

    我想知道有什么方法可以验证美国或加拿大的邮政编码吗 我尝试使用正则表达式 就像美国一样 BOOL validateZip NSString candidate NSString emailRegex 5 4 ABCEGHJKLMNPRSTV
  • 可以跳过幼儿园吗?

    如果我知道某个值可能会在第一次遇到垃圾收集器时幸存下来 是否有某种方法让 GHC 知道 以便它可以直接将其分配到托儿所之外的某个地方 例如 如果我用一堆较小的部件建造一个大型结构 我知道每个部件至少会持续到整个结构完成为止 In GHC 垃
  • Angular 6 材料 - 如何从 matDatepicker 获取日期和时间?

    我的 html 中有这段代码
  • 如何以编程方式设置 BIRT 报告的数据源?

    我有一个连接到我们的测试数据库的 BIRT 报告 在生产环境中 我想提供一个由容器通过 jndi 提供的数据源 如何以编程方式为给定报告设置数据源 IReportRunnable design birtEngine openReportDe