| Oracle® Application Development Framework Developer's Guide For Forms/4GL Developers 10g (10.1.3.1.0) Part Number B25947-01 |
|
|
View PDF |
To create a test client program, create a new Java class using the Create Java Class wizard. This is available in the New Gallery under the General category. Enter a class name like TestClient, a package name like devguide.examples.client, and ensure the Extends field says java.lang.Object. In the Optional Attributes, deselect the Generate Default Constructor and select the Generate Main Method checkbox. Then click OK to create the TestClient.java file. The file opens in the source editor to show you the skeleton code:
Example 5-6 Skeleton Code for TestClient.java
package devguide.examples.client;
public class TestClient {
public static void main(String[] args) {
}
}
Place the cursor on a blank line inside the body of the main() method and use the bc4jclient code template to create the few lines of necessary code. To use this predefined code template, type the characters bc4jclient followed by a [Ctrl]+[Enter] to expands the code template so that the class now should look like this:
Example 5-7 Expanded Skeleton Code for TestClient.java
package devguide.examples.client;
import oracle.jbo.client.Configuration;
import oracle.jbo.*;
import oracle.jbo.domain.Number;
import oracle.jbo.domain.*;
public class TestClient {
public static void main(String[] args) {
String amDef = "test.TestModule";
String config = "TestModuleLocal";
ApplicationModule am =
Configuration.createRootApplicationModule(amDef,config);
ViewObject vo = am.findViewObject("TestView");
// Work with your appmodule and view object here
Configuration.releaseRootApplicationModule(am,true);
}
}
Adjust the values of the amDef andconfig variables to reflect the names of the application module definition and the configuration that you want to use, respectively. For the Example 5-7, you would change these two lines to read:
String amDef = "devguide.examples.UserService"; String config = "UserServiceLocal";
Finally, change view object instance name in the call to findViewObject() to be the one you want to work with. Specify the name exactly as it appears in the Data Model tree on the Data Model panel of the Application Module editor. Here, the view object instance is named UserList, so you need to change the line to:
ViewObject vo = am.findViewObject("UserList");
At this point, you have a working skeleton test client for the UserService application module whose source code looks like what you see in Example 5-8.
|
Note: Section 8.5, "Working Programmatically with an Application Module's Client Interface" expands this test client sample code to illustrate calling custom application module service methods, too. |
Example 5-8 Working Skeleton Code for an Application Module Test Client Program
package devguide.examples.client;
import oracle.jbo.client.Configuration;
import oracle.jbo.*;
import oracle.jbo.domain.Number;
import oracle.jbo.domain.*;
public class TestClient {
public static void main(String[] args) {
String amDef = "devguide.examples.UserService";
String config = "UserServiceLocal";
ApplicationModule am =
Configuration.createRootApplicationModule(amDef,config);
ViewObject vo = am.findViewObject("UserList");
// Work with your appmodule and view object here
Configuration.releaseRootApplicationModule(am,true);
}
}
To execute the view object's query, display the number of rows it will return, and loop over the result to fetch the data and print it out to the console, replace // Work with your appmodule and view object here , with the code in Example 5-9
Example 5-9 Looping Over a View Object and Printing the Results to the Console
System.out.println("Query will return "+
vo.getEstimatedRowCount()+" rows...");
vo.executeQuery();
while (vo.hasNext()) {
Row curUser = vo.next();
System.out.println(vo.getCurrentRowIndex()+". "+
curUser.getAttribute("UserId")+" "+
curUser.getAttribute("Email"));
}
The first line calls getEstimatedRowCount() to show how many rows the query will retrieve. The next line calls the executeQuery() method to execute the view object's query. This produces a row set of zero or more rows that you can loop over using a while statement that iterates until the view object's hasNext() method returns false. Inside the loop, the code puts the current Row in a variable named curUser, then invokes the getAttribute() method twice on that current Row object to get the value of the UserId and Email attributes to print them to the console.
When you run the TestClient class by choosing Run from the context menu of the source editor, you'll see the results of the test in the log window. Notice that the getCurrentRowIndex() used in Example 5-10 shows that the row index in a row set is a zero-based count of the rows:
Example 5-10 Log Output from Running a Test Client
Query will return 27 rows... 0. 303 ahunold 1. 315 akhoo : 25. 306 vpatabal 26. 326 wgietz
The call to createRootApplicationModule() on the Configuration object returns an instance of the UserService application module to work with. As you might have noticed in the debug diagnostic output, the ADF Business Components runtime classes load XML component definitions as necessary to instantiate the application module and the instance of the view object component that you've defined in its data model at design time. The findViewObject() method on the application module finds a view object instance by name from the application module's data model. After the loop described in Example 5-9, the call to releaseRootApplicationModule() on the Configuration object signals that you're done using the application module and allows the framework to clean up resources, like the database connection that was used by the application module.
The createRootApplicationModule() and releaseRootApplicationModule() methods are very useful for command-line access to application module components, however you won't typically ever need to write these two lines of code in the context of an ADF-based web or Swing application. The ADF Model data binding layer cooperates automatically with the ADF Business Components layer to acquire and release application module components for you in those scenarios.