Sunday, September 21, 2008

Arrays in BPEL

Why it made itself into my Project?

What else but FlowN. That was the culprit.
FlowN will use index to create separate flows for each of the parallel flows using the index variable.
And the index was used to read data from where else but array.

How to create a array?

Actually it is not as complex as I originally though it to be.
Array in XML can be any element that can store multiple repetition of some data.

For example if A represents sequence of element B then A can be used as array for B.

What is that I am going to show you below?

In short this is what I am attempting to show you.

1. Add elements into the array using BPEL append. 
Most of the cases people already have a array from the source system.
Use this when you want to build your ow array i the fly.

2. Build the XPath 
If you have decided to use array then build a dynamic XPath.
Store that in a temporary variable.
And use that XPath temporary variable while reading elements from the array.
If you ask me what type of variable, I prefer String type.

2. Read data from array 
Just as plain as use the temporary XPath variable and read data from the array element.
Just like saying A[X] i ay other lang. Here A is the array and X is the temporary XPath.
Movig in rounds to read like A[1], A[2] etc.

How to add value into the array?

What else but use append. I you have not seen this, it looks like a + in the assign activity. Just see an assign.

Behind the scenes the bpel code looks like:

<assign name="Assign_CDM_Contents_Append">
<bpelx:append>
<bpelx:from variable="Variable_Add_Array_Element"
query="/ns1:ABC"/>
<bpelx:to variable="Variable_Array"
part="payload"
query="/ns1:ArrayList"/>
</bpelx:append>
</assign>


Here Variable_Array is the variable to hold the array of Variable_Add_Array_Element
I am trying to add elements into the Array (Variable_Array).
I am doing this by appending the elements to be end of it.

Whatz with XPath?

There are 2 options to read data from XML arrays.
One is to use another XPath variable to build the dynamic XPath query.
Second option is to not use a new variable but dynamically insert the XPath in the bpws:getVariableData()

I prefer the first option as it gives a little clarity over the second option.

Why Dynamically build XPath?

Well what ever the case be you need to build XPath dynamically.
But why? Just to use the changing index into the Array.

How to Read data?

If you have a running index over either a flowN or a while loop that does this for you, either way you need the index to access the array.

Now the way to read is through dynamic XPath. That need to be built.
Here I have used option 1.

The below code shows how to use assign to build the dynamic XPath.

<assign name="Assign_XPath_For_Array">
<copy>
<from expression="concat('/ns1:ArrayList/ns2:ArrayElem[',bpws:getVariableData('Variable_Index_Counter'),']') "/>
<to variable="Variable_Xpath"/>
</copy>
</assing>


Note have used [index] to represent index element here in the array.
Here Variable_Xpath acts as a temporary string variable that holds the intermediate Xpath as string.

Now use this XPath string Variable while reading array index elements.
This would look like
<assign name="Assign_Read_Array">
<copy>
<from expression="bpws:getVariableData('Variable_Array','payload',bpws:getVariableData('Variable_Xpath'))"/>
<to variable="Variable_Read_Array_Element"
query="/ns1: ABC"/>
</copy>
</assign>


How to do with out another variable?

Just do not use the new variable. Build your XPath dynamically right in the bpws:getVariableData() 3rd parameter.

No comments: