Oracle Virtualization and Cloud Consulting
view counter

Creating a Data Capture Form using EJB + JPA and ADF Binding

Thanks to Jobinesh Purushothaman for this story

Java EE technology stack has been improved tremendously in recent past. However, a closer look at this technology spectrum reveals the visible gap that exists between the UI and Service layers. Core Java EE stack really lacks a smart glue layer that avoids large chunk of boiler plate code used for wiring UI with business data. For a typical Java EE business application, we can say that about 20-25% coding effort is being spent for wiring UI to the data from business services and other common UI manipulations such as synchronizing master-child relations, querying entities and displaying results, navigating between records, invoking actions etc.

Oracle Virtualization and Cloud Consulting
view counter

Binding the UI with EJB

Is there any matured binding solution available for Java EE technology stack?
Answer is YES, Your friend is ADF binding layer who can really help you to bridge the gap between UI and business service layer.

Seeing is Believing

So let us try building a data capture form using Java EE technology stack with the help of JDeveloper + ADF Binding Layer , and see how this toolset saves you time and effort.

Let us take the classic example of Employee and Department. The use case is to realize a data capture screen for an Employee. UI may look like as shown below

The example below illustrates how JDeveloper help you to build Java EE applications quickly and efficiently.

Setup the application environment

Step 1: Create a new application by choosing Java EE Web Application template.

Please make sure that required libraries are selected in subsequent steps of the same wizard. Click Next and finish the wizard. This wizard automates the 'setup' of the basic application structure (I would say, architecture rather) for you with clear separation between UI and Service Layer. You can see two projects generated at this stage 1. ViewController (UI) 2. Model (Business Services)

Step 2: Create a new Database Connection. This example uses HR schema that comes with Oracle database

Generate entities from tables

Step 3: Select the Model project, right click, and select New from the context menu. From the dialog, select the option for generating 'Entities from Table'. Select DEPARTMENTS and EMPLOYEES tables in the next step and continue the wizard with default values. Click to Finish at the end. Wizard generates Employees and Departments entities and the required configuration files as well, like persistence.xml file.

Define entity validation declaratively

Step 4: ADF Binding layer provides declarative validations support for the enterprise applications. Binding layer also supports, UI hints for entity attributes which can be later leveraged by the view layer to derive the runtime characteristic of UI elements.

Let us define the validation for email of an Employees entity. Open the Employees.xml in design mode, choose attributes tab and select employee attribute. You can see validators section displayed at bottom. Click on the + icon to add validation. Select ‘Regular Expression’ as Rule type. From the 'expression' drop down list, select Email Address. Now go to the failure handling tab and key in the error message.

Define UI Hints

Step 5: UI Hints defines hints for the attribute which can be later used by the view layer to derive the UI characteristics like filed label, length, precision, required flag etc. Select the entity attributes and specify values for Label, Tooltip, Display width, Precision etc using Property Inspector

Generate service layer (session facade)

Step 6: We done with the model part.  Let us go ahead and generate service layer for the model. Select the persistence.xml file (displayed under META-INF folder of Model project), right click and select the option to create Session Facade. Continue the wizard with default options till the end. This generates SessionEJBBean and other supporting files(Local and Remote interfaces and related configurations). SessionEJBBean will contain the basic infrastructural service methods (generated by the wizard, off the shelf) for performing CRUD operations on the containing entities.

Create accessor method for entity

Step 7: This accessor method will be used later, to retrieve the instance of the entity from the client .
Open the SessionEJBBean, write down the method public Employees getEmployees() as shown in the below diagram. Copy the method declaration to Local and Remote interfaces(SessionEJB.java and SessionEJBLocal.java).This method provides the Employees entity instance that backs up the data capture form.

Generate Data Control

Step 8:  Select SessionEJBBean, right click and select Create Data Control option from the menu

Create UI for the application

Step 9: Create a jspx page. Drag and drop the employees method accessor from the Data Control pallet to the jspx file as a Form. Now the form is ready, let us replace the departmentId field with a SelectOneChoice component, see next step.

Step 10: Define List binding for department. Objective is to support the department filed with a SelectOneChoice component. Create List binding by selecting departmentsFindAll method accessor.

pagedefinition entry :

<list ListOperMode="navigation" 
IterBinding="departmentsFindAllIterator"
ListIter="departmentsFindAllIterator" id="departmentLOV"></list>

Step 11: Make changes in jspx file to add a <af:selectOneChoice> for department, as shown below.

 <af:selectOneChoice value="#{bindings.departmentId.inputValue}"
label="#{bindings.departmentId.hints.label}"
required="#{bindings.departmentId.hints.mandatory}"
showRequired="#{bindings.departmentId.hints.mandatory}"
shortDesc="#{bindings.departmentId.hints.label}}"
id="soc1" valuePassThru="true">
<af:forEach items="#{bindings.departmentLOV.iteratorBinding.allRowsInRange}"
var="li1">
<af:selectItem id="si2"
label="#{li1.dataProvider.departmentName}"
value="#{li1.dataProvider.departmentId}"/>
</af:forEach>
</af:selectOneChoice>

Persist Enity

Step 12: Drag persistEmployees(Employees employees) method action as command button on the Form. Specify the parameter value as #{bindings.employeesIterator.currentRow.dataProvider}

That’s it, job is done without much effort. Now you can try running the page from JDeveloper. Sample workspace is attached below.

A couple of points:
1. Please note that accessorIterator for the employees needs to be refreshed during each page load. So  added a PageController ,to rexecute the IteratorBinding when page loads.
2. Modified the generated source of SessionEJBBean::persistEmployees(Employees employees) to get the reference of associated Departments.

You can download the sample workspace from here.

[Runs with Oracle JDeveloper 11g R1 PS1 + HR Schema]

Read the entire article at its source

view counter