Thursday, March 22, 2018

ADF - Customizing the Standard Search Functionality


In OAF - We have a bean called : QueryBean - which performs Automatic Search Functionality.
There are use cases where we do customization to the Standard Query bean.

Similarly in ADF too, there is search bean and also it provides custom logic to be implemented.

The Same is explained in this tutorial in detailed manner @

http://www.oracle.com/technetwork/developer-tools/adf/learnmore/30-table-filter-queries-169172.pdf









Java Logic Used is mentioned below : EmployeeQueryPG.java

package view.backing;

import java.util.Map;

import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.el.MethodExpression;

import javax.faces.context.FacesContext;

import oracle.adf.view.rich.component.rich.RichDocument;
import oracle.adf.view.rich.component.rich.RichForm;
import oracle.adf.view.rich.component.rich.data.RichTable;
import oracle.adf.view.rich.component.rich.input.RichInputDate;
import oracle.adf.view.rich.component.rich.output.RichMessages;
import oracle.adf.view.rich.event.QueryEvent;
import oracle.adf.view.rich.model.FilterableQueryDescriptor;

public class EmployeeQueryPG {
    private RichForm f1;
    private RichDocument d1;
    private RichMessages m1;
    private RichTable t1;
    private RichInputDate id1;

    public void setF1(RichForm f1) {
        this.f1 = f1;
    }

    public RichForm getF1() {
        return f1;
    }

    public void setD1(RichDocument d1) {
        this.d1 = d1;
    }

    public RichDocument getD1() {
        return d1;
    }

    public void setM1(RichMessages m1) {
        this.m1 = m1;
    }

    public RichMessages getM1() {
        return m1;
    }

    public void setT1(RichTable t1) {
        this.t1 = t1;
    }

    public RichTable getT1() {
        return t1;
    }

    public void setId1(RichInputDate id1) {
        this.id1 = id1;
    }

    public RichInputDate getId1() {
        return id1;
    }

    public void onQuery(QueryEvent queryEvent) {
        // Add event code here...
        System.out.println("user is searching");
       
        // pre-processing code here

         boolean invokeQuery = true;
         /*
         * Method called by the Query Listener. This method checks if
         * the DepartmentId parameter contains a valid number and puts
         * the DepartmentName into the expected case
         */
         FilterableQueryDescriptor fqd =
         (FilterableQueryDescriptor)
         queryEvent.getDescriptor();
         Map map = fqd.getFilterCriteria();
       
        // ensure DepartmentId contains a Number
         String EmployeeId = (String) map.get("EmployeeId");
       
         System.out.println("entered EmployeeId-->" + EmployeeId);
       
        if (EmployeeId != null && EmployeeId.length()>0){
           
            try {
              // try to parse String to integer
               Long.parseLong(EmployeeId);
               
            } catch (Exception ex) {
                // not a string
                System.out.println("Not a string");
                // add some error message here
                // unset selection
                map.remove("DepartmentId");
                invokeQuery = false;
            }
        }
       
        // ensure the initial character is in uppercase
        String departmentName = (String) map.get("FirstName");
        if (departmentName != null && departmentName.length()>0){
            StringBuffer sbuf = new StringBuffer();
            sbuf.append(departmentName.substring(0,1).toUpperCase());
            sbuf.append(departmentName.substring(1).toLowerCase());
            map.put("FirstName",sbuf.toString());           
        }
                       
        if (invokeQuery) {
            invokeMethodExpression("#{bindings.EmployeeEOVO1Query.processQuery}",
                                                   Object.class,QueryEvent.class,
                                                   queryEvent);
        }
       
       
       
    }
   
    public Object invokeMethodExpression(String expr, Class returnType, Class[] argTypes,Object[] args){
            FacesContext fc = FacesContext.getCurrentInstance();     
            ELContext elctx  = fc.getELContext();
            ExpressionFactory elFactory = fc.getApplication().getExpressionFactory();     
            MethodExpression methodExpr = elFactory.createMethodExpression(elctx,expr,returnType,argTypes);     
            return methodExpr.invoke(elctx,args);     
        }
   
   
    public Object invokeMethodExpression(String expr, Class returnType,Class argType, Object argument){   
        return invokeMethodExpression(expr, returnType,new Class[]{argType}, new Object[]{argument});
    }
   

}




Wednesday, March 21, 2018

ADF - Get Selected Rows - Single/Multi Selection


//Single Selection

    public String singleSelection_action() {
        // Add event code here...
       
        System.out.println("User Clicked on Single Button");
       
        DCBindingContainer bindings =
                    (DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
                DCIteratorBinding dcItteratorBindings =
                    bindings.findIteratorBinding("EmployeeEOVO1Iterator");
               
        // Get an object representing the table and what may be selected within it
                ViewObject voTableData = dcItteratorBindings.getViewObject();
       
                // Get selected row
                Row rowSelected = voTableData.getCurrentRow();
       
                // Display attribute of row in console output - would generally be bound to a UI component like a Label and or used to call another proces
                System.out.println(rowSelected.getAttribute("EmployeeId"));
               
               
               
        return null;
    }


//Multi Selection

    public String multipleSelection_action() {
        // Add event code here...
        System.out.println("User Clicked on Multiple Selection ");


        // RowKeySet Object can hold the selected rows from a user as follows
                RowKeySet rksSelectedRows =
                    this.getT1().getSelectedRowKeys();

                // Iterator object provides the ability to use hasNext(), next() and remove() against the selected rows
                Iterator itrSelectedRows = rksSelectedRows.iterator();

                // Get the data control that is bound to the table - e.g. OpenSupportItemsIterator
                DCBindingContainer bindings =
                    (DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
                DCIteratorBinding dcIteratorBindings =
                    bindings.findIteratorBinding("EmployeeEOVO1Iterator");

                // Information from binding that is specific to the rows
                RowSetIterator rsiSelectedRows =
                    dcIteratorBindings.getRowSetIterator();

                // Loop through selected rows
                while (itrSelectedRows.hasNext()) {

                    // Get key for selected row
                    Key key = (Key)((List)itrSelectedRows.next()).get(0);

                    // Use the key to get the data from the above binding that is related to the row
                    Row myRow = rsiSelectedRows.getRow(key);

                    // Display attribute of row in console output - would generally be bound to a UI component like a Label and or used to call another proces
                    System.out.println(myRow.getAttribute("EmployeeId"));
                }
               
               
               
               
        return null;
    }

Thursday, March 01, 2018

ADF - Sample Java Code To Iterate VO Data



Following is the code snippet to extract the data from VO.

Below code is very similar to the logic which we write in OAF (Oracle Application Framework)

public class AppModuleAMTest {
   
     public static void main(String args[])
     {
       //bc4jclient --press enter 
         String amDef = "oracle.fod.storefront.model.module.AppModuleAM";
        String config = "AppModuleAMLocal";
        ApplicationModule am =
            Configuration.createRootApplicationModule(amDef, config);
        ViewObject vo = am.findViewObject("Employees1");
        vo.executeQuery();
        while(vo.hasNext())
        { 
             Row empRow = vo.next();
             System.out.println("Employee ID -->"+ empRow.getAttribute(1));
           
            }
        // Work with your appmodule and view object here
        Configuration.releaseRootApplicationModule(am, true);
         }
}