When the built-in functions do not satisfy your requirements, you can design your own formula functions. JReport provides you with user defined formula (UDF) functions. You can first write the code to implement a function, and then import it as a class when you build a formula.
However, when you want to develop a Java file to implement a function, only the following data types can be used for interacting with JReport products (pass parameters and return parameters): DbBigInt, DbDouble, fCurrency, DbBit (using for boolean type), DbChar (using for String), DbDate, DbTime, DbTimestamp, DbBinary, fText (using for Long VarChar), fImage (using for long VarBinary), fIntArray, fNumArray, fCurArray, fBoolArray, fStrArray, fDateArray, fTimeArray, fDateTimeArray, fBinaryArray, fTextArray, fImageArray, fIntRange, fNumRange, fCurRange, fBoolRange, fStrRange, fDateRange, fTimeRange, fDateTimeRange. All the data types that start with 'f' belong to the package jet.formula.*. The other data types belong to the package jet.connect.*. For details, refer to JReport Javadoc jet.formula and jet.connect packages in <install_root>\help\designer\api.
To use user defined formula functions, follow the steps below:
import instName from "ClassName";
Where,
For example, the following declaration assumes the class MyFunctions is in package jet.formula.javaformula:
import myfunc from "MyFunctions";
And the following declaration allows user defined UDF class in any package, supposing the class is com.mycom.MyFunctions:
import myfunc from "com.mycom.MyFunctions";
instName.MethodName(parameters);
Where,
MethodName is the method name in your class. You can call methods in your class as many times as you want by loading this class only once.
In this example, a demo Java program MyFunctions.java which is put in the package jet.formula.javaformula will be used to illustrate how to write record data to a file using UDF functions. You can get the Java source file from <install_root>\help\designer\samples.
Take the following steps:
javac -classpath "<install_root>\lib\JREngine.jar;<install_root>\lib\report.jar;" MyFunctions.java
You should create directories jet\formula\javaformula and copy the class file generated to that folder. When you add class path, just add the root path. For example, suppose that the class file is located in C:\test\jet\formula\javaformula, you can append C:\test into the ADDCLASSPATH set in the batch file.
import myfunc from "MyFunctions";
global integer filehandle = myfunc.openfile("e:\\test\\data.txt", false);
Note: If the class file is in your own package com.mycom other than jet.formula.javaformula, import the class in a formula as follows:
import myfunc from "com.mycom.MyFunctions";
string contents ="";
contents = @"Customer Name"+"\t"+ @Country+"\t"+ @Phone + "\t"+ PageNumber + "\n";
myfunc.write(filehandle, contents);
The return value of the last statement is the formula result.
PageNumber;
myfunc.closefile(filehandle);
If the statement PageNumber is added to the formula, the formula will be calculated after the page break, which means that the calculation point is controlled.
Note: From this version on, the '8859-1' encoding is not required any more and the UDFs you define should be able to return the correct unicode strings. Thus you may need to modify the UDFs created in previous versions which still return '8859-1' encoded strings to make them return unicode strings.