i-net Clear Reports

User Defined Functions

It is possible to use your own functions in a formula of an report, when these functions are implemented as methods in public Java class(es). To use user defined functions in a formula follow the next steps:

  • Add the class file(s) to the classpath
    • You can do this, for example, by creating a jar file containing the class(es) (with command “jar cf formula-sample.jar FormulaSample.class”) and copy this jar file into the “lib” directory that is included in the installation directory of i-net Clear Reports or i-net Designer.
  • Open the Configuration Manager and add the <classname> to the property 'Formula Expander Class',  where classname means the fully qualified name of the class. This can also be a semicolon separated list of class names. Please check the classpath used by i-net Clear Reports and the package structure if ClassNotFoundException appears at runtime. Alternatively you can use the API to modify the used configuration.
  • Now you can write a formula using user defined functions by calling them by name. You can create a formula using i-net Designer or i-net Clear Reports API (RDC).
  • Note 1: If you have bundled your methods in a library, you have to make bindings. If this library is a Java library you need a wrapping only.
  • Note 2: The methods in the specified class have to be public and could be static.
  • Note 3: The return value and the parameter values of the methods have to had one of the following types:
    • java.lang.Number
    • java.lang.Boolean
    • java.lang.String
    • java.sql.Date
    • java.sql.Time
    • java.sql.Timestamp
    • Object[] of these types

Examples:

static public String aFunction(String str) {
	return "[" + str + "]"; 
}
 
static public Number aFunction(Number d) {
	return new Double( 2.0 * d.doubleValue()); 
}
 
static public String aFunctionToo( Object obj) {
	if (obj instanceof String) {
		return ((String) obj) + " Ha"; 
	} else {
		return null;
	}
}
 
static public Number aFunction(Number i) {
	return new Integer(i.intValue() * 2); 
}
 
public Object[] aFunction(Object[] i){
	Object[] o = new Object[i.length];
	for (int k=0;k<i.length;k++) {
		o[k] = i[i.length-k-1];
	}
	return o;
}
 
public Boolean aFunction( Number i, FormulaRange range ){
	double lower = ((Number)range.getFrom()).doubleValue();
	double upper = ((Number)range.getTo()).doubleValue();
	double n = i.doubleValue();
 
	if( range.isLowLimitIncluded() ? n < lower : n <= lower ){
		return Boolean.FALSE;
	}
	if( range.isHighLimitIncluded() ? n > upper : n >= upper ){
		return Boolean.FALSE;
	}
		return Boolean.TRUE;
	}
}
 
// Note: This function should do a type check on the range bounds first.

You can find more samples in the Java code sample: FormulaSample.

See Also:


i-net software strives to provide accurate product documentation. Please give us your feedback using the form below.
NOTE: This form is for documentation feedback only. For technical assistance, please send an email to clearreports@inetsoftware.de.

 

© Copyright 1996 - 2012, i-net software; All Rights Reserved.