Please disable your adblock and script blockers to view this page

Search this blog

Showing posts with label createViewLinkAccessorRS. Show all posts
Showing posts with label createViewLinkAccessorRS. Show all posts

Wednesday 13 August 2014

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


Hello All

This post is about various usage of view link accessor  , when we create viewLink between two viewObjects , destination accessor is created by default in master viewObject, there is check box to create source accessor also at same time

We can use this view link accessor for calculating attribute's sum, setting value of attributes etc



here Departments is master viewObject and Employees is it's detail, after creating viewLink you can see in viewObject xml source there accessor is present

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 viewObject , 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 are currently referenced by 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 viewObject

suppose i have to get a attribute's value from Master viewObject (Departments) in a attribute of detail viewObject (Employee)
in this example i am getting managerId from Departments ViewObject so for this just write in expression of Employee's ManagerId field
viewLinkAccesorName.AttributeName


now run BC4J tester and check - create new record in Employee and see managerId from Departments is auto populated

 on creating new record-


2. Call aggregate function to calculate sum of an attribute of detail viewObject

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 sum of all rows of detail RowSet 
take a transient attribute in Departments ViewObject and write in it's expression
viewLinkAccesorName.sum("AttributeName")


 Run ApplicationModule and see-


3. Set bind variable value as per master viewObject's attribute (pass value from master viewObject)

This  is tricky part as we can not set bind variable value through expression as it doesn't recognize view link accessor name.
created a viewCriteria and bind variable for managerId in Employee viewObject




applied this criteria to Employees viewObject 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 viewObject


    @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-

Happy Learning :)

Wednesday 23 July 2014

Search on (Filtering) child nodes of af:treeTable using viewCriteria in Oracle ADF (11g,12c)

Hello all
this post is about filtering treeTable on basis of child nodes in Oracle ADF
in this tutorial i have used Departments and Employees table of HR schema to create treeTable
see how to create treeTable-
http://www.awasthiashish.com/2012/11/tree-table-component-in-oracle.html
http://www.awasthiashish.com/2013/08/tree-table-component-with-declarative.html

treeTable look like this-


now next is to search on Employee Names



  • i have dropped a input text for search string and a button to search on page , here i am searching on first name of employees 


  • now created a view Criteria in Employee viewObject to search on first name


  • This viewCriteria doesn't work directly on Employee viewObject, to search in treeTable we have to override createViewLinkAccessorRS method in Departments (master vo) VOImpl class, and in this method we have to call Employees ViewCriteria explicitly , this will filter Employee Vo Rowset as per bind-variable value when a node is disclosed at runtime

  •     /**
         * @param associationDefImpl
         * @param viewObjectImpl
         * @param row
         * @param object
         * @return
         */
        protected ViewRowSetImpl createViewLinkAccessorRS(AssociationDefImpl associationDefImpl,
                                                          ViewObjectImpl viewObjectImpl, Row row, Object[] object) {
    
    
            ViewRowSetImpl viewRowSetImpl = super.createViewLinkAccessorRS(associationDefImpl, viewObjectImpl, row, object);
    
            String firstName = getFirstNm();
            ViewCriteriaManager vcm = viewObjectImpl.getViewCriteriaManager();
            ViewCriteria vc = vcm.getViewCriteria("EmployeesVOCriteria");
            VariableValueManager vvm = vc.ensureVariableManager();
            vvm.setVariableValue("BindFirstNm", firstName);
            viewObjectImpl.applyViewCriteria(vc);
            return viewRowSetImpl;
    
    
            // return super.createViewLinkAccessorRS(associationDefImpl, viewObjectImpl, row, object);
        }
    

  • to pass bind-variable value i have created a variable and it's accessors in VoImpl and exposed set method to client that is further used by managed bean

  •     private String firstNm;
        public void setFirstNm(String firstNm) {
            this.firstNm = firstNm;
        }
    
        public String getFirstNm() {
            return firstNm;
        }
    

  • now this setter method is called in managed bean search button action to set bind variable value

  •     /**Method to search in treeTable childs
         * @param actionEvent
         */
        public void searchAction(ActionEvent actionEvent) {
            if (firstNmBind.getValue() != null) {
                OperationBinding ob = executeOperation("setFirstNm");
                // firstNmBind is binding of inputText
                ob.getParamsMap().put("firstNm", firstNmBind.getValue().toString());
                ob.execute();
                /* Method Expand treeTable after Search see- 
                 http://www.awasthiashish.com/2013/10/expand-and-collapse-aftreetable.html*/
                expandTreeTable();
                AdfFacesContext.getCurrentInstance().addPartialTarget(treeTabBind);
    
            }
        }
    

  • Run your application and see-
    Cheers- Happy Learning
    Download Sample ADF Application