Rapid J2EE Development with Oracle ADF, Page 2
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
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.