Monday, December 29, 2014

Creation of ADF Domain Type for existing data types and Oracle Object type

Here I’ll explain step by step creation of custom domain types and benefits of it.

Apart from the out of the box data types, ADF allows you to define custom data types to handle custom object types used in the database or to extend the functionalists provided by existing data types called Domain Types.

Domain types offer a way to define custom data types with cross-cutting behavior such as basic data type validation, formatting, and custom metadata properties in a way that are inherited by any entity objects or view objects that use the domain as the Java type of any of their attributes. After you've created a domain type in our project, it appears among the list of available data types in the Attribute Type drop down list in the entity object and view object wizards and dialog.

We can either extend the existing data types such as String, Integer, and Date to build domain types, or create new domain types to represent a custom data structure such as Oracle Object type (user-defined types which consist of one or more basic types) used in the database.

Main benefit of domains data type is for validation rules to validate data types and re-usability.

Example: Let's say if there are attributes in several Entity or View objects and the same validation rule is applied for those attributes, it’s wiser not to repeat this validation rule, but to put it into domain.
First we define the data types via extending the existing data types, for Example I’ll create domain which based on java.lang.Long type and it validates for positive salary amount

When we create Domain Types JDeveloper generates domain Java class as well. This class contains standard method - validate(). You can put your validation code over here. It’s important to notice, that validation logic will be applied for existing data and for new data also, means validation is applied during querying from database process. Here I’ve implemented simple validation rule - salary value can't be negative:

Step-1: right  click the Model Project and click New, in New Gallery select the ADF Business Components in Business Tier and there Select Domain, Click Ok

Step-2: define the package and name of customDomainType as below and click next.

Step-3: Select Type click next.

 Note: uncheck the value for persistence and queryable property for Transient attribute, click next.

Step-4: in summary screen Click finish, here open the Domain Java Class (SalaryDomainType.Java) and override the validate method


Step-5: Create the Employee EntityObject and define the type for salary attribute to newly created SalaryDomainType as below

Step-6: now test this using Model Tester and give the negative value for Salary Attribute you will get the error Message as below


Secondly here we define a domain type for a custom Oracle object type. The object type used in this example is as follows:
Create or replace TYPE ADDRESS AS OBJECT (street varchar2 (100), city varchar2 (50), post varchar2(100), state         CHAR(2), country varchar2(5), zip_code CHAR(6) )

Once you defined the above mentioned object type in the database, you will need to build a custom domain type to use the same from business components. We need to define a domain type for the ADDRESS object type, need to perform the following steps:

1.    Right-click on the Model project and select New Domain.
2.    In the Create Domain dialog window, enter the domain name and package.
Note: As we are building a domain for the oracle object type, check the Checkbox Domain for Oracle Object Type so it show the available Type List in Oracle DB, and here we can select the desired object type From the Available Types list, as below.


3.    Continue the wizard via clicking next. The Settings page will allow you to modify the attribute definitions, if required. This example does not need any change in the system generates properties. Click on Finish

4. When we finish generating the domain type, JDeveloper will create an XML definition to hold the metadata for the domain type and a corresponding Java class now we can create the business components by selecting a database table which uses ADDRESS as one of the columns, JDeveloper will automatically map the ADDRESS column type to the custom domain Java type that we defined.

we can even set the attribute types of entity objects or view objects explicitly to the custom domain we generated. To do this, need to select the desired attribute in the Attributes tab for an entity object or view object, and then select the desired Type in the property inspector.
For example: I created the personInfo table in HR Schema using Address Object type using the following script:


CREATE TABLE "HR"."PERSONINFO"
  (
    "PERSON_ID"     VARCHAR2(200) Primary Key,
    "FIRST_NAME"    VARCHAR2(200),
    "LAST_NAME"     VARCHAR2(200) NOT NULL,
    "EMAIL"         VARCHAR2(200) NOT NULL,
    "MOBILE_NUMBER" NUMBER(10,0),
    "DOB" DATE NOT NULL,
    "PRESENT_ADDRESS" "HR"."ADDRESS" ,
    "PARMANENT_ADDRESS" "HR"."ADDRESS"
  );

 insert into personinfo values('1','Sanjeev','Kumar','Sanjeevk@ymail.com', 8939474770,sysdate, Address('101,1st flr','Chennai','Chennai','TN','IN','600097'), null); 
 insert into personinfo values('2','Akhil','Kumar','Akhilk@ymail.com', 8939465770,sysdate, Address('101,1st flr','Chennai','Chennai','TN','IN','600097'), null);



5. Test the value using Model Tester




I created This sample in JDeveloper11.1.2.3.0 so it will work in this release or in higher version of JDeveloper.
 Sample






5 comments:

  1. how to do insert and update operation in address filed

    ReplyDelete
  2. Hi Ritesh,
    it is already explain in the blog, if still having any issue please update will help you.....

    ReplyDelete
  3. I wanted to thank you for this excellent read!! I definitely loved every little bit of it. I have you bookmarked your site to check out the new stuff you post. here

    ReplyDelete
  4. I would like to say that this blog really convinced me to do it! Thanks, very good post. Protocol

    ReplyDelete
  5. Amazing knowledge and I like to share this kind of information with my friends and hope they like it they why I do cool companies names

    ReplyDelete

Use GlassPane in ADF Appliation

Hi, Today I thought of sharing my knowledge via writing Blogs as I also learn a-lot from blogs and it’s really useful when u stuck in s...