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(ServletURL + ".nonMember?task=LI&Type=1");
} else {
// Make sure user isn't locked
if (sessionbase.checkAccount(SessionId)) {
task = "Locked";
}
if (task == null) {
String query = (String)null;
ResultSet result = null;
String Type = (String)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);
out.println("
");
// Check the type of user. If this is a regular user,
// use the BullyDB showHoldings method. If it is a
// company, use the BullyDB showCompany.
query = "select Type from Account where AccountId = " + sessionbase.getAccountId(SessionId);
result = sessionbase.doQuery(query);
try {
while (result.next()) {
Type = result.getString("Type");
}
} catch (Exception e) {
e.printStackTrace();
}
if (Type.equals("U")) {
out.println("
");
sessionbase.showHoldings(out, 1, SessionId);
out.println("");
} else {
out.println("");
sessionbase.showCompany(out, SessionId);
out.println("");
}
// Print bottom section
sessionbase.printBot(out, SessionId, 1);
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);
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.");
sessionbase.printBot(out, SessionId, 1);
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);
out.println("
");
// Check the User Type. Username will be displayed for Company Users.
String Type = "";
query = "select Type from Account where AccountId = " + sessionbase.getAccountId(SessionId);
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
Type = result.getString("Type");
}
} catch (Exception e) {
e.printStackTrace();
}
if (Type.equals("C")) {
out.println("Welcome, " + sessionbase.getUsername(SessionId) + "! Here is a summary of today's market activity, pulled live from our chart generation site.
");
} else {
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);
sessionbase.printBot(out, SessionId, 1);
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);
out.println("
");
out.println("Personal Trading History of " + name + "(" + Symbol + ")
");
out.println("");
out.println("Transaction Date | Transaction Type | Number of Shares | Trade Cost |
");
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("" + dateFormat.format(transDate) + " | " + timeFormat.format(transTime) + " |
| " + Trans + " | " + numShares + " | " + curFormat.format(shareCost) + " |
");
}
} catch(Exception e) {
e.printStackTrace();
}
out.println("
");
sessionbase.printBot(out, SessionId, 1);
}
}
}
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();
}
}