Sunday, December 9, 2012

ADF Multi Select Options (af:selectMany)

ADF Code Corner
https://blogs.oracle.com/jdevotnharvest/entry/how_to_access_selected_rows


http://myadfnotebook.blogspot.com/2010/09/getting-string-value-or-item-code-of.html




Create Master Detail Table
create table xx_users(user_id NUMBER,user_name VARCHAR2(100),
                                  CONSTRAINT xx_users_pk PRIMARY KEY (user_Id));
create table  xx_roles(role_id NUMBER,role_name VARCHAR2(100),
                                   CONSTRAINT xx_roles_pk PRIMARY KEY (role_Id));
Create EO,VO for above 2 tables.



create table xx_user_roles(user_role_id NUMBER,user_id NUMBER,role_name VARCHAR2(100),
                    CONSTRAINT xx_user_roles_pk PRIMARY KEY (user_role_Id),
                    CONSTRAINT xx_user_roles_fk1
                      FOREIGN KEY (user_id)
                      REFERENCES xx_users(user_id));
Create ReadOnly VO based on Above table.

Add All VO instances into AppModule.

From DataControl Add CreateInsert Operation for XxUsersVo1 as Button on Page.
From DataControl XxUsersVo1 as ADF Form.

From Data Control add XxRolesLOVVO1 as ADF Select Many Choice
as
              <af:selectManyChoice value="#{bindings.XxRolesLOVVO1.inputValue}"
                                   label="#{bindings.XxRolesLOVVO1.label}"
                                   id="smc1">
                <f:selectItems value="#{bindings.XxRolesLOVVO1.items}"
                               id="si1"/>
              </af:selectManyChoice>



On Button Pressed

    public void addRoles() {
        BindingContext bctx = BindingContext.getCurrent();
        BindingContainer bindings = bctx.getCurrentBindingsEntry();
        JUCtrlListBinding allRolesList =
                 (JUCtrlListBinding) bindings.get("XxRolesLOVVO1");
          Object[] selVals = allRolesList.getSelectedValues();
          for (int i = 0; i < selVals.length; i++) {
            //Integer val = (Integer)selVals[i];
            String val = (String)selVals[i];
              System.out.println("Int Val "+val);

             // Insert into Table after getting selected Rows or your Code
              OperationBinding crRoleMethod = bindings.getOperationBinding("CreateInsertRoles"); 
              crRoleMethod.execute();
             
              DCBindingContainer bindingsImpl = (DCBindingContainer)bindings;
              DCIteratorBinding iter = bindingsImpl.findIteratorBinding("XxUserRolesVO2Iterator");

              Row row = iter.getCurrentRow();
              // Insert Role Name
              row.setAttribute("RoleName",val );
        
            //...
          }
    }


Use if you want to pull records based on Indices

    public void addRolesUsingIndices() {
        BindingContext bctx = BindingContext.getCurrent();
        BindingContainer bindings = bctx.getCurrentBindingsEntry();
        JUCtrlListBinding allRolesList =
                 (JUCtrlListBinding) bindings.get("XxRolesLOVVO1");
           int[] selVals = allRolesList.getSelectedIndices();
      
          for (int indx : selVals ) {
                  Row rw = allRolesList.getRowAtRangeIndex(indx);
                  Number id  = (Number)rw.getAttribute("RoleId");
                  String val = (String)rw.getAttribute("RoleName");
             
               // Insert into Table after getting selected Rows  or your Code
               OperationBinding crRoleMethod = bindings.getOperationBinding("CreateInsertRoles"); 
              crRoleMethod.execute();
             
              DCBindingContainer bindingsImpl = (DCBindingContainer)bindings;
              DCIteratorBinding iter = bindingsImpl.findIteratorBinding("XxUserRolesVO2Iterator");
   
              Row row = iter.getCurrentRow();
              // Insert Role Name
              row.setAttribute("RoleName",id+" "+val );
        
            //...
          }
    }






If the Choice List is Static eg.
            <af:selectManyCheckbox label="Roles" id="smc1"
                                   binding="#{backingBeanScope.Backing_xxUserCreation.smc1}"
                                   value="#{backingBeanScope.Backing_xxUserCreation.listValue}"
                                   autoSubmit="true">
              <af:selectItem label="Accountant" value="ACCOUNTANT" id="si3"
                             binding="#{backingBeanScope.Backing_xxUserCreation.si3}"/>
              <af:selectItem label="Sales Manager" value="SALES_MANAGER"
                             id="si1"
                             binding="#{backingBeanScope.Backing_xxUserCreation.si1}"/>
              <af:selectItem label="Finance Manager" value="FINANCE_MANAGER"
                             id="si2"
                             binding="#{backingBeanScope.Backing_xxUserCreation.si2}"/>
              <af:selectItem label="Buyer" value="BUYER" id="si4"
                             binding="#{backingBeanScope.Backing_xxUserCreation.si4}"/>
            </af:selectManyCheckbox>



Add below method call on Button Pressed

addRoles(listValue);


Add below code in Managed Bean or Backing Bean


    private Object[] listValue;
   
    public void setListValue(Object[] listvalue){
        this.listValue = listvalue;
    }
   
    public Object[] getListValue(){
        return listValue;
    }
   
    public String getListString(){
        return createString(listValue);
    }
   
    public String createString(Object[] arr){
        if(arr != null){
            String values = "[";
            for(int i=0;i<arr.length;i++) {
                values = values + arr[i];
                if (i <arr.length - 1)
                values = values + ",";
                }
        values = values + "]";
        return values;
       }
        return null;
     }


    public void addRoles(Object[] arr){
       
         BindingContext bindingctx = BindingContext.getCurrent();
         BindingContainer bindings = bindingctx.getCurrentBindingsEntry();
        
        if(arr != null){
            //String values = "[";
            for(int i=0;i<arr.length;i++) {
                //values = values + arr[i];
                System.out.println("Roles to be added "+arr[i]);
                    // Insert Row into USer Roles table
                    OperationBinding crRoleMethod = bindings.getOperationBinding("CreateInsertRoles"); 
                    crRoleMethod.execute();
                   
                   
                    DCBindingContainer bindingsImpl = (DCBindingContainer)bindings;
                    DCIteratorBinding iter = bindingsImpl.findIteratorBinding("XxUserRolesVO2Iterator");
                   
                    Row row = iter.getCurrentRow();
                    // Insert Role Name
                    row.setAttribute("RoleName",arr[i] );
                //if (i <arr.length - 1)
                //values = values + ",";
                }
        //values = values + "]";
       }
     }


 

1 comment:

  1. Nice blog,explanation is good,thank you for sharing this blog.For more details please visit our website..
    Oracle Fusion Cloud HCM Online Training

    ReplyDelete