Friday, 25 December 2015

JAVA : Creating a Webservice using JAX-WS in java

Hi,

Today I am going to demonstrate how we can publish and consume a JAX-WS based webservice.

For this I have created a Simple Java Application that contains webservice that can perform simple calculation
  1. First we need to define what resource or class that we are going to publish.
  2. For that we have to create an Interface named Calculator.java on the basis of which we will publish the webservice. The interface is not exactly needed but this help in maintaining and using the web service from other Java clients far easier.
     
    package calcws.inter;
     
    import javax.jws.WebMethod;
    import javax.jws.WebService;
    import javax.jws.soap.SOAPBinding;
    import javax.jws.soap.SOAPBinding.Style;
     
    //Service Endpoint Interface
    @WebService
    @SOAPBinding(style = Style.RPC)
    public interface Calculator {
        @WebMethod
        Integer addTwoNumbers(Integer firstNum, Integer secondNum);
    }
    
  1. So now we need to create a implementation of the Calculator interface.

    package calcws.impl;
    
    import calcws.inter.Calculator;
    
    import javax.jws.WebService;
    //Service Implementation
    @WebService(serviceName = "CalculatorService", // This is the name of the webservice
                endpointInterface = "calcws.inter.Calculator")
    public class CalculatorImpl implements Calculator {
    
        @Override
        public Integer addTwoNumbers(Integer firstNum, Integer secondNum) {
            // TODO Implement this method
            return firstNum + secondNum;
        }
    }
    
  1. So now we have created the implementation class and now need to publish the class and for this i have created a  Class Publisher.java
    package calcws.publ;
    
    import calcws.impl.CalculatorImpl;
    
    import javax.xml.ws.Endpoint;
    
    public class Publisher {
        public Publisher() {
            super();
        }
    
        public static void main(String[] args) {
            // 1 : Url for the webservice
            // 2 : The the implementation class to be Published
            Endpoint.publish("http://localhost:9999/ws/calc", new CalculatorImpl());
        }
    }
    
  1. Now we have to create a class that will consume the published webservice.
     
    package calcws.client;
    
    import java.net.URL;
    
    import calcws.inter.Calculator;
    
    import javax.xml.namespace.QName;
    import javax.xml.ws.Service;
    
    public class Consumer {
        public Consumer() {
            super();
        }
    
        public static void main(String[] args) throws Exception {
            URL url = new URL("http://localhost:9999/ws/calc?wsdl");
            //1st argument service URI, refer to wsdl document above
            //2nd argument is service name, refer to wsdl document above
            QName qname = new QName("http://impl.calcws/", "CalculatorService");
            Service service = Service.create(url, qname);
            Calculator calc = service.getPort(Calculator.class);
            Integer addTwoNumbers = calc.addTwoNumbers(20, 40);
            System.out.println("The sum is  : " + addTwoNumbers);
        }
    }
    
  1. After this first run the Publisher class and  after that Run the Client to call the service.
    Here is the output.


Reference : http://www.mkyong.com/webservices/jax-ws/jax-ws-hello-world-example/
You can download sampleapplication at :  CalcWS

Thursday, 17 December 2015

How to programmatically set Bind Variable in an LOV in ADF

Hello,

This is again a post about ADF Basics.

Today I am going to demonstrate a  how we can set bindVariable in an Lov programatically without using viewAccessor of the viewObject.

For the demonstration I have created a temporary viewObject and a department ViewObject through which Lov will be made.

I have created a TempVo with a Transient attribute EmployeeIdTrans on which lov of EmployeesVO will be created .
In the EmployeeVo I have created a viewCriteria as follows.



Then apply the Lov on the basis of this EmployeeVO in the TempVO.



In the viewAccessor of the lov Select the ViewCritera



Drag the EmployeeIdTrans on the Jspx page and create a single selection lov.
Then add a new Input text box from which we will fetch the value of the department on which the lov needs to be filtered.
Here is the code of the jspx page

 <af:panelBox text="Set BindVariable in an Lov Programmatically" id="pb1">
                    <f:facet name="toolbar"/>
                    <af:panelGroupLayout id="pgl1" layout="horizontal">
                        <af:inputText label="Department Id" id="it1"
                                      autoSubmit="true" value="#{viewScope.TestBean.deptId}"/>
                        <af:button text="Set Department Id" id="b1"
                                   actionListener="#{viewScope.TestBean.setLovBindVarAL}"/>
                    </af:panelGroupLayout>
                    <af:spacer width="10" height="10" id="s1"/>
                    <af:selectOneChoice value="#{bindings.EmployeeIdTrans.inputValue}"
                                        label="Employees"
                                        required="#{bindings.EmployeeIdTrans.hints.mandatory}"
                                        shortDesc="#{bindings.EmployeeIdTrans.hints.tooltip}" id="soc1"
                                        partialTriggers="b1">
                        <f:selectItems value="#{bindings.EmployeeIdTrans.items}" id="si1"/>
                        <f:validator binding="#{bindings.EmployeeIdTrans.validator}"/>
                    </af:selectOneChoice>
                </af:panelBox>

Then create a method in ApplicaitonModuleImpl that will set the value bindVariable and execute the lov and then call it in the bean.

 /**
     * Method to execute Lov with bind variables
     * @param deptId
     */
    public void setBindVarAndExceuteLov(Integer deptId){
        Row currentRow = getTemp1().getCurrentRow();
        RowSet lovVO = (RowSet)currentRow.getAttribute("EmployeesVO1");
        lovVO.setNamedWhereClauseParam("DeptIdBind", deptId);
        lovVO.executeQuery();
    }

You can refer this if you want to know how to call a method to ApplicationModuleImpl in Bean http://adfjavacodes.blogspot.com/2013/09/calling-method-defined-in-impl-class-of.html

Here is the code used in the Bean.



package bindvariableinlovapp.view;

import javax.faces.event.ActionEvent;

import oracle.adf.model.BindingContext;
import oracle.adf.view.rich.component.rich.input.RichInputText;

import oracle.binding.OperationBinding;

public class TestBean {
    private Integer deptId;

    public void setDeptId(Integer deptId) {
        this.deptId = deptId;
    }

    public Integer getDeptId() {
        return deptId;
    }

    public TestBean() {
    }

    public OperationBinding getBindings(String binding){
        return BindingContext.getCurrent().getCurrentBindingsEntry().getOperationBinding(binding);
    }
    public void setLovBindVarAL(ActionEvent actionEvent) {
        OperationBinding binding = getBindings("setBindVarAndExceuteLov");
        binding.getParamsMap().put("deptId", deptId);
        binding.execute();
    }

}

On running the application it shows all the departments.

On filtering with department Id 1.


Here is the sample application : ProgramaticValueOfBindVarInLovApp