Intellicus Enterprise Reporting and Business Insights 19.1

Using Custom Functions in Scripts

0 views June 28, 2020 0

You can use external (custom) Java library classes and objects in scripts.

For example, there is a database that holds XML as data.  User can create a formula field, which will parse this XML and return data values from it.  To achieve this, user will access third-party xml parser classes.

Few applications where user may need to use custom Java objects can be:

  • Script can use some external java libraries for XML Parsing.
  • Script can pass some parameters to an external custom Java class which connects to a web service using the given parameters and returns the resultant data, e.g. a web service which returns the live price of a stock or current weather condition of a city.
  • Script can use JDBC APIs to connect to a database and do tasks listed in above point.
  • Script can use java or external APIs to read external file (.txt, .xls etc.) data.
  • Script can log required information during report events.
Prerequisite

The third party (custom) class(s) to be accessed from Intellicus script needs to be included in Intellicus report server class path.

Note: Easiest way to do this is to make a jar containing custom class(s) and place it in the Report Engine’s lib folder.

<Install path>IntellicusReportEnginelib

To access a class in script

You can access a custom class in a script by using a keyword Packages (case sensitive) that will be followed by the class name or the entire package pattern (if the class is in a specific package) including class name.

For Example,

var classObj = new Packages.MyClass();

OR

var classObj = new Packages.mypackage.MyClass();

Instance of the class once generated, can be used to invoke any of the method (that contains logic, for e.g. XML parsing code) of the custom class from the script.

var resultValue = classObj.parseXMLData(xmlData);

For the package pattern starting with “java”, “com” or “org”, Packages keyword can be omitted.

var fileObj = new java.io.File(filepath);

var domObj = new org.apache.xerces.parsers.DOMParser();

Importing class in a script

You can import all required packages or classes once in the beginning and then user can use the class directly with the class name anywhere in the script. To achieve this, you can use any of the following techniques.

Using importPackage(packagename)

Use importPackage() to import all classes within a package.

@param packagename: Entire package path whose classes needs to be imported in a script.

You can import multiple packages by specifying all package paths separated by comma as argument of importPackage function.

Import statement should be written before first usage.

If the package starts with java, org or com, You can omit Packages key word if the package starts with java, org or com.

Examples:

Importing Single Package

importPackage(Packages.java.io);
var fileObj = new File(“d:temp.txt”);
if(fileObj.exists())
{
//do something}
else
{
// do something else}

Importing Multiple Packages

importPackage(Packages.java.io, Packages.java.lang); var fileObj = new File(“d:temp.txt”);

Using importClass(classname)

If a user needs to import specific class/classes within a package, importClass() can be used.

@param classname: Path of the class (including entire package path) which needs to be imported in a script.

Multiple classes can also be imported by specifying all class path names separated by comma as an importClass function argument.

Examples:

Importing Single Class

importClass(Packages.java.io.File);
var fileObj = new File(“d:temp.txt”);
if(fileObj.exists())
{
java.lang.System.out.println(“File exists…!”);
}
else
{
java.lang.System.out.println(“File doesn’t exist…!”);
}

Importing Multiple Classes

importClass(Packages.java.io.File,Packages.java.lang.System);
var fileObj = new File(“d:temp.txt”);
if(fileObj.exists())
{
System.out.println(“File exists…!”);
}
else
{
System.out.println(“File doesn’t exist…!”);
}

Using JavaImporter(path)

If a user needs to import entire package or a specific class, JavaImporter() can be used.

@param path: Path of the package/class (including entire package path) which needs to be imported in a script.

Multiple packages/classes can also be imported by specifying full path names for all packages/classes separated by comma as JavaImporter function argument.

Example,

Importing Packages

var importer = JavaImporter(Packages.java.io,Packages.java.lang);
var fileObj;
with(importer)
{
fileObj = new File(“d:temp.txt”);
}
if(fileObj.exists())
{
System.out.println(“File exists…!”);
}
else
{
System.out.println(“File doesn’t exist…!”);
}

Importing Classes

var importer = JavaImporter(Packages.java.io.File,Packages.java.lang.System);
var fileObj;with(importer)
{ fileObj = new File(“d:temp.txt”)
;}
if(fileObj.exists())
{
System.out.println(“File exists…!”);
}
else
{
System.out.println(“File doesn’t exist…!”);
}

Importing Packages & Classes together

var importer = JavaImporter(Packages.java.io.File,Packages.java.lang);
with(importer)
{
var fileObj = new File(“d:temp.txt”);
if(fileObj.exists())
{
System.out.println(“File exists…!”);
}
else
{
System.out.println(“File doesn’t exist…!”);
}
}