Question

I use JRXML template and dynamic input parameters as inputs to generate advanced JRXML. Later this JRXML is taken and handled by JasperServer. The main idea is to let user pick any columns he wants and customize them.

DynamicJasper provides good API for building custom columns but I faced an issue with compound columns.

For example a user wants his column 'A' would calculated as:

$F{INSTRUMENT} + " (" + $F{SECURITY_IDENTIFIER} + ")"

or

$F{QUANTITY} == null ?0:$F{QUANTITY}.setScale(2, RoundingMode.HALF_UP)

so after proceed the output JRXML would have records under its details/band tag like this:

<textField isStretchWithOverflow="true">
    <reportElement key="DK_Instrument" x="0" y="0" width="184" height="12">
    </reportElement>
    <textElement/>
    <textFieldExpression><![CDATA[$F{INSTRUMENT} + " (" + $F{SECURITY_IDENTIFIER} + ")"]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true">
    <reportElement key="DK_Quantity" x="220" y="0" width="56" height="12">
    </reportElement>
    <textElement/>
    <textFieldExpression><![CDATA[$F{QUANTITY} == null ?0:$F{QUANTITY}.setScale(2, RoundingMode.HALF_UP)]]></textFieldExpression>
</textField>

An AbstractColumn in DynamicJasper has this methods:

setColumnProperty(fieldName, className)
setCustomExpression(CustomExpression)
setCustomExpressionForCalculation(CustomExpression)

but non of them works for me.

When DynamicJasper generates JasperReport it tries to match those string to the existing fields and at this point fails since the strings contain compound items.

Any ideas/suggestion would be great.

Was it helpful?

Solution 2

Solved by implementing CustomColumn

public class DynamicColumn extends AbstractColumn {
    ...

    @Override
    public String getTextForExpression() {
        return getColumnProperty().getProperty();
    }
    @Override
    public String getValueClassNameForExpression() {
        return getColumnProperty().getValueClassName();
    }
}

The problem was that all existing implementations of AbstractColumn assume a single parameter passed to the column property and it covers the parameter with $F{...}. By overriding textForExpression the property successfully goes to the output JRXML.

OTHER TIPS

You can use JRDesignTextField and JRDesignExpression classes for this.

JRDesignTextField textField = new JRDesignTextField();
JRDesignExpression expression = new JRDesignExpression();
expression.setText("$F{INSTRUMENT} + \"( \" + $F{SECURITY_IDENTIFIER} +\" ) \"");
//or expression.setText("$F{QUANTITY} == null ?0:$F{QUANTITY}.setScale(2, RoundingMode.HALF_UP)");
textField.setExpression(expression);

I think this will help you. Check syntax, I didn't run the code, but it will give you idea.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top