23
6 Copyright © 2004, Oracle. All rights reserved. Adding Custom Validation

6 Copyright © 2004, Oracle. All rights reserved. Adding Custom Validation

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

Page 1: 6 Copyright © 2004, Oracle. All rights reserved. Adding Custom Validation

6Copyright © 2004, Oracle. All rights reserved.

Adding Custom Validation

Page 2: 6 Copyright © 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

Page 3: 6 Copyright © 2004, Oracle. All rights reserved. Adding Custom Validation

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

Page 4: 6 Copyright © 2004, Oracle. All rights reserved. Adding Custom Validation

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

Page 5: 6 Copyright © 2004, Oracle. All rights reserved. Adding Custom Validation

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

Page 6: 6 Copyright © 2004, Oracle. All rights reserved. Adding Custom Validation

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.

Page 7: 6 Copyright © 2004, Oracle. All rights reserved. Adding Custom Validation

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 );

}

Page 8: 6 Copyright © 2004, Oracle. All rights reserved. Adding Custom Validation

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);}

Page 9: 6 Copyright © 2004, Oracle. All rights reserved. Adding Custom Validation

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;}}

Page 10: 6 Copyright © 2004, Oracle. All rights reserved. Adding Custom Validation

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:

Page 11: 6 Copyright © 2004, Oracle. All rights reserved. Adding Custom Validation

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

Page 12: 6 Copyright © 2004, Oracle. All rights reserved. Adding Custom Validation

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

Page 13: 6 Copyright © 2004, Oracle. All rights reserved. Adding Custom Validation

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

Page 14: 6 Copyright © 2004, Oracle. All rights reserved. Adding Custom Validation

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

Page 15: 6 Copyright © 2004, Oracle. All rights reserved. Adding Custom Validation

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

Page 16: 6 Copyright © 2004, Oracle. All rights reserved. Adding Custom Validation

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.

Page 17: 6 Copyright © 2004, Oracle. All rights reserved. Adding Custom Validation

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.

Page 18: 6 Copyright © 2004, Oracle. All rights reserved. Adding Custom Validation

6-19 Copyright © 2004, Oracle. All rights reserved.

The “one” side of the association

The “many” side of the association

Creating Entity Associations

Page 19: 6 Copyright © 2004, Oracle. All rights reserved. Adding Custom Validation

6-21 Copyright © 2004, Oracle. All rights reserved.

Creating Entity Associations

Page 20: 6 Copyright © 2004, Oracle. All rights reserved. Adding Custom Validation

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()

Page 21: 6 Copyright © 2004, Oracle. All rights reserved. Adding Custom Validation

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

Page 22: 6 Copyright © 2004, Oracle. All rights reserved. Adding Custom Validation

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

Page 23: 6 Copyright © 2004, Oracle. All rights reserved. Adding Custom Validation

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