ADF Faces: Retrieve and/or Set values in a SelectOneChoice Component backed by a RowSetIterator
This entry covers how to retrieve values from a SelectOneChoice component that is backed (populated) by a ViewObject as defined in a ADF Faces page definition file.
I've been working on a fairly large and complex ADF Faces project for my current employer. I often find myself having to both retrieve and set values in SelectOneChoice components in my backing beans. I was initially confounded by the fact that the HTML generated by the component did not include my own code values behind the select options. Rather, each option (option tag) uses a numeric value instead of the actual code you would normally see if you were creating the HTML yourself (such as "NC" as the code for description "North Carolina"). After some digging, I found that the numbers correspond to the Iterator index values of the Iterator that is bound to SelectOneChoice component. By the way, setting the valuePassThru attribute on the component to true did not work for some reason.
Armed with this new knowledge, I was able to create two utility methods that reside in my base (utility) managed bean class.
The first method, getSelectOneChoiceValues, retrieves both the code (the actual code from your database or view object) and the description value from the Iterator that is bound to the SelectOneChoice component given the index value. Here is the code for first method:
/*** * This method returns the code and desc values from a SelectOneChoice. * Parameters * (1) p_iteratorIndex - represents the index value of the iterator row we want. You get * this value from bound ADF component. * Ex. (Integer)boundSelectItem.getValue() * * (2) p_iterator - Name of the Iterator that populates the SelectOneChoice component * (3) p_code_colname - Column attribute name of the column containing the code value (get this * from the PageDefinition file for the screen, or the ViewObject that * is bound to the Iterator.) * (4) p_codedesc_colname - Column attribute name of the column containing the code desc (get this * from the PageDefinition file for the screen, or the ViewObject that * is bound to the Iterator.) * (5) p_noselection_val - Set this value to "true" if you chose to add a "No Selection" row * or null value row to your selection object. This row will become the * zero index row. This "null" value row is not represented in the * Iterator and throws the index values off by 1. * * The return value is a HashMap, but you could create a simple class for this as well. */ protected HashMap getSelectOneChoiceValues(int p_iteratorIndex, String p_iterator, String p_code_colname, String p_codedesc_colname, boolean p_noselection_val) { HashMap hm_lovVals = new HashMap(); String lovTypeCode = null; String lovTypeDesc = null; int noSelectIncrementor = 0; if (p_noselection_val){ noSelectIncrementor = 1; } try{
// The code for "getRowSetIterator" can be found in the blog entry I published directly // before this entry or Google "Jason getRowSetIterator"
RowSetIterator lovIter = getRowSetIterator(p_iterator); Row iterRow = null; iterRow = lovIter.getRowAtRangeIndex(p_iteratorIndex-noSelectIncrementor); lovTypeCode = iterRow.getAttribute(p_code_colname).toString(); lovTypeDesc = iterRow.getAttribute(p_codedesc_colname).toString(); }catch(Exception e){ System.err.println("Error looking up values for LOV iterator "+p_iterator); } //the keys "code" and "desc" should not be hard coded values in your production code ... hm_lovVals.put("code",lovTypeCode); hm_lovVals.put("desc",lovTypeDesc); return hm_lovVals; }
The second method, getSelectOneChoiceIndex, performs the opposite function. Given code value and code column, it returns the actual index value. This allows you set the value of SelectOneChoice component on the screen ( Ex. selectionComponent.setValue(indexVal); ). Here is the code for the second method:
/*** * This method returns the index value for an item in a SelectOneChoice. * Parameters * * (1) p_iterator - Name of the Iterator that populates the SelectOneChoice component * (2) p_code_value - Code value whose index we are looking for. * Ex. We pass in "NC" and get the index for the row in the Iterator * that contains the code "NC". * * (3) p_code_column - Column attribute name of the column containing the code value (get this * from the PageDefinition file for the screen, or the ViewObject that * is bound to the Iterator.) * * (4) p_noselection_val - Set this value to "true" if you chose to add a "No Selection" row * or null value row to your selection object. This row will become the * zero index row. This "null" value row is not represented in the * Iterator and throws the index values off by 1. * * The return value is an Integer. */ protected Integer getSelectOneChoiceIndex(String p_iterator, String p_code_value, String p_code_column, boolean p_noselection_val){ int v_code_index = 0; int noSelectIncrementor = 0; if (p_noselection_val){ noSelectIncrementor = 1; } try{ // The code for "getRowSetIterator" can be found in the blog entry I published directly // before this entry or Google "Jason getRowSetIterator"
RowSetIterator lovIter = getRowSetIterator(p_iterator); Row lovRow = null; for (int x=0;x<lovIter.getRowCount();x++){ lovRow = lovIter.getRowAtRangeIndex(x); if(lovRow.getAttribute(p_code_column).equals(p_code_value)){ v_code_index = x+noSelectIncrementor; break; } } }catch(Exception e){ System.err.println("Error looking up index value for LOV iterator "+p_iterator); } return new Integer(v_code_index); }
7:58:45 AM
|