Adding Jasper Report Templates#
Jasper Report Template Design Overview#
Designing and deploying report templates for Allegra comprises the following steps:
Download an existing template similar to the one you like to create from the Allegra server.
Download some sample report data from the Allegra server to your local disk in XML format.
Start JasperSoft Studio and point the data source to the sample data you have just downloaded.
Interactively design and test your new template locally, using the sample data as test input. Repeat until you are satisfied with the result.
Put all report template files plus some descriptive files into a single ZIP file and upload it to the Allegra server.
You can skip the first step if you want to start from scratch. Of course, you can skip the last step if you are satisfied with creating only local reports. In this case, your report template cannot be run directly on the server, and will not be available to others.
Installing JasperSoft Studio#
JasperSoft Studio is freely available as a ZIP file for non-Windows-based systems, and as an installer executable for Microsoft Windows-based systems.
You must obtain a version that is compatible with the library version used by Allegra.
The library version is displayed under the Administration > Server administration > Server status.
Compatible means that it should not be much newer than the Allegra library version as it then may generate
code that cannot be handled anymore by the older library. It should also have the same major version:
Allegra 9.1 uses JasperReports 7.0, and its templates use a file format that JasperSoft Studio
versions before 7.0 cannot open.
Step 1: Downloading Existing Template from Server#
Usually, it is easier to create a new template from an existing one, rather than start from scratch.
Allegra comes with some ready-to-use report templates.
Log in and go to Reports > Public.
From the list of available templates choose one and download it to your local computer by clicking on the
Download entry in the context menu.
The file you have downloaded is a ZIP-file. To use it you need to unpack it using a ZIP utility like 7-Zip or the Windows unzip utility. Once you have unzipped the file there will be several files as shown below (here we have unzipped the existing template “Concise report, no history”).
We will take a look at the purpose of each of these files a little later on.
Step 2: Downloading Sample Data#
It is very convenient to test your reports during the design process using sample data. Typically, you will in a rapid fashion repeatedly make changes to your template design, compile the template, and run the report to see what the result looks like.
To make sure that your report template matches the data format provided by the Allegra server, you download an XML file from a query you executed on the server. If your report template runs to your satisfaction on your local computer, you can be sure that it will also run with no problems on the Allegra server later on.
To create a sample data set, run the report using a filter. Click on “Create report”, then choose a filter. Check that the data set you get is representative, and should not be too small or too large. It should be large enough to create at least two pages of output, and should not be too large to not slow down the rapid development cycle.
Check that you get a suitable result set with this filter by clicking on “Create report” at the bottom of the filter configuration pop-up window.
Then create the XML data set by repeating the procedure and saving the result to an XML file by clicking on “XML data source” button at the bottom of the filter configuration pop-up window.
Step 3: Creating the JasperReports Project#
To develop your template you have to create a JasperSoft Studio project. Open JasperSoft Studio.
Go to File > New > Project.
On the next screen select “JasperReports Project” and click “Next”. Give a name to the project and save it.
You have now a basic project structure.
Add a new source folder named src by using the context menu, New > Folder.
Use the context menu on your new project in the left navigation area to modify some project properties.
Add the src folder you just created to your Java build path.
Copy the content of the expanded ZIP file you downloaded previously into your src folder. Your project should now look
like shown here.
Step 4: Configuring the Data Adapter#
It is convenient to test your reports during the design process using sample data. To test the report templates with real data you have to provide a data source.
JasperSoft Studio supports a variety of data adapters, but only the XML adapter is useful if you are designing templates for the Allegra server. Switch to the Repository Explorer view and click on the icon marked below to define a new data adapter.
Select the XML document data adapter.
A new form appears that permits editing the properties of your new XML data adapter. Enter the values as shown below. For the XML file, you enter the path for the file that you had previously downloaded from your Allegra server.
Test your adapter and click on “Finish”.
You can now test if everything has been set up correctly. Double click on the .jrxml
file in your project, like ReportStandard.jrxml. In the design window, you will
see the corresponding template.
Switch to the “Preview mode” at the bottom of the design area. At the top select the data adapter you have just defined. Then click on the little green arrow next to the data adapter selection list.
You will see the report with your sample data just as it would appear on the server. You can try different output formats. The respective programs like browser, Word, or Excel you need to configure for your JasperSoft Studio installation.
Step 5: Designing the Report Template#
It is most efficient to start with an existing template when going about designing a new one.
To start the design process, double-click on the jrxml file you want to take as a start.
On the screen, a view similar to that shown below appears. Reports are made up of “bands”. The document structure, showing these bands can be seen in the window on the lower left named “Outline”. For example, there is a title band, a page header band, a page footer band, and a last page footer band which can be conveniently used to print sums.
The actual report data is placed into the detail band. When running the report, the detail band is generated for each item.
Example 1: Changing the Report Logo#
As a first example let us try to change the logo that appears at the top right of the first report sheet. You can see that the logo in the template is placed in the “title” band.
Click on the image and go to the “Properties” windows on the lower right.
Go to the “Image” section. Here you see the expression that defines which image is being included.
The standard templates use the parameter $P{LOGO_URL}. When the report runs on the server, Allegra
fills this parameter with the company logo configured for your Allegra system. If you change the
company logo there, all reports using this parameter show the new logo, and you do not need to
modify the template. In the preview of JasperSoft Studio this parameter is empty, so no logo is shown.
If you want this template to use a different image, place the image file into the template
directory and change the expression to $P{BASE_URL} + "mylogo.png". The parameter
$P{BASE_URL} is required so that later on the system can find the image file in the
template directory on the server.
The image element in our example has a size of 96 x 24 pixels, which you can see on the “Appearance” tab. With the setting “Retain Shape” the image is scaled to fit into this area without being distorted. You can scale the element to the size you like. It is recommended to use JPEG or PNG-encoded files for images.
Once you have changed your template save it to disk (menu File > Save). Then switch to the preview tab.
If you have made any errors, you will get error messages in the bottom window.
After a little while the report shows up in the preview area, just as it would appear on the server.
If this was all you wanted to do, proceed to Step 6 to deploy the modified report template on the server.
Example 2: Inserting a Custom Field#
The report engine itself executes a query on the report data provided to it before assembling the report. In the case of an XML data source, as it is being used by Allegra, the query is expressed in terms of an XPath path expression. The leaf nodes of the resulting data set are mapped to JasperSoft Studio “fields” which then can be used in the report template.
To map fields in the XML data set coming from the Allegra server to JasperSoft Studio fields, right-click on the topmost node of the “Outline” window in the lower left area, which carries the name of your report. In the context menu select “Dataset and Query”.
You see the XPath expression of the current template and the result set of this query.
In the illustration, we have selected a node labeled “IssueNo”. If you look at the bottom section of the same window you see a table where this field “IssueNo” is mapped to a template field named “IssueNumber”. Furthermore, it is defined that this field is of type String.
You can add your own fields to your report template by dragging them from the result node area to the
fields table at the bottom of the window. From then on, you can refer to these fields using the
placeholder $F{yourfieldname}, for example $F{IssueNumber}.
Step 6: Deploying Report Templates on the Server#
Once you are satisfied with your report template design you can deploy the template on the Allegra Server. This enables you and possibly others to have a nicely designed report created directly from a report overview on the server, without having to download and configure anything locally.
Before you publish your new template to the server, make sure that all required files are in one directory. Please pay attention to the following:
Most important, you have to have the compiled report template file in the template directory. The compiled report template files have the ending
jasper. We have set up our project such that they appear in thesrcdirectory.Make sure you have a preview image of your report with some sample data, at most 800 x 600 pixels, in PNG or GIF format. For convenience, call it
preview.png. The file name must match thepreview-gifentry indescription.xml. This enables the users of the Allegra system to have a look at how the report would look without actually executing it.Check that the proper texts are shown in file
description.xml.
Now place all files in the src
directory into a ZIP file. Make sure that all files are at the topmost level of the ZIP file
and that there is no directory at the top level. You need to ZIP the files, not the directory.
After you have created a ZIP file with the structure as shown above, log in to the server. Go to Reports and upload your report template.
Advanced Preprocessing with XSLT Style Sheets#
The Allegra XML data file format may not be suited for a particular report idea that you have. For example, the standard data structure does not support to print a pie chart on the number of issues assigned to each responsible, or other similar presentations relying on summarized data.
Nonetheless, you can create reports like that by preprocessing the original XML data file using an XSLT style sheet and a style sheet processor. Once you have tested your style sheet and report, you include the style sheet into the report zip file, add its name to the report description file, and when you execute the report on the server, it will use the same style sheet before it creates the report!
There is an example in the JasperSoft Studio distribution that uses a style sheet to create a pie chart report. In order to test and execute your style sheet you have to install a style sheet processor on your development computer. The Allegra server uses the Xalan processor which is freely available, so it may be best to use that as well on your client. We will refer to this processor in the following.
The procedure to create reports based on your own data structures is as follows:
Download some sample report data from the Allegra server to your local disk in XML format.
Develop your style sheet and create a new XML data file from the original XML data file, using the XSLT processor.
Start JasperSoft Studio and point the data source to the sample data you have just created using your style sheet and the style sheet processor.
Interactively design and test your new template locally, using the sample data as test input. Repeat until you are satisfied with the result.
Put all report template files plus some descriptive files and your style sheet into a single ZIP file and upload it to the Allegra server.
This process is the same as outlined before for reports without data preprocessing, except for the additional step of creating your own XML data file and pointing your data source towards it. Furthermore, you have to add your style sheet to the report package that you upload to the server.
You can run the Xalan style sheet processor from the command line like this:
java -classpath <yourPathTo xalan.jar> \
org.apache.xalan.xslt.Process -in xmlSource \
-xsl stylesheet -out outputfiles
For example, if you have installed the Xalan jar file (xalan.jar) in C:Program FilesXalan, and you use the original exported file from Allegra, you would enter:
java -classpath C:\Program Files\Xalan\xalan.jar \
org.apache.xalan.xslt.Process -in TrackReport.xml \
-xsl chart.xsl -out TrackChart.xml