ADF: Programmatic View Objects in ADF, Populating programmatic View Object with an ArrayList

Here I am going to show how to create a programmatic View Object in oracle ADF.

Photo by Caspar Camille Rubin on Unsplash

There are basically 4 types of View Objects.
1. View Objects based on Entity
2. View Objects based on SQL Query (also called Read-only View Object)
3. Static View Objects (contains fix now of rows)
4. Programmatic view Objects (Programmatic view Objects are populated by some logic programmatically rather than an SQL Query).

For understanding the working of programmatic view Objects, you must first understand the lifecycle of a View Object. You need to know what methods are called when a View Object is executed.

Here is the sequence of methods that are called when a View Object is executed.

When a View Object is called the following methods are executed in the given sequence.

  1. executeQueryForCollection(Object qc, Object[] params, int noUserParams):
    At first when the View Object is first executed the method first called in the ViewObjectImpl is executeQueryForCollection.
    This method executes the database query in the View Object and then calls the next method.

Check more at about view object lifecycle at: http://adfjavacodes.blogspot.in/2013/12/how-viewobjects-get-executed.html#sthash.whg7AwM9.dpuf

Follow the steps below to create a programmatic view object.

  1. To create an ADF Application and create a programmatic view object.

2. Then press next and define attributes for the viewObject

3. Generate the following java classes

4. Now we have created the View Object. We will have to override the life Cycle methods. Here is the code that I have used for this View Object.

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")));
}
}

5. On running the application.

You can download the sample application here 1 . ProgrammaticVoApp

Likewise you can use other data sources to populate the programmatic view Object. The above model shows a very simple implementation, use cases can be much complex.

Software Engineer