A new A-Team hint
Written by Olivier Le Diouris, Oracle Corporation
October, 2005
Here is a quick document, summarizing the little trick I used to iterate though
the data returned by the DB Adapter. It also adresses a question we commonly have.
Let's say we use the DB Adapter to do a query, and it returns a collection of elements we
wish to treat in a certain way, depending - for example - on the data contained in the
elements of the collection.
Let us suppose we have a view to query, described this way:
SQL> desc approvers_view
Name Null? Type
----------------------------------------- -------- ----------------------------
APPROVER_ID NOT NULL VARCHAR2(128)
APPROVER_NAME NOT NULL VARCHAR2(128)
TECHNOLOGY NOT NULL VARCHAR2(128)
SQL>
We query this view with the technology column:
SQL> col approver_id for a30
SQL> col approver_name for a20
SQL> col technology for a20
SQL> select * from approvers_view where technology = 'Security';
APPROVER_ID APPROVER_NAME TECHNOLOGY
------------------------------ -------------------- --------------------
amit.jasuja@oracle.com Amit Jasuja Security
peter.farkas@oracle.com Pete Farkas Security
SQL>
We have two people responsible for this technology stack, what we want to do is to
send each of them a separate email...
If we use the DB Apadter, we will eventually have to deal with a document - or document fragment - like this one:
<n:ApproversViewCollection xmlns:n="http://xmlns.oracle.com/pcbpel/adapter/db/top/RequestTrackerTest"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ApproversView>
<approverId>amit.jasuja@oracle.com</approverId>
<approverName>Amit Jasuja</approverName>
<technology>Security</technology>
</ApproversView>
<ApproversView>
<approverId>peter.farkas@oracle.com</approverId>
<approverName>Pete Farkas</approverName>
<technology>Security</technology>
</ApproversView>
</n:ApproversViewCollection>
We are going to use the BPEL while activity.
The trick is to find the right formulation for the while-condition.
/ApproversViewCollection/ApproversView[index]/approverId
where 'index' is the index of the row in the array returned by the DB Adapter.
index begins at '1'. And most important, when 'index' exceeds the size of the array, XPath returns
an appropriate zero-length string.
To do so:
|
|
string-length(bpws:getVariableData('GetApprovers_OutputVariable',
'ApproversViewCollection')/ApproversView[number(bpws:getVariableData('index'))]/approverId) > 0
...which is not what the expression builder suggests first.
bpws:getVariableData('GetApprovers_OutputVariable',
'ApproversViewCollection')/ApproversView[number(bpws:getVariableData('index'))]/approverId
number(bpws:getVariableData('index') + 1)
Note:
bpws:getVariableData('GetApprovers_OutputVariable',
'ApproversViewCollection',
'/ns4:ApproversViewCollection/ApproversView[number(bpws:getVariableData("index"))]/approverId')
Notice that you have to escape the quotes, in the reference to the index variable, as you have
two embedded expressions... I prefer the first version.