Exposing View Links to Entity Objects

JDeveloper makes it possible for you to use the relationships defined by View Links to better define the results obtained while traversing Entity Objects. JDeveloper does this by letting you create a method inside an Entity Object that provides easy access to data returned by the View Link.

Assume that you want to write business logic that will delete from the database the records of terminated employees for a given department. Assume also that your database represents terminated employees by a negative employee number. You can create an accessor method that will use the View Link to return only those rows where the employee number is negative. To do this, use the View Link Wizard to specify the source and destination View Objects, the name of the accessor method, the source and destination role attributes, and a SQL statement that will return just the data you need. This example assumes that you already have a project with Dept and Emp Entity Objects, and DeptView and EmpView View Objects.

  1. Right-click the package name and select Create View Link to open the View Link Wizard.

  2. In the Name Panel, name the View Link DeptToEmpVL.

  3. In the Association Views panel, select DeptView as the source View and EmpView as the destination View. Select the Generate accessor in Entity checkbox and enter TerminatedEmps in the Accessor name field.

  4. In the Source Role Attributes and Destination Role Attributes Panels, select Deptno.

  5. In the Association SQL panel, add AND Emp.Empno<0 to the Where field, so that the full query statement reads Dept.DEPTNO = Emp.DEPTNO AND Emp.Empno<0.

  6. Click Finish to generate the View Link.

JDeveloper adds the following code to your DeptImpl.java file:

/**
 * Uses the link DeptToEmpVL to return rows of EmpView
 */

 public oracle.jbo.RowIterator getTerminatedEmps() {
  return (oracle.jbo.RowIterator)getAttributeInternal(TERMINATEDEMPS);
  }

To remove the records of terminated employees for a given department from the database, you could write code like this on the Dept Entity:

public void purge()
{
oracle.jbo.Row r;
oracle.jbo.RowIterator empRSI = getTerminatedEmployees();
while(empRSI.hasNext())
{
r = empRSI.next();
r.remove();
}
}

The purge() function is now an operator that is available on the Dept Entity Object. In this manner, business logic and operators can be built up.

You can then use the purge() function as part of a program to remove terminated employees. The following function, doProcess() tests whether purge()works correctly. The function retrieves the Dept and Emp View Objects, marks the first employee in the first department as terminated, then calls purge() to remove the employee's record from the database.

public void doProcess() throws Exception
{
appModule = getApplicationModule("HRappModule");
      // get view usages
ViewObject deptVO = appModule.findViewObject("DeptView");
ViewObject empVO = appModule.findViewObject("EmpView");
      // setup dataWriter to retrieve and output data
openWriter();
      deptVO.setRangeSize(-1);
empVO.setRangeSize(-1);
      deptVO.reset();
Row deptRow;
Row empRow;
       // get the first emp row in the first department, mark the emp
// for termination by setting by setting empno = -1
      deptVO.first();
empRow = empVO.first();
      dataWriter.writeln("Employee to be terminated
:"+empRow.getAttribute("EmpName").toString());
      empRow.setAttribute("EmpNum",new Integer(-1));
appModule.getTransaction().commit();
      // Now purge all the Terminated emps
      DeptVORowImpl dRow = (DeptVORowImpl)deptVO.first();
DeptImpl deptEO = dRow.getDeptUsage();
      deptEO.purge();
appModule.getTransaction().commit();
      closeWriter();
}
}


Related topics

About Exposing View Links to Entity Objects