Sunday, June 27, 2010

Integrating external webserives into ADF fusion application

Hi,

One of the use cases always comes to desk is to validate that user has entered a valid email address or not? now question is how to verify that the email address is valid? so answer is with the power of webservices we can verify that the email address is valid or not so again....


How to Integrate WebServices in Fusion application? As far as i know there are 2 methods.
1) By creating webservice data control on WSDL document provided by producer.
2) By creating webservice proxy.

Download the completed example 

In this example we will look at the first method Webservice data control:
- We will integrate external web-service which will return result TRUE or FALSE based on email input parameter.
- When user will enter email address in a field the appropriate message will appear with the field.
- On Commit we will also verify the output of the  the method to take further action.


Getting the Web service 
Visit http://www.webservicex.net/WCF/ServiceDetails.aspx?SID=22 and get the address of WSDL document thats all you need to integrate.
 (This can be any site who provides web service)


Test the webservice on the site by entering any email address and press invoke

Webservice will return the boolean value based on input

Lets integrate this webservice into out ADF fusion application

Create a table in HR schema to use in example

create table person_emails
(person_name varchar2(60),
 person_email varchar2(30));

Create Fusion application and Data control
Create Business components from table for person_emails table with application module
Right click Model project and select new follow the slides

Copy and paste the WSDL location and paste into URL field and press Service button to validate.

Select the methods you need to integrate in this service we have only one method


Next Next Next Press Finish

Now we have created a Data control so we can use it on JSF Page

Create a JSF Page
- Drag a collection of PersonEmail1 from data control panel to the page as ADF Form
- Now expand EmailWSDC data control and drag the Boolean Return below the Email field and Formatted output text

- Binding dialog will appear asking you to provide value from email parameter.
expand the expression builder and choose PersonEmail input value as shown in the slide.


as you can see the method is appearing in Page binding section as well

Set AutoSubmit = TRUE of PersonEmail field
Add a partial triggers property of Output text linking to the Email field 


Calling Web service Method
Choose edit from the property inspector in the ValueChangeListener of Email field and create a Managed Bean



Define Bean method by pressing New


In backing bean write code similar to this
The code is executing the Action method which is already linked in the page binding by passing the current value in the email field to the parameter called "Email"


package baigsorcl.view;

import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;

import oracle.adf.model.BindingContext;

import oracle.binding.AttributeBinding;
import oracle.binding.BindingContainer;
import oracle.binding.OperationBinding;


public class Page1Bean {

    public Page1Bean() {
    }
    
    String emailResult;

    //Method to call webservice Action Method
    public boolean verifyEmail(String email) {
        BindingContainer bindings = getBindings();
        OperationBinding method = bindings.getOperationBinding("IsValidEmail");
        Map paramsMap = method.getParamsMap();
        
        //assigning parameter
        paramsMap.put("Email", email);
        Object result = method.execute();
        
        if (result.equals(true)) {
            emailResult = "Thank you for providing valid email address";
            return true;
        } else {
            emailResult = "Please provide valid email";
            return false;
        }
    }

    //value change listener
    public void verifyEmail(ValueChangeEvent valueChangeEvent) {
        if (valueChangeEvent.getNewValue() != null) {
           boolean result =
verifyEmail(valueChangeEvent.getNewValue().toString());
        }
    }

    public BindingContainer getBindings() {
        return BindingContext.getCurrent().getCurrentBindingsEntry();
    }

    //On commit Action
    public String commit_action() {
      
       //Retreive Attribute value of email id
       BindingContainer attrbindings = getBindings();  
       AttributeBinding attr =
       (AttributeBinding)attrbindings.getControlBinding("PersonEmail");  
       boolean emailExists = verifyEmail(attr.getInputValue().toString());
      
       //if method returns TRUE then commit else error message
       if (emailExists){
        BindingContainer bindings = getBindings();
        OperationBinding operationBinding =
            bindings.getOperationBinding("Commit");
        Object result = operationBinding.execute();
        if (!operationBinding.getErrors().isEmpty()) {
            return null;
        }
        return null;
       }
       else {
         FacesMessage msg = new
FacesMessage(FacesMessage.SEVERITY_ERROR, "Error" , "Please provide valid email to continue...record not saved");
         FacesContext.getCurrentInstance().addMessage(null, msg);
         return null;
       }
  
    }

    public void setEmailResult(String emailResult) {
        this.emailResult = emailResult;
    }

    public String getEmailResult() {
        return emailResult;
    }
}

Assign the emailResult Variable to the Result output text we create for proper message



- Run the Page enter invalid email address and tab out message will appear about invalid email address enter any valid email address and tab out or press commit

Press Commit you will see the alert error if email is invalid


finally enter valid email and all is well


Explore more detail in the example

More reading about Integrating webserives check here

Happy JDeveloping,
Baig

2 comments:

  1. hi Baig,

    Thank you for all the blogs here...they are so helpful in learning oracle ADF.

    Can you help me figure out how to do the same example using webservice proxy. I could go so far where i have setup the partial trigger and valueChangeEvent but the formatedoutput is only shown after i click the search button. i am guessing there is something missing with respect to the binding of the formatedoutput

    Thanks for any kind of help.


    Sudha

    ReplyDelete
  2. sir, my project is on online inventory management system. and I would like to add automatic sms generation to the specified user the items get low. SO, would you suggest me how can I acheive this task. It would help me a lot

    thank you

    ReplyDelete