How do you set and pass a parameter to a BIRT report created by the BIRT Report Designer through the BIRT API?

江枫思渺然 提交于 2019-12-04 19:34:16

问题


I've created a simple report that takes a single parameter. This parameter is used in the query and executes fine when directly executed in the report designer. By the way I'm not using javascript or any scripting for this report. I've seen some people trying to pass parameters using scripts and/or javascripts for answers here, however this is not what I'm doing. I pass all my parameters in through java. Continuing, in this report I'm listing active/inactive items. I pass in an 'N' for listing inactive items and a 'Y' for active items. When I try to pass in a parameter through the API, I always get a list of active items regardless to what I pass in. By the way 'Y' is the default value of the parameter passed in. (I am overriding the defaults in the code below) The problem I'm having is that the report doesn't seem to want to take the parameter I set. Yes the value changes in my variable passed in but the report doesn't reflect the change. My code is below. I've tried to follow the advice of this link and how to set the parameters.

http://www.eclipsezone.com/eclipse/forums/t67723.html

If you go to the link go down to #4 and see the list of tasks to do. This is what I have tried to follow. I feel I may be missing something. If you've got this going could you give me some advice to what I'm missing? Thanks much!

-Dale

    public class ReportGenerator {
        public static void main(String args[]) throws Exception{
        ReportGenerator rg = new ReportGenerator();
        rg.executeReport("N");
        }


        @SuppressWarnings({ "unchecked", "deprecation" })
        public void executeReport(String activeIndicator) throws EngineException {

        IReportEngine engine=null;
        EngineConfig config = null;

        try{
            config = new EngineConfig( );            
            config.setBIRTHome("C:\\birt-rcp-report-designer-3_7_2\\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 reportDesign = null;
            reportDesign = engine.openReportDesign("C:\\workspace\\SimpleReport\\ReportTemplates\\ItemListingReport.rptdesign"); 
            IRunAndRenderTask task = engine.createRunAndRenderTask(reportDesign);
            IGetParameterDefinitionTask parameterDefinitionTask = engine.createGetParameterDefinitionTask(reportDesign);
            parameterDefinitionTask.evaluateDefaults();
            HashMap<String, String> params = parameterDefinitionTask.getDefaultValues();
            params.put("aIndicator", activeIndicator);
            parameterDefinitionTask.setParameterValues(params);

            ConnectionHelper connectionHelper = new ConnectionHelper();
            task.getAppContext().put("OdaJDBCDriverPassInConnection", connectionHelper.getConnection());

            PDFRenderOption options = new PDFRenderOption();
            options.setOutputFormat("pdf");
            options.setOutputFileName("C:\\workspace\\SimpleReport\\output\\itemListingReport.pdf");

            task.setRenderOption(options);

            task.run();
            task.close();
            engine.destroy();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            Platform.shutdown();
        }
        }
    }

回答1:


You need to set the parameters on the IRunAndRenderTask:

IRunAndRenderTask task =
    engine.createRunAndRenderTask(reportRunnable);
Map< String, Object > birtParams = ...;
task.setParameterValues( birtParams );


来源:https://stackoverflow.com/questions/10747427/how-do-you-set-and-pass-a-parameter-to-a-birt-report-created-by-the-birt-report

工具导航Map