Some 'more, in-depth' information on Oracle BPEL PM, ESB and other SOA, day2day things

Sunday, March 26, 2006

BPEL: looping over arrays (collections) by hand

Over the last days I have seen some questions on hand-looping over arrays.
A (long) while ago, Marc, a colleaque from Europe sent me a nice example. So I thought to publish it here. I hope I comply with copyrights here :-0

Imagine you get a collection of emps back from a service, as defined by the following schema (from DB adapter in this case)

 
<xs:element name="EmpCollection"
type="EmpCollection"/>
<xs:element name="Emp" type="Emp"/>
<xs:complexType name="EmpCollection">
<xs:sequence>
<xs:element name="Emp" type="Emp"
minOccurs="0"
maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Emp">
<xs:sequence>
<xs:element name="comm" type="xs:decimal"/>
<xs:element name="deptno" type="xs:decimal"/>
<xs:element name="empno" type="xs:decimal"/>
<xs:element name="ename" type="xs:string"/>
<xs:element name="hiredate" type="xs:dateTime"/>
<xs:element name="job" type="xs:string"/>
<xs:element name="mgr" type="xs:decimal"/>
<xs:element name="sal" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>


and accordingly a message type

<message name="EmpCollection_msg">
<part name="EmpCollection"
element="top:EmpCollection"/>
</message>

The first step is to create a variable, based on the msg type that contains this collection, and 2 counters, one (i) for the running index, and one (n) for the length of the collection. All as shown below ..

<variable name="EmpQuerySelect_p_deptno_OutputVariable"
messageType="ns1:EmpCollection_msg"/>
<variable name="i" type="ns3:integer"/>
<variable name="n" type="ns3:integer"/>

Step 2, consists of getting the count of nodes and assigning 1 to the counter

<assign name="prepare_loop">
<copy>
<from expression="number(1)"/>
<to variable="i"/>
</copy>
<copy>
<from expression="ora:countNodes(
'EmpQuerySelect_p_deptno_OutputVariable',
'EmpCollection','/ns2:EmpCollection/Emp')"
/>
<to variable="n"/>
</copy>
</assign>


Step 3, is the while loop and to get the information from a selected node, by applying the concepts further

<while name="While_1"
condition="bpws:getVariableData('i') <=
bpws:getVariableData('n')">
<scope name="Scope_1">
<variables>
<variable name="selector" type="ns3:string"/>
<variable name="element" element="ns2:Emp"/>
</variables>
<sequence name="Sequence_1">
<assign name="Get_Element">
<copy>
<from expression="concat(
"/ns2:EmpCollection/Emp[",
string(bpws:getVariableData('i')),
"]")"/>
<to variable="selector"/>
</copy>
<copy>
<from expression="bpws:getVariableData
('EmpQuerySelect_p_deptno_OutputVariable',
'EmpCollection',
bpws:getVariableData('selector'))"
/>
<to variable="element" query="/ns2:Emp"/>
</copy>
</assign>
<empty name="Do_stuff_with_element_Var"/>
<assign name="Increment_index">
<copy>
<from expression="bpws:getVariableData('i')+1"/>
<to variable="i"/>
</copy>
</assign>
</sequence>
</scope>
</while>

and voila - a loop over your collection is done :-)

Having questions? send your feedback on collections here

0 Comments:

Post a Comment

<< Home