FileDocCategorySizeDatePackage
TravelAgentBean.javaAPI DocExample13468Mon Sep 30 10:54:22 BST 2002com.titan.travelagent

TravelAgentBean

public class TravelAgentBean extends Object implements javax.ejb.SessionBean

Fields Summary
public com.titan.customer.CustomerRemote
customer
public com.titan.cruise.CruiseLocal
cruise
public com.titan.cabin.CabinLocal
cabin
public javax.ejb.SessionContext
ejbContext
public Context
jndiContext
Constructors Summary
Methods Summary
public TicketDObookPassage(com.titan.processpayment.CreditCardDO card, double price)

      
      if (customer == null || cruise == null || cabin == null)
      {
         throw new IncompleteConversationalState ();
      }
      try
      {
         ReservationHomeLocal reshome =
         (ReservationHomeLocal)jndiContext.lookup ("java:comp/env/ejb/ReservationHomeLocal");
         
         ReservationLocal reservation =
         reshome.create (customer, cruise, cabin, price, new java.util.Date ());
         
         Object ref = jndiContext.lookup ("java:comp/env/ejb/ProcessPaymentHomeRemote");
         
         ProcessPaymentHomeRemote ppHome = (ProcessPaymentHomeRemote)
         PortableRemoteObject.narrow (ref, ProcessPaymentHomeRemote.class);
         
         ProcessPaymentRemote process = ppHome.create ();
         process.byCredit (customer, card, price);
         process.remove ();
         
         TicketDO ticket = new TicketDO (customer,cruise,cabin,price);

         // New Code added to exercise 12.2 for sending JMS messages
         //
			String ticketDescription = ticket.toString();

			TopicConnectionFactory factory = (TopicConnectionFactory)
				jndiContext.lookup("java:comp/env/jms/TopicFactory");
			
			Topic topic = (Topic)
				jndiContext.lookup("java:comp/env/jms/TicketTopic");
	
			TopicConnection connect = factory.createTopicConnection();
			TopicSession session = connect.createTopicSession(false,0);
			TopicPublisher publisher = session.createPublisher(topic);

			TextMessage textMsg = session.createTextMessage();
			textMsg.setText(ticketDescription);

			System.out.println("Sending text message to jms/TicketTopic..");
			publisher.publish(textMsg);

			connect.close();         
         
         return ticket;
      }
      catch (Exception e)
      {
         throw new EJBException (e);
      }
   
public java.util.CollectionbuildSampleData()

      Collection results = new ArrayList ();
      
      try
      {
         System.out.println ("TravelAgentBean::buildSampleData()");
         
         Object obj = jndiContext.lookup ("java:comp/env/ejb/CustomerHomeRemote");
         CustomerHomeRemote custhome = (CustomerHomeRemote)
         javax.rmi.PortableRemoteObject.narrow (obj, CustomerHomeRemote.class);
         
         CabinHomeLocal cabinhome =
         (CabinHomeLocal)jndiContext.lookup ("java:comp/env/ejb/CabinHomeLocal");
         ShipHomeLocal shiphome =
         (ShipHomeLocal)jndiContext.lookup ("java:comp/env/ejb/ShipHomeLocal");
         CruiseHomeLocal cruisehome =
         (CruiseHomeLocal)jndiContext.lookup ("java:comp/env/ejb/CruiseHomeLocal");
         ReservationHomeLocal reshome =
         (ReservationHomeLocal)jndiContext.lookup ("java:comp/env/ejb/ReservationHomeLocal");
         
         System.out.println ("Creating Customers 1 and 2..");
         
         // we first clean the db by removing any customer, cabin,
         // ship, cruise and reservation beans.
         //         
         removeBeansInCollection (custhome.findAll());
         results.add ("All customers have been removed");
         removeBeansInCollection (cabinhome.findAll());
         results.add ("All cabins have been removed");
         removeBeansInCollection (shiphome.findAll());
         results.add ("All ships have been removed");
         removeBeansInCollection (cruisehome.findAll());
         results.add ("All cruises have been removed");
         removeBeansInCollection (reshome.findAll());
         results.add ("All reservations have been removed");

         // We now set our new basic environment
         //
         System.out.println ("Creating Customers 1 and 2...");
         CustomerRemote customer1 = custhome.create (new Integer (1));
         customer1.setName ( new Name ("Smith","Bob") );
         results.add ("Customer with ID 1 created (Smith Bob)");
         
         CustomerRemote customer2 = custhome.create (new Integer (2));
         customer2.setName ( new Name ("Stalin","Joseph") );
         results.add ("Customer with ID 2 created (Stalin Joseph)");

         System.out.println ("Creating Ships A and B...");
         ShipLocal shipA = shiphome.create (new Integer (101), "Nordic Prince", 50000.0);
			results.add("Created ship with ID 101...");
         ShipLocal shipB = shiphome.create (new Integer (102), "Bohemian Rhapsody", 70000.0);
			results.add("Created ship with ID 102...");

         System.out.println ("Creating Cabins on the Ships...");         
         ArrayList cabinsA = new ArrayList ();
         ArrayList cabinsB = new ArrayList ();
         for (int jj=0; jj<10; jj++)
         {
            CabinLocal cabinA = cabinhome.create (new Integer (100+jj),shipA,"Suite 10"+jj,1,1);
				cabinsA.add(cabinA);
            CabinLocal cabinB = cabinhome.create (new Integer (200+jj),shipB,"Suite 20"+jj,2,1);
				cabinsB.add(cabinB);
         }
			results.add("Created cabins on Ship A with IDs 100-109");
			results.add("Created cabins on Ship B with IDs 200-209");

         
         System.out.println ("Creating three cruises for each ship...");
         
         CruiseLocal cruiseA1 = cruisehome.create ("Alaska Cruise", shipA);
         results.add("Created Alaska Cruise with ID " + cruiseA1.getId() + " on ShipA");
         CruiseLocal cruiseA2 = cruisehome.create ("Norwegian Fjords", shipA);
         results.add("Created Norwegian Fjords Cruise with ID " + cruiseA2.getId() + " on ShipA");
         CruiseLocal cruiseA3 = cruisehome.create ("Bermuda or Bust", shipA);
         results.add("Created Bermuda or Bust Cruise with ID " + cruiseA3.getId() + " on ShipA");

         
         CruiseLocal cruiseB1 = cruisehome.create ("Indian Sea Cruise", shipB);
         results.add("Created Indian Sea Cruise with ID " + cruiseB1.getId() + " on ShipB");
         CruiseLocal cruiseB2 = cruisehome.create ("Australian Highlights", shipB);
         results.add("Created Australian Highlights Cruise with ID " + cruiseB2.getId() + " on ShipB");
         CruiseLocal cruiseB3 = cruisehome.create ("Three-Hour Cruise", shipB);
         results.add("Created Three-Hour Cruise with ID " + cruiseB3.getId() + " on ShipB");
         
         System.out.println ("Making a few reservations...");
         
         ReservationLocal res =
         reshome.create (customer1, cruiseA1, (CabinLocal)(cabinsA.get (3)), 1000.0, new Date ());
         res = reshome.create (customer1, cruiseB3, (CabinLocal)(cabinsB.get (8)), 2000.0, new Date ());
         res = reshome.create (customer2, cruiseA2, (CabinLocal)(cabinsA.get (5)), 2000.0, new Date ());
         res = reshome.create (customer2, cruiseB3, (CabinLocal)(cabinsB.get (2)), 2000.0, new Date ());
         
         results.add ("Made reservation for Customer 1 on Cruise "+cruiseA1.getId ()+" for Cabin 103");
         results.add ("Made reservation for Customer 1 on Cruise "+cruiseB3.getId ()+" for Cabin 208");
         results.add ("Made reservation for Customer 2 on Cruise "+cruiseA2.getId ()+" for Cabin 105");
         results.add ("Made reservation for Customer 2 on Cruise "+cruiseB3.getId ()+" for Cabin 202");
         
      }
      catch (Exception e)
      {
         e.printStackTrace ();
         throw new EJBException (e);
      }
      
      return results;
   
public voidejbActivate()

public voidejbCreate(com.titan.customer.CustomerRemote cust)

      System.out.println ("TravelAgentBean::ejbCreate");
      customer = cust;
   
public voidejbPassivate()

public voidejbRemove()

private java.sql.ConnectiongetConnection()

      try
      {
         DataSource ds =
         (DataSource)jndiContext.lookup ("java:comp/env/jdbc/titanDB");
         return ds.getConnection ();
      }
      catch(NamingException ne)
      {
         throw new EJBException (ne);
      }
   
public java.lang.String[]listAvailableCabins(int bedCount)

      if (cruise == null)
         throw new IncompleteConversationalState ();
      
      Connection con = null;
      PreparedStatement ps = null;;
      ResultSet result = null;
      
      try
      {
         Integer cruiseID = (Integer)cruise.getPrimaryKey ();
         Integer shipID = (Integer)cruise.getShip ().getPrimaryKey ();
         con = getConnection ();
         ps = con.prepareStatement (
         "select ID, NAME, DECK_LEVEL from CABIN "+
         "where SHIP_ID = ? and BED_COUNT = ? and ID NOT IN "+
         "(SELECT RCL.CABIN_ID FROM RESERVATION_CABIN_LINK AS RCL, RESERVATION AS R "+
         " WHERE RCL.RESERVATION_ID = R.ID AND R.CRUISE_ID = ?)");
         
         ps.setInt (1,shipID.intValue ());
         ps.setInt (2,bedCount);
         ps.setInt (3,cruiseID.intValue ());
         
         result = ps.executeQuery ();
         Vector vect = new Vector ();
         while(result.next ())
         {
            StringBuffer buf = new StringBuffer ();
            buf.append (result.getString (1));
            buf.append (',");
            buf.append (result.getString (2));
            buf.append (',");
            buf.append (result.getString (3));
            vect.addElement (buf.toString ());
         }
         
         String [] returnArray = new String[vect.size ()];
         vect.copyInto (returnArray);
         return returnArray;
      }
      catch (Exception e)
      {
         throw new EJBException (e);
      }
      finally
      {
         try
         { result.close (); } catch (Exception ignored)
         {}
         try
         { ps.close (); } catch (Exception ignored)
         {}
         try
         { con.close (); } catch (Exception ignored)
         {}
      }
   
protected voidremoveBeansInCollection(java.util.Collection beans)

      if (beans != null && beans.size () > 0)
      {
         java.util.Iterator iter = beans.iterator ();
         while (iter.hasNext ())
         {
            Object bean = iter.next ();
            if (bean instanceof javax.ejb.EJBObject)
               ((javax.ejb.EJBObject)bean).remove ();
            else if (bean instanceof javax.ejb.EJBLocalObject)
               ((javax.ejb.EJBLocalObject)bean).remove ();
            else
               throw new EJBException ("Unknown object: " + bean);
         }
      }
   
public voidsetCabinID(java.lang.Integer cabinID)

      try
      {
         CabinHomeLocal home = (CabinHomeLocal)
         jndiContext.lookup ("java:comp/env/ejb/CabinHomeLocal");
         cabin = home.findByPrimaryKey (cabinID);
      }
      catch (NamingException ne)
      {
         throw new EJBException (ne);
      }
   
public voidsetCruiseID(java.lang.Integer cruiseID)

      
      try
      {
         CruiseHomeLocal home = (CruiseHomeLocal)
         jndiContext.lookup ("java:comp/env/ejb/CruiseHomeLocal");
         cruise = home.findByPrimaryKey (cruiseID);
      }
      catch (NamingException ne)
      {
         throw new EJBException (ne);
      }
      
   
public voidsetSessionContext(javax.ejb.SessionContext cntx)

      ejbContext = cntx;
      try
      {
         jndiContext = new javax.naming.InitialContext ();
      }
      catch(NamingException ne)
      {
         throw new EJBException (ne);
      }