Custom Search SOABYTE here

Wednesday, June 23, 2010

Looping over collection (Array) in Oracle BPEL

Imagine you get a collection of books back from a service, as defined by the following schema.

   <xs:element name="BooksCollection"   type="BooksCollection"/>
   <xs:element name="Book" type="Book"/>
   <xs:complexType name="BooksCollection">
         <xs:element name="Book" type="Book"   minOccurs="0"  maxOccurs="unbounded"/>
   <xs:complexType name="Book">
         <xs:element name="Name" type="xs:string"/>
         <xs:element name="Author" type="xs:string"/>
         <xs:element name="Cost" type="xs:decimal"/>
         <xs:element name="Publisher" type="xs:string"/>

And accordingly a message type

    <message name="BooksCollection_msg">
        <part name="BooksCollection"

Step 1

The first step is to create a variable based on the above message type that contains this collection <Which may already be created in the code>

<variable name="BooksCollection_OutputVariable"  messageType="ns1:BooksCollection_msg"/>

And  2 counters, one (i) for the running index, and one (n) for the length of the collection.

 <variable name="i" type="ns3:integer"/>
<variable name="n" type="ns3:integer"/>

Step 2

Get the count of nodes and initialize the counter <i> with value 1.

    <assign name="prepare_loop">
        <from expression="number(1)"/>
        <to variable="i"/>
        <from expression="ora:countNodes(
        <to variable="n"/>

Step 3

Comes the while loop to loop over Collection and to get the information from a selected node.


<while name="Loop_Over_Collection"
              condition="bpws:getVariableData('i') <= bpws:getVariableData('n')">
      <scope name="Scope_1">
          <variable name="selectorVar" type="ns3:string"/>
          <variable name="Book_Element" element="ns2:Book"/>
        <sequence name="Sequence_1">
          <assign name="Get_Book_Element">
              <from expression="concat(
              <to variable="selectorVar"/>
              <from expression="bpws:getVariableData
              <to variable="Book_Element" query="/ns2:Book"/>
          <empty name="Logic_For_Book_Element_Comes_Here"/>
          <assign name="Increment_index">
              <from expression="bpws:getVariableData('i')+1"/>
              <to variable="i"/>
            This is the way to loop over a collection message.


Post a Comment

Blogger Profile