Libraries

Often, report development involves repetitive tasks. Things such as report headers, commonly used datasets and data sources, formatting, and other elements are used in many different reports. This is where libraries come into play. When using libraries, report developers can store commonly used components for later reuse. It is also a useful tool when working in groups.

The concepts of reuse and group development are often overlooked in report development, and this seems to be all but entirely ignored with BIRT. However, if we have worked in a report development group, it is an all too often needed functionality. When I was working in a large report development group, the data that got pulled was often the same, if not similar, to previous report requests, with modification to visual layouts, or modified parameters. Another scenario that was common was to develop reports only to find out that someone had done the same report several months ago. With BIRT's report libraries, these kinds of common tasks can be stored in a central report library and made accessible to other developers to reuse and share their components. This is even useful in single developer situations to avoid having to rebuild commonly used objects in different reports.

In BIRT, libraries are similar in structure and design to a report document. The main difference being that libraries can be referenced inside of report designs and the visual editor for libraries will be different. When there are changes to library elements, these changes will trickle down into the report designs that are using them. The only exception is with chart elements in versions of BIRT prior to 2.2.

Creating a new library

As with report designs, a library can be created using either the New option under the Navigator or the File menu. This is nice as it does not deviate from the expected behavior. In the following example, we are going to create a new Report Project called Classic Cars Library and create a new library using it.

  1. From either the Navigator or File menu, go to New | Project. Name the project Classic Cars With Library.
  2. In the Navigator, right-click on the new project, and choose New | Library. Name the library as ClassicCarsLibrary.rptlibrary.
    Creating a new library
  3. When we click on Finish, we will get a dialog telling you how to work with the visual designer for a library. Click OK.
Creating a new library

Adding components to a library

Now that we have a library, we need to add a few components to it. With the reports that we have created throughout the book, we have used a consistent data source, the Classic Cars Sample Database. This would make a good candidate for a library item.

Adding a data source to a library

In the following example, we will add a data source to a library.

  1. Open the ClassicCarsLibrary.rptlibrary, if it is not already opened.
  2. Open the Outline view.
    Adding a data source to a library
  3. Open the Palette view above the Outline pane view so that both panes are open at the same time.
    Adding a data source to a library
  4. Right-click on the Data Sources icon under the Outline and select New Data Source.
    Adding a data source to a library
  5. Select the Classic Models Inc. Samples database and name the datasource dsClassicCars. Click Next.
  6. Click Finish.

Now we have a data source in a library. This was easy to do. Most of this should be familiar as it is the same steps to add to a report design.

Adding a visual element to a library

In our reports, we will want to add in simple report headers containing a logo for the Classic Cars Company, a company title line, and a report title line. The report title line will be populated by a report parameter so that the design of the header is consistent, but the title will be dynamic.

  1. In the Outline view, right-click on the Report Parameters icon and select New Parameter.
    Adding a visual element to a library
  2. Enter the following properties:
    • Name: rprmReportTitle
    • Prompt text: Enter the Reports Title
    • Change the parameter to a hidden parameter by clicking on the Hidden checkbox.
    Adding a visual element to a library
  3. Drag a grid item from the Palette to the Report Items section under the Outline.
    Adding a visual element to a library
  4. Set the Number of columns to 2 and the Number of rows to 3.
    Adding a visual element to a library
  5. In the leftmost columns, select all three rows, right-click on the selection, and choose Merge Cells. The Merge Cells option is also available from the Element menu.
    Adding a visual element to a library
  6. Change the name of the grid to hdrGrid under the General tab of the Property Editor.
    Adding a visual element to a library
  7. Drag an Image object from the Palette over to the single, large cell.
  8. In the dialog that pops up, click on the function button for the URL. Change the type to Constant in the drop-down list next to the textbox.
    Adding a visual element to a library
  9. Simply borrow the Classic Cars logo from the Eclipse BIRT website. The logo can be obtained from: http://www.eclipse.org/birt/phoenix/examples/solution/ClassicLogo.jpg.
    Adding a visual element to a library
  10. Once the image has been added, change the Name to hdrLogo under the General tab of the Property Editor.
    Adding a visual element to a library
  11. Drag the column line over from the center as far as it will go to the left. This will eliminate the empty space in the column containing the image.
    Adding a visual element to a library
  12. Drag a label component over to the second column, first row cell.
    Adding a visual element to a library
  13. For the Text, enter Classic Cars, Inc.
  14. Set the label name to hdrHeaderLabel1 under Properties.
  15. Change the font properties as follows:
    • Size: 24 points
    • Weight: Bold
    Adding a visual element to a library
  16. Drag a data object to the second row. Set the name to hdrHeaderLabel2.
    Adding a visual element to a library
  17. Enter the Expression Editor for the data component. Navigate to Report Parameters/All/rprmReportTitle as the data expression. The final expression should be params["rprmReportTitle"] when done.
    Adding a visual element to a library
  18. Enter the following font parameters:
    • Size: 16 points
    • Weight: Bold
    Adding a visual element to a library
  19. Drag a Text object over to the third row. Set the name to hdrHeaderLabel3.
    Adding a visual element to a library
  20. Change the value to<VALUE-OF>new Date()</VALUE-OF>.
  21. Select the grid component and change the Background color to Silver.
Adding a visual element to a library

The finished component will look like the following image:

Adding a visual element to a library

Using components stored in a library

We now have two components stored in a library—a report header and a data source. How do you consume them. Well, that's what we are going to look at next. Using elements in a library is fairly straightforward. We only need to use the library in a report design that is stored in a project, and we are ready to go. We will look at how to do this in the next example.

  1. Create a new Report Design under the Classic Cars With Library report. Call it CustomerOrderForm.rptDesign. Use the blank report template.
    Using components stored in a library
  2. Open the Outline view for the new report.
  3. Open the Resource Explorer above the Outline view. We should see the library, with a drop hierarchical tree view showing its components.
    Using components stored in a library
  4. Drag the data source from under the Resource Explorers data sources to the data sources in the Outline view. This will reference the library's data source in the report design.
    Using components stored in a library

    Take note of the icon in the report outline. The chain in the icon indicates that this report item is linked to a library. This is a different icon from a regular data source icon.

  5. Under the Report Parameters section of the library, we can either drag-and-drop rprmReportTitle to the report or simply right-click on rprmReportTitle and choose Add To Report.
    Using components stored in a library
  6. Now, under the library, expand the Report Items section. Right-click on the hdrGrid object. Select the Data Sources icon under the Outline tab. We will notice that the option to Add to Report is grayed out as the correct target location is not selected in the report design outline. This is one caveat to adding through context menus, which is not an issue with dragging and dropping.
    Using components stored in a library
  7. Select the MasterPages section in the Outline tab and then select Header.
    Using components stored in a library
  8. Right-click on hdrGrids in the libraries and choose Add to Report.
    Using components stored in a library
  9. In the Outline tab, select rprmReportTitle under Report Parameters, right-click, and choose Edit.
    Using components stored in a library
  10. Enter Customer Orders Report in the Default value textbox.
    Using components stored in a library
  11. Save the report and preview.
Using components stored in a library

Updating components in the library to update reports

In the previous example, we must have noticed that when we preview the report, date is incorrect. In fact, it is showing us the HTML markup, not an actual date, which isn't right. The problem is that any report that uses this report header will suffer from the same problem. Now, we could fix these in each of the reports themselves; however, the fix would be local to that report only. We would surely like this fix to trickle down to all reports that are using these items. In order for that to happen, we need to edit the library itself.

  1. Open ClassicCarsLibrary.
  2. Under the Report Items, select hdrGrid. It should become visible in the Report Design pane.
    Updating components in the library to update reports
  3. Double-click on hdrHeaderLabel3 in the third row, second column to open the Text Item Editor.
    Updating components in the library to update reports
  4. Change the value of the mode from Auto to HTML and click OK.
    Updating components in the library to update reports
  5. Save ClassicCarsLibrary.rptlibrary.
  6. Open CustomerOrdersForm.rptDesign. We will be prompted that the library has been updated and we will be asked whether we want to reload the changes. Choose OK.
    Updating components in the library to update reports
  7. Rerun the report. Now that the library has been updated, when we run the report, we can see that the broken items have been fixed and the date gets displayed correctly.
    Updating components in the library to update reports

    It should be noted that once we change a component in a report such as its text, it then becomes a part of the report and not of the library. This is because technically the components consumed within a report become extensions of components in a library. When we change something, we are changing the extension, not the original; however, unmodified components will still reflect changes. The following example will demonstrate that changes made in the report will take precedence over the original library item.

  8. Open the Master Page section of the report design.
  9. Double-click on the hdrHeaderLine1 label in the Report Design file.
  10. Change the text to read Classic Cars Incorporated.
    Updating components in the library to update reports
  11. Save the report.
  12. Open ClassicCarsLibrary.rptlibrary.
  13. In the Outline tab, select Report Items | hdrGrid to make the header visible in the Report Designer.
    Updating components in the library to update reports
  14. Delete the third line with the<Value-Of>new Date()</Value-Of> tag. Save the library.
  15. In the First line, remove the period from Classic Cars Inc..
    Updating components in the library to update reports
  16. Save the library.
  17. Run the report.

In the example that we just discussed, the change made in the report will take precedence over the change made in the library, while the deleted line will not be displayed in the report as the change will trickle down. It can be inferred from the following screenshot that the local change to the company title take precedence over the library changes, but the deleted date line shows in both.

Updating components in the library to update reports

Adding existing components in reports to libraries

So far in this chapter, we have looked at developing and publishing components into a report library from scratch. We then looked at how reports could consume those libraries and link in those components. Next, we looked at editing the libraries and having them trickle down to reports.

So, what happens when we have a report we have developed and there are portions we want to share with others; how do you go about publishing these components to libraries?

Fortunately, there is an easy way to do this. From the context menu, we can right-click on an element and add it to a library.

The next example will show us how to add report components from a report to a library. We will create a basic query to retrieve customer information, which we will use later to combine with a second dataset to have a master/detail type report.

  1. Add a dataset called getCustomerInformation to the report.
    Adding existing components in reports to libraries
  2. Use the following query:
    select * from CLASSICMODELS.CUSTOMERS where CLASSICMODELS.CUSTOMERS.CUSTOMERNUMBER = ?
    
    Adding existing components in reports to libraries
  3. Create a report parameter called rprmCustomerID and link it to the dataset. Use the default value of 148 to make development a little easier so that we can debug and not have to enter a value each time.
    Adding existing components in reports to libraries
  4. Save the report.
  5. From the Outline or the Data Explorer tab, select getCustomerInformation.
    Adding existing components in reports to libraries
  6. Right-click and choose Export to Library.
    Adding existing components in reports to libraries
  7. Select ClassicCarsLibrary.rptlibrary as the location where we wish to export.
    Adding existing components in reports to libraries
  8. Click OK.
  9. Repeat steps 5 to 8 for rprmCustomerID, located under the Report Parameters section of the Outline or Data Explorer.
  10. Save the library.

The problem with this method is that the dataset and the report parameter are both two separate instances from the report design files and the report libraries. The way to link them is to delete the instances in the report and consume them using the steps described earlier in this chapter.

  1. In the Report Design file, delete both getCustomerInformation and rprmCustomerID.
  2. Add both components from the library back into the report.

Tip

Note: I encountered an issue here and had to close both the library and the report design to accomplish this step.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset