Monday, February 18, 2019

Getting selected value (not index) & display value of select one choice programmatically in ADF

Hello All,
This post is about a common requirement of getting selected value & display value of af:selectOneChoice in the managed bean
normally when we try to get selected value of  selectOneChoice in the managed bean (in ValueChange/ Validator or using binding) it returns the index of that row
so there is a little piece of code using that we can get selected value of choice component


    • I have used Departments table of HR Schema in the sample and created a lov on a transient field


    • Now dropped that ViewObject on the page as a table


    • Created a value change listener in managed bean fro DeptIdTrans but it returns the index of the selected value


    • Then i googled about it and used this method to solve my headache, try to get attributeValue instead of inputValue, see the code written in valueChaneListener in managed bean
/**Value Change Listener of DeptIdTrans (to get selected and display value)
     * @param vce
     */
    public void deptIdVCE(ValueChangeEvent vce) {
        System.out.println("New Value is-" + vce.getNewValue());
        if (vce.getNewValue() != null) {
            this.setvalueToExpression("#{row.bindings.DeptIdTrans.inputValue}",
                                      vce.getNewValue()); //Updating Model Values
            Integer selectedCode =
                Integer.parseInt(this.getValueFrmExpression("#{row.bindings.DeptIdTrans.attributeValue}").toString());

System.out.println("******** Selected Value in List***** " + selectedCode);
System.out.println("*******Display Value in List ****" +
getValueFrmExpression("#{row.bindings.DeptIdTrans.selectedValue.attributeValues[1]}"));

        }
    }


  • There is two methods to set and get value in expression (EL)

 /**Method to set value in Expression (EL)
     * @param el
     * @param val
     */
    public void setvalueToExpression(String el, Object val) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        ELContext elContext = facesContext.getELContext();
        ExpressionFactory expressionFactory = facesContext.getApplication().getExpressionFactory();
        ValueExpression exp = expressionFactory.createValueExpression(elContext, el, Object.class);
        exp.setValue(elContext, val);
    }

    /**Method to get value from Expression (EL)
 * @param data
 * @return
 */
    public String getValueFrmExpression(String data) {
FacesContext fc = FacesContext.getCurrentInstance();
Application app = fc.getApplication();
ExpressionFactory elFactory = app.getExpressionFactory();
ELContext elContext = fc.getELContext();
ValueExpression valueExp = elFactory.createValueExpression(elContext, data, Object.class);
String Message = null;
Object obj = valueExp.getValue(elContext);
        if (obj != null) {
Message = obj.toString();
        }
        return Message;
    }



  • Again run the page and select any value in selectOneChoice (LOV) and see the result
Getting selected value

Tuesday, February 12, 2019

populate data into af:table with createrowsetiterator

        ViewObject vo = CommonCode.getViewObjectByIteratorName("PosCustomerInfoVuVORef1Iterator");
        RowSetIterator iter =  vo.createRowSetIterator(null);
       
        DCBindingContainer bindings = (DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
        DCIteratorBinding itr = bindings.findIteratorBinding("PiCustOfferDtlVO2Iterator");
        ViewObject voc = itr.getViewObject();
       
         while(iter.hasNext()){
             Row rw = iter.next();
             String isSelected = (String)rw.getAttribute("TSelectCust");
             BigDecimal custid = (BigDecimal)rw.getAttribute("CustId");
             String custcode = (String)rw.getAttribute("CustCode");
             if(isSelected!=null && "Y".equals(isSelected)){
                if(custid!=null){
                    Row rw1 = voc.createRow();
                    System.out.println("custid *****"+custid);
                    rw1.setAttribute("CustId", custid);
                    rw1.setAttribute("CustCode", custcode);
                    rw1.setAttribute("CustOfferPromoCode", generatePromoCode());
                    System.out.println("custid ***** CustOfferPromoCode "+ rw1.getAttribute("CustOfferPromoCode"));
                   
                    voc.insertRow(rw1);
                }
             } /* else{
                 CommonCode.errmsg("Please Select Categories", null);
             }  */
         
         }
        iter.closeRowSetIterator();

viewcriteria applying for VO

        ViewObject vo= ReusableMethodsClass.getAM().findViewObject("PosCustomerInfoVuVORef1");
        DCBindingContainer bindings = (DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
        DCIteratorBinding dcItteratorBindings = bindings.findIteratorBinding("PosCustomerInfoVuVORef1Iterator");
        // Get an object representing the table and what may be selected within it
        ViewObjectImpl voi = (ViewObjectImpl)dcItteratorBindings.getViewObject();
        ViewCriteria vc = voi.getViewCriteria("PosCustomerInfoVuVORefCriteriaCustType");
        voi.applyViewCriteria(vc);
        voi.setNamedWhereClauseParam("pcompcust", compcode);
        voi.setNamedWhereClauseParam("pcusttype", "N");
        vo.executeQuery();
        System.out.println("no of rows vo.getFetchedRowCount() "+vo.getFetchedRowCount());