How Do I Create a Custom Function for the Java Report Engine?
The Java Engine (the Engine) ships with many functions, however, you may find from time to time you require a function customized to your needs that does not exist in our library. The Engine allows you to define your own custom macro functions to expand the Windward reporting functionality.
In this article we'll work step by step through a Catapult Java Report Engine custom function example. We'll create a function "E()", which returns the first 15 digits of the value of Euler's Constant e.
Requirements
Create the Custom Function
Navigate to: https://github.com/windward-studios/Windward-Samples and download the code samples as a zip.
Once the zip is downloaded, extract the zip. Then, under Java Samples Samples > Specialized, there will be a project called CustomFunction. Open that folder and open the WindwardCustomFunctions.ipr with Intellij.
In the Project Explorer, select the file WindwardCustomFunctions.java.
In that file you can write custom functions to use with the Java Report Engine. We have provided three custom functions: PI(); SQRT(); and MULTIPLYALL(). Now we'll edit WindwardCustomFunctions.java to add our new custom function E().
First, change the number of functions to 4.
Paste this code into the second section (below MULTIPLYALL()) to add some metadata for the new custom function:
functionName[3] = "E";
functionFullName[3] = "E()";
functionDescription[3] = "Returns the value of e, 2.71828182845904, accurate to 15 digits.";
functionNumberOfArgument[3] = new Integer(0);
functionArgumentName[3] = null;
functionArgumentDescription[3] = null;
functionArgumentType[3] = null;
Paste this code into the fifth section (below MULTIPLYALL()) to add the function definition itself:
public static Object E() {
return new Double(2.71828182845904);
}
Finally, click on Build, then select Rebuild Project.
A notification in the message bar at the bottom of IntelliJ IDEA indicates the build was successful.
Install the Custom Function
The result of the steps above is to create a new WindwardCustomFunctions.jar file. Now we need to install that file so the Java Report Engine will use it.
Click on the Custom Functions button in Catapult again to open Windows Explorer on the Custom Functions Java Report Engine demo directory.
Navigate to out\, artifacts\ then WindwardCustomFunctions\.
Copy WindwardCustomFunctions.jar. Then navigate to the installation directory of the Java Report Engine. This is wherever you placed the jars
directory from the Java Engine Zip file.
Open the jars\ directory and paste the new WindwardCustomFunctions.jar, overwriting the old one.
Now the custom function E() is available when generating output with the Java Report Engine.