Creating Custom Data Types Using Domain in ADF

Hi All,

In this post I am going to show you How you can create the custom Data Type for validation using Domain in ADF.

Suppose you have to validate the Email or URL Attribute in your Entity Object then you can create a custom data type in ADF and use that custom data type for validation of email or URL.

So to create a custom data type, First create a ADF application and right click on "Model" project and select "New" option.


Then from "Categories"  tab select "ADF Business Component" and then select "Domain" as shown below. Then click on OK.


Give the meaningful name for your custom data type according to your choice as shown below.


Click on "Next".


Now click on Finish.


Then go to "Java" section of created data type and open the java file.




When you will open the "EmailDomain" class then you will see the following code.

There is one "validate" method in this class which will comes by default. You can put your validation logic for Email or URL in this method. This method will validate existing as well as the newly created record.


package com.kunal.test.model.common;

import java.io.Serializable;

import oracle.jbo.JboException;
import oracle.jbo.Transaction;
import java.net.MalformedURLException;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import oracle.jbo.domain.DomainInterface;
import oracle.jbo.domain.DomainOwnerInterface;
// ---------------------------------------------------------------------
// ---    File generated by Oracle ADF Business Components Design Time.
// ---    Sun Oct 19 00:10:31 IST 2014
// ---    Custom code may be added to this class.
// ---    Warning: Do not modify method signatures of generated methods.
// ---------------------------------------------------------------------
public class EmailDomain implements DomainInterface, Serializable {
    public EmailDomain(String val) {
        mData = new String(val);
        validate();
    }
    private String mData;

    protected EmailDomain() {
        mData = "";
    }

    public Object getData() {
        return mData;
    }

    /**
     * <b>Internal:</b> <em>Applications should not use this method.</em>
     */
    public void setContext(DomainOwnerInterface owner, Transaction trans,
                           Object obj) {
    }

    /**
     * Implements domain validation logic and throws a JboException on error.
     */
    protected void validate() {
        //  ### Implement custom domain validation logic here. ###
    }


    public String toString() {
        if (mData != null) {
            return mData.toString();
        }
        return "<null>";
    }

    public boolean equals(Object obj) {
        if (obj instanceof DomainInterface) {
            if (mData != null) {
                return mData.equals(((DomainInterface)obj).getData());
            }
            return ((DomainInterface)obj).getData() == null;
        }
        return false;
    }
}

If you want to do the Email validation then your validate method should have the following code. 

 protected void validate() {
        String regString = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
        Pattern emailNamePtrn = Pattern.compile(regString);
        Matcher mtch = emailNamePtrn.matcher(mData);
        if (!mtch.matches()) {
            throw new JboException("Invalid Email Address.");
        }
    }


And if you want to validate the URL your validate method should have the following code.

    protected void validate() {
        try {
            java.net.URL url = new java.net.URL(mData);
        } catch (MalformedURLException e) {
            throw new JboException("Invalid url");
        }
    }


Once you have written the validation logic inside validate method then go to your EO and select Email attribute and change the type to your custom data type as shown below.


Once you change the Type you Entity Object will look like this.



Now when you run the Application module and check the validation it will show you the error message on invalid entry as like below.



Comments

  1. How to integrate EBS(ie.frontend) into JDev mobile application???? please send me the step by step procedure???? it would be a life saviour for me...

    ReplyDelete

Post a Comment

Popular posts from this blog

Setting up the environment for Angular2 and Hello World Example in Angular2

Showing number of rows or row count on top and bottom of table in ADF.

Build a Simple ReactJS application using react-cli