package com.sams.jws.chapter18;

import com.ibm.wsif.WSIFDynamicPortFactory;
import com.ibm.wsif.WSIFPort;
import com.ibm.wsif.WSIFPart;
import com.ibm.wsif.WSIFJavaPart;
import com.ibm.wsif.WSIFMessage;
import com.ibm.wsif.stub.WSIFUtils;
import javax.wsdl.Definition;
import javax.wsdl.Service;
import javax.wsdl.PortType;

/**
 * WsifTimeServerClient
 * Simple demonstration of the most basic WSIF functionality.
 */

public class WsifTimeServerClient {

    public static void main(String[] args) throws Exception {

        String operationName = null;
        String zoneName = null;
        if (args.length == 1) {
            operationName = "synchronizeTime";
        } else if (args.length == 2) {
            operationName = "getTimeAtCity";
            zoneName = args[1];
        } else {
            String className = WsifTimeServerClient.class.getName();
            System.err.println("Usage: java " + className + " portName [zoneName]");
            System.err.println(
                "Eg: java "
                    + className
                    + " TimeServiceSOAPPort \t- Returns server system time as a long in units of millisec since epoch");
            System.err.println(
                "    java "
                    + className
                    + " TimeServiceSOAPPort America/Vancouver \t- Returns server's perspective on current date and time in Vancouver as a string");
            System.exit(0);
        }
        String portName = args[0];

        System.out.println("Loading WSDL document");
        Definition definition =
            WSIFUtils.readWSDL(null, "http://localhost:8080/wsif/TimeServer.wsdl");

        Service service =
            WSIFUtils.selectService(
                definition,
                "http://localhost:8080/wsif/TimeServer",
                "TimeService");
        PortType portType =
            WSIFUtils.selectPortType(
                definition,
                "http://localhost:8080/wsif/TimeServer-interface",
                "TimeServicePortType");
        WSIFDynamicPortFactory wsifDynPortFac =
            new WSIFDynamicPortFactory(definition, service, portType);
        System.out.println("Retrieving WSIFPort");
        WSIFPort wsifPort = wsifDynPortFac.getPort(portName);

        WSIFMessage inputWsifMsg = wsifPort.createInputMessage();
        WSIFMessage outputWsifMsg = wsifPort.createOutputMessage();
        WSIFMessage faultWsifMsg = wsifPort.createFaultMessage();

        // Set input argument for getTimeAtCity
        if (operationName.equals("getTimeAtCity")) {
            WSIFPart inputWsifPart = new WSIFJavaPart(String.class, zoneName);
            WSIFPart inputWsifPart2 = new WSIFJavaPart(java.lang.Integer.class, new Integer(123));
            inputWsifMsg.setPart("zoneName", inputWsifPart);
        }

        // Run request
        wsifPort.executeRequestResponseOperation(
            operationName,
            inputWsifMsg,
            outputWsifMsg,
            faultWsifMsg);

        // Process return values
        WSIFPart outputWsifPart = outputWsifMsg.getPart("return");
        System.out.println(
            "Return value for "
                + operationName
                + "() is: "
                + outputWsifPart.getJavaValue());

        System.out.println("Done.");
    }
}