I see a lot of examples of using CustomExpression but for some reason it doesn't work for me. I am using Java 7, Dynamic Jasper 5.0.2, Jasper Reports 5.5.0. In this simple expression I am just trying to return the string "foo". It seems that evaluate never gets called so all the rows show blank instead of "foo". The "foo" example is just for testing purpose, but my final custom expressions will be: one as a concatenation of 2 fields and another will be a replacement of booleans 1/0 with Yes/No.
AbstractColumn columnField = ColumnBuilder.getNew()
.setCustomExpression(getMyCustomExpression())
.setTitle(bundle.getString(fld.getFieldNameDesc())).setWidth(Integer.parseInt(JR_DYNAMIC_DEFAULT_COLUMN_WIDTH))
.setPattern(FieldTypeNameConvertor.getPatternByOracleType(fld.getFieldType()))
.setStyle(getStyle(fld.getFieldType()))
.build();
drb.addColumn(columnField);
...
private CustomExpression getMyCustomExpression() {
return new CustomExpression() {
public Object evaluate(Map fields, Map variables, Map parameters) {
final String myString = "foo";
return myString;
}
public String getClassName() {
return String.class.getName();
}
};
}
I also get blanks if I create a column and I set the format:
Format textFormatter = new Format(){
public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
if (obj == null || Boolean.FALSE.equals(obj))
toAppendTo.append("No");
else
toAppendTo.append("Yes");
return toAppendTo;
}
public Object parseObject(String source, ParsePosition pos) {
return null;
}
};
....
columnField = ColumnBuilder.getNew()
.setColumnProperty(fld.getFieldNameKey(), getJavaType(fld.getFieldType()))
.setTextFormatter(textFormatter)
.setTitle(bundle.getString(fld.getFieldNameDesc())).setWidth(Integer.parseInt(JR_DYNAMIC_DEFAULT_COLUMN_WIDTH))
.setPattern(FieldTypeNameConvertor.getPatternByOracleType(fld.getFieldType()))
.setStyle(getStyle(fld.getFieldType()))
.build();