package isip.java.bullyse.bullydb;
import java.io.*;
import java.util.*;
import java.text.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
// This file displays a user's Holdings and the startup Member page
// when a user logs in.
public class Portfolio extends HttpServlet {
    // Initialize BullyDB Object
    static BullyDB sessionbase = new BullyDB();
    // System parameters are stored in the database, retrieve the values
    // through the BullyDB object.
    static String SystemURL = sessionbase.getURL();
    static String ServletURL = sessionbase.getServletURL();
    static String SystemEmail = sessionbase.getEmail();
    static String SystemPath = sessionbase.getPath();
    static String SystemName = sessionbase.getName();
    static String SystemShortName = sessionbase.getShortName();
    public void doGet(HttpServletRequest request, HttpServletResponse response)
	throws IOException, ServletException
    {
	// Initialize variables
	String AccountId = (String)null;
	String task = request.getParameter("task");
	// Get user's session
	HttpSession session = request.getSession();
	String SessionId = session.getId();
	// Retrieve AccountId
	AccountId = sessionbase.getAccountId(SessionId);
	if (AccountId == null) {
	    // User must not be logged in. Redirect accordingly.
	    response.sendRedirect(SystemURL + "/html/errors/not_logged_in.html");
	} else {
	    // Make sure user isn't locked
	    if (sessionbase.checkAccount(SessionId)) {
		task = "Locked";
	    }
	    
	    if (task == null) {
		// No task specified, simply display the user's Holdings
		// Set up the output stream
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		// Print top section
		sessionbase.printTop(out, 2);
		sessionbase.QuoteFlash(out);
		// Fill in the blanks from the template
		out.println("");
		out.println("
");
		out.println(" ");
		out.println("| ");
		out.println(" ");
		out.println(" ");
		// Call the BullyDB object to print users holdings.
		// The BullyDB object calls up the User object and prints the
		// holdings through that.
		sessionbase.showHoldings(out, 1, SessionId);
		// Print bottom section
		out.println("
 | ");
		sessionbase.printBot(out, SessionId);
		out.close();
	    } else if(task.equals("Locked")) {
		String Reason = (String)null;
		// Set up the output stream
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		String query = "select Reason from Locks where AccountId = " + sessionbase.getAccountId(SessionId);
		ResultSet result = sessionbase.doQuery(query, SessionId);
		try {
		    while (result.next()) {
			Reason = result.getString("Reason");
		    }
		} catch (Exception e) {
		    e.printStackTrace();
		}
		sessionbase.printTop(out, 2);
		sessionbase.QuoteFlash(out);
		// Fill in the blanks from the template
		out.println("");
		out.println(" | ");
		out.println(" ");
		out.println("| ");
		out.println(" ");
		out.println(" ");
		out.println("
 Error: Account Locked");
		out.println("Your account is currently locked.Reason:
 ");
		out.println(Reason);
		out.println("
 Email " + SystemEmail + " with any questions.");
		out.println("
 | ");
		sessionbase.printBot(out, SessionId);
		out.close();
		sessionbase.doLogout(SessionId);
		session.invalidate();
	    } else if(task.equals("Members")) {
		// When a member logs in, they are redirected to this
		// section.
		// Set up output stream
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		String Name = (String)null;
		String query = (String)null;
		ResultSet result = null;
		// Retrieve user's first name
		Name = sessionbase.getFname(SessionId);
		sessionbase.printTop(out, 1);
		sessionbase.QuoteFlash(out);
		// Fill in the blanks from the template
		out.println("");
		out.println(" | ");
		out.println(" ");
		out.println("| ");
		out.println(" ");
		out.println(" ");
		out.println("Welcome, " + Name + "! Here is a summary of today's market activity, pulled live from our chart generation site.
 
 ");
		sessionbase.displayGraph(out, "1", "SYS", 0);
		out.println("
 | ");
		sessionbase.printBot(out, SessionId);
		out.close();
	    } else if(task.equals("history")) {
		// ******************
		// Not currently used
		// ******************
		String query = (String)null;
		String Symbol = request.getParameter("Symbol");
		String Trans = (String)null;
		String name = (String)null;
		java.util.Date transDate = new java.util.Date();
		java.util.Date transTime = new java.util.Date();
		int numShares = 0;
		double shareCost = 0.00;
		ResultSet result = null;
		Locale locale = new Locale("en","US");
		// Set up decimal formatting for digits.
		// This will force at least $0.00.
		DecimalFormat curFormat = new DecimalFormat("$###,###,##0.00");
		// Set the format for time and date
		DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG, locale);
		DateFormat timeFormat = DateFormat.getTimeInstance(DateFormat.MEDIUM, locale);
		// Set up output stream
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		query = "select Name from Companies where Symbol = '" + Symbol + "'";
		result = sessionbase.doQuery(query, SessionId);
		try {
		    while(result.next()) {
			name = result.getString("Name");
		    }
		} catch(Exception e) {
		    e.printStackTrace();
		}
		query = "select * from Trades where AccountId = " + AccountId + " and Symbol = '" + Symbol + "'";
		result = sessionbase.doQuery(query, SessionId);
		sessionbase.printTop(out, 2);
		sessionbase.QuoteFlash(out);
		// Fill in the blanks from the template
		out.println("");
		out.println(" | ");
		out.println(" ");
		out.println("| ");
		out.println(" ");
		out.println(" ");
		out.println("Personal Trading History of " + name + "(" + Symbol + ")
 ");
		out.println("
 ");
		out.println("");
		out.println("");
		try {
		    while(result.next()) {
			Trans = result.getString("Trans");
			numShares = result.getInt("NumShares");
			shareCost = result.getDouble("TradedAt");
			transDate = result.getDate("Dtime");
			transTime = result.getTime("Dtime");
			if(Trans.equals("B")) {
			    Trans = "Buy";
			} else {
			    Trans = "Sell";
			}
			out.println("| Transaction Date | Transaction Type | Number of Shares | Trade Cost |  ");
		    }
		} catch(Exception e) {
		    e.printStackTrace();
		}
		out.println("| | " + dateFormat.format(transDate) + " |  | " + timeFormat.format(transTime) + " | 
 | " + Trans + " | " + numShares + " | " + curFormat.format(shareCost) + " |  | ");
		sessionbase.printBot(out, SessionId);
	    }
	}
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
	throws IOException, ServletException
    {
	// A user shouldn't be here. Nothing fancy, just display Error
	response.setContentType("text/html");
	PrintWriter out = response.getWriter();
	out.println("Error");
	out.close();
    }
    static public void reload() {
	SystemURL = sessionbase.getURL();
	ServletURL = sessionbase.getServletURL();
	SystemEmail = sessionbase.getEmail();
	SystemPath = sessionbase.getPath();
	SystemName = sessionbase.getName();
	SystemShortName = sessionbase.getShortName();
    }
} |  |  |  |  |