Friday, June 30, 2017

Getting Ref of the View Object referenced by the current Iterator binding for One iterator page without knowing the name of the iterator

Getting Ref of the View Object referenced by the current Iterator binding for One iterator page without knowing the name of the iterator

In a previous blog (Playing around with ADFdatacontrol),  it was shown that you can get the ApplicationModule object without having to know the name of the Data Control. i mean Instead of this line of code
AppModuleImpl appmodule = (AppModuleImpl)ADFUtils.getApplicationModuleForDataControl("AppModuleDataControl");

you could write the following
        DCBindingContainer binding =   (DCBindingContainer)ADFUtils.getBindingContainer();
        DCDataControl cDataControl = binding.getDataControl();
        ApplicationModule AM =cDataControl.getApplicationModule();


Now, and in order to be more generic in code writing, here is a code that will get you the View Object to which the iterator binding of a page is bound to.
This code will work for simple pages that contain one iterator binding because if there are two iterator bindings within the same page definition, you will need to specify which one you
want

// this piece of code will get all iterator bindings for the page and will check that there is only ONE iterator binding, this implementation is much faster than iterator through the iterator and count it
    
   ArrayList arr = binding.getAllIterBindingList();
        if (arr instanceof Collection) {
            int size = (arr).size();
            System.out.println(size);
            if (size != 1) {
                System.out.println("not single iterator ok");
  //  practically, should through an exception  
              }
          
       else
 {
//  once you are sure there is only one iterator, then you fetch the first iteratorbinding in the list

                DCIteratorBinding ItrBind = (DCIteratorBinding)arr.get(0);

// here you get the viewobject that the iterator references without its name

                ViewObject voData = ItrBind.getViewObject();
 //  just playing around with some method of the view object to test               
                Long range = voData.getEstimatedRowCount();
                System.out.println("Range :" + range.toString());
                System.out.println("VO name " + voData.getDefFullName());
            }
        }
      else {
            System.out.println("not a collection ");
        }


You can now create a method that shall return the ViewObject 

    public ViewObject  getCurrentIteratorVO () {
        DCBindingContainer binding = (DCBindingContainer)ADFUtils.getBindingContainer();
        ArrayList arr = binding.getAllIterBindingList();
        if (arr instanceof Collection) {
            int size = (arr).size();
            System.out.println(size);
            if (size != 1) {
                System.out.println("not single iterator ");
            // practically, should through an exception
            }
            else {
               DCIteratorBinding ItrBind = (DCIteratorBinding)arr.get(0);
               ViewObject voData = ItrBind.getViewObject();
              return voData;
                 }
           } 
         else 
          {
            System.out.println("not a collection ");
         }
                return null;
    }

Call the method
    public String cb6_action() {
        // Add event code here...
        ViewObject vv = getCurrentIteratorVO();
        return null;
    }

The output is

1
Range :14


VO name model.EmpView

No comments:

Getting Ref of the View Object referenced by the current Iterator binding for One iterator page without knowing the name of the iterator

Getting Ref of the View Object referenced by the current Iterator binding for One iterator page without knowing the name of the iterator ...