Sunday, March 9, 2014

Creating Search Form in ADF Essentials using JPA Entities

In this post we will see how a search form can be created in ADF Essentials when the model layer is based on JPA entities. I am going to use the same ADFE application based on Oracle HR schema that I have previously used in other posts.

We will create a search form that will let us search employees based on their first and last names. To achieve this we will first create a JPA named query for employee search based on employee's first and last name then we will expose this named query in ADF Data Control and from there we will use it in ADF Faces to create a search form.

Here are these steps in detail:

Create a Named Query for search
There is already a default named query that returns all employees but for our purpose we need to create a new named query which will accept two input parameters for first and last name and return the employee/employees matching that first and the last name.
  @NamedQuery(name = "Employee.findAll", query = "SELECT e FROM Employee e"),
  @NamedQuery(name = "", query = "SELECT e FROM Employee e WHERE e.firstName = :firstName and e.lastName = :lastName") })

Expose it in ADF Data Control
This step is not required if we have not already created an ADF Data Control for our ADFE application. But if we already have an ADF Data Control then it needs to be refreshed to take in the new changes made in Employee entity. To refresh ADF Data Control, one way is to right click on the Session Bean and select Model Components > Edit Session Bean Facade. Then on the next screen unselect Employee entity and then select it back. Also, to see the changes in Data Control the ADFE Web project may need to be closed and opened again in Eclipse.

Use it in Search Form
We can see the new new named query we created for employee search appearing as a named collection in our Data Control for the ADFE Web project.

Also, we can see ExecuteWithParams operation for employeeSearch collection. This is the operation we are going to use to implement search functionality.
We will drag and drop ExecuteWithParams on the page and choose Parameter > ADF Parameter Form. This will create a form with two input fields to enter first and last name and a button which will call ExecuteWithParams and pass the values provided in input fields as parameters to it.
Now we will drag and drop employeeSearch collection on the page as a table. This table will contain the search results. We will also Set partialTriggers property of table with the id of ExecuteWithParams button and make sure Partial submit property of button is set to true. By default Partial submit is set to true.
After building and running the page we will get to see a search form implementation like this:

The Named Query that we have used searches for the employee who has the specified first and last name and does not return any results if either one or both of criteria are null. To make search functionality more user friendly such that it works with null values, we can change it to following:
@NamedQuery(name = "", query = "SELECT e FROM Employee e WHERE (e.firstName = :firstName or :firstName is null) and (e.lastName = :lastName or :lastName is null)") })

No comments :

Post a Comment