It is possible to use your own functions in a formula of a report, if these functions are implemented as methods in public (formula expander) Java class(es). To use such "user defined functions" in a formula, the following steps are required:
<classname>
to the property 'Formula Expander Class(es)', where
classname
means the fully qualified name of your class. This property can as well be a semicolon separated list of class names. Please check the classpath used by i-net Clear Reports and the package structure in case ClassNotFoundException occurs at runtime. Alternatively you can use the API to modify the configuration in use.
As of version 13 it is possible to use one or more of the following hidden parameters as method parameter in the formula expander class: "Engine", "HttpSession" and "HttpServletRequest". This can be useful, for example, to use the Engine API to get user defined parameters from the report engine. Please note that "HttpServletRequest" does not work in the i-net Designer.
static public String aFunction(String str) { return "[" + str + "]"; } static public String getUserProperty(String propKey, Engine engine) { Properties properties; String userProperty = ""; try { properties = engine.getUserProperties(); if (properties != null) { userProperty = (String)properties.get(propertyKey); } } catch( ReportException e ) { e.printStackTrace(); } return userProperty; } 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 samples.