Please disable your adblock and script blockers to view this page

Search this blog

Showing posts with label Multiple Selection. Show all posts
Showing posts with label Multiple Selection. Show all posts

Saturday 3 June 2017

oracle.jbo.domain.DataCreationException: JBO-25009 while using multiple selection component in ADF Faces


Previously I have posted about using multi-selection components (af:selectManyCheckbox, af:selectManyChoice, af:selectManyListbox, af:selectManyShuttle) of ADF Faces. These components make use of list binding and work on base attribute and display attribute concept

Thursday 16 July 2015

Get selected row (single/multiple) from POJO based table in ADF


Previously i have posted about populating af:table from managed bean using POJO and adding records in it, Check this-
Populate af:table programmatically from managead bean using POJO 
In this post i am extending previous post application
This post is about getting selected row from POJO based table , It is a common requirement while using af:table based on POJO

Get single row using custom selection listener-


SelectionListener  handles selection event of table , whenever user selects a row in table ,selection listener is fired
Set table RowSelection property to single (to select one row at a time) or multiple (to select multiple row ) and create a custom selection listener in managed bean that will handle table's selection event




See selection listener code in managed bean we can get selected row  using this -



import oracle.adf.view.rich.component.rich.data.RichTable;
import org.apache.myfaces.trinidad.event.SelectionEvent;

 /**Method to get selected row(single)
     * @param selectionEvent
     */
    public void tableSelection(SelectionEvent selectionEvent) {
        //Get table from selectionEvent
        RichTable richTable = (RichTable) selectionEvent.getSource();
        //Cast to the List that populates table
        PersonBean row = (PersonBean) richTable.getSelectedRowData();
        //Get the attributes (column) from list
        System.out.println(row.getName());
    }

Now check this -

 Output on console :)

Get single/multiple selected row on a button click (ActionEvent)-


Set table RowSelection to multiple and select multiple row using Ctrl key of your keyboard
and check this code to get multiple selected row using RowKeySet, We can get all row using getSelectedRowKeys method


import org.apache.myfaces.trinidad.model.RowKeySet;
import java.util.Iterator;    



/**Method to get all selected record in af:table
     * @param actionEvent
     */
    public void getSelectedRecord(ActionEvent actionEvent) {
        //getTableBind is binding of table on page.
        RowKeySet selectedEmps = getTableBind().getSelectedRowKeys();
        //Create iterator from RowKeySet
        Iterator selectedEmpIter = selectedEmps.iterator();

        while (selectedEmpIter.hasNext()) {
            String i = selectedEmpIter.next().toString();
            //personList is the list used to populate table and name is a column of table
            //So here filter list using index and get values then
            PersonBean rowIter = personList.get(Integer.parseInt(i));
            System.out.println(rowIter.getName());
        }
       
    }

Now run and check again -

 Output on console :)

Sample ADF Application- Download
Cheers :) Happy Learning

Friday 5 June 2015

Programmatically Select all values in ADF BC based selectMany (af:selectManyCheckbox, af:selectManyChoice, af:selectManyListbox, af:selectManyShuttle) component


Hello All,
Previously i have posted a lot about component that supports multiple selection in ADF Faces (af:selectManyCheckbox, af:selectManyChoice, af:selectManyListbox, af:selectManyShuttle)
- Multiple Selection in ADF Faces

This post is about selecting all values in a component programmatically on a event like button click, value change event etc.
Note that this post is designed for ADF BC (viewObject) based components , to set values in bean based component check this-
Programmatically populate values in ADF Faces multiSelect component (af:selectManyCheckbox, af:selectManyChoice, af:selectManyListbox, af:selectManyShuttle)



So for this i have just dropped Departments viewObject as multiSelect component on page
(af:selectManyCheckbox, af:selectManyChoice, af:selectManyListbox, af:selectManyShuttle)


 Page bindings section looks like this-


Now see code of Set all values as selected button-


    /**Method to get BindingContainer of current viewPort (page)
     * @return
     */
    public BindingContainer getBindings() {
        return BindingContext.getCurrent().getCurrentBindingsEntry();
    }



    /**Method to set all values as selected in SelectMany Components.
     * @param actionEvent
     */
    public void setAllValuesToSelected(ActionEvent actionEvent) {

        int arrIndex[];
        //Get the iterator binding of component
        DCIteratorBinding deptIter = (DCIteratorBinding) getBindings().get("DepartmentsView1Iterator");
        //Get viewObject from Iterator
        ViewObject deptVo = deptIter.getViewObject();
        //Get component list binding , component is directly based on this
        JUCtrlListBinding list = (JUCtrlListBinding) getBindings().get("DepartmentsView1");
        DCIteratorBinding iterList = list.getDCIteratorBinding();
        RowSetIterator rsi = deptVo.createRowSetIterator(null);
        int i = 0;
        int rowCount = (int) deptVo.getEstimatedRowCount();
        //Initialize array and set it's size (equal to number of rows in List)
        arrIndex = new int[rowCount];
        while (rsi.hasNext()) {
            //Get viewObject next row from RowSetIterator
            Row nextRow = rsi.next();
            //Set this row as page iterator's current row
            iterList.setCurrentRowWithKey(nextRow.getKey().toStringFormat(true));
            //Now get index of this row
            int indx = iterList.getCurrentRowIndexInRange();
            //Add it to array
            arrIndex[i] = indx;
            i++;
        }
        rsi.closeRowSetIterator();
        // Set as selected indices
        list.setSelectedIndices(arrIndex);
    }


All done, now run and check application , click on button and see what happens ?


Great, it's working :)
I have explained one more approach to set values in multi select component that makes use of component binding
check it - Set values in af:selectManyChoice programmatically - Oracle ADF

Sample ADF Application-Download
Cheers :) Happy Learning

Wednesday 7 January 2015

Programmatically populate values in ADF Faces multiSelect component (af:selectManyCheckbox, af:selectManyChoice, af:selectManyListbox, af:selectManyShuttle)

ADF Faces provides some component to support multiple selection as af:selectManyListbox, af:selectManyCheckbox, af:selectManyChoice, af:selectManyShuttle
In this post we will see how to populate values in these component from managed bean using POJO
You can read my previous post on multiple selection that was about populating multiselect component using ADF BC and binding layer
Using Multiple Selection (selectManyListbox & selectManyCheckbox component) in ADF
Shuttle Component in Oracle ADF (Allow Multiple Selection)

See the step by step implementation-



  • Create a Fusion Web Application and a managed bean in viewController


  • Create a variable of type java.util.List in managed bean, and generate its accessors

  •     //List to show records in selectMany components
        List<SelectItem> allValuesList;
    
        public void setAllValuesList(List<SelectItem> allValuesList) {
            this.allValuesList = allValuesList;
        }
    
        public List<SelectItem> getAllValuesList() {
            return allValuesList;
        }
    

  • This list contains value of type javax.faces.model.SelectItem; that is supported by af:selectManyCheckbox, af:selectManyChoice, af:selectManyListbox compoent

  •     //List to show records in selectMany components
        List<SelectItem> allValuesList;
    
        public void setAllValuesList(List<SelectItem> allValuesList) {
            this.allValuesList = allValuesList;
        }
    
        public List<SelectItem> getAllValuesList() {
            if (allValuesList == null) {
                allValuesList = new ArrayList<SelectItem>();
                allValuesList.add(new SelectItem(1, "India"));
                allValuesList.add(new SelectItem(2, "Australia"));
                allValuesList.add(new SelectItem(3, "America"));
                allValuesList.add(new SelectItem(4, "United Kingdom"));
            }
            return allValuesList;
        }
    

  • See how to add this list reference to  af:selectManyCheckbox component, just drag n drop component on page


  • Set managed bean list reference to selectManyCheckbox component, records stored in this list will be populated in component


  • Suppose I have to show some record as selected by default on page load so for this requirement created another List in managed bean and added it to value property of selectManyCheckbox component
    Value property of component refers only selected records but selectItems component refers all records of component

  •     //List to show selected values in selectMany Component
        List selectedValues;
    
        public void setSelectedValues(List selectedValues) {
            this.selectedValues = selectedValues;
        }
    
        public List getSelectedValues() {
            if (selectedValues == null) {
                selectedValues = new ArrayList();
                selectedValues.add(1);
                selectedValues.add(3);
                System.out.println("List is-" + selectedValues);
            }
            return selectedValues;
        }
    

    See af:selectManyCheckbox source -

    <af:selectManyCheckbox id="smc1" value="#{viewScope.ProgSelectManyComp.selectedValues}">
                                        <f:selectItems value="#{viewScope.ProgSelectManyComp.allValuesList}" id="si1"/>
                                    </af:selectManyCheckbox>
    

  • Now run this application and see component on page


  • Following same steps i have three more component on page , all 3 adf faces component support multiple selection
    af:selectManyChoice-

    <af:selectManyChoice label="Label 1" id="smc2"
                                                         value="#{viewScope.ProgSelectManyComp.selectedValues}"
                                                         simple="true">
                                        <f:selectItems value="#{viewScope.ProgSelectManyComp.allValuesList}" id="si2"/>
                                    </af:selectManyChoice>
    


    af:selectManyListbox-

    <af:selectManyListbox label="Label 1" id="sml1"
                                                          value="#{viewScope.ProgSelectManyComp.selectedValues}"
                                                          simple="true">
                                        <f:selectItems value="#{viewScope.ProgSelectManyComp.allValuesList}" id="si3"/>
                                    </af:selectManyListbox>
    


    af:selectManyShuttle-

     <af:selectManyShuttle label="Label 1" id="sos1" simple="true"
                                                           value="#{viewScope.ProgSelectManyComp.selectedValues}"
                                                           contentStyle="width:50px;">
                                        <f:selectItems value="#{viewScope.ProgSelectManyComp.allValuesList}" id="si4"/>
                                    </af:selectManyShuttle>
    

  • All 4 components on page are populated using same List, this is just an example to show that all 4 components share same strucutre, run this application and see 


  • Thanks, Happy Learning :)
    Download- Sample ADF Application

Monday 13 October 2014

Set values in af:selectManyChoice programmatically - Oracle ADF

This post is about a very common question
How to set selected values in af:selectManyChoice component ?
Sometimes we need to set some values in selectManyChoice component on some action

In this post i am using Departments table of HR Schema to create selectManyChoice (Multiple Selection)
Just drag and drop Departments viewObject as ADF Select Many Choice
see- Using Multiple Selection (selectManyListbox & selectManyCheckbox component) in ADF


(Jdev Version- 12.1.3)
dropped a button on page and on this button action ,  setting values in component
See this simple managed bean code -




import java.util.ArrayList;

import javax.faces.event.ActionEvent;

import oracle.adf.view.rich.component.rich.input.RichSelectManyChoice;
import oracle.adf.view.rich.context.AdfFacesContext;

public class SetValueSmcBean {
    private RichSelectManyChoice selectMcBind;

    public SetValueSmcBean() {
    }

    /**Methos to set selected values in SelectManyChoice
     * @param actionEvent
     */
    public void setSelectedValuesAction(ActionEvent actionEvent) {
        ArrayList listVal = new ArrayList(20);
        //Add DepartmentId to list that you want to set as selected
        listVal.add(101);
        listVal.add(102);
        // Set this List as value using component binding
        selectMcBind.setValue(listVal.toArray());
        //Refresh Component on page (partial target)
        AdfFacesContext.getCurrentInstance().addPartialTarget(selectMcBind);
    }

    public void setSelectMcBind(RichSelectManyChoice selectMcBind) {
        this.selectMcBind = selectMcBind;
    }

    public RichSelectManyChoice getSelectMcBind() {
        return selectMcBind;
    }
}

run application and check-

Downoad Sample Application
Thanks , Happy Learning :)

Monday 15 September 2014

Showing all values instead of 'All' text in selectManyChoice using JavaScript -Oracle ADF

Happy Engineer's Day :)
This post is about overriding default text for select all feature in af:selectManyChoice
af:selectManyChoice supports multiple selection in ADF Faces and framework provides a default feature to select all values of list but after selecting all value it doesn't show values ,only show a String 'All'.
Like this-
but sometimes it is not clear that what are the values by just seeing this 'All' text or it is a requirement to show all values (if there is not much data) instead of this default text



So in this post i am going to override this default text using JavaScript and this is a quick overview that how we can play with JavaScript and try it in ADF Faces
see previous posts on JavaScript in Oracle ADF-

Show message (Invoke FacesMessage) using JavaScript in ADF Faces
Launching browser print dialog using simple javascript function in ADF
Using JQuery in Oracle ADF

So in this implementation i am using Departments table (HR Schema -Oracle) and Jdeveloper 12C (12.1.3)
  • Prepare model using Departments table and drop viewObject on page as af:selectManyChoice


  • Now  see when we select one -two values , it appears on component but in case of all only that 'All' string appears


  • Here i am changing this text on valueChange event of selectManyChoice , so created a valueChangeListener in managed bean. 

  • See what i am going to do in this listener is -
    1. Get all selected values means DepartmentId
    2. Next is to get corresponding DepartmentName for DepartmentId 
    3. Add all DepartmentName into an String
    4. Call JavaScript method to set this String as value of selectManyChoice instead of 'All'

  • See code written in valueChangeListener , i have used enough comments to understand each line

  •     /**ValueChangeListener for SelectManyChoice (Executes Javascript to replace 'All' text)
         * @param vce
         */
        public void selectManyVCE(ValueChangeEvent vce) {
            //String to store all selected Departments Name
            String displayVal = "";
            //Get BindingContainer of current page
            BindingContext bctx = BindingContext.getCurrent();
            BindingContainer bindings = bctx.getCurrentBindingsEntry();
            //Get Iterator of SelectManyChoice
            DCIteratorBinding iter = (DCIteratorBinding) bindings.get("Departments1Iterator");
    
            if (vce.getNewValue() != null) {
                //Get all selected values in an Object array
                Object[] selectedVals = (Object[]) vce.getNewValue();
                //Iterate over array to get all selected DepartmentId
                for (int i = 0; i < selectedVals.length; i++) {
                    Integer val = (Integer) selectedVals[i];
                    //Create Key using DepartmentId to use furhter
                    Key key = new Key(new Object[] { val });
                    //Get ViewObject row using Key vlaue
                    Row row = iter.getViewObject().getRow(key);
                    // Get DepartmentName from row and add it to String
                    if (displayVal != "") {
                        displayVal = displayVal.concat(", ").concat(row.getAttribute("DepartmentName").toString());
                    } else {
                        displayVal = displayVal.concat(row.getAttribute("DepartmentName").toString());
                    }
                }
                //Write JavaScript code to change text of selectManyChoice as a StingBuilder Object
                StringBuilder jsString = new StringBuilder();
                //First Step-get clientID of component
                jsString.append("var elementId = '" + vce.getComponent().getClientId());
                //Second- add ::content to access it's value
                jsString.append("::content';");
                //Third- Check that current value is 'All' or not
                jsString.append("\n if (document.getElementById(elementId).value == 'All') {");
                //Forth- if yes then assign Department Name's string as value of selectManyChoice
                jsString.append("\n document.getElementById(elementId).value ='" + displayVal + "' \n};");
                System.out.println("JS File-" + jsString);
                //Call this JavaScript code using this Helper method
                writeJavaScriptToClient(jsString.toString());
    
            }
        }
    

    Method to call JavaScript from managed bean-

        /**Helper Method to call Javascript
         * @param javascriptCode
         */
        public static void writeJavaScriptToClient(String javascriptCode) {
            FacesContext facesCtx = FacesContext.getCurrentInstance();
            ExtendedRenderKitService service = Service.getRenderKitService(facesCtx, ExtendedRenderKitService.class);
            service.addScript(facesCtx, javascriptCode);
        }
    

    Import packages-

    import javax.faces.context.FacesContext;
    import javax.faces.event.ValueChangeEvent;
    
    import oracle.adf.model.BindingContext;
    import oracle.adf.model.binding.DCIteratorBinding;
    
    import oracle.binding.BindingContainer;
    
    import oracle.jbo.Key;
    import oracle.jbo.Row;
    
    import org.apache.myfaces.trinidad.render.ExtendedRenderKitService;
    import org.apache.myfaces.trinidad.util.Service;
    

  • Run application and check, on selecting all values it shows -


  • but this will happen only on value change event of selectManyChoice , you can call this JavaScript any time as per your requirement like on page load
Thanks , Happy Learning :-)

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

Saturday 21 December 2013

Using Multiple Selection (selectManyListbox & selectManyCheckbox component) in ADF

Hello All ,
This tutorial is based on use of selectManyListbox & selectManyCheckbox component in ADF to enable multiple selection.
Both component are same in functionality only somewhat different in look-feel.



Follow steps to use these component -



  • Create a fusion web application and create business components for Departments table of HR Schema

  • Now create a page and drop Departments Vo from data control on page as multiple selection (checkbox or listbox) 




  • Now binding for this component is created in page-bindings, add a button on page to get total selected values


  • See how to get selected values in managed bean, JUCtrlListBinding is used to get selected values

  • import javax.faces.event.ActionEvent;
    
    import oracle.adf.model.BindingContext;
    
    import oracle.binding.BindingContainer;
    
    import oracle.jbo.uicli.binding.JUCtrlListBinding;   
    
     public BindingContainer getBindings() {
            return BindingContext.getCurrent().getCurrentBindingsEntry();
        }
    
        public void getSelectedValue(ActionEvent actionEvent) {
    
            JUCtrlListBinding listBindings = (JUCtrlListBinding)getBindings().get("DepartmentsView1");
            Object str[] = listBindings.getSelectedValues();
            for (int i = 0; i < str.length; i++) {
                System.out.println(str[i]);
            }
        }
    

  • this method works for both listbox and checkbox, i have added all selected departments in a FacesMessage and displayed on page

 Select All-


And Check-box component look like this-


Complete code written on 'Get Selected Values' button-


    /**Method to get BindingContainer of page
     * @return
     */
    public BindingContainer getBindings() {
        return BindingContext.getCurrent().getCurrentBindingsEntry();
    }

    /**Method to get Selected Values
     * @param actionEvent
     */
    public void getSelectedValue(ActionEvent actionEvent) {

        JUCtrlListBinding listBindings = (JUCtrlListBinding)getBindings().get("DepartmentsView1");
        Object str[] = listBindings.getSelectedValues();
        StringBuilder saveMsg =
            new StringBuilder("<html><body><b><p style='color:red'>Selected Departments are-</p></b>");

        saveMsg.append("<ul>");
        for (int i = 0; i < str.length; i++) {
            System.out.println(str[i]);
            saveMsg.append("<li> <b>" + str[i].toString() + "</b></li>");
        }

        saveMsg.append("</ul><br>");
        saveMsg.append("</body></html>");
        FacesMessage msg = new FacesMessage(saveMsg.toString());
        FacesContext.getCurrentInstance().addMessage(null, msg);

    }

Cheers- Download Sample