Beruflich Dokumente
Kultur Dokumente
(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.
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.
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 :
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:
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 .
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.
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
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.
11
12
b. Select Regular Expression as the Rule Type and then select Email
Address from the predefined Expressions
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.
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.
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
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
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
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
18
4. Test your rules, run the HRModule and test the 3 rules that you have created.
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:
20
21
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
In this exercise we will be using the second approach to generate the EmployeeId from
the database sequence EMPLOYEES_SEQ:
22
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
24
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.
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.
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
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
28
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.
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.
30
g. click the UI Hints tab. Shuttle LastName into the Selected pane as the
list's display attribute
31
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
32
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.
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.
b. Shuttle the EmpViewCriteria named criteria to the selected side and click
the Find button.
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
35
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
36
37