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)") })

Wednesday, January 8, 2014

Creating ADF Essentials application with Oracle Enterprise Pack for Eclipse Part 2 of 2

This post is in continuation of my previous post on Creating ADF Essential application for Glassfish server using Oracle Enterprise Pack for Eclipse. In this post we will add a new JSF page in our ADFE application which will display data coming from Departments and Employees tables of HR schema. We will use an ADF data control based on a session bean to make data available for ADF faces components in our JSF page.

Create JPA Entities
We will start with creating JPA entities for Emplyees and Departments tables. To do so, right click on SampleADFEModel and select Generate Entities From Tables from JPA Tools.
In the next window select the database connection in our case it is HR_Conn and database schema  to connect to. You will see a list of available tables for HR schema. Here select Departments and Employees tables and proceed to the next window. Keep all the information regarding entities to be generated in consecutive windows as default and finish the Generate Custom Entities wizard.

Create Session Bean
Right click on Model project and go to New > Session Bean (EJB 3.x) from JPA Entities
In Create EJB Session Bean wizard specify the  session bean class name and package name it will be created in. Leave other settings as they are.

In the next window select model.Department and model.Employee entites and press Finish to close the wizard. As a result a session bean class will be created in the model project.

Create Data Control
Right click on and select Model Components > Create ADF Data Control...
In New Data Control wizard enter the name for data control and keep all the settings in subsequent windows as default.
Now a data control based on an EJB session bean is available for us to be used in our web project. Information about data controls created in the application can be found in DataControls.dcx file

Create JSF page
We are ready to create a web page which uses data coming from HR schema in database through ADF data control.
Right click on SampleADFEAppWeb/WebContent and select New > ADF Page
In New ADF Page wizard specify the page name and select Structure as Page and Technology JSP XML. In the next window select Create blank page and finish the wizard

Now we will work on the jspx page we just created. Make sure Palette window is visible in eclipse. It will provide available Data Controls and ADF Faces components that can be used in the web project. It can be enabled by going to Window > Show View > Palette

Open Pallete > Data > Data Controls. You will see the data control we created previously based on session bean. Expand it and you will find departmentFindAll and employeeFindAll which provide data from Departments and Employees tables through Department and Employee entities. There is a one-to-many relationship between Departments and Employees so on expanding departmentFindAll you will see an employees named collection. We will now see how easily master-detail data can be displayed in ADF faces using ADF data controls.

Now either drag and drop employees under departmentFindAll on the new ADF page or right clik on employees and select Insert in the page. You will see some options on how to display the data on the page. We will select Master-Detail and ADF Master Form, Detail Table as display format. You can choose from other options as well for displaying data in master-detail relationship.

As a result the page will now contain a panel form layout for Departments data and a table containing Employees data. Page layout can be adjusted further as per the requirements.

Run Application
We are almost ready to test the JSF page we created but if you try to run the application on Glassfish server now you will get few exceptions. Make sure you have covered following points before running the application:
1. web.xml looks like this:
<?xml version = '1.0' encoding = 'UTF-8'?>
<web-app xmlns:xsi=""
  <servlet-name>Faces Servlet</servlet-name>
  <servlet-name>Faces Servlet</servlet-name>
  <servlet-name>Faces Servlet</servlet-name>
  <servlet-name>Faces Servlet</servlet-name>
2. All the attribute values of any type in jspx page need to be in EL expression form except id, name and var e.g. height="#{'10'}" instead of height=10
3. A data source needs to be specified in persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="" xmlns:xsi="" xsi:schemaLocation="">
    <persistence-unit name="SampleADFEAppModel" transaction-type="JTA">

Now run the JSF page on Glassfish server. At runtime you will see departments data in a form layout such that the data of only one department is displayed at a time. Data of employees related to the department currently being displayed will be available in a tabular format below the Departments form.