package com.oreilly.jent.people.soap;

/**
 * In general, you may use the code in this book in your programs and 
 * documentation. You do not need to contact us for permission unless 
 * you're reproducing a significant portion of the code. For example, 
 * writing a program that uses several chunks of code from this book does 
 * not require permission. Selling or distributing a CD-ROM of examples 
 * from O'Reilly books does require permission. Answering a question by 
 * citing this book and quoting example code does not require permission. 
 * Incorporating a significant amount of example code from this book into 
 * your product's documentation does require permission.
 * 
 * We appreciate, but do not require, attribution. An attribution usually 
 * includes the title, author, publisher, and ISBN. For example: 
 * 
 *   "Java Enterprise in a Nutshell, Third Edition, 
 *    by Jim Farley and William Crawford 
 *    with Prakash Malani, John G. Norman, and Justin Gehtland. 
 *    Copyright 2006 O'Reilly Media, Inc., 0-596-10142-2."
 *  
 *  If you feel your use of code examples falls outside fair use or the 
 *  permission given above, feel free to contact us at 
 *  permissions@oreilly.com.
 */

import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import javax.xml.namespace.QName;
import javax.xml.rpc.Call;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.Service;
import javax.xml.rpc.ServiceFactory;

import com.oreilly.jent.people.Person;

public class PeopleFinderDIIClient {
    public static void main(String[] args) {
        // The endpoint, service and port we want to invoke
        final String endpoint = "http://localhost:9090/JEnt-examples/pf-service";
        final String qnameService = "PeopleFinderWS";
        final String qnamePort = "PeopleFinderPort";
        // Some namespaces we'll be using in our client calls
        final String serviceNS =      "http://soap.people.jent.oreilly/";
        final String serviceTypesNS = "http://soap.people.jent.oreilly/types";
        final String xsdNS =          "http://www.w3.org/2001/XMLSchema";
        final String jaxrpcNS =       "http://java.sun.com/jax-rpc-ri/internal";
        
        try {
            ServiceFactory factory = ServiceFactory.newInstance();
            // Create a URL to the service endpoint
            URL endpointURL = new URL(endpoint);
            // Create a reference to the service
            Service service = factory.createService(endpointURL,
                                                    new QName(serviceNS, qnameService));
            // Specify how Person objects should be mapped
//            TypeMapping serviceMapping = 
//                service.getTypeMappingRegistry().getDefaultTypeMapping();
//            QName personQName = new QName(serviceTypesNS, "Person");
//            serviceMapping.register(Person.class,
//                                    personQName,
//                                    new BeanSerializerFactory(Person.class, personQName), 
//                                    new BeanDeserializerFactory(Person.class, personQName));
            // Create a call to a particular port on that service
            QName port = new QName(serviceNS, qnamePort);
            Call call = service.createCall(port);
            
            // Set some properties on the call
            call.setProperty(Call.SOAPACTION_USE_PROPERTY,
                             new Boolean(true));
            call.setProperty(Call.SOAPACTION_URI_PROPERTY, "");
            // Indicate that we're using standard SOAP encoding for the
            // message body
            call.setProperty("javax.xml.rpc.encodingstyle.namespace.uri",
                             "http://schemas.xmlsoap.org/soap/encoding/");
            // Set the return type of the operation we want to invoke
            call.setReturnType(new QName(serviceTypesNS, "ArrayOfPerson"));
            // Set the operation we want to invoke
            call.setOperationName(new QName(serviceNS, "findPeople"));
            // Set the type of the single parameter to the operation
            call.addParameter("argNames",
                              new QName(serviceTypesNS, "ArrayOfstring"),
                              ParameterMode.IN);
            call.addParameter("argValues",
                              new QName(serviceTypesNS, "ArrayOfstring"),
                              ParameterMode.IN);
            // Create the actual operation arguments and put them in an
            // object array
            Map searchArgs = new HashMap();
            searchArgs.put("firstName", "Jim");
            String[] argNames = {"firstName"};
            String[] argValues = {"Jim"};
            Object[] opArgs  = {argNames, argValues};
            // Invoke the operation and collect the result
            Person[] people = (Person[])call.invoke(opArgs);
            for (int i = 0; i < people.length; i++) {
                System.out.println("Found person: \"" + 
                                   people[i].getFirstName() + " " +
                                   people[i].getLastName() + "\"");
            }
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
