简体   繁体   中英

Start a BIRT report by Java (datasource is dynamic)

I want to ask, if somebody has some experience, to start BIRT report from Java code. The datasource of the elements of the BIRT template is dynamically. So, I must tell the datasource (CSV or XML files) via Java to the BIRT report. Maybe someone know a good example on the web.

For how to integrate BIRT in a Java application start with reading Report Engine API

BIRT also provides an API to create report templates. See Design Engine API

Even if the answer is accepted and I agree that there an API, when I needed to do this, I had to waste some precious time in order to get this to work, and as such, here is the code (partial) I ended up implementing in order to create a Java Runnable jar that can take some parameters in the console, and generate the HTML, PDF or both reports:

/**
 * 0x01 is PDF,
 * 0x02 is HTML
 */
static int supportedExportTypesFlag = 0x03;

public static void main(final String[] args) {

    int result = -1;

    IReportEngine engine = null;
    EngineConfig config = null;
    IReportRunnable design = null;

    Object[] validationResults = ValidateArguments(args);
    if (((Boolean)validationResults[0]).equals(Boolean.TRUE)) {
        try {
            config = new EngineConfig();
            config.setLogConfig("./", Level.FINE);

            Platform.startup(config);
            IReportEngineFactory factory = (IReportEngineFactory) Platform
                    .createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);
            engine = factory.createReportEngine(config);
            engine.changeLogLevel(Level.WARNING);

            design = engine.openReportDesign((String)validationResults[1]);

            IRunAndRenderTask task = engine.createRunAndRenderTask(design);

            byte[] paramStr = Files.readAllBytes(Paths.get((String)validationResults[3]));
            InputStream iS = new ByteArrayInputStream(paramStr);
            iS.mark(paramStr.length);

            HashMap<String, Object> contextMap = new HashMap<String, Object>();
            contextMap.put("org.eclipse.birt.report.data.oda.xml.inputStream", iS);
            contextMap.put("org.eclipse.birt.report.data.oda.xml.closeInputStream", Boolean.FALSE);
            task.setAppContext(contextMap);

            int exportTypes = ((Integer)validationResults[2]).intValue();

            if((exportTypes & 0x02) == 0x02)
            {
                final HTMLRenderOption HTML_OPTIONS = new HTMLRenderOption();
                HTML_OPTIONS.setOutputFileName((String)validationResults[4] + ".html");
                HTML_OPTIONS.setOutputFormat("html");

                task.setRenderOption(HTML_OPTIONS);
                task.run();
                task.close();
            }

            iS.reset();

            if((exportTypes & 0x01) == 0x01)
            {
                 final PDFRenderOption PDF_OPTIONS = new PDFRenderOption();
                 PDF_OPTIONS.setOutputFileName((String)validationResults[4] + ".pdf");
                 PDF_OPTIONS.setOutputFormat("pdf");

                 task = engine.createRunAndRenderTask(design);
                 task.setAppContext(contextMap);
                 task.setRenderOption(PDF_OPTIONS);
                 task.run();
                 task.close();
            }

            iS.close();

        } catch (IOException e) {
            result = 1;
            e.printStackTrace();
        } catch (EngineException e) {
            result = 2;
            e.printStackTrace();
        } catch (BirtException e) {
            result = 3;
            e.printStackTrace();
        }

        // Shutdown
        engine.destroy();
        Platform.shutdown();
        // Bugzilla 351052
        RegistryProviderFactory.releaseDefault();
        result = 0;
    }

    System.exit(result);
}

I think ValidateArguments here is not so important, you can guess what it does and what it returns.

Hope this will help someone!

This is my java code integrated within Spring MVC, hope it helps:

public String executeReport(String path,HttpServletRequest request) throws EngineException
{

    IReportEngine engine=null;
    EngineConfig config = null;

    try{
        // start up Platform
        config = new EngineConfig( );
        config.setLogConfig("/logs", java.util.logging.Level.FINEST);
        Platform.startup( config );


        // create new Report Engine
        IReportEngineFactory factory = (IReportEngineFactory) Platform
                .createFactoryObject( IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY );
        engine = factory.createReportEngine( config );



        String format = "html";
        ServletContext sc = request.getSession().getServletContext();
        if( format == null ){
            format="html";
        }
        // open the report design
        IReportRunnable design = null;
        design = engine.openReportDesign(path);

        // create RunandRender Task
        IRunAndRenderTask task = engine.createRunAndRenderTask(design);

        // pass necessary parameter
        task.setParameterValue("PARAMETER_NAME", "PARAMETER_VALUE");

        task.validateParameters();


        // set render options including output type
        HTMLRenderOption options = new HTMLRenderOption();
        ByteArrayOutputStream outs = new ByteArrayOutputStream();
        options.setSupportedImageFormats("PNG;GIF;JPG;BMP;SWF;SVG");
        options.setOutputStream(outs);

        options.setImageHandler(new HTMLServerImageHandler());
        options.setBaseImageURL(request.getContextPath()+"/images");
        options.setImageDirectory(sc.getRealPath("/images"));

        options.setEmbeddable(true);
        options.setOutputFormat("html");
        task.setRenderOption(options);

        // run task
        String output;
        task.run();
        output = outs.toString();
        task.close();
        engine.destroy();
        return output;
    }catch( Exception ex){
        ex.printStackTrace();
        return "Error";
    }
    finally
    {
        Platform.shutdown( );

    }
}

You can use Script datasource for get dynamic data in your birt report from database.

Go for: http://www.eclipse.org/birt/phoenix/examples/scripting/scripteddatasource/ for script datasource basic.

and further :http://www.eclipse.org/birt/phoenix/deploy/reportScripting.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM