/**
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 -
 - JASS: Java Advanced tranSaction Support
 -
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 -
 - This module was originally developed by 
 -
 -    LSD (Distributed Systems Lab, http://lsd.ls.fi.upm.es/lsd/lsd.htm)
 -    at Universidad Politecnica de Madrid (UPM) as an ObjectWeb Consortium
 -    (http://www.objectweb.org) project.
 -
 -    This project has been partially funded by the European Commission under
 -    the IST programme of V FP grant IST-2001-37126 and by the Spanish
 -    Ministry of Science & Technology (MCyT) grants TIC2002-10376-E and
 -    TIC2001-1586-C03-02
 -
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 -  The original code and portions created by LSD are 
 -  Copyright (c) 2004 LSD (UPM)  
 -  All rights reserved.
 -  
 - Redistribution and use in source and binary forms, with or without 
 - modification, are permitted provided that the following conditions are met:
 -
 - -Redistributions of source code must retain the above copyright notice, this
 - list of conditions and the following disclaimer. 
 -
 - -Redistributions in binary form must reproduce the above copyright notice, 
 - this list of conditions and the following disclaimer in the documentation 
 - and/or other materials provided with the distribution. 
 -
 - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 - AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 - ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 - LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 - CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 - SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 - INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 - CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 - ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 - POSSIBILITY OF SUCH DAMAGE.
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 -
 -  Author: Francisco Perez Sorrosal (frperezs)
 -
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
*/

package org.objectweb.jass.examples.travelagency.ws;

import java.rmi.RemoteException;

import javax.xml.rpc.ServiceException;

import org.apache.axis.EngineConfiguration;
import org.apache.axis.Handler;
import org.apache.axis.SimpleChain;
import org.apache.axis.SimpleTargetedChain;
import org.apache.axis.client.AxisClient;
import org.apache.axis.configuration.SimpleProvider;
import org.apache.axis.handlers.SimpleSessionHandler;
import org.apache.axis.transport.http.HTTPSender;
import org.apache.axis.transport.http.HTTPTransport;

/**
 * This is the client for the travel agency WS example.
 * @author fran
 * Date: Feb 26, 2004
 * org.objectweb.jass.examples.travelagency.wsWSClient.java
 */
public class WSClient {
	/**
	 * Configures the AXIS engine on the client side.
	 * @return the engine configuration.
	 */
	private static EngineConfiguration getClientConfig() {
		SimpleProvider clientConfig = new SimpleProvider();
		Handler sessionHandler = (Handler) new SimpleSessionHandler();
		SimpleChain reqHandler = new SimpleChain();
		SimpleChain respHandler = new SimpleChain();
		reqHandler.addHandler(sessionHandler);
		respHandler.addHandler(sessionHandler);
		Handler pivot = (Handler) new HTTPSender();
		Handler transport =
			new SimpleTargetedChain(reqHandler, pivot, respHandler);
		clientConfig.deployTransport(
			HTTPTransport.DEFAULT_TRANSPORT_NAME,
			transport);

		return clientConfig;
	}

	public static void main(String args[]) {
		
		if (args.length != 3) {
			System.out.println("Usage: WSClient reservation_name number_of_seats number_of_rooms");
			System.exit(1);
		}			
		
		String rName = args[0];
		int nSeats = Integer.parseInt(args[1]);
		int nRooms = Integer.parseInt(args[2]);
		
		try {
			EngineConfiguration clientConfig = getClientConfig();
			TravelAgencyService service = new TravelAgencyServiceLocator();
			((org.apache.axis.client.Service) service).setEngineConfiguration(clientConfig);
			((org.apache.axis.client.Service) service).setEngine(new AxisClient(clientConfig));
			((org.apache.axis.client.Service) service).setMaintainSession(true);
			
			TravelAgency port = service.getTravelAgency();
			nSeats = port.reserveSeats(nSeats);
			nRooms = port.reserveRooms(nRooms);			
			System.out.println("Reservation made succesfully !!!");
			System.out.println(rName + " has reserved " + nSeats + " seats and " + nRooms + " rooms.");
		} catch (NotEnoughSeatsException e) {
			System.out.println("Not enough seats!!!");
		} catch (NotEnoughRoomsException e) {
			System.out.println("Not enough rooms!!!");								
		} catch (DBException e) {
			System.out.println("DB Error!!!");
		} catch (ServiceException e) {
			e.printStackTrace();
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		System.exit(0);
	}
}
