Embedding reports within JBuilder web applications projects Overview Contents With the recent addition of embedded reporting to the JBuilderX feature-set, it is now possible to process and render industry-standard report templates within your JavaServer Pages (JSPs). This article discusses the requirement for embedded reporting and highlights generic requirements for reporting infrastructure. It also demonstrates how to add existing report templates into a JBuilder project and deploy those reports to Apache Tomcat. Future articles in this series will discuss the process of designing new reports against JDBC/JNDI data sources as well as configuring data access for staging and deployment scenarios. HAND-CODING REPORTS...2 AN EMBEDDING REPORTING DESIGN MODEL...2 AN MVC PATTERN FOR EMBEDDED REPORTING...3 EMBEDDING A CRYSTAL REPORT INTO A JSP PAGE...4 Step 1 Create the EmbeddedReportingDemo project... 4 Step 2 Add the Crystal Viewer to your JSP page... 4 Step 3 Customize the viewer... 5 Step 4 Compile your application to deploy to Apache Tomcat... 6 Step 5 Let end-users interact with it... 7 ADDITIONAL RESOURCES...7 Copyright 2003 Crystal Decisions, Inc. All Rights Reserved. Page 1
Hand-coding reports An independent study of professional Java developers 1 conducted recently indicates that 47% of Java developers are hand-coding the reporting aspect of their applications. An additional 11% realize that it is inefficient to manually produce reports from scratch on a project-by-project basis, and have developed their own reporting frameworks in-house. The survey also reveals that Java developers are spending up to one-third of their time developing and maintaining these reports to meet ill-defined and ever-changing end-user requirements for information. The success of any custom application development project is measured by the degree to which the application is adopted by an end-user community. Success is usually determined in no small measure by the ease with which users can extract useful information from the application. Reporting is therefore an integral part of the success of most custom application development initiatives. In practice, developing reports for end-users is a code-intensive process that should be incorporated into every project plan, if the development team chooses to implement the reporting infrastructure in-house. However, embedding reporting within a custom application is too often left to the latter stages of a project, an afterthought that contributes to project slippage. Irrespective of whether development teams choose to build or buy embedded reporting functionality, end-user reporting requirements and associated pre- and postimplementation costs should first be assessed. An embedding reporting design model There are a recurring set of common reporting requirements that developers and architects encounter in every custom application development project. It is therefore possible to develop a reporting framework that meets these requirements and can be reused across multiple projects. The set of common reporting requirements and best-practices applied to this problem constitute a reporting design pattern that development teams can leverage to mitigate the risk of inadequate presentation of application data. An embedded reporting framework must provide support for: Rapid, iterative, design-time prototyping of embedded reports between report authors and application end-users Repurposing the information contained in reports to a wide variety of viewing formats online, printed/paginated, as well as popular electronic file formats without refactoring the report for each format Decoupling reporting infrastructure from core application to improve maintainability and reduce QA costs Thick-client presentation quality and interactivity in a zero-client footprint Parameterization of data to contextualize result-sets With the most recent release of JBuilder, Borland has recognized the role of embedded reporting in the application development lifecycle, and is shipping reporting functionality in-the-box through a custom edition of Crystal Reports. An alternative to hand-coding the reporting aspect of a J2EE application or developing an in-house reporting framework, Crystal Reports for Borland 1 TRG Research, N=465 Professional Java Developers, July 03 Copyright 2003 Crystal Decisions, Inc. All Rights Reserved. Page 2
JBuilder accelerates the process of accessing, formatting and integrating application data within JSPs. An MVC Pattern for Embedded Reporting Crystal Reports for JBuilder represents an MVC design pattern which relates the user interface to the underlying data model. This pattern allows for a significant reduction in the time required to develop the reporting aspect of custom applications. The report template file is a proxy for the underlying data model. The view is the Crystal Viewer, which represents all the visible elements of the browser-based user interface, including the event model supported by the Crystal Viewer. The controller is the Java Reporting Component, which connects the model and the view and enables communication between these two components. For the purpose of this article, we have created a JBuilder project, EmbeddedReportingDemo.jpx which contains a Web Module, EmbeddedReportingModule. EmbeddedReportingModule contains a JBuildergenerated WorldSalesReport.jsp. We will launch a single instance of the Crystal Report Viewer, SalesReportPane, from within this JSP. The Project also includes a new JBuilder native object, a Crystal Report template. As the diagram below outlines, the Crystal Reports Java Reporting Component bundled with JBuilder uses the report metadata stored in the WorldSalesReport.rpt template to connect to the application s JDBC data source and produce the desired data resultset. The formatted resultset is then passed to the instance of the Crystal Report Viewer created by WorldSalesReport.jsp, which pushes the formatted data, in HTML form, to the browser client. The Crystal Viewer supports a rich event-model that allows the browser client to drill down on grouped report data, as well as print the report locally, or export the report for redistribution in a variety of popular electronic file formats. The sole purpose of the EmbeddedReportingDemo project is to provide a working example of the Crystal Report Viewer processing and rendering a single report template file (.RPT) from within a JSP page deployed to the Apache Tomcat JSP container. WorldSalesReport.jsp SalesReportPane Instance of Crystal Viewer Java Reporting Component WorldSalesReport.rpt JDBC/JNDI EmbeddedReportingModule (WAR) Apache Tomcat Copyright 2003 Crystal Decisions, Inc. All Rights Reserved. Page 3
NOTE Report templates are generated using the Crystal Reports for Borland JBuilder desktop report designer, which can be downloaded directly from within the JBuilder IDE. Embedding a Crystal report into a JSP page Step 1 Create the EmbeddedReportingDemo project To create the project, from the File menu, select New Project. Name the project EmbeddedReportingDemo. Select Finish. To create a web module within this project, from the File menu, select New / Web Module (WAR). Select Next. Name the web module EmbeddedReportingModule. Select Next. To embed the custom JSP viewer tags we will be using in this example, select the Crystal Reports Engine 10 (Crystal Reports Viewer Tags 10). This will include the crystalreportviewers10 subdirectory under the Module Directory node, and the CrystalReportEngine-config.xml file in your /WEB-INF/classes subdirectory. It will also add the crystal-tags-reportviewer.tld tag library definition within your WEB-INF subdirectory. Select Finish. To create a JSP page within your web module, from the File menu, select New / JavaServer Page. Name the page WorldSalesReport. Select Next. Select the Crystal Reports Viewer Tags 10 checkbox. This will add the tag library declaration to your JSP page. Select Finish. Step 2 Add the Crystal Viewer to your JSP page Now you need to add the Crystal Viewer to your JSP page. Place the cursor on the location within the JSP structure where you want to insert the viewer. From the Wizards menu, select Insert Crystal Reports Viewer. Type SalesReportPane for the name of this viewer instance. Browse for the World Sales Report.rpt Copyright 2003 Crystal Decisions, Inc. All Rights Reserved. Page 4
report in the Existing File field. This report is available in the Crystal Reports for Borland JBuilder desktop report designer, at c:/program Files/Crystal Decisions/Crystal Reports 9/Samples/En/Reports/General Business. Select this.rpt file. Select Next. JBuilder needs to know where, within your project structure, you would like to save a copy of the report template. Select WEB- INF. Select OK. The report will be copied into your project structure at /Documents and Settings/<youraccountname>/jbproject/EmbeddedReportingDemo/EmbeddedRe portingmodule/web-inf. Step 3 Customize the viewer The next dialog box allows you to customize the appearance and behavior of the viewer. The values displayed are the defaults; therefore if you do not explicitly set a value, the attribute is not set in the inserted code. For this example, we have not customized the Crystal Viewer. NOTE A Custom JSP Tag Library is included. To access it, simply press and hold CTRL space Copyright 2003 Crystal Decisions, Inc. All Rights Reserved. Page 5
Step 4 Compile your application to deploy to Apache Tomcat Select Run / Run Project. JBuilder will build the project and deploy it to Apache Tomcat. The World Sales Report will be processed by the Java Reporting Component. The Crystal Viewer instance will then render the report to HTML, within JBuilder s built-in browser interface. If you would like to see what the report will look out within the context of another browser, copy the URL from JBuilder s internal browser URL field and copy it into your favorite browser. Note that you can interact with the report by drilling down several levels into the underlying data. You can also print the report locally, or export it to a variety of electronic file formats. Copyright 2003 Crystal Decisions, Inc. All Rights Reserved. Page 6
Step 5 Let end-users interact with it. It s as simple as this. If you have existing Crystal reports you can now use them in JBuilder X. If you don t, you can try out the intuitive designer. As demonstrated above, only a few simple steps are required to render the report within your JSP page. And, if end-user requirements ever change, the reports can be easily updated without impacting your core application business logic. The World Sales Report is illustrated below, as it appears in the report designer preview view. Additional Resources For additional information on Crystal Reports for Borland JBuilder please consult the integrated JBuilder online documentation or visit the Crystal Decisions JavaZone developer community at: http://www.crystaldecisions.com/javazone. Copyright 2003 Crystal Decisions, Inc. All Rights Reserved. Page 7