Showing posts with label Vo. Show all posts
Showing posts with label Vo. Show all posts

Wednesday, 9 April 2014

ADF : Programmatic ViewObjects in ADF, Populating programmatic viewObject with an ArrayList

Hello all,

Today I am going to demonstrate about how to make programmatic viewObject in ADF.
There are basically 4 types of viewObjects.

1. ViewObjects based on Entity.
2. ViewObjects based on SqlQuery (also called Readonly viewObject)
3. Static viewObjects (contains fix now of rows)
4. Programmatic viewObjects (Programmatic viewObjects are viewObjects that are not populated from an sql query).

For understanding  the working of programmatic viewObjects, you must first understand the lifecycle of a viewObject. You need to know what methods are called when a viewObject is executed. Here is the sequence of methods that are called when a viewObject is executed.

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.
- See more at: http://adfjavacodes.blogspot.in/2013/12/how-viewobjects-get-executed.html#sthash.whg7AwM9.dpuf

Now create an ADF Application and create a programmatic viewobject



Then press next and define attributes for the viewObject




Generate the following java classes




Click Finish.


Now we have made the viewObject. We will have to override the lifeCycle methods. Here is the code that I have used for this viewObject.


package programmaticvotestapp.model.views;

import java.sql.ResultSet;

import java.util.ArrayList;

import oracle.jbo.Row;
import oracle.jbo.server.ViewObjectImpl;
import oracle.jbo.server.ViewRowImpl;
import oracle.jbo.server.ViewRowSetImpl;

import programmaticvotestapp.model.EmpDC;
// ---------------------------------------------------------------------
// ---    File generated by Oracle ADF Business Components Design Time.
// ---    Wed Apr 09 14:28:40 IST 2014
// ---    Custom code may be added to this class.
// ---    Warning: Do not modify method signatures of generated methods.
// ---------------------------------------------------------------------
public class ProgrammaticVOImpl extends ViewObjectImpl {
    private ArrayList<EmpDC> empList = new ArrayList<EmpDC>(); 
    /**
     * This is the default constructor (do not remove).
     */
    public ProgrammaticVOImpl() {
    }

    /**
     * executeQueryForCollection - overridden for custom java data source support.
     * This method is executed at first.
     * So in this method we need to load our data source . 
     * In simple words initialize the list
     */
    protected void executeQueryForCollection(Object qc, Object[] params, int noUserParams) {
        // Populate the list for the firstTime
        populateArrayList();
        //To set the initial position for fetch to start
        setFetchPosition(qc,0);
        super.executeQueryForCollection(qc, params, noUserParams);
    }

    /**
     * hasNextForCollection - overridden for custom java data source support.
     * This method is called after executeQueryForCollection to check if any row exist in the datasource or not.
     * When returned true, createRowFromResultSet is called and a new row from the rowset is created.
     */
    protected boolean hasNextForCollection(Object qc) {
        return getFetchPosition(qc) < empList.size();
    }

    /**
     * createRowFromResultSet - overridden for custom java data source support.
     * creates a newRow and adds it to viewObject
     */
    protected ViewRowImpl createRowFromResultSet(Object qc, ResultSet resultSet) {
        int fetchPosition = getFetchPosition(qc);
        System.out.println("Fetch Position is : "+fetchPosition);
        ViewRowImpl newRow = (ViewRowImpl)createNewRowForCollection(qc);
        EmpDC c = empList.get(fetchPosition);
        // Setting value in the new row which is created.
        newRow.setAttribute("EmpId", c.getEmpId());
        newRow.setAttribute("EmpName", c.getEmpName());
        newRow.setAttribute("Salary", c.getSalary());
        // Updating the fetch Position
        setFetchPosition(qc, fetchPosition+1);
        
        return newRow;
    }

    /**
     * getQueryHitCount - overridden for custom java data source support.
     */
    public long getQueryHitCount(ViewRowSetImpl viewRowSet) {
        long value = empList.size();
        return value;
    }
    /**
         * Method to set the new fetch position
         * @param rowset
         * @param position
         * To set the position on the nextRecord to fetch i.e. next record of arrayList
         */
        private void setFetchPosition(Object rowset, int position) {
            if (position == empList.size()-1) {
                setFetchCompleteForCollection(rowset, true);
            }
            setUserDataForCollection(rowset, new Integer(position));
        
    }
    /**
         * Method to get the current fetch position
         * @param rowset
         * @return
         * 
         * This method gets the fetchPosition to fetch the row from the arrayList to retrieve the data
         */
        private int getFetchPosition(Object rowset) {
            int value = ((Integer)getUserDataForCollection(rowset)).intValue();
            return value;
        }
    
    public void populateArrayList(){
        empList.clear();
        empList.add(new EmpDC(1,1100,new StringBuffer("First Employee")));
        empList.add(new EmpDC(2,2100,new StringBuffer("Second Employee")));
        empList.add(new EmpDC(3,1300,new StringBuffer("Third Employee")));
        empList.add(new EmpDC(4,1700,new StringBuffer("Fourth Employee")));
        empList.add(new EmpDC(5,1200,new StringBuffer("Fifth Employee")));
        empList.add(new EmpDC(6,5100,new StringBuffer("Sixth Employee")));
        empList.add(new EmpDC(7,1900,new StringBuffer("Seventh Employee")));
        empList.add(new EmpDC(8,1200,new StringBuffer("Eight Employee")));
        empList.add(new EmpDC(9,1200,new StringBuffer("Ninth Employee")));
        empList.add(new EmpDC(10,1100,new StringBuffer("Tenth Employee")));
    }
}

on running the application you can see



You can download the sample application here ProgrammaticVoApp

Likewise you can use other datasources to populate the programmatic viewObject. The above model shows a very simple implementation, use cases can be much complex.


References :
1 .http://huysmansitt.blogspot.in/2012/08/adf-programmatical-views-made-simple.html?showComment=1397033254692#c2288712975950397947
2 .http://adfjavacodes.blogspot.in/2013/12/how-viewobjects-get-executed.html


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 .


Monday, 2 September 2013

Following the MVC Architecture in ADF, AMImpl method Called in Managed Bean

Hello everyone,

Here am about to explain how we can optimize our application for performance in ADF. ADF follows MVC architecture for application development.So there are times when we need an instance of model impl classes in the bean such as for example: we need AppModuleImpl in our bean class to get or fetch value from a view object. So for that we have to create an AppModuleImpl instance in the bean and then get viewObject from that.

So, if we get the AppModuleImpl object in bean then the purpose of MVC is being violated.I mean then we would be merging the model and viewController separation. And further more new instances of AppModuleImpl and etc, will created in our Managed Bean, this adds to violation of MVC Architecture . So it is better to keep the database logic in the database layer i.e. model and keep the controller logic in in viewController.

So the problem is that if we have to interact with the model layer from the bean, then how shall we do it?

The answer to this question is the use of ADF bindings.
Binding is used to interact with model layer from the viewController layer.

Suppose in a scenario, just for example we have to get the location of the employee by using EmployeeId. This needs interaction with the model layer.

So to do that we have to create a method that will take EmployeeId as input and give back location in return. 

  1. Go to the AppModuleImpl and create a method.

    **Please do not use "Object" type in return parameter or in inputParameter,if you do this then the method will not be shown in ClientInterface**
        public Integer getLoc(Integer EmpId){
            Integer sal = null;
            Row[] row_2 = this.getEmployee1().getFilteredRows("EmployeeId", EmpId);
            if(row_2.length>0){
                sal = Integer.parseInt(row_2[0].getAttribute("Salary").toString());
            }
            return sal;
        }
    


  2. Then go to clientInterface of the AM and add the method to clientInterface.





  3. Then you have to create binding for this method on the page.







  4. Now we have to call this binding and put the parameters in it from the bean.
        public void fetchSalary(ActionEvent actionEvent) {
            OperationBinding binding = getBindings().getOperationBinding("getLoc");
            // to put parameters value in the method
            binding.getParamsMap().put("EmpId", empId);
            Object execute = binding.execute();
            salary  = (Integer) execute;
        }
    
    
    
  5. Now when you run the application.


    and when Fetch Salary button is clicked

  6. You can download the sample application here : MethodBindingApp.rar.
  7. If you have any query regarding this, please write in the comments. 

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. :)