Trouble using a simple CustomExpression

368 views Asked by At

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();

1

There are 1 answers

0
Irene On

Issue resolved after stepping through DJ API and revising the parameters passed to DynamicJasperHelper.generateJasperReport method. In my project I’ve tried to integrate dynamic jasper code into existing reporting framework build using Jasper API and there was an overwrite of some of the parameters. Had to make few changes on how the existing code was handling parameters, locale, resource bundles vs what DynamicJasper is expecting (ended up setting resource and locale to DynamicReport dr before generateJasperReport was called):

dr.setResourceBundle(fooResourceBundleName);
dr.setReportLocale(fooLocale);