Discussions
Categories
Groups
Community Home
Categories
INTERNAL ENABLEMENT
POPULAR
PUBLIC CLOUD
PRIVATE CLOUD
Quick Links
MY LINKS
HELPFUL TIPS
Back to website
Home
Intelligence (Analytics)
Composite Fields
chrisdutz
Hi,
I started working with Birt about 6 Hours ago and my current task is to extend an existing report to match changes done in the Frontend of the application.
Ok ... so in the Frontend I have a table cell that contains an output that is generated based upon 3 different variables:
The Pseudo-Code for the output would be
{I18N(Concat([field1], [field2]))}{IF([field3] >= 0, CONCAT(" (", [field3], ")"))}
So field1 and field2 are concatenated and this concatenation is the key which is then translated to a localized label. If field3 has a non-negative value this value is appended to the localized label.
How would I do this in a Birt report? (Please keep in mind ... I'm a quick learner, but I only started with Birt today ;-) )
Chris
Find more posts tagged with
Comments
CBR
Hi Chris,
i m assuming that you already implemented i18 in your reports and that the only problem is that you don't know how to get a label for a specific key out of the relevant property file (the one representing the current language).
If you are inside a report you can simply use BIRT scripting functionality to let BIRT get the label for specific key in a specific language. This logic is brought to you by the famous reportContext object. This object is initialized by the engine and can be used everywhere in your report.
To solve your problem add a dynamic text to your report. This opens the BIRT scripting editor where you can provide RhinoScript code that is interpreted when the report is executed. Add script:
var myKey = ""+[field1]+[field2];
if([field3]>0 {
myKey += [field3]
}
reportContext.getMessage(myKey, reportContext.getLocale())
Please substitute [field...] with the variable containing the field value. Let me know if you need additional informations.
Christian
CBR
also keep in mind that the dynamic text element is comparable with a function. The only difference is that there is no return statement available. Because of that fact the last statement in the editor is the return value of that function, which means that it's value is shown at the position of the dynamic text element in the report output.
chrisdutz
Hi Christian,<br />
<br />
thanks for that input. It seems to have been what I needed. I managed to get it working :-)<br />
<br />
This is the script of my dynamic text:<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
var myKey = ""+row["vulnerabilityClass"]+"." +row["result"];
if(row["cvssScore"] >= 0) {
reportContext.getMessage(myKey, reportContext.getLocale()) + " (" + row["cvssScore"] + ")";
} else {
reportContext.getMessage(myKey, reportContext.getLocale());
}
</pre>
<br />
Unfortunately Birt doesn't seem to format the Float in 'row["cvssScore"]' according to the locale settings. How can I manually make Birt output "1,5" for a German locale and "1.5" for an US locale? I've seen the Formatter.format function, but unfortunately I seem to be unable to get the current locales decimal pattern and Google seems to be ignoring my "+" in the query "+Birt" showing me all sorts of Java and Mac related stuff, but not the stuff I'm looking for :-(<br />
<br />
Chris
CBR
Hi Chris,
the locale for the formatter function is set by the framework. The only thing you have to do is to provide a java pattern. It is the same as in Java. You do not add locale sensitive data to the pattern because it is used for all locales.
To format a number just use pattern:
#,###.# (it will format the number as is with a thousands separator)
EDIT: sorry accidentially put a . where it should be a , :-)
chrisdutz
Hi Christian,<br />
<br />
I actually managed to figgure it out myself. Your solution would have been hard-coded to the German locale. I was looking for something that was using the current locale. I ended up with this code and it's working nicely:<br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
importPackage( Packages.java.text );
var myKey = ""+row["vulnerabilityClass"]+"." +row["result"];
if(row["cvssScore"] >= 0) {
var nf = NumberFormat.getNumberInstance(reportContext.getLocale());
nf.setMinimumFractionDigits(1);
nf.setMaximumFractionDigits(1);
reportContext.getMessage(myKey, reportContext.getLocale()) + " (" + nf.format(row["cvssScore"]) + ")";
} else {
reportContext.getMessage(myKey, reportContext.getLocale());
}
</pre>
CBR
Hi Chris,<br />
<br />
that's another solution. I don't think that my solution would hard code the format to german or us number format. The format code that is beeing set using the parameter is not bound to a specific locale. I accidentially had a . where it should be a , (see my edit comment)<br />
<br />
The javadoc says:<br />
<blockquote class='ipsBlockquote' ><p>The characters listed here are used in non-localized patterns. Localized patterns use the corresponding characters taken from this formatter's DecimalFormatSymbols object instead, and these characters lose their special status. Two exceptions are the currency sign and quote, which are not localized.</p></blockquote>
<br />
see: <a class='bbc_url' href='
http://download.oracle.com/javase/1.4.2/docs/api/java/text/DecimalFormat.html'>http://download.oracle.com/javase/1.4.2/docs/api/java/text/DecimalFormat.html</a><br
/>
<br />
But anyways: Your solution is as good as using the BIRT Formatter object.<br />
<br />
hope that i see you again in our next bug meeting.<br />
<br />
regards<br />
Christian