How to set variable value after "Print when Expression" from textfield returned true - jasper-reports

Using iReport I have a textfield with following "Print When Expression":
new Boolean($F{data}.doubleValue()<3 && $V{check}.intValue()==0);
My problem is that I only want to print this textfield once. For that I thought I can use a variable (check).
I tried to use this expression for the variable:
($F{data}.doubleValue()<3) ? 1 : 0
The problem is that the expression for the variable is called before the textfield checks its "print condition" and so the textfield never get printed.
How can I achieve that the variable value is set AFTER the textfield "print condition" returned true?
I want to mark the first value which is smaller than a reference value

I often use the parameter map (P{REPORT_PARAMETERS_MAP}) to store and evaluate custom expression depending on previous values and outputs
Example (return true if value $F{X}<100 and it has never been below before)
<printWhenExpression>
<![CDATA[$F{X}.doubleValue()<100?$P{REPORT_PARAMETERS_MAP}.put("X_LESS_100",true)==null?true:false:false]]>
</printWhenExpression>
This expression leverage the fact that Map.put(K,V) returns the previous value associated with key, that is null if never called, while second time $F{X} is below 100 it will return our previous set value that is true.
Note: you do not need to define any variable.

I found a workaround, but I don't find it a very good way, because my solution is related to the evaluation time of the conditions from the textfield and the variable.
So if anybody has a nice solution please post it.
I set the initial value of my check variable to 0.
The variable expression looks like this:
($F{data}.doubleValue() >= 3)? 0 : ($F{data}.doubleValue() < 3 && $V{check}.intValue()==0) ? 2 : 1;
The "Print When Expression" from the textfield is:
new Boolean($V{check}.intValue()==2);

Related

Crystal Reports - Only filter by parameter if it has a value, else return all values

How do you return all values if the parameter field is left blank? This is currently what I have in the selection expert:
if not(hasvalue({?parameter})) then true else {Table.Column} = {?parameter}
Set the 'Optional Prompt' property of the parameter to True:
Try this formula instead:
(not HasValue({?parameter}) OR {Table.Column} = {?parameter})
Using optional parameters in Select Expert formulas can be tricky. As soon as code is parsed that tries to say the parameter is equal to anything, it throws an error because the parameter has no value. The If...Then...Else logic still parses the entire block of code, regardless of whether the condition is true or false. In the formula above, if the parameter has no value in the first statement, the result is True, then the OR is parsed and the rest of the statement is ignored without parsing because it's result won't change the outcome of the formula.

XML Expression Binding - Proceed Code in conditional operator

I am currently working on a Fiori app. At the moment I try to set a title depending on the value of a property I get from my OData service. Therefore I want to use expression binding with the conditional operator.
So when ${PROPERTIY} has the value "EXAMPLE", it should print the value of OUTPUT_PROPERTY_1. Otherwise, it should print the value of OUTPUT_PROPERTY_2.
XML:
<ObjectListItem title="{= ${PROPERTIY} === 'EXAMPLE' ? '${OUTPUT_PROPERTY_1}' : '${OUTPUT_PROPERTY_2}'}">
Unfortunately, it just prints ${OUTPUT_PROPERTY_1} or ${OUTPUT_PROPERTY_2}, and does not proceed the code to get the actual value of the properties.
Is there any chance to solve this problem or even a good workaround in order to print the actual value of the related property?
Remove the apostrophes around the expression binding syntax:
title="{= ${PROPERTIY} === 'EXAMPLE' ? ${OUTPUT_PROPERTY_1} : ${OUTPUT_PROPERTY_2}}"
Otherwise, '${OUTPUT_PROPERTY_x}' will be treated as a string literal.

Enter a query in a default value expression

In Jasperreports I would like to enter a Default Value Expression to a Parameter as a Query string to be able to dynamically provide the user with a default value that is correct, but not force him to choose it.
Is there anyway?
I guess the result should look something like this (even though it doesn't work):
I am using this for a form with a single-value selection method (the user can write which ever number he/she wants but I want the default value to be selected from the database).
Here's how I handle this:
The user selects a value from an input control (in my example, I will call it $P{time_toggle}). Then, I have another parameter ($P{time_constraint}) that takes the user input from $P{time_toggle} and decides what SQL string to inject into the main query based on it. The default value expression for $P{time_constraint} looks like:
$P{time_toggle} == "rolling_quarter" ? "..." : (
$P{time_toggle} == "rolling_30_days" ? "..." : (...
)
)
Then, in my main report query I reference $P{time_constraint}:
SELECT * FROM tblTable WHERE $P!{time_constraint}
To set a default time period, I set the default value expression for $P{time_toggle} to my desired default.

Variable expression contains another variable

I am trying to put some variable inside Variable Expression of another variable.
For example:
$V{sum} = $F{quantity} * ${price}, where sum is simple variable without any calculation
$V{total} = $F{disb} * $V{price}, where total has 'Sum' calculation type.
As a result I receive the wrong amount.
But If I use:
$V{total} = $F{disb} * $F{quantity} * ${price}
the amount is valid.
Is there any reason that variable inside variable expression gives wrong value? Thank you
If you are outputting the $V{total} of your first example in a textfield, then you will need to make sure the Evaluation Time is set correctly per your report. Most likely you will want to set the field evaluation time to "Report".
The evaluation time determines when dynamically calculated variables are actually processed during the report's generation life-cycle.

how should I set <PrintWhenExperssion> value true or false from java code

I want to set
<printWhenExpression>![CDATA[Boolean.valueOf(true)]]</printWhenExpression>
to Boolean.valueOf(false) when I fill the report according to some conditions. This means I want to set value 'false' in <printWhenExpression> at run time. Can any one help me for this?
Instead of having hard coded value Boolean.valueOf(true)/Boolean.valueOf(false) use a variable which is evaluation to a boolean:
Boolean.valueOf(someVariableOfTypeBoolean)
or
Boolean.valueOf(someMethodThatReturnsBoolean())