33
3 Copyright © 2008, Oracle. All rights reserved. Using ADF Business Components for Validation, Calculations, and LOV

03_ValidationCalculationsLOV

Embed Size (px)

DESCRIPTION

adf lov

Citation preview

3Copyright © 2008, Oracle. All rights reserved.

Using ADF Business Components for Validation, Calculations, and LOV

Copyright © 2008, Oracle. All rights reserved.3 - 2

Objectives

After completing this lesson, you should be able to do the following:

• Build cascading lists of values (LOV)

• Enhance the application with calculations

• Validate foreign keys

• Employ Groovy expressions in validation

Copyright © 2008, Oracle. All rights reserved.3 - 3

Agenda

• Cascading List of Values

• Transient Attributes and Calculations

• Entity Object Validation

• Failure Messages

Copyright © 2008, Oracle. All rights reserved.3 - 4

Cascading Lists

Use one list to determine the values of a second list:select a Manager, who works in the same department or has a job of MANAGER or PRESIDENT.

Employees in the Sales department

Employees in the Research department

Change department to Research

Copyright © 2008, Oracle. All rights reserved.3 - 5

Creating the Basics

Configure view objects to support the lists. You can:

• Use existing view objects (VO) by creating different view criteria to support different lists

• Use entity-based VOs for data that may change when you want the list to reflect the changed data (such as adding a new department)

• Use non-entity-based VOs for static data

• Use bind variables to restrict the second list to the value specified in the first

• Add view accessors in the main (Emp) EO, to VO that are used for the lists

• Attach each list to a specific attribute to be populated

Copyright © 2008, Oracle. All rights reserved.3 - 6

Dependent List Example

• The example contains two tables, EMP and DEPT.

• The EMP table contains two foreign keys: – One to DEPT– A recursive one back to EMP

• You create the default Business Components from tables for EMP and DEPT, adding components to AppModule.

Copyright © 2008, Oracle. All rights reserved.3 - 7

Creating the View Criteria

For the VO that is used for the dependent list, create a view criterion that uses a bind variable.

Copyright © 2008, Oracle. All rights reserved.3 - 8

Defining View Accessors to the List Data Sources

In the EO for the view that uses the lists, define view accessors to the views that are to be used as data sources for lists:

Unrestricted:Using view criteria with value

assigned to bind variable:

Copyright © 2008, Oracle. All rights reserved.3 - 9

Attaching the List VO to an Attribute

In the main VO that uses the lists:

• Enable a list of values for the attributes that use lists

• Select the appropriate view and attribute as a data source for each list

Copyright © 2008, Oracle. All rights reserved.3 - 10

Setting the List UI Hints

• Set the Default List Type.

• Set the value displayed in the list to an attribute or attributes from the list’s view object.

• Set the behavior for the required or not required value.

Copyright © 2008, Oracle. All rights reserved.3 - 11

Setting the List for the Dependent List

Follow the same process to add the dependent list.

Copyright © 2008, Oracle. All rights reserved.3 - 12

Agenda

• Cascading List of Values

• Transient Attributes and Calculations

• Entity Object Validation

• Failure Messages

Copyright © 2008, Oracle. All rights reserved.3 - 13

Transient Attributes

• They do not map to the database column.

• They are value holders to display the calculated data.

• After you add a transient attribute to the entity object, to make it a calculated attribute, you can define the calculation by one of the following methods:– Defining a Groovy or regular expression– Writing Java code and using a SQL expression:

— Enable a custom entity object class on the Java page of the Entity Object Editor, choosing to generate accessor methods.

— Write Java code inside the accessor method for the transient attribute to return the calculated value.

Copyright © 2008, Oracle. All rights reserved.3 - 14

Creating Transient Attributes

• Create transient attributes to hold the calculated values.

• Define them at the entity object level or view object level.

Copyright © 2008, Oracle. All rights reserved.3 - 15

Entering a Calculation

• In the Attribute Editor, to use a Groovy expression, select the Expression option and enter the calculation in the Value property.

• Set the dependencies to include all attributes in the expression.

Copyright © 2008, Oracle. All rights reserved.3 - 16

Testing the Calculation

If the attribute property of Updatable is set to Never, the field will be disabled in the tester.

Copyright © 2008, Oracle. All rights reserved.3 - 17

Agenda

• Cascading List of Values

• Transient Attributes and Calculations

• Entity Object Validation

• Failure Messages

Copyright © 2008, Oracle. All rights reserved.3 - 18

Entity Object Validation

Validator Rule Name Description

Compare Compares the attribute’s value with a literal value

List Validates whether or not the value is in or is not in an LOV

Range Validates whether or not the value is within a range of values

Length Validates the value’s character or byte size against a size and operand (such as greater than or equal to)

Regular Expression Validates the data using Java regular expression syntax

Copyright © 2008, Oracle. All rights reserved.3 - 19

Using Groovy for Validation

• Groovy:– Is an agile and dynamic language for the JVM– Increases developer productivity by using

simple constructs and dot notation– Compiles straight to Java bytecode, so you

can use it anywhere you use Java

• With Groovy, you can define validation rules, conditional validation, default values, and failure handling.

Comparison of Java and Groovy Syntax

Java Code Equivalent Groovy Script

((Number)getAttribute("Sal").multiply(new Number(0.10))

Sal * 0.10

((Date)getAttribute("PromotionDate")).compareTo((Date)getAttribute("HireDate")) > 0

Sal * 0.10

Copyright © 2008, Oracle. All rights reserved.3 - 20

Constraining Dependent Values

Use the Compare validation rule to constrain dependent values.

Copyright © 2008, Oracle. All rights reserved.3 - 21

Defining the Conditional Expression

On the Validation Execution tabbed page, you can use a Groovy expression to define when the validation should be executed.

Copyright © 2008, Oracle. All rights reserved.3 - 22

Agenda

• Cascading List of Values

• Transient Attributes and Calculations

• Entity Object Validation

• Failure Messages

Copyright © 2008, Oracle. All rights reserved.3 - 23

Defining the Failure Message

You can use Groovy to display the attribute and its value in the error message, using message tokens.

Copyright © 2008, Oracle. All rights reserved.3 - 24

Storing Error Messages as Resources

1

2 3

Copyright © 2008, Oracle. All rights reserved.3 - 25

Validating Foreign Keys

• You perform another type of validation on foreign keys.

• On the Validators panel of the Entity Object Editor, perform the following:1. Select Entity.

2. Click Create new validator.

Copyright © 2008, Oracle. All rights reserved.3 - 26

Adding a Validation Rule

In the Add Validation Rule dialog box, select Key Exists from the Rule Type list.

Copyright © 2008, Oracle. All rights reserved.3 - 27

Managing Failures

On the Failure Handling tabbed page, enter the error message to be displayed when the validation fails.

Copyright © 2008, Oracle. All rights reserved.3 - 28

Testing the Validation

In the Tester, when you enter an invalid Foreign Key, the failure message displays.

Copyright © 2008, Oracle. All rights reserved.3 - 29

Script Expression Validation

• For this example, you need to know the view accessor name between Dept and Emp.

• Create a Script Expression validation rule.

Copyright © 2008, Oracle. All rights reserved.3 - 30

Groovy Rule Definition

• On the Rule Definition tabbed page, this code: – Converts the name to uppercase– Determines if it ends in an “S”– Validates if the Salary ends in a multiple of “5”

• The expression must return a Boolean true or false.

Copyright © 2008, Oracle. All rights reserved.3 - 31

Failure Handling

Groovy validation rules can conditionally raise one of several exceptions.

Copyright © 2008, Oracle. All rights reserved.3 - 32

Summary

In this lesson, you should have learned how to:

• Use two view objects to create dependent lists

• Add transient attributes to hold calculations

• Catch foreign key validations at the entity object level

• Create expression validations using Groovy

Copyright © 2008, Oracle. All rights reserved.3 - 33

Practice 3 Overview: Advanced ADF Business Components

This practice covers the following topics:

• Creating cascading lists

• Validating Entity objects