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 SampleAppSessionBean.java 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="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
 version="3.1">
 <display-name>SampleADFEAppWeb</display-name>
 <filter>
  <filter-name>adfBindings</filter-name>
  <filter-class>oracle.adf.model.servlet.ADFBindingFilter</filter-class>
 </filter>
 <filter-mapping>
  <filter-name>adfBindings</filter-name>
  <servlet-name>Faces Servlet</servlet-name>
  <dispatcher>FORWARD</dispatcher>
  <dispatcher>REQUEST</dispatcher>
 </filter-mapping>
 <filter>
  <filter-name>trinidad</filter-name>
  <filter-class>org.apache.myfaces.trinidad.webapp.TrinidadFilter</filter-class>
 </filter>
 <filter-mapping>
  <filter-name>trinidad</filter-name>
  <servlet-name>Faces Servlet</servlet-name>
  <dispatcher>FORWARD</dispatcher>
  <dispatcher>REQUEST</dispatcher>
 </filter-mapping>
 <servlet>
  <servlet-name>Faces Servlet</servlet-name>
  <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>Faces Servlet</servlet-name>
  <url-pattern>/faces/*</url-pattern>
 </servlet-mapping>
 <servlet>
  <servlet-name>resources</servlet-name>
  <servlet-class>org.apache.myfaces.trinidad.webapp.ResourceServlet</servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>resources</servlet-name>
  <url-pattern>/adf/*</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
  <servlet-name>resources</servlet-name>
  <url-pattern>/afr/*</url-pattern>
 </servlet-mapping>
 <context-param>
  <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
  <param-value>resources.application</param-value>
 </context-param>
 <ejb-local-ref>
  <ejb-ref-name>ejb/local/SampleAppSessionBean</ejb-ref-name>
  <ejb-ref-type>Session</ejb-ref-type>
  <local>model.SampleAppSessionBeanLocal</local>
  <ejb-link>SampleAppSessionBean</ejb-link>
 </ejb-local-ref>
</web-app> 
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="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="SampleADFEAppModel" transaction-type="JTA">
    <jta-data-source>jdbc/test_hr</jta-data-source>
        <class>model.Department</class>
        <class>model.Employee</class>
    </persistence-unit>
</persistence>

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.