Sunday, November 25, 2012

ADF Applications with Webservice creation and consumption


The following ADF projects illustrates the features of creating a webservice out of Java Class for doing the database insertion and retrieval, and consuming the webservice in an ADF fusion application.

Create the database user with name gkm.

#sqlplus "/as sysdba"
$create user gkm identifies by welcome1;
$grant dba to gkm;

Then login with gkm user. and create a table using the below command,

CREATE TABLE "GKM"."ADMIN"
   ( "ID" NUMBER NOT NULL ENABLE,
"USERNAME" VARCHAR2(20 BYTE),
"PASSWORD" VARCHAR2(20 BYTE),
"FIRSTNAME" VARCHAR2(20 BYTE),
"LASTNAME" VARCHAR2(20 BYTE),
"CONTACTNUMBER" NUMBER,
"EMAILID" VARCHAR2(20 BYTE),
CONSTRAINT "ADMIN_PK" PRIMARY KEY ("ID")
  USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE "SYSTEM"  ENABLE
   ) SEGMENT CREATION IMMEDIATE
  PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE "SYSTEM" ;

The same script is available here

After that create an adf java application for updating the database.

You can download the project from here.

In this application, I am updating the database fields using a java method and retrieving all the values. Then right click on the particular java class and select 'generate Webservice'
In my project I have selected 'MyWS.java' and created the webservice.

Then start the weblogic application server and deploy the application.


I have created the war deployment profile and ear deployment profile and then deployed the project using ear deployment profile.

After the deployment is success, login into the weblogic admin console, and click on 'deployments'. the select the project and click on the webservice profile. Then click on the 'test' tab and get the wsdl url.







Once you have webservice URL, you can paste it in a browser and check whether it displays the xml content.

http://localhost:7101/myJavaWS/MyWSPort?WSDL


Create another adf fusion application. You can download the application from here.

Create the webservice client and proxy from the wsdl url. Create a java class and create the methods for updating the database and retrieving the database values.

convert this java class as data control.

Method for retieving the database :

    public List<Admin> getPersons() {
        MyWSService myWSService = new MyWSService();
        MyWS myWS = myWSService.getMyWSPort();
        List<wsc.Admin> myList = new ArrayList<wsc.Admin>();
        myList = myWS.getData("select * from admin");
        for (Admin ad : myList) {
            System.out.println(ad);
        }
        return myList;
    }

Method for updating the table :

   public void updateTable() {
        System.out.println("I am in update table");
        MyWSService myWSService = new MyWSService();
        MyWS myWS = myWSService.getMyWSPort();
        DCBindingContainer dcBindings = (DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
        DCIteratorBinding iterBind= (DCIteratorBinding)dcBindings.get("personsIterator");
        int id = (Integer)iterBind.getCurrentRow().getAttribute("id");
        String username = (String)iterBind.getCurrentRow().getAttribute("username");
        String password = (String)iterBind.getCurrentRow().getAttribute("password");
        String firstName = (String)iterBind.getCurrentRow().getAttribute("firstName");
        String lastName = (String)iterBind.getCurrentRow().getAttribute("lastName");
        int contactNumber = (Integer)iterBind.getCurrentRow().getAttribute("contactNumber");
        String emailId = (String)iterBind.getCurrentRow().getAttribute("emailId");
        String query="insert into admin values (" + id + ",'" + username + "','"
            +password + "','" + firstName + "','" + lastName + "'," +contactNumber
            + ",'" + emailId +"')";
        System.out.println(query);
        int i = myWS.updateTable(query);
        System.out.println(i);
    }

Create an adf jspx page and drag and drop the person data control to the page and select ADF form, select navigation contol.




Then drag and drop the 'updateTable' method from data control window to page and select ADF button.




Finally the page looks like below,

Run the ADF application and check the below screenshots.

(In my application, I have created an ADF taskflow and then consumed it in a page).