package com.titan.travelagent;
import com.titan.cruise.*;
import com.titan.cabin.*;
import com.titan.customer.*;
import com.titan.reservation.*;
import com.titan.processpayment.*;
import java.rmi.RemoteException;
import java.util.Properties;
import javax.naming.InitialContext;
import javax.naming.Context;
import com.titan.ship.*;
import java.sql.*;
import java.util.Vector;
public class TravelAgentBean implements javax.ejb.SessionBean {
public Customer customer;
public Cruise cruise;
public Cabin cabin;
public javax.ejb.SessionContext ejbContext;
public transient javax.naming.Context jndiContext;
public void ejbCreate(Customer cust){
customer = cust;
}
public int getCustomerID( )throws RemoteException{
if(customer == null)
throw new RemoteException();
return ((CustomerPK)customer.getPrimaryKey()).id;
}
public int getCruiseID( )throws RemoteException{
if(cruise == null)
throw new RemoteException();
return ((CruisePK)cruise.getPrimaryKey()).id;
}
public int getCabinID( )throws RemoteException{
if(cabin==null)
throw new RemoteException();
return ((CabinPK)cabin.getPrimaryKey()).id;
}
public void setCabinID(int cabinID)
throws RemoteException, javax.ejb.FinderException{
CabinHome home = (CabinHome)getHome("jndiName_CabinHome");
CabinPK pk = new CabinPK();
pk.id=cabinID;
cabin = home.findByPrimaryKey(pk);
}
public void setCruiseID(int cruiseID)
throws RemoteException, javax.ejb.FinderException{
CruiseHome home = (CruiseHome)getHome("jndiName_CruiseHome");
cruise = home.findByPrimaryKey(new CruisePK(cruiseID));
}
public Ticket bookPassage(CreditCard card, double price)
throws RemoteException,
IncompleteConversationalState, DoubleBookingException{
if(customer == null || cruise == null || cabin == null){
throw new IncompleteConversationalState();
}
try{
ReservationHome resHome =
(ReservationHome) getHome("jndiName_ReservationHome");
Reservation reservation =
resHome.create(customer, cruise, cabin,price);
ProcessPaymentHome ppHome =
(ProcessPaymentHome) getHome("jndiName_ProcessPaymentHome");
ProcessPayment process = ppHome.create();
process.byCredit(customer, card, price);
Ticket ticket = new Ticket(customer,cruise,cabin,price);
return ticket;
}catch(javax.ejb.DuplicateKeyException dke){
throw new DoubleBookingException(
"The cabin is already reserved");
}catch(Exception ce){
throw new RemoteException("Book Passage",ce);
}
}
public String [] listAvailableCabins(int bedCount)
throws RemoteException, IncompleteConversationalState{
if(cruise == null) throw new IncompleteConversationalState();
int cruiseID = ((CruisePK)cruise.getPrimaryKey()).id;
int shipID = cruise.getShipID();
Connection con = null;
PreparedStatement ps = null;;
ResultSet result = null;
try {
con = getConnection();
ps = con.prepareStatement(
"select ID, NAME, DECK_LEVEL from CABIN "+
"where SHIP_ID = ? and ID NOT IN "+
"(SELECT CABIN_ID FROM RESERVATION WHERE CRUISE_ID = ?)");
ps.setInt(1,shipID);
ps.setInt(2,cruiseID);
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 (SQLException se) {
throw new RemoteException (se.getMessage());
}
finally {
try {
if (result != null) result.close();
if (ps != null) ps.close();
if (con!= null) con.close();
}catch(SQLException se){se.printStackTrace();}
}
}
public void ejbRemove(){}
public void ejbActivate(){}
public void ejbPassivate(){
try{
jndiContext.close();
}catch(javax.naming.NamingException ne){}
jndiContext = null;
}
public void setSessionContext(javax.ejb.SessionContext cntx){
ejbContext = cntx;
}
protected Object getHome(String name)throws RemoteException{
try{
String jndiName =
ejbContext.getEnvironment().getProperty(name);
return getJndiContext().lookup(jndiName);
}catch(javax.naming.NamingException ne){
throw new RemoteException("Could not lookup ("+name+")",ne);
}
}
protected javax.naming.Context getJndiContext()
throws javax.naming.NamingException{
if(jndiContext!=null)
return jndiContext;
// ... specify the JNDI properties specific to the vendor
jndiContext = new InitialContext(p);
return jndiContext;
}
private Connection getConnection() throws SQLException {
return DriverManager.getConnection(
ejbContext.getEnvironment().getProperty("jdbcURL"));
}
}
|