Thursday, 24 October 2013

ADF : TreeTable: Read selected Node from ADF TreeTable.

TreeTable: Read selected Node from ADF TreeTable.


I have 2 level of tree structure as shown below
   ObjectMainVO
        |_ ObjectFakeVO
                

My requirement is :
  User can select any row from the above heirarchy
  In my bean class i have to find, what VO user has selected and do some operation.

###################In JSPX page ################################################
1. remove ..makeCurrent from treeTable and call TreeSelectionListner (since we are executing makeCurrent from bean class)
---------------------------------------------------------------------------------
<af:treeTable value="#{bindings.ObjectMain2.treeModel}" var="node"
                      selectionListener="#{backingBeanScope.backing_ObjectMain_ObjectFake_MapView.TreeSelectionListener}"
                      rowSelection="single"
                      binding="#{backingBeanScope.backing_ObjectMain_ObjectFake_MapView.tt1}"
                      id="tt1">
          <f:facet name="nodeStamp">
            <af:column id="c1">
              <af:outputText value="#{node}" id="ot2"/>
            </af:column>
          </f:facet>
          <f:facet name="pathStamp">
            <af:group binding="#{backingBeanScope.backing_ObjectMain_ObjectFake_MapView.g1}"
                      id="g1">
              <af:outputText value="#{node}"
                             binding="#{backingBeanScope.backing_ObjectMain_ObjectFake_MapView.ot1}"
                             id="ot1"/>
            </af:group>
          </f:facet>
        </af:treeTable>

##############################In Bean Class ##################################
1. Execute the makeCurrent for parentVO manually
2. get the selected key (make sure that parent and child VO has unique key)
3. Check selected node is what type of VO in if else condition
------------------------------------------------------------------------------------

import java.util.Iterator;

import java.util.List;

import javax.el.ELContext;

import javax.el.ExpressionFactory;

import javax.el.MethodExpression;

import javax.faces.application.Application;
import javax.faces.context.FacesContext;

import oracle.adf.model.BindingContext;
import oracle.adf.model.binding.DCBindingContainer;
import oracle.adf.model.binding.DCDataControl;
import oracle.adf.model.binding.DCIteratorBinding;
import oracle.adf.view.rich.component.rich.data.RichTreeTable;
import oracle.adf.view.rich.component.rich.input.RichInputText;
import oracle.adf.view.rich.context.AdfFacesContext;

import oracle.adfinternal.view.faces.model.binding.FacesCtrlHierBinding;

import oracle.binding.BindingContainer;

import oracle.jbo.ApplicationModule;
import oracle.jbo.Row;
import oracle.jbo.ViewObject;
import oracle.jbo.uicli.binding.JUCtrlHierBinding;

import oracle.jbo.uicli.binding.JUCtrlHierNodeBinding;

import org.apache.myfaces.trinidad.event.SelectionEvent;
import org.apache.myfaces.trinidad.model.CollectionModel;
import org.apache.myfaces.trinidad.model.RowKeySet;
import org.apache.myfaces.trinidad.model.TreeModel;


    public void TreeSelectionListener(SelectionEvent selectionEvent) {
            System.out.println("Inside onTreeSelectInstanceeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee...//////////////////////////////////////////////////////////////");
           
            //original selection listener set by ADF //#{bindings.allDepartments.treeModel.makeCurrent}
            String adfSelectionListener = "#{bindings.ObjectMain2.treeModel.makeCurrent}";
              System.out.println("adfSelectionListener : "+adfSelectionListener);
            FacesContext fctx = FacesContext.getCurrentInstance();
              System.out.println("fctx : "+fctx);
            Application application = fctx.getApplication();
            ELContext elCtx = fctx.getELContext();
            ExpressionFactory exprFactory = application.getExpressionFactory();
            System.out.println("Inside onTreeSelect...11");
            MethodExpression me = null;
            me = exprFactory.createMethodExpression(elCtx, adfSelectionListener, Object.class, new Class[]
                  { SelectionEvent.class });
            System.out.println("Inside onTreeSelect...22");
            me.invoke(elCtx, new Object[] { selectionEvent });
            System.out.println("Inside onTreeSelect...33");
           
            RichTreeTable tree = (RichTreeTable)selectionEvent.getSource();
              System.out.println("Inside onTreeSelect...33..111");
            TreeModel model = (TreeModel)tree.getValue();
            System.out.println("Inside onTreeSelect...444 model : "+model);
            RowKeySet rowKeySet = selectionEvent.getAddedSet();
            Iterator rksIterator = rowKeySet.iterator();
            System.out.println("Inside onTreeSelect..555.");
           
               
            while (rksIterator.hasNext()) {
              List key = (List)rksIterator.next();
              System.out.println("Inside onTreeSelect...66 : "+key.get(0));
              JUCtrlHierBinding treeBinding = null;
              CollectionModel collectionModel = (CollectionModel)tree.getValue();
              System.out.println("Inside onTreeSelect...collectionModel : "+collectionModel);
              treeBinding = (JUCtrlHierBinding)collectionModel.getWrappedData();
              JUCtrlHierNodeBinding nodeBinding = null;
              nodeBinding = treeBinding.findNodeByKeyPath(key);
              System.out.println("Inside onTreeSelect...nodeBinding : " + nodeBinding);
              //Row rw = nodeBinding.getRow();      
             
              //String rowType = rw.getStructureDef().getDefName();
            
             //************************************
             FacesCtrlHierBinding bind = (FacesCtrlHierBinding)collectionModel.getWrappedData();
             DCIteratorBinding rowiter = bind.getDCIteratorBinding();
              Row r = rowiter.getCurrentRow();
             
                String nodeStuctureDefname = nodeBinding.getHierTypeBinding().getStructureDefName();
                System.out.println("Selected Node Def Name : "+nodeStuctureDefname);
                String objectMainDef = "model.ObjectMain";
                String objectFakeDef = "model.ObjectFake";
               
                BindingContext bctx = BindingContext.getCurrent();
                DCDataControl dc = bctx.findDataControl("AppModuleDataControl");
                ApplicationModule service = (ApplicationModule)dc.getDataProvider();
                System.out.println("service : "+service);
                ApplicationModule am = service.findApplicationModule("AppModule");
                System.out.println("am : "+am);
                if (nodeStuctureDefname.equalsIgnoreCase(objectMainDef)){
                System.out.println("Insideeeeee OBJECT -> OBJECTFFFFMMMMAAAAIIIIINNNNNNN");
                    ViewObject objectMainVO = am.findViewObject("ObjectMain2");
                    System.out.println("objectMainVO : "+objectMainVO);
                    //Row selectedRow = langVO.getCurrentRow();
                    Row selectedRow = objectMainVO.getRow(nodeBinding.getRowKey());
                    System.out.println("selectedRow : "+selectedRow);
                    System.out.println("selectedRow - map Id1 : "+selectedRow.getAttribute("Id1"));
                    System.out.println("selectedRow - obj : "+selectedRow.getAttribute("Obj"));
                    DCBindingContainer bc = (DCBindingContainer)this.getBindings();
                    ViewObject mapViewVO = bc.findIteratorBinding("MapView1Iterator").getViewObject();
                    mapViewVO.setWhereClause(null);
                    mapViewVO.setWhereClause("ID1 = '"+selectedRow.getAttribute("Id1")+"'");
                    mapViewVO.executeQuery();
                    AdfFacesContext.getCurrentInstance().addPartialTarget(this.tt2);
                  
                }
                else if (nodeStuctureDefname.equalsIgnoreCase(objectFakeDef)){
                //work with departments node data
                System.out.println("Insideeeeee OBJECT -> OBJECTFFFFAAAAAKKKKKKKKKKEEEEEEEEEE");
                    ViewObject objectFackeVO = am.findViewObject("ObjectFake2");
                    System.out.println("objectFackeVO : "+objectFackeVO);
                    //Row selectedRow = langVO.getCurrentRow();
                    Row selectedRow = objectFackeVO.getRow(nodeBinding.getRowKey());
                    System.out.println("selectedRow : "+selectedRow);
                    System.out.println("selectedRow - map Id1 : "+selectedRow.getAttribute("Id1"));
                    System.out.println("selectedRow - obj : "+selectedRow.getAttribute("Obj"));
                    DCBindingContainer bc = (DCBindingContainer)this.getBindings();
                    ViewObject mapViewVO = bc.findIteratorBinding("MapView1Iterator").getViewObject();
                    mapViewVO.setWhereClause(null);
                    mapViewVO.setWhereClause("ID1 = '"+selectedRow.getAttribute("Id1")+"'");
                    mapViewVO.executeQuery();
                    AdfFacesContext.getCurrentInstance().addPartialTarget(this.tt2);
                }
            //          else if (nodeStuctureDefname.equalsIgnoreCase(employeesDef)){
            //          System.out.println("Insideeeeee OBJECT -> OBJECT_DETAIL -> OBJECT_ATTRRRSSSSSSSSSS");
            //          }
                else{
                    //what the heck did the user click on? Ask him ;-) }
                    System.out.println("Insideeeeee Nothingggggggggggggggggggg.........");
                }
               
                //*********************************
               
            }
        }

No comments:

Post a Comment