Monday, February 27, 2017

Intercept ADF Table filters through region controllers

Use case : Intercepting ADF table filters through query listener is a regular approach. In this post i will show you how to do it through region controllers..

Step 1: Drag and drop departments table on to the page with filter mode available.Create binding for the table to a managed bean

<af:table value="#{bindings.DepartmentsEOView1.collectionModel}" var="row"
              rows="#{bindings.DepartmentsEOView1.rangeSize}"
              emptyText="#{bindings.DepartmentsEOView1.viewable ? 'No data to display.' : 'Access Denied.'}"
              fetchSize="#{bindings.DepartmentsEOView1.rangeSize}"
              rowBandingInterval="0"
              filterModel="#{bindings.DepartmentsEOView1Query.queryDescriptor}"
              queryListener="#{bindings.DepartmentsEOView1Query.processQuery}"
              filterVisible="true" varStatus="vs"
              selectedRowKeys="#{bindings.DepartmentsEOView1.collectionModel.selectedRow}"
              selectionListener="#{bindings.DepartmentsEOView1.collectionModel.makeCurrent}"
              rowSelection="single" id="t1" styleClass="AFStretchWidth"
              binding="#{pageFlowScope.view3Bean.richTab}">

Step 2 : Go to page definition file and add a controller class


<?xml version="1.0" encoding="UTF-8" ?>
<pageDefinition xmlns="http://xmlns.oracle.com/adfm/uimodel"
                ControllerClass="ui.bean.PageFragementController"
                version="11.1.1.64.93" id="view3PageDef" Package="uic">

PageFragementController is a java class that should implement RegionController . 


Step 3 : PageFragementController  implementation

public class PageFragementController implements RegionController {
     
    // This method will be called during page load everytime
    public boolean refreshRegion(RegionContext regionContext) {
       
        int refreshFlag = regionContext.getRefreshFlag();         
        FacesContext fctx = FacesContext.getCurrentInstance();
        Map requestMap = fctx.getExternalContext().getRequestMap();         
        Map pageFlowScopeMap = ADFContext.getCurrent().getPageFlowScope();
        
   // Call the method only during life cycle render model
        if (pageFlowScopeMap != null) {
            if (regionContext.getRefreshFlag() == 2) {

//we have already created a binding for the table to a managed bean which is in pageFlowScope .Access the bean from pageflowscope               


                view3Bean testBan =   (view3Bean)pageFlowScopeMap.get("view3Bean");                

                RichTable rtable = testBan.getRichTab();
                FilterableQueryDescriptor filters = FilterableQueryDescriptor)rtable.getFilterModel();
                Map<String, Object> map = filters.getFilterCriteria();

              if (map != null) {

                String str  = null;
                    for (String key : map.keySet()) {
                        if (map.get(key) != null) {
                            str = (String)map.get(key);
                 }

//Intercept the user filtered column and add the required value. Here user would search for any department ,but i would always want to make it search for Executive only.If the user does empty search then it will work in ordinary way

                                             
               if(str!=null && !str.equals("")){
                            if( key.equals("DepartmentName")){
                                map.put(key, "Executive");
                            }
                        }
                        else{
                            map.put(key, "");
                        }
                    }
                }

//create query Event object with the intercepted filter values. filters is the object which we intercepted through its filter map values.we modified the filters object


 QueryEvent queryEvent =  new QueryEvent(testBan.getRichTab(), filters);


//Invoke the search operation with the below code

  
invokeMethod("#{bindings.DepartmentsEOView1Query.processQuery}",  QueryEvent.class, queryEvent);

            }

        }

        regionContext.getRegionBinding().refresh(refreshFlag);

        return false;
    }


   public static Object invokeMethod(String expr, Class paramType,
                                      Object param) {
        return invokeMethod(expr, new Class[] { paramType },
                            new Object[] { param });
    }

    public static Object invokeMethod(String expr, Class[] paramTypes,

                                      Object[] params) {
        FacesContext fc = FacesContext.getCurrentInstance();
        ELContext elc = fc.getELContext();
        ExpressionFactory ef = fc.getApplication().getExpressionFactory();
        MethodExpression me =
            ef.createMethodExpression(elc, expr, Object.class, paramTypes);
        return me.invoke(elc, params);

    }



No comments:

Post a Comment