If else conditional assign in Assign or Script Activity of SOA or BPM
Note: It's not a complete solution. however, you can use this method in most of the cases.
Use Case:
Assume an input string which can have value "Submit" or "Cancel" and based on this you want to output string to be either "APP" or "REJ"
if input_arg = "Submit"
Then
output_arg = "APP"
Else
output_arg = "REJ"
General way of giving a solution for this to write an XSLT or use switch/exclusive gateway and have 2 Assign/Script tasks to assign the appropriate value to the output element.
Here in this example, I will use "translate" function to achieve the same without gateways and in a single Assign/Script Task.
The xpath expression used here is:
translate(string(contains(bpmn:getDataObject('projectDataObject1'),"Submit")),"trufalse","APPREJ")
Lemme split the function and explain it in detail.
1. contains(bpmn:getDataObject('projectDataObject1'),"Submit") : This is obvious, checking whether the input argument has the string "Submit". If yes, the function returns "true" else it returns "false"
2. string(contains(bpmn:getDataObject('projectDataObject1'),"Submit")) : Converting the above returned Boolean "true or false" into a string.
3. translate(string(contains(bpmn:getDataObject('projectDataObject1'),"Submit")),"trufalse","APPREJ") : This is the important part, translate function does replacement each character by character.
So, in this above statement
string(contains(bpmn:getDataObject('projectDataObject1'),"Submit")) is the input argument to "translate" function
"trufalse" Indicates what characters to be replaced. This is the 2nd argument.
"APPREJ" Indicates with which characters they need to be replaced. This is the 3rd argument to the function.
"t" would be replaced with "A"
"r" would be replaced with "P"
"u" would be replaced with "P"
"f" would be replaced with "R"
so... on
The last letter "e" doesn't have the corresponding replacement element. So, that will be ignored.
Even "true" has "e" in it, but I didn't mention it explicitly, since we need to mention each character only once.
So, finally summing up, based on the input argument containing "Submit" or not, the output argument will be updated to "APP" or "REJ".
This might be little confusing :-)
To help you understand more, here are few examples on how the translate function works:
translate("AABBCDBA","ABCD","XYZW") : AABBCD is the input and output would be XXYYZWYX
Each character is replaced appropriately.
translate("ABC123DDC","ABCD32154","abcd00000") : ABC123DDC is the input here and output would be abc000ddc
You can have pretty complicated xpath's to achieve nested if's too. Example here:
translate(string(not(
contains(bpmn:getDataObject('applicationDataDO')/ns:ApplicationStatus,'REJ'))),
'trufalse',
concat(translate(string(
contains(bpmn:getDataOutput('outcome'),'REJECT')),'trufalse','REJSUB'),'REJ'))
You can literally replace any character with any character. Just make sure what you are doing. :-)
Oracle BPM 12c supported much easier way of nested if's in Script tasks, I will post on a blog in it soon. Keep watching !
Uploaded a sample project here: https://drive.google.com/folderview?id=0B-B6k-1tHlm4SlY4VGpFNzJOSm8&usp=sharing
Shoot me in the comments, if you need any further help.
translate("ABC123DDC","ABCD32154","abcd00000") : ABC123DDC is the input here and output would be abc000dc
ReplyDeletei cannot understand that result. should be abc000ddc (double "d")?
Sorry, my bad. It's double "d" indeed.
DeleteThanks !