Insert Record by Drag & Drop from ADF Tree to ADF Tree Table

If you want to create record based on the values Dragged from ADF Tree and Dropped on a ADF Tree Table, then here you go.

UseCase Description
User Drags a tree node from ADF Tree and Drops it on a ADF Tree Table node. A new row gets added in the Tree Table based on the source tree node, subsequently a record gets added to the database table on which Tree table in based on.

Following description helps to achieve this using ADF BC.

  • Run the
    DragDropSchema.sql
     to create required tables.
  • Create Business Components from tables (PRODUCTS, COMPONENTS, SUB_COMPONENTS, USERS, USER_COMPONENTS) created above.
  • Add custom method to App Module Impl, this method will be used to insert record from view layer.
 
public String createUserComponents(String p_bugdbId, String p_productId, String p_componentId, String p_subComponentId){
    Row newUserComponentsRow = this.getUserComponentsView1().createRow();
    try {
      newUserComponentsRow.setAttribute("Bugdbid", p_bugdbId);
      newUserComponentsRow.setAttribute("ProductId", new oracle.jbo.domain.Number(p_productId));
      newUserComponentsRow.setAttribute("Component1", p_componentId);
      newUserComponentsRow.setAttribute("SubComponent", p_subComponentId);
    } catch (Exception e) {
        e.printStackTrace();
        return "Failure";
    }   
    return "Success";
  }


  • Expose this method to client interface.
  • To display the root node we need a custom VO which can be achieved using below query.


SELECT Users.ACTIVE,
Users.BUGDB_ID,
Users.EMAIL,
Users.FIRSTNAME,
Users.GLOBAL_ID,
Users.LASTNAME,
Users.MANAGER_ID,
Users.MANAGER_PRIVILEGE
FROM USERS Users
WHERE Users.MANAGER_ID is NULL

  • Create VL between UsersView and UsersRootNodeView VOs.
DragDrop_User_RootUser_ViewLink.JPG


  • Drop ProductsView from DC as ADF Tree to jspx page.
  • Add Tree Level Rule based on ComponentsView and SubComponentsView.

DragDrop_Product_TreeBinding.JPG


  • Drop UsersRootNodeView as ADF Tree Table
  • Add Tree Level Rules based on UserComponentsView and UsersView.

DragDrop_RootUser_TreeBinding.JPG


  • Add DragSource to ADF Tree and CollectionDropTarget to ADF Tree Table respectively.

DragDrop_CollectionDropTarget.JPG


  • Bind CollectionDropTarget's DropTarget to backing bean and implement method of signature DnDAction (DropEvent), this method gets invoked when Tree Table encounters a drop action, here details required for creating new record are captured from the drag source and passed to 'createUserComponents' method.

public DnDAction onTreeDrop(DropEvent dropEvent) {
      String newBugdbId = "";
      String msgtxt="";     
      try {
          // Getting the target node bugdb id
          Object serverRowKey = dropEvent.getDropSite();
          if (serverRowKey != null) {   
              //Code for Tree Table as target
              String dropcomponent = dropEvent.getDropComponent().toString();
              dropcomponent = (String)dropcomponent.subSequence(0, dropcomponent.indexOf("["));
              if (dropcomponent.equals("RichTreeTable")){
                RichTreeTable richTreeTable = (RichTreeTable)dropEvent.getDropComponent();
                richTreeTable.setRowKey(serverRowKey);
                int rowIndexTreeTable = richTreeTable.getRowIndex();
                //Drop Target Logic
                if (((JUCtrlHierNodeBinding)richTreeTable.getRowData(rowIndexTreeTable)).getAttributeValue()==null) {
                  //Get Parent
                  newBugdbId = (String)((JUCtrlHierNodeBinding)richTreeTable.getRowData(rowIndexTreeTable)).getParent().getAttributeValue();
                } else {
                  if (isNum(((JUCtrlHierNodeBinding)richTreeTable.getRowData(rowIndexTreeTable)).getAttributeValue().toString())) {
                    //Get Parent's parent         
                    newBugdbId = (String)((JUCtrlHierNodeBinding)richTreeTable.getRowData(rowIndexTreeTable)).getParent().getParent().getAttributeValue();
                  } else{
                      //Dropped on USER                   
                      newBugdbId = (String)((JUCtrlHierNodeBinding)richTreeTable.getRowData(rowIndexTreeTable)).getAttributeValue();
                  } 
                }
              }
          }
         
          DataFlavor<RowKeySet> df = DataFlavor.getDataFlavor(RowKeySet.class);
          RowKeySet droppedValue = dropEvent.getTransferable().getData(df); 
          Object[] keys = droppedValue.toArray();
          Key componentKey = null;
          Key subComponentKey = null;
          // binding for createUserComponents method defined in AppModuleImpl class  to insert record in database.
           
          operationBinding = bindings.getOperationBinding("createUserComponents");
            // get the Product, Component, Subcomponent details and insert to UserComponents table.
          // loop through the keys if more than one comp/subcomponent is select.        
          for (int i = 0; i < keys.length; i++) {   
              System.out.println("in for :"+i);
              List list = (List)keys[i];   
              System.out.println("list "+i+" : "+list);
              System.out.println("list size "+list.size());
              if (list.size() == 1) {             
                  // we cannot drag and drop  the highest node !             
                  msgtxt="You cannot drop Products, please drop Component or SubComponent from the Tree.";
                  System.out.println(msgtxt);             
                  this.showInfoMessage(msgtxt);
              } else {
                  if (list.size() == 2) {
                    // were doing the first branch, in this case all components.
                    componentKey = (Key)list.get(1);
                    Object[] droppedProdCompValues = componentKey.getAttributeValues();
                    operationBinding.getParamsMap().put("p_bugdbId",newBugdbId);
                    operationBinding.getParamsMap().put("p_productId",droppedProdCompValues[0]);
                    operationBinding.getParamsMap().put("p_componentId",droppedProdCompValues[1]);
                    operationBinding.getParamsMap().put("p_subComponentId","ALL");
                    Object result = operationBinding.execute();
              } else {
                    subComponentKey = (Key)list.get(2);
                    Object[] droppedProdCompSubCompValues = subComponentKey.getAttributeValues();
                    operationBinding.getParamsMap().put("p_bugdbId",newBugdbId);
                    operationBinding.getParamsMap().put("p_productId",droppedProdCompSubCompValues[0]);
                    operationBinding.getParamsMap().put("p_componentId",droppedProdCompSubCompValues[1]);
                    operationBinding.getParamsMap().put("p_subComponentId",droppedProdCompSubCompValues[2]);
                    Object result = operationBinding.execute();
                  }   
               }
            }
           
            /* this.getCil1().setDisabled(false);
            this.getCil1().setPartialSubmit(true); */
         
            return DnDAction.MOVE;
        } catch (Exception ex) {
          System.out.println("drop failed with : " + ex.getMessage());
          ex.printStackTrace();       
          /* this.getCil1().setDisabled(true); */
          return DnDAction.NONE; 
        }
    }


  • Run jspx page and drop a Component or Subcomponent from Products Tree to UserComponents Tree Table.


Comments:

Hello,

Is it possible to use drag and drop functionality between trees - where a child from Tree1-Node1 should be moved to Tree 2- Node 2 ?

Thanks,
Jack.

Posted by Jack on December 19, 2011 at 07:42 AM IST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Tips and Tricks from Oracle's JDeveloper & ADF QA

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today