`

BIRT/Stripes framework example

 
阅读更多

BirtEngine.java
BIRT Report Engine configuration.

 

package com.samaxes.webreport.presentation;
 
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.Level;
 
import javax.servlet.ServletContext;
 
import org.eclipse.birt.core.exception.BirtException;
import org.eclipse.birt.core.framework.IPlatformContext;
import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.core.framework.PlatformServletContext;
import org.eclipse.birt.report.engine.api.EngineConfig;
import org.eclipse.birt.report.engine.api.IReportEngine;
import org.eclipse.birt.report.engine.api.IReportEngineFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
/**
 * BIRT Report Engine configuration.
 *
 * @author Samuel Santos
 * @version $Revision$
 */
public class BirtEngine {
 
    private static final Logger logger = LoggerFactory.getLogger(BirtEngine.class);
 
    private static IReportEngine birtEngine = null;
 
    private static Level level = Level.OFF;
 
    private static String logDirectory = null;
 
    /**
     * Gets the BIRT Report Engine.
     *
     * @return the BIRT Report Engine
     */
    public static IReportEngine getBirtEngine() {
        return birtEngine;
    }
 
    /**
     * Initialize BIRT Report Engine configuration.
     *
     * @param servletContext the ServletContext
     */
    public static synchronized void initBirtEngine(ServletContext servletContext) {
        if (birtEngine == null) {
            loadEngineProps();
 
            IPlatformContext context = new PlatformServletContext(servletContext);
            EngineConfig config = new EngineConfig();
 
            config.setLogConfig(logDirectory, level);
            config.setEngineHome("");
            config.setPlatformContext(context);
            config.setResourcePath(BirtEngine.class.getResource("/").getPath());
 
            try {
                Platform.startup(config);
            } catch (BirtException e) {
                logger.error(e.getMessage(), e);
            }
 
            IReportEngineFactory factory = (IReportEngineFactory) Platform
                    .createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);
            birtEngine = factory.createReportEngine(config);
            birtEngine.changeLogLevel(Level.WARNING);
        }
    }
 
    /**
     * Destroys the BIRT Report Engine.
     */
    public static synchronized void destroyBirtEngine() {
        if (birtEngine != null) {
            birtEngine.destroy();
        }
        Platform.shutdown();
    }
 
    /**
     * Creates and returns a copy of this object.
     *
     * @return a clone of this instance.
     * @exception CloneNotSupportedException if the object's class does not support the <code>Cloneable</code>
     *                interface. Subclasses that override the <code>clone</code> method can also throw this exception
     *                to indicate that an instance cannot be cloned.
     */
    public Object clone() throws CloneNotSupportedException {
        throw new CloneNotSupportedException();
    }
 
    /**
     * Loads the Engone properties.
     */
    private static void loadEngineProps() {
        try {
            // Config File must be in classpath
            ClassLoader cl = Thread.currentThread().getContextClassLoader();
            Properties configProps = new Properties();
            InputStream in = null;
 
            in = cl.getResourceAsStream("BirtConfig.properties");
            configProps.load(in);
            in.close();
 
            String logLevel = configProps.getProperty("logLevel");
            if ("SEVERE".equalsIgnoreCase(logLevel)) {
                level = Level.SEVERE;
            } else if ("WARNING".equalsIgnoreCase(logLevel)) {
                level = Level.WARNING;
            } else if ("INFO".equalsIgnoreCase(logLevel)) {
                level = Level.INFO;
            } else if ("CONFIG".equalsIgnoreCase(logLevel)) {
                level = Level.CONFIG;
            } else if ("FINE".equalsIgnoreCase(logLevel)) {
                level = Level.FINE;
            } else if ("FINER".equalsIgnoreCase(logLevel)) {
                level = Level.FINER;
            } else if ("FINEST".equalsIgnoreCase(logLevel)) {
                level = Level.FINEST;
            } else if ("ALL".equalsIgnoreCase(logLevel)) {
                level = Level.ALL;
            }
            logDirectory = configProps.getProperty("logDirectory");
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        }
 
    }
 

WebReportActionBean.java
Actions related to the reports generation.

 

package com.samaxes.webreport.presentation.action;
 
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.StreamingResolution;
 
import org.eclipse.birt.report.engine.api.EngineConstants;
import org.eclipse.birt.report.engine.api.EngineException;
import org.eclipse.birt.report.engine.api.HTMLRenderOption;
import org.eclipse.birt.report.engine.api.HTMLServerImageHandler;
import org.eclipse.birt.report.engine.api.IHTMLRenderOption;
import org.eclipse.birt.report.engine.api.IPDFRenderOption;
import org.eclipse.birt.report.engine.api.IRenderOption;
import org.eclipse.birt.report.engine.api.IReportEngine;
import org.eclipse.birt.report.engine.api.IReportRunnable;
import org.eclipse.birt.report.engine.api.IRunAndRenderTask;
import org.eclipse.birt.report.engine.api.PDFRenderOption;
import org.eclipse.birt.report.engine.api.RenderOption;
import org.eclipse.birt.report.model.api.DesignElementHandle;
import org.eclipse.birt.report.model.api.activity.SemanticException;
 
import com.samaxes.webreport.entities.WebReportEntity;
import com.samaxes.webreport.presentation.BirtEngine;
 
/**
* Actions related to the reports generation.
*
* @author Samuel Santos
* @version $Revision$
*/
public class WebReportActionBean implements ActionBean {
 
   private ActionBeanContext context;
 
   public ActionBeanContext getContext() {
       return context;
   }
 
   public void setContext(ActionBeanContext context) {
       this.context = context;
   }
 
   /**
    * Generates the html report.
    *
    * @return forward to the jsp page
    * @throws EngineException when opening report design or reunning the report
    * @throws SemanticException when changing properties of DesignElementHandle
    */
   @DefaultHandler
   public Resolution htmlReport() throws EngineException, SemanticException {
       ByteArrayOutputStream reportOutput = new ByteArrayOutputStream();
 
       // set output options
       IHTMLRenderOption options = new HTMLRenderOption();
       options.setOutputFormat(HTMLRenderOption.OUTPUT_FORMAT_HTML);
       options.setOutputStream(reportOutput);
       options.setEmbeddable(true);
       options.setBaseImageURL(context.getRequest().getContextPath() + "/images");
       options.setImageDirectory(context.getServletContext().getRealPath("/images"));
       options.setImageHandler(new HTMLServerImageHandler());
       options.setMasterPageContent(false);
 
       generateReport(options);
 
       return new StreamingResolution("text/html", new ByteArrayInputStream(reportOutput.toByteArray()));
   }
 
   /**
    * Generates the PDF report.
    *
    * @return PDF file with the report output
    * @throws EngineException when opening report design or reunning the report
    * @throws SemanticException when changing properties of DesignElementHandle
    */
   public Resolution pdfReport() throws EngineException, SemanticException {
       ByteArrayOutputStream reportOutput = new ByteArrayOutputStream();
 
       // set output options
       IPDFRenderOption options = new PDFRenderOption();
       options.setOutputFormat(PDFRenderOption.OUTPUT_FORMAT_PDF);
       options.setOutputStream(reportOutput);
 
       generateReport(options);
 
       return new StreamingResolution("application/pdf", new ByteArrayInputStream(reportOutput.toByteArray()))
               .setFilename("WebReport.pdf");
   }
 
   /**
    * Generates the Excel report.
    *
    * @return Excel file with the report output
    * @throws EngineException when opening report design or reunning the report
    * @throws SemanticException when changing properties of DesignElementHandle
    */
   public Resolution xlsReport() throws EngineException, SemanticException {
       ByteArrayOutputStream reportOutput = new ByteArrayOutputStream();
 
       // set output options
       IRenderOption options = new RenderOption();
       options.setOutputFormat("xls");
       options.setOutputStream(reportOutput);
 
       generateReport(options);
 
       return new StreamingResolution("application/vnd.ms-excel", new ByteArrayInputStream(reportOutput.toByteArray()))
               .setFilename("WebReport.xls");
   }
 
   /**
    * Generates the Word report.
    *
    * @return Excel file with the report output
    * @throws EngineException when opening report design or reunning the report
    * @throws SemanticException when changing properties of DesignElementHandle
    */
   public Resolution docReport() throws EngineException, SemanticException {
       ByteArrayOutputStream reportOutput = new ByteArrayOutputStream();
 
       // set output options
       IRenderOption options = new RenderOption();
       options.setOutputFormat("doc");
       options.setOutputStream(reportOutput);
 
       generateReport(options);
 
       return new StreamingResolution("application/vnd.ms-word", new ByteArrayInputStream(reportOutput.toByteArray()))
               .setFilename("WebReport.doc");
   }
 
   /**
    * Generates the Powerpoint report.
    *
    * @return Excel file with the report output
    * @throws EngineException when opening report design or reunning the report
    * @throws SemanticException when changing properties of DesignElementHandle
    */
   public Resolution pptReport() throws EngineException, SemanticException {
       ByteArrayOutputStream reportOutput = new ByteArrayOutputStream();
 
       // set output options
       IRenderOption options = new RenderOption();
       options.setOutputFormat("ppt");
       options.setOutputStream(reportOutput);
 
       generateReport(options);
 
       return new StreamingResolution("application/vnd.ms-powerpoint", new ByteArrayInputStream(reportOutput
               .toByteArray())).setFilename("WebReport.ppt");
   }
 
   /**
    * Generates the Postscript report.
    *
    * @return Excel file with the report output
    * @throws EngineException when opening report design or reunning the report
    * @throws SemanticException when changing properties of DesignElementHandle
    */
   public Resolution psReport() throws EngineException, SemanticException {
       ByteArrayOutputStream reportOutput = new ByteArrayOutputStream();
 
       // set output options
       IRenderOption options = new RenderOption();
       options.setOutputFormat("postscript");
       options.setOutputStream(reportOutput);
 
       generateReport(options);
 
       return new StreamingResolution("application/postscript", new ByteArrayInputStream(reportOutput.toByteArray()))
               .setFilename("WebReport.ps");
   }
 
   /**
    * Generates the report output.
    *
    * @return the report output
    * @throws EngineException when opening report design or reunning the report
    * @throws SemanticException when changing properties of DesignElementHandle
    */
   private void generateReport(IRenderOption options) throws EngineException, SemanticException {
       // this list simulates a call to the application business logic
       List<webReportEntity> webReportEntities = new ArrayList<webReportEntity>();
       webReportEntities.add(new WebReportEntity(new Double(2), "Product1"));
       webReportEntities.add(new WebReportEntity(new Double(4), "Product2"));
       webReportEntities.add(new WebReportEntity(new Double(7), "Product3"));
 
       // get the engine
       IReportEngine birtReportEngine = BirtEngine.getBirtEngine();
 
       // open the report design
       IReportRunnable design = birtReportEngine.openReportDesign(context.getServletContext().getRealPath("/reports")
               + "/webReport.rptdesign");
 
       // create task to run and render report
       IRunAndRenderTask task = birtReportEngine.createRunAndRenderTask(design);
       @SuppressWarnings("unchecked")
       Map<string, Object> appContext = task.getAppContext();
       DesignElementHandle reportChart = task.getReportRunnable().getDesignHandle().getModuleHandle().findElement(
               "reportChart");
 
       appContext.put(EngineConstants.APPCONTEXT_CLASSLOADER_KEY, WebReportActionBean.class.getClassLoader());
       appContext.put("webReportEntities", webReportEntities);
       reportChart.setProperty("dataSet", "Scripted Data Set"); // change the chart dataset
 
       task.setLocale(context.getLocale());
       task.setRenderOption(options);
       task.setAppContext(appContext);
 
       // run report
       task.run();
       task.close();
   }
}
 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

    birt IE8-IE9兼容

    1、 webcontent/birt/layout文件里面 具体路径为:webcontent/birt/pages/layout下 分别在这FramesetFragment.jsp 、RequesterFragment.jsp、RunFragment.jsp 3个文件的头部里面也就是&lt;head&gt;加上 &lt;META ...

    birt 项目 配置

    birt-runtime 2.5 网址:http://download.eclipse.org/birt/downloads/ 下载后解压,将birt-runtime-2_5_1\WebViewerExample\WEB-INF中的lib,platform两个文件夹copy到eclipse中的相应的两个文件夹中..能过eclipse发布...

    Birt 4.2 学习手册

    最近在公司内部进行Birt 4.2的培训,着手准备的一份文档。只适合 新手学习用。文档分为,环境搭建,组件学习,脚本编写。

    BIRT4.x 客户端汉化文件

    1:去/WebViewerExample/WEB-INF/lib下找到名为“viewservlets.jar”的jar包,使用压缩工具打开(不用解压),进入/org/eclipse/birt/report/resource目录,这里会发现一个Messages.properties文件,这个就是birt...

    birt-report-framework插件

    eclipse birt 开发插件birt-report-framework-4_4_1-20140916.zip

    birt-report-framework

    报表插件报表插件报表插件报表插件报表插件报表插件报表插件报表插件报表插件

    birt 日历控件已经实现 。资源包括:Calendar.js ,具体实现步骤:操作步骤.txt 。

    &lt;script src="birt/ajax/utility/Calendar.js" type="text/javascript"&gt;&lt;/script&gt; 3、修改TextBoxParameterFragment.jsp 在textbox中加入onclick事件 (encodedParameterName.indexOf("Time")&gt;=0) {%&gt; onclick=...

    birt-report-framework-sdk-4_4_2.part1

    eclipse开发报表的插件birt 只是一个插件,安装方法和其它eclipse插件一样

    birt日历控件

    &lt;script src="birt/ajax/core/BirtSoapResponse.js" type="text/javascript"&gt;&lt;/script&gt;, 再下面加上 &lt;script src="&lt;%= baseHref %&gt;/ajax/utility/My97DatePicker/WdatePicker.js" type="text/javascript"&gt;&lt;/script&gt;...

    eclipse birt chart engine example resource code

    eclipse birt chart engine resource code

    BIRT framework

    BIRT framework ppt.This presentation is created to introduce BIRT framework and architecture. It is broken into four units introducing BIRT component, BIRT designer installation, basics, data sources,...

    birt-report-framework-sdk-2_6_2.zip

    birt-report-framework-sdk-2_6_2.zip

    BIRT开发手册

    BIRT(Business Intelligence and Reporting Tools), 是为 Web 应用程序开发的基于 Eclipse 的开源报表系统,特别之处在于它是以 Java 和 JavaEE 为基础。BIRT 有两个主要组件:基于 Eclipse 的报表设计器,以及部署...

    birt-report-framework-2_2_2.zip 下载

    1.将features文件夹里面的所有文件复制,粘贴到eclipse的解压安装文件夹下的features文件夹下,这里我的是:D:\javawork\javasoft\eclipse 3.7.0\eclipse\features 2.将plugins文件夹下面的所有文件复制,粘贴到...

    birt五本奇书(全英文的,讲的比较透彻)

    如果你想深入学习的话,...【BIRT资料】EclipseCon2005_Introducting_the_BIRT_Project.ppt 其他资料 BIRT教程大全 http://download.csdn.net/source/1583781 BIRT升级脚本 http://download.csdn.net/source/1497457

    birt-jsp-tag

    birt-jsp-tag,birt标签

    Birt报表注意Birt报表注意Birt报表注意Birt报表注意

    Birt报表注意Birt报表注意Birt报表注意Birt报表注意Birt报表注意Birt报表注意Birt报表注意Birt报表注意Birt报表注意

    birt-runtime2.3.1/2.3.2汉化包

    本资源为birt官网所提供的birt运行时的汉化语言包,为方便大家使用,传到csdn上,如有需要的朋友请不要错过

    Eclipse Birt源码4.5.0

    Birt4.5.0 的源码,全部为JAR格式,导入Eclipse请参考文章 http://blog.csdn.net/supercooly/article/details/49357709

Global site tag (gtag.js) - Google Analytics