Tuesday, May 19, 2015

Cont. Adf sample app add dept

The common uses of the business components are as follows:
‹ Performing Create, Retrieve, Update, Delete (CRUD) operations
‹ Apply business logic and validations to the database objects
‹ Transaction management
Some of the highlights of using ADF business components are as follows:
‹ Simplified data access
‹ It is based on XML and Java
‹ Works with different application servers and databases
‹ Uses some of the design patterns that are widely accepted
‹ Components are metadata-driven, with optional Java coding
The following are the core components of ADF business components:
‹ Entity objects
‹ Associations
‹ View objects
‹ View links
‹ Application module
Let us consider the EmpDirectoryApplication module, which we created in the previous
chapter. The DeptEO and DeptVO objects will be created in this chapter, which is why it is
not shown with the icons in the following diagram:

What is an entity object?
Remember that the EmpEO.xml file is mapped to the EMP table in HRSchema. What does this really mean? We are representing the database table as an XML file that has a configuration based on the table column and constraint definitions in our application. This is so that we can interact with this particular table using the metadata defined in the XML file. The location of the EmpEO.xml file
In this section, we will create another entity object for the DEPT table from HRSchema. There are two ways of creating an entity object—the first way of creating an entity object was explained in Chapter 2, Getting started with ADF. Now, we will see how to create an entity object using another way.
1. Right-click on the com.empdirectory.model package from the Model project
in the Application Navigator panel where the EmpEO.xml is located and select
the New Entity Object option from the menu.
3. Enter com.empdirectory.model.entity as the package information for the
entity object and DeptEO as its Name, as shown in the following screenshot:


6. In the next screen, the Deptno attribute settings and information related to the attribute can be selected.
7. When you attempt to move to the next screen, a message, which warns the user that there was no primary key attribute selected for the entity object, will be displayed.
8. You can cancel the message and go back to select the Primary Key option for the
Deptno attribute. This option was not checked for EmpEO.xml, hence we could
see a column called Rowid added to the entity object.
Once the primary key option is checked, we can remove the Rowid attribute from
the EmpEO.xml file. In reality, the database table will surely have a primary key
defined and most of the time, Rowid will not be generated.
9. The next screen will provide you with an option to create a Java class for the entity
object. We will not do any coding in Java right now, so we will skip this section and
move forward.
10. The next option will allow you to create a view object and add the view object to
the application module. Select the Generate Default View Object checkbox and
modify the package name from com.empdirectory.model.entity to com.
empdirectory.model.view. Enter DeptVO in the name box.
11. Check the Add to Application Module option to add the view object to the already
existing EmpDirectoryModule module.
You may skip this option to create the view object from scratch by selecting DeptEO
after completing all the steps.
12. Click Next > to accept the summary and click on the Finish button to create the
DeptEO entity object.
What just happened?
We have now created an entity object which maps to the DEPT table. So, we have the entity
object in the same package in which we had created our entity object for the EMP table.
The DeptEO.xml file will have all the columns mapped to the corresponding attributes.
Why association?
As the name suggests, association is used to associate entity objects. When you look closely
at the EMP table, you will find a DEPTNO column. In database terms, it is called a foreign key
because DEPTNO is the primary key for the DEPT table. The foreign key association for entity
objects is done using associations.
The usage of association is exactly as that of a foreign key in a relational database for
maintaining referential integrity. When the database has a foreign key constraint, the
association is created automatically by ADF. The following screenshot explains the
relationship between the EMP and DEPT table through Deptno:


Carry out the following steps to create an association between EmpEO and DeptEO objects:
1. Select the New option from the File menu and select ADF Business Component from the Business Tier.
2. Enter the package name as com.empdirectory.model.association and the name as EmpEOToDeptEOAssoc in the Create Association wizard. The package structure for all these business objects can be predefined from the Tools | Preferences | ADF Business Components | Packages section.
3. Click on the Next > button to create an association between the entity objects. Select the value of Cardinality as * to 1 from the drop-down menu to represent the many-to-one relationship between EmpEO and DeptEO.
4. Locate the Deptno attribute from EmpEO listed in the Source Attribute section
and map it to the Deptno attribute of DeptEO in the Destination Attribute list.
5. After the selection, click on the Add button to add the association entry for the
attributes.
6. Click on the Next > button to select properties for the association. Right now,
you can accept the default settings in the Options for the Business Components
section in this chapter.
7. Click on the Finish button in the Summary section to complete the creation of
the association.
The use of having a view object is as follows:
. To fetch information from the data source using SQL query
. To sort, filter, project, and join data
. To isolate the view data from other parts of the application
Types of view object
We can create a view object from four different data sources; they are entity object,
SQL query, programmatic query, and static list.
‹ View object based on an entity object: A view object will be based on an entity
object for any interaction with the database. The entity object data source is
responsible for updating the data, validating the business logic, and synchronizing
data with the database.
The advantage of using an entity data source is that it will take care of maintaining
consistency in the data that is being fetched from the database, by providing an
entity cache. The EmpVO.xml view object is an entity-based view object, which is
based on the EmpEO entity object.
. View object based on SQL query: The view object based on SQL query is read-only and is not meant for updating the database table. The query will hold all the information that has to be retrieved from the database,
‹ Programmatic query: The view object can be created programmatically using
Java code. In some special cases, we can create a view object customized for
our business needs. The options available in a normal view object will be created
programmatically using the API provided for the creation of a view object.
‹ Static list: Sometimes, we would like to have a list of values displayed in the user
interface for the purpose of selecting a value. The static list can be provided by
the user during the design phase and the list will be displayed in the same order
it was entered as a list at runtime to the user. A static list will not contain any SQL
query, it will contain only the attributes that have to be listed in the UI.
We have the entity object for DEPT table created as DeptEO. Now it's time for us to create
a view object based on this entity object. The view object can be created at the time of
creating the entity object, by checking the option to create the view object in the create
entity wizard. We had skipped that step so that we can create the view object now, using
the following steps:
1. Right-click on the com.empdirectory.model.view package and select the New
View Object… option. The creation of the view object can also be done from File |
New and selecting View Object under the ADF Business Components section.
2. Enter the object's name as DeptVO in the Name box and select an entity object
as the data source in the next screen.
3. In the next screen, you will have to select the DeptEO as the entity object for the view object. Select the Updateable option to make the entity object updateable from the view object,
4. In the next screen, select the Attributes from the entity object to be displayed in the
view object. You can select all the attributes from the Available list and shuttle it to
the Selected list
5. Click on the Next > button to change the Attributes settings for the view object.
Currently, we do not have to make any changes, so we can move forward.
6. The next screen will display the SQL query that will be used to retrieve information
from the DEPT table through the DeptEO object. You cannot change the query if the
data source for the view object is an entity object. You have an option to update the
Where: and the Order By: clause.
The Binding: option will help you to choose the type of binding to be used for
the view object. The Order By: option is provided in order to see the data sorted
column-wise:
7. In the next screen, you can choose to create a bind variable to pass to the view
object query. The bind variable is helpful in retrieving the values from the data
source at runtime. It is used as placeholders in a WHERE clause of a query or in
combination with view criteria.
A view criterion is a named WHERE clause added to the view object which is applied
at runtime based on our business needs.
8. The next screen can be used to generate Java classes for the view object. The Java
classes are used to override the framework code and do some advanced operations
for the view object
9. In the next screen, you can check the Add to Application Module to add the
view object to the application module. Move to the next screen after entering
the application module as EmpDirectoryModule in the Name: box:
10. At the end, a summary screen will be displayed with all the options selected in the
wizard. Once the Finish button is clicked, the view object will be created.
What just happened?
So now we have the view object created for the DeptEO as well:


Importance of a view link
View links are the components to link two view objects. The relationship between view
objects can be one-to-one, one-to-many, many-to-one, or many-to-many. This is similar
to the association component that we have for the entity object. The only difference is
that the view link is used in the presentation layer to fetch data if the child and the
master view object are connected through related attributes.
If we have two view objects that have an entity object as a data source, then we can map
our view link based on the association between the entity objects. View links based on
associations are used to traverse master-detail behavior at the entity level.
A view link can also exist without association by relating two attributes from the view object.
The following diagram will explain the view link relationship between EmpVO and DeptVO:
we will see how to create a view link between EmpVO and DeptVO:
1. Since both the view objects are based on entity objects, we can map the
view link based on the association between EmpEO and DeptEO.
2. Right-click on the com.empdirectory.model.view package and select
New View Link.
3. In the Create View Link wizard, enter com.empdirectory.model.viewlink
as the Package name and EmpVOToDeptVOLink as the Name value:
4. In the next screen, we will select the cardinality as * to 1 which represents a
many-to-one relationship and map the association EmpEOToDeptEOAssoc
between EmpVO and DeptVO.
5. Click on the Add button to add the view link:
6. Click on the Next > button to select the view link accessor attribute for the source
view object and the destination view object.
7. Check the In View Object: DeptVO option to have this accessor attribute created in
EmpVO. This accessor is used to retrieve the related information from the DeptVO:
8. The next two screens will display the source and destination WHERE clause to fetch
the relevant data from the view object, as a result of the view link
9. As usual, the next screen will ask you to add the view link to the application module,
which we will skip this time and discuss in the next section.
10. The last screen will list out a summary of the options selected, and clicking on the
Finish button will create the view link.
What just happened?
We have created a view link between the EmpVO and DeptVO objects. The accessors will be
listed in both the view objects.ve a go hero – drag-and-drop remaining components
Once again, let us try to drag-and-drop the EmpVO and DeptVO view objects and the newly
created view link together onto EmployeeDirectoryDiagram.

No comments:

Post a Comment