Skip Headers
Oracle® Application Development Framework Developer's Guide For Forms/4GL Developers
10g Release 3 (10.1.3.0)

Part Number B25947-02
Go to Documentation Home
Home
Go to Table of Contents
Contents
Go to Index
Index
Go to Feedback page
Contact Us

Go to previous page
Previous
Go to next page
Next
View PDF

27 Advanced View Object Techniques

This chapter describes advanced techniques you can use while designing and working with your view objects.

This chapter includes the following sections:

Note:

To experiment with a working version of the examples in this chapter, download the AdvancedViewObjectsExamples workspace from Example Downloads page at http://otn.oracle.com/documentation/jdev/b25947_01/.

27.1 Advanced View Object Concepts and Features

This section describes a number of interesting view object concepts and features that have not been discussed in previous chapters.

27.1.1 Using a Max Fetch Size to Only Fetch the First N Rows

The default maximum fetch size of a view object is minus one (-1), which indicates that there is no artificial limit to the number of rows that can be fetched. Keep in mind that by default, rows are fetched as needed, so this default does not imply a view object will necessary fetch all the rows. It simply means that if you attempt to iterate through all the rows in the query result, you will get them all.

However, you might want to put an upper bound on the maximum number of rows that a view object will retrieve. If you write a query containing an ORDER BY clause and only want to return the first N rows to display the "Top-N" entries in a page, you can call the setMaxFetchSize() method on your view object to set the maximum fetch size to N. The view object will stop fetching rows when it hits the maximum fetch size. Often you will combine this technique with specifying a Query Optimizer Hint of FIRST_ROWS on the Tuning panel of the View Object Editor. This gives a hint to the database that you want to retrieve the first rows as quickly as possible, rather than trying to optimize the retrieval of all rows.

27.1.2 Consistently Displaying New Rows in View Objects Based on the Same Entity

When multiple instances of entity-based view objects in an application module are based on the same underlying entity object, a new row created in one of them can be automatically added (without having to re-query) to the row sets of the others to keep your user interface consistent or simply to consistently reflect new rows in different application pages for a pending transaction. Consider the SRDemo application's SRList page that displays an end-user's list of service requests. If the end-user goes to create a new service request, this task is performed through a different view object and handled by a custom application module method. Using this view object new row consistency feature, the newly created service request automatically appears in the end-user's list of open service requests on the SRList page without having to re-query the database.

For historical reasons, this capability is known as the view link consistency feature because in prior releases of Oracle ADF the addition of new rows to other relevant row sets only was supported for detail view object instances in a view link based on an association. Now this view link consistency feature works for any view objects for which it is enabled, regardless of whether they are involved in a view link or not.

27.1.2.1 How View Link Consistency Mode Works

Consider two entity-based view objects ServiceRequestSummary and ServiceRequests both based on the same underlying ServiceRequest entity object. When a new row is created in a row set for one of these view objects (like ServiceRequests) and the row's primary key is set, any of the other row sets for view objects based on the same ServiceRequest entity object (like ServiceRequestSummary) receive an event indicating a new row has been created. If their view link consistency flag is enabled, then a copy of the new row is inserted into their row set as well.

27.1.2.2 Understanding the Default View Link Consistency Setting and How to Change It

You can control the default setting for the view link consistency feature using the jbo.viewlink.consistent configuration parameter. The default setting for this parameter is the word "DEFAULT" which has the following meaning. If your view object has:

  • A single entity usage, view link consistency is enabled

  • Multiple entity usages, and:

    • If all secondary entity usages are marked as contributing reference information, then view link consistency is enabled

    • If any secondary entity usage marked as not being a reference view link consistency is disabled.

You can globally disable this feature by setting the jbo.viewlink.consistent to the value false in your configuration. Conversely, you could globally enable this feature by setting jbo.viewlink.consistent to the value true, but Oracle does not recommend doing this. Doing so would force view link consistency to be set on for view objects with secondary entity usages that are not marked as a reference which presently do not support the view link consistency feature well.

To set the feature programmatically, use the setAssociationConsistent() API on any RowSet. When you call this method on a view object, it affects its default row set.

27.1.2.3 Using a RowMatch to Qualify Which New, Unposted Rows Get Added to a Row Set

If a view object has view link consistency enabled, any new row created by another view object based on the same entity object is added to its row set. By default the mechanism adds new rows in an unqualified way. If your view object has a design-time WHERE clause that queries only a certain subset of rows, you can apply a RowMatch object to your view object to perform the same filtering in-memory. The filtering expression of the RowMatch object you specify prevents new rows from being added that wouldn't make sense to appear in that view object.

For example, the ServiceRequestsByStatus view object in the SRDemo application includes a design time WHERE clause like this:

WHERE /* ... */ AND STATUS LIKE NVL(:StatusCode,'%') 

Its custom Java class overrides the create() method as shown in Example 27-1 to force view link consistency to be enabled. It also applies a RowMatch object whose filtering expression matches rows whose Status attribute matches the value of the :StatusCode named bind variable (or matches any row if :StatusCode = '%'). This RowMatch filter is used by the view link consistency mechanism to qualify the row that is a candidate to add to the row set. If the row qualifies by the RowMatch, it is added. Otherwise, it is not.

Example 27-1 Providing a Custom RowMatch to Control Which New Rows are Added

// In ServiceRequestsByStatusImpl.java
protected void create() {
  super.create();
  setAssociationConsistent(true);
  setRowMatch(new RowMatch("Status = :StatusCode or :StatusCode = '%'"));  
} 

See Section 27.5.4, "Performing In-Memory Filtering with RowMatch" for more information on creating and using a RowMatch object.

Note:

If the RowMatch facility does not provide enough control, you can override the view object's rowQualifies() method to implement a custom filtering solution. Your code can determine whether a new row qualifies to be added by the view link consistency mechanism or not.

27.1.2.4 Setting a Dynamic Where Clause Disables View Link Consistency

If you call setWhereClause() on a view object to set a dynamic where clause, the view link consistency feature is disabled on that view object. If you have provided an appropriate custom RowMatch object to qualify new rows for adding to the row set, you can call setAssociationConsistent(true) after setWhereClause() to re-enable view link consistency.

27.1.2.5 New Row from Other View Objects Added at the Bottom

If a row set has view link consistency enabled, then new rows added due to creation by other row sets are added to the bottom of the row set.

27.1.2.6 New, Unposted Rows Added to Top of RowSet when Re-Executed

If a row set has view link consistency enabled, then when you call the executeQuery() method, any qualifying new, unposted rows are added to the top of the row set before the queried rows from the database are added.

27.1.3 Understanding View Link Accessors Versus Data Model View Link Instances

View objects support two different styles of master-detail coordination:

  • View link instances for active data model master/detail coordination

  • View link accessor attributes for programmatically accessing detail row sets on demand

27.1.3.1 Enabling a Dynamic Detail Row Set with Active Master/Detail Coordination

When you add a view link instance to your application module's data model, you connect two specific view object instances and indicate that you want active master/detail coordination between the two. At runtime the view link instance in the data model facilitates the eventing that enables this coordination. Whenever the current row is changed on the master view object instance, an event causes the detail view object to be refreshed by automatically invoking executeQuery() with a new set of bind parameters for the new current row in the master view object.

A key feature of this active data model master/detail is that the master and detail view object instances are stable objects to which client user interfaces can establish bindings. When the current row changes in the master — instead of producing a new detail view object instance — the existing detail view object instance updates its default row set to contain a the set of rows related to the new current master row. In addition, the user interface binding objects receive events that allow the display to update to show the detail view object's refreshed row set.

Another key feature that is exclusive to active data model master/detail is that a detail view object instance can have multiple master view object instances. For example, an ExpertiseAreas view object instance may be a detail of both a Products and a Technicians view object instances. Whenever the current row in either the Products or Technicians view object instance changes, the default row set of the detail ExpertiseAreas view object instance is refreshed to include the row of expertise area information for the current technician and the current product. See Section 27.1.6, "Setting Up a Data Model with Multiple Masters" for details on setting up a detail view object instance with multiple-masters.

27.1.3.2 Accessing a Stable Detail Row Set Using View Link Accessor Attributes

When you need to programmatically access the detail row set related to a view object row by virtue of a view link, you can use the view link accessor attribute. You control the name of the view link accessor attribute on the View Link Properties panel of the View Link Editor. Assuming you've named your accessor attribute AccessorAttrName, you can access the detail row set using the generic getAttribute() API like:

RowSet detail = (RowSet)currentRow.getAttribute("AccessorAttrName");

If you've generated a custom view row class for the master view object and exposed the getter method for the view link accessor attribute on the client view row interface, you can write strongly-typed code to access the detail row set like this:

RowSet detail = (RowSet)currentRow.getAccessorAttrName();

Unlike the active data model master/detail, programmatic access of view link accessor attributes does not require a detail view object instance in the application module's data model. Each time you invoke the view link accessor attribute, it returns a RowSet containing the set of detail rows related to the master row on which you invoke it.

Using the view link accessor attribute, the detail data rows are stable. As long as the attribute value(s) involved in the view link definition in the master row remain unchanged, the detail data rows will not change. Changing of the current row in the master does not affect the detail row set which is "attached" to a given master row. For this reason, in addition to being useful for general programmatic access of detail rows, view link accessor attributes are appropriate for UI object like the tree control, where data for each master node in a tree needs to retain its distinct set of detail rows.

27.1.3.3 Accessor Attributes Create Distinct Row Sets Based on an Internal View Object

When you combine the use of active data model master/detail with programmatic access of detail row sets using view link accessor, it is even more important to understand that they are distinct mechanisms. For example, imagine that you have:

  • Defined ServiceRequests and ServiceHistories view objects

  • Defined a view link between them, naming the view link accessor HistoriesForRequest

  • Added instances of them to an application module's data model named master (of type ServiceRequests) and detail (of type ServiceHistories) coordinated actively by a view link instance.

If you find a service request in the master view object instance, the detail view object instance updates as expected to show the corresponding service request histories. At this point, if you invoke a custom method that programmatically accesses the HistoriesForRequest view link accessor attribute of the current ServiceRequests row, you get a RowSet containing the set of ServiceHistory rows. You might reasonably expect this programmatically access RowSet to have come from the detail view object instance in the data model, but this is not the case.

The RowSet returned by a view link accessor always originates from an internally created view object instance, not one you that added to the data model. This internal view object instance is created as needed and added with a system-defined name to the root application module.

The principal reason a distinct, internally-created view object instance is used is to guarantee that it remains unaffected by developer-related changes to their own view objects instances in the data model. For example, if the view row were to use the detail view object in the data model for view link accessor RowSet, the resulting row set could be inadvertently affected when the developer dynamically:

  1. Adds a WHERE clause with new named bind parameters

    If such a view object instance were used for the view link accessor result, unexpected results or an error could ensue because the dynamically-added WHERE clause bind parameter values have not been supplied for the view link accessor's RowSet: they were only supplied for the default row set of the detail view object instance in the data model.

  2. Adds an additional master view object instance for the detail view object instance in the data model.

    In this scenario, the semantics of the accessor would be changed. Instead of the accessor returning ServiceHistory rows for the current ServiceRequest row, it could all of a sudden start returning only the ServiceHistory rows for the current ServiceRequest that were created by a current technician, for example.

  3. Removes the detail view object instance or its containing application module instance.

    In this scenario, all rows in the programmatically-accessed detail RowSet would become invalid.

Furthermore, Oracle ADF needs to distinguish between the active data model master/detail and view link accessor row sets for certain operations. For example, when you create a new row in a detail view object, the framework automatically populates the attributes involved in the view link with corresponding values of the master. In the active data model master/detail case, it gets these values from the current row(s) of the possibly multiple master view object instances in the data model. In the case of creating a new row in a RowSet returned by a view link accessor, it populates these values from the master row on which the accessor was called.

27.1.4 Presenting and Scrolling Data a Page at a Time Using the Range

To present and scroll through data a page at a time, you can configure a view object to manage for you an appropriately-sized range of rows. The range facility allows a client to easily display and update a subset of the rows in a row set, as well as easily scroll to subsequent pages N rows as a time. You call setRangeSize() to define how many rows of data should appear on each page. The default range size is one (1) row. A range size of minus one (-1) indicates the range should include all rows in the row set.

Note:

When using the ADF Model layer's declarative data binding, the iterator binding in the page definition has a RangeSize property. At runtime, the iterator binding invokes the setRangeSize() method on its corresponding row set iterator, passing the value of this RangeSize property. The ADF design time by default sets this RangeSize property to 10 rows for most iterator bindings. An exception is the range size specified for a List binding to supply the set of valid values for a UI component like a dropdown list. In this case, the default range size is minus one (-1) to allow the range to include all rows in the row set.

When you set a range size greater than one, you control the row set paging behavior using the iterator mode. The two iterator mode flags you can pass to the setIterMode() method are:

  • RowIterator.ITER_MODE_LAST_PAGE_PARTIAL

    In this mode, the last page of rows may contain fewer rows than the range size. For example, if you set the range size to 10 and your row set contains 23 rows, the third page of rows will contain only three rows. This is the style that works best for web applications.

  • RowIterator.ITER_MODE_LAST_PAGE_FULL

    In this mode, the last page of rows is kept full, possibly including rows at the top of the page that had appeared at the bottom of the previous page. For example, if you set the range size to 10 and your row set contains 23 rows, the third page of rows will contain 10 rows, the first seven of which appeared as the last seven rows of page two. This is the style that works best for desktop-fidelity applications using Swing.

27.1.5 Efficiently Scrolling Through Large Result Sets Using Range Paging

As a general rule, for highest performance, Oracle recommends building your application in a way that avoids giving the end-user the opportunity to scroll through very large query results. To enforce this recommendation, call the getEstimatedRowCount() method on a view object to determine how many rows would be returned the user's query before actually executing the query and allowing the user to proceed. If the estimated row count is unreasonably large, your application can demand that the end-user provide additional search criteria.

However, when you must work with very large result sets, you can use the view object's access mode called "range paging" to improve performance. The feature allows your applications to page back and forth through data, a range of rows at a time, in a way that is more efficient for large data sets than the default "scrollable" access mode.

27.1.5.1 Understanding How to Oracle Supports "TOP-N" Queries

The Oracle database supports a feature called a "Top-N" query to efficiently return the first N ordered rows in a query. For example, if you have a query like:

SELECT EMPNO, ENAME,SAL FROM EMP ORDER BY SAL DESC

If you want to retrieve the top 5 employees by salary, you can write a query like:

SELECT * FROM (
   SELECT X.*,ROWNUM AS RN FROM (
      SELECT EMPNO,ENAME,SAL FROM EMP ORDER BY SAL DESC 
   ) X
) WHERE RN <= 5

which gives you results like:

EMPNO ENAME       SAL   RN
---------- -------- ------ ----
      7839 KING       5000    1
      7788 SCOTT      3000    2
      7902 FORD       3000    3
      7566 JONES      2975    4
      7698 BLAKE      2850    5

The feature is not only limited to retrieving the first N rows in order. By adjusting the criteria in the outmost WHERE clause you can efficiently retrieve any range of rows in the query's sorted order. For example, to retrieve rows 6 through 10 you could alter the query this way:

SELECT * FROM (
  SELECT X.*,ROWNUM AS RN FROM  (
     SELECT EMPNO,ENAME,SAL FROM EMP ORDER BY SAL DESC
  ) X
) WHERE RN BETWEEN 6 AND 10

Generalizing this idea, if you want to see page number P of the query results, where each page contains R rows, then you would write a query like:

SELECT * FROM (
  SELECT X.*,ROWNUM AS RN FROM (
    SELECT EMPNO,ENAME,SAL FROM EMP ORDER BY SAL DESC
  ) X
) WHERE RN BETWEEN ((:P - 1) * :R) + 1 AND (:P) * :R

As the result set you consider grows larger and larger, it becomes more and more efficient to use this technique to page through the rows. Rather than retrieving hundreds or thousands of rows over the network from the database, only to display ten of them on the page, instead you can produce a clever query to retrieve only the R rows on page number P from the database. No more than a handful of rows at a time needs to be returned over the network when you adopt this strategy.

To implement this database-centric paging strategy in your application, you could handcraft the clever query yourself and write code to manage the appropriate values of the :R and :P bind variables. Alternatively, you can use the view object's range paging access mode, which implements it automatically for you.

27.1.5.2 How to Enable Range Paging for a View Object

To enable range paging for your view object, first call setRangeSize() to define the number of rows per page, then call the following method:

yourViewObject.setAccessMode(RowSet.RANGE_PAGING);

27.1.5.3 What Happens When You Enable Range Paging

When a view object's access mode is set to RANGE_PAGING, the view object takes its default query like:

SELECT EMPNO, ENAME, SAL FROM EMP ORDER BY SAL DESC

and automatically "wraps" it to produce a Top-N query.

For best performance, the statement uses a combination of greater than and less than conditions instead of the BETWEEN operator, but the logical outcome is the same as the Top-N wrapping query you saw above. The actual query produced to wrap a base query of:

SELECT EMPNO, ENAME, SAL FROM EMP ORDER BY SAL DESC

looks like this:

SELECT * FROM (
  SELECT /*+ FIRST_ROWS */ IQ.*, ROWNUM AS Z_R_N FROM (
    SELECT EMPNO, ENAME, SAL FROM EMP ORDER BY SAL DESC
  ) IQ  WHERE ROWNUM < :0)
WHERE Z_R_N > :1

The two bind variables are bound as follows:

  • :1 index of the first row in the current page

  • :0 is bound to the last row in the current page

27.1.5.4 How are View Rows Cached When Using Range Paging?

When a view object operates in RANGE_PAGING access mode, it only keeps the current range (or "page") of rows in memory in the view row cache at a time. That is, if you are paging through results ten at a time, then on the first page, you'll have rows 1 through 10 in the view row cache. When you navigate to page two, you'll have rows 11 through 20 in the cache. This also can help make sure for large row sets that you don't end up with tons of rows cached just because you want to preserve the ability to scroll backwards and forwards.

27.1.5.5 How to Scroll to a Given Page Number Using Range Paging

When a view object operates in RANGE_PAGING access mode, to scroll to page number N call its scrollToRangePage() method, passing N as the parameter value.

27.1.5.6 Estimating the Number of Pages in the Row Set Using Range Paging

When a view object operates in RANGE_PAGING access mode, you can access an estimate of the total number of pages the entire query result would produce using the getEstimatedRangePageCount() method.

27.1.5.7 Accommodating Inserts and Deletes Using Auto Posting

The range paging access mode is typically used for paging through read-only row sets, and often is used with read-only view objects. You allow the user to find the row they are looking for by paging through a large row set with range paging access mode, then you use the Key of that row to find the selected row in a different view object for editing.

Additionally, the view object supports a RANGE_PAGING_AUTO_POST access mode to accommodate the inserting and deleting of rows from the row set. This mode behaves like the RANGE_PAGING mode, except that it eagerly calls postChanges() on the database transaction whenever any changes are made to the row set. This communicates the pending changes to the database via appropriate INSERT, UPDATE, or DELETE statements so that the changes are preserved when scrolling backward or forward.

27.1.5.8 Understanding the Tradeoffs of Using Range Paging Mode

You might ask yourself, "Why wouldn't I always want to use RANGE_PAGING mode?" The answer is that using range paging potentially causes more overall queries to be executed as you are navigating forward and backward through your view object rows. You would want to avoid using RANGE_PAGING mode in these situations:

  • You plan to read all the rows in the row set immediately (for example, to populate a dropdown list).

    In this case your range size would be set to -1 and there really is only a single "page" of all rows, so range paging does not add value.

  • You need to page back and forth through a small-sized row set.

    If you have 100 rows or fewer, and are paging through them 10 at a time, with RANGE_PAGING mode you will execute a query each time you go forward and backward to a new page. In normal mode, you will cache the view object rows as you read them in, and paging backwards through the previous pages will not re-execute queries to show those already-seen rows.

In the case of a very large (or unpredictably large) row set, the trade off of potentially doing a few more queries — each of which only returns up to the RangeSize number of rows from the database — is more efficient then trying to cache all of the previously-viewed rows. This is especially true if you allow the user to jump to an arbitrary page in the list of results. Doing so in normal, scrollable mode requires fetching and caching all of the rows between the current page and the page the users jumps to. In RANGE_PAGING mode, it will ask the database just for the rows on that page. Then, if the user jumps back to a page of rows that they have already visited, in RANGE_PAGING mode, those rows get re-queried again since only the current page of rows is held in memory in this mode.

27.1.6 Setting Up a Data Model with Multiple Masters

When useful, you can set up your data model to have multiple master view object instances for the same detail view object instance. Consider view objects named Technicians, Products, and ExpertiseAreas with view links defined between:

  • Products and ExpertiseAreas

  • Technicians and ExpertiseAreas

Note:

The example in this section refer to the MultipleMasters project in the AdvancedViewObjectExamples workspace. See the note at the beginning of this chapter for download instructions.

Figure 27-1 shows what the data model panel looks like when you've configured both Technicians and Products view object instances to be masters of the same ExpertiseAreas view object instance.

Figure 27-1 Multiple Master View Object Instances for the Same Detail

Image of Data Model panel

To set up the data model as shown in Figure 27-1 open the Application Module Editor and follow these steps on the Data Model panel:

  1. Add an instance of the Technicians view object to the data model.

    Assume you name it Technicians.

  2. Add an instance of the Products view object to the data model

    Assume you name it Products.

  3. Select the Technicians view object instance in the Data Model list

  4. In the Available View Objects list, select the ExpertiseAreas view object indented beneath the Technicians view object, enter the view object instance name of ExpertiseAreas in the Name field, and click > to shuttle it into data model as a detail of the existing Technicians view object instance.

  5. Select the Products view object instance in the Data Model list

  6. In the Available View Objects list, select the ExpertiseAreas view object indented beneath the Products view object, enter the same view object instance name of ExpertiseAreas in the Name field, and click > to shuttle it into data model as a detail of the existing Products view object instance.

    An alert will appear: An instance of a View Object with the name ExpertiseAreas has already been used in the data model. Would you like to use the same instance?

  7. Click Yes to confirm you want the ExpertiseAreas view object instance to also be the detail of the Products view object instance.

27.1.7 Understanding When You Can Use Partial Keys with findByKey()

View objects based on multiple entity usages support the ability to find view rows by specifying a partially populated key. A partial key is a multi-attribute Key object with some of its attributes set to null. However, there are strict rules about what kinds of partial keys can be used to perform the findByKey().

If a view object is based on N entity usages, where N > 1, then the view row key is by default comprised of all of the primary key attributes from all of the participating entity usages. Only the ones from the first entity object are required to participate in the view row key, but by default all of them do.

If you allow the key attributes from some secondary entity usages to remain as key attributes at the view row level, then you should leave all of the attributes that form the primary key for that entity object as part of the view row key. Assuming you have left the one or more key attributes in the view row for M of the N entity usages, where (M <= N), then you can use findByKey() to find rows based on any subset of these M entity usages. Each entity usage for which you provide values in the Key object, requires that you must provide non-null values for all of the attributes in that entity's primary key.

You have to follow this rule because when a view object is based on at least one or more entity usages, its findByKey() method finds rows by delegating to the findByPrimaryKey() method on the entity definition corresponding to the first entity usage whose attributes in the view row key are non-null. The entity definition's findByPrimaryKey() method requires all key attributes for any given entity object to be non-null in order to find the entity row in the cache.

As a concrete example, imagine that you have a ServiceRequests view object with an ServiceRequest entity object as its primary entity usage, and a Product entity as secondary reference entity usage. Furthermore, assume that you leave the Key Attribute property of both of the following view row attributes set to true:

  • SvrId — primary key for the ServiceRequest entity

  • ProdId1 — primary key for the Product entity

The view row key will therefore be the (SvrId, ProdId1) combination. When you do a findByKey(), you can provide a Key object that provides:

  • A completely specified key for the underlying ServiceRequest entity

    Key k = new Key(new Object[]{new Number(200), null});
    
  • A completely specified key for the underlying Product entity

    Key k = new Key(new Object[]{null, new Number(118)}); 
    
  • A completely specified key for both entities

    Key k = new Key(new Object[]{new Number(200), new Number(118)});
    

When a valid partial key is specified, the findByKey() method can return multiple rows as a result, treating the missing entity usage attributes in the Key object as a wildcard.

27.1.8 Creating Dynamic Attributes to Store UI State

You can add one or more dynamic attributes to a view object at runtime using the addDynamicAttribute() method. Dynamic attributes can hold any object as their value. Typically, you will consider using dynamic attributes when writing generic framework extension code that requires storing some additional per-row transient state to implement a feature you want to add to the framework in a global, generic way.

27.1.9 Working with Multiple Row Sets and Row Set Iterators

While you typically work with a view object's default row set, you can call the createRowSet() method on the ViewObject interface to create secondary, named row sets based on the same view object's query. One situation where this could make sense is when your view object's SQL query contains named bind variables. Since each RowSet object stores its own copy of bind variable values, you could use a single view object to produce and process multiple row sets based on different combinations of bind variables values. You can find a named row set you've created using the findRowSet() method. When you're done using a secondary row set, call its closeRowSet() method.

For any RowSet, while you typically work with its default row set iterator, you can call the createRowSetIterator() method of the RowSet interface to create secondary, named row set iterators. You can use find a named row set iterator you've created using the findRowSetIterator() method. When you're done using a secondary row set iterator, call its closeRowSetIterator() method.

Note:

Through the ADF Model declarative data binding layer, user interface pages or panels in your application work with the default row set iterator of the default row set of view objects in the application module's data model. Due to this fact, the most typical scenario for creating secondary row set iterators is to write business logic that iterates over a view object's default row set without disturbing the current row of the default row set iterator used by the user interface layer.

27.1.10 Optimizing View Link Accessor Access By Retaining the Row Set

Each time you retrieve a view link accessor row set, by default the view object creates a new RowSet object to allow you to work with the rows. This does not imply re-executing the query to produce the results each time, only creating a new instance of a RowSet object with its default iterator reset to the "slot" before the first row. To force the row set to refresh its rows from the database, you can call its executeQuery() method.

Since there is a small amount of overhead associated with creating the row set, if your code makes numerous calls to the same view link accessor attributes you can consider enabling view link accessor row set retention for the source view object in the view link. To use the view link accessor retention feature, enable a custom Java class for your view object, override the create() method, and add a line after super.create() that calls the setViewLinkAccessorRetained() method passing true as the parameter. It affects all view link accessor attributes for that view object.

When this feature is enabled for a view object, since the view link accessor row set it not recreated each time, the current row of its default row set iterator is also retained as a side-effect. This means that your code will need to explicitly call the reset() method on the row set you retrieve from the view link accessor to reset the current row in its default row set iterator back to the "slot" before the first row.

Note, however, that with accessor retention enabled, your failure to call reset() each time before you iterate through the rows in the accessor row set can result in a subtle, hard-to-detect error in your application. For example, if you iterate over the rows in a view link accessor row set like this, for example to calculate some aggregate total:

RowSet rs = (RowSet)row.getAttribute("ServiceRequestsForProduct");
while (rs.hasNext()) {
  Row r = rs.next();
  // Do something important with attributes in each row
}

The first time you work with the accessor row set the code will work. However, since the row set (and its default row set iterator) are retained, the second and subsequent times you access the row set the current row will already be at the end of the row set and the while loop will be skipped since rs.hasNext() will be false. Instead, with this feature enabled, write your accessor iteration code like this:

RowSet rs = (RowSet)row.getAttribute("ServiceRequestsForProduct");
rs.reset(); // Reset default row set iterator to slot before first row!
while (rs.hasNext()) {
  Row r = rs.next();
  // Do something important with attributes in each row
}

Recall that if view link consistency is on, when the accessor is retained the new unposted rows will show up at the end of the row set. This is slightly different from when the accessor is not retained (the default), where new unposted rows will appear at the beginning of the accessor row set.

27.2 Tuning Your View Objects for Best Performance

You can use view objects to read rows of data, create and store rows of transient data, as well as automatically coordinate inserts, updates, and deletes made by end users with your underlying business objects. How you design and use your view objects can definitely affect their performance at runtime. This section provides guidance on configuring your view objects to get the best possible performance.

27.2.1 Use Bind Variables for Parameterized Queries

Whenever the WHERE clause of your query includes values that might change from execution to execution, you should use named bind variables. Their use also protects your application against abuse through SQL injection attacks by malicious end-users.

27.2.1.1 Use Bind Variables to Avoid Re-parsing of Queries

Bind variables are place holders in the SQL string whose value you can easily change at runtime without altering the text of the SQL string itself. Since the query text doesn't change from execution to execution, the database can efficiently reuse the same parsed statement each time. Avoiding re-parsing of your statement alleviates the database from having to continually re-determine its query optimization plan and eliminates contention by multiple end-users on other costly database resources used during this parsing operation. This savings leads to higher runtime performance of your application. See Section 5.9, "Using Named Bind Variables" for details on how to use named bind variables.

27.2.1.2 Use Bind Variables to Prevent SQL-Injection Attacks

Using bind variables for parameterized WHERE clause values is especially important if their values will be supplied by end-users of your application. Consider the example shown in Example 27-2. It adds a dynamic WHERE clause formed by concatenating a user-supplied parameter value into the statement.

Example 27-2 Using String Concatenation Instead of Bind Variables is Vulnerable to SQL-Injection Attacks

// EXAMPLE OF BAD PRACTICE, Do not follow this approach!
String userSuppliedValue = ... ;
yourViewObject.setWhereClause("BANK_ACCOUNT_ID = "+userSuppliedValue);

A user with malicious intentions — if able to learn any details about your application's underlying database schema — could supply a carefully-constructed "bank account number" as a field value or URL parameter like:

BANK_ACCOUNT_ID

When the code in Example 27-2 concatenates this value into the dynamically-applied where clause, what the database sees is a query predicate like this:

WHERE (BANK_ACCOUNT_ID = BANK_ACCOUNT_ID)

This WHERE clause retrieves all bank accounts instead of just the current user's, perhaps allowing the hacker to view private information of another person's account. This technique of short-circuiting an application's WHERE clause by trying to supply a maliciously-constructed parameter value into a SQL statement is called a SQL injection attack. Using named bind variables instead for these situations as shown in Example 27-3 prevents the vulnerability.

Example 27-3 Use Named Bind Variables Instead of String Concatenation

// Best practice using named bind variables
String userSuppliedValue = ... ;
yourViewObject.setWhereClause("BANK_ACCOUNT_ID = :BankAcccountId");
yourViewObject.defineNamedWhereClauseParam("BankAcccountId", null, null);
yourViewObject.setNamedWhereClauseParam("BankAcccountId",userSuppliedValue);

If a malicious user supplies an illegal value in this case, they receive an error your application can handle instead of obtaining data they are not suppose to see.

27.2.2 Use Read-Only View Objects When Entity-Based Features Not Required

View objects can either be related to underlying entity objects or not. When a view object is related to one or more underlying entity objects you can create new rows, and modify or remove queried rows. The view object coordinates with underlying entity objects to enforce business rules and to permanently save the changes. In addition, entity-based view objects:

  • Immediately reflect pending changes made to relevant entity object attributes made through other view objects in the same transaction

  • Initialize attribute values in newly created rows to the values from the underlying entity object attributes

  • Reflect updated reference information when foreign key attribute values are changed

On the other hand, view objects that are not related to any entity object are read-only, do not pickup entity-derived default values, do not reflect pending changes, and do not reflect updated reference information. You need to decide what kind of functionality your application requires and design the view object accordingly. Typically view objects used for SQL-based validation purposes, as well as for displaying the list of valid selections in a dropdown list, can be read-only.

There is a small amount of runtime overhead associated with the coordination between view object rows and entity object rows, so if you don't need any of the functionality offered by an entity-mapped view object, you can slightly increase performance by using a read-only view object with no related entity objects.

27.2.3 Use SQL Tracing to Identify Ill-Performing Queries

After deciding whether your view object should be mapped to entities or not, your attention should turn to the query itself. The Explain Plan button on the Query panel of the View Object Editor allows you to see the query plan that the database query optimizer will use. If you see that it is doing a full table scan, you should consider adding indexes or providing a value for the Optimizer Hint field on the Tuning panel to explicitly control which query plan will be used. These facilities provide some useful tools to the developer to evaluate the query plans for individual view object SQL statements. However, their use is not a substitute for tracing the SQL of the entire application to identify poorly performing queries in the presence of a production environment's amount of data and number of end users.

You can use the Oracle database's SQL Tracing facilities to produce a complete log of all SQL statements your application performs. The approach that works in all versions of the Oracle database is to issue the command:

ALTER SESSION SET SQL_TRACE TRUE

This command enables tracing of the current database session and logs all SQL statements to a server-side trace file until you either enter ALTER SESSION SET SQL_TRACE FALSE or close the connection. To simplify enabling this option to trace your ADF applications, override the afterConnect() method of your application module (or custom application module framework extension class) to conditionally perform the ALTER SESSION command to enable SQL tracing based on the presence of a Java system property as shown in Example 27-4.

Example 27-4 Conditionally Enabling SQL Tracing in an Application Module

// In YourCustomApplicationModuleImpl.java
protected void afterConnect() {
  super.afterConnect();
  if (System.getProperty("enableTrace") != null) {
    getDBTransaction().executeCommand("ALTER SESSION SET SQL_TRACE TRUE");
  }
}  

After producing a trace file, you use the tkprof utility supplied with the database to format the information and to better understand information about each query executed like:

  • The number of times it was (re)parsed

  • The number of times it was executed

  • How many round-trips were made between application server and the database

  • Various quantitative measurements of query execution time

Using these techniques, you can decide which additional indexes might be required to speed up particular queries your application performs, or which queries could be changed to improve their query optimization plan.

Note:

The Oracle 10g database provides the new DBMS_MONITOR package that further simplifies SQL tracing and integrates it with Oracle Enterprise Manager for visually monitoring the most frequently performed query statements your applications perform.

27.2.4 Consider the Appropriate Tuning Settings for Every View Object

The Tuning panel of the View Object Editor lets you set various options that can dramatically effect your query's performance.

27.2.4.1 Set the Database Retrieval Options Appropriately

The Retrieve from the Database section, controls how the view object retrieves rows from the database server. The options for the fetch mode are All Rows, At Most One Row, and No Rows. Most view objects will stick with the default All Rows option, which will be retrieved As Needed or All at Once depending on which option you choose. The "as needed" option ensures that an executeQuery() operation on the view object initially retrieves only as many rows as necessary to fill the first page of a display, whose number of rows is set based on the view object's range size.

For view objects whose WHERE clause expects to retrieve a single row, set the option to At Most One Row for best performance. This way, the view object knows you don't expect any more rows and will skip its normal test for that situation. Finally, if you use the view object only for creating new rows, set the option to No Rows so no query will ever be performed.

27.2.4.2 Consider Whether Fetching One Row at a Time is Appropriate

The fetch size controls how many rows will be returned in each round trip to the database. By default, the framework will fetch rows in batches of one row at a time. If you are fetching any more than one row, you will gain efficiency by setting this in Batches of value.

However the higher the number, the larger the client-side buffer required, so avoid setting this number arbitrarily high. If you are displaying results N rows at a time in the user interface, it's good to set the fetch size to at least N+1 so that each page of results can be retrieved in a single round trip to the database.

Caution:

Unless your query really fetches just one row, leaving the default fetch size of one (1) in the in Batches of field on the Tuning panel is a recipe for bad performance due to many unnecessary round trips between the application server and the database. Oracle strongly recommends considering the appropriate value for each view object's fetch size.

27.2.4.3 Specify a Query Optimizer Hint if Necessary

The Query Optimizer Hint field allows you to specify an optional hint to the Oracle query optimizer to influence what execution plan it will use. At runtime, the hint you provide is added immediately after the SELECT keyword in the query, wrapped by the special comment syntax /*+ YOUR_HINT */. Two common optimizer hints are:

  • FIRST_ROWS — to hint that you want the first rows as quickly as possible

  • ALL_ROWS — to hint that you want all rows as quickly as possible

There are many other optimizer hints that are beyond the scope of this manual to document. Reference the Oracle 10g database reference manuals for more information on available hints.

27.2.5 Creating View Objects at Design Time

It's important to understand the overhead associated with creating view objects at runtime. Avoid the temptation to do this without a compelling business requirement. For example, if your application issues a query against a table whose name you know at design time and if the list of columns to retrieve is also fixed, then create a view object at design time. When you do this, your SQL statements are neatly encapsulated, can be easily explained and tuned during development, and incur no runtime overhead to discover the structure and data types of the resulting rows.

In contrast, when you use the createViewObjectFromQueryStmt() API on the ApplicationModule interface at runtime, your query is buried in code, it's more complicated to proactively tune your SQL, and you pay a performance penalty each time the view object is created. Since the SQL query statement for a dynamically-created view object could theoretically be different on each execution, an extra database round trip is required to discover the "shape" of the query results on-the-fly. Only create queries dynamically if you cannot know the name of the table to query until runtime. Most other needs can be addressed using a design-time created view object in combination with runtime API's to set bind variables in a fixed where clause, or to add an additional WHERE clause (with optional bind variables) at runtime.

27.2.6 Use Forward Only Mode to Avoid Caching View Rows

Often you will use write code that programmatically iterates through the results of a view object. A typical situation will be custom validation code that must process multiple rows of query results to determine whether an attribute or an entity is valid or not. In these cases, if you intend to read each row in the row set a single time and never require scrolling backward or re-iterating the row set a subsequent time, then you can use "forward only" mode to avoid caching the retrieved rows. To enable forward only mode, call setForwardOnly(true) on the view object.

Note:

Using a read-only view object (with no entity usages) in forward-only mode with an appropriately tuned fetch size is the most efficient way to programmatically read data.

You can also use forward-only mode to avoid caching rows when inserting, updating, or deleting data as long as you never scroll backward through the row set and never call reset() to set the iterator back to the first row. Forward only mode only works with a range size of one (1).

27.3 Using Expert Mode for Full Control Over SQL Query

When defining entity-based view objects, you can fully-specify the WHERE and ORDER BY clauses, whereas, by default, the FROM clause and SELECT list are automatically derived. The names of the tables related to the participating entity usages determine the FROM clause, while the SELECT list is based on the:

When you require full control over the SELECT or FROM clause in a query, you can enable "Expert Mode".

27.3.1 How to Enable Expert Mode for Full SQL Control

To enable expert mode, select Expert Mode on the SQL Statement panel of the Create View Object wizard or View Object Editor.

27.3.2 What Happens When You Enable Expert Mode

When you enable expert mode, the read-only Generated Statement section of the SQL Statement panel becomes a fully-editable Query Statement text box, displaying the full SQL statement. Using this text box, you can change every aspect of the SQL query.

For example, Example 27-4 shows the SQL Statement page of the View Object editor for the SRDemo application's ServiceHistories view object. It's an expert mode, entity-based view object that references a PL/SQL function context_pkg.app_user_name and joins the USERS table an additional time in the FROM clause to filter hidden service history notes from end-users who are not in the technician or manager roles.

Figure 27-2 ServiceHistories Expert Mode View Object in the SRDemo Application

Image of SQL Statement page of View Object editor

27.3.3 What You May Need to Know

{para}?>

27.3.3.1 You May Need to Perform Manual Attribute Mapping

The automatic cooperation of a view object with its underlying entity objects depends on correct attribute-mapping metadata saved in the XML component definition. This information relates the view object attributes to corresponding attributes from participating entity usages. JDeveloper maintains this attribute mapping information in a fully-automatic way for normal entity-based view objects. However, when you decide to use expert mode with a view object, you need to pay attention to the changes you make to the SELECT list. That is the part of the SQL query that directly relates to the attribute mapping. Even in expert mode, JDeveloper continues to offer some assistance in maintaining the attribute mapping metadata when you do the following to the SELECT list:

  • Reorder an expression without changing its column alias

    JDeveloper reorders the corresponding view object attribute and maintains the attribute mapping.

  • Add a new expression

  • JDeveloper adds a new SQL-calculated view object attribute with a corresponding Camel-Capped name based on the column alias of the new expression.

  • Remove an expression

    JDeveloper converts the corresponding SQL-calculated or entity-mapped attribute related to that expression to a transient attribute.

However, if you rename a column alias in the SELECT list, JDeveloper has no way to detect this, so it is treated as if you removed the old column expression and added a new one of a different name.

After making any changes to the SELECT list of the query, visit the Attribute Mappings panel to ensure that the attribute mapping metadata is correct. The table on this panel, which is disabled for view objects in normal mode, becomes enabled for expert mode view objects. For each view object attribute, you will see its corresponding SQL column alias in the table. By clicking into a cell in the View Attributes column, you can use the dropdown list that appears to select the appropriate entity object attribute to which any entity-mapped view attributes should correspond.

Note:

If the view attribute is SQL-calculated or transient, a corresponding attribute with a ÒSQLÓ icon appears in the View Attributes column to represent it. Since these attributes are not related to underlying entity objects, there is no entity attribute related information required for them.

27.3.3.2 Disabling Expert Mode Loses Any Custom Edits

When you disable expert mode for a view object it will return to having its SELECT and FROM clause be derived again. JDeveloper warns you that doing this might lose any of your custom edits to the SQL statement. If this is what you want, after acknowledging the alert, your view object's SQL query reverts back to the default.

27.3.3.3 Once In Expert Mode, Changes to SQL Expressions Are Ignored

Consider a Products view object with a SQL-calculated attribute named Shortens whose SQL expression you defined as SUBSTR(NAME,1,10). If you switch this view object to expert mode, the Query Statement box will show a SQL query like this:

SELECT Products.PROD_ID, 
       Products.NAME, 
       Products.IMAGE, 
       Products.DESCRIPTION, 
       SUBSTR(NAME,1,10) AS SHORT_NAME
FROM PRODUCTS Products

If you go back to the attribute definition for the Shortens attribute and change the SQL Expression field from SUBSTR(NAME,1,10) to SUBSTR(NAME,1,15), then the change will be saved in the view object's XML component definition. Note, however, that the SQL query will remain as above. This occurs because JDeveloper never tries to modify the text of an expert mode query. In expert mode, the developer is in full control. JDeveloper attempts to adjust metadata as described above in function of some kinds of changes you make yourself to the expert mode SQL statement, but it does not perform the reverse. Therefore, if you change view object metadata, the expert mode SQL statement is not updated to reflect it.

To make the above change to the SQL calculated Shortens attribute, you need to update the expression in the expert mode SQL statement itself. To be 100% thorough, you should make the change both in the attribute metadata and in the expert mode SQL statement. This would ensure — if you (or another developer on your team) ever decides to toggle expert mode off at a later point in time — that the automatically derived SELECT list would contain the correct SQL-derived expression.

Note:

If you find you had to make numerous changes to the view object metadata of an expert mode view object, you can consider the following technique to avoid having to manually translate any effects those changes might have implied to the SQL statement yourself. First, copy the text of your customized query to a temporary file. Then, disable expert mode for the view object and acknowledge the warning that you will lose your changes. At this point JDeveloper will re-derive the correct generated SQL statement based on all the new metadata changes you've made. Finally, you can enable expert mode once again and re-apply your SQL customizations.

27.3.3.4 Don't Map Incorrect Calculated Expressions to Entity Attributes

When changing the SELECT list expression that corresponds to entity-mapped attributes, don't introduce SQL calculations that change the value of the attribute when retrieving the data. To illustrate the problem that will occur if you do this, consider the following query for a simple entity-based view object named Products:

SELECT Products.PROD_ID, 
       Products.NAME, 
       Products.IMAGE, 
       Products.DESCRIPTION
FROM PRODUCTS Products

Imagine that you wanted to limit the name column to showing only the first ten characters of the name for some use case. The correct way to do that would be to introduce a new SQL-calculated field called ShortName with an expression like SUBSTR(Products.NAME,1,10). However, one way you might have thought to accomplish this was to switch the view object to expert mode and change the SELECT list expression for the entity-mapped NAME column to the following:

SELECT Products.PROD_ID, 
       SUBSTR(Products.NAME,1,10) AS NAME, 
       Products.IMAGE, 
       Products.DESCRIPTION
FROM PRODUCTS Products

This alternative strategy would initially appear to work. At runtime, you see the truncated value of the name as you are expecting. However, if you modify the row, when the underlying entity object attempts to lock the row it does the following:

  • Issues a SELECT FOR UPDATE statement, retrieving all columns as it tries to lock the row.

  • If the entity object successfully locks the row, it compares the original values of all the persistent attributes in the entity cache as they were last retrieved from the database with the values of those attributes just retrieved from the database during the lock operation.

  • If any of the values differs, then the following error is thrown:

    (oracle.jbo.RowInconsistentException) 
    JBO-25014: Another user has changed the row with primary key [...]
    

If you see an error like this at runtime even though you are the only user testing the system, it is most likely due to your inadvertently introducing a SQL function in your expert mode view object that changed the selected value of an entity-mapped attribute. In the example above, the SUBSTR(Products.NAME,1,10) function introduced causes the original selected value of the Name attribute to be truncated. When the row-lock SQL statement selects the value of the NAME column, it will select the entire value. This will cause the comparison described above to fail, producing the "phantom" error that another user has changed the row.

The same thing would happen with NUMBER, or DATE valued attributes if you inadvertently apply SQL functions in expert mode to truncate or alter their retrieved values for entity-mapped attributes. If you need to present altered versions of entity-mapped attribute data, introduce a new SQL-calculated attribute with the appropriate expression to handle the job.

27.3.3.5 Expert Mode SQL Formatting is Retained

When you switch a view object to expert mode, its XML component definition switches from storing parts of the query in separate XML attributes, to saving the entire query in a single SQLQuery element. The query is wrapped in a XML CDATA section to preserve the line formatting you may have done to make a complex query be easier to understand.

27.3.3.6 Expert Mode Queries Are Wrapped as Inline Views

If your expert-mode view object:

  • Contains a design-time ORDER BY clause specified in the Order By field of the Query Clauses panel, or

  • Has a dynamic where clause or order by clause applied at runtime using setWhereClause() or setOrderByClause()

then its query gets nested into an inline view before applying these clauses. For example, suppose your expert-mode query was defined as:

select USER_ID, EMAIL, FIRST_NAME, LAST_NAME
from USERS 
union all
select USER_ID, EMAIL, FIRST_NAME, LAST_NAME
from INACTIVE_USERS

At runtime, when you set an additional WHERE clause like email = :TheUserEmail, the view object nests its original query into an inline view like this:

SELECT * FROM(
select USER_ID, EMAIL, FIRST_NAME, LAST_NAME
from USERS 
union all
select USER_ID, EMAIL, FIRST_NAME, LAST_NAME
from INACTIVE_USERS) QRSLT

and then adds the dynamic where clause predicate at the end, so that the final query the database sees is:

SELECT * FROM(
select USER_ID, EMAIL, FIRST_NAME, LAST_NAME
from USERS 
union all
select USER_ID, EMAIL, FIRST_NAME, LAST_NAME
from INACTIVE_USERS) QRSLT
WHERE email = :TheUserEmail

This query "wrapping" is necessary in general for expert mode queries since the original query could be arbitrarily complex, including SQL UNION, INTERSECT, MINUS, or other operators that combine multiple queries into a single result. In those cases, simply "gluing" the additional runtime WHERE clause onto the end of the query text could produce unexpected results since. For example, it might only apply to the last of several UNION'ed statements. By nesting the original query verbatim into an inline view, the view object guarantees that your additional WHERE clause is correctly used to filter the results of the original query, regardless of how complex it is.

27.3.3.7 Disabling the Use of Inline View Wrapping at Runtime

Due to the inline view wrapping of expert mode view objects, the dynamically-added WHERE clause can only refer to columns in the SELECT list of the original query. To avoid this limitation, when necessary you can disable the use of the inline view wrapping by calling setNestedSelectForFullSql(false).

27.3.3.8 Enabling Expert Mode May Impact Dependent Objects

When you modify a query to be in expert mode after you have already created:

  • View links involving it, or

  • Other view objects that extend it

JDeveloper will warn you with the alert shown in Figure 27-3 to remind you that you should revisit these dependent components to ensure their SQL statements still reflect the correct query.

Figure 27-3 Proactive Reminder to Revisit Dependent Components

Image of dependent components dialog reminder

For example, if you were to modify the ServiceRequests view object in the SRDemo application to use expert mode, since the ServiceRequestsByStatus view object extends it, you need to revisit the extended component to ensure its query still logically reflects an extension of the modified parent component.

27.4 Working with Multiple Named View Criteria

You can define multiple named view criteria and then selectively apply any combination of them to your view object at runtime as needed.

Note:

The examples in this section refer to the MultipleViewCriterias project in the AdvancedViewObjectExamples workspace. See the note at the beginning of this chapter for download instructions.

27.4.1 Defining Named View Criteria

To define named view criteria, you override the create() method in your view object's custom Java class and call the putViewCriteria() method to define one or more named ViewCriteria objects.

For example, given a Users view object based on the USERS table in the SRDemo schema, you could override the create() method as shown in Example 27-5 to define named view criteria called CountryIsUS, CountryIsNotUS, IsStaff, and IsCustomer by calling appropriate helper methods.

Example 27-5 Defining Multiple Named View Criteria in an Overridden create() Method

package devguide.advanced.multiplevc;
// Imports omitted
public class UsersImpl extends ViewObjectImpl implements Users {
  // etc.
  protected void create() {
    super.create();
    defineCountryIsUSCriteria();
    defineCountryIsNotUSCriteria();
    defineIsStaffCriteria();
    defineIsCustomerCriteria();
  }
  private void defineCountryIsUSCriteria() {
    ViewCriteria vc = createViewCriteria();
    ViewCriteriaRow vcr = vc.createViewCriteriaRow();
    vcr.setAttribute("CountryId","US");
    vc.add(vcr);
    putViewCriteria("CountryIsUS",vc);
  }
  private void defineCountryIsNotUSCriteria() {
    ViewCriteria vc = createViewCriteria();
    ViewCriteriaRow vcr = vc.createViewCriteriaRow();
    vcr.setAttribute("CountryId","US");
    vcr.setConjunction(ViewCriteriaRow.VCROW_CONJ_NOT);
    vc.add(vcr);
    putViewCriteria("CountryIsNotUS",vc);
  }
  private void defineIsStaffCriteria() {
    ViewCriteria vc = createViewCriteria();
    ViewCriteriaRow vcr = vc.createViewCriteriaRow();
    vcr.setAttribute("UserRole","IN ('technician','manager')");
    vc.add(vcr);
    putViewCriteria("IsStaff",vc);
  }
  private void defineIsCustomerCriteria() {
    ViewCriteria vc = createViewCriteria();
    ViewCriteriaRow vcr = vc.createViewCriteriaRow();
    vcr.setAttribute("UserRole","user");
    vc.add(vcr);
    putViewCriteria("IsCustomer",vc);
  } 
  // etc.  
}

27.4.2 Applying One or More Named View Criteria

To apply one or more named view criteria, use the setApplyViewCriteriaNames() method. This method accepts a String array of the names of the criteria you want to apply. If you apply more than one named criteria, they are AND-ed together in the WHERE clause produced at runtime. Then, you can expose custom methods on the client interface of the view object to encapsulate applying combinations of the named view criteria. For example, Example 27-6 shows custom methods showStaffInUS(), showCustomersOutsideUS(), and showCustomersInUS(), each of which uses the setApplyViewCriteriaNames() method to apply an appropriate combination of named view criteria. Once these methods are exposed on the view object's client interface, at runtime clients can invoke these methods as needed to change the information displayed by the view object.

Example 27-6 Exposing Client Methods to Enable Appropriate Named Criterias

// In UsersImpl.java
public void showStaffInUS() {
  setApplyViewCriteriaNames(new String[]{"CountryIsUS","IsStaff"});
  executeQuery();
}
public void showCustomersOutsideUS() {
  setApplyViewCriteriaNames(new String[]{"CountryIsNotUS","IsCustomer"});
  executeQuery();
}
public void showCustomersInUS() {
  setApplyViewCriteriaNames(new String[]{"CountryIsUS","IsCustomer"});
  executeQuery();
}   

27.4.3 Removing All Applied Named View Criteria

To remove any currently applied named view criteria, use setApplyViewCriteriaNames(null). For example, you could add the showAll() method in Example 27-7 to the Users view object and expose it on the client interface. This would allow clients to return to an unfiltered view of the data when needed.

Example 27-7 Removing All Applied Named View Criterias

// In UsersImpl.java
public void showAll() {
  setApplyViewCriteriaNames(null);
  executeQuery();
}

Note:

The setApplyViewCriterias(null) removes all applied view criteria, but allows you to later reapply any combination of them. In contrast, the clearViewCriterias() method deletes all named view criteria. After calling clearViewCriterias() you would have to use putViewCriteria() again to define new named criteria before you could apply them.

27.4.4 Using the Named Criteria at Runtime

Example 27-8 shows the interesting lines of a TestClient class that works with the Users view object described above. It invokes different client methods on the Users view object interface to show different filtered sets of data. The showRows() method is a helper method that iterates over the rows in the view object to display some attributes.

Example 27-8 Test Client Code Working with Named View Criterias

// In TestClientMultipleViewCriterias.java
Users vo = (Users)am.findViewObject("Users");
vo.showCustomersOutsideUS();
showRows(vo,"After applying view criterias for customers outside US");
vo.showStaffInUS();
showRows(vo,"After applying view criterias for staff in US");
vo.showCustomersInUS();
showRows(vo,"After applying view criterias for customers in US");
vo.showAll();
showRows(vo,"After clearing all view criterias");

Running the TestClient program produces output as follows:

--- After applying view criterias for customers outside US ---
Hermann Baer [user, DE]
John Chen [user, TH]
:
--- After applying view criterias for staff in US ---
David Austin [technician, US]
Bruce Ernst [technician, US]
:
--- After applying view criterias for customers in US ---
Shelli Baida [user, US]
Emerson Clabe [user, US]
:
--- After clearing all view criterias ---
David Austin [technician, US]
Hermann Baer [user, DE]
:

27.5 Performing In-Memory Sorting and Filtering of Row Sets

By default a view object performs its query against the database to retrieve the rows in its resulting row set. However, you can also use view objects to perform in-memory searches and sorting to avoid unnecessary trips to the database.

Note:

The examples in this section refer to the InMemoryOperations project in the AdvancedViewObjectExamples workspace. See the note at the beginning of this chapter for download instructions. The examples illustrate using the in-memory sorting and filtering functionality from the client side using methods on the interfaces in the oracle.jbo package. The same functionality can be, and typically should be, encapsulated inside custom methods of your application module or view object components, which you expose on their respective client interface.

27.5.1 Understanding the View Object's Query Mode

The view object's query mode controls the source used to retrieve rows to populate its row set. The setQueryMode() allows you to control which mode, or combination of modes, are used:

  • ViewObject.QUERY_MODE_SCAN_DATABASE_TABLES

    This is the default mode that retrieves results from the database.

  • ViewObject.QUERY_MODE_SCAN_VIEW_ROWS

    This mode uses rows already in the row set as the source, allowing you to progressively refine the row set's contents through in-memory filtering.

  • ViewObject.QUERY_MODE_SCAN_ENTITY_ROWS

    This mode, valid only for entity-based view objects, uses the entity rows presently in the entity cache as the source to produce results based on the contents of the cache.

You can use the modes individually, or combine them using Java's logical OR operator (X|Y). For example, to create a view object that queries the entity cache for unposted new entity rows, as well as the database for existing rows, you could write code like:

setQueryMode(ViewObject.QUERY_MODE_SCAN_DATABASE_TABLES |
             ViewObject.QUERY_MODE_SCAN_ENTITY_ROWS)

If you combine the query modes, the view object automatically handles skipping of duplicate rows. In addition, there is an implied order to the results that are found:

  1. Scan view rows (if specified)

  2. Scan entity cache (if specified)

  3. Scan database tables (if specified) by issuing a SQL query

If you call the setQueryMode() method to change the query mode, your new setting takes effect the next time you call the executeQuery() method.

27.5.2 Sorting View Object Rows In Memory

To sort the rows in a view object at runtime, use the setSortBy() method. You pass a sort expression that looks like a SQL ORDER BY clause. However, instead of referencing the column names of the table, you use the view object's attribute names. For example, for a view object containing attributes named DaysOpen and CreatedByUser, you could sort the view object first by DaysOpen descending, then by CreatedByUser by calling:

setSortBy("DaysOpen desc, CreatedByUser");

Alternatively, you can use the zero-based attribute index position in the sorting clause like this:

setSortBy("2 desc, 3");

After calling the setSortBy() method, the rows will be sorted the next time you call the executeQuery() method. The view object translates this sorting clause into an appropriate format to use for ordering the rows depending on the query mode of the view object. If you use the default query mode, the SortBy clause is translated into an appropriate ORDER BY clause and used as part of the SQL statement sent to the database. If you use either of the in-memory query modes, then the SortBy by clause is translated into one or more SortCriteria objects for use in performing the in-memory sort.

Note:

While SQL ORDER BY expressions treat column names in a case-insensitive way, the attribute names in a SortBy expression are case-sensitive.

27.5.2.1 Combining setSortBy and setQueryMode for In-Memory Sorting

Example 27-9 shows the interesting lines of code from the TestClientSetSortBy class that uses setSortBy() and setQueryMode() to perform an in-memory sort on the rows produced by a read-only view object ResolvedServiceRequests.

Example 27-9 Combining setSortBy and setQueryMode for In-Memory Sorting

// In TestClientSetSortBy.java
am.getTransaction().executeCommand("ALTER SESSION SET SQL_TRACE TRUE");
ViewObject vo = am.findViewObject("ResolvedServiceRequests");
vo.executeQuery();
showRows(vo,"Initial database results");
vo.setSortBy("DaysOpen desc");
vo.setQueryMode(ViewObject.QUERY_MODE_SCAN_VIEW_ROWS);
vo.executeQuery();
showRows(vo,"After in-memory sorting by DaysOpen desc");
vo.setSortBy("DaysOpen desc, CreatedByUser");
vo.executeQuery();
showRows(vo,"After in-memory sorting by DaysOpen desc, CreatedByUser");

Running the example produces the results:

--- Initial database results ---
106,Ice machine not working,1,mhartste
103,Washing machine leaks,4,ngreenbe
105,Air in dryer not hot,4,jmurman
109,Freezer is not cold,4,jwhalen
:
--- After in-memory sorting by DaysOpen desc ---
100,I have noticed that every time I do a...,9,dfaviet
101,Agitator does not work,8,sbaida
103,Washing machine leaks,4,ngreenbe
105,Air in dryer not hot,4,jmurman
:
--- After in-memory sorting by DaysOpen desc, CreatedByUser ---
100,I have noticed that every time I do a...,9,dfaviet
101,Agitator does not work,8,sbaida
105,Air in dryer not hot,4,jmurman
109,Freezer is not cold,4,jwhalen
:

The first line in Example 27-9 containing the executeCommand() call issues the ALTER SESSION SET SQL TRACE command to enable SQL tracing for the current database session. This causes the Oracle database to log every SQL statement performed to a server-side trace file. It records information about the text of each SQL statement, including how many times the database parsed the statement and how many round-trips the client made to fetch batches of rows while retrieving the query result.

Note:

You might need a DBA to grant permission to the SRDEMO account to perform the ALTER SESSION command to do the tracing of SQL output.

Once you've produced a trace file, you can use the tkprof utility that comes with the database to format the file:

tkprof xe_ora_3916.trc trace.prf

This will produces a trace.prf file containing the interesting information shown in Example 27-10 about the SQL statement performed by the ResolvedServiceRequests view object. You can see that after initially querying six rows of data in a single execute and fetch from the database, the two subsequent sorts of those results did not cause any further executions. Since the code set the query mode to ViewObject.QUERY_MODE_SCAN_VIEW_ROWS the setSortBy() followed by the executeQuery() performed the sort in memory.

Example 27-10 TKPROF Output of a Trace File Confirming Sort Was Done In Memory

*************************************************************
SELECT * FROM (select sr.svr_id,
       case 
         when length(sr.problem_description) > 37 then
         rtrim(substr(sr.problem_description,1,37))||'...'
         else sr.problem_description 
       end as problem_description, 
       ceil(
         (select trunc(max(svh_date))
            from service_histories
           where svr_id = sr.svr_id)
         - trunc(request_date)
       ) as days_open,
       u.email as created_by_user
from service_requests sr, users u
 where sr.created_by = u.user_id
   and status = 'Closed') QRSLT  ORDER BY days_open

call    count     cpu  elapsed disk  query  current     rows
------- -----  ------ -------- ---- ------ --------  -------
Parse       1    0.00     0.00    0      0        0        0
Execute     1    0.00     0.00    0      0        0        0
Fetch       1    0.00     0.00    0     22        0        6
------- -----  ------ -------- ---- ------ --------  -------
total       3    0.00     0.00    0     22        0        6
*************************************************************

27.5.2.2 Extensibility Points for In-Memory Sorting

Should you need to customize the way that rows are sorted in memory, you have the following two extensibility points:

  1. You can override the method:

    public void sortRows(Row[] rows)
    

    This method performs the actual in-memory sorting of rows. By overriding this method you can plug in an alternative sorting approach if needed.

  2. You can override the method:

    public Comparator getRowComparator()
    

    The default implementation of this method returns an oracle.jbo.RowComparator. RowComparator invokes the compareTo() method to compare two data values. These methods/objects can be overridden to provide custom compare routines.

27.5.3 Performing In-Memory Filtering with View Criteria

To filter the contents of a row set using ViewCriteria, you can call:

  • applyViewCriteria() or setApplyViewCriteriaNames() followed by executeQuery() to produce a new, filtered row set.

  • findByViewCriteria() to retrieve a new row set to process programmatically without changing the contents of the original row set.

Both of these approaches can be used against the database or to perform in-memory filtering, or both, depending on the view criteria mode. You set the criteria mode using the setCriteriaMode() method on the ViewCriteria object, to which you can pass either of the following integer flags, or the logical OR of both:

  • ViewCriteria.CRITERIA_MODE_QUERY

  • ViewCriteria.CRITERIA_MODE_CACHE

When used for in-memory filtering, the operators supported are ViewCriteria are =, >, <, <=, >=, <>, and LIKE.

Example 27-11 shows the interesting lines from a TestClientFindByViewCriteria class that uses the two features described above both against the database and in-memory. It uses a CustomerList view object instance and performs the following basic steps:

  1. Queries customers from the database with a last name starting with a 'C', producing the output:

    --- Initial database results with applied view criteria ---
    John Chen
    Emerson Clabe
    Karen Colmenares
    
  2. Subsets the results from step 1 in memory to only those with a first name starting with 'J'. It does this by adding a second view criteria row to the view criteria and setting the conjunction to use "AND". This produces the output:

    --- After augmenting view criteria and applying in-memory ---
    John Chen
    
  3. Sets the conjunction back to OR and re-applies the criteria to the database to query customers with last name like 'J%' or first name like 'C%'. This produces the output:

    --- After changing view criteria and applying to database again ---
    John Chen
    Jose Manuel Urman
    Emerson Clabe
    Karen Colmenares
    Jennifer Whalen
    
  4. Defines a new criteria to find customers in-memory with first or last name that contain a letter 'o'

  5. Uses findByViewCriteria() to produce new row set instead of subsetting, producing the output:

    --- Rows returned from in-memory findByViewCriteria ---
    John Chen
    Jose Manuel Urman
    Emerson Clabe
    Karen Colmenares
    
  6. Shows that original row set hasn't changed when findByViewCriteria() was used, producing the output:

    --- Note findByViewCriteria didn't change rows in the view ---
    John Chen
    Jose Manuel Urman
    Emerson Clabe
    Karen Colmenares
    Jennifer Whalen
    

Example 27-11 Performing Database and In-Memory Filtering with View Criteria

// In TestClientFindByViewCriteria.java
ViewObject vo = am.findViewObject("CustomerList");
// 1. Show customers with a last name starting with a 'C'
ViewCriteria vc = vo.createViewCriteria();
ViewCriteriaRow vcr1 = vc.createViewCriteriaRow();
vcr1.setAttribute("LastName","LIKE 'C%'");
vo.applyViewCriteria(vc);
vo.executeQuery();
vc.add(vcr1);
vo.executeQuery();
showRows(vo, "Initial database results with applied view criteria");
// 2. Subset results in memory to those with first name starting with 'J'
vo.setQueryMode(ViewObject.QUERY_MODE_SCAN_VIEW_ROWS);
ViewCriteriaRow vcr2 = vc.createViewCriteriaRow();
vcr2.setAttribute("FirstName","LIKE 'J%'");
vcr2.setConjunction(ViewCriteriaRow.VCROW_CONJ_AND);
vc.setCriteriaMode(ViewCriteria.CRITERIA_MODE_CACHE);
vc.add(vcr2);
vo.executeQuery();
showRows(vo,"After augmenting view criteria and applying in-memory");
// 3. Set conjuction back to OR and re-apply to database query to find
// customers with last name like 'J%' or first name like 'C%'
vc.setCriteriaMode(ViewCriteria.CRITERIA_MODE_QUERY);
vo.setQueryMode(ViewObject.QUERY_MODE_SCAN_DATABASE_TABLES);
vcr2.setConjunction(ViewCriteriaRow.VCROW_CONJ_OR);
vo.executeQuery();
showRows(vo,"After changing view criteria and applying to database again");
// 4. Define new critera to find customers with first or last name like '%o%'
ViewCriteria nameContainsO = vo.createViewCriteria();
ViewCriteriaRow lastContainsO = nameContainsO.createViewCriteriaRow();
lastContainsO.setAttribute("LastName","LIKE '%o%'");
ViewCriteriaRow firstContainsO = nameContainsO.createViewCriteriaRow();
firstContainsO.setAttribute("FirstName","LIKE '%o%'");
nameContainsO.add(firstContainsO);
nameContainsO.add(lastContainsO);
// 5. Use findByViewCriteria() to produce new rowset instead of subsetting
nameContainsO.setCriteriaMode(ViewCriteria.CRITERIA_MODE_CACHE);
RowSet rs = (RowSet)vo.findByViewCriteria(nameContainsO,
                         -1,ViewObject.QUERY_MODE_SCAN_VIEW_ROWS);
showRows(rs,"Rows returned from in-memory findByViewCriteria");
// 6. Show that original rowset hasn't changed
showRows(vo,"Note findByViewCriteria didn't change rows in the view");

27.5.4 Performing In-Memory Filtering with RowMatch

The RowMatch object provides an even more convenient way to express in-memory filtering conditions. You create a RowMatch object by passing a query predicate expression to the constructor like this:

RowMatch rm = 
 new RowMatch("LastName = 'Popp' or (FirstName like 'L%' and LastName like 'D%')");

As you do with the SortBy clause, you phrase the RowMatch expression in terms of the view object attribute names, using the supported operators like =, >, <, <=, >=, <>, and LIKE. You can group subexpressions with parenthesis and use the and and or operators between subexpressions.

Note:

While SQL query predicates treat column names in a case-insensitive way, the attribute names in a RowMatch expression are case-sensitive.

27.5.4.1 Applying a RowMatch to a View Object

To apply a RowMatch to your view object, call the setRowMatch() method. In contrast to a ViewCriteria, the RowMatch is only used for in-memory filtering, so there is no "match mode" to set. You can use a RowMatch on view objects in any supported query mode, and you will see the results of applying it the next time you call the executeQuery() method.

When you apply a RowMatch to a view object, the RowMatch expression can reference the view object's named bind variables using the same :VarName notation that you would use in a SQL statement. For example, if a view object had a named bind variable named StatusCode, you could apply a RowMatch to it with an expression like:

Status = :StatusCode or :StatusCode = '%'

Example 27-12 shows the interesting lines of a TestClientRowMatch class that illustrate the RowMatch in action. The CustomerList view object used in the example has a transient Boolean attribute named Selected. The code performs the following basic steps:

  1. Queries the full customer list, producing the output:

    --- Initial database results ---
    Neena Kochhar [null]
    Lex De Haan [null]
    Nancy Greenberg [null]
    :
    
  2. Marks odd-numbered rows selected by setting the Selected attribute of odd rows to Boolean.TRUE, producing the output:

    --- After marking odd rows selected ---
    Neena Kochhar [null]
    Lex De Haan [true]
    Nancy Greenberg [null]
    Daniel Faviet [true]
    John Chen [null]
    Ismael Sciarra [true]
    :
    
  3. Uses a RowMatch to subset the row set to contain only the select rows, that is, those with Selected = true. This produces the output:

    --- After in-memory filtering on only selected rows ---
    Lex De Haan [true]
    Daniel Faviet [true]
    Ismael Sciarra [true]
    Luis Popp [true]
    :
    
  4. Further subsets the row set using a more complicated RowMatch expression, producing the output:

    --- After in-memory filtering with more complex expression ---
    Lex De Haan [true]
    Luis Popp [true]
    

Example 27-12 Performing In-Memory Filtering with RowMatch

// In TestClientRowMatch.java
// 1. Query the full customer list
ViewObject vo = am.findViewObject("CustomerList");
vo.executeQuery();
showRows(vo,"Initial database results");
// 2. Mark odd-numbered rows selected by setting Selected = Boolean.TRUE
markOddRowsAsSelected(vo);
showRows(vo,"After marking odd rows selected");
// 3. Use a RowMatch to subset row set to only those with Selected = true
RowMatch rm = new RowMatch("Selected = true");
vo.setRowMatch(rm);
vo.setQueryMode(ViewObject.QUERY_MODE_SCAN_VIEW_ROWS);
vo.executeQuery();
showRows(vo, "After in-memory filtering on only selected rows");
// 4. Further subset rowset using more complicated RowMatch expression
rm = new RowMatch("LastName = 'Popp' "+
                  "or (FirstName like 'L%' and LastName like 'D%')");
vo.setRowMatch(rm);
vo.executeQuery();
showRows(vo,"After in-memory filtering with more complex expression");
// 5. Remove RowMatch, set query mode back to database, requery to see full list
vo.setRowMatch(null);
vo.setQueryMode(ViewObject.QUERY_MODE_SCAN_DATABASE_TABLES);
vo.executeQuery();
showRows(vo,"After re-querying to see a full list again");

27.5.4.2 Using RowMatch to Test an Individual Row

In addition to using a RowMatch to filter a row set, you can also use its rowQualifies() method to test whether any individual row matches the criteria it encapsulates. For example:

RowMatch rowMatch = new RowMatch("CountryId = 'US'");
if (rowMatch.rowQualifies(row)) {
  System.out.println("Customer is from the United States ");
}

27.5.4.3 How a RowMatch Affects Rows Fetched from the Database

Once you apply a RowMatch, if the view object's query mode is set to retrieve rows from the database, when you call executeQuery() the RowMatch is applied to rows as they are fetched. If a fetched row does not qualify, it is not added to the rowset.

Unlike a SQL WHERE clause, a RowMatch can evaluate expressions involving transient view object attributes and not-yet-posted attribute values. This can be useful to filter queried rows based on RowMatch expressions involving transient view row attributes whose values are calculated in Java. This interesting aspect should be used with care, however, if your application needs to process a large rowset. Oracle recommends using database-level filtering to retrieve the smallest-possible rowset first, and then using RowMatch as appropriate to subset that list in memory.

27.6 Using View Objects to Work with Multiple Row Types

In Section 26.6, "Using Inheritance in Your Business Domain Layer" you saw how to create an inheritance hierarchy of User, Technician, and Manager entity objects. Sometimes you will create a view object to work with entity rows of a single type like Technician, which perhaps includes Technician-specific attributes. At other times you may want to query and update rows for users, technicians, and managers in the same row set, working with attributes that they all share in common.

Note:

To experiment with the example described in this section, use the same InheritanceAndPolymorphicQueries project in the AdvancedEntityExamples workspace used in Section 26.6, "Using Inheritance in Your Business Domain Layer".

27.6.1 What is a Polymorphic Entity Usage?

A polymorphic entity usage is one that references a base entity object in an inheritance hierarchy and is configured to handle subtypes of that entity as well. Figure 27-4 shows the results of using a view object with a polymorphic entity usage. The entity-based UserList view object has the User entity object as its primary entity usage. The view object partitions each row retrieved from the database into an entity row part of the appropriate entity object subtype of User. It creates the appropriate entity row subtype based on consulting the value of the discriminator attribute. For example, if the UserList query retrieves one row for user ngreenbe, one row for manager sking, and one row for technician ahunold, the underlying entity row parts would be as shown in the figure.

Figure 27-4 View Object with a Polymorphic Entity Usage Handles Entity Subtypes

Image shows flow of entity subtypes

27.6.2 How To Create a View Object with a Polymorphic Entity Usage

To create a view object with a polymorphic entity usage, follow these steps:

  1. Identify the entity object that represents the base type in the entity inheritance hierarchy you want to work with.

  2. Create an entity-based view object with that base entity as its entity usage.

    Note:

    When an entity-based view object references an entity object with a discriminator attribute, then JDeveloper enforces that the discriminator attribute is included in the query as well (in addition to the primary key attribute).
  3. On the Entity Objects panel of the Create View Object wizard, select the entity usage in the Selected list and click Subtypes...

  • In the Subtypes dialog that appears, shuttle the desired entity subtypes you want to allow from the Available to the Selected list, and click OK

Then click OK to create the view object.

27.6.3 What Happens When You Create a View Object with a Polymorphic Entity Usage

When you create an entity-based view object with a polymorphic entity usage, JDeveloper adds information about the allowed entity subtypes to the view object's XML component definition. For example, when creating the UserList view object above, the names of the allowed subtype entity objects are recorded in an AttrArray tag like this:

<ViewObject Name="UserList" ... >
   <EntityUsage Name="TheUser"
                Entity="devguide.advanced.inheritance.entities.User" >
   </EntityUsage>
   <AttrArray Name="EntityImports">
      <Item Value="devguide.advanced.inheritance.entities.Manager" />
      <Item Value="devguide.advanced.inheritance.entities.Technician" />
   </AttrArray>
   <!-- etc. -->
</ViewObject>

27.6.4 What You May Need to Know

{para}?>

27.6.4.1 Your Query Must Limit Rows to Expected Entity Subtypes

If your view object expects to work with only a subset of the available entity subtypes in a hierarchy, you need to include an appropriate WHERE clause that limits the query to only return rows whose discriminator column matches the expected entity types.

27.6.4.2 Exposing Selected Entity Methods in View Rows Using Delegation

By design, clients do not work directly with entity objects. Instead, they work indirectly with entity objects through the view rows of an appropriate view object that presents a relevant set of information related to the task as hand. Just as a view object can expose a particular set of the underlying attributes of one or more entity objects related to the task at hand, it can also expose a selected set of methods from those entities. You accomplish this by enabling a custom view row Java class and writing a method in the view row class that:

  • Accesses the appropriate underlying entity row using the generated entity accessor in the view row, and

  • Invokes a method on it

For example, assume that the User entity object contains a performUserFeature() method in its UserImpl class. To expose this method to clients on the UserList view row, you can enable a custom view row Java class and write the method shown in Example 27-13. JDeveloper generates an entity accessor method in the view row class for each participating entity usage based on the entity usage alias name. Since the alias for the User entity in the UserList view object is "TheUser", it generates a getTheUser() method to return the entity row part related to that entity usage.

Example 27-13 Exposing Selected Entity Object Methods on View Rows Through Delegation

// In UserListRowImpl.java
public void performUserFeature() {
  getTheUser().performUserFeature();
}

The code in the view row's performUserFeature() method uses this getTheUser() method to access the underlying UserImpl entity row class and then invokes its performUserFeature() method. This style of coding is known as delegation, where a view row method delegates the implementation of one of its methods to a corresponding method on an underlying entity object. When delegation is used in a view row with a polymorphic entity usage, the delegated method call is handled by appropriate underlying entity row subtype. This means that if the UserImpl, ManagerImpl, and TechnicianImpl classes implement the performUserFeature() method in a different way, the appropriate implementation is used depending on the entity subtype for the current row.

After exposing this method on the client row interface, client programs can use the custom row interface to invoke custom business functionality on a particular view row. Example 27-14 shows the interesting lines of code from a TestEntityPolymorphism class. It iterates over all the rows in the UserList view object instance, casts each one to the custom UserListRow interface, and invokes the performUserFeature() method.

Example 27-14 Invoking a View Row Method That Delegates to an Entity Object

UserList userlist = (UserList)am.findViewObject("UserList");
userlist.executeQuery();
while (userlist.hasNext()) {
  UserListRow user = (UserListRow)userlist.next();
  System.out.print(user.getEmail()+"->");
  user.performUserFeature();
}

Running the client code in Example 27-14 produces the following output:

austin->## performUserFeature as Technician
hbaer->## performUserFeature as User
:
sking->## performUserFeature as Manager
:

Rows related to User entities display a message confirming that the performUserFeature() method in the UserImpl class was used. Rows related to Technician and Manager entities display a different message, highlighting the different implementations that the respective TechnicianImpl and ManagerImpl classes have for the inherited performUserFeature() method.

27.6.4.3 Creating New Rows With the Desired Entity Subtype

In a view object with a polymorphic entity usage, when you create a new view row it contains a new entity row part whose type matches the base entity usage. To create a new view row with one of the entity subtypes instead, use the createAndInitRow() method. Example 27-15 shows two custom methods in the UserList view object's Java class that use createAndInitRow() to allow a client to create new rows having entity rows either of Manager or Technician subtypes. To use the createAndInitRow(), as shown in the example, create an instance of the NameValuePairs object and set it to have an appropriate value for the discriminator attribute. Then, pass that NameValuePairs to the createAndInitRow() method to create a new view row with the appropriate entity row subtype, based on the value of the discriminator attribute you passed in.

Example 27-15 Exposing Custom Methods to Create New Rows with Entity Subtypes

// In UserListImpl.java
public UserListRow createManagerRow() {
  NameValuePairs nvp = new NameValuePairs();
  nvp.setAttribute("UserRole","manager");
  return (UserListRow)createAndInitRow(nvp);
}
public UserListRow createTechnicianRow() {
  NameValuePairs nvp = new NameValuePairs();
  nvp.setAttribute("UserRole","technician");
  return (UserListRow)createAndInitRow(nvp);
} 

If you expose methods like this on the view object's custom interface, then at runtime, a client can call them to create new view rows with appropriate entity subtypes. Example 27-16 shows the interesting lines relevant to this functionality from a TestEntityPolymorphism class. First, it uses the createRow(), createManagerRow(), and createTechnicianRow() methods to create three new view rows. Then, it invokes the performUserFeature() method from the UserListRow custom interface on each of the new rows.

As expected, each row handles the method in a way that is specific to the subtype of entity row related to it, producing the results:

## performUserFeature as User
## performUserFeature as Manager
## performUserFeature as Technician

Example 27-16 Creating New View Rows with Different Entity Subtypes

// In TestEntityPolymorphism.java
UserListRow newUser = (UserListRow)userlist.createRow();
UserListRow newMgr  = userlist.createManagerRow();
UserListRow newTech = userlist.createTechnicianRow();
newUser.performUserFeature();
newMgr.performUserFeature();
newTech.performUserFeature();

27.6.5 What are Polymorphic View Rows?

In the example shown in Section 27.6, "Using View Objects to Work with Multiple Row Types", the polymorphism occurs "behind the scenes" at the entity object level. Since the client code works with all view rows using the same UserListRow interface, it cannot distinguish between rows based on a Manager entity object from those based on a User entity object. The code works with all view rows using the same set of view row attributes and methods common to all types of underlying entity subtypes.

If you configure a view object to support polymorphic view rows, then the client can work with different types of view rows using a view row interface specific to the type of row it is. By doing this, the client can access view attributes or invoke view row methods that are specific to a given subtype as needed. Figure 27-5 illustrates the hierarchy of view objects that enables this feature for the UserList example considered above. TechnicianList and ManagerList are view objects that extend the base UserList view object. Notice that each one includes an additional attribute specific to the subtype of User they have as their entity usage. TechnicianList includes an additional Certified attribute, while ManagerList includes the additional NextReview attribute. When configured for view row polymorphism as described in the next section, a client can work with the results of the UserList view object using:

  • UserListRow interface for view rows related to

  • TechnicianListRow interface for view rows related to technicians

  • ManagerListRow interface for view rows related to managers

As you'll see, this allows the client to access the additional attributes and view row methods that are specific to a given subtype of view row.

Figure 27-5 Hierarchy of View Object Subtypes Enables View Row Polymorphism

Image shows subtype hierarchy enabling row polymorphism

27.6.6 How to Create a View Object with Polymorphic View Rows

To create a view object with polymorphic view rows, follow these steps:

  1. Identify an existing view object to be the base

    In the example above, the UserList view object is the base.

  2. Identify a discriminator attribute for the view row, and give it a default value.

    Check the Discriminator checkbox on the attribute panel to mark the attribute as the one that distinguishes which view row interface to use. You must supply a value for the Default field that matches the attribute value for which you expect the base view object's view row interface to be used. For example, in the UserList view object, you would mark the UserRole attribute as the discriminator attribute and supply a default value of "user".

  3. Enable a custom view row class for the base view object, and expose at least one method on the client row interface. This can be one or all of the view row attribute accessor methods, as well as any custom view row methods.

  4. Create a new view object that extends the base view object

    In the example above, TechnicianList extends the base UserList view object.

  5. Enable a custom view row class for the extended view object.

    If appropriate, add additional custom view row methods or override custom view row methods inherited from the parent view object's row class.

  6. Supply a distinct value for the discriminator attribute in the extended view object.

    The TechnicianList view object provides the value of "technician" for the UserRole discriminator attribute.

  7. Repeat steps 4-6 to add additional extended view objects as needed.

    For example, the ManagerList view object is a second one that extends UserList. It supplies the value "manager" for the UserRole discriminator attribute.

After setting up the view object hierarchy, you need to define the list of view object subtypes that participate in the view row polymorphism. To accomplish this, do the following:

  1. Add an instance of each type of view object in the hierarchy to the data model of an application module.

    For example, the UserModule application module in the example has instances of UserList, TechnicianList, and ManagerList view objects.

  2. In the Data Model panel of the Application Module Editor, click Subtypes...

  3. In the Subtypes dialog that appears, shuttle the desired view object subtypes that you want to participate in view row polymorphism from the Available to the Selected list, and click OK

27.6.7 What You May Need to Know

{para}?>

27.6.7.1 Selecting Subtype-Specific Attributes in Extended View Objects

When you create an extended view object, it inherits the entity usage of its parent. If the parent view object's entity usage is based on an entity object with subtypes in your domain layer, you may want your extended view object to work with one of these subtypes instead of the inherited parent entity usage type. Two reasons you might want to do this are:

  1. To select attributes that are specific to the entity subtype

  2. To be able to write view row methods that delegate to methods specific to the entity subtype

In order to do this, you need to override the inherited entity usage to refer to the desired entity subtype. To do this, perform these steps in the View Object Editor for your extended view object:

  1. On the Entity Objects panel, verify that you are working with an extended entity usage.

    For example, when creating the TechnicianList view object that extends the UserList view object, the entity usage with the alias TheUser will initially display in the Selected list as: TheUser (User): extended. The type of the entity usage is in parenthesis, and the "extended" label confirms that the entity usage is currently inherited from its parent.

  2. Select the desired entity subtype in the Available list that you want to override the inherited one. It must be a subtype entity of the existing entity usage's type.

    For example, you would select the Technician entity object in the Available list to overridden the inherited entity usage based on the User entity type.

  3. Click > to shuttle it to the Selected list

  4. Acknowledge the alert that appears, confirming that you want to override the existing, inherited entity usage.

When you have performed these steps, the Selected list updates to reflect the overridden entity usage. For example, for the TechnicianList view object, after overriding the User-based entity usage with the Technician entity subtype, it updates to show: TheUser (Technician): overridden.

After overriding the entity usage to be related to an entity subtype, you can then use the Attributes tab of the editor to select additional attributes that are specific to the subtype. For example, the TechnicianList view object includes the additional attribute named Certified that is specific to the Technician entity object.

27.6.7.2 Delegating to Subtype-Specific Methods After Overriding the Entity Usage

After overriding the entity usage in an extended view object to reference a subtype entity, you can write view row methods that delegate to methods specific to the subtype entity class. Example 27-17 shows the code for a performTechnicianFeature() method in the custom view row class for the TechnicianList view object. It casts the return value from the getTheUser() entity row accessor to the subtype TechnicianImpl, and then invokes the performTechnicianFeature() method that is specific to Technician entity objects.

Example 27-17 View Row Method Delegating to Method in Subtype Entity

// In TechnicianListRowImpl.java
public void performTechnicianFeature() {
   TechnicianImpl tech = (TechnicianImpl)getTheUser();
   tech.performTechnicianFeature();
}

Note:

You need to perform the explicit cast to the entity subtype here because JDeveloper does not yet take advantage of the new JDK 5.0 feature called covariant return types that would allow a subclass like TechnicianListRowImpl to override a method like getTheUser() and change its return type.

27.6.7.3 Working with Different View Row Interface Types in Client Code

Example 27-18 shows the interesting lines of code from a TestViewRowPolymorphism class that performs the following steps:

  1. Iterates over the rows in the UserList view object.

    For each row in the loop, it uses Java's instanceof operator to test whether the current row is an instance of the ManagerListRow or the TechnicianListRow.

  2. If the row is a ManagerListRow, then cast it to this more specific type and:

    • Call the performManagerFeature() method specific to the ManagerListRow interface, and

    • Access the value of the NextReview attribute that is specific to the ManagerList view object.

  3. If the row is a TechnicianListRow, then cast it to this more specific type and:

    • Call the performTechnicianFeature() method specific to the TechnicianListRow interface, and

    • Access the value of the Certified attribute that is specific to the TechnicianList view object.

  4. Otherwise, just call a method on the UserListRow

Example 27-18 Using View Row Polymorphism in Client Code

// In TestViewRowPolymorphism.java
ViewObject vo = am.findViewObject("UserList");
vo.executeQuery();
// 1. Iterate over the rows in the UserList view object
while (vo.hasNext()) {
  UserListRow user = (UserListRow)vo.next();
  System.out.print(user.getEmail()+"->");
  if (user instanceof ManagerListRow) {
    // 2. If the row is a ManagerListRow, cast it
    ManagerListRow mgr = (ManagerListRow)user;
    mgr.performManagerFeature();       
    System.out.println("Next Review:"+mgr.getNextReview());
  }
  else if (user instanceof TechnicianListRow) {
    // 3. If the row is a ManagerListRow, cast it
    TechnicianListRow tech = (TechnicianListRow)user;
    tech.performTechnicianFeature();       
    System.out.println("Certified:"+tech.getCertified());        
  }
  else {
    // 4. Otherwise, just call a method on the UserListRow 
    user.performUserFeature();
  }
}

Running the code in Example 27-18 produces the following output:

daustin->## performTechnicianFeature called
Certified:Y
hbaer->## performUserFeature as User
:
sking->## performManagerFeature called
Next Review:2006-05-09
:

This illustrates that by using the view row polymorphism feature the client was able to distinguish between view rows of different types and access methods and attributes specific to each subtype of view row.

27.6.7.4 View Row Polymorphism and Polymorphic Entity Usage are Orthogonal

While often even more useful when used together, the view row polymorphism and the polymorphic entity usage features are distinct and can be used separately. In particular, the view row polymorphism feature can be used for read-only view objects, as well as for entity-based view objects. When you combine both mechanisms, you can have both the entity row part being polymorphic, as well as the view row type.

Note to use view row polymorphism with either view objects or entity objects, you must configure the discriminator attribute property separately for each. This is necessary because read-only view objects contain no related entity usages from which to infer the discriminator information.

In summary, to use view row polymorphism:

  1. Configure an attribute to be the discriminator at the view object level in the root view object in an inheritance hierarchy.

  2. Have a hierarchy of inherited view objects each of which provides a distinct value for the "Default Value" property of that view object level discriminator attribute.

  3. List the subclassed view objects in this hierarchy in the application module's list of Subtypes.

Whereas, to create a view object with a polymorphic entity usage:

  1. Configure an attribute to be the discriminator at the entity object level in the root entity object in an inheritance hierarchy.

  2. Have a hierarchy of inherited entity objects, each of which overrides and provides a distinct value for the "Default Value" property of that entity object level discriminator attribute.

  3. List the subclassed entity objects in a view object's list of Subtypes.

27.7 Reading and Writing XML

The Extensible Markup Language (XML) standard from the Worldwide Web Consortium (W3C) defines a language-neutral approach for electronic data exchange. Its rigorous set of rules enables the structure inherent in data to be easily encoded and unambiguously interpreted using human-readable text documents.

View objects support the ability to write these XML documents based on their queried data. View objects also support the ability to read XML documents in order to apply changes to data including inserts, updates, and deletes. When you've introduced view links, this XML capability supports reading and writing multi-level nested information for master/detail hierarchies of any complexity. While the XML produced and consumed by view objects follows a canonical format, you can combine the view object's XML features with XML Stylesheet Language Transformations (XSLT) to easily convert between this canonical XML format and any format you need to work with.

Note:

The examples in this section refer to the ReadingAndWritingXML project in the AdvancedViewObjectExamples workspace. See the note at the beginning of this chapter for download instructions.

27.7.1 How to Produce XML for Queried Data

To produce XML from a view object, use the writeXML() method. If offers two ways to control the XML produced:

  1. For precise control over the XML produced, you can specify a view object attribute map indicating which attributes should appear, including which view link accessor attributes should be accessed for nested, detail information:

    Node writeXML(long options, HashMap voAttrMap)
    
  2. To producing XML that includes all attributes, you can simply specify a depth-level that indicates how many levels of view link accessor attributes should be traversed to produce the result:

    Node writeXML(int depthCount, long options)
    

The options parameter is a integer flag field that can be set to one of the following bit flags:

  • XMLInterface.XML_OPT_ALL_ROWS

    Includes all rows in the view object's row set in the XML.

  • XMLInterface.XML_OPT_LIMIT_RANGE

    Includes only the rows in the current range in the XML.

Using the logical OR operation, you can combine either of the above flags with the XMLInterface.XML_OPT_ASSOC_CONSISTENT flag when you want to include new, unposted rows in the current transaction in the XML output.

Both versions of the writeXML() method accept an optional third argument which is an XSLT stylesheet that, if supplied, is used to transform the XML output before returning it.

27.7.2 What Happens When You Produce XML

When you produce XML using writeXML(), the view object begins by creating a wrapping XML element whose default name matches the name of the view object definition. For example, for a Users view object in the devguide.advanced.xml.queries package, the XML produces will be wrapped in an outermost Users tag.

Then, it converts the attribute data for the appropriate rows into XML elements. By default, each row's data is wrapped in an row element whose name is the name of the view object with the Row suffix. For example, each row of data from a view object named Users is wrapped in an UsersRow element. The elements representing the attribute data for each row appear as nested children inside this row element.

If any of the attributes is a view link accessor attribute, and if the parameters passed to writeXML() enable it, the view object will include the data for the detail rowset returned by the view link accessor. This nested data is wrapped by an element whose name is determined by the name of the view link accessor attribute. The return value of the writeXML() method is an object that implements the standard W3C Node interface, representing the root element of the generated XML.

Note:

The writeXML() method uses view link accessor attributes to programmatically access detail collections. It does not require adding view link instances in the data model.

For example, to produce an XML element for all rows of a Users view object instance, and following view link accessors as many levels deep as exists, Example 27-19 shows the code required.

Example 27-19 Generating XML for All Rows of a View Object to All View Link Levels

ViewObject vo = am.findViewObject("Users");
printXML(vo.writeXML(-1,XMLInterface.XML_OPT_ALL_ROWS));

The Users view object is linked to a ServiceRequests view object showing the service requests created by that user. In turn, the ServiceRequests view object is linked to a ServiceHistories view object providing details on the notes entered for the service request by customers and technicians. Running the code in Example 27-19 produces the XML shown in Example 27-20, reflecting the nested structure defined by the view links.

Example 27-20 XML from a Users View Object with Two Levels of View Linked Details

<Users>
    :
   <User>
      <UserId>316</UserId>
      <UserRole>user</UserRole>
      <EmailAddress>sbaida</EmailAddress>
      <FirstName>Shelli</FirstName>
      <LastName>Baida</LastName>
      <StreetAddress>4715 Sprecher Rd</StreetAddress>
      <City>Madison</City>
      <StateProvince>Wisconsin</StateProvince>
      <PostalCode>53704</PostalCode>
      <CountryId>US</CountryId>
      <UserRequests>
         <ServiceRequestsRow>
            <SvrId>101</SvrId>
            <Status>Closed</Status>
            <RequestDate>2006-04-16 13:32:54.0</RequestDate>
            <ProblemDescription>Agitator does not work</ProblemDescription>
            <ProdId>101</ProdId>
            <CreatedBy>316</CreatedBy>
            <AssignedTo>304</AssignedTo>
            <AssignedDate>2006-04-23 13:32:54.0</AssignedDate>
            <ServiceHistories>
               <ServiceHistoriesRow>
                  <SvrId>101</SvrId>
                  <LineNo>1</LineNo>
                  <SvhDate>2006-04-23 13:32:54.0</SvhDate>
                  <Notes>Asked customer to ensure the lid was closed</Notes>
                  <SvhType>Technician</SvhType>
                  <CreatedBy>304</CreatedBy>
               </ServiceHistoriesRow>
               <ServiceHistoriesRow>
                  <SvrId>101</SvrId>
                  <LineNo>2</LineNo>
                  <SvhDate>2006-04-24 13:32:54.0</SvhDate>
                  <Notes>Problem is fixed</Notes>
                  <SvhType>Customer</SvhType>
                  <CreatedBy>316</CreatedBy>
               </ServiceHistoriesRow>
            </ServiceHistories>
         </ServiceRequestsRow>
      </UserRequests>
   </User>
   :
</Users>

27.7.3 What You May Need to Know

{para}?>

27.7.3.1 Controlling XML Element Names

You can change the default XML element names used in the view object's canonical XML format by setting custom properties:

  • Set the custom attribute-level property named XML_ELEMENT to a value SomeOtherName to change the XML element name used for that attribute to SomeOtherName

    For example, the Email attribute in the Users view object defines this property to change the XML element you see in Example 27-20 to be EmailAddress instead of Email.

  • Set the custom view object-level property named XML_ROW_ELEMENT to a value SomeOtherRowName to change the XML element name used for that attribute to SomeOtherRowName

    For example, the Users view object defines this property to change the XML element name for the rows you see in Example 27-20 to be User instead of UsersRow.

  • To change the name of the element names that wrapper nested row set data from view link attribute accessors, you need to use the View Link Properties panel of the View Link Editor to change the name of the view link accessor attribute.

27.7.3.2 Controlling Element Suppression for Null-Valued Attributes

By default, if a view row attribute is null, then its corresponding element is omitted from the generated XML. You can set the custom attribute-level property named XML_EXPLICIT_NULL to any value (e.g. "true" or "yes") to cause an element to be included for the attribute if its value is null. For example, if an attribute named AssignedDate has this property set, then a row containing a null assigned date will contain a corresponding AssignedDate null="true"/ element. If you want this behavior for all attributes of a view object, you can define the XML_EXPLICIT_NULL custom property at the view object level as a shortcut for defining it on each attribute.

27.7.3.3 Printing or Searching the Generated XML Using XPath

Two of the most common things you might want to do with the XML Node object returned from writeXML() are:

  1. Printing the node to its serialized text representation — to send across the network or save in a file, for example

  2. Searching the generated XML using W3C XPath expressions

Unfortunately, the standard W3C Document Object Model (DOM) API does not include methods for doing either of these useful operations. But there is hope. Since ADF Business Components uses the Oracle XML parser's implementation of the DOM, you can cast the Node return value from writeXML() to the Oracle specific classes XMLNode or XMLElement (in the oracle.xml.parser.v2 package) to access additional useful functionality like:

  • Printing the XML element to its serialized text format using the print() method

  • Searching the XML element in memory with XPath expressions using the selectNodes() method

  • Finding the value of an XPath expression related to the XML element using the valueOf() method.

Example 27-21 shows the printXML() method in the TestClientWriteXML. It casts the Node parameter to an XMLNode and calls the print() method to dump the XML to the console.

Example 27-21 Using the XMLNode's print() Method to Serialize XML

// In TestClientWriteXML.java
private static void printXML(Node n) throws IOException {
  ((XMLNode)n).print(System.out);
}

27.7.3.4 Using the Attribute Map For Fine Control Over Generated XML

When you need fine control over which attributes appear in the generated XML, use the version of the writeXML() method that accepts a HashMap. Example 27-22 shows the interesting lines from a TestClientWriteXML class that use this technique. After creating the HashMap, you put String[]-valued entries into it containing the names of the attributes you want to include in the XML, keyed by the fully-qualified name of the view definition those attributes belong to. The example includes the UserId, Email, StateProvince, and UserRequests attributes from the Users view object, and the SvrId, Status, AssignedDate, and ProblemDescription attributes from the ServiceRequests view object.

Note:

For upward compatibility reasons with earlier versions of ADF Business Components the HashMap expected by the writeXML() method is the one in the com.sun.java.util.collections package.

While processing the view rows for a given view object instance:

  • If an entry exists in the attribute map with a key matching the fully-qualified view definition name for that view object, then only the attributes named in the corresponding String array are included in the XML.

    Furthermore, if the string array includes the name of a view link accessor attribute, then the nested contents of its detail row set are included in the XML. If a view link accessor attribute name does not appear in the string array, then the contents of its detail row set are not included.

  • If no such entry exists in the map, then all attributes for that row are included in the XML.

Example 27-22 Using a View Definition Attribute Map for Fine Control Over Generated XML

HashMap viewDefMap = new HashMap();
viewDefMap.put("devguide.advanced.xml.queries.Users",
        new String[]{"UserId",
                     "Email",
                     "StateProvince",
                     "UserRequests" /* View link accessor attribute */
                     });
viewDefMap.put("devguide.advanced.xml.queries.ServiceRequests",
        new String[]{"SvrId","Status","AssignedDate","ProblemDescription"});  
printXML(vo.writeXML(XMLInterface.XML_OPT_ALL_ROWS,viewDefMap)); 

Running the example produces the XML shown in Example 27-23, including only the exact attributes and view link accessors indicated by the supplied attribute map.

Example 27-23 XML from a Users View Object Produced Using an Attribute Map

<Users>
   <User>
      <UserId>300</UserId>
      <EmailAddress>sking</EmailAddress>
      <StateProvince>Washington</StateProvince>
      <UserRequests>
         <ServiceRequestsRow>
            <SvrId>200</SvrId>
            <Status>Open</Status>
            <AssignedDate null="true"/>
            <ProblemDescription>x</ProblemDescription>
         </ServiceRequestsRow>
      </UserRequests>
   </User>
   <User>
      <UserId>301</UserId>
      <EmailAddress>nkochhar</EmailAddress>
      <StateProvince>Maryland</StateProvince>
   </User>
   :
</Users>

27.7.3.5 Use the Attribute Map Approach with Bi-Directional View Links

If your view objects are related through a view link that you have configured to be bi-directional, then you must use the writeXML() approach that uses the attribute map. If you were to use the writeXML() approach in the presence of bi-directional view links and were to supply a maximum depth of -1 to include all levels of view links that exist, the writeXML() method will go into an infinite loop as it follows the bi-directional view links back and forth, generating deeply nested XML containing duplicate data until it runs out of memory. Use writeXML() with an attribute map instead in this situation. Only by using this approach can you control which view link accessors are included in the XML and which are not to avoid infinite recursion while generating the XML.

27.7.3.6 Transforming Generated XML Using an XSLT Stylesheet

When the canonical XML format produced by writeXML() does not meet your needs, you can supply an XSLT stylesheet as an optional argument. It will produce the XML as it would normally, but then transform that result using the supplied stylesheet before returning the final XML to the caller.

Consider the XSLT stylesheet shown in Example 27-24. It is a simple transformation with a single template that matches the root element of the generated XML from Example 27-23 to create a new CustomerEmailAddresses element in the result. The template uses the xsl:for-each instruction to process all User element children of Users that contain more than one ServiceRequestsRow child element inside a nested UserRequests element. For each User element that qualifies, it creates a Customer element in the result whose Contact attribute is populated from the value of the EmailAddress child element of the User.

Example 27-24 XSLT Stylesheet to Transform Generated XML Into Another Format

<?xml version="1.0" encoding="windows-1252" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <CustomerEmailAddresses>
      <xsl:for-each
           select="/Users/User[count(UserRequests/ServiceRequestsRow) > 1]">
        <xsl:sort select="EmailAddress"/>
        <Customer Contact="{EmailAddress}"/>
      </xsl:for-each>
    </CustomerEmailAddresses>
  </xsl:template>
</xsl:stylesheet>

Example 27-25 shows the interesting lines from a TestClientWriteXML class that put this XSLT stylesheet into action when calling writeXML().

Example 27-25 Passing an XSLT Stylesheet to writeXML() to Transform the Resulting XML

// In TestClientWriteXML.java
XSLStylesheet xsl = getXSLStylesheet();
printXML(vo.writeXML(XMLInterface.XML_OPT_ALL_ROWS,viewDefMap,xsl));

Running the code in Example 27-25 produces the transformed XML shown here:

<CustomerEmailAddresses>
   <Customer Contact="dfaviet"/>
   <Customer Contact="jchen"/>
   <Customer Contact="ngreenbe"/>
</CustomerEmailAddresses>

The getXSLStylesheet() helper method shown in Example 27-26 is also interesting to study since it illustrates how to read a resource like an XSLT stylesheet from the classpath at runtime. The code expects the Example.xsl stylesheet to be in the same directory as the TestClientWriteXML class. By referencing the Class object for the TestClientWriteXML class using the .class operator, the code uses the getResource() method to get a URL to the resource. Then, it passes the URL to the newXSLStylesheet() method of the XSLProcessor class to create a new XSLStylesheet object to return. That object represents the compiled version of the XSLT stylesheet read in from the *.xslfile.

Example 27-26 Reading an XSLT Stylesheet as a Resource from the Classpath

private static XSLStylesheet getXSLStylesheet()
        throws XMLParseException, SAXException,IOException,XSLException {
  String xslurl = "Example.xsl";
  URL xslURL = TestClientWriteXML.class.getResource(xslurl);
  XSLProcessor xslProc = new XSLProcessor();
  return xslProc.newXSLStylesheet(xslURL);
}

Note:

When working with resources like XSLT stylesheets that you want to be included in the output directory along with your compiled Java classes and XML metadata, you can use the Compiler page of the Project Properties dialog to update the Copy File Types to Output Directory field to include .xsl in the semicolon-separated list.

27.7.3.7 Generating XML for a Single Row

In addition to calling writeXML() on a view object, you can call the same method with the same parameters and options on any Row as well. If the Row object on which you call writeXML() is a entity row, you can bitwise-OR the additional XMLInterface.XML_OPT_CHANGES_ONLY flag if you only want the changed entity attributes to appear in the XML.

27.7.4 How to Consume XML Documents to Apply Changes

To have a view object consume an XML document to process inserts, updates, and deletes, use the readXML() method:

void readXML(Element elem, int depthcount)

The canonical format expected by readXML() is the same as what would be produced by a call to the writeXML() method on the same view object. If the XML document to process does not correspond to this canonical format, you can supply an XSLT stylesheet as an optional third argument to readXML() to transform the incoming XML document into the canonical format before it is read for processing.

27.7.5 What Happens When You Consume XML Documents

When a view object consumes an XML document in canonical format, it processes the document to recognize row elements, their attribute element children, and any nested elements representing view link accessor attributes. It processes the document recursively to a maximum level indicated by the depthcount parameter. Passing -1 for the depthcount to request that it process all levels of the XML document.

27.7.5.1 How ViewObject.readXML() Processes an XML Document

For each row element it recognizes, the readXML() method does the following:

  • Identifies the related view object to process the row.

  • Reads the children attribute elements to get the values of the primary key attributes for the row.

  • Performs a findByKey() using the primary key attributes to detect whether the row already exists or not.

  • If the row exists:

    • If the row element contains the marker attribute bc4j-action="remove", then the existing row is deleted.

    • Otherwise, the row's attributes are updated using the values in any attribute element children of the current row element in the XML

  • If the row does not exist, then a new row is created, inserted into the view object's rowset. Its attributes are populated using the values in any attribute element children of the current row element in the XML.

27.7.5.2 Using readXML() to Processes XML for a Single Row

The same readXML() method is also supported on any Row object. The canonical XML format it expects is the same format produced by a call to writeXML() on the same row. You can invoke readXML() method on a row to:

  • Update its attribute values from XML

  • Remove the row, if the bc4j-action="remove" marker attribute is present on the corresponding row element.

  • Insert, update, or delete any nested rows via view link accessors

Consider the XML document shown in Example 27-27. It is in the canonical format expected by a single row in the Technicians view object. Nested inside the root TechniciansRow element, the City attribute represents the technician's city. The nested ExpertiseAreas element corresponds to the ExpertiseAreas view link accessor attribute and contains three ExpertiseAreasRow elements. Each of these includes ProdId elements representing part of the two-attribute primary key of a ExpertiseAreas row. The other primary key attribute, UserId, is inferred from the enclosing parent TechniciansRow element.

Example 27-27 XML Document in Canonical Format to Insert, Update, and Delete Rows

<TechniciansRow>
   <!-- This will update Techncian's City attribute -->
   <City>Padova</City>
   <ExpertiseAreas>
      <!-- This will be an update since it does exist -->
      <ExpertiseAreasRow>
         <ProdId>100</ProdId>
         <ExpertiseLevel>Expert</ExpertiseLevel>
      </ExpertiseAreasRow>
      <!-- This will be an insert since it doesn't exist -->
      <ExpertiseAreasRow>
         <ProdId>110</ProdId>
         <ExpertiseLevel>Expert</ExpertiseLevel>
      </ExpertiseAreasRow>
      <!-- This will be deleted -->
      <ExpertiseAreasRow bc4j-action="remove">
         <ProdId>112</ProdId>
      </ExpertiseAreasRow>        
   </ExpertiseAreas>
</TechniciansRow>

Example 27-28 shows the interesting lines of code from a TestClientReadXML class that applies this XML datagram to a particular row in the Technicians view object, and to the nested set of the technician's areas of expertise via the view link accessor attribute to the ExpertiseAreas view object. TestClientReadXML class performs the following basic steps:

  1. Finds a target row by key (e.g. for technician "ahunold").

  2. Shows the XML produced for the row before changes are applied.

  3. Obtains the parsed XML document with changes to apply using a helper method.

  4. Reads the XML document to apply changes to the row.

  5. Shows the XML with the pending changes applied.

    TestClientReadXML class is using the XMLInterface.XML_OPT_ASSOC_CONSISTENT flag described in Section 27.7.1, "How to Produce XML for Queried Data" to ensure that new, unposted rows are included in the XML.

Example 27-28 Applying Changes to and Existing Row with readXML()

ViewObject vo = am.findViewObject("Technicians");
Key k = new Key(new Object[] { 303 });
// 1. Find a target row by key (e.g. for technician "ahunold")
Row ahunold = vo.findByKey(k, 1)[0];
// 2. Show the XML produced for the row before changes are applied
printXML(ahunold.writeXML(-1, XMLInterface.XML_OPT_ALL_ROWS));
// 3. Obtain parsed XML document with changes to apply using helper method
Element xmlToRead = getInsertUpdateDeleteXMLGram();
printXML(xmlToRead);
// 4. Read the XML document to apply changes to the row
ahunold.readXML(getInsertUpdateDeleteXMLGram(), -1);
// 5. Show the XML with the pending changes applied
printXML(ahunold.writeXML(-1, XMLInterface.XML_OPT_ALL_ROWS | 
                              XMLInterface.XML_OPT_ASSOC_CONSISTENT));

Running the code in Example 27-28 initially displays the "before" version of Alexander Hunold's information. Notice that:

  • The City attribute has the value "Southlake"

  • The expertise area for product 100 has a level of "Qualified"

  • There is an expertise row for product 112, and

  • There is no expertise area row related to product 110.

<TechniciansRow>
   <UserId>303</UserId>
   <UserRole>technician</UserRole>
   <Email>ahunold</Email>
    :
   <City>Southlake</City>
    :
   <ExpertiseAreas>
      <ExpertiseAreasRow>
         <ProdId>100</ProdId>
         <UserId>303</UserId>
         <ExpertiseLevel>Qualified</ExpertiseLevel>
      </ExpertiseAreasRow>
       :
      <ExpertiseAreasRow>
         <ProdId>112</ProdId>
         <UserId>303</UserId>
         <ExpertiseLevel>Expert</ExpertiseLevel>
      </ExpertiseAreasRow>
       :
   </ExpertiseAreas>
</TechniciansRow>

After applying the changes from the XML document using readXML() to the row and printing its XML again using writeXML() you see that:

  • The City is now "Padova"

  • The expertise area for product 100 has a level of "Expert"

  • The expertise row for product 112 is removed, and

  • A new expertise area row for product 110 got created.

<TechniciansRow>
   <UserId>303</UserId>
   <UserRole>technician</UserRole>
   <Email>ahunold</Email>
    :
   <City>Padova</City>
    :
   <ExpertiseAreas>
      <ExpertiseAreasRow>
         <ProdId>110</ProdId>
         <UserId>303</UserId>
         <ExpertiseLevel>Expert</ExpertiseLevel>
      </ExpertiseAreasRow>
      <ExpertiseAreasRow>
         <ProdId>100</ProdId>
         <UserId>303</UserId>
         <ExpertiseLevel>Expert</ExpertiseLevel>
      </ExpertiseAreasRow>
</TechniciansRow>

Note:

The example illustrated using readXML() to apply changes to a single row. If the XML document contained a wrapping Technicians row, including the primary key attribute in each of its one or more nested TechniciansRow elements, then that document could be processed using the readXML() method on the Technicians view object for handling operations for multiple Technician rows.

27.8 Using Programmatic View Objects for Alternative Data Sources

By default view objects read their data from the database and automate the task of working with the Java Database Connectivity (JDBC) layer to process the database result sets. However, by overriding appropriate methods in its custom Java class, you can create a view object that programmatically retrieves data from alterative data sources like a REF CURSOR, an in-memory array, or a Java *.properties file, to name a few.

27.8.1 How to Create a Read-Only Programmatic View Object

To create a read-only programmatic view object, use the Create View Object wizard and follow these steps:

  1. In step 1 on the Name panel, provide a name and package for the view object. In the What kind of data do you need this view object to manage? radio group, select Rows Populated Programmatically, not Based on a Query

  2. In step 2 on the Attributes panel, click New one or more times to define the view object attributes your programmatic view object requires.

  3. In step 3 on the Attribute Settings panel, adjust any setting you may need to for the attributes you defined.

  4. In step 4 on the Java panel, enable a custom view object class to contain your code.

  5. Click Finish to create the view object.

In your view object's custom Java class, override the methods described in Section 27.8.3, "Key Framework Methods to Override for Programmatic View Objects" to implement your custom data retrieval strategy.

27.8.2 How to Create an Entity-Based Programmatic View Object

To create a entity-based view object with programmatic data retrieval, create the view object in the normal way, enable a custom Java class for it, and override the methods described in the next section to implement your custom data retrieval strategy.

27.8.3 Key Framework Methods to Override for Programmatic View Objects

A programmatic view object typically overrides all of the following methods of the base ViewObjectImpl class to implement its custom strategy for retrieving data:

  • create()

    This method is called when the view object instance is created and can be used to initialize any state required by the programmatic view object. At a minimum, this overridden method will contain the following lines to ensure the programmatic view object has no trace of a SQL query related to it:

    // Wipe out all traces of a query for this VO
    getViewDef().setQuery(null);
    getViewDef().setSelectClause(null);
    setQuery(null);
    
  • executeQueryForCollection()

    This method is called whenever the view object's query needs to be executed (or re-executed).

  • hasNextForCollection()

    This method is called to support the hasNext() method on the row set iterator for a row set created from this view object. Your implementation returns true if you have not yet exhausted the rows to retrieve from your programmatic data source.

  • createRowFromResultSet()

    This method is called to populate each row of "fetched" data. Your implementation will call createNewRowForCollection() to create a new blank row and then populateAttributeForRow() to populate each attribute of data for the row.

  • getQueryHitCount()

    This method is called to support the getEstimatedRowCount() method. Your implementation returns a count, or estimated count, of the number of rows that will be retrieved by the programmatic view object's query.

  • protected void releaseUserDataForCollection()

    Your code can store and retrieve a user data context object with each row set. This method is called to allow you to release any resources that may be associated with a row set that is being closed.

Since the view object component can be related to several active row sets at runtime, many of the above framework methods receive an Object parameter named qc in which the framework will pass the collection of rows in question that your code is supposed to be filling, as well as the array of bind variable values that might affect which rows get populated into the specific collection.

You can store a user-data object with each collection of rows so your custom datasource implementation can associate any needed datasource context information. The framework provides the setUserDataForCollection() and getUserDataForCollection() methods to get and set this per-collection context information. Each time one of the overridden framework methods is called, you can use the getUserDataForCollection() method to retrieve the correct ResultSet object associated with the collection of rows the framework wants you to populate.

The examples in the following sections each override these methods to implement different kinds of programmatic view objects.

27.8.4 How to Create a View Object on a REF CURSOR

Sometimes your application might need to work with the results of a query that is encapsulated within a stored procedure. PL/SQL allows you to open a cursor to iterate through the results of a query, and then return a reference to this cursor to the client. This so-called REF CURSOR is a handle with which the client can then iterate the results of the query. This is possible even though the client never actually issued the original SQL SELECT statement.

Note:

The examples in this section refer to the ViewObjectOnRefCursor project in the AdvancedViewObjectExamples workspace. See the note at the beginning of this chapter for download instructions. Run the CreateRefCursorPackage.sql script in the Resources folder against the SRDemo connection to set up the additional database objects required for the project.

Declaring a PL/SQL package with a function that returns a REF CURSOR is straightforward. For example, your package might look like this:

CREATE OR REPLACE PACKAGE RefCursorExample IS
  TYPE ref_cursor IS REF CURSOR;
  FUNCTION get_requests_for_tech(p_email VARCHAR2) RETURN ref_cursor;
  FUNCTION count_requests_for_tech(p_email VARCHAR2) RETURN NUMBER;
END RefCursorExample;

After defining an entity-based RequestForTech view object with an entity usage for a ServiceRequest entity object, go to its custom Java class RequestForTechImpl.java. At the top of the view object class, define some constant Strings to hold the anonymous blocks of PL/SQL that you'll execute using JDBC CallableStatement objects to invoke the stored functions:

/*
 * Execute this block to retrieve the REF CURSOR
 */
 private static final String SQL = 
           "begin ? := RefCursorSample.getEmployeesForDept(?);end;";
/*
 * Execute this block to retrieve the count of service requests that
 * would be returned if you executed the statement above.
 */
private static final String COUNTSQL =
           "begin ? := RefCursorSample.countEmployeesForDept(?);end;";

Then, override the methods of the view object as described in the following sections.

27.8.4.1 The Overridden create() Method

The create() method removes all traces of a SQL query for this view object.

protected void create() {
  getViewDef().setQuery(null);
  getViewDef().setSelectClause(null);
  setQuery(null); 
}

27.8.4.2 The Overridden executeQueryForCollection() Method

The executeQueryForCollection() method calls a helper method retrieveRefCursor() to execute the stored function and return the REF CURSOR return value, cast as a JDBC ResultSet. Then, it calls the helper method storeNewResultSet() that uses the setUserDataForCollection() method to store this ResultSet with the collection of rows for which the framework is asking to execute the query.

protected void executeQueryForCollection(Object qc,Object[] params,
                                         int numUserParams) { 
  storeNewResultSet(qc,retrieveRefCursor(qc,params));
  super.executeQueryForCollection(qc, params, numUserParams); 
}

The retrieveRefCursor() uses the helper method described in Section 25.5, "Invoking Stored Procedures and Functions" to invoke the stored function and return the REF CURSOR:

private ResultSet retrieveRefCursor(Object qc, Object[] params) {
  ResultSet rs = (ResultSet)callStoredFunction(OracleTypes.CURSOR,
                   "RefCursorExample.get_requests_for_tech(?)",
                   new Object[]{getNamedBindParamValue("Email",params)});
  return rs ;
}

27.8.4.3 The Overridden createRowFromResultSet() Method

For each row that the framework needs fetched from the datasource, it will invoke your overridden createRowFromResultSet() method. The implementation retrieves the collection-specific ResultSet object from the user-data context, uses the createNewRowForCollection() method to create a new blank row in the collection, and then use the populateAttributeForRow() method to populate the attribute values for each attribute defined at design time in the View Object Editor.

protected ViewRowImpl createRowFromResultSet(Object qc, ResultSet rs) { 
  /*
   * We ignore the JDBC ResultSet passed by the framework (null anyway) and
   * use the resultset that we've stored in the query-collection-private
   * user data storage
   */
  rs = getResultSet(qc);
  
  /*
   * Create a new row to populate
   */
  ViewRowImpl r = createNewRowForCollection(qc);
  try {
    /*
     * Populate new row by attribute slot number for current row in Result Set
     */
    populateAttributeForRow(r,0, rs.getLong(1));
    populateAttributeForRow(r,1, rs.getString(2));
    populateAttributeForRow(r,2, rs.getString(3));
  }
  catch (SQLException s) {
    throw new JboException(s);
  }
  return r;
}

27.8.4.4 The Overridden hasNextForCollectionMethod()

The overridden implementation of the framework method hasNextForCollection() has the responsibility to return true or false based on whether there are more rows to fetch. When you've hit the end, you call the setFetchCompleteForCollection() to tell view object that this collection is done being populated.

protected boolean hasNextForCollection(Object qc) {
  ResultSet rs = getResultSet(qc);
  boolean nextOne = false;
  try {
    nextOne = rs.next();
    /*
     * When were at the end of the result set, mark the query collection
     * as "FetchComplete".
     */
    if (!nextOne) {
      setFetchCompleteForCollection(qc, true); 
      /*
       * Close the result set, we're done with it
       */
      rs.close();
    }
  }
  catch (SQLException s) {
   throw new JboException(s);
  }
  return nextOne;
}

27.8.4.5 The Overridden releaseUserDataForCollection() Method

Once the collection is done with its fetch-processing, the overridden releaseUserDataForCollection() method gets invoked and closes the ResultSet cleanly so no database cursors are left open.

protected void releaseUserDataForCollection(Object qc, Object rs) {
     ResultSet userDataRS = getResultSet(qc);
     if (userDataRS != null) {
      try {
        userDataRS.close();
      } 
      catch (SQLException s) {
        /* Ignore */
      }   
    }
    super.releaseUserDataForCollection(qc, rs);
  }

27.8.4.6 The Overridden getQueryHitCount() Method

Lastly, in order to properly support the view object's getEstimatedRowCount() method, the overridden getQueryHitCount() method returns a count of the rows that would be retrieved if all rows were fetched from the row set. Here the code uses a CallableStatement to get the job done. Since the query is completely encapsulated behind the stored function API, the code also relies on the PL/SQL package to provide an implementation of the count logic as well to support this functionality.

public long getQueryHitCount(ViewRowSetImpl viewRowSet) {
  Object[] params = viewRowSet.getParameters(true);
  BigDecimal id = (BigDecimal)params[0];
  CallableStatement st = null;
  try {
    st = getDBTransaction().createCallableStatement(COUNTSQL,
                            DBTransaction.DEFAULT);
    /*
     * Register the first bind parameter as our return value of type CURSOR
     */
    st.registerOutParameter(1,Types.NUMERIC);
    /* 
     * Set the value of the 2nd bind variable to pass id as argument
     */ 
    if (id == null) st.setNull(2,Types.NUMERIC);
    else            st.setBigDecimal(2,id);
    st.execute();
    return st.getLong(1);
  }
  catch (SQLException s) {
    throw new JboException(s);
  }
  finally {try {st.close();} catch (SQLException s) {}}
}

27.8.5 Populating a View Object from Static Data

The SRDemo application's SRStaticDataViewObjectImpl class in the FrameworkExtensions project provides a programmatic view object implementation you can extend to populate code and description "lookup" data from static data in an in-memory array.

As shown in Example 27-29, it performs the following tasks in its overridden implementation of the key programmatic view object methods:

  • create()

    When the view object is created, the data is loaded from the in-memory array. It calls a helper method to set up the codesAndDescriptions array of codes and descriptions and wipes out all traces of a query for this view object.

  • executeQueryForCollection()

    Since the data is static, you don't really need to perform any query, but you still need to call the super to allow other framework setup for the row set to be done correctly. Since the code nulls out of traces of a query in the create() method, the view object won't actually perform any query during the call to super.

  • hasNextForCollection()

    The code returns true if the fetchPosition is still less than the number of rows in the in-memory array

  • createRowFromResultSet()

    Populates the "fetched" data for one row when the base view object implementation asks it to. It gets the data from the codesAndDescriptions array to populate the first and second attributes in the view object row (by zero-based index position).

  • getQueryHitCount()

    The code returns the number of "rows" in the codesAndDescriptions array that was previously stored in the rows member field.

In addition, the following other methods help get the data setup:

  • setFetchPos()

    Sets the current fetch position for the query collection. Since one view object can be used to create multiple row sets, you need to keep track of the current fetch position of each rowset in its "user data" context. It calls the setFetchCompleteForCollection() to signal to the view object that it's done fetching rows.

  • getFetchPos()

    Get the current fetch position for the query collection. This returns the fetch position for a given row set that was stored in its user data context.

  • initializeStaticData()

    Subclasses override this method to initialize the static data for display.

  • setCodesAndDescriptions()

    Sets the static code and description data for this view object.

Example 27-29 Custom View Object Class to Populate Data from a Static Array

package oracle.srdemo.model.frameworkExt;
// Imports omitted
public class SRStaticDataViewObjectImpl extends SRViewObjectImpl {
  private static final int CODE = 0;
  private static final int DESCRIPTION = 1;
  int rows = -1;
  private String[][] codesAndDescriptions = null;

  protected void executeQueryForCollection(Object rowset, Object[] params, 
                                           int noUserParams) {
    // Initialize our fetch position for the query collection
    setFetchPos(rowset, 0);
    super.executeQueryForCollection(rowset, params, noUserParams);
  }
  // Help the hasNext() method know if there are more rows to fetch or not
  protected boolean hasNextForCollection(Object rowset) {
    return getFetchPos(rowset) < rows;
  }
  // Create and populate the "next" row in the rowset when needed
  protected ViewRowImpl createRowFromResultSet(Object rowset,ResultSet rs) {
    ViewRowImpl r = createNewRowForCollection(rowset);
    int pos = getFetchPos(rowset);
    populateAttributeForRow(r, 0, codesAndDescriptions[pos][CODE]);
    populateAttributeForRow(r, 1, codesAndDescriptions[pos][DESCRIPTION]);
    setFetchPos(rowset, pos + 1);
    return r;
  }
  // When created, initialize static data and remove trace of any SQL query
  protected void create() {
    super.create();
    // Setup string arrays of codes and values from VO custom properties
    initializeStaticData();
    rows = (codesAndDescriptions != null) ? codesAndDescriptions.length : 0;
    // Wipe out all traces of a query for this VO
    getViewDef().setQuery(null);
    getViewDef().setSelectClause(null);
    setQuery(null);
  }
  // Return the estimatedRowCount of the collection
  public long getQueryHitCount(ViewRowSetImpl viewRowSet) {
    return rows;
  }
  // Subclasses override this to initialize their static data
  protected void initializeStaticData() {
    setCodesAndDescriptions(new String[][]{
      {"Code1","Description1"},
      {"Code2","Description2"}
    });
  }
  // Allow subclasses to initialize the codesAndDescriptions array
  protected void setCodesAndDescriptions(String[][] codesAndDescriptions) {
    this.codesAndDescriptions = codesAndDescriptions;
  }
  // Store the current fetch position in the user data context
  private void setFetchPos(Object rowset, int pos) {
    if (pos == rows) {
      setFetchCompleteForCollection(rowset, true);
    }
    setUserDataForCollection(rowset, new Integer(pos));
  }
  // Get the current fetch position from the user data context
  private int getFetchPos(Object rowset) {
    return ((Integer)getUserDataForCollection(rowset)).intValue();
  }
}

27.8.5.1 Basing Lookup View Object on SRStaticDataViewObjectImpl

The ServiceRequestStatusList view object in the SRDemo application defines two String attributes named Code and Description, and extends the SRStaticDataViewObjectImpl class. It overrides the initializeStaticData() method to supply the values of the legal service request status codes:

public class ServiceRequestStatusListImpl
       extends SRStaticDataViewObjectImpl {
  protected void initializeStaticData() {
    setCodesAndDescriptions(new String[][]{
        {"Open","Open"},
        {"Pending","Pending"},
        {"Closed","Closed"}
      });
  }
}

27.8.5.2 Creating a View Object Based on Static Data from a Properties File

Rather than compiling the static data for a view object into the Java class itself, it can be convenient to externalize it into a standard Java properties file with a Name=Value format like this:

#This is the property file format. Comments like this are ok
US=United States
IT=Italy

The SRPropertiesFileViewObjectImpl in the SRDemo application extends SRStaticDataViewObjectImpl to override the initializeStaticData() method and invoke the loadDataFromPropertiesFile() method shown in Example 27-30 to read the static data from a properties file. This method does the following basic steps:

  1. Derives the property file name based on the view definition name.

    For example, a CountryList view object in a x.y.z.queries package that extends this class would expect to read the properties file named ./x/y/z/queries/CountryList.properties file.

  2. Initializes a list to hold the name=value pairs.

  3. Opens an input stream to read the properties file from the class path.

  4. Loops over each line in the properties file.

  5. If line contains and equals sign and is not a comment line that begins with a hash, then add a string array of {code,description} to the list.

  6. Closes the line number reader and input stream.

  7. Returns the list contains as a two-dimensional String array.

Example 27-30 Reading Static Data for a View Object from a Properties File

// In SRPropertiesFileViewObjectImpl.java
private synchronized String[][] loadDataFromPropertiesFile() {
  // 1. Derive the property file name based on the view definition name
  String propertyFile = 
    getViewDef().getFullName().replace('.', '/') + ".properties";
  // 2. Initialize a list to hold the name=value pairs
  List codesAndDescriptionsList = new ArrayList(20);
  try {
    // 3. Open an input stream to read the properties file from the class path
    InputStream is = Thread.currentThread().getContextClassLoader()
                                           .getResourceAsStream(propertyFile);
    LineNumberReader lnr = new LineNumberReader(new InputStreamReader(is));
    String line = null;
    // 4. Loop over each line in the properties file
    while ((line = lnr.readLine()) != null) {
      line.trim();
      int eqPos = line.indexOf('=');
      if ((eqPos >= 1) && (line.charAt(0) != '#')) {
        // 5. If line contains "=" and isn't a comment, add String[]
        //    of {code,description} to the list
        codesAndDescriptionsList.add(new String[]{
                   line.substring(0, eqPos),
                   line.substring(eqPos + 1)});
      }
    }
    // 6. Close the line number reader and input stream
    lnr.close();
    is.close();
  } catch (IOException iox) {
    iox.printStackTrace();
    return new String[0][0];
  }
  // 7. Return the list contains as a two-dimensional String array
  return (String[][])codesAndDescriptionsList.toArray();
}

27.8.5.3 Creating Your Own View Object with Static Data

To create your own view object with static data that extends one of the example classes provided in the SRDemo application, define a new read-only programmatic view object with String attributes named Code and Description. On the Java panel of the View Object Editor, click Class Extends to specify the fully-qualified name of the SRStaticDataViewObjectImpl or SRPropertiesFileViewObjectImpl class as the custom class in the Object field. Then, enable a custom Java class for your view object and do the following:

If you extend SRStaticDataViewObjectImpl...

Then override the initializeStaticData() method and invoke the loadDataFromPropertiesFile() method shown

If you extend SRStaticDataViewObjectImpl ...

Then create the appropriate *.properties file in the same directory as the view object's XML component definition, with a name that matches the name of the view object (ViewObjectName.properties).

27.9 Creating a View Object with Multiple Updatable Entities

Note:

To experiment with the example described in this section, use the same ControllingPostingOrder project in the AdvancedEntityExamples workspace used in Section 26.7, "Controlling Entity Posting Order to Avoid Constraint Violations".

When you create a view object with multiple entity usages, you can enable a secondary entity usage to be updatable by selecting it in the Selected list of the Entity Objects panel of the View Object Editor and:

If you only plan to use the view object to update or delete existing data, then this is the only step required. The user can update attributes related to any of the non-reference, updatable entity usages and the view row will delegate the changes to the appropriate underlying entity rows.

However, if you need a view object with multiple updatable entities to support creating new rows, then you need to write a bit of code to enable that to work correctly. When you call createRow() on a view object with multiple update entities, it creates new entity row parts for each updatable entity usage. Since the multiple entities in this scenario are related by an association, there are three pieces of code you might need to implement to ensure the new, associated entity rows can be saved without errors:

  1. You may need to override the postChanges() method on entity objects involved to control the correct posting order.

  2. If the primary key of the associated entity is populated by a database sequence using DBSequence, and if the multiple entity objects are associated but not composed, then you need to override the postChanges() and refreshFKInNewContainees() method to handle cascading the refreshed primary key value to the associated rows that were referencing the temporary value.

  3. You need to override the create() method of the view object's custom view row class to modify the default row creation behavior to pass the context of the parent entity object to the newly-created child entity.

In Section 26.7, "Controlling Entity Posting Order to Avoid Constraint Violations", you've already seen the code required for 1 and 2 above in an example with associated Product and ServiceRequest entity objects. The only thing remaining is the overridden create() method on the view row. Consider a ServiceRequestAndProduct view object with a primary entity usage of ServiceRequest and secondary entity usages of Product and User. Assume the Product entity usage is marked as updatable and non-reference, while the User entity usage is a reference entity usage.

Example 27-31 shows the commented code required to correctly sequence the creation of the multiple, updatable entity row parts during a view row create operation.

Example 27-31 Overriding View Row create() Method for Multiple Updatable Entities

/**
   * By default, the framework will automatically create the new
   * underlying entity object instances that are related to this
   * view object row being created.
   *
   * We override this default view object row creation to explicitly
   * pre-populate the new (detail) ServiceRequestImpl instance using
   * the new (master) ProductImpl instance. Since all entity objects
   * implement the AttributeList interface, we can directly pass the
   * new ProductImpl instance to the ServiceRequestImpl create()
   * method that accepts an AttributeList.
   */
  protected void create(AttributeList attributeList) {
    // The view row will already have created "blank" entity instances
    ProductImpl newProduct = getProduct();
    ServiceRequestImpl newServiceRequest = getServiceRequest();
     try {
        // Let product "blank" entity instance to do programmatic defaulting
        newProduct.create(attributeList);
        // Let service request "blank" entity instance to do programmatic
        // defaulting passing in new ProductImpl instance so its attributes
        // are available to the EmployeeImpl's create method.
        newServiceRequest.create(newProduct);
     }
     catch (JboException ex) {
       newProduct.revert();
       newServiceRequest.revert();      
       throw ex;
     }
     catch (Exception otherEx) {
       newProduct.revert();
       newServiceRequest.revert();      
       throw new RowCreateException(true      /* EO Row? */,
                                    "Product" /* EO Name */,
                                    otherEx   /* Details */);
     }
  }

In order for this view row class to be able to invoke the protected create() method on the Product and ServiceRequest entity objects, they need to override the create() method. If the view object and entity objects are in the same package, the overridden create() method can have protected access. Otherwise, it requires public access.

/**
 * Overridding this method in this class allows friendly access
 * to the create() method by other classes in this same package, like the
 * ServiceRequestsAndProduct view object implementation class, whose overridden
 * create() method needs to call this.
 * @param nameValuePair
 */
  protected void create(AttributeList nameValuePair) {
    super.create(nameValuePair);
  }

27.10 Declaratively Preventing Insert, Update, and Delete

Some 4GL tools like Oracle Forms provide declarative properties that control whether a given data collection allows inserts, updates, or deletes. While the view object does not yet support this as a built-in feature in the current release, it's easy to add this facility using a framework extension class that exploits custom metadata properties as the developer-supplied flags to control insert, update, or delete on a view object.

Note:

The examples in this section refer to the DeclarativeBlockOperations project in the AdvancedViewObjectExamples workspace. See the note at the beginning of this chapter for download instructions.

To allow developers to have control over individual view object instances, you could adopt the convention of using application module custom properties by the same name as the view object instance. For example, if an application module has view object instances named ProductsInsertOnly, ProductsUpdateOnly, ProductsNoDelete, and Products, your generic code might look for application module custom properties by these same names. If the property value contains Insert, then insert is enabled for that view object instance. If the property contains Update, then update allowed. And, similarly, if the property value contains Delete, then delete is allowed. You could use helper methods like this to test for these application module properties and determine whether insert, update, and delete are allowed for a given view object:

private boolean isInsertAllowed() {
  return isStringInAppModulePropertyNamedAfterVOInstance("Insert");
}
private boolean isUpdateAllowed() {
  return isStringInAppModulePropertyNamedAfterVOInstance("Update");
}
private boolean isDeleteAllowed() {
  return isStringInAppModulePropertyNamedAfterVOInstance("Delete");
}
private boolean isStringInAppModulePropertyNamedAfterVOInstance(String s) {
  String voInstName = getViewObject().getName();
  String propVal = (String)getApplicationModule().getProperty(voInstName);
  return propVal != null ? propVal.indexOf(s) >= 0 : true;
}

Example 27-32 shows the other code required in a custom framework extension class for view rows to complete the implementation. It overrides the following methods:

Example 27-32 Preventing Insert, Update, or Delete Based on Custom Properties

public class CustomViewRowImpl extends ViewRowImpl {
  public boolean isAttributeUpdateable(int index) {
    if (hasEntities() && 
        ((isNewOrInitialized() && !isInsertAllowed()) ||
         (isModifiedOrUnmodified() && !isUpdateAllowed()))) {
      return false;
    }
    return super.isAttributeUpdateable(index);
  }
  protected void setAttributeInternal(int index, Object val) {
    if (hasEntities()) {
      if (isNewOrInitialized() && !isInsertAllowed())
        throw new JboException("No inserts allowed in this view");
      else if (isModifiedOrUnmodified() && !isUpdateAllowed())
        throw new JboException("No updates allowed in this view");
    }
    super.setAttributeInternal(index, val);
  }
  public void remove() {
    if (!hasEntities() || isDeleteAllowed() || isNewOrInitialized())
      super.remove();
    else
        throw new JboException("Delete not allowed in this view");
  }
  protected void create(AttributeList nvp) {
    if (isInsertAllowed()) {
      super.create(nvp);
    } else {
      throw new JboException("Insert not allowed in this view");
    }
  }
  // private helper methods omitted
}