Client Application Developer’s Guide

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

Advanced Topics

This chapter describes miscellaneous features that are related to client programming with BEA AquaLogic Data Services Platform. It includes the following topics:

ALDSP Control Source Upgrade

You need to upgrade ALDSP 8.x controls to 9.x using source upgrade. After the upgrade, a version of ALDSP Control.jar is placed in the WEB-INF/lib folder. The Control.jar file contains control runtime classes.

While performing the upgrade, ensure that the domain to which the Web-App is to be deployed includes the following files:

Source upgrade also upgrades the schemas used by ALDSP Control.

For more information on upgrading ALDSP Control, refer to Upgrading an ALDSP Control from 2.5 to 3.0 section in the Installation Guide.

 


Accessing Metadata Using Catalog Services

BEA AquaLogic Data Services Platform maintains metadata about data services, application, functions, and schemas through Catalog Services, which is a system catalog-type data service. Catalog services provide a convenient way for client-application developers to programmatically obtain information about Data Services Platform applications, data services, schemas, functions, and relationships.

Catalog Services are also data services; you can view them using the DSP Console, the ALDSP Palette, and ALDSP Controls.

Some advantages of using Catalog Services are as follows:

This section provides details about installing and using Catalog Services to access metadata for any Data Services Platform application. It includes the following topics:

Installing Catalog Services

You can install Catalog Services as a project for an ALDSP application or as a JAR file that is added to the Library folder in Data Services Studio. The Catalog Services project (_catalogservices) contains data services that provide information about the application, folders, data services, functions, schemas, and relationships available with the application.

DataServiceRef and SchemaRef are additional data services that consist of functions that retrieve the paths to the data services and schemas available with the Data Services Platform application. For more information about the data services and functions available with Catalog Services, refer to Using Catalog Services on page 9-4.

To install Catalog Services as a project:

  1. Right-click the Data Services Platform application in Data Services Studio.
  2. Select the Install Catalog Services (Expanded) option if you want to use the catalog services for development, as shown in Figure 9-1. If you need catalog services only during runtime then select Install Catalog Services (Jar) option.
  3. Figure 9-1 Installing Catalog Services


    Installing Catalog Services

Using Catalog Services

After installing Catalog Services, the catalog services project, _catalogservices, is created for the Data Services Platform application. All the data services associated with catalog services are available under this project. You can invoke the data service functions to access metadata. The client Mediator API is used to invoke the Catalog Service methods.

The data services available under _catalogservices include:

Note: To use data service functions available with Catalog services, refer to the code samples available at:
Note: http://dev2dev.bea.com/wiki/pub/CodeShare/Sample1/catalogsrv_output.zip

Application (application.ds)

The following table provides the declaration and description for the getApplication()function in Application.ds.

Table 9-2 Functions in Application.ds
Function Declaration
Description
getApplication() as schema-element(t1:Application) external;
This function returns the name of the AquaLogic Data Services Platform application. It does not take any parameters.

DataService (DataService.ds)

Table 9-3 provides declaration and description information for the functions available in DataService.ds.

Table 9-3 Functions in DataService.ds
Function Declaration
Description
Sample Input
getDataServiceRef($arg as element(md:DataService)) as element(md:DataServiceRef) {$arg/md:DataServiceRef}
This function returns the path of the data service associated with the function.
For this function, you need to specify the following:
  • Path of the data service
  • Path of the schema for the data service
  • Function ID of the function for which you need the data service reference
<urn:DataService kind="javaFunction" xmlns:acc="ld:RTLAppDataServices/CustomerDB/Customer" xmlns:urn="urn:metadata.ld.bea.com">
<urn:DataServiceRef>
<id>
ld:RTLAppDataServices/CustomerDB/Customer.ds</id>
</urn:DataServiceRef>
<returnType name="CUSTOMER" kind="read" quantifier="*" schemaId="ld:RTLAppDataServices/CustomerDB/schemas/CUSTOMER.xsd"/>
<!--Zero or more repetitions:-->
<key>
<!--1 or more repetitions:--> <path>ld:RTLAppDataServices/CustomerDB/Customer.ds</path>
</key>
<!--Zero or more repetitions:-->
<urn:FunctionId name="CUSTOMER" arity="0"/>
</urn:DataService>
getDataService($x1 as element(t1:DataServiceRef)) as schema-element(t1:DataService)? external
This function returns the attributes of the specified data service such as the schema path, functions, and relational data source.
Specify the path of the data service to retrieve the required result.
<DataServiceRef xmlns="urn:metadata.ld.bea.com">
<id xmlns="">ld:DataServices/CustomerDB/CUSTOMER.ds</id>
</DataServiceRef>

DataServiceRef (DataServiceRef.ds)

The following table provides the declaration and description for the functions available in DataServiceRef.ds.

Table 9-4 Functions in DataService
Function Declaration
Description
Sample Input
getDataServiceRefsByFolder($x1 as xsd:string, $x2 as xsd:boolean) as schema-element(t1:DataServiceRef)* external
This function returns the data services that exist within a folder in the project. You need to specify the path of the project folder and set the boolean value to true for this function.
String parameter = ld:RTLAppDataServices/CustomerDB/
Boolean = true
getDataServiceRefs() as schema-element(t1:DataServiceRef)* external
This function returns the path to all the data services in the project. It does not require any parameters.
No input required.
getDependents($x1 as element(t1:DataServiceRef), $x2 as xsd:boolean) as schema-element(t1:DataServicRef)* external
This function returns the path of the data services on which the specified data service depends.
For this function, you need to specify the path of the data service whose dependents you need to determine. For example, if you need to find out the dependents for CUSTOMER.ds then specify the path of the data service as:
ld:DataServices/CustomerDB/CUSTOMER.ds
<urn:DataServiceRefdat xmlns:urn="urn:metadata.ld.bea.com"> <id>ld:DataServices/CustomerDB/CUSTOMER.ds</id>
</urn:DataServiceRef>
getDependencies($x1 as element(t1:DataServiceRef), $x2 as xsd:boolean) as schema-element(t1:DataServiceRef)* external
This function returns the dependencies for the specified data service.
For this function, you need to specify the path of the data service whose dependencies you need to determine.
<urn:DataServiceRef xmlns:urn="urn:metadata.ld.bea.com"> <id>ld:DataServices/Demo/CustomerProfile.ds</id>
</urn:DataServiceRef>
getFunctions($x1 as element(t1:DataServiceRef)) as schema-element(t1:Function)* external
This function returns the list of data service functions and their attributes such as function kind, arity, and schema path.
For this function, specify the path of the data service as input.
<DataServiceRef xmlns="urn:metadata.ld.bea.com">
<id xmlns="">ld:RTLAppDataServices/CustomerDB/CUSTOMER.ds</id>
</DataServiceRef>
getRelationships($x1 as element(t1:DataServiceRef)) as schema-elemen43t(t1:Relationship)* external
This function retrieves the path of data services which have any relationship with the specified data service. You need to specify the path of the data service, such as ld:RTLAppDataServices/CustomerDB/CUSTOMER.ds
DataServiceRef xmlns="urn:metadata.ld.bea.com">
<id xmlns="">ld:RTLAppDataServices/CustomerDB/CUSTOMER.ds</id>
</DataServiceRef>
getSchemaRefs($arg as element(t1:DataServiceRef), $transitive as xs:boolean) as element(t1:SchemaRef)* external
For this function, enter the path of the data service and set the boolean value to true for retrieving the list of associated schemas. This function also lists the paths of schemas for data services, which have a relationship with the specified data service.
<urn:DataServiceRef xmlns:urn="urn:metadata.ld.bea.com"> <id>ld:RTLAppDataServices/CustomerDB/CUSTOMER.ds</id>
</urn:DataServiceRef>
Enter true as the boolean parameter.
getDataService($x1 as element(t1:DataServiceRef)) as schema-element(t1:DataService)? external
This function returns the attributes of the specified data service such as the schema path, functions, and relational data source.
Specify the path of the data service to retrieve the required result.
<urn:DataServiceRef xmlns:urn="urn:metadata.ld.bea.com">
<id xmlns="">ld:RTLAppDataServices/CustomerDB/CUSTOMER.ds</id>
</urn:DataServiceRef>

Folder (folder.ds)

The following table provides the declaration and description for the functions available in Folder.ds.

Table 9-5 Functions in Folder.ds
Function Declaration
Description
Sample Input
getFolder() as schema-element(t1:Folder)* external
This function provides a list of paths of folders and data services that exist within the Data Services Platform project. It does not require any parameters.
No input required.
getFolder($x1 as xsd:string, $x2 as xsd:boolean) as schema-element(t1:Folder)* external
This folder returns the paths of all the data services that exists within a specified folder. You need to specify two parameters for this function, which include:
  • Path of the folder such as ld:RTLAppDataServices/CustomerDB
  • Boolean value (usually set to true)
  • Parameter 1 (string) = ld:RTLAppDataServices/CustomerDB
  • Parameter 2 (boolean) = true
getDataServiceRefs($x1 as element(t1:Folder)) as schema-element(t1:DataServiceRef)* external
This function also provides the paths of the data services that exist within a folder. To retrieve this information, specify the path of the folder as input.
<Folder xmlns="urn:metadata.ld.bea.com">
<id xmlns="">ld:RTLAppDataServices/CustomerDB</id>
</Folder>

Function (Function.ds)

The following table provides the declaration and description for the functions in Function.ds.

Table 9-6 Functions in Function.ds
Function Declaration
Description
Sample Input
getFunctionById($x1 as element(t1:FunctionId)) as schema-element(t1:Function) external4
This function returns the path of the data service and schema along with function arity, function kind and return type information about the specified function.
For this function, specify the function ID and arity as input.
<FunctionId name=" cus:CUSTOMER " arity="0" xmlns:cus="ld:RTLAppDataServices/CustomerDB/CUSTOMER" xmlns="urn:metadata.ld.bea.com"/>
getDataService($arg as element(md:Function)) as element(md:DataService
This function returns the function arity and physical data source information for the specified function.
For this function, you need to specify the function ID, path of the data service and schema.
<?xml version="1.0" encoding="UTF-8" ?>
<urn:Function kind="read" xmlns:acc="ld:RTLAppDataServices/CustomerDB/CUSTOMER"
xmlns:urn="urn:metadata.ld.bea.com">
<urn:FunctionId arity="0" name="acc:getAll"></urn:FunctionId>
<returnType kind="element" name="urn:Account" quantifier="1" schemaId="ld:RTLAppDataServices/CustomerDB/schemas/CUSTOMER.xsd">
</returnType>
<urn:DataServiceRef> <id>ld:RTLAppDataServices/CustomerDB/CUSTOMER.ds</id>
</urn:DataServiceRef>
</urn:Function>
getRelationship($arg as element(md:Function)) as element(md:Relationship) external
getFunction()relationship function
This function returns the relationship target and path of the data services with which the navigation function has a relationship.
Specify the function ID, path of the data service and schema as input.

Note: This function is applicable to navigation functions only.

<?xml version="1.0" encoding="UTF-8" ?>
<urn:Function kind="navigate" xmlns:acc="ld:RTLAppDataServices/CustomerDB/Customer" xmlns:urn="urn:metadata.ld.bea.com">
<urn:FunctionId arity="1" name="acc:getDISCOUNT">
</urn:FunctionId>
<returnType kind="element" name="urn:getDISCOUNT" quantifier="1" schemaId="ld:RTLAppDataServices/CustomerDB/schemas/CUSTOMER.xsd">
</returnType>
<parameter name="arg">
<type kind="navigate" name="urn:DISCOUNT" quantifier="*" schemaId="ld:RTLAppDataServices/CustomerDB/CUSTOMER.xsd">
</type>
</parameter>
<urn:DataServiceRef>
<id>ld:RTLAppDataServices/CustomerDB/CUSTOMER.ds</id>
</urn:DataServiceRef>
<roleName>DISCOUNT</roleName>
</urn:Function>
getSchemaRefs($x1 as element(t1:Function), $x2 as xsd:boolean) as schema-element(t1:SchemaRef)* external
DataServicesRef_
For this function, specify the function ID and path of the data service to retrieve the path of the schemas associated with the data service.
<urn:Function kind="navigate" xmlns:acc="ld:RTLAppDataServices/CustomerDB/CUSTOMER" xmlns:urn="urn:metadata.ld.bea.com">
<urn:FunctionId name="acc:getDISCOUNT" arity="1"/>
<returnType name="DISCOUNT" kind="element" quantifier="*" schemaId="ld:RTLAppDataServices/CustomerDB/schemas/CUSTOMER.xsd"/>
<!--Zero or more repetitions:-->
<parameter name="arg">
<type name="DISCOUNT" kind="element" quantifier="*" schemaId="ld:RTLAppDataServices/CustomerDB/schemas/CUSTOMER.xsd"/>
</parameter>
<urn:DataServiceRef>
<id>ld:RTLAppDataServices/CustomerDB/Customer.ds</id>
</urn:DataServiceRef>
<!--Optional:-->
<roleName>DISCOUNT</roleName>
</urn:Function>

Relationship (Relationship.ds)

The following table provides the declaration and description for the functions available in Relationship.ds.

Note: The functions in Relationship.ds can be used to access metadata only for navigation functions.

Table 9-7 Functions in Relationship.ds
Function Declaration
Description
Sample Input
getFunctions($arg as element(md:Relationship)) as element(md:Function)
This function returns the attributes of the function that you specify as input. You need to specify the following parameters for this function:
  • String parameter = Path of the data service
  • Function ID
  • Values for minOccurs and maxOccurs
<urn:Relationship xmlns:acc="ld:RTLAppDataServices/CustomerDB/CUSTOMER" xmlns:urn="urn:metadata.ld.bea.com">
<!--1 to 2 repetitions:-->
<relationshipTarget roleName="DISCOUNT" minOccurs="1" maxOccurs="1" description="">
<urn:DataServiceRef>
<id>ld:RTLAppDataServices/CustomerDB/CUSTOMER.ds</id>
</urn:DataServiceRef>
<!--Zero or more repetitions:-->
<urn:FunctionId name="acc:getDISCOUNT" arity="1"/>
</relationshipTarget>
</urn:Relationship>
getDataServiceRefs($x1 as element(t1:Relationship)) as schema-element(t1:DataServiceRef)
You need to specify the following parameters for this function:
  • String parameter = Path of the data service
  • Function ID
  • Values for minOccurs and maxOccurs
<urn:Relationship xmlns:acc="ld:RTLAppDataServices/CustomerDB/CUSTOMER" xmlns:urn="urn:metadata.ld.bea.com">
<!--1 to 2 repetitions:-->
<relationshipTarget roleName="DISCOUNT" minOccurs="1" maxOccurs="1" description="">
<urn:DataServiceRef>
<id>ld:RTLAppDataServices/CustomerDB/CUSTOMER.ds</id>
</urn:DataServiceRef>
<!--Zero or more repetitions:-->
<urn:FunctionId name="acc:getDISCOUNT" arity="1"/>
</relationshipTarget>
</urn:Relationship>
getDataServices($arg as element(md:Relationship)) as element(md:DataService)
This function returns the attributes, such as relational datasource and function arity, of the navigation function of the data service. For this function, you need to specify the following parameters:
  • String parameter = Path of the data service
  • String parameter = Path of the schema
  • Values for maxOccurs and minOccurs
  • FunctionID
<?xml version="1.0" encoding="UTF-8" ?>
<urn:Relationship xmlns:acc="ld:RTLAppDataServices/CustomerDB/CUSTOMER" xmlns:urn="urn:metadata.ld.bea.com">
<relationshipTarget description="" maxOccurs="1" minOccurs="1" roleName="DISCOUNT">
<urn:DataServiceRef>
<id>
ld:RTLAppDataServices/CustomerDB/CUSTOMER.ds</id>
</urn:DataServiceRef>
<urn:FunctionId arity="1" name="acc:getDISCOUNT">
</urn:FunctionId>
</relationshipTarget>
<relationshipTarget description="" maxOccurs="1" minOccurs="1" roleName="DISCOUNT" xmlns:acc="ld:RTLAppDataServices/CustomerDB/CUSTOMER">
<urn:DataServiceRef>
<id>ld:RTLAppDataServices/CustomerDB/CUSTOMER.ds</id>
</urn:DataServiceRef>
<urn:FunctionId arity="1" name="acc:getDISCOUNT">
</urn:FunctionId>
</relationshipTarget>
</urn:Relationship>

Schema (Schema.ds)

The following table provides the declaration and description for the functions available in Schema.ds

Table 9-8 Functions in Schema.ds
Function Declaration
Description
Sample Input
getSchema($x1 as element(t1:SchemaRef)) as schema-element(t1:Schema)* external
This functions returns the schema attributes of the schema associated with the data service. You need to specify the path of the schema which you need to access as string parameter. For example:
ld:RTLAppDataServices/CustomerDB/schemas/CUSTOMER.xsd
<urn:SchemaRef xmlns:urn="urn:metadata.ld.bea.com">
<id>ld:RTLAppDataServices/CustomerDB/schemas/CUSTOMER.xsd</id>
</urn:SchemaRef>
getSchemaRef($x1 as element(t1:Schema)) as schema-element(t1:SchemaRef)
This function returns the path of the schema of the data service.
Specify the schema path to get the reference to the schema. For example:
ld:RTLAppDataServices/CustomerDB/schemas/CUSTOMER_TABLE.xsd
<urn:Schema xmlns:urn="urn:metadata.ld.bea.com">
<urn:SchemaRef>
<id>ld:RTLAppDataServices/CustomerDB/schemas/CUSTOMER_TABLE.xsd</id>
</urn:SchemaRef>
</urn:Schema>

SchemaRef (SchemaRef.ds)

The following table provides the declaration and description for the functions available in SchemaRef.ds.

Table 9-9 Functions in SchemaRef.ds
Function Declaration
Description
Sample Input
getDependencies($x1 as element(t1:SchemaRef), $x2 as xs:boolean) as schema-element(t1:SchemaRef)* external
This function returns the dependencies of the specified data service.
You need to specify the path of the schema for the data service as a string parameter. For example:
ld:DataServices/Demo/schemas/CustomerProfile.xsd
<urn:SchemaRef xmlns:urn="urn:metadata.ld.bea.com">
<id xmlns="">ld:DataServices/Demo/schemas/CustomerProfile.xsd</id>
</urn:SchemaRef>
The second parameter is boolean and the value can be either true or false.
getSchema($x1 as element(md:SchemaRef)) as schema-element(md:Schema)* external
This functions returns the schemas associated with the data service.
You need to specify the path of the schema for the data service as a string parameter. For example:
ld:RTLAppDataServices/CustomerDB/schemas/CUSTOMER.xsd
<urn:SchemaRef xmlns:urn="urn:metadata.ld.bea.com">
<id>ld:RTLAppDataServices/CustomerDB/schemas/CUSTOMER.xsd</id>
</urn:SchemaRef>

 


Filtering, Sorting, and Fine-tuning Query Results

The Filter API enables client applications to apply filtering conditions to the information returned by data service functions. In a sense, filtering allows client applications to extend a data service interface by allowing the application to specify more about how data objects are to be instantiated and returned by functions.

The Filter API alleviates data service designers from having to anticipate every possible data view that clients may require and to implement a data service function for each view. Instead, designers can choose to specify a broader, more generic interface for accessing a business entity and allow client applications to control views as desired through filters.

Using the API, you can specify that only objects that meet a particular condition in the function return set be returned to the client. A filter is therefore similar to a WHERE clause in an XQuery or SQL statement—it applies conditions to a possible result set. You can apply multiple filter conditions using AND and OR operators.

The effects of a filter can vary, depending on the desired results. Consider, for example, the CUSTOMERS data object shown in Figure 9-10. The data object contains multiple complex elements (CUSTOMER and ORDERS) and several simple elements, including ORDER_AMOUNT.

Figure 9-10 Nested Value Filtering

Nested Value Filtering

You can apply a filter to any element in this hierarchy. For example, you could apply a filter to return all CUSTOMER objects but filter ORDERS than have an ORDER_AMOUNT greater than 1000. Similarly, you could apply a filter to return only the CUSTOMER objects that have at least one large order.

You can also use a filter to specify the order criteria (ascending or descending) in which results should be returned from the data service. Finally, you can use a filter to set the maximum number of results to be returned.

Note: Filter evaluation occurs at the server, so objects that are filtered are not passed over the network. Often, objects that are filtered out are not even retrieved from the underlying data sources.

Introducing the Filter API

You specify filters using the FilterXQuery object, which includes the following methods enabling you to add a filter, create a filter to apply later, specify the sort order, and set a limit on the number of results returned:

addFilter()

The addFilter() method enables you to create a filter and add it to the list of filters. The addFilter() method has several signatures with different parameters, including the following:

public void addFilter(java.lang.String appliesTo,
java.lang.String field,
java.lang.String operator,
java.lang.String value,
java.lang.Boolean everyChild)

This version of the method takes the following arguments:

createFilter()

The createFilter() method enables you to create a filter that you can later apply to any of the objects in the hierarchy. The createFilter() method has several signatures with different parameters, including the following:

public void createFilter(java.lang.String field,
java.lang.String operator,
java.lang.String value,
java.lang.Boolean everyChild)

This version of the method takes the following arguments:

addOrderBy()

The addOrderBy() method enables you to add a sort criteria (either ascending or descending) to the specified object. The addOrderBy() method has the following signature:

public void addOrderBy(java.lang.String appliesTo,
java.lang.String field,
java.lang.String sort)

The method takes the following arguments:

setLimit()

The setLimit() method enables you to specify the maximum number of entries to return of the specified object. The setLimit() method has the following signature:

public void setLimit(java.lang.String appliesTo,
java.lang.String max)

The method takes the following arguments:

Exploring the Filter Operators

Table 9-11 describes the operators that you can apply to filter conditions.

Table 9-11 Filter Operators  
Operator
Usage Note or Example
LESS_THAN
Can also use "<". For example:
myFilter.addFilter("CUST/CUST_ORDER/ORDER", "CUST/CUST_ORDER/ORDER/ORDER_AMOUNT", "<", "1000");
is identical to
myFilter.addFilter("CUST/CUST_ORDER/ORDER", "CUST/CUST_ORDER/ORDER/ORDER_AMOUNT", FilterXQuery.LESS_THAN, "1000");
GREATER_THAN
Can also use ">".
LESS_THAN_EQUAL
Can also use "<=".
GREATER_THAN_EQUAL
Can also use ">=".
EQUAL
Can also use "=".
NOT_EQUAL
Can also use "!=".
MATCHES
Tests for string equality.
BEA_SQL_LIKE
Tests whether a string contains a specified pattern in a manner similar to the SQL LIKE clause.
AND
Compound operator that can apply to more than one filter.
OR
Compound operator that can apply to more than one filter.
NOT
Compound operator that can apply to more than one filter.

Note: Filter API Javadoc, and other Data Services Platform APIs are available on e-docs.

Using Filters

Filtering capabilities are available to Mediator and ALDSP Control client applications. To use filters in a mediator client application, import the appropriate package and use the supplied interfaces for creating and applying filter conditions.

Data service control clients get the interface automatically. When a function is added to a control, a corresponding "WithFilter" function is added as well.

The filter package is named as follows:

	com.bea.ld.filter.FilterXQuery; 

To use a filter, perform the following steps:

  1. Create an FilterXQuery object, such as:
  2. FilterXQuery myFilter = new FilterXQuery();
  3. Add a condition to the filter object using the addFilter() method.
  4. The following example shows how to add a filter to have orders with an order amount greater than 1000 returned (note that the optional everyChild parameter is not specified, so order amounts below 1000 will also be returned):

         myFilter.addFilter("CUSTOMERS/CUSTOMER/ORDER",
    "CUSTOMERS/CUSTOMER/ORDER/ORDER_AMOUNT",
    ">",
    "1000");
  5. Use the Mediator API call setFilter() to add the filter to a data service, passing the FilterXQuery instance as an argument. For example,
  6. RequestConfig config = new RequestConfig();
    config.setFilter(myFilter);
    CUSTOMERDAS custDAS = CUSTOMER.getInstance(ctx, "RTLApp");
    custDS.myOperation(config);
  7. Invoke the data service function.
  8. For more information on invoking data service functions, see Invoking Data Services from Java Clients.

Filtering Examples

In general, with nested XML data, a condition such as “CUSTOMER/ORDER/ORDER_AMOUNT > 1000” can affect what objects are returned in several ways. For example, it can cause all CUSTOMER objects to be returned, but filter ORDERS that have an amount less than 1000.

Alternatively, it can cause only CUSTOMER objects to be returned that have at least one large order, but containing all ORDERs (small and large) for each such CUSTOMER.

The following examples show how filters can be applied in several different ways:

Specifying a Compound Filter

You can create a filter with two conditions using logical AND and OR operators. Listing 9-1 uses the AND operator to apply a combination of filters to a result set, given a data service instance customerDS.

Listing 9-1 Example of Combining Filters by Using Logical Operators
FilterXQuery myFilter = new FilterXQuery();
Filter f1 = myFilter.createFilter("CUSTOMER_PROFILE/ADDRESS/ISDEFAULT",
FilterXQuery.NOT_EQUAL,"0");
Filter f2 = myFilter.createFilter("CUSTOMER/ADDRESS/STATUS",
FilterXQuery.EQUAL,
"\"ACTIVE\"");
Filter f3 = myFilter.createFilter(f1,f2, FilterXQuery.AND);
Customer customerDS = Customer.getInstance(ctx, "RTLApp");
CustomerDS.setFilterCondition(myFilter);

Ordering and Truncating Data Service Results

You can specify the order criteria (ascending or descending) in which results should be returned from the data service. The addOrderBy() method accepts a property name as the criterion upon which the ascending or descending decision is based.

Listing 9-2 provides an example of creating a filter to return customer profiles in ascending order, based on the date each person became a customer.

Listing 9-2 Example of Applying an Ordering Filter
FilterXQuery myFilter = new FilterXQuery();
myFilter.addOrderBy("CUSTOMER_PROFILE",
"CustomerSince" ,FilterXQuery.ASCENDING);
ds.setFilterCondition(myFilter);
DataObject objArrayOfCust = (DataObject) ds.invoke("getCustomer", null);

Similarly, you can set the maximum number of results to be returned using the setLimit() method. Listing 9-3 shows how to use the setLimit() method to limit the number of active addresses in the result set to 10, given a data service instance ds.

Listing 9-3 Example of Applying a Filter that Truncates (Limits) Results
FilterXQuery myFilter = new FilterXQuery();
Filter f2 = myFilter.createFilter("CUSTOMER_PROFILE/ADDRESS",
FilterXQuery.EQUAL,"\"INACTIVE\"");
myFilter.addFilter("CUSTOMER_PROFILE", f2);
myFilter.setLimit("CUSTOMER_PROFILE", "10");
ds.setFilterCondition(myFilter);

Using Ad Hoc Queries to Fine-tune Results from the Client

An ad hoc query is an XQuery function that is not defined as part of a data service, but is instead defined in the context of a client application. Ad hoc queries are typically used in client applications to invoke data service functions and refine the results in some way. You can use an ad hoc query to execute any valid XQuery expression against a data service. The expression can target the actual data sources that underlie the data service, or can use the functions and procedures hosted by the data service.

To execute an XQuery expression, use the PreparedExpression interface, available in the Mediator API. Similar to JDBC PreparedStatement interface, the PreparedExpression interface takes the XQuery expression as a string in its constructor, along with the JNDI server context and application name. After constructing the prepared expression object in this way, you can call the executeQuery( ) method on it. If the ad hoc query invokes data service functions or procedures, the data service’s namespace must be imported into query string before you can reference the methods in your ad hoc query.

Listing 9-4 shows a complete example; the code returns the results of a data service function named getCustomers( ), which is in the namespace:

ld:DataServices/RTLServices/Customer
Listing 9-4 Invoking Data Service Functions using an Ad Hoc Query
import com.bea.ld.dsmediator.client.PreparedExpression;
String queryStr = 
"declare namespace ns0=\"ld:DataServices/RTLServices/Customer\";" +
"<Results>" +
" { for $customer_profile in ns0:getCustomer()" +
" return $customer_profile }" +
"</Results>";
PreparedExpression adHocQuery =
DataServiceFactory.prepareExpression(context,"RTLApp",queryStr );
XmlObject objResult = (XmlObject) adHocQuery.executeQuery();

ALDSP passes information back to the ad hoc query caller as an XMLObject data type. Once you have the XMLObject, you can downcast to the data type of the deployed XML schema. Since XMLObject has only a single root type, if the data service function returns an array, your ad hoc query should include a root element as a container for the array.

For example, the ad hoc query shown in Listing 9-4 specifies a <Results> container object to hold the array of CUSTOMER_PROFILE elements that will be returned by the getCustomer() data service function.

Security policies defined for a data service apply to the data service calls in an ad hoc query as well. If an ad hoc query uses secured resources, the appropriate credentials must be passed when creating the JNDI initial context. (For more information, see Obtaining the WebLogic JNDI Context for ALDSP on page 3-44.)

As with the PreparedStatement interface of JDBC, the PreparedExpression interface supports dynamically binding variables in ad hoc query expressions. PreparedExpression provides several methods (bindType( ) methods; see Table 9-12), for binding values of various data types.

Table 9-12 PreparedExpression Methods for Bind Variables  
To bind data type of...
Use bind method...
Binary
bindBinary(javax.xml.namespace.QName qname, byte[] abyte0)
BinaryXML
bindBinaryXML(javax.xml.namespace.QName qname, byte[] abyte0)
Boolean
bindBoolean(javax.xml.namespace.QName qname, boolean flag)
Byte
bindByte(javax.xml.namespace.QName qname, byte byte0)
Date
bindDate(javax.xml.namespace.QName qname, java.sql.Date date)
Calendar
bindDateTime(javax.xml.namespace.QName qname, java.util.Calendar calendar)
DateTime
bindDateTime(javax.xml.namespace.QName qname, java.util.Date date)
DateTime
bindDateTime(javax.xml.namespace.QName qname, java.sql.Timestamp timestamp)
BigDecimal
bindDecimal(javax.xml.namespace.QName qname, java.math.BigDecimal bigdecimal)
double
bindDouble(javax.xml.namespace.QName qname, double d)
Element
bindElement(javax.xml.namespace.QName qname, org.w3c.dom.Element element)
Object
bindElement(javax.xml.namespace.QName qname, java.lang.String s)
float
bindFloat(javax.xml.namespace.QName qname, float f)
int
bindInt(javax.xml.namespace.QName qname, int i)
long
bindLong(javax.xml.namespace.QName qname, long l)
Object
bindObject(javax.xml.namespace.QName qname, java.lang.Object obj)
short
bindShort(javax.xml.namespace.QName qname, short word0)
String
bindString(javax.xml.namespace.QName qname, java.lang.String s)
Time
bindTime(javax.xml.namespace.QName qname, java.sql.Time time)
URI
bindURI(javax.xml.namespace.QName qname, java.net.URI uri)

To use the bindType methods, pass the variable name as an XML qualified name (QName) along with its value; for example:

adHocQuery.bindInt(new QName("i"),94133);

Listing 9-5 shows an example of using a bindInt() method in the context of an ad hoc query.

Listing 9-5 Binding a Variable to a QName (Qualified Name) for use in an Ad Hoc Query
PreparedExpression adHocQuery = DataServiceFactory.preparedExpression(
context, "RTLApp",
"declare variable $i as xs:int external;
<result><zip>{fn:data($i)}</zip></result>");
adHocQuery.bindInt(new QName("i"),94133);
XmlObject adHocResult = adHocQuery.executeQuery();
Note: For more information on QNames, see:

http://www.w3.org/TR/xmlschema-2/#QName

Listing 9-6 shows a complete ad hoc query example, using the PreparedExpression interface and QNames to pass values in bind methods.

Listing 9-6 Sample Ad Hoc Query
import com.bea.ld.dsmediator.client.DataServiceFactory;
import com.bea.ld.dsmediator.client.PreparedExpression;
import com.bea.xml.XmlObject;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.xml.namespace.QName;
import weblogic.jndi.Environment;

public class AdHocQuery
{
public static InitialContext getInitialContext() throws NamingException {
Environment env = new Environment();
env.setProviderUrl("t3://localhost:7001");
env.setInitialContextFactory("weblogic.jndi.WLInitialContextFactory");
env.setSecurityPrincipal("weblogic");
env.setSecurityCredentials("weblogic");
return new InitialContext(env.getInitialContext().getEnvironment());
}

public static void main (String args[]) {
System.out.println("========== Ad Hoc Client ==========");
try {
StringBuffer xquery = new StringBuffer();
xquery.append("declare variable $p_firstname as xs:string external; \n");
xquery.append("declare variable $p_lastname as xs:string external; \n");

xquery.append(
"declare namespace ns1=\"ld:DataServices/MyQueries/XQueries\"; \n");
xquery.append(
"declare namespace ns0=\"ld:DataServices/CustomerDB/CUSTOMER\"; \n\n");

xquery.append("<ns1:RESULTS> \n");
xquery.append("{ \n");
xquery.append(" for $customer in ns0:CUSTOMER() \n");
xquery.append(" where ($customer/FIRST_NAME eq $p_firstname \n");
xquery.append(" and $customer/LAST_NAME eq $p_lastname) \n");
xquery.append(" return \n");
xquery.append(" $customer \n");
xquery.append(" } \n");
xquery.append("</ns1:RESULTS> \n");

PreparedExpression pe = DataServiceFactory.prepareExpression(
getInitialContext(), "RTLApp", xquery.toString());
pe.bindString(new QName("p_firstname"), "Jack");
pe.bindString(new QName("p_lastname"), "Black");
XmlObject results = pe.executeQuery();
System.out.println(results);

} catch (Exception e) {
e.printStackTrace();
}
}

  Back to Top       Previous  Next