290 likes | 431 Views
Building Business Logic. Objectives. After completing this lesson, you should be able to do the following: Decide where to place business logic Describe entity objects’ role in business logic implementation Create entity objects
E N D
Objectives • After completing this lesson, you should be able to • do the following: • Decide where to place business logic • Describe entity objects’ role in business logic implementation • Create entity objects • Describe the different methods of implementing business logic in ADF Business Components • Add business logic to an ADF Business Components application • Use the Business Components Modeler to create and manage Business Components
Business Logic Validation Client tier Middle tier EIS tier Web Container ADF Business Services Tier Controller ADFApplicationModule ADFView Object ADFEntity Object Database Model ClientValidation Business LogicValidation DatabaseValidation
Implementing Validation in the Business Services Tier • Business logic tier: • Is used when creating or updating data • Assumes that existing data in the database is valid • Is defined at entity object level • Uses programmatic or declarative techniques • Declarative: Defined in wizards and editors • Programmatic: Coded in Java • Is specified in Java or XML or both
Entity Object: Overview Entity object Customers IdNameStatusEmail Attributes Status List(Gold Silver Platinum) Validation rule CUSTOMERS Database table ID NAME STATUS Email 201 Steve Gold msmith@company.com 202 Mike Silver dgonzales@company.com
Entity Object • Represents a database table or other data source • Handles all database interaction • Contains attributes representing the database columns • Encapsulates attribute-level and entity-level validation logic • Can contain custom business methods Customer Order
Creating an Entity Object • Select the Business Components node in the New Gallery, and then select Entity Object. • Right-click the package name in the navigator, and select New Entity Object.
Entity Object Wizard Enter a name. Select a database object. Move to the next step.
Files Generated for the CustomersEntity Object • Customers.xml: Metadata • The Entity Object Wizard obtains all its information from this file. • Is created by default (mandatory) • CustomersImpl.java: Entity object class • Extends the EntityImpl class • Is row level • Includes getter and setter methods for attributes • Is created by default (optional)
Optional Files Generated for the Customers Entity Object • CustomersDefImpl.java: Entity definition class • Extends the EntityDefImplclass • Contains run-time metadata describing the entity object • Describes all instances of the entity • CustomersCollImpl.java: Entity collection class • Extends the EntityCache class • Contains run-time querying the entity collection • Caches queried rows of the entity
Using the Business Components Modeler for Entity Objects 1. Create a diagram from the New Gallery. 2. Drag objects to the diagram. • Entity objects • Database definitions
Using the Business Components Modeler for Entity Objects • Create entity objects by using the Component Palette. • Use the Entity Object Editor for Entity Object properties.
Assigning an Initial Value to an Entity Attribute • Add code to the create() method in EntityImpl.java. • create() runs automatically when the entity is instantiated. Optional Java filesselection Select the Create Method check box to generate a create() method.
Mapping an Attribute to a Database Sequence create or replace trigger insert_ord_id BEFORE INSERT ON orders FOR EACH ROW DECLARE new_id number; BEGIN SELECT orders_seq.NextVal INTO new_id from Dual; :new.order_id := new_id; END; 1. Create a databasetrigger. • 2. Set the attribute typeto DBSequence.
Practice 4-1: Overview • This practice covers the following topics: • Initializing an attribute to a default value • Assigning a database sequence to an attribute
Adding Validation Entity-level validation Attribute-level validation
Declarative Validation Rules • Use declarative validation rules to validate an entity attribute. • Use the Entity Object Wizard to add and edit rules. • The Entity Object Wizard provides different types of declarative rules: • Compare with a single value • Compare with a list of valid values—either hard coded or the results of a SQL query • Compare with the minimum value, maximum value, or a range of values • Validate using a custom method • Declarative validation is XML based.
… … ListValidator: Example • Specify a list of values for Customers.Status. Customers.xml
Adding UniqueKeyValidator to an Entity • A UniqueKeyValidator: • Implements validation in the middle tier • Checks uniqueness of the primary key for an entity object • Checks that values are unique in the entity cache
Domains • Use domains for more complex validation: • Format of a phone number • Validity of a URL • Validity of an e-mail address • Checksum of digits of a credit card number • Validation is done by the domain constructor. • A domain is not bound to a particular entity or attribute. • You can use domains to validate multiple attributes across multiple entity objects.
Domains: Example • URLDomain verifies that an attribute is a valid URL. • The validate() method is called by the domain’s constructor. URLDomain.java protected void validate() { try { // Make sure the user entered a valid URL java.net.URL u = new java.net.URL(mData); } catch (java.net.MalformedURLException e) {throw new oracle.jbo.domain.DomainValidationException ("Invalid URL"); }
How to Create a Domain 1. Create the domain from the New Gallery. 2. Add validation code to the validate() method automatically created in URLDomain.java.
How to Use a Domain Edit the entity object and change the type of an attribute to URLDomain. URLDomain appears in the list of types.
Summary • In this lesson, you should have learned how to: • Implement declarative business logic • Create entity objects • Generate Java code for entity objects • Validate entity attributes using validation rules or domain objects • Use the Business Component modeler
Practice 4-2: Overview • This practice covers the following topics: • Adding a list validator to an attribute • Creating a domain • Assigning an attribute to a domain