doing comparison if else in JasperReports - jasper-reports

I want to do a comparison such as:
if <field> == 0 then "-"
Can somebody tell me the syntax using JasperReports?

iReport (JasperReports) uses a Ternary operator. For example, consider the following logic:
IF boolean condition THEN
execute true code
ELSE
execute false code
END IF
Using a ternary operator, this becomes:
boolean condition ? execute true code : execute false code
When using a variable with the following expression:
$F{column_value}.intValue() == 42 ? "Life, Universe, Everything" : "Naught"
Then the variable's value would be "Life, Universe, Everything" if, and only if, the integer value of $F{column_value} is equal to 42.
Where things get a little obtuse is when you have to have nested conditions. For these, put the nested conditions in parenthesis and on a separate line:
condition1 ?
(condition2 ? true_code2 : false_code2) :
false_code1
So when you need to do many of them:
condition1 ?
(condition2 ?
(condition3 ? true_code3 : false_code3) :
false_code2) :
(condition4 ? true_code4 : false_code4)

example of expression in ireport:
(
$F{foo} == 0 ?
"Planned" :
$F{foo} == 1 ?
"Reserved" :
$F{foo} == 2 ?
"Canceled" :
$F{foo} == 3 ?
"Absent" :
$F{foo} == 4 ?
"Complete" :
"Unknown"
)

Use if-else condition:
if customer name is null write '-' (absent), else write customer name.
Be careful of your field data type!
<textFieldExpression class="java.lang.String">
<![CDATA[
$F{CustomerName} == null ? '-' : $F{CustomerName}
]]>
</textFieldExpression>

Related

How to use Case statement in report?

Can anyone let me know how to write a CASE STATEMENT like in SQL but in a Jaspersoft 6 report? More precisely inside the expression editor.
For example:
CASE ProductLine
WHEN 'R' THEN 'Road'
WHEN 'M' THEN 'Mountain'
WHEN 'T' THEN 'Touring'
WHEN 'S' THEN 'Other sale items'
ELSE 'Not for sale'
END
You need to use a ternary operator like below:
$F{variable_1}.equals("R") == true ? 'Road' : "Else not for Sale"
Well you can't use case directly but having if else condition within another if else condition will give the solution.
Here's an example below where the switch-case functionality is achieved through multiple if else conditions
( $F{FIELDV}.equals("ab") ? "aaaa" :
( $F{FIELDV}.equals("bc") ? "bbbb" :
( $F{FIELDV}.equals("de") ? "cccc" : ""
)))

Drools rule multiple objects with multiple constraints

I have following rule
rule "Row LoanApproval"
dialect "mvel"
when
f2 : Loan( amount >= 0 )
f1 : Applicant( creditScore <= 200 )
then
f2.setApproved( true );
System.out.println("rule call");
end
My problem is value of field "approved" will be true when amount>=0. Second constraint f1 : Applicant( creditScore <= 200 ) plays no role here.
so how can I write it when both conditions are true. then "approved" should be true?

Multiple field in text field

I have an XML documents which include 3 titles.
<page>
<title>
<title>
<title>
Now I want to show them in one text field. I do the following
$F(title)+","+$F(title2)+","+$F(title3)
and it works well. There is a problem if title2 and title3 will be empty. assume if title2 is empty in XML than the result is being like blow:I
This is the title 1 ,,This is title 3
is there suggestion for this?
Something like this should work:
($F{title} != null ? $F{title} : "" )
+ ($F{title2} != null ? ", " + $F{title2} : "")
+ ($F{title3} != null ? ", " + $F{title3} : "")
This is assumes that:
a) empty means null
b) field $F{title} will never be null
If I am wrong on assumption (b) this the expression gets only a little more complex. It can be done.

showing comparison if in iReport

I have problem with IF Expression ,
This expression shows settled and outstandings records.
value = ((java.lang.Boolean)field_paid.getValue()) == false ? "Yes" : "No";
This shows transaction number:
$F{transactionNumber}
How i can report only outstanding transactions?

field value can't display in null condition in jasper report

I have problem when i'm using a condition for checking null values in report. My condition is
$F{BILANGAN4}==0 ? "-" : ""
The values of field does not appear but it just appear "-".
Anyone know about this?
I'm not sure what $F{BILANGAN4}==0 ? "-" : "" will compile-down to. Have you looked at the generated .java code for your report? java.lang.Long could be null, so you want to check like this:
( ( null == $F{BILANGAN4} || $F{BILANGAN4} == 0 ) ? "-" : "" )
I usually explicitly add .longValue() to calls like that just to be explicit: I'll get a compiler failure if the type isn't correct and I know I need to adjust something and maybe re-consider the code.
Note that the above code (as a text-field value for instance) will only display "-" or nothing. If you want the value of the $F to actually display, you'll have to put it in there. I'm fairly sure this is actually what you want:
( ( null == $F{BILANGAN4} || $F{BILANGAN4} == 0 ) ? "-" : $F{BILANGAN4} )