Thu. Jun 17th, 2021

Requirement: There will be many scenarios where we have to create and execute the ViewCriteria at runtime. Thus, in this post I will describe how we can create and execute ViewCriteria programmatically.

ViewCriteria: ViewCriteria are the named expressions for queries that are used to further refine the result. We create ViewCriteria on View Object.

Solution: For solution of the above requirement follow the steps as shown below:

Step 1: Create an Oracle ADF Fusion Web Application.

Step 2: Create an ADF Business Components from Table (EO, VO, and AM) for the Employees table present in the HR schema of the Oracle Database 11g XE Edition.

Create an AppModuleImpl.java file.

Thus, the Model Project will look as shown below:

Step 3: Create a demo.jspx page inside the ViewController project.

Drag and drop af:inputText in the demo.jspx page. Set label=”Enter Department ID”, and create the binding as binding=”#{pageFlowScope.MyBean.departmentIdBinding}”

Drag and drop af:button. Set text=”Search”, and create an actionListener as actionListener=”#{pageFlowScope.MyBean.searchAL}”

Drag and drop the EmployeesVO1 from the AppModuleDataControl as a af:table on the demo.jspx page.

Thus, the complete code for demo.jspx page is shown below:

<?xml version='1.0' encoding='UTF-8'?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1" xmlns:f="http://java.sun.com/jsf/core"
          xmlns:af="http://xmlns.oracle.com/adf/faces/rich">
    <jsp:directive.page contentType="text/html;charset=UTF-8"/>
    <f:view>
        <af:document title="demo.jspx" id="d1">
            <af:messages id="m1"/>
            <af:form id="f1">
                <af:inputText label="Enter Department ID" id="it1"
                              binding="#{pageFlowScope.MyBean.departmentIdBinding}"/>
                <af:button text="Search" id="b1" actionListener="#{pageFlowScope.MyBean.searchAL}"/>
                <af:table value="#{bindings.EmployeesVO1.collectionModel}" var="row"
                          rows="#{bindings.EmployeesVO1.rangeSize}"
                          emptyText="#{bindings.EmployeesVO1.viewable ? 'No data to display.' : 'Access Denied.'}"
                          rowBandingInterval="0" selectedRowKeys="#{bindings.EmployeesVO1.collectionModel.selectedRow}"
                          selectionListener="#{bindings.EmployeesVO1.collectionModel.makeCurrent}" rowSelection="single"
                          fetchSize="#{bindings.EmployeesVO1.rangeSize}" id="t1">
                    <af:column headerText="#{bindings.EmployeesVO1.hints.EmployeeId.label}" id="c1">
                        <af:outputText value="#{row.EmployeeId}"
                                       shortDesc="#{bindings.EmployeesVO1.hints.EmployeeId.tooltip}" id="ot1">
                            <af:convertNumber groupingUsed="false"
                                              pattern="#{bindings.EmployeesVO1.hints.EmployeeId.format}"/>
                        </af:outputText>
                    </af:column>
                    <af:column headerText="#{bindings.EmployeesVO1.hints.FirstName.label}" id="c2">
                        <af:outputText value="#{row.FirstName}"
                                       shortDesc="#{bindings.EmployeesVO1.hints.FirstName.tooltip}" id="ot2"/>
                    </af:column>
                    <af:column headerText="#{bindings.EmployeesVO1.hints.LastName.label}" id="c3">
                        <af:outputText value="#{row.LastName}"
                                       shortDesc="#{bindings.EmployeesVO1.hints.LastName.tooltip}" id="ot3"/>
                    </af:column>
                    <af:column headerText="#{bindings.EmployeesVO1.hints.Email.label}" id="c4">
                        <af:outputText value="#{row.Email}" shortDesc="#{bindings.EmployeesVO1.hints.Email.tooltip}"
                                       id="ot4"/>
                    </af:column>
                    <af:column headerText="#{bindings.EmployeesVO1.hints.PhoneNumber.label}" id="c5">
                        <af:outputText value="#{row.PhoneNumber}"
                                       shortDesc="#{bindings.EmployeesVO1.hints.PhoneNumber.tooltip}" id="ot5"/>
                    </af:column>
                    <af:column headerText="#{bindings.EmployeesVO1.hints.HireDate.label}" id="c6">
                        <af:outputText value="#{row.HireDate}"
                                       shortDesc="#{bindings.EmployeesVO1.hints.HireDate.tooltip}" id="ot6">
                            <af:convertDateTime pattern="#{bindings.EmployeesVO1.hints.HireDate.format}"/>
                        </af:outputText>
                    </af:column>
                    <af:column headerText="#{bindings.EmployeesVO1.hints.JobId.label}" id="c7">
                        <af:outputText value="#{row.JobId}" shortDesc="#{bindings.EmployeesVO1.hints.JobId.tooltip}"
                                       id="ot7"/>
                    </af:column>
                    <af:column headerText="#{bindings.EmployeesVO1.hints.Salary.label}" id="c8">
                        <af:outputText value="#{row.Salary}" shortDesc="#{bindings.EmployeesVO1.hints.Salary.tooltip}"
                                       id="ot8">
                            <af:convertNumber groupingUsed="false"
                                              pattern="#{bindings.EmployeesVO1.hints.Salary.format}"/>
                        </af:outputText>
                    </af:column>
                    <af:column headerText="#{bindings.EmployeesVO1.hints.CommissionPct.label}" id="c9">
                        <af:outputText value="#{row.CommissionPct}"
                                       shortDesc="#{bindings.EmployeesVO1.hints.CommissionPct.tooltip}" id="ot9">
                            <af:convertNumber groupingUsed="false"
                                              pattern="#{bindings.EmployeesVO1.hints.CommissionPct.format}"/>
                        </af:outputText>
                    </af:column>
                    <af:column headerText="#{bindings.EmployeesVO1.hints.ManagerId.label}" id="c10">
                        <af:outputText value="#{row.ManagerId}"
                                       shortDesc="#{bindings.EmployeesVO1.hints.ManagerId.tooltip}" id="ot10">
                            <af:convertNumber groupingUsed="false"
                                              pattern="#{bindings.EmployeesVO1.hints.ManagerId.format}"/>
                        </af:outputText>
                    </af:column>
                    <af:column headerText="#{bindings.EmployeesVO1.hints.DepartmentId.label}" id="c11">
                        <af:outputText value="#{row.DepartmentId}"
                                       shortDesc="#{bindings.EmployeesVO1.hints.DepartmentId.tooltip}" id="ot11">
                            <af:convertNumber groupingUsed="false"
                                              pattern="#{bindings.EmployeesVO1.hints.DepartmentId.format}"/>
                        </af:outputText>
                    </af:column>
                </af:table>
            </af:form>
        </af:document>
    </f:view>
</jsp:root>

And the complete code for the MyBean.java is shown below:

package susantotech.com;

import javax.faces.event.ActionEvent;

import model.AppModuleImpl;

import oracle.adf.model.BindingContext;
import oracle.adf.model.binding.DCDataControl;
import oracle.adf.view.rich.component.rich.input.RichInputText;

import oracle.jbo.ViewCriteria;
import oracle.jbo.ViewCriteriaRow;
import oracle.jbo.ViewObject;

public class MyBean {
    private RichInputText departmentIdBinding;

    public MyBean() {
    }

    public void setDepartmentIdBinding(RichInputText departmentIdBinding) {
        this.departmentIdBinding = departmentIdBinding;
    }

    public RichInputText getDepartmentIdBinding() {
        return departmentIdBinding;
    }

    public void searchAL(ActionEvent actionEvent) {
        int departmentIdVal = 0;
        if (null != departmentIdBinding.getValue()) {
            departmentIdVal = Integer.parseInt(departmentIdBinding.getValue().toString());
        }
        System.out.println("departmentIdVal is : " + departmentIdVal);
        ViewObject vo = getAppModule().getEmployeesVO1();
        ViewCriteria vc = vo.createViewCriteria();
        ViewCriteriaRow vcRow = vc.createViewCriteriaRow();
        vcRow.setAttribute("DepartmentId", departmentIdVal);
        vc.addRow(vcRow);
        vo.applyViewCriteria(vc);
        vo.executeQuery();
    }

    public AppModuleImpl getAppModule() {
        BindingContext bindingContext = BindingContext.getCurrent();
        DCDataControl dc = bindingContext.findDataControl("AppModuleDataControl");
        AppModuleImpl appModule = (AppModuleImpl) dc.getDataProvider();
        return appModule;
    }
}

Save all and run the application. Thus, the ran application is shown below:

Enter value in the Department ID field as 90 and click on the Search button. And we can see that the ViewCriteria is applied to the EmployeesVO1 and the the table data is now filtered with rows that has DepartmentId as 90 only.

Hence, the solution to our requirement.

If you like the post please comment, share, and do join me on Facebook. Please subscribe to my YouTube Channel for video tutorials.

Thanks & Regards,
Susanto Paul

 400 total views,  1 views today

By Susanto Paul

Susanto is an Oracle ACE, a qualified MCA, MBA, and a highly-skilled Senior Oracle Specialist. He is an enthusiastic Blogger and YouTuber who helps learners to solve their complex problems more efficiently. He has 9+ years of experience in multiple technologies like AWS, Oracle ADF, Oracle APEX, Oracle JET, Oracle VBCS, Oracle IDCS, Oracle PL/SQL, Oracle Integration Cloud, Java, JDBC, Servlets, JSP, Spring, Hibernate, HTML5, CSS3, JavaScript, TypeScript, NodesJS, Angular, MySQL, Oracle WebLogic Server, JUnit, JMeter, Selenium Web Driver, etc. He is a certified: Oracle Certified Professional Java SE 6 Programmer, Oracle ADF 11g Certified Implementation Specialist, Oracle Cloud Platform Application Integration 2020 Certified Specialist, Oracle Cloud Infrastructure Foundations 2020 Certified Associate, and Oracle Cloud Infrastructure Developer 2020 Certified Associate

2 thoughts on “Programmatically Create and Execute ViewCriteria in Oracle ADF”

Leave a Reply

Your email address will not be published. Required fields are marked *