Skip to main content

Custom LOV with AutoSuggest implementation in ADF

Prerequisites of this implementation :
  1. ViewObject (in the example it is "MyViewObject")
  2. Some utility is used in the example, check my other blogs "ADF  Utility"
#1. Create LOV Listener class

This class will open the LOV window in a popup and also when user selects a row and click OK in the popup, it will set the value property of the input Text ui component where this LOV is invoked from.

// Add the getters and setters of all the class level attributes.
// I have removed them from this example to shorten the length of the blog.
// For "getUIComponent()" check my other blog to find a UI Component from managed bean
public class MyLOVListener {
   
    /**
     * ID of the popup where af:table is defined
     */
    private String popupId;
    /**
     * ID of the inputText component where the selected value
     * from the popup should be copied back
     */
    private String inputTextId;
    /**
     * Name of the iterator defined the in pagedef, from which the selected row
     * can be read
     */
    private String iteratorName;
    /**
     * Name of the attribute of the view object which should be read and copied
     * back to the "inputText" component
     */
    private String attributeName;
    /**
     * Name of the view object
     */
    private String viewObjectToQuery;
         
    public void openLOV(ActionEvent actionEvent) {

        ViewObjectImpl lovViewObject = (ViewObjectImpl) Constants.getService().findViewObject(getViewObjectToQuery());
        lovViewObject.executeQuery();
        RichPopup.PopupHints ph = new RichPopup.PopupHints();
        ((RichPopup)getUIComponent(getPopupId())).show(ph);
    }

    public void setSelectedRow(DialogEvent dialogEvent) {

        if(dialogEvent.getOutcome() == DialogEvent.Outcome.ok) {
            DCBindingContainer bindings = (DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
            DCIteratorBinding dcItteratorBindings = bindings.findIteratorBinding(getIteratorName());
            ViewObject voTableData = dcItteratorBindings.getViewObject();    
            // Get selected row
            Row rowSelected = voTableData.getCurrentRow();    
           
            if (rowSelected != null) {
                RichInputText inputTextBox = (RichInputText)getUIComponent(getInputTextId());
                inputTextBox.resetValue();
                inputTextBox.setValue((String)rowSelected.getAttribute(getAttributeName()));
                AdfFacesContext adfFacesContext = AdfFacesContext.getCurrentInstance();
                adfFacesContext.addPartialTarget(inputTextBox);
            }
        }
    }

}

Declare this class as a taskFlow managed bean

<managed-bean id="__7">
      <managed-bean-name id="__8">MyLOVListener</managed-bean-name>
      <managed-bean-class id="__6">com.view.beans.MyLOVListener</managed-bean-class>
      <managed-bean-scope id="__5">pageFlow</managed-bean-scope>
    </managed-bean>


#2. Managed bean for auto suggest behaviour :

public class MyAutoSuggestionListener {
   
    public List getEmployees(String employee) {
        List<SelectItem> matchingItemsList = new ArrayList<SelectItem>();
       
        if(employee != null) {
            matchingItemsList = getMatchingItems(getService().getMyViewObject(), "employee", employee);
        }
        return matchingItemsList;
    }
    

  private ApplicationModuleImpl getService() {
      return ((ApplicationModuleImpl)AppModuleBean.getService(FacesContext.getCurrentInstance(), "MyAppModule"));
  }


    private List getMatchingItems(ViewObjectImpl viewObject, String attributeName, String stringToMatch) {
       
        List<SelectItem> matchingItemsList = new ArrayList<SelectItem>();
        viewObject.executeQuery();
           
        for (Row row : viewObject.getAllRowsInRange()) {
            String matchingEntries = (String)row.getAttribute(attributeName);
            if(matchingEntries.toUpperCase().startsWith(stringToMatch.toUpperCase())) {
                matchingItemsList.add(new SelectItem(matchingEntries, matchingEntries));
            }
        }
        return matchingItemsList;
    }
}


Declare this class as a taskFlow managed bean

 <managed-bean id="__9">
      <managed-bean-name id="__16">MyAutoSuggestionListener</managed-bean-name>
      <managed-bean-class id="__15">view.beans.MyAutoSuggestionListener</managed-bean-class>
      <managed-bean-scope id="__14">pageFlow</managed-bean-scope>
    </managed-bean>


#3. Add input Text for the LOV :

<af:inputText simple="true" id="employeeIt1" columns="25">
                      <af:autoSuggestBehavior maxSuggestedItems="40"
                                              suggestedItems="#{pageFlowScope.MyAutoSuggestionListener.getEmployees}"/>
                    </af:inputText>
                    <af:spacer id="sp1" width="3"/>
                    <af:commandImageLink id="empPopup" icon="/common/images/details.png"
                                         immediate="true" partialSubmit="true"
                                         actionListener="#{pageFlowScope.MyLOVListener.openLOV}">
                        <af:setPropertyListener from="employeeIt1" to="#{pageFlowScope.MyLOVListener.inputTextId}" type="action"/>
                        <af:setPropertyListener from="employeePopup" to="#{pageFlowScope.MyLOVListener.popupId}" type="action"/>
                        <af:setPropertyListener from="MyViewObjectIterator" to="#{pageFlowScope.MyLOVListener.iteratorName}" type="action"/>
                        <af:setPropertyListener from="employee" to="#{pageFlowScope.MyLOVListener.attributeName}" type="action"/>
                        <af:setPropertyListener from="MyViewObject" to="#{pageFlowScope.MyLOVListener.viewObjectToQuery}" type="action"/>
                    </af:commandImageLink>


In the above command image link : the icon used is a spy glass, which normally used by ADF in case of LOV in a popup.

#4. Add the popup for LOV in the page:

First define a af:popup element in the page like below :
<af:popup autoCancel="disabled" childCreation="immediate" id="employeePopup">
 <af:dialog id="employeeDialog" dialogListener="#{pageFlowScope.MyLOVListener.setSelectedRow}">
 

</af:dialog></af:popup>

Then drag and drop the view object from DataControls panel in JDeveloper. Make sure you display it as a read only table. It will look like below :

<af:popup autoCancel="disabled" childCreation="immediate" id="employeePopup">
                    <af:dialog id="employeeDialog"
                               dialogListener="#{pageFlowScope.MyLOVListener.setSelectedRow}">
                      <af:table value="#{bindings.MyViewObject.collectionModel}"
                                var="row"
                                rows="#{bindings.MyViewObject.rangeSize}"
                                emptyText="#{bindings.MyViewObject.viewable ? 'No data to display.' : 'Access Denied.'}"
                                fetchSize="#{bindings.MyViewObject.rangeSize}"
                                rowBandingInterval="0"
                                selectedRowKeys="#{bindings.MyViewObject.collectionModel.selectedRow}"
                                selectionListener="#{bindings.MyViewObject.collectionModel.makeCurrent}"
                                rowSelection="single" id="t1">
                        <af:column sortProperty="#{bindings.MyViewObject.hints.employee.name}"
                                   sortable="true"
                                   headerText="#{bindings.MyViewObject.hints.employee.label}"
                                   id="c2" width="400">
                          <af:outputText value="#{row.employee}" id="ot1"/>
                        </af:column>
                      </af:table>
                    </af:dialog>
                  </af:popup>


This implementation has some advantages :
  1. If you implement ADF managed LOVs, if the view object returns 0 rows, it throws NullPointer Exception. But this implementation will open an empty table in the popup.
  2. This implementation is reusable in the same taskFlow for multiple other LOV implementations. You only need to add one extra method in MyAutoSuggestionListener and follow #3 and #4 step for the new LOVs. The LOV listener class is robust enough to handle any new calls.

Comments

Popular posts from this blog

Exception Handling in ADF

This blog will give you an overview on how you can successfully deal with unhandled Runtime exceptions in an ADF application.

This will give you an idea of:
How to catch the unhandled exceptions.Write a separate log file with stacktrace and thread dumps.Redirect the user to an static error page
#1. Catch unhandled exceptions : 

Create a class "MyExceptionHandler" which extends : oracle.adf.view.rich.context.ExceptionHandler. Override handleException() method.

public void handleException(FacesContext facesContext, Throwable throwable, PhaseId phaseId) throws Throwable {
        // this method is going to create a separate file with stacktrace and thread dumps
        writeException(throwable);
        // redirect to error page
        redirectToErrorPage(facesContext);
    }

 Create a folder "services" inside : ViewController\src\META-INF and then create a file named "oracle.adf.view.rich.context.ExceptionHandler".
In the file, add the absolute name of your custom e…

Bean Based ADF Table with List View in detailStamp facet

One of our projects, we had to display a "tree type" table with different datamodel for children. To solve this, I choose a normal af:table with a detailStamp facet with a List view inside it.
But pretty soon, I ran into an issue of correctly identifying the "disclosed" rows. Since my table's datasource is a bean, not ADF managed datacontrol.

So I solved it using a discloseListener on the af:table.

Full Application is on GitHub : https://github.com/sohamda/Row-Disclose-Example/


Classes invloved :

1. POJO.java : dataProvider class for af:table
2. SubPOJO.java : dataProvider for the list view inside detailStamp of the table.
3. TableBean.java : provides the methods for populating af:table and af:listView. Also has a discloseListener method to correctly populate the listView datasource.

af:table definition :

Chatbots and Oracle Cloud Services

Thanks to Oracle A-Team, I had a chance to work with Chatbots.
3 pure NodeJS applications, on couple of Oracle Cloud platforms and Facebook messenger, and my chatbot was running.

Let me explain, the architecture a bit. To start with, following is the simple representation of how it works.