package stocks;
import java.io.*;
import java.sql.*;
import javax.sql.*;

// Bean class for session bean "StockQuotes"


import javax.ejb.*;
import javax.naming.*;
import java.rmi.*;

public class StockQuotesBean implements javax.ejb.SessionBean {

	//Instance member variables.
	//
	public String stock = null;
	
	int nPurchased = 0;
	float purchasePrice = 0.0F;

	// Session context, can be used to obtain handles etc
	//

	javax.ejb.SessionContext ejbSessionContext = null;

	//Getter/setter methods
	//

	public String getStock() throws java.rmi.RemoteException
	{
		return stock;
	}

	public void setStock( String stock ) throws java.rmi.RemoteException
	{
		this.stock = stock;
	}

    public float getPrice() throws java.rmi.RemoteException,
                                java.io.IOException, NamingException, SQLException
    {
        DataSource dataSource = (DataSource) (new InitialContext()).lookup( "java:comp/env/jdbc/stocksDataSource" );
        Connection con = dataSource.getConnection();
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery( "SELECT MIN(StockPrice) FROM Stocks WHERE StockName='" + stock + "'" );
        if ( ! rs.next()) {
            stmt.close();
            con.close();
            throw new java.rmi.RemoteException( "Not found" );
        }
        float result = rs.getFloat(1);
        stmt.close();
        con.close();
        if ( result == 0.0 )
            throw new java.rmi.RemoteException( "Not found" );
        return result;
    }

    public int getNumber() throws java.rmi.RemoteException, NamingException, SQLException
    {
        DataSource dataSource = (DataSource) (new InitialContext()).lookup( "java:comp/env/jdbc/stocksDataSource" );
        Connection con = dataSource.getConnection();
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery( "SELECT NumberAvailable FROM Stocks WHERE StockName='" + stock + "' AND StockPrice=(SELECT MIN(StockPrice) FROM Stocks WHERE StockName='" + stock + "')" );
        if ( ! rs.next()) {
            stmt.close();
            con.close();
            return 0;
        }
        int result = rs.getInt(1);
        stmt.close();
        con.close();
        return result;
    }
    
    public boolean buy( float maxPrice, int maxAmount ) throws java.rmi.RemoteException, NamingException, SQLException
    {
        // EJB Container takes care of the transaction handling.
        DataSource dataSource = (DataSource) (new InitialContext()).lookup( "java:comp/env/jdbc/stocksDataSource" );
        Connection con = dataSource.getConnection();
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery( "SELECT NumberAvailable,StockPrice FROM Stocks WHERE StockPrice < " + maxPrice + " AND StockName='" + stock + "' AND StockPrice=(SELECT MIN(StockPrice) FROM Stocks WHERE StockName='" + stock + "')" );
        if ( ! rs.next()) {
            stmt.close();
            con.close();
            return false;
        }
        int number = rs.getInt( 1 );
        purchasePrice = rs.getFloat(2);
        rs.close();
        if ( number > maxAmount ) {
            int newNumber = number - maxAmount;
            stmt.executeUpdate( "UPDATE Stocks SET NumberAvailable=" + newNumber + " WHERE StockPrice=" + purchasePrice + " AND StockName='" + stock + "'" );
            nPurchased = maxAmount;
        } else {
            stmt.executeUpdate( "DELETE FROM Stocks WHERE StockPrice=" + purchasePrice + " AND StockName='" + stock + "'" );
            nPurchased = number;
        }
        stmt.close();
        con.close();
        return true;
    }
    
    public int getNumPurchased() throws java.rmi.RemoteException
    {
        return nPurchased;
    }
    
    public float getPurchasePrice() throws java.rmi.RemoteException
    {
        return purchasePrice;
    }

	// The default ejbCreate method.
	//

	public void ejbCreate( 
			String stock
			)
		throws javax.ejb.CreateException, java.rmi.RemoteException
	{
		this.stock = stock;
	}

	// TBD:  If any other ejbCreate's are added manually to the home interface, define them.
	//

	// Other methods required in a session bean
	//

	public void setSessionContext( javax.ejb.SessionContext ejbSessionContext )
		throws RemoteException
	{
		this.ejbSessionContext = ejbSessionContext;
	}

	public void unsetSessionContext()
		throws RemoteException
	{
		this.ejbSessionContext = null;
	}

	public void ejbRemove()
		throws java.rmi.RemoteException, javax.ejb.EJBException
	{
	// TBD:  Do any processing here when instance is being removed
	}

	public void ejbActivate()
		throws java.rmi.RemoteException
	{
	// TBD:  Restore any saved resources
	}

	public void ejbPassivate()
		throws java.rmi.RemoteException
	{
	// TBD:  Save any resources here before bean is passivated
	}


}

