When designing your data service, you can specify read functions that filter data service return values. However, instead of trying to create a read function for every possible client requirement, you can create generalized read functions to which client applications can apply custom filtering or ordering criteria at runtime.
After completing this lesson, you will be able to:
Data users often want to access information in ways that are not anticipated in the design of a data service. The filtering and ordering API allow client applications to control what data is returned by a data service read function call based on conditions specified at runtime.
Although you can specify read functions that filter data service return values, it may be difficult to anticipate all the ways that client applications may want to filter return values. To deal with this contingency, ALDSP lets client applications specify dynamic filtering, sorting, and truncating criteria against the data service. These criteria are evaluated on the Server, before being transmitted on the network, thereby reducing the data set results to items matching the criteria. Where possible, these instances are "pushed down" to the underlying data source, thereby reducing the data set returned to the user.
The advantage of the FilterXQuery class is that you can define client-side filtering operations, without modifying or re-deploying your data services.
With the FilterXQuery class addFilter() method, filtering criteria are specified as Boolean condition statements (for example, ORDER_AMOUNT > 1000). Only items that meet the condition are included in the return set.
The addFilter() method also lets you create compound filters that provide significant flexibility, given the hierarchical structure of the data service return type. In other words, given a condition on a nested element, compound filters let you control the effects of the condition in relation to the parent element.
For example, consider a multi-level data hierarchy for CUSTOMERS/CUSTOMER/ORDER, in which CUSTOMERS is the top level document element, and CUSTOMER and ORDER are sequences within CUSTOMERS and CUSTOMER respectively. Finally, ORDER_AMOUNT is an element within ORDER.
An ORDER_AMOUNT condition (for example, CUSTOMER/ORDER/ORDER_AMOUNT > 1000) can affect what values are returned in several ways:
Instead of writing XQuery functions for each case, you just pass the filter object as a parameter when executing a data service function, either using the Data Service Control or Mediator API.
CustomerProfileDocument[] doc = (CustomerProfileDocument[]) ds.invoke("getCustomerProfile",params);
System.out.println("Connected to Liquid Data 8.2 : CustomerProfile Data Service ...");
import com.bea.ld.filter.FilterXQuery;
import com.bea.dsp.RequestConfig;
//Create a filter and condition
FilterXQuery filter = new FilterXQuery();
filter.addFilter(
"CustomerProfile/customer/orders/order", "CustomerProfile/customer/orders/order/total_order_amount",
">", "1000");
// Apply the filter
RequestConfig config = new RequestConfig();
config.setFilter(filter);
CustomerProfileDocument doc[] = (CustomerProfileDocument[]) ds.invoke("getCustomerProfile",params, config);
//Show Customer Data
code to the following:// Show Customer Data
System.out.println("======================= Customers =====================");
Customer customer = doc[0].getCustomerProfile().getCustomerArray(0);
System.out.println("Connected to ALDSP: CustomerProfile Data Service ...");
With the FilterXQuery class sortfilter.addOrderBy() method, you can specify criteria for organizing the data service return results. For example, to sort the order amount results in ascending order, you would use a sort condition similar to the following:
("CustomerProfile/customer/orders/order","total_order_amount",
FilterXQuery.ASCENDING);
// Create a sort
FilterXQuery sortfilter = new FilterXQuery();
sortfilter.addOrderBy(
"CustomerProfile/customer/orders/order",
"total_order_amount",
FilterXQuery.ASCENDING);
// Apply the sort
filter.setOrderByList(sortfilter.getOrderByList());
The FilterXQuery class also provides the filter.setLimit() method, which lets you limit the number of return results. For example, to limit the return results to two line items, you would use a truncate condition similar to the following:
("CustomerProfile/customer/orders/order/order_line","2");
The filter.setLimit method is based on the following:
public void setLimit(java.lang.String appliesTo, String max)
// Truncate result set
filter.setLimit("CustomerProfile/customer/orders/order/order_line","2");
In this lesson, you learned how to: