Use View Link Accessor to call aggregate functions, set attribute value , set bind variables value (Oracle ADF)

Sharing is Caring
Hello All
This post is about the various uses of the view link accessor, when we create viewLink between two view objects, destination accessor is created by default in the master view object, there is a checkbox to create source accessor also at the same time.We can use this view link accessor for calculating attribute’s sum, setting the value of attributes etc

 

View Link Accessor

here Departments is master viewObject and Employees is it’s detail, after creating viewLink you can see in viewObject XML source, the accessor is present there.

In Departments ViewObject- 

<ViewLinkAccessor
    Name="Employees"
    ViewLink="sample.model.view.link.DeptTOEmpVL"
    Type="oracle.jbo.RowIterator"
    IsUpdateable="false"/>

In EmployeesViewObject- 
 

<ViewLinkAccessor
    Name="Departments"
    ViewLink="sample.model.view.link.DeptTOEmpVL"
    Type="oracle.jbo.Row"
    Reversed="true"
    IsUpdateable="false"/>

So what is the use of these view link accessors?
Master accessor in detail viewObject returns current Row of master viewObject when you generate RowImpl class for detail view object, it also has a method for this accessor

/**
 * Gets the associated <code>Row</code> using master-detail link Departments.
 */
public Row getDepartments() {
return (Row) getAttributeInternal(DEPARTMENTS);
}

/**
 * Sets the master-detail link Departments between this object and <code>value</code>.
 */
public void setDepartments(Row value) {
setAttributeInternal(DEPARTMENTS, value);
}

Detail accessor in master viewObject returns a row set of all row of details viewObject that is currently referenced by the master record

    /**
     * Gets the associated <code>RowIterator</code> using master-detail link Employees.
     */
    public RowIterator getEmployees() {
        return (RowIterator) getAttributeInternal(EMPLOYEES);
    }

Now see what we can do with viewLink Accessor

1. Get master attribute value in detail view object

suppose I have to get an attribute’s value from Master viewObject (Departments) in an attribute of detail viewObject (Employee)
in this example, I am getting managerId from Departments ViewObject so for this just write in the expression of Employee’s ManagerId field
viewLinkAccesorName.AttributeName
 
now run BC4J tester and check – create a new record in Employee and see managerId from Departments is auto-populated
 

on creating a new record-

 

2. Call aggregate function to calculate the sum of an attribute of detail view object

suppose now I have to calculate total salary of a Department (sum of Employees salary of that department)
for this purpose just call sum function to calculate the sum of all rows of detail RowSet
take a transient attribute in Departments ViewObject and write in its expression
viewLinkAccesorName.sum(“AttributeName”)
 

Run ApplicationModule and see-

 

3. Set bind variable value as per master view object’s attribute (pass value from master view object)

This is tricky part as we cannot set bind variable value through expression as it doesn’t recognize view link accessor name.
created view criteria and bind variable for managerId in Employee viewObject

 

applied this criteria to Employees view object instance in Application Module (Just Shuttle criteria to selected side)
now to set bind variable’s value we have to override prepareRowSetForQuery method in Employees VOImpl class
this method gets the value of managerId from currentRow of master ViewObject (Departments)and sets in bind variable of Employees view object

    @Override
    public void prepareRowSetForQuery(ViewRowSetImpl viewRowSetImpl) {
        RowSetIterator[] masterRows = viewRowSetImpl.getMasterRowSetIterators();
        if (masterRows != null && masterRows.length > 0 && masterRows[0].getCurrentRow() != null &&
            masterRows[0].getCurrentRow().getAttribute("ManagerId") != null) {
            Integer managerId = (Integer) masterRows[0].getCurrentRow().getAttribute("ManagerId");
            viewRowSetImpl.ensureVariableManager().setVariableValue("BindManagerId", managerId);
            System.out.println("ManagerID in bind Var-" + managerId);

}
super.prepareRowSetForQuery(viewRowSetImpl);
}

now run AM and check it-

Cheers 🙂 Happy Learning

Related Posts

An Oracle ACE, Blogger, Reviewer, Technical Lead working on Oracle ADF

Leave a Reply

Your email address will not be published. Required fields are marked *