Please disable your adblock and script blockers to view this page

Search this blog

Saturday 18 June 2016

Add and delete records (Parent/Child) in POJO Based ADF TreeTable


Hello All

Previously I have posted about creating POJO based ADF TreeTable , Get Selected Record from POJO based TreeTable and Traverse POJO based TreeTable

Recently a developer asked me about adding and deleting records in POJO based TreeTable so here goes a new blog post

To add records in af:treeTable first we have to check that if user has selected any parent node then new record will be added under it and if there is no selection then new record will be added as parent node, This is the core concept of this post and all these posts uses same sample application and same 2-Level Tree



Added an inputText to enter new record, a button to add this record to TreeTable and a link in treeTable column to delete record
af:treeTable XML Source-

<af:treeTable rowBandingInterval="0" id="tt1"
                                          value="#{viewScope.ProgrammaticTreeTabBean.charatcerVal}" var="node"
                                          rowSelection="multiple" initiallyExpanded="true" width="300px"
                                          binding="#{viewScope.ProgrammaticTreeTabBean.treeTableBind}">
                                <f:facet name="nodeStamp">
                                    <af:column headerText="Node Stamp" id="c1" width="250">
                                        <af:outputText value="#{node.name}" id="ot1" inlineStyle="font-weight:bold;"/>
                                    </af:column>
                                </f:facet>
                                <af:column id="c2" width="30" align="center">
                                    <af:link id="l1" icon="#{resource['images:delete1.png']}"
                                             actionListener="#{viewScope.ProgrammaticTreeTabBean.deleteSelectedRecordAction}"/>
                                </af:column>
                            </af:treeTable>

and page looks like this-

then created component binding of input text in managed bean to get it's value ,

    //Component Binding on af:inputText
    private ComponentReference inputValBind;

    public void setInputValBind(RichInputText inputValBind) {
        this.inputValBind = ComponentReference.newUIComponentReference(inputValBind);
    }

    public RichInputText getInputValBind() {
        if (inputValBind != null) {
            return (RichInputText) inputValBind.getComponent();
        }
        return null;
    }

Code to Add Records in POJO based TreeTable-


    /**Method to add records (Parent/Child) in treeTable
     * @param actionEvent
     */
    public void addRecordsTreeTableAction(ActionEvent actionEvent) {
        //Check that if there is a value in input text
        if (getInputValBind().getValue() != null) {
            //Get Collection Model from treeTable binding
            CollectionModel treeModel = null;
            treeModel = (CollectionModel) this.getTreeTableBind().getValue();
            //Get selected row keys from treeTable
            RowKeySet selectedChildKeys = getTreeTableBind().getSelectedRowKeys();
            //If a record is selected
            if (!selectedChildKeys.isEmpty()) {
                List<Seasons> seasonList = (List<Seasons>) treeModel.getWrappedData();
                //Create iterator from RowKeySet
                Iterator selectedCharIter = selectedChildKeys.iterator();
                //Iterate over RowKeySet to get all selected childs of treeTable
                while (selectedCharIter.hasNext()) {
                    List val = (List) selectedCharIter.next();
                    //Get Seasons (Parent) List of selected row
                    Seasons s = seasonList.get(Integer.parseInt(val.get(0).toString()));
                    //Get charaters list of selected seasons
                    List<Seasons> characterList = s.getCharacters();
                    //If selected record is parent (Character) then add new record as it's child
                    if (val.size() == 1) {
                        Seasons character = new Seasons(getInputValBind().getValue().toString());
                        s.addCharaters(character);
                    }
                }
            }
            //If no record is selected then new record will added as parent
            else {
                Seasons season = new Seasons(getInputValBind().getValue().toString());
                seasonList.add(season);
            }
            //Refresh af:treeTable after adding records
            AdfFacesContext.getCurrentInstance().addPartialTarget(treeTableBind.getComponent());
        }
    }


Code to Delete selected records from TreeTable-


    /**Method to delete selected node from af:treeTable
     * @param actionEvent
     */
    public void deleteSelectedRecordAction(ActionEvent actionEvent) {
        //Get Collection Model from treeTable binding
        CollectionModel treeModel = null;
        treeModel = (CollectionModel) this.getTreeTableBind().getValue();
        //Get selected row keys from treeTable
        RowKeySet selectedChildKeys = getTreeTableBind().getSelectedRowKeys();

        if (!selectedChildKeys.isEmpty()) {
            List<Seasons> seasonList = (List<Seasons>) treeModel.getWrappedData();
            //Create iterator from RowKeySet
            Iterator selectedCharIter = selectedChildKeys.iterator();
            //Iterate over RowKeySet to get all selected childs of treeTable
            while (selectedCharIter.hasNext()) {
                List val = (List) selectedCharIter.next();
                //Get Seasons (Parent) List of selected row
                Seasons s = seasonList.get(Integer.parseInt(val.get(0).toString()));
                //Get charaters list of selected seasons
                List<Seasons> characterList = s.getCharacters();
                //If selected record is child (Character)

                if (val.size() > 1) {
                    Seasons character = characterList.get(Integer.parseInt(val.get(1).toString()));
                    //Remove character name from Season List
                    s.removeCharaters(character);
                } else {
                    //Remove entire Season from the list
                    seasonList.remove(s);
                }
            }
        }
    }

All Done :) Now run and check application


Sample ADF Application-Download
Cheers :) Happy Learning

7 comments :

  1. Thank you so much i don't know really how to thank you this helped soooooooo mucch

    ReplyDelete
  2. one question what different between values coming from list and values coming from view object ?

    ReplyDelete
    Replies
    1. Salah

      In ViewObject based treeTable we use ADF binding layer and it's methods but POJO based treeTable is populated from managed bean directly so there is a difference in way of performing operation , getting selected records etc

      Ashish

      Delete
    2. Ok thanks that what i was need to know how to make this example in ViewObject Based TreeTable thanks

      Delete
    3. For this you need two DB tables
      1. Seasons (SEASON_ID, SEASON_NAME)
      2. Charaters (SEASON_ID,CHARACTER_ID,CHARACTER_NAME)

      then you can create viewObject using these tables, create a viewLink between VOs using SEASON_ID and then drop on page as treeTable
      For reference check it- ViewObject based Tree Table Component in Oracle ADF

      Ashish

      Delete
  3. Hi Ashish,

    We have a specific requirement in which user wants to buy single Oracle EBS(R12) license and wants to build a custom login page using ADF so as to authenticate multiple users on top of single license and capture the login info. Do you have any idea about how to implement this particular req.

    Thanks in advance,
    Mohit

    ReplyDelete
    Replies
    1. Hi Mohit

      I am not sure about this license thing but you can post your question in OTN Forum
      There you'll get an answer

      Ashish

      Delete