Sie sind auf Seite 1von 37

Lab 2: ADF Business Components

(60 minutes)
Objectives
At the end of this exercise, you will be able to:
Create ADF Business Components(Entity Objects, View Objects and Application
Modules)
Add some validation rules
Add a calculated field
Use a Database Sequence as a primary key
Set default values for the fields
Set the UI hints for the fields
Use list of values
Overview
Oracle ADF Business Components (ADF BC) is one of the major components of Oracle
ADF that simplifies database access from your application.
Through this lab you will walk through the different features provided by ADF BC and
see how it can help you to build your application.
Detailed Instructions
Step 1 Creating a new ADF Application
The first step is to create a new ADF Application. Oracle ADF can be used to build Web,
Desktop and Mobile application, but for this workshop we will use it to build a Web
Application:
1. Open Oracle JDeveloper
2. From the File menu select New

3. From the New Gallery screen select the Applications category from the left
side and from the right side select Fusion Web Application(ADF)
Fusion Web Application templates will help us to create a new ADF web
application; by default this template will create 2 projects within this application,
one for the model and one for the UI.

Oracle Sales Consulting SWAT

4. Now we need to complete the new application wizard:


a. For the Application Name enter HR_APP and click Next

b. The next step will ask for the details of the model project, leave it as is and
click Next, you can see that ADF Business Components is currently
selected as one of the project features.

Oracle Sales Consulting SWAT

c. The next screen will ask if you want to change the Java Settings of your
model project. It is recommended to change the package to something
more unique to your application. For this workshop we will change the
package to adfbasics.hr.model and click Next.

d. The next screen will ask for the details of your UI project, keep it as is and
click Next.
e. The last screen will ask if you want to change the Java Settings of your UI
project. It is recommended to change the package to something more
unique to your application. For this workshop we change the package to
adfbasics.hr.view and click Finish.
f. Using the Application Navigator , you will see the your new application
and the sub projects within :

Oracle Sales Consulting SWAT

Step 2 Creating the Business Components


In this step youll create the business components in order to access the HR schema, we
need to have read/write access to the employees and departments and read access to the
Jobs, so basically we need to create the following:
Entity Object:
o Employee
o Department
Updatable View Objects :
o Employees
o Managers
o Departments
Read-only View Objects
o Jobs
Application Modules
o HR Application
Oracle BC allows creating all of the objects above using one wizard, or we can create
each type separately using its specific wizard.
We will use the Business Components from Tables wizard to create the entities, the
updatable views (except for the Managers view) and the application module, and for the
Jobs and Managers we will use the View Object wizard.
1. Creating the Entities, Updatable views and the application module:
a. Right Click on the Model project and select New to open the New
Gallery Window
b. Invoke the Business Components from Tables wizard

Oracle Sales Consulting SWAT

c. Since we need to connect to the database you need first to configure the
DB connection, but as you remember we have already configured the HR
Connection in the previous lab, so we need just to import the connection to
our current application. Click on the glass icon
HRConn to your project:

and import the

d. When youre done , click Ok to start building your objects


e. The first step will ask you to create the entity objects, we will put all of
our entities in their own package, so change the package name to
adfbasics.hr.model.entities
f. Click Query to retrieve the available tables in the HR schema
g. From the available tables , select the DEPARTMENTS and EMPLOYEES
and move them to the right side

Oracle Sales Consulting SWAT

h. From the Selected Objects, Select DEPARTMENTS and rename it to


DeptEO

i. Do the same for the EMPLOYEES and rename it to EmpEO and Click
Next when you are done.
j. Now we will create updatable views(entity based views), we will put all of
our views in their own package, so change the package name to
adfbasics.hr.model.views
k. Select EmpEO and DeptEO and move them to right side
l. Rename DeptEOView to DeptsView and EmpEOView to EmpsView
.Click Next twice(we will skip the Query-Based views step) when youre
done
m. The last thing is to create the Application Module, set the package to
adfbasics.hr.model and set the Name to HRModule. Click Finish when
youre done .

n. Your project should look like this:


Oracle Sales Consulting SWAT

2. Creating the Query-Based View


a. Right click on the views package and select New View Object

b. Fill-in the details of the view object and click Next

Oracle Sales Consulting SWAT

c. In the next screen , click on Query Builder to select the JOBS table,
Click OK when youre done

d. Click Next until you reach Step 8 , by default the wizard will try to create
new application module to add the new view object to it, however we
already have the HRModule, so no need to create a new App. Module.

Oracle Sales Consulting SWAT

3. Creating an Entity-Based View using the New View Object wizard


In this step we will build a view object that returns a filtered list of employees
who can serve as the current employee's manager. For the sake of this exercise,
you can assume that the list of available managers for a given employee is the list
of other employees in the same department, including any employee without a
manager (such as the president).
We will use this view object to build a list of values for the ManagerId attribute in
the EmpsView
a. Right click on the views package and select New View Object

b. Fill in the details of the Managers view and click Next

Oracle Sales Consulting SWAT

c. In the Entity Objects page, select the EmpEO entity object from the
Available list, click the Add button to shuttle it into the selected pane, and
click Next.

d. Since were going to use this view to build a list of values, we only need
the EmployeeId and the LastName , when you finish click Next Twice

e. In order to filter the result , we will use the following where close:
(DEPARTMENT_ID = :CurrentDept OR MANAGER_ID IS NULL)
AND EMPLOYEE_ID ! =:CurrentEmp

Oracle Sales Consulting SWAT

10

f. In the Bind Variables page, create two new variables called CurrentDept
and CurrentEmp (which are both referenced in the WHERE clause) and
set both variables to be of type Integer.
Click Finish when you are done to create the view object.

4. Testing your Business Components:


a. You can test your business components using ADF Model Tester, to start
the tester , right click on your HRModule and select Run

Oracle Sales Consulting SWAT

11

b. Explore the different features provided by the Model Tester

c. Notice that the Manages view is missing, why?!

Oracle Sales Consulting SWAT

12

Step 3 Validation Rules


Now that the basic model layer objects are in place, we will add some validation rules to
your entity objects, remember that the entry objects are responsible for the DML
operations, so its the place where you need to define your validations.
We will add the following rules to the Employees Entity Object (EmpEO):
1. Ensure that the email field is well formatted
2. The salary has to be greater than 1000
3. If the employees department ends with S , the salary has to be a multiple of 5
1. Email Format check using Regular Expressions
a. Add a new validation rule to the Email attribute of the EmpEO entity

b. Select Regular Expression as the Rule Type and then select Email
Address from the predefined Expressions

Oracle Sales Consulting SWAT

13

c. When you click OK, the wizard will tell you that you forgot to define the
error message for your rule. So select the Failure Handling tab and
define an appropriate error message.

2. The minimum salary rule using Compare Validation Rule


a. Add a new validation rule to the Salary attribute of the EmpEO entity
b. Select Compare as the Rule Type and then select Greater than as
the operator and set the value to 1000

Oracle Sales Consulting SWAT

14

c. Go to the Failure Handling tab and enter the following validation error
message in the Message Text box:
The {attribute} of {value} must be greater than 1000
In the Token Message Expressions table:
Click the row for the attribute token and double-click the Expression cell
in that row. Enter the expression source.hints.Salary.label to reference
the value of the user-friendly display label for the Salary attribute of the
source entity object being validated.
Similarly, enter the expression newValue for the value token to reference
the new value of the Salary attribute.

These message expressions use Groovy syntax. Although these are very
simple expressions, it's important to understand that developers can
leverage the full power of Groovy when necessary.
To finish, click OK to define the new rule.

3. The Salary and the Department Validation Rule: If the employees


department ends with S , the salary has to be a multiple of 5

Oracle Sales Consulting SWAT

15

The first two rules are called attribute-level validation rules,this time we will
define a validation rule at the entity level; entity-level validation rules are rowlevel validation rules.
To build this rule we will use the method validation rule.
a. Since this is a Java entity, we need first to generate the Java Class for our
entity

b. Do the same for the DeptEO entity


c. Add new entity validator to the EmpEO:

d. Now you need to define the validation rule details , you will select the
type of the validation rule and define the error message, but the validation
rule code will be done inside the Java class of your entity.
For the error message please enter the following:
If the department name ends with "S" then Salary must be a multiple
of 5

Oracle Sales Consulting SWAT

16

e. Using the Validation Execution tab , set the Salary and the DepartmentId
as triggering attributes for this rule:

f. The next step is to open the Java class and write your code

Oracle Sales Consulting SWAT

17

g. Copy the following code to your class, note that we dont have an attribute
called DepartmentName in the employees entity, so we had to access the
associated department entity (by using the getDeptEO1 , DeptEO1 is the
accessor to the departments entity) to get access to the DepartmentName .
if(this.getDeptEO1().getDepartmentName().toUpperCase().endsWith("S"
) & this.getSalary().intValue() % 5 != 0)
return false;
else
return true;
h. Save your work by clicking the save all icon

Oracle Sales Consulting SWAT

18

4. Test your rules, run the HRModule and test the 3 rules that you have created.

Step 4 Calculated Fields


When you add a calculated field its better to do it at the entity level to make it more
reusable. So in this step we will edit the EmpEO entity object and add a new transient
(calculated) attribute called TotalComp.
The attribute will be the sum of the employee's salary and that person's commission (as
defined by the Salary and CommissionPct attributes, respectively). In the formula, you
must take into account that the CommissionPct and Salary might be null.
1. Edit the EmpEO to add a new attribute and name TotalComp

Oracle Sales Consulting SWAT

19

2. Using the attribute editor to update the details of the TotalComp attribute, and in
order to calculate the value , please use the following groovy expression:
(Salary!=null?Salary:0)+((Salary!=null?Salary:0)*(CommissionPct!=null?Co
mmissionPct:0))
This formula uses a ternary operator that tests a Boolean condition (Salary!=null)
to return the value of Salary if it is not null and zero otherwise. It then performs a
similar calculation on the value of CommisionPct, and multiplies it by the Salary,
then returns the sum of both calculations.

3. In order to recalculate this atribute every time we change the salary or the
commission, we have to make this attribute dependant on the base attributes:

Oracle Sales Consulting SWAT

20

4. Save your work


5. The last step is to add this attribute to the EmpsView , as we wont be able to see
this attribute unless we use it in a view object.
In the Application Navigator, double-click the EmpsView. In the editor, select the
Attributes tab and click the Add Attribute from Entity

6. Save your work and test it.

Oracle Sales Consulting SWAT

21

Step 5 Use a Database Sequence as a primary key


In this step we will use a database sequence in order to populate EmployeeId attribute
automatically.
In order to achieve this, we have more than one option:

If you know that the underlying column value will be updated by a database
trigger during insert or update operations, you can set the type of your attribute to
DBSequence, and you can enable the respective Refresh on Insert or Refresh
on Update checkboxes on the Details tab to ensure that the framework
automatically retrieves the modified value and keeps the entity object and
database row in sync.
In this case, ADF will generate a negative value for your attribute until you
commit the record to the database.
For More Information, please read the following section in the developer guide:
4.10.10 How to Get Trigger-Assigned Primary Key Values from a Database
Sequence

Sometimes you may want to eagerly allocate a sequence number at entity row
creation time so that the user can see its value and so that this value does not
change when the data is saved. To accomplish this, use the SequenceImpl helper
class in the oracle.jbo.server package in an overridden create() method to get the
next sequence number and use it as the EmployeeId whenever you create a new
record. But this might leave you with gaps in your database sequence.
For More Information, please read the following section in the developer guide:
8.3.1.2 Eagerly Defaulting an Attribute Value from a Database Sequence

Use a programmatically generated sequence number instead of database sequence.

In this exercise we will be using the second approach to generate the EmployeeId from
the database sequence EMPLOYEES_SEQ:

1. Open EmpEOImpl.java and locate the create() method:

Oracle Sales Consulting SWAT

22

2. add the following lines of code under the super.create(attributeList):


SequenceImpl sequence = new
SequenceImpl("EMPLOYEES_SEQ",getDBTransaction());
setEmployeeId(sequence.getSequenceNumber().intValue());
When you are prompted to import SequenceImpl , enter Alt+Enter to import
SequenceImpl (oracle.jbo.server) class.
Your code should now look like this:

3. Save and test your work:

Oracle Sales Consulting SWAT

23

Step 6 Using the system date as a default value for the date
Oracle ADF provides many ways to set the default value for the attributes; you can use
the attribute editor to define the value declaratively or you can use the object java class to
define the value programmatically.
In the previous steps:

We have used the programmatic way to define the default value in the previous
step when we set the default value for the EmployeeId.

We have used the attribute editor to define an expression that calculates the value
of the TotalComp attribute

In this step we will use the attribute editor to define a groovy expression to populate the
HireDate with the current system date.
1. Open the EmpEO entity and select the HireDate from the attributes tab
2. For the Default Value :
a. Select Expression as the type
b. Use adf.currentDate as the expression

adf.currentDate is one of the groovy expressions provided by ADF, for more


information on groovy support, please refer to :
3.6 Overview of Groovy Scripting Language Support
3. Save and Test Your work

Oracle Sales Consulting SWAT

24

Step 7 Defining List of Values(LOVs)


In this step you will incorporate lists of values in the Employees View to make the
selection of values much easier for the end user.
Every list of value is based on a view object (list data source), you will create 3 LOVs:
DepartmentId LOV : based on the departments view
JobId LOV :based on the jobs view
ManagerId LOV : based on the managers
The first two are very simple and straight forward, however the last one is a bit more
complex; the ManagerId LOV is based on the managers view, the managers view has two
bind variables(CurrentEmp and CurrentDept) , when you create the LOV you have to
provide some logical values for those variable in order to retrieve the correct list .
So Lets begin:
1. Creating the DeprtmentId LOV:
a. Open the EmpsView from the application navigator and add a new list of
values to the DepartmentId attribute

b. When the List of Values dialog box appears, click the green plus-sign icon
to the right of the List Data Source field to add a data source that will
supply the list of valid choices for this attribute's value.

Oracle Sales Consulting SWAT

25

c. In the View Accessors dialog box that appears, select the DeptsView
object in the Available View Objects list on the left and click the Add
Instance button to add a new view accessor named DeptView1. Then click
OK.

d. Click the arrow at the right of the List Attribute field, and select
DepartmentId from the list of available attributes. Note that the List
Return Values section updates to show how attributes in the list datasource
will be assigned to attributes in the base view object when the user makes
a choice from the list.

Oracle Sales Consulting SWAT

26

Click the UI Hints tab, in the Display Attributes section, select the
DepartmentName attribute from the Available list and click the Add
button to add it to the list of selected attributes. This selection indicates
that you want the user to see the department name, rather than the
department number, in the list of values

Oracle Sales Consulting SWAT

27

Note that the default list type indicates a Choice List display style. You
use this default list type for this simple example, but if you click the list,
you can see the other types of list controls available for use with your
applications.
Click OK when your done.
e. Great, we have created the first list.
2. Creating the JobId LOV:
a. Using the same steps you used to create the DepartmentId LOV , select the
JobId attribute and add a list of values based on the JobsView.
b. When you reach the UI Hints step use the following:

We have changed the List Type to Input Text with list of values, to test the
available list types.
c. Click OK When Your Done

Oracle Sales Consulting SWAT

28

3. Creating the ManagerId LOV:


a. Open the EmpsView from the application navigator and add a new list of
values to the ManagerId attribute
b. When the List of Values dialog box appears, click the green plus-sign icon
in the List Data Source section to add a new data source to supply the list
of valid choices for this attribute's value
c. In the View Accessors dialog box, select the Managers View object from
the Available View Objects list on the left and click the Add Instance
button to add a new view accessor for the selected view object.

d. As this view object has bind variables, you need to configure how those
bind values get their values. To do so, make sure the Managers1 view
accessor is selected and click the Edit button
You want to assign the value of the CurrentEmp bind variable to be the
value of the EmployeeId attribute in the current row, so select
EmployeeId form the list, and the same thing also applies for the
CurrentDept bind variable.

Oracle Sales Consulting SWAT

29

e. Click OK to dismiss the Edit View Accessor window, and click OK again
to dismiss the View Accessor window.
f. In the List of Values dialog box, click the arrow in the List Attribute field
and select the EmployeeId attribute from the list.

Oracle Sales Consulting SWAT

30

g. click the UI Hints tab. Shuttle LastName into the Selected pane as the
list's display attribute

h. Click OK to close the List of Values dialog box.


i. Save your work.
4. Test Your Work

Oracle Sales Consulting SWAT

31

Step 8 Creating View Criteria


A View Criteria is a filter that you apply programmatically or by definition to a View
Object instance. It augments the WHERE clause in a View Object query. Named View
Criteria are defined in the Query panel of the View Object and are used:

In combination with the af:query component to build search forms. The View
Criteria that we will build in this step will be used to build the Employees Search
page in Lab 5 using af:query.
To restrict the result set of a View Object instance in the Application Module
model.

View Criteria use bind variables for query conditions that you want to pass in
dynamically at runtime. Beside of the ability to apply View Criteria declaratively, you
can apply them programmatically in Java.
A WHERE clause, if added to a View Object query by design restricts all instances of
this View Object, which usually is not what developers want, with View Criteria you can
restrict the desired instances only.
1. Open EmpsView , go to the Query tab and create new view criteria

Oracle Sales Consulting SWAT

32

2. The view criteria will be filtering the records based on LastName.

3. This view criterion will compare the LastName field with a value provided by the
user at runtime; this value will be stored in a bind variable called name.

Oracle Sales Consulting SWAT

33

4. Click the UI Hints tab and select always from the show operators tab.
Click OK to close the dialog.
These UI hints will be used by the ADF Query Component when we build the UI.

5. Save and test the view criteria.


a. Run HRModule and double click the EmpView1 node. In the data pane
press the Specify View Criteria button (binocular icon).

b. Shuttle the EmpViewCriteria named criteria to the selected side and click
the Find button.

Oracle Sales Consulting SWAT

34

c. In the Bind Variables dialog, enter a valid last name, for example Austin
in the Value field and press OK.
Only one record is returned

Oracle Sales Consulting SWAT

35

Step 9 Set the UI Hints for the Attributes


In this step we will define labels for each attribute in EmpEO, DeptEO and JobsView .
The main benefits behind defining the labels are:
Those labels will be used automatically whenever we use the entities and the
views to create our pages.
The labels will be stored automatically in a resource bundle that will help us
provide multi-lingual support in our application.
1. Using the Application Navigator, open EmpEO entity object.
a. Go the Attributes tab and select the first attribute, and using the UI Hints
tab to define a suitable label.

b. Repeat the last point to define a label for each attribute in the EmpEO
entity.
2. Repeat Step 1, but this time to define a label for each attribute in the DeptEO
entity.
3. Repeat Step 1, but this time to define a label for each attribute in the JobsView.
4. When You Finish , Open the ModelBundle.properties using the Application
Navigator and to see your labels with their keys

Oracle Sales Consulting SWAT

36

Oracle Sales Consulting SWAT

37

Das könnte Ihnen auch gefallen