Seam (BIRT) Recipe

From Eclipsepedia

Jump to: navigation, search

Contents

Set up environment

Get started with Seam

#Generated by seam setup
#Sun May 11 07:49:04 CEST 2008
hibernate.connection.password=
workspace.home=/home/user/projects
model.package=org.seambirt.sample.entity
driver.jar=lib/hsqldb.jar
action.package=org.seambirt.sample
test.package=org.seambirt.sample.test
database.type=hsql
richfaces.skin=deepMarine
hibernate.default_catalog.null=
hibernate.default_schema.null=
database.drop=n
project.name=seambirt
hibernate.connection.username=sa
hibernate.connection.driver_class=org.hsqldb.jdbcDriver
hibernate.cache.provider_class=org.hibernate.cache.HashtableCacheProvider
project.type=ear
icefaces.home=
database.exists=n
jboss.home=/opt/jboss-4.2.2.GA
hibernate.dialect=org.hibernate.dialect.HSQLDialect
hibernate.connection.url=jdbc\:hsqldb\:.
  • Generate new entity ($ seam new-entity). I named it SampleReport.
  • [OPTIONAL] Include Seam project into your IDE. Eclipse Users: Add the project

into Eclipse using File > New > Project and select General > Project (not Java Project). NetBeans Users: Open the project in NetBeans.

Get started with BIRT

cp $BIRT_RUNTIME/WebViewerExample/WEB-INF/lib/com.ibm.icu_3.6.1.v20070906.jar $PROJECT/lib
cp $BIRT_RUNTIME/WebViewerExample/WEB-INF/lib/commons-cli-1.0.jar $PROJECT/lib
cp $BIRT_RUNTIME/WebViewerExample/WEB-INF/lib/coreapi.jar $PROJECT/lib
cp $BIRT_RUNTIME/WebViewerExample/WEB-INF/lib/dataadapterapi.jar $PROJECT/lib
cp $BIRT_RUNTIME/WebViewerExample/WEB-INF/lib/dteapi.jar $PROJECT/lib
cp $BIRT_RUNTIME/WebViewerExample/WEB-INF/lib/engineapi.jar $PROJECT/lib
cp $BIRT_RUNTIME/WebViewerExample/WEB-INF/lib/flute.jar $PROJECT/lib
cp $BIRT_RUNTIME/WebViewerExample/WEB-INF/lib/chartengineapi.jar $PROJECT/lib
cp $BIRT_RUNTIME/WebViewerExample/WEB-INF/lib/js.jar $PROJECT/lib
cp $BIRT_RUNTIME/WebViewerExample/WEB-INF/lib/modelapi.jar $PROJECT/lib
cp $BIRT_RUNTIME/WebViewerExample/WEB-INF/lib/modelodaapi.jar $PROJECT/lib
cp $BIRT_RUNTIME/WebViewerExample/WEB-INF/lib/odadesignapi.jar $PROJECT/lib
cp $BIRT_RUNTIME/WebViewerExample/WEB-INF/lib/org.apache.commons.codec_1.3.0.v200706111738.jar $PROJECT/lib
cp $BIRT_RUNTIME/WebViewerExample/WEB-INF/lib/org.eclipse.emf.common_2.2.1.v200702131851.jar $PROJECT/lib
cp $BIRT_RUNTIME/WebViewerExample/WEB-INF/lib/org.eclipse.emf.ecore.xmi_2.2.2.v200702131851.jar $PROJECT/lib
cp $BIRT_RUNTIME/WebViewerExample/WEB-INF/lib/org.eclipse.emf.ecore_2.2.2.v200702131851.jar $PROJECT/lib
cp $BIRT_RUNTIME/WebViewerExample/WEB-INF/lib/org.w3c.css.sac_1.3.0.v200706111724.jar $PROJECT/lib
cp $BIRT_RUNTIME/WebViewerExample/WEB-INF/lib/scriptapi.jar $PROJECT/lib
  • Copy dir (include subdirs) from $BIRT_RUNTIME/WebViewerExample/WEB-INF/platform to $PROJECT/resources/WEB-INF/platform
cp -r $BIRT_RUNTIME/WebViewerExample/WEB-INF/platform $PROJECT/resources/WEB-INF/platform

Integrate

Change project defaults

  • Change ant script $PROJECT/build.xml
    • Add property
<property name="birt.platform.dir" value="resources/WEB-INF/platform" />
    • Change war target:
...
<target name="war" depends="compile" description="Build the distribution .war file">             
...
    <copy todir="${war.dir}/WEB-INF/lib">
                        <fileset dir="${lib.dir}">
                                <include name="richfaces-impl*.jar" />
                                <include name="richfaces-ui*.jar" />
                                <include name="oscache*.jar" />
                                <include name="commons-digester.jar" />
                                <include name="commons-beanutils.jar" />
                                <include name="jsf-facelets.jar" />
                                <include name="jboss-seam-*.jar" />
                                <exclude name="jboss-seam-gen.jar" />
                                <!-- BIRT -->
                                <include name="seam-birt.jar" />
                                <include name="com.ibm.icu_3.6.1.v20070906.jar" />
                                <include name="commons-cli-1.0.jar" />
                                <include name="coreapi.jar" />
                                <include name="dataadapterapi.jar" />
                                <include name="dteapi.jar" />
                                <include name="engineapi.jar" />
                                <include name="flute.jar" />
                                <include name="chartengineapi.jar" />
                                <include name="js.jar" />
                                <include name="modelapi.jar" />
                                <include name="modelodaapi.jar" />
                                <include name="odadesignapi.jar" />
                                <include name="org.apache.commons.codec_1.3.0.v200706111738.jar" />
                                <include name="org.eclipse.emf.common_2.2.1.v200702131851.jar" />
                                <include name="org.eclipse.emf.ecore.xmi_2.2.2.v200702131851.jar" />
                                <include name="org.eclipse.emf.ecore_2.2.2.v200702131851.jar" />
                                <include name="org.w3c.css.sac_1.3.0.v200706111724.jar" />
                                <include name="scriptapi.jar" />
                        </fileset>
                </copy>
                <copy todir="${war.dir}/WEB-INF/platform/">
                        <fileset dir="${birt.platform.dir}" />
                </copy>

build.xml all in one: Seam (BIRT) Recipe - $PROJECT/build.xml

Code Seam-BIRT servlet

  • Make two new classes in the project (e.g. in $PROJECT/src/action dir)
package org.jboss.seam.pdf.birt;
 
import java.io.IOException;
 
public class DocumentStoreServlet extends HttpServlet {
	protected IReportEngine engine;
	protected IFragment requester;
 
	private static final long serialVersionUID = 519600274155718202L;
 
	public void init(ServletConfig servletconfig) throws ServletException {
		super.init(servletconfig);
		BirtEngine.initBirtConfig();
	}
 
	@Override
	protected void doGet(final HttpServletRequest request,
			final HttpServletResponse response) throws ServletException,
			IOException {
 
		new ContextualHttpServletRequest(request) {
			@Override
			public void process() throws ServletException, IOException {
				try {
					executeReport( request, response);
				} catch (EngineException e) {
					log("executeReport error", e);
				}
 
			}
		}.run();
 
	}
 
 
	void executeReport(HttpServletRequest request, HttpServletResponse response) throws EngineException {
 
        //resp.setContentType("text/html");
        response.setContentType( "application/pdf" ); 
        response.setHeader ("Content-Disposition","inline; filename=test.pdf");           
        String reportName = request.getServletPath();
        ServletContext sc = request.getSession().getServletContext();
        this.engine = BirtEngine.getBirtEngine(sc);
 
 
        IReportRunnable design;
        try
        {
                //Open report design
       		design = engine.openReportDesign( sc.getRealPath("./")+reportName.substring(0, reportName.length() -4) + ".xhtml" );
                //create task to run and render report
                IRunAndRenderTask task = engine.createRunAndRenderTask( design );             
                task.getAppContext().put(EngineConstants.APPCONTEXT_CLASSLOADER_KEY, DocumentStoreServlet.class.getClassLoader()); 
                //set output options
                //HTMLRenderOption options = new HTMLRenderOption();
                PDFRenderOption options = new PDFRenderOption();
                //options.setOutputFormat(HTMLRenderOption.OUTPUT_FORMAT_HTML);
                options.setOutputFormat(HTMLRenderOption.OUTPUT_FORMAT_PDF);
                options.setOutputStream(response.getOutputStream());
                //options.setBaseImageURL(req.getContextPath()+"/images");
                //options.setImageDirectory(sc.getRealPath("/images"));
                task.setRenderOption(options);
 
                //run report
                task.run();
                task.close();
        }catch (IOException e){
 
                e.printStackTrace();
                return;
        }
 
 
		System.out.println("Finished");
	}
 
	/**
	 * Destruction of the servlet. 
 
	 */
	public void destroy() {
	 super.destroy(); 
	 BirtEngine.destroyBirtEngine();
	}
}
  • Add BIRT pdf servlet to $PROJECT/resources/WEB-INF/web.xml .
....
  <!--  BIRT PDF servlet -->
   <servlet>
        <servlet-name>Document Store Servlet</servlet-name>
        <servlet-class>org.jboss.seam.pdf.birt.DocumentStoreServlet</servlet-class>
    </servlet>
 
    <servlet-mapping>
        <servlet-name>Document Store Servlet</servlet-name>
        <url-pattern>*.pdf</url-pattern>
    </servlet-mapping>
...

Make BIRT report

  • make $PROJECT/view/sample.xhtml report (rptdesign)

Instantiate Seam component by

...
            <method name="open"><![CDATA[var reports = Packages.org.jboss.seam.Component.getInstance("sampleReportList", true);
            reportsIterator = reports.getResultList().listIterator();]]></method>
...

sample.xhtml all in one: Seam (BIRT) Recipe - Sample Report

    • Add create report button to $PROJECT/view/sampleReportList.xhtml
...
<div class="actionButtons">
        <s:button id="done" 
               value="Create sampleReport"
                view="/sampleReport.xhtml"/>	
        <s:button id="report" value="Report" view="/sample.pdf?ReportName=report.rptdesign" />		  
</div>
...

Bon appetit

  • Run ant explode, start jBoss app. server
  • Cook in oven about 40 minutes & visit restaurant :-)