Sunday, July 4, 2010

Creating your own Webservices in JDeveloper11g Part 1 - Creating XML Schema

Hi,

These days my mind is in webservices mode so i will show how to create your own webservices in JDeveloper.

Creating your own webservies is a 3 step process as follows Note: You can also create webservices from Java class directly
  1. Create a XML Schema (XML schema define the structure of the webserivces)
  2. Create WSDL Document (Define the operations of Webservices)
  3. Generate Webservice from WSDL and implement code (the logic behind) and of course Test and deploy the Webservice (Output) 
You can download the sample workspace for all 3 parts 
     This post will cover the first step which is Creating XML schema

    Use case example
    • We will have a webservice which takes a input of credit card number and return the status of creadit card VALID or INVALID from the database table column "creditcard_status"
    • Following script to create sample table with data which we will use in this example (you can run the script.sql available in the project file)

    create table CreditStatus
    (creditCard_number Number,
     creditCard_status varchar2(15 char));
    
     insert into creditstatus
     values(1234,'VALID');
    
    insert into creditstatus
     values(9999,'INVALID');
      
    insert into creditstatus
     values(1111,'VALID');
    
    commit;
    

      Enough talking lets cook the Hot and Spicy webservice dish :)

      • Create an application with Generic Template as shown in the slide


      •  Create a project inside named validateCreditCard

      • Right click choose New on the Project, Go to page All Technologies >  XML > XML Schema and Press OK
      • Enter the Schema name as shown in the slide Make sure to extend the path by \public_html\WEB-INF\xsd

      • According to our scenario we need to define what will come as Input and what would be the output, so we will create a request parameter and response as CreditCardReques and CreditCardResponse.
      • Rename the first element to "CreditCardRequest", drag a sequence element from the component pallete to the CreditCardRequest element, drag a "element" component to the sequence component as shown in the slide, change the name to "ccNumber" and type to xsd:integer
      • This will act as our Input request parameter


      • Now do the same for output as well, drag the element component to the top schema element, drag a sequence, drag a element on sequence and renamed to "ccStatus" and type as "xsd:string" 
      • Your schema would look like this

      This is end of part 1

      Next Step: We will create a WSDL document, define the operation that webservice will perform and bind our XML schema with that WSDL.

      Happy JDeveloping,
      Baig

      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

      Saturday, June 26, 2010

      Migrating APEX 3.2 to APEX 4.0

      Hi all,

      Yesterday i migrated APEX 3.2 to 4.0 on my local XE database and it went smoothly, i thought it would be good to share with you some tips. One of the coolest thing is APEX 4.0 installer migrates automatically all meta-data from your old installations.

      One of the main features i like in APEX 4.0
      - Team development.
      - Dynamic Actions.
      - Flash charts.
      - Better themes including maps (which is good enough to impress management for geographical reporting)
      and many more to explore :)

      Steps to follow:

      1) Your database must be 10.2.0.3 or greater (else you will waste lot of time as my manager did yesterday LOL ... ) (if you have oracle XE install then no worries)
      also make sure that Oracle Text and XML DB is installed and configured

      2) Backup you database and all applications (good practice)

      3) Unzip Apex 4.0 folder to some folder other than 3.2 apex i copied into
         C:\oracleXE\app\oracle\product\10.2.0\server
      the folder must be named as apex (without any version name)

      4) Run the installation scripts

      Full development installation

      c:\cd  c:\oracleXE\app\oracle\product\10.2.0\server\apex
      c:\ sqlplus sys/mypassword as sysdba
      sql> @apexins.sql SYSAUX SYSAUX TEMP /i/
      


      Load Images


      c:\cd  c:\oracleXE\app\oracle\product\10.2.0\server\apex
      c:\ 
      sqlplus sys/mypassword as sysdba
      sql> @apxldimg.sql c:\oracleXE\app\oracle\product\10.2.0\server
      

      Note: Dont give complete path with apex  folder

      Change ADMIN password if required (script will prompt for password)


      c:\cd  c:\oracleXE\app\oracle\product\10.2.0\server\apex
      c:\ 
      sqlplus sys/mypassword as sysdba
      sql> @apxchpwd.sql  
      




      Thats all while installing you will see the above message means your old apex installation is migrating to new one.

      Run the apex home page as http://localhost:8080/apex you will see new interface


      Perform post installation tasks

      More info at Oracle APEX installation docs

      Have a nice day,
      Baig

      Saturday, June 19, 2010

      Display LOV on F9 Key in Oracle ADF

      Hi,

      Recently i learned from OTN forums how can we display LOV by pressing F9 key on field (which is Oracle forms default key to display LOV and migration to ADF would required developers to keep the common features alive) the answer is by using JavaScript we can control which key has been pressed by user or which mouse button is pressed without a round trip to server.

      Lets get into example:

      - Create Simple LOVs on Employees table's field 'DepartmentId' and 'ManagerId'

      - Create metaContainer facet in af:document (Recommended area to put javascript code)
      - In structure window right af:document and choose insert inside > JSF Core > facet

      - Choose metaContainer from list and press OK

      - Drag af:resource component to metaContainer facet from Component palette and choose type 'javascript'

      - Go to page source and create a function showMyLOV as shown in the slide
      Note: You can ignore any errors in the structure window regarding "Expected name instead of &."

      - Add a client listener to the field you want to display Lov on F9

      - Run the page and test


      Download the example

      More Reading on JavaScript implementation in Oracle ADF check Webuser Interface  guide

      Happy JDeveloping,
      Baig

      Wednesday, June 16, 2010

      Working with Dynamic Regions in Oracle ADF

      Hi all,

      I will extend my old post "Creating Pages with Regions in Oracle ADF" here so now i will show you how to work with Dynamic Region.

      Note: Updated on 23-Sep-2010 Identified bug in the sample code. Thanks Asim and Pino

      What is Dynamic Region? 

      According to Oracle docs:
      An ADF dynamic region is an ADF region where the task flow binding dynamically
      determines the value of its taskFlowId attribute at runtime. This allows the Fusion
      web application to determine which bounded task flow to execute within the ADF
      dynamic region based on the result of evaluation of the task flow binding’s
      taskFlowId attribute.

      In this example:
      - We will have 2 Bounded task flow one will show the list view of the Employee table and other shows the form style view
      - We will have a home page with 2 different button to activate the particular region.

      As shown the runtime view of the use case.




      Steps to follow: 
      (I assumed you already have enough ADF experience now if not feel free to check ADF section on this blog)

      1) Create 2 bounded task flows with page fragments, named list-flow and form-flow respectively.
      2) Create view activities on these pages one each.
      3) Drag and drop the collection from data control and design your page as your taste.
      4) Create a new activity in adfc-config flow called Home
      5) Create home page by choosing default 2 column template as shown in the slide


      6) Create 2 button on the first facet labeled as List and Form respectively.
      7) Drag a List-flow to second facet and choose Dynamic Region from the menu
      8) Create a Managed Bean named "DynamicRegionBean" class "DynamicRegion" Press OK Scope must be viewScope
      9) We are not passing parameters to our Task flow so we will keep this default press OK
      10) Change the code in the DynamicRegion class as following the method getDynamicTaskFlowId()  returns the TaskflowId of the BTF so we have changed our code to return the ID dynamically


      11) Drag and drop af:setPropertyListener to both buttons on HOME page set values to static string as 'list' and 'form' respectively as shown in the slide

      12) Set the partial submit to true on both these buttons and set partial triggers property to button ids in af:region component.

      Thats all you are ready Run the page Press Form button the form-flow will appear press List button the other view appears




      More Reading: Oracle Fusion Developer Guide Working with Dynamic Regions

      Download the example workspace


      Note: This example is to show how to Dynamic Regions works, When you have more than couple Regions then its better to write the Region code logic in a separate java class for ease of maintenance and import that class in DynamicRegionBean class.


      Have a nice day,
      Baig

      Saturday, June 12, 2010

      Converting Text to Upper Case in Oracle ADF

      Hi all,

      Just got to know the cool solution using Javascript at OTN forums i feel it would be better to share it.

      If you want to convert a text of field to uppercase 2 possible solutions in my opinion

      Solution 1:
        - JavaScript code which converts the text to uppercase using af:resource tag
        - link the function with the InputValue of the component.

      Solution 2:
      - Covert the value in setter method of the ViewObjectImpl class


      Solution 3: (Updated on July 10, 2010)
      Found great and better solution by Chris Muir (better than mine)

      In this example:
      - I will use Solution 1 on the First Name field
      - I will use Solution 2 on Last Name field.

      Note: Solution 1 in my opinion is better choice which is faster and less overhead on application


      Solution 1 by Frank Nimphius
      Here is the post by Frank Nimphius

      1. drag and drop an input text field to the filter facet of a column2. create a JS function as shown below
      3. Drag and drop an af:clientListenener to the input text field to reference the JS function







      Solution 2:
      - Generate RowImpl java class on EmployeView View Object by double clicking go to Java tab press the pencil icon on the top right and check the options.

      - Change value to value.ToUpperCase() in the method of setLastName as shown in the slide

      - Set AutoSubmit to TRUE of LastName field in JSF page


      Run the application Enter "steve" in the First Name field and tab out you will notice it will automatically convert to "STEVE" do the same for Last Name enter "king" and tab out it will convert to "KING"

      Download the example workspace

      Happy JDeveloping,
      Baig