Wednesday, June 20, 2012

Creating a Master-Detail Relationship Using Oracle ADF

Master Detail Relationships using Oracle ADF allows you to view data from related tables at same time. we often face a situation in Software Development where we need to display some rows based on a row data from some other table . To put it more simple ,  in a page you have the first table that holds countries information and a table below to it contains the locations information . when you click on a row in countries table then automatically all the locations pertaining to the country should display. There are various ways in various J2EE frameworks provides this functionality. what is so special about ADF in giving this functionality.Yes, it is . ADF Model along with View Controller does many things for you .  Lets see how this works

Pre-requisites:

HR Schema in Oracle database with tables

For those who are new to ADF can go through this link Oracle ADF Model Sample Application download and can find a sample application to start with this post or else can simply download the complete application Oracle ADF Master-Detail Relationship Sample Application.

ADF Model project holds all the View Objects and View Links, and ADF View Controller project holds all the UI Pages like jsff, jspx etc ..

Right Click on mode.entity package under AdfDemoModel and create two entity objects

CountriesEO
LocationsEO

Right Click on mode.viewpackage under AdfDemoModel and create two view objects

CountriesVO
LocationsVO

Note : (when you create EO and VO for Locations table, Association LocCIdFkAssoc, ViewLink LocCIdFkLink will be created by ADF for you)  But why a View link is needed ? Look at the picture





ViewLink is a join condition between two view objects. Read ADF View Link. Now go back to your ADF Application Module AppModule and see the Data Models section

 
 LocationsVO via LocCIdFkLink has come under CountriesVO automatically . This means LOCATIONS table in the Database already has a foreign key reference to the COUNTRIES table and when we try to create a LocationsVO , ADF understood the relation and created a View Link for us . So based on this relationship, Locations Information can be retrieved based on the country id with the use of View Link . we will see this later how did this happen in UI part.

Open model.applicationmodule >> AppModule >> Data Model

Shuttle CountriesVO to the right side.
Being CountiesVO1 selected at right side, shuttle LocationsVO via LocCIdFkLink to the right side
Now shuttle the CountriesVO to the right side in the AM .



 Save All.
 You can test it using  Business component Browser which is useful to check the business logic and relation in your Application without creating any User Interface,Right  click on AppModule and choose Run 

 ..












                             All to set start with building UI Components ..what we need to have in the UI Project.
Right Click on Web Content > > AdfDemoUi From New Gallery , select JSF from categories section which is in the left side and select ADF Task Flow in the right side and click OK


MasteDetailFlow.xml being opened in the editor, drag and drop  View component from Components Palette on to the MasteDetailFlow.xml diagram . Change the name from view1 to MasterDetailView. Double click on view and enter the filename and path as such in the screen shot













Drag Panel Stretch Layout from Layout under Component Palette on to the jsff page we just created. Now Drag LocationsVO1 under CountriesVO1 and drop on to the jsff page and select ADF Master Table ,  Detail Table.
Save All..









     In Order to run this , we need to have jspx page . we cannot able to run jsff pages bcoz it is not a runnable target for ADF Controller. so I created a jspx page called MasteDetailMain.jspx under pages package,drag & drop the MasterDetailFlow.xml on to the jspx page as a Region . Save All . Now Right Click on the page and Run











Click on any country in the countries table to get its  locations only in the locations table.Later you can customize your page to look rich like labeling with proper header names, surrounding with table borders and many more . 

Logical Questions : 

How did the Detail table rendered based on the Master Data or How Master-Detail Table Relationships works in ADF ?

The  Parent tag of the detail component automatically has the partialtriggers attribute set to the id of the master component . At Runtime the partial trigger attribute  causes only the detail component tobe rendered when the user makes a selection  in the master component . In our MasterDetailView.jsff source

<af:table partialTriggers="::md1"
                    rows="#{bindings.LocationsVO1.rangeSize}"
                    fetchSize="#{bindings.LocationsVO1.rangeSize}"
                    emptyText="#{bindings.LocationsVO1.viewable ? 'No data to display.' : 'Access Denied.'}"
                    var="row" value="#{bindings.LocationsVO1.collectionModel}"
                    rowBandingInterval="0"
                    selectedRowKeys="#{bindings.LocationsVO1.collectionModel.selectedRow}"
                    selectionListener="#{bindings.LocationsVO1.collectionModel.makeCurrent}"
                    rowSelection="single"
                    binding="#{backingBeanScope.backing_pages_MasterDetailView.t1}"
                    id="t1" inlineStyle="width:500.0px;">




where md1 is the ID of parent table , so whenever user makes a selection in the master table will also cause the detail table to change which is called partial rendering 

what if i want to have ADF Master - Detail on separate pages ?

Create two jsff pages . One page contains master table and another contains child table . Create a button in the master table which navigates to the child page on the button click and Vice Versa. You may wonder how do we get the Child records based on a row selected in the master table which is in diff page. Yes ADF does automatically based on the partial triggers set . You do not need to set any variable or attribute in some scope to retrieve information.




No comments:

Post a Comment