Using Custom Functions in Scripts

Using Custom Functions in Scripts

Intellicus scripting supports the use of external (custom) Java classes and libraries. This allows users to extend scripting capabilities by incorporating third-party or custom logic such as XML parsing, database access, file reading, and integration with web services.

Use Cases

You can use external Java libraries for various purposes, such as:

  • Parsing XML data using third-party libraries.

  • Passing parameters to a custom Java class that connects to a web service and returns results (e.g., live stock price or current weather).

  • Using JDBC APIs to fetch data from external databases.

  • Reading external file formats such as .txt or .xls.

  • Logging information during report execution events.

Prerequisite

To use a custom Java class in scripts:

  1. Package the custom class into a .jar file.

  2. Place the .jar file in the following directory:

    <Install path>\IntellicusReportEngine\lib

This ensures the class is included in the Report Server's classpath.

Accessing Custom Java Classes in Scripts

Use the Packages keyword to access a custom class or its methods from a script. This is case-sensitive.

Examples

Accessing Classes

var classObj = new Packages.MyClass(); var result = classObj.parseXMLData(xmlData);
var classObj = new Packages.mypackage.MyClass();

Omitting Packages

For classes in packages that begin with java, com, or org, you can omit the Packages keyword.

var file = new java.io.File(filepath); var parser = new org.apache.xerces.parsers.DOMParser();

Importing Classes and Packages

You can import required classes or packages at the beginning of your script using one of the following methods:

1. importPackage(packagename)

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

Syntax

importPackage(Packages.<packageName>);

Examples

Importing a Single Package:

importPackage(Packages.java.io); var file = new File("d:\\temp.txt"); if (file.exists()) { // Handle file exists } else { // Handle file does not exist }

Importing Multiple Packages:

importPackage(Packages.java.io, Packages.java.lang); var file = new File("d:\\temp.txt");

2. importClass(classname)

Use importClass() to import specific classes from a package.

Syntax

importClass(Packages.<fullClassName>);

Examples

Importing a Single Class:

importClass(Packages.java.io.File); var file = new File("d:\\temp.txt"); if (file.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 file = new File("d:\\temp.txt"); if (file.exists()) { System.out.println("File exists."); } else { System.out.println("File doesn't exist."); }

3. JavaImporter(path)

Use JavaImporter() when you want to scope your imports and use them within a specific code block.

Syntax

var importer = JavaImporter(Packages.<package/class>); with (importer) { // Script logic using imported classes }

Examples

Importing Packages:

var importer = JavaImporter(Packages.java.io, Packages.java.lang); var file; with (importer) { file = new File("d:\\temp.txt"); } if (file.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); with (importer) { var file = new File("d:\\temp.txt"); if (file.exists()) { System.out.println("File exists."); } else { System.out.println("File doesn't exist."); } }

Importing Packages and Classes Together:

var importer = JavaImporter(Packages.java.io.File, Packages.java.lang); with (importer) { var file = new File("d:\\temp.txt"); if (file.exists()) { System.out.println("File exists."); } else { System.out.println("File doesn't exist."); } }

Copyright Intellicus Technologies Pvt. Ltd. All rights reserved.