1 / 23

Adding Custom Validation

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.

reinaldob
Download Presentation

Adding Custom Validation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Adding Custom Validation

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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.

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

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

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

  10. 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 }}

  11. 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

  12. 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

  13. 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

  14. Association Example • A customer can place one or many orders. • An order is placed by just one customer. OrderPlacedBy Customers Orders Association Source Destination

  15. 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

  16. 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.

  17. 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.

  18. Creating Entity Associations The “one” side of the association The “many” side of the association

  19. Creating Entity Associations

  20. 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(); }

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

  22. 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

  23. Practice 6-1: Overview • This practice covers the following topics: • Enforcing ListValidator rules • Creating domain validation code • Testing the validation rules

More Related