# Adding Nested If-Then-Else Expressions with Complex Conditions to a Link

The following example shows adding nested if-then-else expressions to a link to supply the appropriate supplier id for an incoming purchase order. In addition this section shows how you can use functions in if-then-else expressions.

In this example, if partId is between 0 and 200, use the Supplier with the id of 12453. If the partId is between 201 and 400, the Supplier id is dependent on the quantity requested. If the quantity requested is over the maximum volume for one Supplier (id: 35667) use the high volume Supplier (id: 20045.) In addition, this example appends the source countryCode to the supplier id code using the concat function call as shown in the following figure.

Figure : Complex If-Then-Else

To Create the If-Then-Else Example with Complex Conditions

1. Open a query in the mapper pane. For instructions see General Steps to Open or Create a Query in the Mapper.

For this example, import the files: PurchaseOrder.xsd and Supplier.xsd files (step 2 in General Steps to Open or Create a Query in the Mapper.) If you installed WebLogic Platform in the c:\bea directory, import these files from the c:\bea\weblogic81\workshop\help\doc\en\integration\reffiles\transform\ifThenElse directory.

For this example, select and add the PurchaseOrder.xsd/PurchaseOrder node as an source type (step 4 in General Steps to Open or Create a Query in the Mapper.)

For this example, select and add the Supplier.xsd/Supplier node as an target type (step 4 in General Steps to Open or Create a Query in the Mapper.)

2. In the Design View, create a link by dragging-and-dropping the PurchaseOrderDoc/countryCode Source element over the Supplier/id Target node.

Keep the link selected for the next step.

3. Select the Target Expression tab. (If the Target Expression tab is not visible in WebLogic Workshop, choose View —> Windows —> Target Expression from the menu bar.)
4. Select the If Then Else option.

An XQuery if-then-else construct is added to the link.

5. Click If Condition.

The If Condition pane appears.

6. Add the first condition to the if section of the if-then-else:

1. From the Source pane, drag the PurchaseOrderDoc/partId element into the Left Hand Expression section of the If Condition pane.
2. Select the operator: >.
3. In the Right Hand Expression section of the If Condition pane, enter: 0 and click Add.

The first condition is added to the if section of the if-then-else.

7. Construct the second condition to the if section of the if-then-else:

1. From the Source pane, drag-and-drop the PurchaseOrderDoc/partId element into the Left Hand Expression section of the If Condition pane.
2. Select the operator: <=.
3. In the Right Hand Expression section of the If Condition pane enter: 200.

8. From the Join Type field select the AND option.

The Join Type determines how the conditions that make up if section of the if-then-else are evaluated during run time.

9. Click Add.

The second condition is added to the if section of the if-then-else.

During run time, for example if partId is equal to 100, the if condition is evaluated as described here:

- The first condition (partId>0) evaluates to true.
- The second condition (partId<=200) evaluates to true resulting in the if condition: (true and true).
- The if condition: (true and true) evaluates to true resulting in execution of the then expression.

10. Click Then Expression.

The contents of the Then Expression are selected.

Keep the text selected for the next step.

11. Click the Delete key.

The text in the Then Expression pane is deleted.

12. Create the then expression:

1. Select the Palette. (If the Palette is not visible in WebLogic Workshop, choose View —> Windows —> Palette from the menu bar.)
2. In the Palette, expand String Functions.
3. Select the concat function and drag it into the Then Expression pane. Leave the first $string-var argument selected for the next step.
4. From the Source pane, drag-and-drop the PurchaseOrderDoc/countryCode element over the first $string-var argument of the concat function in the Then Expression pane.
5. Select the text: $string-var and click the Delete key. Leave the cursor in the current location.
6. Enter: "-12453".

The $string-var argument is replaced by the string: "-12453".

7. Click Apply.

The following XQuery code of the then expression is saved:

    xf:concat($PurchaseOrderDoc/countryCode,
    	"-12453")

During run time, when the if condition evaluates to true, the then expression is evaluated. When the then expression created in this step is evaluated, it concatenates the countryCode with supplier id of 12453 and returns this value as the Target Supplier/id.

13. In this step, you add a nested if-then-else to the else.

The nested if-then-else added in this step, tests if the supplied source partId is between 201 and 400. For partIds in this range, the returned Supplier id is dependent on the quantity requested. If the quantity requested is over the maximum volume for one Supplier (id: 20045) the high volume Supplier (id: 35667) is returned.

To add the nested if-then-else to the else expression:

1. Select the down arrow to the right of Else Expression, and from the drop-down menu select Insert Nested If-Then-Else.

A nested if-then-else is added to the else and Else becomes Else If Condition.

2. Add the first condition to the if section of the second level if-then-else:

From the Source pane, drag-and-drop the PurchaseOrderDoc/partId element into the Left Hand Expression section of the If Condition pane.

Select the operator: >.

In the Right Hand Expression section of the Else If Condition pane, enter: 200 and click Add.

The first condition is added to the if section of the second level if-then-else.

3. Construct the second condition to the if section of the second level if-then-else:

From the Source pane, drag the PurchaseOrderDoc/partId element into the Left Hand Expression section of the Else If Condition pane.

Select the operator: <=.

In the Right Hand Expression section of the Else If Condition pane, enter: 400.

4. From the Join Type field, select the AND option.

The selected Join Type determines how the conditions that make up if section of the if-then-else are evaluated during run time.

5. Click Add.

The second condition is added to the if section of the second level if-then-else.

14. Select Else If Condition of the second level if-then-else to collapse the Else If Condition pane.

In the Target Expression tab the following is displayed as shown in the following figure.

15. In this step, you add another nested if-then-else to the then of the second level if-then-else. This if-then-else determines if the quantity requested is over the maximum volume for one Supplier (id: 20045) return the high volume Supplier (id: 35667).

To add the third if-then-else to the then expression:

1. Select the down arrow to the right of the second Then Expression at the bottom of the pane, and from the drop-down menu select Insert Nested If-Then-Else as shown in the following figure.

A nested third level if-then-else is added to the then and Then Expression becomes Then If Condition.

2. Add a condition to the if section of the third level if-then-else:

From the Source pane, drag the PurchaseOrderDoc/quantity element into the Left Hand Expression section of the Then If Condition pane as in the following figure.

Select the operator: >.

Select the Palette. (If the Palette is not visible in WebLogic Workshop, choose View —> Windows —> Palette from the menu bar.)

In the Palette, expand Type Functions.

Select the decimal function and drag-and-drop it into the Right Hand Expression section of the Then If Condition pane. Leave the first $string-var argument selected for the next step.

From the Source pane, drag the PurchaseOrderDoc/maxVolumeForSupplier element over the first $string-var argument of the decimal function in the Right Hand Expression section of the Then If Condition pane and click Add.

A condition is added to the if section of the third level if-then-else.

Warning: You must explicitly cast the PurchaseOrderDoc/maxVolumeForSupplier element as a decimal to force the comparison between the Left Hand Expression and the Right Hand Expression to be a numerical comparison and not a lexicographical one.

16. Create the then expression of the third level if-then-else:

1. Select the Then Expression of the third level if-then-else.

The following is displayed in the Target Expression tab.

2. Select the Palette. (If the Palette is not visible in WebLogic Workshop, choose View —> Windows —> Palette from the menu bar.)
3. In the Palette, expand String Functions.
4. Select the concat function and drag it into the Then Expression pane. Leave the first $string-var argument selected for the next step.
5. From the Source pane, drag-and-drop the PurchaseOrderDoc/countryCode element over the first $string-var argument of the concat function in the Then Expression pane.
6. Select the text: $string-var and click the Delete key. Leave the cursor in the current location.
7. Enter: "-20045".

The $string-var argument is replaced by "-20045".

8. Click Apply.

The XQuery code of the then expression of the third level if-then-else is saved.

During run time, when the if condition evaluates to true, the then expression is evaluated. If the then expression created in this step is evaluated, it concatenates the countryCode with supplier id of 20045 and returns this value as the Target Supplier/id.

17. Create the else expression of the third level if-then-else:

1. Select the Else Expression of the third level if-then-else.

The following is displayed in the Target Expression tab.

2. Select the Palette. (If the Palette is not visible in WebLogic Workshop, choose View —> Windows —> Palette from the menu bar.)
3. In the Palette, expand String Functions.
4. Select the concat function and drag it into the Then Expression pane. Leave the first $string-var argument selected for the next step.
5. From the Source pane, drag-and-drop the PurchaseOrderDoc/countryCode element over the first $string-var argument of the concat function in the Then Expression pane.
6. Select the text: $string-var and click the Delete key. Leave the cursor in the current location.
7. Enter: "-35667".

The $string-var argument is replaced by "-35667".

8. Click Apply.

The XQuery code of the else expression of the third level if-then-else is saved.

During run time, when the if condition evaluates to false, the else expression is evaluated. If the else expression created in this step is evaluated, it concatenates the countryCode with supplier id of 35667 and returns this value as the Target Supplier/id.

18. Create the else expression of the second level if-then-else:

1. Select the Else Expression of the second level if-then-else.

The following is displayed in the Target Expression tab.

2. In the Palette, expand String Functions.
3. Select the concat function and drag it into the Then Expression pane. Leave the first $string-var argument selected for the next step.
4. From the Source pane, drag-and-drop the Purchase Order Doc/contracted element over the first $string-var argument of the concat function in the Then Expression pane.
5. Select the text: $string-var and click the Delete key. Leave the cursor in the current location.
6. Enter: "-56898".

The $string-var argument is replaced by "-56898".

7. Click Apply.

19. Select the General option, to view the generated XQuery source code. The following XQuery code is displayed:

    if ((data($PurchaseOrderDoc/partId) > 0
    and data($PurchaseOrderDoc/partId)<= 200)) then
    	xf:concat($PurchaseOrderDoc/countryCode,
    		"-12353")
    else
    	if ((data($PurchaseOrderDoc/partId) > 200
    	and data($PurchaseOrderDoc/partId) <= 400)) then
    		if (data($PurchaseOrderDoc/quantity) >
    		xs:decimal(data($PurchaseOrderDoc/maxVolumeForSupplier))) then
    			xf:concat($PurchaseOrderDoc/countryCode,
    				"-20045")
    		else
    			xf:concat($PurchaseOrderDoc/countryCode,
    				"-35667")
    	else
    		xf:concat($PurchaseOrderDoc/countryCode,
    			"-56898")

20. To run the query, select the Test View of the XQ file.
21. Import the file: PurchaseOrder.xml. If you installed WebLogic Platform in the c:\bea directory, import this file from the c:\bea\weblogic81\workshop\help\doc\en\integration\reffiles\transform\ifThenElse\XML directory.

Note: You can cut and past directory paths into the Name field of the Open File to Test pane to jump to directory locations. If you installed WebLogic Platform in the c:\bea directory, you can jump to the directory that contains the XML files for this example, by pasting the following directory path into the Name field: c:\bea\weblogic81\workshop\help\doc\en\integration\reffiles\transform\ifThenElse\XML and then pressing enter.

22. In the Result Data pane, click Test.

If not currently running, the WebLogic Server for the current application will be started. In order for a query to run, the WebLogic Server for the current application must be running.

In the Result Data pane, a graphical representation of the target data is displayed.

In the Result Data pane, the resulting value of Supplier/id element is UK-35667. The specified source partId (345) is between 200 and 400 and the specified source quantity (678) is less than the specified source maxVolumeForSupplier (1000), so the else expression: xf:concat($PurchaseOrderDoc/countryCode, "-", xf:string(35667)) is evaluated (shown in bold in the preceeding XQuery code listing), resulting in the string: UK-35667 being returned as the value of the Supplier/id element as shown in the following resulting XML data:

    <?xml version="1.0" encoding="UTF-8"?>
    <Supplier>
    	<id>UK-35667</id>
    </Supplier>

23. Save the DTF and the XQ file. From the menu bar, choose File —> Save All.