DatabaseRapid J2EE Development with Oracle ADF

Rapid J2EE Development with Oracle ADF

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

In a push toward simplified J2EE, vendors have been unveiling rapid application development (RAD) tools at a furious pace. Today, for example, most integrated development environments (IDEs) provide wizards to generate the mundane and repetitive framework code necessary when building Model-View-Controller (MVC) applications. A good framework provides an easy, consistent (and preferably declarative) way to configure application components together. That framework should also enforce a ‘clean seperation of concerns’ that will enable you to switch the implementation for an individual layer without affecting the rest of the application. The Oracle Application Development Framework (ADF) is based upon J2EE design patterns, features from other proven frameworks such as Struts, and concepts borrowed from its own previous framework, Business Components for Java (BC4J). The goal of ADF is to conceal the complexities of J2EE, enabling developers to focus on business logic.

This article demonstrates how the ADF framework can be utilized to design a Web-based application. You will use the JDeveloper 10g IDE from Oracle. You can download a free version of it here. I’m also using the popular, open-source MySQL database that can be downloaded here. If you prefer to use Oracle’s own database, the adjustments that you will need to make should be trivial.

Overview of ADF

In an effort to keep things simple, I want to avoid a detailed discussion of ADF. There are a number of existing demos and articles at the JDeveloper site. However, a high-level explanation of ADF components, and how they fit into the MVC architecture, is in order.

Model

Entities

An ADF Entity object represents a business entity. This usually translates to a row of data in a table, and each Entity attribute represents a column in that table.

In ADF, an Entity handles other tasks, such as business validation and format masking. In most cases, the model is the most logical place to address these concerns because it will enable maximum reuse. Validations, predicated by business requirements, are likely to be relevant no matter how the model is used. For example, verifying that an employee is always assigned to one and only one department will not change if the application were converted from a Web application to a Swing application. ADF will propagate these validations and formats to the controller and view, alleviating the need for you to address these issues elsewhere.

Views

An ADF View object exposes an Entity object to users of the application. An Entity may be accessed in numerous places, but the context in which it is used will vary. Filtering, sorting, and object relationships can be established by a view object, ensuring that the model is represented in a way that makes the most sense.

Application Module

The ADF Application Module bundles together View components, while providing access to your business services layer, which might use technologies such as EJB to manage your database. The Application Module provides ADF Views with transaction support, and other important data-centric services.

View and Controller

Nothing in your model should dictate a particular view technology. An Application Module can serve as the foundation for a Swing or Struts Web application. If developing a thick client application, Swing components might communicate directly with the application module. When developing a Web-based application, your application will use a clearly defined controller to manage model-view interaction.

Controller

In previous releases, ADF provided its own controller. That controller has since been replaced in favor of Struts. Struts uses an XML file, called struts-config.xml, to handle application requests and dispatch them to designated request handlers. Experienced Struts developers will appreciate the ability to leverage this existing knowledge.

View

JDeveloper makes it easy to visually design pages using JSP and JSTL. ADF also provides its own view framework, called UIX, that offers a set of HTML widgets for generating quick and consistent-looking view pages. You will be using UIX in this demonstration. UIX components will become the foundation for Oracle’s JSF (JavaServer Faces) standard implementation in future releases.

Building a Sample Application

The application you will build allows employees to reserve conference rooms for work-related meetings. As I mentioned earlier, a MySQL database is used to create two tables, conference_rooms and room_reservations. The DDL to create these tables is listed below.

DROP TABLE IF EXISTS conference_rooms;

CREATE TABLE conference_rooms
(
   room_id           INTEGER NOT NULL AUTO_INCREMENT,
   room_name         VARCHAR(32),
   number_of_seats   INTEGER,
PRIMARY KEY (room_id)
);

DROP TABLE IF EXISTS room_reservations;

CREATE TABLE room_reservations
(
   reservation_id    INTEGER NOT NULL AUTO_INCREMENT,
   room_id           INTEGER NOT NULL,
   reservation_start DATETIME,
   reservation_end   DATETIME,
   reservation_team  VARCHAR(50),
PRIMARY KEY (reservation_id)
);

Start JDeveloper if you have not already done so. On the left, you will see the Applications-Navigator. Click the Connections tab to establish a connection to your MySQL database. Before doing this, however, it is necessary to make the MySQL JDBC driver available to JDeveloper and the embedded OC4J server you will use to run the application. You can do this by dropping the driver .jar file into the j2eehomeapplib directory of your JDeveloper home (ex. C:jdevj2eehomeapplib). Once complete, perform the following steps in the Applications-Navigator:

  • Select Database. Right-click, and choose New Database Connection… Click Next.
  • Enter ‘MySQL’ for Connection Name and select ‘Third Party JDBC Driver’ for Connection Type. Click Next.
  • The authentication page appears. Enter the username and password for your MySQL database. Check the Deploy Password checkbox, and then click Next.
  • Next, select the Driver Class ‘com.mysql.jdbc.Driver’ and enter a URL of ‘jdbc:mysql://localhost/{database name}‘. Click Next.
  • Click the Test Connection button. If connected, you will see ‘Success’. Click Finish.

Building a Model

To create an application in JDeveloper, right-click the Applications tab in the Applications-Navigator. Select New Application Workspace, and then type ‘RoomReservations’ for the Application Name. Leave the Application Template set to ‘Web Application [Default]’.

These actions will create a new application based upon a template that contains two folders: ‘Model’ and ‘ViewController’. As their names suggest, they will contain the model of your MVC application, and the Struts controller and presentation pages respectively.

First, you need to create two ADF Entity objects, based upon your MySQL tables.

  • Right-click Model, and select New.
  • Under the Business Tier category, select Business Components.
  • From the list of items, select Entity Object. Click OK.

The next wizard screen asks you to establish a database connection to obtain the schema objects.

  • The first page should ask you to connect to your MySQL data source. Click Connect.
  • Continue to the Create Entity Object window. From Database Objects, select the conference_rooms table.
  • Click the Next buttons until you arrive at a screen for Attribute Settings. Attribute RoomId should be selected. The ADF wizard must know the object’s identifier. Check the Primary Key box and click Next.
  • Proceed through the wizard to Finish.

You’ve created a Entity object entitled ‘ConferenceRooms’ in your model. All very straightforward. You need a second object representing the room_reservations table. Using the previous steps as a guide, create the RoomReservations model. When you reach the Attribute Settings screen, designate reservationId as the primary key.

Now that you have your entities, you need View Objects to expose them to the rest of the application. As you’ll recall, an ADF View exposes Entity objects to the rest of the application. A single ADF Entity may require several View objects in non-trivial applications.

  • To create a View object for ConferenceRooms, right-click Model again and select New.
  • Under the Business Tier category, select Business Components.
  • This time, from the list of items, select View Object. Click OK. Click Next.
  • In the Create View Object window, name this view ‘ConferenceRoomsView’. Click Next.
  • The next page of the wizard asks you to select an Entity for the view; select ‘ConferenceRooms’. Click Next.
  • On the next screen, select all available attributes for this ADF View.
  • Click Next and continue on until Finish.

Repeat the previous steps to create RoomReservationsView to front your relevant ADF Entity, RoomReservations. This time, however, stop when the wizard displays the View SQL generated by the ADF framework.

  • In the ‘Query Clauses’ section, click Edit… to edit the ‘Order By’ for the view.
  • Select ‘RoomReservations.reservation_start’ from the list to sort the contents according to start date.
  • Because you’ve altered the view, it will ask you to test the query to insure correctness.
  • Click Next and proceed to Finish.

You would like to present users with a master-detail view of rooms and current reservations. Tools such as Oracle Forms provide built-in support for such features. However, support for master-detail forms has been lacking in Web development frameworks. Luckily, the ADF framework makes it easy. By following these next steps, you will establish a link between your Views to allow ADF to co-ordinate activity between them.

  • Right-click Model. Select New.
  • Choose View Link from the available Business Components. Click Next, and then Next again.
  • Name your View Link ‘RoomReservationsViewLink’. Click Next.
  • From the left panel, expand ConferenceRoomsView and highlight attribute RoomId.
  • From the right panel, select RoomId from RoomReservationsView. Click Add.
  • Click Next to see the SQL join that was generated as a result. Proceed to Finish.

There is but one piece remaining for your application’s model: an Application Module to tie the ‘M’ in your MVC application to the business services layer.

  • In the Application Navigator, under Model, Application Sources, right-click the model’s package and select New Application Module. Click Next.
  • Name this Application Module ‘RoomReservationsAppModule’. Click Next.
  • The next window asks for the Views relevant to RoomReservationsAppModule. On the left pane, select all three ADF View components: ConferenceRoomsView, RoomReservationsViewLink beneath it, and RoomReservationsView. These actions will create View instances for each of them in the Application Module.
  • Proceed to Finish.

Model in the JDeveloper Application Navigator

View/Controller

At this point, you’ve not committed to a particular view technology. RoomReservationsAppModule could easily serve as the basis for a Swing application. Instead, you will create a Web-based application. ADF utilizes the Struts framework to implement an application controller. As for the view, you are not really limited here. However, JDeveloper provides numerous features to visually edit JSP (JavaServer Pages) or UIX pages. YOu will use UIX technology.

The first thing to do is to create a page and specify it in struts-config.xml.

  • In the Application Navigator, under ViewController, Web Content, double-click struts-config.xml.
  • In the Component Palette on the right, highlight Data Page and drag it onto your empty Struts Data Flow Editor.
  • Rename this new object to ‘/main’; then, double-click it.
  • From the list of options, select ‘/main.uix’ as the page name to designate this as a UIX component. Click OK.
  • The Data Control Palette should appear on the right now, replacing the Component Palette. (If not, Ctrl+Shift+d to select it.)
  • Expand the tree that appears to see the View instances you assigned to your Application Module.
  • Expand the node for ConferenceRoomsView1. Highlight RoomReservationsView1 under it.
  • From the Drag and Drop As: box, select Master Detail (One to Many).
  • Drag the selected RoomReservationsView1 onto the dotted box in the editor showing main.uix.

With a few clicks of the mouse, you’ve created a simple master-detail page. Notice how navigation buttons and pagination components have been automatically added for you. Later, you can customize your view: modify labels, add a look and feel, and so forth. By default, ADF applies the ‘BLAF’ (browser look and feel). You can change the look and feel by editing the uix-config.xml file. However, I won’t go so far as to address these issues. Just make a few simple cosmetic changes, and then run the app.

  • Highlight the master block in main.uix. Beneath the Data Control Palette, you should see the Property Inspector window.
  • Change the text from ‘Master’ to ‘Conference Rooms’.
  • Change the text for the ‘Detail’ block to ‘Current Room Reservations’.
  • Click the Components tab to show the Component Palette. ‘All UIX Components’ should display with a collection of HTML widgets to add to your UIX page. Select image and drag it above the master block.
  • Select any Web-compatible graphic from your system to add as a logo to the page. I added a very simplistic logo.
  • JDeveloper asks if you want to add the image to your application root. Do so.

You can run this application with the embedded OC4J server provided with JDeveloper by right-clicking ‘/main’ in the Struts Data Flow Editor, and selecting Run. Not the prettiest of pages, and there are a few superfluous items, but these are minor issues that can be addressed later.

Making New Reservations

The current page displays current reservations in a tabular format. Next, you will create a page to make new reservations.

  • From the Component Palette, drag a second Data Page onto the Struts Data Flow Editor.
  • Name this component ‘/newReservations’.
  • Double-click and select the value ‘/newReservations.uix’. Click OK.
  • In the Data Control Palette, select the RoomReservationsView2 instance (the one not beneath ConferenceRoomsView1).
  • Select Input Form and drag it onto the page to create an input page.
  • Because ReservationId is auto generated, remove this binding by highlighting it in the editor and pressing delete.

UIX date controls were added for your ReservationStart and ReservationEnd fields. Now, add some Struts logic.

  • From the Struts Page Flow group in the Component Palette, select Action.
  • Drag Action onto the flow diagram to create a new Struts Action.
  • Name this Action ‘NewReservationAction’, and then double-click the Action to create a source file for it.
  • Close the code editor that appears containing the new Struts Action.
  • Select Page Link from the Component Palette.
  • Click ‘/main’ in the diagram, and then the Struts Action ‘NewReservationAction’ to create a link.
  • Select Forward from the Component Palette.
  • Click Action ‘NewReservationAction’ in the diagram, then ‘/newReservations’ to create a Struts Forward from this Action to the form.
  • In the editor, highlight the Forward named ‘success’ and change it to ‘create’.

You have created some Struts mappings between the two pages of your application. You did not enter any custom code to your Struts Action at this time. If you return to the editor for main.uix, you will see a new button that the framework generated as a result of the mappings you created.

  • In the Property Inspector, change the button text to ‘Add New Reservation…’

Based upon your work thus far, rerunning the application should produce a page like the one below. Try clicking the ‘Add New Reservation…’ button. You should be taken to your page where you can input a new reservation.

Initial page of our application

On the main page, notice a column entitled ‘Select’. ADF provides a nice way to select a record and pass that record’s unique identifier along with the request. You would typically use this to give context to an edit or delete request for a particular entry. This demonstration will only cover creating new reservations, so you can delete this field if you wish.

Your application is used to reserve conference rooms. Typically, reservations might last an hour or two, but not the entire day. Viewing the data, however, it becomes apparent that you have a big problem: By default, the format mask for start and end times do not display time. In addition, now that users are able to make reservations, you need to address the issue of validation. You must ensure that users 1.) Only make reservations for a future date and 2.) Enter an end time greater than the start time.

As was mentioned earlier, the ADF framework allows you to specify this type of information in your Entity objects. To do this, you need to modify the model of your application, and this makes perfect sense in this case. It is likely that you will always want the time displayed and you most definitely will want to enforce those validations. Now, return to the model and address the formatting and validation issues just described.

  • In the Applications – Navigator, right-click the RoomReservations Entity and open it for editing.
  • Expand Attributes; highlight ReservationStart.
  • Select the Control Hints tab.
  • Select a Format Type of ‘Simple Date’.
  • Select a Format of ‘MM/dd/yyyy HH:mm’.

Add identical formatting for ReservationEnd. Next, add the validation logic.

  • With RoomReservations still open, select Validation.
  • Highlight ReservationStart and click New….
  • You are creating a ‘CompareValidator’, so leave the Rules as is.
  • Select ‘GreaterThan’ for Operator.
  • Select ‘Query Result’ for Compare With.
  • Select the current system date using MySQL syntax by entering ‘SELECT NOW();’ in the Enter SQL statement field.
  • Error Message should contain the text: ‘Start time must be greater than the current time!’

You have ensured that reservations can only be made for future dates. Follow these steps to ensure that any end time specified is greater than the start time:

  • Select Validation.
  • Highlight ReservationEnd and click New….
  • Leave Rules and Operator as is (‘CompareValidator’ and ‘GreaterThan’).
  • Instead of ‘Query Operator’, select ‘View Object Attribute’ for Compare With.
  • Under Select Attribute, select ReservationStart from RoomReservationsView.
  • Error Message should contain the text: ‘End time must be greater than the start time!’

When you created a link to your Struts Action, it created a button on the master-detail page to lead you there. It is usually a good idea to channel all requests to your Struts controller. However, if you just wanted to create a simple navigation button, you can do this easily. You need some sort of navigation from your input form, back to the main page, so you will add such a button.

  • Click the Components tab to show the Component Palette. Go to the ‘All UIX Components’ section.
  • Select Button and drag it onto the editor for the ‘newReservations.uix’ page.
  • Set the button label to ‘View All Reservations’ in the Property Inspector.
  • In the destination property, select main.do from the drop-down to create a link back to the first page.

After applying these changes, run the application, and attempt to create some reservations. Select dates and times that violate your validation rules. An attempt to set the end time field to a time prior to the start time should result in an error message like the one below.

Validation Error when Making a Reservation

Conclusion

So, what have you accomplished? You have created a MVC-based J2EE application, in very little time, without a single line of Java code. By using a MySQL database, the ADF framework allowed you to create a fully-functional reservations system that was deployed to Oracle’s OC4J server. As a final note, ADF-developed applications are not confined to Oracle’s application server. JDeveloper provides support for installing the ADF libraries and packaging and deploying a complete ADF application to another J2EE application server such as JBoss.

Oracle ADF provides a RAD environment for developing J2EE applications in a fast, efficient manner.

About the Author

Michael Klaene is a Senior Consultant with Sogeti LLC. He has spent over 9 years in Information Technology and is an experienced Systems Analyst, delivering solutions that involve numerous technologies, such as J2EE and .NET.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories