Legal | Privacy
Creating Custom Business Methods Using ADF Business Components

Creating Custom Business Methods using ADF Business Components

This document describes how to add a custom method to a business model using Oracle ADF Business Components. The purpose of a custom method is to provide easy client application access to specific application data. This data is usually calculated or derived from multiple business objects. Custom methods provide an easy and encapsulated way of publishing this type of data.

Topics

This lesson will discuss the following:


1 hour

Move your mouse over this icon to show all screenshots. You can also move your mouse over each individual icon to see only the screenshot associated with it.


Overview

This tutorial steps you through creating a custom method in an ADF Business Components project. When a client application calls this method, it will pass a department number to the method and get the total salary for that department back from the method. The purpose of a custom method is to provide an easy way for client applications to get calculated or derived data from the ADF business model without having to instantiate a specific view object or multiple view objects.

Back to Topic List

You have been working on the data model for a Human Resource Information System. You have now discovered that a number of client applications need to determine the total salary for a given department. You could leave it to the client applications to calculate the total, but that would lead to redundant work and quite possibly introduce erroneous data.
You have decided to create a custom method in your application that accepts a Department Id as a parameter, calculates that total salary for that department, and returns the value to the calling client application. By building the code within the Business Model, your provide access to any client application that can use the Business Model. You therefore help ensure consistency and reduce redundant code.

Prerequisites

Back to Topic List

In order for this lesson to work successfully, you will need to have performed the following:

1. Install Oracle JDeveloper 10g.
2. Install the sample schema and create a connection to the HR schema to use in this lesson. See Installing the Sample Schemas and Establishing a Database Connection..

Step 1 - Getting Started - Create an ADF Business Component Project

Back to Topic List

In this step you will create a new application workspace and project to help organize your application. All of the work you do in JDeveloper is organized into projects within an application workspace. After you have created a workspace and project, you will create a default Business Components Model for your business.


Creating an Application Workspace and Project

Back to List

1.

Create a new Application workspace to hold the business model components. Select File | New, then select the Application workspace node.
Click OK.

This will open a dialog where you can define location, templates, and application package prefix

 

2.

Change the name of the application to HRApplication. Accept the other defaults for the application and click OK.

Move your mouse over this icon to see the image

 

3.

On completion you should have an application named HRApplication and two projects named Model and ViewController. You will only use the Model project for this exercise.

Move your mouse over this icon to see the image

 

 

Creating Default Business Components

Back to List

1.

It is generally easier and more effective to start with a set of default business components than to create each one individually. Whichever technique you use will result in a set of components that you can customize to fit your specific business needs.

To create a set of default business components, right-click the Model project node in the System Navigator and select New.

Move your mouse over this icon to see the image

 

2.

This opens the New Gallery wizard. From this gallery, select the Business Components category and Business Components from Tables and click OK. You can also double-click Business Components from Tables.

Move your mouse over this icon to see the image

 

3.

Selecting Business Components from Tables opens the Business Components wizard. The first step of this wizard is to establish a connection to your database. Select the connection name that connects to the HR schema. If the welcome page is displayed, click Next to go the step 1 of the wizard.

Move your mouse over this icon to see the image

 

Note: If you have not established this connection to the HR schema, please see the prerequisites section.

 

4.

In step 1 of the Business Component wizard, select the tables for which you want to create entity objects. Select the Departments, and Employees tables.

Move your mouse over this icon to see the image

 

5.

In step 2 of the wizard, select which view objects you want to create from the default entity objects just selected. Select all of the available entity objects and click the shuttle button.

Move your mouse over this icon to see the image

 

6.

In step 3 of the wizard, change the name of the Application Module to HRAppModule. If the Application Module checkbox is checked, the wizard will create a default application module for you.

If it is not checked, check it.

Move your mouse over this icon to see the image

 

7.

The last step of the wizard confirms what Business Component objects you are about to create. Click Finish to accept your choices.

Move your mouse over this icon to see the image

 

8.

Your project should now include an application module with entity and view objects.

Move your mouse over this icon to see the image

 

 

Testing Business Components

Back to List

1.

JDeveloper includes a built-in Business Components Browser that tests business components. The browser works without requiring you to build any client code.

To invoke the tester, right-click the Application Module you just created in the System Navigator and select Test from the context menu.

Move your mouse over this icon to see the image

 

2.

The first step of the Tester is to select a database connection to use for this test. Select the database connection you used to create the default objects and click Connect.

Move your mouse over this icon to see the image

 

3.

The Business Components Browser shows all of the View objects defined in your Application Module.

Move your mouse over this icon to see the image

 

4.

Double-click a view object instance to display a single-record data viewer.

Move your mouse over this icon to see the image

 

You can scroll through the rows in this View object using the controls on the top of the window. You can double-click other View objects to run multiple data viewers.

The Business Components Browser gives you a view of the application module that you have just created.

When you are finished testing your application, close the Business Components Browser.

 

 

Step 2- Adding a Custom Method to an Application Module

Back to Topic List

Before you add a custom method, you must decide where to put it. In general, it depends on how and from where, the method will be called. If the method will be called while a client is using a View Object, then the method could go in the View Object. However, if the method may be called outside the context of a specific View Object, the method should go in the Application Module. Putting the method in the Application Module makes it easier for client applications to access the method. They won't have to instantiate a View Object to access the method. In our example, we will create a method in the Application Module.

Adding the method stub
Getting the Department and Employees View Objects

Adding the method stub

In this step, you will add a simple method stub that you will complete in the later steps. Adding this stub takes you through steps you will repeat every time you add or modify code using JDeveloper's code editor.

Back to List

1.

In the System Navigator, click HRAppModule. This will show the contents of the Application Module in the Structure pane. Notice the two source files: HRAppModule.xml and HRAppModuleImpl.java

Move your mouse over this icon to see the image

 

2.

Double click the source file HRAppModuleImpl.java to open the code editor.

Move your mouse over this icon to see the image

 

3.

Now that we have a code editor open, we can start adding the custom code. The first step is to create the method signature.
Enter the following code after the end of the default constructor.

 public Number getDeptSalaryTotal(String dept_id)
           {
           
           return new Number(0);
           }

Since we will be using the Oracle implementation of Number, add an import statement to the top of the class as follows:

import oracle.jbo.domain.Number;

 

Move your mouse over this icon to see the image

 

4.

You should compile your class periodically to make sure that you have not introduced errors in your code that the compiler will catch. To compile the class, right-click anywhere in the code editor window and select Make from the context menu.

Move your mouse over this icon to see the image

 

 

5.

Check the message window for any compile errors. If there were no errors, the compilation was successful.

Move your mouse over this icon to see the image

 

Getting the Department and Employees View Objects

In this step, add code to your method that will:

  • Instantiate a Departments View Object
  • Set the where clause
  • Execute the query
  • Set the current row to the first row in the View Object
  • Get the related Employees View Object
  • Loop through the rows, summarizing the Salary attribute

Back to List

1.

Before you can use data from the Departments View Object, you must instantiate it. JDeveloper ADF provides methods that make this a simple task. To instantiate the view object call getDepartmentsView1() to your method.

 // get a department view object
 DepartmentsViewImpl dept = getDepartmentsView1();

Move your mouse over this icon to see the image

 

2.

Once you have access to the Departments View object, you can manipulate it by calling standard methods on the object. In our case, we need to modify the where clause to include the department ID. Add the following code to your method which:

  • Sets the where clause
  • Executes the query
  • Sets the current row to the first row of the rowset
     public HRAppModuleImpl()
     {
     }
     public Number getDeptSalaryTotal (String dept_id)
     {
      // get a department view object
      DepartmentsViewImpl dept = this.getDepartmentsView1();
      dept.setWhereClause("DEPARTMENT_ID = " + dept_id);
      dept.executeQuery();
      dept.first();
                 
      return new Number(0);
      }

Move your mouse over this icon to see the image

 

3.

Make sure to compile your code periodically to find any coding errors you may have introduced.

Next you will use another method that is part of ADF Business Components to get all of the Employees that are related to the Department you just retrieved. To get the Employee rows, add a call to the getEmployeesView3() method.

EmployeesViewImpl emps = getEmployeesView3(); 

Move your mouse over this icon to see the image

 

4.

The ADF Business Components framework automatically coordinates the master detail relationship between Departments and Employees. This means the Employee rowset you just created will contain only Employees that belong to the department you queried in the previous step. Now all you have to do is loop through the Employee rows and summarize the salary attribute.
Before you can loop through the Rows, you need to import oracle.jbo.Row. JDeveloper provides automatic advice on import statements. When you enter the following code, you will notice that JDeveloper underlines the Row object and prompts you to import oracle.jbo.Row. If you hit Alt + Enter, JDeveloper will add the import for you.
You need to create a temporary RowSetIterator. You will use this iterator to loop through the employee rows.When you are through with the iterator, make sure to close it.

Number salaryTotal = new Number(0);

RowSetIterator empsIter = emps.createRowSetIterator(null);
while(empsIter.hasNext())
{
Row empsRow = (Row)empsIter.next();
salaryTotal = salaryTotal.add((Number)(empsRow.getAttribute("Salary")));
}
empsIter.closeRowSetIterator();
return salaryTotal;

Move your mouse over this icon to see the image

 

 

5.

Your completed method should look like the following:

public Number getDeptSalaryTotal(String dept_id)
{
// get a department view object
DepartmentsViewImpl dept = getDepartmentsView1();
dept.setWhereClause("DEPARTMENT_ID = " + dept_id);
dept.executeQuery();
dept.first();

EmployeesViewImpl emps = getEmployeesView3();

Number salaryTotal = new Number(0);

RowSetIterator empsIter = emps.createRowSetIterator(null);
while(empsIter.hasNext())
{
Row empsRow = (Row)empsIter.next();
salaryTotal = salaryTotal.add((Number)(empsRow.getAttribute("Salary")));
}
empsIter.closeRowSetIterator();
return salaryTotal;
}

Move your mouse over this icon to see the image

 

Step 3- Creating a Test Client

Back to Topic List

So far, you have created and tested a couple of default business components. You have also added code to the Application Module that any client can call to easily get the total salary of a given department.
The next step in this scenario is to create a client application that uses this method and test it. There are several types of clients we could create; a JSP, a Java rich-client interface, a UIX client, or a Java command line client. For the purposes of this exercise, the easiest and quickest is the Java command line client. JDeveloper provides design-time tools that make this approach fairly easy. This will also help familiarize you with the JDeveloper IDE.


Create a Test Client Application

The first step in testing our custom method is to create a simple Java class.

Back to List

1.

In the System Navigator, right-click the Model project and select New from the context menu.

Move your mouse over this icon to see the image

2.

In the New Gallery, select Simple Files and Java Class. This will open the Create Java Class Wizard.

Move your mouse over this icon to see the image

3.

In the Java Class Wizard, rename the class to TestClient. Make sure to check the box that has the wizard create a Main method for you. Click OK to accept all the other default values.

Move your mouse over this icon to see the image

4.

JDeveloper has now created a simple Java class named TestClient and has opened it in the editor window. The next step will be to add the code to test your custom method.

Move your mouse over this icon to see the image

 

Adding Code to Instantiate the Application Module

Now that you have the shell of a simple Java program, you will add the code to instantiate the Application Module. This will give you access to the objects within that Application Module. Remember that in ADF Business Components, the application module provides and controls access to all of its objects. In our case those objects include the Departments and Employees view objects along with the application module itself.

Back to List

1.

First, let's create a couple of String variables to hold the name of the application module and the name of the configuration. The application module name is whatever you named it when you created the Business Components. It should be HRAppModule. The default configuration name is the application module name appended with Local (HRAppModuleLocal). You also need to include the package name as part of the application module. Add the following code the your TestClient. The code will go within the Main() method and replaces the default code "TestClient testClient = new TestClient();"

    String amDef = "model.HRAppModule";
String config = "HRAppModuleLocal";

Move your mouse over this icon to see the image

 

2.

Next we'll call a method the instantiate the application module. The method we call accepts two arguments: the application module name, including the package name, and the configuration name. For the next few steps, you will import the classes that are required by letting JDeveloper remind you which ones are needed and hitting Alt+Enter to include the import. Add the following code:

 ApplicationModule am = 
     Configuration.createRootApplicationModule(amDef, config);

Move your mouse over this icon to see the image

 

3.

Now that we have an generic application module, we will need to cast it to our specific application module. This will allow us access to any application specific code you have added, like your custom method. Add the following line of code to cast the application module.

    HRAppModuleImpl myAm = (HRAppModuleImpl)am;

Move your mouse over this icon to see the image

4.

At this point, you should be able to compile and run your class. It won't do anything interesting but it will prove that your code can successfully find and instantiate the application module.
To run the application, right-click anywhere in the code editor window and select run. Your program should run successfully. Your code should look like the following:

package model;
import oracle.jbo.ApplicationModule;
import oracle.jbo.client.Configuration;
public class TestClient 
  {
   public TestClient()
    {
    }
    /**
     * 
     * @param args
     */
    public static void main(String[] args)
     {
      String amDef = "model.HRAppModule";
      String config = "HRAppModuleLocal";
      ApplicationModule am = 
        Configuration.createRootApplicationModule(amDef, config);
      HRAppModuleImpl myAm = (HRAppModuleImpl)am;
     }
  }
         

Move your mouse over this icon to see the image

 

Adding Code to Call Your Custom Method.

Now that you have instantiated the Application Module and cast it to your own, you can add code to call your custom method.

Back to List

1.

Now that we have an instance of our application module, we can simply call the method. Remember that the method returns an oracle.jbo.domain.Number object. The following code calls the method. Notice that we have hardcoded the department Id. We could have passed an argument to the program but this is a little easier for now. When you create a more robust client application, you will call the method with a department number probably derived from a form field or the like.

Number salaryTotal = myAm.getDeptSalaryTotal("10");

Move your mouse over this icon to see the image

 

2.

That last line of code set your salaryTotal variable to the results of the call. All we have left to do is display the results. You can display the results by calling System.out.println() with some text concatenated with salaryTotal variable. The code should look like:

 System.out.println("Total salary for Department 10: " + salaryTotal);

Move your mouse over this icon to see the image

 

3.

Now we need to do a little housekeeping. Whenever you create an instance of an application module, you should remove it when you are done. The last step in creating this test client is to release the application module. The code for that is:

    Configuration.releaseRootApplicationModule(am,true);

Move your mouse over this icon to see the image

4.

You have successfully created a test client that calls your custom method. Your code should look like:

package model;
import oracle.jbo.ApplicationModule;
import oracle.jbo.client.Configuration;
import oracle.jbo.domain.Number;
public class TestClient 
 {
 public TestClient()
  {
  }
 /**
  * 
  * @param args
  */
 public static void main(String[] args)
   {
    String amDef = "model.HRAppModule";
    String config = "HRAppModuleLocal";
    ApplicationModule am = 
       Configuration.createRootApplicationModule(amDef, config);
    HRAppModuleImpl myAm = (HRAppModuleImpl)am;
           
    Number salaryTotal = myAm.getDeptSalaryTotal("10");
    System.out.println("Total salary for Department 10: " + salaryTotal);
    Configuration.releaseRootApplicationModule(am,true);
           
   }
} 
         

Move your mouse over this icon to see the image

5.

Of course the last step is to run the TestClient to see the results. Remember, to run an application, right-click anywhere in the code editor and select Run from the context menu. You will see the results in the message window at the bottom of the JDeveloper window.

Move your mouse over this icon to see the image

 

In this tutorial you learned how to create a custom method that makes it easier for client applications to access calculated or derived data. Using custom methods to provide that specialized data reduces the amount of client coding and therefore reduces the risk of client applications introducing bugs into the application.

You have also learned how to test your custom method by creating a Java program that instantiates your application module and calls the method.

Move your mouse over this icon to hide all screenshots

E-mail this page
Printer View Printer View
Oracle Is The Information Company About Oracle | Oracle RSS Feeds | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Privacy