/**
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 -
 - 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 java.sql.SQLException;

import javax.activity.opennested.ONTActivity;
import javax.activity.opennested.UserOpenNested;
import javax.ejb.CreateException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.apache.axis.session.Session;
import org.apache.axis.session.SimpleSession;
import org.objectweb.jass.examples.travelagency.ejbs.Airline;
import org.objectweb.jass.examples.travelagency.ejbs.AirlineHome;
import org.objectweb.jass.examples.travelagency.ejbs.Hotel;
import org.objectweb.jass.examples.travelagency.ejbs.HotelHome;

public class TravelAgencySoapBindingImpl
	implements org.objectweb.jass.examples.travelagency.ws.TravelAgency {

	private AirlineHome airlineHome;
	private Airline airline;
	private HotelHome hotelHome;
	private Hotel hotel;

	private UserOpenNested uon = null;
	private Session s = null;
	

	/**
	 * Constructor. Obtains a reference to the UserOpenNested and the EJBs.
	 */
	public TravelAgencySoapBindingImpl() {
		Context ctx = null;

		s = new SimpleSession();

		try {
			ctx = new InitialContext();
			uon = (UserOpenNested) ctx.lookup("UserOpenNested");
		} catch (NamingException e) {
			e.printStackTrace();
		}
		getAdaptAirlineEJB();
		getAdaptHotelEJB();
	}

	/**
	 * Makes the seat reservation calling the EJB.
	 */
	public int reserveSeats(int in0)
		throws
			java.rmi.RemoteException,
			org.objectweb.jass.examples.travelagency.ws.NotEnoughSeatsException,
			org.objectweb.jass.examples.travelagency.ws.DBException {
		try {
			System.out.println("Starting ROOT activity");
			uon.activityBegin(0);
			System.out.println("Calling EJB to reserve seats");
			airline.reserveSeats(in0);
			System.out.println("Seats reserved");
		} catch (
			org
				.objectweb
				.jass
				.examples
				.travelagency
				.exceptions
				.NotEnoughSeatsException e) {
			try {
				System.out.println(
					"Not enough seats!!! Rolling-back ROOT acitivity");
				uon.activityRollback();
			} catch (Exception e1) {
				e1.printStackTrace();
			}
			throw new NotEnoughSeatsException();
		} catch (SQLException e) {
			try {
				System.out.println("DB Error. Rolling-back ROOT activity");
				uon.activityRollback();
			} catch (Exception e1) {
				e1.printStackTrace();
			}
			throw new DBException();
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("Suspending ROOT activity");
		ONTActivity activity = uon.suspend();		
		System.out.println("ROOT activity suspended");
		s.set("ACTIVITY", activity);
		return in0;
	}

	/**
	 * Makes the rooms reservation calling the EJB.
	 */
	public int reserveRooms(int in0)
		throws
			java.rmi.RemoteException,
			org.objectweb.jass.examples.travelagency.ws.NotEnoughRoomsException,
			org.objectweb.jass.examples.travelagency.ws.DBException {

		System.out.println("Resuming ROOT activity");
		ONTActivity activity = (ONTActivity) s.get("ACTIVITY");
		uon.resume(activity);
		System.out.println("ROOT activity resumed");

		try {
			System.out.println("Calling EJB to reserve rooms");
			hotel.reserveRooms(in0);
			System.out.println("Rooms reserved");
			System.out.println("Committing ROOT activity");
			uon.activityCommit(null);
			System.out.println("ROOT activity committed!!!");
		} catch (
			org
				.objectweb
				.jass
				.examples
				.travelagency
				.exceptions
				.NotEnoughRoomsException e) {
			try {
				System.out.println(
					"Not enough rooms!!! Rolling-back ROOT acitivity");
				uon.activityRollback();
			} catch (Exception e1) {
				e1.printStackTrace();
			}
			throw new NotEnoughRoomsException();
		} catch (SQLException e) {
			try {
				System.out.println("DB Error. Rolling-back ROOT activity");
				uon.activityRollback();
			} catch (Exception e1) {
				e1.printStackTrace();
			}
			throw new DBException();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return in0;
	}

	/**
	* Gets the reference to the UserOpenNested service.
	*/
	private UserOpenNested getUserOpenNested() {
		UserOpenNested uon = null;
		Context ctx = null;

		try {
			ctx = new InitialContext();
			uon = (UserOpenNested) ctx.lookup("UserOpenNested");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return uon;
	}

	/**
	 * Gets the reference to the Airline EJB.
	 */
	private void getAdaptAirlineEJB() {
		Object obj = null;
		Context ctx = null;
		try {
			ctx = new InitialContext();
			obj = ctx.lookup("AirlineEJB");
			airlineHome =
				(AirlineHome) javax.rmi.PortableRemoteObject.narrow(
					obj,
					AirlineHome.class);
			airline = airlineHome.create();
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (CreateException e) {
			e.printStackTrace();
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Gets the reference to the Hotel EJB.
	 */
	private void getAdaptHotelEJB() {
		Object obj = null;
		Context ctx = null;
		try {
			ctx = new InitialContext();
			obj = ctx.lookup("HotelEJB");
			hotelHome =
				(HotelHome) javax.rmi.PortableRemoteObject.narrow(
					obj,
					HotelHome.class);
			hotel = hotelHome.create();
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (CreateException e) {
			e.printStackTrace();
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}

}
