JasperServer - Creating User Defined Formulas Tutorial
Introduction
Ad Hoc provides a framework for creating user-defined formulas. Like the available fields, the user-defined formulas can then be used to create custom fields and columns in a table. This tutorial uses an example of user-defined formula round2(), that rounds a number value at a specific decimal position.
This tutorial does not apply to non-numeric formulas.
Product Versions
This tutorial works with version 3.5 and later.
User
Developer
Objectives
This guide will enable you to:
Defining Formulas
Defining a formula involves specifying the formula class and associated properties. In JasperServer, the Spring bean definition mechanism is used for this purpose. If Tomcat is used, the bean definition file can be found in tomcatdir\webapps\jasperserver-pro\WEB_INF\ directory.
Follow these steps to define a formula Round2.
| Step | Action | ||||||||||
| 1 |
Define the bean class as follows:
Where RoundFormulaImpl2 is the formula Java class, ADH_406_ROUND2 is the formula id and Round2 ( arg1,arg2) is the display expression used in the GUI. |
||||||||||
| 2 |
Assign the formula category.
Notes:
|
||||||||||
| 3 |
Presently, formulas are numeric only. Formulas are grouped under the Number section as follows.
|
||||||||||
| 4 |
Formulas are classified into basic and special. The basic formula performs basic arithmetic operations such as add, subtract, multiply and divide. The special formula performs advanced arithmetic operations, such as round, rank and percent of total. Since Round2 is considered a special formula, it is placed in the special formula section as follows.
|
||||||||||
| 5 |
The root of the formula definition is defined by the formula factory provided by JasperServer as follows.
|
||||||||||
| 6 |
Add a message definition for ADH_406_ROUND2 in adhoc_messages.properties under the \WEB_INF\bundles directory, i.e., ADH_406_ROUND2=Round2(value,scale).
|
Implementing Formulas
Implementing a formula involves creating and compiling the formula class. JasperServer provides a FormulaAdaptor abstract class to assist a user in adding a specific formula. Extend the FormulaAdaptor class and provide implementation of the following methods.
| Methods | Description |
| public Object calculate(Object[] args) | The calculate method accepts an integer as the decimal position for the rounding result. |
| public String getCategorizerType() | The method returns the output category of the formula. |
| protected String determineType() | The method returns the data type of the formula output. |
| protected int getSupportedMask() | The method returns the formula mask specifying the argument list of the formula. |
| public String getOperatorName() | The method returns the name label of the formula. |
Implement the Java class for the Round2 formula.
| Step | Action | ||||||||
| 1 |
Create a class named RoundFormulaImpl2 that extends the FormulaAdaptor class.
|
||||||||
| 2 |
Add implementation for the calculate() method, which takes a numeric value in its first argument and accepts an integer in its second argument as the decimal position for the rounding result.
|
||||||||
| 3 |
Add implementation for the getCategorizerType() method, which returns the output category of the formula. Note: The category can be String, Numeric and Date.
|
||||||||
| 4 |
Add the determineType() method, which specifies the output data type of the formula.
|
||||||||
| 4 |
Add the getSupportedMask() method, which specifies the input mask of the formula. Notes:
|
||||||||
| 5 |
Add the getOperatorName() method, which returns the name label of the formula.
|
||||||||
| 6 |
The final class implementation:
|
||||||||
| 7 |
Compile the file from the command line.
Example: |
Installing Formulas
Follow these steps to install the formula on Tomcat.
| Step | Action |
| 1 |
Update WEB_INF\applicatonContext-adhoc.xml and \WEB_INF\bundles\ adhoc_messages.properties as described in part 1.
|
| 2 |
Create a jar file that contains the user-defined class and place it in the WEB_INF\lib directory.
|
| 3 | Restart JasperServer. |
Written By
This tutorial was written by Andrew Sokolnikov and Min Zhang Oct, 2009
Language











