Friday 8 May 2015

ADF: How to create SelectOneChoice based on values from a database table in ADF Web Application - Part1

Recently there was couple of questions in the OTN forum in regards to creating dropdown in the based on values from a table. I googled and couldn't find much results on this. So thought of adding here.

Normally when we need an LOV/dropdown in the page, it will be for an attribute in a VO. So we define the LOV for that attribute in the VO definition.

ex: We will create a DeptVO with DeptId and DeptName and use this VO to create LOV for the departmentId attribute of EmployeeVO.

What if we just need a dropdown in the page with the values coming from a database table.

ex:- Just show a dropdown with all the Department Names.


Option1 : You can create a DummyViewObject with needed attribute(s) and create an LOV on that attribute(s) using the DeptVO. [I prefer this approach. This approach is explained here ADF: How to create SelectOneChoice based on values from a database table in ADF Web Application - Part2]


Option 2:
  • Create a Query based View Object DeptVO with DepartmentId and DepartmentName.
  • You just drag the DeptVO as  a selectOneChoice into the page.
  • Select the DepartmentName as the display attribute.
  • Remove the value property and map it to a Managed Bean variable.
  • Set autoSubmit to true.  (Only if you want to handle the Value Change event)
  • Set the Unselected Label to whatever you want. (ex: ***Select a Value***)
  • You need to select the 'Include No Selection' checkbox in the ViewObject definition.
This is good enough to show the dropdown in the page based on values from the database, without creating additional ViewObject.


Now to get the values in the valueChangeListener event, you can write the below code:
   public void valueChangeEvent(ValueChangeEvent valueChangeEvent) {  
     System.out.println("New Name : " + valueChangeEvent.getNewValue());  
     valueChangeEvent.getComponent().processUpdates(FacesContext.getCurrentInstance());   
     DCBindingContainer bindingContainer = (DCBindingContainer) BindingContext.getCurrent().getCurrentBindingsEntry();   
     DCIteratorBinding binding = bindingContainer.findIteratorBinding("DeptVO1Iterator");   
     Row newRow = binding.getCurrentRow();   
     System.out.println("New ID: "+newRow.getAttribute("DepartmentId"));   
   }  

If you want to get the selected value on the click of a button, you can just get it from the ManagedBean variable.
   public void buttonActionListener(ActionEvent actionEvent) {  
     System.out.println("New Name : " + this.lovValue);  
   }  

Sample application built using Jdev 12.1.3 can be found here.

Feel free to point out if anything is missing/wrong in this blog.

4 comments:

  1. Sometimes little, small stuff is important but not written anywhere
    Good Post Johny :)

    ReplyDelete
  2. Thanks AJ,
    This is udys. I am now very lost. I implemented the code suggested in my bean associated with ValueChangeListener of selectOneChoice in my application. here is the method.
    public void HZHRLocsocMtd(ValueChangeEvent valueChangeEvent) {
    // Add event code here...
    //this.setValueToEL("#{bindings.HZHRLocations2.inputValue}", valueChangeEvent.getNewValue());
    //System.out.println ("***** Selected From Location: "+resolveExpression("#{bindings.HZHRLocations2.attributeValue}"));
    //System.out.println ("***** Display Value: "+resolveExpression("#{bindings.AssetGrp1.selectedValue ne ' ' ? bindings.AssetGrp1.selectedValue.attributeValues[1] : ''}"));
    System.out.println("New Loc : " + valueChangeEvent.getNewValue());
    valueChangeEvent.getComponent().processUpdates(FacesContext.getCurrentInstance());
    DCBindingContainer bindingContainer = (DCBindingContainer) BindingContext.getCurrent().getCurrentBindingsEntry();
    DCIteratorBinding binding = bindingContainer.findIteratorBinding("HZHRLocations2Iterator");
    Row newRow = binding.getCurrentRow();
    System.out.println("New Loc ID: "+newRow.getAttribute("LocationId"));
    System.out.println("New Loc Code: "+newRow.getAttribute("LocCode"));
    System.out.println("New Loc Addr: "+newRow.getAttribute("Address"));
    }

    When run, I chose CHCG.IL.CA.DS1 and the output was thus. It resets to the first list element of the sOC
    New Loc : CHCG.IL.CA.DS1
    New Loc ID: 18168
    New Loc Code: FAC-Facilities
    New Loc Addr: 1100 Abernathy Road,Northpark Towers, Building 500,Suite 1120,Atlanta,GA,30328,US

    Next I chose HZ-V1-1 and resultant output was thus.
    New Loc : HZ-V1-1
    New Loc ID: 18168
    New Loc Code: FAC-Facilities
    New Loc Addr: 1100 Abernathy Road,Northpark Towers, Building 500,Suite 1120,Atlanta,GA,30328,US

    Now I need the selected code into the method in a bean associated with the button "Find Assets". I have had it all correctly earlier, but if I did not make a selection, it would still derive the first list element from the sOC. It happens to other sOC as well, when no selection is made. For example, if no selection is made, the out put shows thus.
    Loc: FAC-Facilities Grp: Automatic Category: BLDG100.Room100
    where FAC-Facilities, Automatic and BLDG100.Room100 are the first elements of each of the three sOC. Please let me know.
    Thanks for your help.

    ReplyDelete
    Replies
    1. udys,

      Lets discuss this in the same thread in the forum.
      Please attach the selectOneChoice xml and the bean code in the thread.

      Note:- If you just want to get the dropdown values in the click of a button, you don't need to write code in the valueChangeListener. You write the code in the valueChangeListener when you want to do something when the value changes.

      Have you mapped all the LOV's to a bean variable ?

      Lets discuss in the thread.

      Cheers
      AJ

      Delete