Please disable your adblock and script blockers to view this page

Search this blog

Monday 12 May 2014

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 managed bean
normally when we try to get selected value of  selectOneChoice in managed bean (in ValueChange/ Validator or using binding) it returns 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 sample and created a lov on a transient field


  • Now dropped that ViewObject on page as a table 


  • Created a value change listener in managed bean fro DeptIdTrans but it return index of 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 page and select any value in selectOneChoice (lov) and see the result

Cheers :-) Happy Learning

Tuesday 6 May 2014

ADF Basics : Selecting and showing time-stamp in ADF Faces using af:inputDate & af:convertDateTime

Hello All
This post is about a very common & basic requirement of developers ,
how to show & select time with date in ADF Faces ?
We use af:inputDate to show and select Date & Timestamp type of attributes, by default it looks like this



ADF provides a default converter (af:convertDateTime) to format date & timestamp field, we can define a pattern to change it's format & to show time selector in calendar box



Suppose pattern is- dd-MMM-yyy HH:mm:ss , now see there is a hour/minute/second selector appears in calendar box



you can change this pattern as per your format , suppose you want to show AM or PM after time just use dd-MMM-yyy HH:mm:ss a



see xml source of af:inputDate-


<af:inputDate label="Label 1" id="id1" contentStyle="width:250px;padding:5px;">
                        <af:convertDateTime pattern="dd-MMM-yyy HH:mm:ss a"/>
                    </af:inputDate>

this is how we can change format of Date & Timestamp - Cheers :-)

Friday 2 May 2014

Pre-populate selected values in viewObject based shuttle component (multiple selection)- Oracle ADF

Hello All,
This post talks about a common development requirement
while using shuttle component sometimes we need to show some values on selected side of af:selectManyShuttle , so to do this normally we use a custom shuttle component that is populated from managed bean code
but when i have all data in a viewObject so why should i iterate viewObject to get all data in managed bean and then set some values in an arrayList to populate selected shuttle values.
it is time consuming and requires lot of coding, so for viewObject based shuttle we can do it in efficient way.

  • In this example I'm using Departments table of HR schema to populate af:selectManyShuttle, i think you all know how to create a shuttle in ADF
  • So next is to populate selected shuttle values on some event, created a fusion web application using Departments table

  • It's time to drop Departments ViewObject on page as shuttle component

  • <af:selectManyShuttle value="#{bindings.Departments1.inputValue}" label="#{bindings.Departments1.label}"
                                          id="sms1" size="15" binding="#{pageFlowScope.ShuttleSelectBean.shuttleBind}"
                                          partialTriggers="b1">
                        <f:selectItems value="#{bindings.Departments1.items}" id="si1"/>
                        <f:validator binding="#{bindings.Departments1.validator}"/>
                    </af:selectManyShuttle>
    

  • So suppose on a button click i want to set selected values in shuttle, so i have created a actionEvent for button in managed bean and binding of af:selectManyShuttle, so basic structure of page is like this




  • See the code of actionEvent , create an arrayList and add all values in it that you want to set as selected values and then just set the value in shuttle using binding

  •     /**Method to set selected values in af:selectManyShuttle
         * @param actionEvent
         */
        public void selectValuesShuttle(ActionEvent actionEvent) {
            DCIteratorBinding iter = (DCIteratorBinding) getBindingsCont().get("Departments1Iterator");
            ViewObject vo = iter.getViewObject();
            RowSetIterator rsi = vo.createRowSetIterator(null);
            ArrayList listVal = new ArrayList(20);
    
            while (rsi.hasNext()) {
                Row nextRow = rsi.next();
                if (nextRow.getAttribute("DepartmentId") != null) {
                    listVal.add(nextRow.getAttribute("DepartmentId"));
                }
            }
            shuttleBind.resetValue();// Reset Shuttle Component
            System.out.println("Setting values -" + listVal);
            shuttleBind.setValue(listVal.toArray()); // Setting selected values 
            AdfFacesContext.getCurrentInstance().addPartialTarget(shuttleBind);
        }
    

  • Now run this page and test your shuttle component



  • Cheers- Happy Coding

  • But this method works only when you have to set values on some action or other event after page load, because before page load , binding of shuttle component will not be accessible
  • So if you have a requirement of setting selected values on page load then you must use a list defined in managed bean and bind it to  value property of shuttle component, value property denotes selected values of shuttle 


  • And just write same code on getter of List, in this case selected values in shuttle are populated on page load

  •     List pageLoad = new ArrayList(20);
        public void setPageLoad(List pageLoad) {
            this.pageLoad = pageLoad;
        }
    
        public List getPageLoad() {
            DCIteratorBinding iter = (DCIteratorBinding) getBindingsCont().get("Departments1Iterator");
            ViewObject vo = iter.getViewObject();
            RowSetIterator rsi = vo.createRowSetIterator(null);
            while (rsi.hasNext()) {
                Row nextRow = rsi.next();
                if (nextRow.getAttribute("DepartmentId") != null) {
                    pageLoad.add(nextRow.getAttribute("DepartmentId"));
                }
            }
            return pageLoad;
        }
    

    Cheers- Use both method as per your requirement :-) Download Sample App