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

Sharing is Caring

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 that how to add and delete 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 the page looks like this-

Add and delete records

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

Related Posts

An Oracle ACE, Blogger, Reviewer, Technical Lead working on Oracle ADF

0 thoughts on “Add and delete records (Parent/Child) in POJO Based ADF TreeTable”

  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

  2. 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

Leave a Reply

Your email address will not be published. Required fields are marked *