Please see this Expression Language
styleClass="#{obj.validationErrorMap eq null ? ' ' :
obj.validationErrorMap.contains('key')?'highlight_field':'highlight_row'}"
Even if the map is null, highlight_row style is getting applied.
So I changed to
styleClass="#{empty obj.validationErrorMap ? ' ' :
obj.validationErrorMap.contains('key')?'highlight_field':'highlight_row'}"
Even then, highlight_row is getting applied.
if the map is empty OR null I dont want any style to be applied.
Any help? and reasons for this behaviour?
Use empty (it checks both nullness and emptiness) and group the nested ternary expression by parentheses (EL is in certain implementations/versions namely somewhat problematic with nested ternary expressions). Thus, so:
styleClass="#{empty obj.validationErrorMap ? ' ' :
(obj.validationErrorMap.contains('key') ? 'highlight_field' : 'highlight_row')}"
If still in vain (I would then check JBoss EL configs), use the "normal" EL approach:
styleClass="#{empty obj.validationErrorMap ? ' ' :
(obj.validationErrorMap['key'] ne null ? 'highlight_field' : 'highlight_row')}"
Update: as per the comments, the Map turns out to actually be a List (please work on your naming conventions). To check if a List contains an item the "normal" EL way, use JSTL fn:contains (although not explicitly documented, it works for List as well).
styleClass="#{empty obj.validationErrorMap ? ' ' :
(fn:contains(obj.validationErrorMap, 'key') ? 'highlight_field' : 'highlight_row')}"
Related
I'm a bit new to using Spring Expression Language but I'm trying to see what is the best way to check a that a Boolean value is not null and true?
For type "Display" there is a property "removeMessage" which is of type java.lang.Boolean.
So I want to first check that this value is not null and that it is true. Based on if this is true or not I will either return an empty string or a calculatedMessage.
I am currently checking with below expression:
display?.removeMessage != null && display?.removeMessage ? '' : display.calculatedMessage
While this works, I was hoping to find a more nicer way to do the null and true check. I was thinking/hoping for something more like how we would check normally such as e.g. BoolenUtils.isTrue(value) (apache), or even just checking like Boolean.TRUE.equals(value) which takes care of the null check.
Just having this should work , and already include your null check:
display?.removeMessage ? null : display.calculatedMessage
We are trying to map data using Talend DI tool. In that, we have to capture transformation which is related to Conditional operator.(Because of limitation of tool it won't allow if-then-else syntax instead it does support conditional operator.
Sample Data :
I am trying to write this expression into talend tmap component . How to write this expression into tmap component expression builder using ternary operator. Additional, i have to check for null values.
case when [TCode]='(00) PRE-PAID' then '00'when[TCode]='(01) C.O.D.' then '01'when[TCode]='(02) EOM' then '02'when[TCode]='10' then '(10) NET 10 DAYS'when[TCode]='15' then '(15) NET 15 DAYS'when[TCode]='21' then '(21) 2 % 30 NET 31'when[TCode]='23' then '(23) 2% NET 30 DAYS'when[TCode]='3' then '(3) CHECK'when[TCode]='30' then '(30) NET 30 DAYS' else [TCode]end as TCode
Tried this conditional operator :
"(00) PRE-PAID".equals(row.tCode) ?"00" :
"(01) C.O.D".equals(row.tCode) ?"01" :
"(02) EOM".equals(row.tCode) ? "02" :
"Unknown"
Getting error when tried above conditional operator :
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
XML_API_tXMLMap_1 cannot be resolved to a type
XML_API_tXMLMap_1 cannot be resolved to a type
Syntax error on token ""(00) PRE-PAID"", delete this token
Thanks in advance !
Analysis
According to the link in your comment, you try to execute the following:
row1.tcode==null?null:row1.tcode.length()==0?null:row1.tcode.toUpperCase()
"(00) PRE-PAID".equals(row.tCode) ?"00" :
"(01) C.O.D".equals(row.tCode) ?"01" :
"(02) EOM".equals(row.tCode) ? "02" :
"Unknown"
and you are getting the following error:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
XML_API_tXMLMap_1 cannot be resolved to a type
XML_API_tXMLMap_1 cannot be resolved to a type
Syntax error on token ""(00) PRE-PAID"", delete this token
NOTE: Java is a case sensitive language. So either tcode or tCode is correct, not both.
Explanation
You provided two separate rows of code and Java doesn't know how to interpret this.
Your first line of code is (usually ended with a ;):
row1.tcode==null?null:row1.tcode.length()==0?null:row1.tcode.toUpperCase()
and the second line (albeit it has more lines in itself it is seen as "one line") of code is:
"(00) PRE-PAID".equals(row.tCode) ?"00" :
"(01) C.O.D".equals(row.tCode) ?"01" :
"(02) EOM".equals(row.tCode) ? "02" :
"Unknown"
We need to put those two instructions together.
Solution
(row1.tCode != null && !row1.tCode.equals("")) ? (
"(00) PRE-PAID".equals(row.tCode.toUpperCase()) ? "00" :
"(01) C.O.D".equals(row.tCode.toUpperCase()) ? "01" :
"(02) EOM".equals(row.tCode.toUpperCase()) ? "02" :
"Unknown") : "Unknown"
Alternatively, to shorten the first row, you could set a Default value for tCode if that makes any sense. The default value could be "" and you don't have to check for null anymore. Again, this depends on your use case.
I keep getting compile errors when I try to write my rules.
I am trying to translate this condition into drools
if(model.type.series != null && model.type.series.name.mathes(".*FANR.*") ||
model.type.series.name.matches(".*SANA.*"))
//do something....
This is what I have...
rule "Rule 01" salience 0
when
m : model(type.series != null,
type.series.name.matches(".*FANR.*") ||
type.series.name.matches(".*SANA.*")
a : Result(state == Result.GOOD )
then
a.setState(RESULT.BAD);
....
end
What I was trying to do is to use regular expression to match the part of the string where the 'name' is String type. As I am fair new to drools I don't see where it can cause problems, any help would be appreciated
Use correct Drools syntax, according to the matches operator, as described in the Drools manual.
rule FANRorSANA
when
$n: model($v: type.series.name matches ".*(FANR|SANA).*")
then
And you can use the power of regular expressions for testing alternatives.
I want to evaluate facts dynamically using drool engine. Rule conditions attributes & their conditional operators are stored in database and load in to WM when engine start.
So I want to use that operator in rule file as below.
$dynCx : DynCustomer()
$attrib : Attribute() from $dynCx.attributes
$offer : Offer($ofCode : offer_code, $domainName : domainName )
$rdef : OfferRuleDef($entity : entity,
$code : code,
$value : value,
$atrName : attributeName,
$atrVal : attributeVal,
$op : operation,
$entity == $domainName,
$code == "OFFER_CODE",
$value == $ofCode,
$atrName == $attrib.name,
$atrVal $op $attrib.value
)
but I'm getting below error
Caused by: java.lang.RuntimeException: [59,14]: [ERR 102] Line 59:14 mismatched input '$op' in rule "Evaluate Generic Offer Eligibility"
[0,0]: Parser returned a null Package
How we can achieve this?
Operators can't be resolved at runtime, so you can't write that kind of rule. Based on the version you are using and the number of operators you envision, you have a few options.
1) Write a rule for each operator:
OfferRuleDef( ..., operator == "==", attributeVal == $attrib.value )
to avoid excessive code repetition, consider that rules can "extend" each other.
2) In recent versions, create a static helper function and pass the three values:
OfferRuleDef( ..., MyHelper.applyOperator($attrValue, $op, $attrib.value) )
In JavaCC, for example in state DEFAULT, I want to perform a state switch, if the next token is <A>, I want to switch to state STATE_A, otherwise, I want to switch to state STATE_B.
I tried to use something like the following code with "" as a wildcard:
TOKEN:
{
<A: "aa"> : STATE_A
| <NOT_A: ""> : STATE_B
}
But it doesn't work, when a character that cannot be reduced to A is met, the function returns immediately, and doesn't get switched to STATE_B, therefore "" doesn't seem to be able to do the job.
Do you have any suggestions? Thanks.
Well I find that this actually works.
The empty string will be matched when A cannot be matched, however we need to explicitly refer to NOT_A in the definitions of non-terminals. Therefore expressions like
[ <A> ]
should be rewritten as
( <A> | <NOT_A> )
to enforce a state switch.