Get value of SelectOneChoice from af Query panel instead of index in ADF

Hi All,

In this post I am going to show you that How you can get the value instead of index from SelectOneChoice component in query panel.

By default when you try to access the value of SelectOneChoice from query panel, it will give you the index instead of actual value. But here will will try to get the actual value.

Suppose you have a view object with name "SearchVO". In this VO we have create one attribute with name "DepartmentId".


Also we have created a read only view object to fetch DepartmentId and DepartmentName from DEPARTMENTS table.


Now we will attach the LOV to DepartmentId attribute in SearchVO.

To do so click on Add icon of the List Of Value option.


Select the DepartmentRO and map the DepartmentId column.


Select Display Attribute as "DepartmentName" and click on "Ok".


Now go to your SearchVO and create a View criteria as shown below and click on "Ok".


You can see your created View Criteria in your view object now.


Now go to Data Control tab and drag and drop the View Criteria that you have created on your jspx/jsff page as query panel.


Now override the queryListener property of query panel and point it to your managed bean.


You manages bean should have the following methods and codes.


package com.kunal.test.view.bean;

import java.util.List;

import javax.el.ELContext;

import javax.el.ExpressionFactory;

import javax.el.MethodExpression;

import javax.faces.context.FacesContext;

import oracle.adf.model.BindingContext;
import oracle.adf.model.binding.DCBindingContainer;
import oracle.adf.view.rich.event.QueryEvent;
import oracle.adf.view.rich.model.AttributeCriterion;
import oracle.adf.view.rich.model.ConjunctionCriterion;
import oracle.adf.view.rich.model.Criterion;
import oracle.adf.view.rich.model.QueryDescriptor;

import oracle.binding.OperationBinding;

public class TestQueryBean {
    public TestQueryBean() {
    }

    public void executeQueryForQuerypanel(QueryEvent queryEvent) {
        int index = 0;
        oracle.jbo.domain.Number  departmentId=null;
        QueryDescriptor qd = queryEvent.getDescriptor();
        ConjunctionCriterion conCrit = qd.getConjunctionCriterion();
        List<Criterion> criterionList = conCrit.getCriterionList();
        for (Criterion criterion : criterionList) {
            if ("DepartmentId".equals(((AttributeCriterion)criterion).getAttribute().getName())) {
                index =
                        ((Integer)((AttributeCriterion)criterion).getValues().get(0));
                                 departmentId =
                                           getDepartmentCode(index, ((AttributeCriterion)criterion).getAttribute().getName());
                               }
            }
        System.out.println("Department Id=============>"+departmentId);
        invokeQueryEventMethodExpression("#{bindings.SearchVOCriteriaQuery.processQuery}",
                                         queryEvent);

    }

    private void invokeQueryEventMethodExpression(String expression,
                                                  QueryEvent queryEvent) {
        FacesContext fctx = FacesContext.getCurrentInstance();
        ELContext elctx = fctx.getELContext();
        ExpressionFactory efactory =
            fctx.getApplication().getExpressionFactory();

        MethodExpression me =
            efactory.createMethodExpression(elctx, expression, Object.class,
                                            new Class[] { QueryEvent.class });
        me.invoke(elctx, new Object[] { queryEvent });

    }
   
    public oracle.jbo.domain.Number getDepartmentCode(int index, String attrName){
        oracle.jbo.domain.Number result = null;
               Object departmentId = null;
                BindingContext bCtx = BindingContext.getCurrent();
                DCBindingContainer dcb = (DCBindingContainer)bCtx.getCurrentBindingsEntry();
                OperationBinding oper = dcb.getOperationBinding("getValueFromIndex");
               oper.getParamsMap().put("index", index);
               oper.getParamsMap().put("attrName", attrName);
               oper.execute();
               departmentId = oper.getResult();

               if (null != departmentId) {
                   result = (oracle.jbo.domain.Number)departmentId;
               }

               return result;
    }
}

Now go to SearchVO and generate VOImpl and RowImpl classes.


Now go to the SearchVOImpl.java and add the following method.


    public oracle.jbo.domain.Number getValueFromIndex(int index, String attrName){
        oracle.jbo.domain.Number selectedCode=null;
        RowSet departmentRO = null;
       SearchVORowImpl searchRow =
                  (SearchVORowImpl)this.createRow();
        if ("DepartmentId".equalsIgnoreCase(attrName)) {
                  departmentRO = searchRow.getDepartmentRO1();
                  Integer i = new Integer(0);
                              while (departmentRO.hasNext()) {
                                  departmentRO.next();
                                  i++;
                                  if (i.equals(index+1)) {
                                      Row row = departmentRO.getCurrentRow();
                                      selectedCode = ( oracle.jbo.domain.Number )row.getAttribute("DepartmentId");
                                      break;
                                  }
                              }
              }
        return selectedCode;
    }

Expose the method created in Client Interface to access then in page definition file.


Once you expose that method in client interface the java section of your view object SearchVO will have basically four classes as shown below.


Now go to page definition of your jspx/jsff file and click on Add icon and select "methodAction" option in General Binding option as shown below.


Now select the method of SearchVOImpl as shown below.


Now run your page and select any Department Name in SelectOneChoice component and click on Search button.


You can see in the Jdeveloper console the selected depatment id value.






Comments

  1. Hi Kunal, This is a great Article! Thank you for sharing. However, I believe that for SearchVOs that are not EO based, you cannot createRow like you mentioned in the article. Instead, we can alternatively access the DepartmentVO directly and Iterate through its rows and indexes!

    -Rudra

    ReplyDelete
  2. Please continue this great work and I look forward to more of your awesome blog posts. Zonnepanelen installateur

    ReplyDelete

Post a Comment

Popular posts from this blog

Setting up the environment for Angular2 and Hello World Example in Angular2

Showing number of rows or row count on top and bottom of table in ADF.

Build a Simple ReactJS application using react-cli