Showing posts with label Viewlink. Show all posts
Showing posts with label Viewlink. Show all posts

Wednesday, 4 December 2013

ADF : How ViewObjects (VO) get executed ? | View Object LifeCycle

View Objects are esssential part of ADF Business Components . These are associated with represention of DataSet.
ViewObjects contains a Query which when executed returns a set of results in form of rows. Then the view Object convert the rows returned from the query to ADF undestandable form i.e. ViewObjectRowImpl form.
So, the question is what happens when a viewObject gets a call?

ViewObject goes through a series of methods before representing a dataset. In other words we can name it ViewObject LifeCycle .


LifeCycle

When a viewObject is called the following methods are executed in the given sequence.
  • At first when the viewObject is first executed the method first called in the ViewObjectImpl is
    executeQueryForCollection(Object qc, Object[] params, int noUserParams)
    This method executes the Database Query in the viewObject and then calls the next method.

  • After executeQueryForCollection is executed then method hasNextForCollection(Object qc) is called. This method checks if the collection returned have a row or not. If hasNextForCollection(Object qc) returns True then the next method of the lifeCycle is called which converts the row to ADF understandable form i.e. into ViewObjectRowImpl from.

  • So when method hasNextForCollection retuns true then method createRowFromResultSet(Object qc, ResultSet resultSet) is called and this method converts the row into ADF understandable form.

  • This goes on until all the rows are covered and there is no rows left in collection. When there are no rows in the collection then the method hasNextForCollection returns false .

  • Then method setFetchCompleteForCollection(java.lang.Object qc,boolean val) is called and it sets the flag for fetch completion. This indicates that the rows from the collection are fetched.


Here is a diagrammatic representation of ViewObject LifeCycle .


Saturday, 20 April 2013

ADF: Calculating total sum of Salary of all the employees in a Department using Groovy expression.

If there is a condition in which you have to calculate the sumtotal of a VO field in ADF (like if you want to calculate the sum of salary of all the employees of a Department) , then better of all option is to use groovy expression to calculate the sum.
Here is an application which is used to calculate the sum of salary of all the employees of a given department.

  1. Create a new ADF application and create database connection with HR Schema. Generate the Business components. Here am using two tables ie Employee and Departments.
  2. Now create a ViewLink between Department and Employee using DepartmentId as foreign key.

  3. Use the Accessor to get the salary from the employee table to department table.
  4. Then create a transient variable in the DepartmentVO, and then assign value to it using the expression

    EmpVO.sum("SalTot");
  5. Here is an screen shot. The name 'EmpVO' is the name of the Accessor of the Employee table in the deptToEmpViewLink.
  6. Then run the AM to see the Application.
  7. You can find the ADF application at Groovy.rar

Friday, 12 April 2013

how to populate data in ADF table from Bean using POJO | Populating af:table using ArrayList

Hi,

This post is about populating af:table from a POJO from the bean.

Suppose we have to show information related to books to the user.
So first we have to understand how we populate a table from a List.
  1. For this we have to create a dataType for storing the data of the book.
  2. For the same, we have created a Class named BookInfo. Here is the code for on the BookInfo class.

    package edittableonpojo.view.dc;
    
    public class BookInfo {
        private String bookDesc;
        private String bookAuthNm;
        private String bookSrNo;
        
        public BookInfo(String bookSrNo,String bookDesc,String bookAuthNm) {
            super();
            this.bookSrNo = bookSrNo;
            this.bookAuthNm = bookAuthNm;
            this.bookDesc = bookDesc;
        }
    
        public void setBookDesc(String bookdesc) {
            this.bookDesc = bookdesc;
        }
    
        public String getBookDesc() {
            return bookDesc;
        }
    
        public void setBookAuthNm(String bookAuthNm) {
            this.bookAuthNm = bookAuthNm;
        }
    
        public String getBookAuthNm() {
            return bookAuthNm;
        }
    
        public void setBookSrNo(String bookSrNo) {
            this.bookSrNo = bookSrNo;
        }
    
        public String getBookSrNo() {
            return bookSrNo;
        }
        
        public String getKey(){
            return this.getBookSrNo();
        }
        
        public String toString(){
            return bookSrNo+"_"+bookAuthNm+"_"+bookDesc;
        }
    }
    

  3. Then we have to create a Bean from which data will be provided to the table. In the constructer of the Bean the code is written to populate the data in the ArrayList. Here is the code of the Bean.

    package edittableonpojo.view.bean;
    
    import edittableonpojo.view.dc.BookInfo;
    
    import java.util.ArrayList;
    
    public class BookBean {
        private ArrayList<BookInfo> bookDtls = new ArrayList<BookInfo>();
    
    
        public BookBean() {
            bookDtls.add(new BookInfo("1A", "The Alchemist", "Paulo Cohelo"));
            bookDtls.add(new BookInfo("2A", "Game Of Thrones", "George R. R. Martin"));
            bookDtls.add(new BookInfo("3A", "Five Point Someone", "Chetan Bhagat"));
            bookDtls.add(new BookInfo("4A", "Harry Potter", "J.K.Rowling"));
            bookDtls.add(new BookInfo("5A", "Wings of Fire", "A.P.J Abdul Kalam"));
        }
    
        public ArrayList<BookInfo> getBookInfo() {
            return bookDtls;
        }
    }
    

  4. Now we have to create a Page where we can show the data of the List. For this, I have created a jspx page and dropped an af:table in which we will show the data and done some styling ;).
  5. The table contains three Columns
    1. Book SrNo.
    2. Author Name.
    3. Book Description
  6. In the Value attribute of the Table from the Property inspector, select the value of the attribute from the bean-like   
<af:table var="row" rowBandingInterval="0" id="t1" value="#{viewScope.BookBean.bookInfo}"
                                  rowSelection="none"
                                  inlineStyle="line-Height: 15px;">

  1. In the Columns drop af:outputText and in the value of the outputText put the value like value="#{row.bookAuthNm}" for showing the Author name , and similarly you have to put values in other rows.
  2. Here is the code of the jspx page.

    <?xml version='1.0' encoding='UTF-8'?>
    <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1" xmlns:f="http://java.sun.com/jsf/core"
              xmlns:af="http://xmlns.oracle.com/adf/faces/rich">
        <jsp:directive.page contentType="text/html;charset=UTF-8"/>
        <f:view>
            <af:document title="untitled1.jspx" id="d1">
                <af:form id="f1">
                    <af:panelBox text="Book Details" id="pb1">
                        <f:facet name="toolbar"/>
                        <af:panelCollection id="pc1" styleClass="AFStretchWidth">
                            <f:facet name="menus"/>
                            <f:facet name="toolbar"/>
                            <f:facet name="statusbar"/>
                            <af:table var="row" rowBandingInterval="0" id="t1" value="#{viewScope.BookBean.bookInfo}"
                                      rowSelection="multiple"
                                      inlineStyle="line-Height: 15px;">
                                <af:column sortable="true" headerText="Book SrNo." id="c1">
                                    <af:outputText value="#{row.bookSrNo}" id="ot1"/>
                                </af:column>
                                <af:column sortable="false" headerText="Author Name" id="c2">
                                    <af:outputText value="#{row.bookAuthNm}" id="ot2"/>
                                </af:column>
                                <af:column sortable="true" headerText="Book Description" id="c3" width="300">
                                    <af:outputText value="#{row.bookDesc}" id="ot3"/>
                                </af:column>
                            </af:table>
                        </af:panelCollection>
                    </af:panelBox>
                </af:form>
            </af:document>
        </f:view>
    </jsp:root>
    

  3. The jspx page looks like this.
  4. Now simply run the application.
  5. After the screen that comes up is something like this.
Here is the sample application : TableOnPOJO.rar

Monday, 25 February 2013

Code to find an UI Component in an Oracle ADF Application

## Find an UI Component in the page

Hello all,
There are cases where you have to find a component on Page by id in the bean. Here is the method.


private UIComponent findComponentOnPage(String id){    
    String comp = id;
   //see ADF Code Corner sample #58 to read about the code
//used in the search below
FacesContext fctx = FacesContext.getCurrentInstance();
UIViewRoot root = fctx.getViewRoot();            
   root.invokeOnComponent(fctx,comp,
new ContextCallback(){
      public void invokeContextCallback(FacesContext facesContext,UIComponent uiComponent) {
       lookupComponent = uiComponent;
     }         
  });
return lookupComponent;
}

Thursday, 21 February 2013

ADF: Code to use rowIterator

Hi,
Here is the method to use iterator in ADF Applications.

1)To get iterator object populated with the view rows
RowsetIterator itr=view.createRowsetIterator(null);
*dont use view.getIterator
2)To iterate within the iterator
itr.hasNext();
its.next();

If you have any questions regarding this or in ADF. I will be happy if i can help. :)

ADF: Code to validate duplicate record in Java Bean.

Hi,

Here am posting the code for applying Duplicate validation in ADF Applications through Java Bean. This can be used to check duplicate records in the oracle ADF Application.


    public void empIdValidator(FacesContext facesContext, UIComponent uIComponent, Object object) {
        if (object != null) {
            // 'am' is the applcation module
            ViewObjectImpl vo = am.getVo();
            // To get current Row off the Vo
            Row cRow = vo.getCurrentRow();
            // Create a Iterator to Iterate Over the ViewObject Vo
            // There is a also a method called vo.getRowSetIterator() but it is advised to use vo.createRowSetIterator(null)
            Integer count = 0;
            RowSetIterator rsi = vo.createRowSetIterator(null);
            while (rsi.hasNext()) {
                Row rw = rsi.next();
                if (rw != cRow) {
                    if (rw.getAttribute("EmpId") != null) {
                        Integer empId = (Integer) rw.getAttribute("EmpId");
                        if (empId = (Integer) object) {
                            count = 1;
                        }
                    }
                }
            }
            // Closing the iterator is advised after the use
            rsi.closeRowSetIterator();
            if (count == 1) {
                FacesMessage message2 = new FacesMessage("Duplicate EmpId !");
                message2.setSeverity(FacesMessage.SEVERITY_ERROR);
                throw new ValidatorException(message2);
            }
        }
    }

If you have any Questions regarding the above code. Feel free to ask. :)

ADF: Popup Dialauge listener


## ADF: Popup Dialauge listener

public void dailogok(DialogEvent dialogEvent) {
   
    if(dialogEvent.getOutcome().name().equals("ok")){
        BindingContainer bindings = getBindings();
        OperationBinding operationBinding =bindings.getOperationBinding("Commit");
        operationBinding.execute();
    }