Upload
cynthia-barnett
View
226
Download
0
Embed Size (px)
DESCRIPTION
6-3 Copyright © 2004, Oracle. All rights reserved. Customers Id Name Status Business Rule Overview Entity Object Orders Id CustomerId OrderMode OrderTotal Validation Type Attribute Method Validator Name must not be longer than 50 characters Customer must exist If the OrderMode is "ONLINE" Domain must contain an and "." Entity Method Validator
Citation preview
6Copyright © 2004, Oracle. All rights reserved.
Adding Custom Validation
6-2 Copyright © 2004, Oracle. All rights reserved.
Objectives
After completing this lesson, you should be able to do the following:• Add custom methods to validate business data • Use the typesafe data access methods • Use entity associations in business logic • Traverse entity associations
6-3 Copyright © 2004, Oracle. All rights reserved.
Customers
IdNameStatusEmail
Business Rule
Overview
Entity Object
Orders
IdCustomerIdOrderModeOrderTotal
Validation Type
AttributeMethod Validator
Name must not be longer than 50 characters
Customer e-mail must exist
If the OrderMode is "ONLINE"
Domain E-mail must contain an "@"
and "."
EntityMethod
Validator
6-4 Copyright © 2004, Oracle. All rights reserved.
Adding Validation to an Entity
There are a number of places you can put validation:• Predefined validators: For simple XML-based rules• Custom method validators: For more complex
attribute and entity rules• Attribute setter method: For complex attribute
rules• Override EntityImpl.java methods: For more
complex entity rules and custom behaviors• Domains: Can be used for multiple attributes
across multiple entity objects
6-5 Copyright © 2004, Oracle. All rights reserved.
Validating Attributes
To create custom validation for attributes, you can either:• Create a MethodValidator in the
EntityImpl.java fileor• Modify the setter() method of the attribute in the
EntityImpl.java file
6-6 Copyright © 2004, Oracle. All rights reserved.
Creating a MethodValidator for an Attribute
To create a custom validator, create a new method in the EntityImpl.java file. Select the EntityImpl.java file in the Structure pane or select Go to Entity Object Class from the context menu.
6-7 Copyright © 2004, Oracle. All rights reserved.
Creating a MethodValidator for an Attribute
To create the custom validation as a MethodValidator, the method must:• Be defined as public• Accept a single argument of the same type as the
attribute• Return a Boolean value• Start with validate
public boolean validateEmail(String value){ return (value.indexOf('@') != -1 );
}
6-8 Copyright © 2004, Oracle. All rights reserved.
Utilizing Typesafe Methods
EntityImpl.java contains typesafe methods to get and set each of the entity’s attributes.To override the setter method in the EntityImpl.java file:• Add custom validation code in the methods.• Call setAttributeInternal() to set the
attribute value after your validation code.public void setCreditLimit(Number value){ // add your custom code here setAttributeInternal(CREDITLIMIT, value);}
6-9 Copyright © 2004, Oracle. All rights reserved.
Validating Entity Objects
MethodValidators can also be created in the EntityImpl.java file to validate entity objects. Custom entity validation methods must:• Be defined as public• Return a Boolean valuepublic boolean validateOrder()
{ String date = new java.util.Date().toString();
if( (getOrderStatus().equals("2")) &&
(getOrderDate().toString() == date ))
{return true; }
else {return false;}}
6-10 Copyright © 2004, Oracle. All rights reserved.
public boolean checkOrderMode(){
if ( ("ONLINE".equals(getOrderMode())) ||
!(getCustomerEmail() == null))
{ //success
}
else {
// Error - online order must have email address
}}
Validating Entity Objects
If entity MethodValidators are too limiting, create a custom method in the EntityImpl.java file:
6-11 Copyright © 2004, Oracle. All rights reserved.
Call EntityImpl Methods
You can also override other methods in EntityImpl.java. For example:• doDML()—Log changes in another entity• beforeCommit()—Validate multiple instances of
the same entity• remove()—Record a deletion in another entity
6-12 Copyright © 2004, Oracle. All rights reserved.
Validation Order
Attribute validation occurs in the following order:• Domain validation: On instantiation of an entity
object• Setter method: On creation or modification of
an attribute• Predefined validators: On a call to
setAttributeInternal()• Attribute MethodValidators• validateEntity() method• Entity MethodValidators• doDML() method• beforeCommit() method
6-13 Copyright © 2004, Oracle. All rights reserved.
Associations
Associations define a relationship between entity objects. Associations:• Facilitate access to data in related entity objects• May be based on database constraints• May be independent of database constraints• Consist of a source (master) and a destination
(detail) entity
6-15 Copyright © 2004, Oracle. All rights reserved.
Source DestinationAssociation
Customers Orders
Association Example
• A customer can place one or many orders.• An order is placed by just one customer.
OrderPlacedBy
6-16 Copyright © 2004, Oracle. All rights reserved.
Accessor Methods
• Are optional methods created by the Association Wizard
• Provide access to data from the associated entity• Are bidirectional
For example:– Get all orders for a customer– Get customer information from an order
6-17 Copyright © 2004, Oracle. All rights reserved.
Association Types
• Association– Entities are related but not completely dependent.– Either end of the association can exist without the
other.– It is usually a categorization.
• Composition– Destination entity is completely dependent on the
source entity.– The source entity owns the destination entity.– No destination entity can be created without the
owning entity existing first.
6-18 Copyright © 2004, Oracle. All rights reserved.
Determining the Association Type
Two questions to ask:• Can a destination entity object exist without the
source?– If yes, the source is associated to the destination.– If no, the source is composed of the destination.
• When I delete the source, do I delete the destination?– If yes, the relationship is a composition.– If no, the relationship is an association.
6-19 Copyright © 2004, Oracle. All rights reserved.
The “one” side of the association
The “many” side of the association
Creating Entity Associations
6-21 Copyright © 2004, Oracle. All rights reserved.
Creating Entity Associations
6-23 Copyright © 2004, Oracle. All rights reserved.
public String getTrackingNo() { return getOrd().getTrackingNo(); }
Traversing Associations:Destination to Source
• The destination entity’s EntityImpl.java file contains methods to get and set the source entity.For example, LineItemImpl.java contains getOrd() and setOrd().
• You can add a method to LineItemImpl.java to get the tracking number of the order containing this item:
getLineItem()
Order
ItemOrderedOnAssoc
LineItem
getTrackingNo()
6-24 Copyright © 2004, Oracle. All rights reserved.
Traversing Associations:Source to Destination
• The source entity’s EntityImpl.java file contains a method to get the destination entity.For example, OrdImpl.java contains the method:
• Use the methods of RowIterator to step from row to row and get individual attribute values.
public oracle.jbo.RowIterator getLineItem()
getLineItem()
Order
ItemOrderedOnAssoc
LineItem
6-25 Copyright © 2004, Oracle. All rights reserved.
Summary
In this lesson, you should have learned how to:• Add business rules to ADF Business Components• Validate entities, attributes, and domains• Test the validation rules
6-26 Copyright © 2004, Oracle. All rights reserved.
Practice 6-1: Overview
This practice covers the following topics:• Enforcing ListValidator rules • Creating domain validation code• Testing the validation rules