package com.gers.test;

import org.apache.wsif.WSIFMessage;
import org.apache.wsif.WSIFOperation;
import org.apache.wsif.WSIFPort;
import org.apache.wsif.WSIFService;
import org.apache.wsif.WSIFServiceFactory;
import org.apache.wsif.WSIFException;
import org.apache.wsif.providers.soap.apacheaxis.WSIFDynamicProvider_ApacheAxis;
import org.apache.wsif.providers.soap.apachesoap.WSIFDynamicProvider_ApacheSOAP;
import org.apache.wsif.util.WSIFPluggableProviders;

import com.gers.cust.Customer;
import com.gers.cust.Email;

import javax.xml.namespace.QName;
import java.rmi.RemoteException;
import java.util.Iterator;

public class TestWsif {
	
	public static WSIFPort getPortFromAvailablePortNames(WSIFService service)
					throws WSIFException {
			  String portChosen = null;
        
			  // Obtain a list of the available port names for the service
			  Iterator it = service.getAvailablePortNames();
			  {
					System.out.println("Available ports for the service are: ");
					while (it.hasNext()) {
						 String nextPort = (String) it.next();
						 if (portChosen == null)
							  portChosen = nextPort;
						 System.out.println(" - " + nextPort);
					}
			  }
			  if (portChosen == null) {
					throw new WSIFException("No ports found for the service!");
			  }
			  System.out.println("Using port " + portChosen + "\n");
        
			  // An alternative way of specifying the port to use on the service
			  // is to use the setPreferredPort method. Once a preferred port has
			  // been set on the service, a WSIFPort can be obtained via getPort
			  // (no arguments). If a preferred port has not been set and more than
			  // one port is available for the port type specified in the WSIFService,
			  // an exception is thrown. 
			  service.setPreferredPort(portChosen);
			  WSIFPort port = service.getPort();
			  return port;
		 }

    public static void main(String[] args) {
        try {
            if (args.length != 1) {
                System.out.println(
                    "Usage: wsdl on the run");
                System.exit(1);
            }
            
			WSIFPluggableProviders.overrideDefaultProvider("http://schemas.xmlsoap.org/wsdl/soap/", new WSIFDynamicProvider_ApacheSOAP());
			
			//WSIFPluggableProviders.overrideDefaultProvider("http://schemas.xmlsoap.org/wsdl/soap/", new WSIFDynamicProvider_ApacheAxis());
			
            // create a service factory
            WSIFServiceFactory factory = WSIFServiceFactory.newInstance();

            // parse WSDL, wsdl location passed in.
            WSIFService service =
                factory.getService(
                    args[0],
                    null,
                    null,
                    "http://cust.gers.com/orchlet",
                    "CommonPT");

            // map types for Apache-SOAP
            service.mapType(
                new QName("http://cust.gers.com/definitions", "customer"),
                Class.forName(
                    "com.gers.cust.Customer"));
                    
			service.mapType(
							new QName("http://cust.gers.com/definitions", "customers"),
							Class.forName(
								"com.gers.cust.Customers"));         
                    
			   service.mapType(
					new QName("http://cust.gers.com/definitions", "email"),
				   Class.forName(
					    "com.gers.cust.Email"));
					    
			   service.mapType(
								new QName("http://cust.gers.com/definitions", "address"),
							   Class.forName(
									"com.gers.cust.Address"));
									
			   service.mapType(
								new QName("http://cust.gers.com/definitions", "comment"),
							   Class.forName(
									"com.gers.cust.Comment"));
								
			service.mapType(
							new QName("http://cust.gers.com/definitions", "newPostal"),
							   Class.forName(
								"com.gers.cust.NewPostal"));
								
			service.mapType(
							new QName("http://cust.gers.com/definitions", "createDate"),
							  Class.forName(
								"com.gers.cust.CreateDate"));
								
					    
			   Email id = new Email();
			   id.setCharTp("101");
			   
			   Customer id2 = new Customer();
			   id2.setCustomerId(id);
			   
               System.out.println("before invocation....");
            
			   WSIFPort port = null;
			   
			   port = getPortFromAvailablePortNames(service);

			 WSIFOperation operation =
			   port.createOperation("getCustomer", "CustomerId", "Customer");
			 
 			//WSIFOperation operation =
         	//		 port.createOperation("searchCustomers", "Customer", "Customers");
         			 
           if (operation == null)
           {
              System.out.println("why is it null");
           }

			WSIFMessage inputMessage  = operation.createInputMessage();
   		    WSIFMessage outputMessage = operation.createOutputMessage();
	   	    WSIFMessage faultMessage  = operation.createFaultMessage();
	   	
			inputMessage.setObjectPart("customerId", id);
			
			//inputMessage.setObjectPart("customer", id2);
			
			boolean operationSucceeded =
								 operation.executeRequestResponseOperation(
									  inputMessage,
									  outputMessage,
									  faultMessage);

			if (operationSucceeded) {
								 System.out.println("Successfully added to operation\n");
							} else {
								 System.out.println("Failed to operation");
							}

         System.out.println("after execution.....");
         
			Customer custFound = (Customer) outputMessage.getObjectPart("customer");
			
//            // create the stub
//            CustImpl stub = (CustImpl) service.getStub(CustImpl.class);
//            
//            System.out.println("after invocation");
//
//            // do the invocation
//            // args[1] is the cust code
//            Customer cust = stub.getCustomer("1234");

        } catch (WSIFException we) {
            System.out.println(
                "Error while executing sample, received an exception from WSIF; details:");
            we.printStackTrace();
        } 
        catch (ClassNotFoundException ce) {
            System.out.println(
                "Error while executing sample, could not find required class complexsoap.client.stub.com.cdyne.ws.LatLongReturn; please add it to your classpath; details:");
            ce.printStackTrace();
        }

    }
}

