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.
@NamedQueries({
  @NamedQuery(name = "Employee.findAll", query = "SELECT e FROM Employee e"),
  @NamedQuery(name = "Employee.search", 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 = "Employee.search", 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 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.

Monday, December 2, 2013

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

I am going to describe the steps needed to create a sample data bound application built on ADF Essentials framework and developed using Oracle Enterprise Pack for Eclipse 12.1.2.1.1. The application server used is Glassfish 4.

The list of online resources I referenced while creating this tutorial:
https://blogs.oracle.com/shay/entry/deploying_oracle_adf_applications_to
http://oracle-adf.org/how_to_install_Oracle_ADF_Essentials_on_GlassFish_Server.php
http://docs.oracle.com/cd/E47843_02/12121/OEPUG/oracle_adf_tools.htm

You need to follow below steps to create an ADF essentials application that can run on Glassfish server:

Download

1. Download Oracle Enterprise Pack for Eclipse 12.1.2.1.1 from OTN
2. Download ADF essentials libraries (both Core and Client) from  OTN
3. Download Glassfish 4 if you do not have it installed already.

Configure  

In order to run ADF application on Glassfish, ADF Runtime needs to be installed on the server. This requires ADF Essential core libraries to be copied into lib folder of your Glassfish domain. You will also need to deploy ADF Essentials Client libraries along with your code to Glassfish server by including them in your application EAR.
There are ways of doing this but I prefer using Ant script provided in OEPE for configuring Glassfish as described in Oracle docs

1. Create a Java project in OEPE.
2. In your OEPE install, go to $install_dir/plugins/oracle.eclipse.tools.adf_6.2.0.$buildrelease/adf-essentials-glassfish-config
3. Copy the files called build.xml and build.properties into the root of your Java Project .
4. Open the build properties file and update the properties to reflect your specific environment properties.
5. Save the build properties file with your changes.
6. In OEPE, right click on build.xml in the Project Explorer and select Run As > Ant Build... , which opens the Edit Configuration and Launch dialog.
7. Choose the JRE tab and select Run in same JRE as workspace.
8. Choose the Targets tab and select create-domain/recreate-domain. Make sure all of the other options under Name are unselected.
9. Click Apply.
10. Click Run.


By following above steps you create/recreate a domain that you want to use for your ADFE application. By default Glassfish server provides domain1 that can be configured and used as the domain for deploying ADFE applications. You can create a fresh new domain or change the configuration of domain1. One of the settings you may need to change is HTTP listen port of your Glassfish server. By default the server uses 8080, which in many cases has already been used by other apps on your machine e.g. on my machine Oracle XE database listens to 8080.

Secondly you need to set up your domain for ADF Essentials applications. For that you can run build.xml again by selecting install-adfe-full.
If this command does not execute properly or you want to perform steps involved in this process individually (my preferred way) then you need to do as follows:
1. Unzip the core library files into lib folder of your Glassfish domain by running install-adfe-core in build.xml or using unzip -j command
2. Unzip the client libraries into any folder of your choice by running install-adfe-client in build.xml or by using unzip -j command
3. Start Glassfish server by running start-dev-domain in build.xml. Open admin console of Glassfish server using http://<your-host-name>:<glassfish_admin_port>.  Go to Configurations->Server-config->JVM Settings and choose the JVM Options tab and add the following:
 -XX:MaxPermSize=512m (this entry should already be there so just modify it)
 -Doracle.mds.cache=simple


Your Glassfish server is configured for running ADFE applications on it. Now you will need to create a Glassfish server connection in Eclipse using the domain you just configured before starting to create an ADFE application.

Create

Create an ADF application. Go to File > New > ADF Application.


In next window, specify the Application name and Target runtime. You will see the runtime for your Glassfish server if you have already created a server connection in eclipse, otherwise you can create one here. Now select New JPA Project as we need t o create a new JPAproject for our application.


In Create JPA Project wizard, keep default settings and press Next > , keep Java settings as they are and proceed to Java Facet window. Here select EclipseLink 2.5x  as the platform, Glassfish System Library as JPA implementation. Create a new connection by clicking on Add connection and select the type of connection you want to use. In this example we are going to use Oracle XE database. So select Oracle Database Connection and in next window specify the database details. For this tutorial I have created a connection to  HR schema of XE database. Keep rest of the settings as default and press Finish.


The JPA project will now appear in your new application window


Proceeding to next window we need to create a user library which will consist of ADFE client libraries we unzipped in a folder during the configuration process. Click on Manage Libraries, and specify the library name in User Libraries window.




Now Click on Add External JARs.. and naviagte to the folder where ADFE client libs have been extracted. Select and Open all the libraries


After verifying that  all the libraries have been included , Click Ok.
In next window, select the user library created in previous steps. Also select Include libraries with this application . This will make sure the client libraries are deployed as part of your your application EAR


Click Finish and you will see three new projects created in your workspace 
SampleADFEApp : Root project containing EAR contents for the Application
SampleADFEAppModel: JPA project
SampleADFEAppWeb: Web project


To test whether the application has been created properly and it can run on Glassfish  server,  right click on index.jspx in the web project and select Run As > Run on Server. In subsequent windows select the Glassfish server instance that is configured for ADFE and the application to be deployed on it


Monitor server console for any errors in server startup or during application deployment. If everything goes well then index.jspx should come up in your browser.
You should see welcome message on the index page in your browser. If you do not see anything in your browser then open web.xml, which is located at WebContent/WEB-INF and add the following in it:

  <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>

    <dispatcher>ERROR</dispatcher>

  </filter-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>

Now  Clean all the projects and run index.jspx again. You will see the welcome message in your browser.

We will see how to use ADF model layer in an ADFE application to display data coming from database tables in part 2 of this series.

 

Wednesday, November 27, 2013

How to resolve java.lang.IllegalStateException: null windowId in OEPE ADF application


I was getting this exception while trying to run the basic index.jspx page which was generated when I created a new ADF application using Oracle Enterprise Pack for Eclipse 12.1.2.1.1 with Glassfish 4.0

StandardWrapperValve[Faces Servlet]: Servlet.service() for servlet Faces Servlet threw exception
java.lang.IllegalStateException: null windowId
    at oracle.adfinternal.view.faces.webapp.rich.RichWindowManager._storeLoopbackId(RichWindowManager.java:1454)
    at oracle.adfinternal.view.faces.webapp.rich.RichWindowManager.updateRedirectURI(RichWindowManager.java:1234)
    at oracle.adfinternal.view.faces.config.rich.DetectRedirect.redirect(DetectRedirect.java:40)
    at javax.faces.context.ExternalContextWrapper.redirect(ExternalContextWrapper.java:578)
    at javax.faces.context.ExternalContextWrapper.redirect(ExternalContextWrapper.java:578)
    at oracle.adf.view.rich.context.UriManagerBase.addQueryParameters(UriManagerBase.java:44)
    at oracle.adfinternal.view.faces.webapp.rich.UriManagerImpl.addQueryParameters(UriManagerImpl.java:29)
    at oracle.adfinternal.controller.util.AdfvInterfaceImpl.addQueryParameters(AdfvInterfaceImpl.java:205)
    at oracle.adfinternal.controller.state.ControllerState.initializeUrl(ControllerState.java:715)
    at oracle.adfinternal.controller.state.ControllerState.synchronizeStatePart2(ControllerState.java:473)
    at oracle.adfinternal.controller.application.SyncNavigationStateListener.afterPhase(SyncNavigationStateListener.java:59)
    at oracle.adfinternal.controller.lifecycle.ADFLifecycleImpl$PagePhaseListenerWrapper.afterPhase(ADFLifecycleImpl.java:530)
    at oracle.adfinternal.controller.lifecycle.LifecycleImpl.internalDispatchAfterEvent(LifecycleImpl.java:120)
    at oracle.adfinternal.controller.lifecycle.LifecycleImpl.dispatchAfterPagePhaseEvent(LifecycleImpl.java:168)
    at oracle.adfinternal.controller.faces.lifecycle.ADFPhaseListener$PhaseInvokerImpl.dispatchAfterPagePhaseEvent(ADFPhaseListener.java:131)
    at oracle.adfinternal.controller.faces.lifecycle.ADFPhaseListener.afterPhase(ADFPhaseListener.java:74)
    at oracle.adfinternal.controller.faces.lifecycle.ADFLifecyclePhaseListener.afterPhase(ADFLifecyclePhaseListener.java:53)
    at oracle.adfinternal.view.faces.lifecycle.LifecycleImpl._executePhase(LifecycleImpl.java:447)
    at oracle.adfinternal.view.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:202)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
    at java.lang.Thread.run(Thread.java:744)


I compared the configuration of my web project created in OEPE with the one of a normal ADF view-controller project created using Jdev and found out that my web.xml file was mising some settings. Following needs to be added in web.xml to get rid of the exception.

<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>