How to auto-load JS file from classpath on BIRT

814 Views Asked by At

I created a set of utils to use inside my BIRT reports.

I put them (my-utils.jar) inside BIRT's lib directory:

BIRT_HOME\plugins\org.eclipse.birt.report.viewer_x.x.x.x\birt\scriptlib

They are working fine, but now I want to create a JS file, put it on the classpath to be able to use it functions.

// my-utils.js
GLOBAL_VARIABLE = {  
    formatSomething: funnction(value) {
       // do my stuff with 'value' and return
    }
};

I want to use it directly, without explicity load it inside a Dynamic Text or Script event, like this:

GLOBAL_VARIABLE.formatSomething('bla bla bla')

How can I do this?

2

There are 2 best solutions below

0
On BEST ANSWER

You want to create an extension point. In Eclipse, create a new blank plugin-project, then add this extension point:

org.eclipse.birt.core.ScriptFunctionService

Create a folder just under the project root, name it for instance "jslib" and put your js files in this folder. Edit plugin.xml and add a JSLib tag, so that it should look like this:

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
   <extension
         id="my.js.function"
         name="My custom JS functions"
         point="org.eclipse.birt.core.ScriptFunctionService">
   </extension>
  <JSLib
        location="jslib">
  </JSLib>

</plugin>

Export the plugin as a jar and deploy it in Eclipse and/or web applications and your js functions should be available. More informations about custom birt functions here

1
On

Note that Dominique's solution is only for the case when the functions are implemented using Java.

The following solution is for the case when the functions are implemented using Javascript:

First, you don't need to add the **.js* files to the classpath. Instead, you put them into the directory specified for resources and reference them in your report (or in one of the libraries that you are using in your reports). See the following example:

Directory structure:

workspace
    \birt    # I have this configured as as "Report Project" in the WS
        my_report.rptdesign
    \res  
        my-utils.js

In the preferences menu: Report Design / Resource / Resource folder = <Current Project Folder>/res

(At runtime, you can set the resource folder using the API, I don't know how to configure this for the Web Viewer example)

In my_report.rptdesign, see the properties of the report itself. Inside the "Resources" tab, add my-utils.js to the Javascript Files.

Now you can use the functions of your library in your report.