简体   繁体   中英

Jasper reports - parameter transformation before passing it to report engine?

Is it possible in any way to transform report parameter (input control) before it is passed to report engine?

I'll give an example. I'm using XML as data source:

<Results>
    <Object Id="0042" Val="dfg0bb" DateTime="2011-09-30T22:00:04Z" />
    <Object Id="0097" Val="abf0cc" DateTime="2011-09-30T22:00:06Z" />
    ...
</Results>

This is my XPath query:

//Object[translate(translate(translate(translate(@DateTime, '-', ''), ':', ''), 'T', ''), 'Z', '') <= $P{dateTimeValue}]

Descriptions for the fields are:

@Id, @DateTime, @Val

This is working OK, I get what I want, but the date attribute is kind of a problem, because I need to represent it as a number for comparison. As it can be seen I need to strip all non-numerical characters in order to compare dates as plain numbers (limitation of XPath 1.0 in Java). Furthermore, I want to define in JasperServer an input control for this parameter, and I want it to be date type, so user can choose from calendar.

So, is there any way (scriptlet or something) to transform supplied parameter before it is passed to report engine? (In this case date to numerical value so the XPath can work)

The idea you need to use is that one parameter can have a default value based on another parameter. Prompt your user for "MyDate" then convert this to "StringBasedOnMyDate" for use in the XPath query. It will look something like this in the .jrxml:

<parameter name="MyDate" class="java.util.Date" isForPrompting="true">
  <defaultValueExpression><![CDATA[]]></defaultValueExpression>
</parameter>
<parameter name="StringBasedOnMyDate" class="java.lang.String" isForPrompting="false">
  <defaultValueExpression><![CDATA[new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format($P{MyDate})]]></defaultValueExpression>
</parameter>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM