bea.com | products | dev2dev | support | askBEA |
![]() |
![]() |
|
![]() |
e-docs > Liquid Data for WebLogic > Building Queries and Data Views > Example 6: Complex Parameter Type (CPT) |
Building Queries and Data Views
|
Example 6: Complex Parameter Type (CPT)
The Complex Parameter Type Cookbook example shows how to use Liquid Data to create an integrated view that connects two enterprise information systems: a database and an in-flight XML data source using a complex parameter type (CPT). A query that uses both data sources determines whether the customer has sufficient credit for the incoming order to be processed.
The company receives dozens of electronically transmitted orders daily and needs to quickly respond to its field office if an order cannot be accepted because a customer has exceeded their credit limit. The credit limit and amount of outstanding orders is known to the system. The quantity and price of the items being ordered is supplied in real-time along with the order.
The company develops a complex parameter type (CPT) that models the incoming purchase order as an XML schema and sets a simple orderLimit parameter that an operator can change whenever the query is run. The query also calculates the total amount outstanding of current orders and the total amount of the incoming order. The objective is to accept orders if the total amount of both outstanding and incoming orders is within the order limit. Otherwise, the order is rejected.
To recreate the solution, follow these steps:
Note: The implementation details of the Complex Parameter Type demo, the DB-COCPT sample, and the CPT cookbook example vary slightly.
Complex Parameter Type (CPT) Demo. If you are looking at this documentation online, you can click the "Demo" button to see a viewlet demo showing how to build the conditions and create the mappings described in this example.
Ex 6: Step 1. Verify the Availability of Schemas and Sample Data Stream
In creating the DB-CPTCO sample query, we use the following files that are installed with Liquid Data samples. (See Example directories for information on how example directory names are used.)
From the Liquid Data samples repository schema directory:
ld_repository/schemas/broadbandp.sql
ld_repository/schemas/coCptSample2.xsd
ld_repository/schemas/COCPTSampleTarget-Schema.xsd
ld_repository/xml_files/coCPTsample2.xml
If you want to refer to the sample DB-CPTCO project, it is installed as the following file:
<BEA_HOME>/liquiddata/samples/buildQuery/db-cptco/coCPTSample.qpr
For reference purposes, code listings for several of the XML files used in this example appear below:
Listing 9-17 DB-CPTCO Sample CPT Schema (coCptSample2.xsd)
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema targetNamespace="urn:schemas-bea-com:ld-cocpt"
xmlns:cocpt="urn:schemas-bea-com:ld-cocpt"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="CustOrder">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="CUSTOMER_ORDER" minOccurs="0" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="CUSTOMER_ID" type="xsd:string"/>
<xsd:element name=
"NEW_ORDER_LINE_ITEM"type="cocpt:NEW_ORDER_LINE_ITEMType"
minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:complexType name="NEW_ORDER_LINE_ITEMType">
<xsd:sequence>
<xsd:element name="PRODUCT_NAME" type="xsd:string"/>
<xsd:element name="QUANTITY" type="xsd:decimal"/>
<xsd:element name="PRICE" type="xsd:decimal"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
Listing 9-18 DB-CPTCO Target Schema (COCPTSampleTargetSchema.xsd)
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema targetNamespace="urn:schemas-bea-com:ld-cocpt" xmlns:cocpt="urn:schemas-bea-com:ld-cocpt" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="CustOrder">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="CUSTOMER_ORDER" minOccurs="0" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="CUSTOMER_ID" type="xsd:string"/>
<xsd:element name="NEW_ORDER_LINE_ITEM" type="cocpt:NEW_ORDER_LINE_ITEMType" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:complexType name="NEW_ORDER_LINE_ITEMType">
<xsd:sequence>
<xsd:element name="PRODUCT_NAME" type="xsd:string"/>
<xsd:element name="QUANTITY" type="xsd:decimal"/>
<xsd:element name="PRICE" type="xsd:decimal"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
Listing 9-19 DB-CPTCO Sample XML Data Stream (coCptSample2.xml)
<?xml version="1.0" encoding="UTF-8"?>
<cocpt:CustOrder xmlns:cocpt="urn:schemas-bea-com:ld-cocpt"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:schemas-bea-com:ld-cocpt
coCptSample2.xsd">
<CUSTOMER_ORDER>
<CUSTOMER_ID>CUSTOMER_1</CUSTOMER_ID>
<NEW_ORDER_LINE_ITEM>
<PRODUCT_NAME>RBBC01</PRODUCT_NAME>
<QUANTITY>1000</QUANTITY>
<PRICE>20</PRICE>
</NEW_ORDER_LINE_ITEM>
<NEW_ORDER_LINE_ITEM>
<PRODUCT_NAME>CS2610</PRODUCT_NAME>
<QUANTITY>1000</QUANTITY>
<PRICE>20</PRICE>
</NEW_ORDER_LINE_ITEM>
</CUSTOMER_ORDER>
</cocpt:CustOrder>
You may also want to examine the CO-CPTSAMPLE definition in the WLS Administration Console for the Samples server you are using.
See the section Creating a Complex Parameter Type for details.
Ex 6: Step 2. Open the Target Schema and CO-CPTSAMPLE CPT
ld_repository/schemas/COCPTSampleTargetSchema.xsd
Ex: 6: Step 3. Create an orderLimit Query Parameter
Since credit limits vary from customer to customer, it is convenient to have an order limit query parameter that can be changed whenever a query is run.
Ex 6: Step 4. Save the Project
You can save a project at any time. To initially create a project, use File—>Save Project As. Use the file browser to choose a location and project name (we use myCoCPT).
Ex 6: Step 5. Test Access to the Complex Parameter Source
Follow these steps to verify access to the CPT data source:
ld_repository/xml_files/coCptSample2.xml
Listing 9-20 Interim Results (1) from CPT Example Query
<cocpt:CustOrderStatus xmlns:cocpt="urn:schemas-bea-com:ld-cocpt">
<CUSTOMER>
<CUSTOMER_ID>CUSTOMER_1</CUSTOMER_ID>
<CUSTOMER_ORDER>
<TOTAL_OPEN_ORDERLIMIT>200000</TOTAL_OPEN_ORDERLIMIT>
</CUSTOMER_ORDER>
</CUSTOMER>
</cocpt:CustOrderStatus>
In the case of this data source, the customer identification is provided so there is no need to create a customer ID query parameter.
Ex 6: Step 6: Determine the Total Amount of New Orders
Since all runtime source items from a CPT are treated as character strings, any data items from the CO-CPTSAMPLE data source must first be cast appropriately. Then quantities and prices are multiplied together. The sum of the products of quantity and prices is the total amount of new orders.
Figure 9-5 Functions Used to Calculate Total New Orders in Data Stream
The only query construction components we will reuse are sum and CO-CPTSAMPLE source. The others can be closed or minimized.
Ex 6: Step 7. Create the Necessary Joins and Mappings to the Target Schema
Move the PB-BB relational source schema onto the design area.
Create the necessary joins to allow us to fetch the line items for a particular order for a particular customer.
Figure 9-6 Setting Joins Between CPT and Relational Data Source
Next, project FIRST_NAME and LAST_NAME elements onto the target schema.
Note: In this version of the CO-CPTSAMPLE, automatic scoping is used. See Understanding Scope in Basic and Advanced Views for more information.
Although your query is not complete, you can test run it again.
Listing 9-21 Interim Results (2) from CPT Example Query
<cocpt:CustOrderStatus xmlns:cocpt="urn:schemas-bea-com:ld-cocpt">
<CUSTOMER>
<FIRST_NAME>JOHN_B_1</FIRST_NAME>
<LAST_NAME>KAY_1</LAST_NAME>
<CUSTOMER_ID>CUSTOMER_1</CUSTOMER_ID>
<CUSTOMER_ORDER>
<NEWORDER_TOTAL_AMOUNT>40000</NEWORDER_TOTAL_AMOUNT>
<TOTAL_OPEN_ORDERLIMIT>200000</TOTAL_OPEN_ORDERLIMIT>
</CUSTOMER_ORDER>
</CUSTOMER>
</cocpt:CustOrderStatus>
Ex 6: Step 8. Determine the Amount of Currently Open Orders
Follow these steps to find the total amount of open orders in the sample PB-BB database:
Finally, we need to restrict results to open orders:
Ex 6: Step 9: Determine the Total Amount of All Open and New Orders
Ex 6: Step 10: Test If Open Orders + New Orders Exceeds the Order Limit
Ex 6: Step 11: Determine If the Order is Accepted or Rejected
Now that the relationships and conditions are established, set up an if-then-else test to solve the business problem. See The Problem.
Ex 6: Step 12: View the XQuery
The generated XQuery is shown in the following code listing.
Listing 9-22 XQuery for Example 6: Complex Parameter Type (CPT)
{-- Generated by Data View Builder 1.1 --}
namespace cocpt = "urn:schemas-bea-com:ld-cocpt"
<cocpt:CustOrderStatus>
{
for $CO_CPTSAMPLE.CUSTOMER_ORDER_2 in ($#CO-CPTSAMPLE of type element cocpt:CustOrder)/CUSTOMER_ORDER
for $MyBroadBand_LD_DS.CUSTOMER_3 in document("MyBroadBand-LD-DS")/db/CUSTOMER
let $srcval_4 :=
for $MyBroadBand_LD_DS.CUSTOMER_ORDER_LINE_ITEM_5 in document("MyBroadBand-LD-DS")/db/CUSTOMER_ORDER_LINE_ITEM
let $srcval_9 :=
for $MyBroadBand_LD_DS.CUSTOMER_ORDER_10 in document("MyBroadBand-LD-DS")/db/CUSTOMER_ORDER
where ($MyBroadBand_LD_DS.CUSTOMER_ORDER_10/ORDER_ID eq $MyBroadBand_LD_DS.CUSTOMER_ORDER_LINE_ITEM_5/ORDER_ID)
and ($MyBroadBand_LD_DS.CUSTOMER_3/CUSTOMER_ID eq $MyBroadBand_LD_DS.CUSTOMER_ORDER_10/CUSTOMER_ID)
return
xf:true()
where xf:not(xf:empty($srcval_9))
and ("OPEN" eq $MyBroadBand_LD_DS.CUSTOMER_ORDER_LINE_ITEM_5/STATUS)
return
$MyBroadBand_LD_DS.CUSTOMER_ORDER_LINE_ITEM_5/QUANTITY * $MyBroadBand_LD_DS.CUSTOMER_ORDER_LINE_ITEM_5/PRICE
let $xf:sum2_11 := xf:sum($srcval_4)
let $srcval_12 :=
for $CO_CPTSAMPLE.NEW_ORDER_LINE_ITEM_14 in $CO_CPTSAMPLE.CUSTOMER_ORDER_2/NEW_ORDER_LINE_ITEM
let $cast_as_xs:decimal2_17 := cast as xs:decimal($CO_CPTSAMPLE.NEW_ORDER_LINE_ITEM_14/QUANTITY)
let $cast_as_xs:decimal_20 := cast as xs:decimal($CO_CPTSAMPLE.NEW_ORDER_LINE_ITEM_14/PRICE)
return
$cast_as_xs:decimal2_17 * $cast_as_xs:decimal_20
let $xf:sum_22 := xf:sum($srcval_12)
let $v_23 := $xf:sum2_11 + $xf:sum_22
let $gt_24 := $v_23 gt $#orderLimit of type xs:decimal
where ($CO_CPTSAMPLE.CUSTOMER_ORDER_2/CUSTOMER_ID eq $MyBroadBand_LD_DS.CUSTOMER_3/CUSTOMER_ID)
return
<CUSTOMER>
<FIRST_NAME>{ xf:data($MyBroadBand_LD_DS.CUSTOMER_3/FIRST_NAME) }</FIRST_NAME>
<LAST_NAME>{ xf:data($MyBroadBand_LD_DS.CUSTOMER_3/LAST_NAME) }</LAST_NAME>
<CUSTOMER_ID>{ xf:data($CO_CPTSAMPLE.CUSTOMER_ORDER_2/CUSTOMER_ID) }</CUSTOMER_ID>
<CUSTOMER_ORDER>
<OPENORDER_TOTAL_AMOUNT>{ cast as xs:decimal($xf:sum2_11) }</OPENORDER_TOTAL_AMOUNT>
<NEWORDER_TOTAL_AMOUNT>{ cast as xs:decimal($xf:sum_22) }</NEWORDER_TOTAL_AMOUNT>
<TOTAL_OPEN_ORDERLIMIT>{ $#orderLimit of type xs:decimal }</TOTAL_OPEN_ORDERLIMIT>
<ORDER_REVIEW_STATUS>{ cast as xs:string(xfext:if-then-else( treat as xs:boolean($gt_24), "REJECT", "ACCEPT")) }</ORDER_REVIEW_STATUS>
</CUSTOMER_ORDER>
</CUSTOMER>
}
</cocpt:CustOrderStatus>
Ex 6: Step 13. Run the XQuery to Verify the Result
When you run this query on the sample data sources as described in this example, the result is an accepted order.
Listing 9-23 Result of Example 6: Complex Parameter Type (CPT)
<cocpt:CustOrderStatus xmlns:cocpt="urn:schemas-bea-com:ld-cocpt">
<CUSTOMER>
<FIRST_NAME>JOHN_B_1</FIRST_NAME>
<LAST_NAME>KAY_1</LAST_NAME>
<CUSTOMER_ID>CUSTOMER_1</CUSTOMER_ID>
<CUSTOMER_ORDER>
<OPENORDER_TOTAL_AMOUNT>150000</OPENORDER_TOTAL_AMOUNT>
<NEWORDER_TOTAL_AMOUNT>40000</NEWORDER_TOTAL_AMOUNT>
<TOTAL_OPEN_ORDERLIMIT>200000</TOTAL_OPEN_ORDERLIMIT>
<ORDER_REVIEW_STATUS>ACCEPT</ORDER_REVIEW_STATUS>
</CUSTOMER_ORDER>
</CUSTOMER>
</cocpt:CustOrderStatus>
![]() |
![]() |
![]() |
![]() |
||
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |