User defined formula functions

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\api.

Importing UDF classes

To import a user defined formula class, follow the steps below:

  1. Develop your own Java program that implements the functions you need.
  2. Compile the Java program and add it to the ADDCLASSPATH variable of the batch file setenv.bat in <install_root>\bin.
  3. Start JReport Designer and open the Formula Editor.
  4. Import your class file in either of the following two ways:

Using UDF functions

When your class is successfully imported, the functions defined in the class will be displayed in the UDF node in the Functions panel of the Formula Editor. You can then call the functions in a formula by double-clicking them or using the following statement:

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.

If you want to view the formula references of any UDF function, select the function and click Formula References on the Formula menu, or right-click the function and click Formula References on the shortcut menu, then the UDF References dialog will be displayed, showing all the formulas that reference the UDF function if there are.

Example of using UDF functions

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\samples\APIUDFormula\jet\formula\javaformula.

Take the following steps:

  1. Compile this Java file to generate the class file.

    javac -classpath "<install_root>\lib\JREngine.jar;<install_root>\lib\report.jar;" MyFunctions.java

  2. Edit the batch file setenv.bat in <install_root>\bin.

    When you add class path, just add the root path. For example, suppose that the class file is located in C:\JReport\Designer\help\samples\APIUDFormula\jet\formula\javaformula, you can append C:\JReport\Designer\help\samples\APIUDFormula into the ADDCLASSPATH variable in the batch file.

  3. Start JReport Designer with the modified batch file.
  4. Create a new formula to load this class and open a file on disk by calling the methods in MyFunctions.java. Here we define the formula (fmlA) to load the class and call the method to open a temp file:

    pagenumber;
    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";

  5. Compose a formula (fmlB) to call the method to write data into the temp file:

    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.

  6. Compose another formula (fmlC) to close the temp file:

    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. For more information, see Formula levels.

  7. We have now defined three formulas (fmlA, fmlB, fmlC) in the above three steps. You can use it in a report as with a normal one.

Note: Now in JReport, the '8859-1' encoding is not required any more and the UDF functions you define should be able to return the correct unicode strings. Thus, if you have UDF fuctions created in previous JReport versions which still return '8859-1' encoded strings, you may need to modify them to make them return unicode strings.