Adding and Editing a Method that Executes in Response to an Entity Event

While editing an entity object with the Entity Object Editor, you can use the Subscribe page to define a method that the subscriber will execute in response to a particular event. Click Add or Edit, and select a method and the arguments that should be passed to the method.

Warning: This panel only validates that the count of the arguments match the required count for the selected method. No type-checking is done for the arguments. The generated code may try to perform a conversion, but will fail if the event parameter's type is not convertible to the type of the argument a method expects.

Field

Description

Method

The name of the method. You can accept the default method supplied by JDeveloper, or choose another method from the drop-down list. You can also click Browse to choose a Java class for the method.

Available

The Available column lists the available parameters (attributes) that you can include in the event. Select the attributes you want to include, and click the single right arrow to move them to the Selected list. Click the double right arrow to move all of the attributes to the Selected list.

Selected

The Selected column list displays an ordered list of parameters that are selected to be passed as arguments to the selected method.

Note: The Selected attribute list should match the count and type of the arguments for the selected method. JDeveloper verifies only the count. Type cast errors are thrown only at runtime.

Up and down arrow buttons

Select an attribute and click the up or down arrow to move it up or down in the selected list by one position.

Example

The following steps show how to make a Dept entity object subscribe and respond to a change in an employee's salary. This example assumes that Emp and Dept are defined in a package named d2e, and that the Emp entity object publishes a SalaryChange event as described in Publishing an Event. The first section defines a method that executes when the event is fired. The second section describes how to subscribe to the event.

Implement a method to which to subscribe:

Implement a method to execute when an Emp entity object instance fires the SalaryChange event. Use the Source Editor to add a calcDeptTotal method (the method name is arbitrary) to the Dept entity object's .java file. Import oracle.jbo.domain.Number and paste the following code into the Dept class definition in DeptImpl.java.

public Number calcDeptTotal(Number EMPNO, 
Number NewSalary,
Number DEPTNO) {
      Number total = new Number(0); 
RowIterator emps = this.getEmp();
EmpImpl empRow = null;
emps.reset();
while (emps.hasNext()) {
empRow = (EmpImpl)emps.next();
total = total.add(empRow.getSal());
}
      System.out.println("TOTAL ->"+total); 
return total;
  } 

To subscribe to the new method:

  1. Right-click the entity object named Dept and choose Edit.

  2. Click the Subscribe tab.

  3. Choose SalaryChange from the Published Events list, then click the right-arrow to move it to the Subscribed Events list.

  4. Choose SalaryChange from the Subscribed Events list, then click the Add button to open the Define Method dialog.

  5. In the Define Method dialog, choose calcDeptTotal from the Method dropdown list. The wizard adds attributes Empno, Sal, and Deptno to the Available list.

  6. Click the double right-arrow to move Empno, Sal, and Deptno from the Available list to the Selected list. Empno should be first in the Selected list. If it is not, click it, then click the up-arrow to move it to the top of the list.

  7. Click OK to close the Define Methods dialog box, then click Finish to close the Entity Object Wizard. The Business Components for Java framework generates Java and XML code for the event subscription. At run time, when an employee's salary changes, the Dept entity object responds by executing calcDeptTotal.