Follow by Email

Saturday, 30 July 2016

ADF Basics: Populate simple programmatic View Object using POJO


Hello All

I hope you all know that we have 4 type of viewObjects in ADF, Here we'll discuss about a simple programmatic viewObject
  • Entity Object Based
  • SQL Query Based
  • Static List Based
  • Programmatic ViewObject
Programmatic ViewObject can be created by overriding appropriate methods in ViewObject's Impl class to retireve data from REF CURSOR, Array or any Java file
Here in this post I am using an ArrayList (POJO based) to populate viewObject records so for that I'll override some methods of ViewObject Impl class


executeQueryForCollection- This method is start of viewObject execution , here we can initialize values (for query and EO based viewObjects it is called whenever query needs to be executed)

hasNextForCollection-This method executes hasNext on viewObject rowset and returns true if there are more rows to create and if it's an end then it returns false

createRowFromResultSet-This method populates each row of fetched data , actually creates a new row and populate data for each attribute of that row

So this is basic theory behind programmatic viewObject implementation, now we'll see how to create a simple programmatic viewObject

  • Create a Fusion Web Application , Right click on Model project and select viewObject from new window


  • ViewObject creation wizard appears , Set viewObject name and it's type to programmatic in first step


  • In Step 2 define attributes for this viewObject, Here I am defining 3 attributes (Name and Type)


  • Set updatable always for attributes and define atleast one keyAttribute in step 3


  • Check the box to create ViewObject Impl and RowImpl classes in step 4


  • In Step 5 Add this viewObject to an Application Module, Provide Application Module name


So viewObject creation is done now next is to popuate data in this viewObject , Here you can see I have taken 3 attributes in viewObject so for that created a POJO class in model project that'll be further used in ArrayList to populate data

public class CharacterListBean {
    //Constrcutor to add new record in POJO class
    public CharacterListBean(String id, String sName, String cName) {
        this.Id = id;
        this.seasonName = sName;
        this.characterName = cName;
    }
    //Variables for each viewObject attribute and it's accessors
    private String Id;
    private String seasonName;
    private String characterName;


    public void setId(String Id) {
        this.Id = Id;
    }

    public String getId() {
        return Id;
    }

    public void setSeasonName(String seasonName) {
        this.seasonName = seasonName;
    }

    public String getSeasonName() {
        return seasonName;
    }

    public void setCharacterName(String characterName) {
        this.characterName = characterName;
    }

    public String getCharacterName() {
        return characterName;
    }
}

Now override methods in View Object Impl class , There are proper comments in code so you can understand that 

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 programmaticvodemo.model.bean.CharacterListBean;
// ---------------------------------------------------------------------
// ---    File generated by Oracle ADF Business Components Design Time.
// ---    Sat Jul 30 11:55:06 IST 2016
// ---    Custom code may be added to this class.
// ---    Warning: Do not modify method signatures of generated methods.
// ---------------------------------------------------------------------
public class ProgTestVOImpl extends ViewObjectImpl {
    /**
     * This is the default constructor (do not remove).
     */
    public ProgTestVOImpl() {
    }
    //Array List to store Season-Character Information as using POJO class CharacterListBean
    private ArrayList<CharacterListBean> characterList = new ArrayList<CharacterListBean>();

    /**Initialize values here, Used POJO based class to add records in List
     * executeQueryForCollection - overridden for custom java data source support.
     */
    @Override
    protected void executeQueryForCollection(Object qc, Object[] params, int noUserParams) {
        //Initialize Records here
        characterList.add(new CharacterListBean("SC1", "Game of Thrones", "Tywin lanister"));
        characterList.add(new CharacterListBean("SC2", "Game of Thrones", "Tyrion lanister"));
        characterList.add(new CharacterListBean("SC3", "Game of Thrones", "Jon Snow"));
        characterList.add(new CharacterListBean("SC4", "Arrow", "Oliver Queen"));
        characterList.add(new CharacterListBean("SC5", "Arrow", "Sara Lance"));
        characterList.add(new CharacterListBean("SC6", "The Flash", "Barry Allen"));
        characterList.add(new CharacterListBean("SC7", "The Flash", "Harrison Wells"));
        characterList.add(new CharacterListBean("SC8", "The Flash", "Cisco Ramon"));
        //Set Initial fetch index to 0
        setFetchIndex(qc, 0);
        super.executeQueryForCollection(qc, params, noUserParams);
    }

    /**
     * hasNextForCollection - overridden for custom java data source support.
     */
    @Override
    protected boolean hasNextForCollection(Object qc) {
        // Check that there is other records to fetch from ArrayList
        return getFetchIndex(qc) < characterList.size();
    }

    /**In this method new row is created and values are assigned to each attribute of that row
     * createRowFromResultSet - overridden for custom java data source support.
     */
    @Override
    protected ViewRowImpl createRowFromResultSet(Object qc, ResultSet resultSet) {
        // Get the current fetch index
        int curIndex = getFetchIndex(qc);
        // Create a new row
        ProgTestVORowImpl characterRow = (ProgTestVORowImpl) createNewRowForCollection(qc);
        //Here set attribute values in new row using RowImpl class
        characterRow.setId(characterList.get(curIndex).getId());
        characterRow.setSeasonName(characterList.get(curIndex).getSeasonName());
        characterRow.setCharacterName(characterList.get(curIndex).getCharacterName());
        // Change the fetch index as one record is increased in result set
        setFetchIndex(qc, curIndex + 1);
        // Return the newly created row
        return characterRow;
    }

    /**
     * getQueryHitCount - overridden for custom java data source support.
     */
    @Override
    public long getQueryHitCount(ViewRowSetImpl viewRowSet) {
        long value = super.getQueryHitCount(viewRowSet);
        return value;
    }

    /**
     * getCappedQueryHitCount - overridden for custom java data source support.
     */
    @Override
    public long getCappedQueryHitCount(ViewRowSetImpl viewRowSet, Row[] masterRows, long oldCap, long cap) {
        long value = super.getCappedQueryHitCount(viewRowSet, masterRows, oldCap, cap);
        return value;
    }

    /**
     * Method to set the new fetch index
     * @param rowset
     * @param index
     */
    private void setFetchIndex(Object rowset, int index) {
        if (index == characterList.size()) {
            setFetchCompleteForCollection(rowset, true);
        }
        setUserDataForCollection(rowset, new Integer(index));
    }

    /**
     * Method to get the current fetch index
     * @param rowset
     * @return
     *
     */
    private int getFetchIndex(Object rowset) {
        int value = ((Integer) getUserDataForCollection(rowset)).intValue();
        return value;
    }
}

All done :) Now time to check programmatic viewObject so for that Run Application Module


Sample ADF Application-Download
Cheers :) Happy Learning

5 comments :

  1. Thanks for the nice post. it gives great information

    ReplyDelete
  2. Can you please share this application..??

    ReplyDelete
    Replies
    1. Hi Sumit

      Check post I have attached sample application, You can download that

      Ashish

      Delete