230 likes | 240 Views
Learn to add custom validation methods, use typesafe data access, and traverse entity associations. Understand how to validate attributes, create MethodValidators, and utilize typesafe methods in EntityImpl.java.
E N D
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
Overview Entity Object Validation Type Business Rule Name must not be longer than 50 characters Customers IdNameStatusEmail Attribute Method Validator E-mail must contain an "@" and "." Domain Orders Customer e-mail must exist If the OrderMode is "ONLINE" IdCustomerIdOrderModeOrderTotal Entity Method Validator
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
Validating Attributes • To create custom validation for attributes, you can either: • Create a MethodValidator in the EntityImpl.java file or • Modify the setter() method of the attribute in the EntityImpl.java file
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.
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 ); }
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);}
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 value public boolean validateOrder() { String date = new java.util.Date().toString(); if( (getOrderStatus().equals("2")) && (getOrderDate().toString() == date )) {return true; } else {return false;}}
Validating Entity Objects • If entity MethodValidators are too limiting, create a custom method in the EntityImpl.java file: public boolean checkOrderMode(){ if ( ("ONLINE".equals(getOrderMode())) || !(getCustomerEmail() == null)) { //success } else { // Error - online order must have email address }}
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
Validation Order • Attribute validation occurs in the following order: • Domain validation: On instantiation of an entity object • Setter method: On creation or modification ofan attribute • Predefined validators: On a call to setAttributeInternal() • Attribute MethodValidators • validateEntity() method • Entity MethodValidators • doDML() method • beforeCommit() method
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
Association Example • A customer can place one or many orders. • An order is placed by just one customer. OrderPlacedBy Customers Orders Association Source Destination
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
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.
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.
Creating Entity Associations The “one” side of the association The “many” side of the association
Traversing Associations:Destination to Source LineItem Order • 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: ItemOrderedOnAssoc getLineItem() getTrackingNo() public String getTrackingNo() { return getOrd().getTrackingNo(); }
Traversing Associations:Source to Destination LineItem Order • 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. ItemOrderedOnAssoc getLineItem() public oracle.jbo.RowIterator getLineItem()
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
Practice 6-1: Overview • This practice covers the following topics: • Enforcing ListValidator rules • Creating domain validation code • Testing the validation rules