How to embed .js files into BIRT

One of the most unique features of BIRT is its ability to use other programming languages to do complex tasks within the report file. Take a look at the forums on BIRT-Exchange.com and you will see examples of people using PHP, java, javascript and more. This post will show how you can embed a javascript file (.js) into a Maximo BIRT report to do complex tasks.

One of the common tasks a report writer will get asked to do is make sure the report looks good. The goal is to have the information laid out cleanly so the end user will clearly see the information. A typical request is to transform a text string’s case, say from upper case to lower. That’s easy to do in BIRT, just apply a string transform on a dynamic text field like this: BirtStr.toLower(row["description"])

Now the beauty of BIRT means it can also use the javascript method to apply a lower case string transform too. Like this: row["description"].toLowerCase().

So another common request is to change a text string from upper or lower case to proper case (UPPER CASE –> Proper Case). Easy, just use a dynamic text field and apply a string transform again - BirtStr.toProper(row["description"])

Guess what? BIRT doesn’t have a proper case text transform function. Using the string transform above will cause the report to error out and not show any information in that data row. A new string transform function, in an external javascript file, will need to be created to do the text transformation requested.

You’ll find plenty of examples searching the web on ‘javascript change string to proper case‘ for a javascript example to do a proper case text string transformation. The code that I’ll use was provided by Tuan on stackoverflow.com.

String.prototype.toProperCase = function () {
    return this.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() 
    + txt.substr(1).toLowerCase();});
    };

Copy/paste the javascript into a text file and save it as toProperCase.js in the Library folder of the BIRT Report Designer application. The path should similar to this: x:\birt_232\reports\birt\libraries

Now open the report file that will use the javascript file in BIRT Report Designer. On the Outline tab, click on the report file name. This will bring up the general properties of the report design file.

The 6th tab under the 'Property Editor' tab lists 'Resources', click on this to open where the .js file is associated.

Notice there is an option to add/include a JAR or javascript file. Click the 'Add' button for the javascript file and the 'toProperCase.js' file will be listed as an option. If it's not listed, double check where the .js file was copied to from the steps above. Once attached, save your report file to update the file.

To apply the text string transformation function, just append the text transform to any value in a dynamic text field with .toProperCase(). For example:

An example of the text transform is shown below. Text in black has been left unchanged, while the text in blue has been applied the .toProperCase text transform.

The next step is to get the .js file to work with the BIRT report inside Maximo. First the .js file has to be compressed into a zip file. This zipped file will be added as part of the import process into Maximo.

Next, log into Maximo and create the report object (skip this step if you’re going to re-import an existing report). Save the new report object and then click on Select Action –> Import Report. When you import the rptdesign file, you will also import the zipped .js flle as a resource file:

Now click 'Ok' and generate the Request Page.

Run the report in Maximo and the extended javascript text transformation function will work on the report from inside Maximo too.

Good luck and have fun with this new option for your Maximo BIRT reports.

Comments

Top