Guide to Building Business Processes

     Previous  Next    Open TOC in new window  Open Index in new window  View as PDF - New Window  Get Adobe Reader - New Window
Content starts here

Looping Through Items in a List

A frequently designed pattern in your business processes is one that specifies the performance of a set of activities once for every iteration of the flow over a sequence of XML elements, retrieved from an XML document.

For Each nodes represent points in a business process at which a set of activities is performed repeatedly, once for each item in a list. For Each nodes includes an iterator node (on which a list of items is specified) and a loop (in which the activities to be performed for each item in the list are defined). An XML document (or a section of an XML document) is passed into the For Each loop in a business process variable. An iteration variable holds the current element being processed in the For Each loop, for the life of the loop.

This section describes how to add this looping logic to your business process. It includes the following topics:

Creating For Each Nodes in Your Business Process

To Add A For Each Node to Your Business Process
  1. On the Package Explorer pane, click the business process (Process.java file) you want to add the For Each node to.Your business process is displayed in the Design view.
  2. If the Node Palette is not visible in BEA Workshop for WebLogic Platform, choose WindowArrow symbolShow ViewArrow symbolNode Palette from the BEA Workshop for WebLogic Platform menu.
  3. Click XML Representation For Each in the Node Palette. Then drag and drop the For Each node onto the business process, placing it on the business process at the point in your business process at which you want to design a pattern in which a set of activities is performed repeatedly, once for each item in a list.
  4. The Design view is updated to contain a representation of the For Each node as shown in Figure 12-1.

    Figure 12-1 For Each Node


    For Each Node

    Note the following characteristics of the For Each node:

    • The For Each node represents a placeholder for one or more additional nodes, and a looping mechanism. The node builder, which is described in Designing For Each Nodes, allows you to easily select a sequence of nodes from a business process variable.
    • By default, the node is named For Each. You can change the name by double-clicking the name and entering the new name.
    • For Each Node indicates that the design of this node is incomplete. When you complete the design of the node, For Each Node is replaced by For Each Node. A For Each node is completed when the iterator has been specified in the node builder.

Designing For Each Nodes

Before you can add the logic that causes the iteration over a sequence of XML nodes in your business process, your project must contain an XML Schema or MFL file that defines the repeating XML or MFL element over which you want your business process to iterate. To learn how to import an XML Schema or MFL file into your project, see Creating and Importing Schema Files.

After importing an XML Schema or MFL file into your project, you can complete the design of the For Each node. It includes the following tasks:

To Select a Repeating XML or MFL Element Over Which to Iterate

The For Each node only iterates over repeating elements. The node builder allows you to select a repeating node from the variable you created in the preceding section.

  1. In the Design view, double-click the For Each node to invoke its node builder.
  2. Click Select Variable to select a variable that you have already created in your project or create a new variable to use in your decision node:
    1. If you want to use a variable that is already created, select the variable that contains the XML or typed non-XML on which you want to build the condition.
    2. For example, if we import an XML Schema (QuoteRequest.xsd) into our project, and create a business process variable (requestXML) of type quoteRequest (based on the QuoteRequest.xsd schema), the requestXML variable is available in the drop-down list of business process variables as shown in Figure 12-2.

      Figure 12-2 Select Variable


      Select Variable

      Note: To learn about creating business process variables and importing schemas to your project, see Business Process Variables and Data Types.
    3. If you want to create a new variable, select Create new variable... from the drop-down list.
    4. The Create Variable dialog box opens.

    5. Enter a name for your new variable in the Variable Name field.
    6. Select the XML or Non-XML option, depending on if your variable is based on a XML document or MFL file and select the appropriate variable type in the displayed list of type options.
    7. Click OK.
    8. The Create Variable dialog box closes and your new variable is displayed in the Select Node pane.

Arepresentation of the XML in the requestXML variable is displayed in the Select Node panel, see Figure 12-3.

Figure 12-3 XML Representation

XML Representation

Note the following characteristics of the QuoteRequest.xsd schema as displayed in the preceding figure:

  1. Select a repeating element in the Select Node field.
  2. The Repeating Element and Iteration Variable fields are populated with data:

    • Repeating Element—Contains an XPath expression, which when applied against the XML document associated with the XML variable, returns the set of repeating XML elements. Building on our example, if you select the repeating element widgetRequest in the Select Node panel, the Repeating Element field is populated with the following expression: $requestXML/ns0:widgetRequest. This expression returns all the widgetRequest elements in an XML document.
    • Iteration Variable—Contains the name of an iteration variable. An iteration variable is generated to hold the current element being processed in the For Each loop at run time. In our example, the iteration variable is named iter_forEach1, by default. You can change the name by entering a new name in the Iteration Variable field.
  3. Click Close, to close the node builder and return to the Design view.
  4. In the Design view, note that the icon in your For Each node displays the following graphics:

    • XML Representation is a visual reminder that the iteration variable you defined on this node is based on an XML element.
    • XML Representation is a visual reminder that the iteration variable you defined on this node is based on a MFL or typed non-XML element.
  5. To save your work, select FileArrow symbolSave.
To Add Activities to the For Each Node

You must define the activity or set of activities that are performed for each item in the list you created in the preceding step ( To Select a Repeating XML or MFL Element Over Which to Iterate). Each iteration of the For Each loop executes the activity or activities you specify in a node (or nodes) in the loop.

  1. In the Node Palette, click a node that represents the logic you want to add to the business process.
  2. Drag and drop the node from the Node Palette onto the business process in the Design view, placing it on the business process within the For Each loop.
  3. As you drag a node onto the For Each loop, a target XML Representation appears on the loop, representing a valid location in the For Each loop where you can place the node. As you drag the node near the valid location, the target is activated XML Representation and the cursor changes to an arrow XML Representation. You can release the mouse button and the node snaps to the For Each loop.

  4. On the node (or nodes) you add to the For Each loop, create the activities appropriate for your business process's business logic.

Related Topics

Creating Looping Logic

Grouping Nodes in Your Business Process

Handling Exceptions

Adding Message Paths

Adding Timeout Paths


  Back to Top       Previous  Next