");
if (SystemFreeze.equals("N")) {
out.println("Freeze all trading on " + SystemName + ". ");
out.println("This will prevent any trading from taking place. Users can still ");
out.println("log in but will not be able to buy or sell.
");
} else {
out.println("Allow trading on " + SystemName + ". ");
out.println("This will allow buying and selling to resume.
");
}
if (SystemLock.equals("N")) {
out.println("Lock " + SystemName + ". ");
out.println("This will prevent anyone from being able to log in. ");
} else {
out.println("Unlock " + SystemName + ". ");
out.println("This will allow users to log in. ");
}
sessionbase.printBot(out, SessionId, 3);
out.close();
} else if (task.equals("showOwners")) {
Symbol = request.getParameter("Symbol");
String Name = (String)null;
ResultSet users = null;
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 Username from Account where Username != 'root' order by Username";
users = sessionbase.doQuery(query);
sessionbase.printTop(out, 25);
out.println(" ");
out.println("View Owners of " + Name + "(" + Symbol + ") ");
try {
while (users.next()) {
userName = users.getString("Username");
query = "select NumShares, Trans from " + userName + " where Symbol = '" + Symbol + "' and Trans = 'C' or Symbol = '" + Symbol + "' and Trans = 'S' or Symbol = '" + Symbol + "' and Trans = 'FS' order by Trans";
result = sessionbase.doQuery(query, SessionId);
int userShares = 0;
Trans = (String)null;
while (result.next()) {
userShares = result.getInt("NumShares");
Trans = result.getString("Trans");
if (Trans.equals("C")) {
out.println("User " + userName + " owns " + userShares + " shares. ");
} else {
out.println("User " + userName + " is selling " + userShares + " shares. ");
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
sessionbase.printBot(out, SessionId, 3);
out.close();
} else if (task.equals("confirmresetUsers")) {
// We will need to change all user Balances to the default values
// keeping in mind User and Company have different defaults. Then
// all user holdings will be removed and given back to the Company.
String Username = (String)null;
// Loop through the list of regular users
query = "select AccountId from Account where AccountId != 1 and Type != 'C'";
result = sessionbase.doQuery(query);
try {
while (result.next()) {
Username = result.getString("AccountId");
deleteHoldings(Username);
resetBalance(Username, "U");
}
} catch (Exception e) {
e.printStackTrace();
}
// Cycle through all Company users to reset their balance
query = "select AccountId from Account where Type = 'C'";
result = sessionbase.doQuery(query);
try {
while (result.next()) {
Username = result.getString("AccountId");
resetBalance(Username, "C");
}
} catch (Exception e) {
e.printStackTrace();
}
// Delete all Frozen transactions
query = "delete from Frozen";
sessionbase.doUpdate(query);
// Everything should be done, print a success message
sessionbase.printTop(out, 21);
out.println(" ");
out.println("Clear Holdings Complete ");
out.println("All user holdings have been cleared from " + SystemName + " ");
sessionbase.printBot(out, SessionId, 3);
out.close();
} else if (task.equals("confirmresetQueues")) {
// All pending or frozen sales will be deleted and the Selling shares
// will be restored to the original owner.
ResultSet result2 = null;
// First loop through all Company Queue tables then go through
// the Frozen table. Lastly, all User tables, deleting their
// pending and frozen jobs
query = "select Symbol from Companies where Symbol != 'SYS'";
result = sessionbase.doQuery(query);
try {
while (result.next()) {
Symbol = result.getString("Symbol");
// Delete all Buy offers
query = "delete from qt" + Symbol.toLowerCase() + " where Trans = 'B'";
sessionbase.doUpdate(query);
// Restore all Sell offers to the user
query = "select AccountId, NumShares from qt" + Symbol.toLowerCase();
result2 = sessionbase.doQuery(query);
while (result2.next()) {
Owner = result2.getString("AccountId");
Shares = result2.getInt("NumShares");
sessionbase.modHoldings(sessionbase.getUser(Owner), Symbol, Shares);
}
// Delete the remaining offers
query = "delete from qt" + Symbol.toLowerCase();
sessionbase.doUpdate(query);
}
} catch (Exception e) {
e.printStackTrace();
}
query = "delete from Frozen where Trans = 'B'";
sessionbase.doUpdate(query);
// Restore all Sell offers to the user
query = "select AccountId, NumShares, Symbol from Frozen";
result = sessionbase.doQuery(query);
try {
while (result.next()) {
Owner = result.getString("AccountId");
Shares = result.getInt("NumShares");
Symbol = result.getString("Symbol");
sessionbase.modHoldings(sessionbase.getUser(Owner), Symbol, Shares);
}
} catch (Exception e) {
e.printStackTrace();
}
// Now go through all User holdings, deleting anything pending
// or frozen.
query = "select Username from Account";
result = sessionbase.doQuery(query);
try {
while (result.next()) {
Owner = result.getString("Username");
query = "delete from " + Owner + " where Trans = 'B' or Trans = 'S' or Trans = 'FB' or Trans = 'FS'";
sessionbase.doUpdate(query);
}
} catch (Exception e) {
e.printStackTrace();
}
// Everything should be done, print a success message
sessionbase.printTop(out, 21);
out.println(" ");
out.println("Clear Transactions Complete ");
out.println("All transactions have been cleared from " + SystemName + " ");
sessionbase.printBot(out, SessionId, 3);
out.close();
} else if (task.equals("confirmdeleteUsers")) {
// Will need to delete all except Company users. All shares will be
// restored to the Companies.
ResultSet result2 = null;
String User = (String)null;
query = "select Username from Account where AccountId != 1 and Type != 'C'";
result = sessionbase.doQuery(query);
System.out.println("Hi.");
try {
while (result.next()) {
User = result.getString("Username");
AccountId = sessionbase.getAccountId(User, 1);
deleteHoldings(AccountId);
query = "drop table " + User;
sessionbase.doUpdate(query);
query = "delete from Frozen where AccountId = " + AccountId;
sessionbase.doUpdate(query);
query = "delete from Portfolio where AccountId = " + AccountId;
sessionbase.doUpdate(query);
query = "delete from Account where Username = '" + User + "'";
sessionbase.doUpdate(query);
// Delete any user pending jobs from the Company queues
query = "select Symbol from Companies where Symbol != 'SYS'";
result2 = sessionbase.doQuery(query);
while (result2.next()) {
Symbol = result2.getString("Symbol");
query = "delete from qt" + Symbol.toLowerCase() + " where AccountId = " + AccountId;
sessionbase.doUpdate(query);
}
}
} catch (Exception e) {
e.printStackTrace();
}
// Everything should be done, print a success message
sessionbase.printTop(out, 21);
out.println(" ");
out.println("Delete Users Complete ");
out.println("All users have been removed from " + SystemName);
sessionbase.printBot(out, SessionId, 3);
out.close();
} else if (task.equals("confirmdeleteCompanies")) {
// Delete all Companies and their users. User holdings will be converted
// to cash based on the current value of the company.
// Get a list of companies
query = "select Symbol from Companies where Symbol != 'SYS'";
result = sessionbase.doQuery(query);
try {
while (result.next()) {
Symbol = result.getString("Symbol");
deleteCompany(Symbol);
}
} catch (Exception e) {
e.printStackTrace();
}
// Clear SYS in the Companies table
query = "update Companies set Tshares = 1, Open = 1, Last = 1, Volume = 1, 52WkHi = 1, 52WkLo = 1, DayHi = 1, DayLo = 1, Previous = 1 where Symbol = 'SYS'";
sessionbase.doUpdate(query);
// Everything should be done, print a success message
sessionbase.printTop(out, 21);
out.println(" ");
out.println("Delete Companies Complete ");
out.println("All companies have been removed from " + SystemName);
sessionbase.printBot(out, SessionId, 3);
out.close();
} else if (task.equals("confirmStartOver")) {
String Username = (String)null;
String UserId = (String)null;
String Company = (String)null;
// We're clearing everything. Start with the users.
// Limit the results to make sure we don't touch the admin account
query = "select Username, AccountId from Account where AccountId > 1";
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
Username = result.getString("Username");
UserId = result.getString("AccountId");
// For every user in the list we must:
// Delete their Holdings table
// Remove them from the Accounts table
// Remove them from the Personal table
// Remove them from the Portfolio table
query = "drop table " + Username;
sessionbase.doUpdate(query, SessionId);
query = "delete from Account where Username = '" + Username + "'";
sessionbase.doUpdate(query, SessionId);
query = "delete from Personal where AccountId = '" + UserId + "'";
sessionbase.doUpdate(query, SessionId);
query = "delete from Portfolio where AccountId = '" + UserId + "'";
sessionbase.doUpdate(query, SessionId);
}
} catch (Exception e) {
e.printStackTrace();
}
// Now get the companies
query = "select Symbol from Companies where Symbol != 'SYS'";
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
Company = result.getString("Symbol");
// For each company we must:
// Delete the Queue table
// Delete the History table
// Delete the Extended History table
// Remove from Companies table
query = "drop table qt" + Company.toLowerCase();
sessionbase.doUpdate(query, SessionId);
query = "drop table ht" + Company.toLowerCase();
sessionbase.doUpdate(query, SessionId);
query = "drop table eht" + Company.toLowerCase();
sessionbase.doUpdate(query, SessionId);
query = "delete from Companies where Symbol = '" + Company + "'";
sessionbase.doUpdate(query, SessionId);
}
} catch (Exception e) {
e.printStackTrace();
}
// Clear any Frozen jobs, News items and Locked accounts
query = "delete from News";
sessionbase.doUpdate(query, SessionId);
query = "delete from Frozen";
sessionbase.doUpdate(query, SessionId);
query = "delete from Locks";
sessionbase.doUpdate(query, SessionId);
// Final sweep of Account and Portfolio to make sure they are clean
query = "delete from Account where AccountId != 1";
sessionbase.doUpdate(query, SessionId);
query = "delete from Portfolio where AccountId != 1";
sessionbase.doUpdate(query, SessionId);
// Clear out any potential holdings in the Admin portfolio
// Admin username may change so get it
query = "select Username from Account where AccountId = 1";
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
Username = result.getString("Username");
}
} catch (Exception e) {
e.printStackTrace();
}
query = "delete from " + Username;
sessionbase.doUpdate(query, SessionId);
// Clear SYS in the Companies table
query = "update Companies set Tshares = 1, Open = 1, Last = 1, Volume = 1, 52WkHi = 1, 52WkLo = 1, DayHi = 1, DayLo = 1, Previous = 1 where Symbol = 'SYS'";
sessionbase.doUpdate(query);
// Everything should be done, print a success message
sessionbase.printTop(out, 21);
out.println(" ");
out.println("Restart Complete ");
out.println(SystemName + " has been cleared of all data. ");
sessionbase.printBot(out, SessionId, 3);
out.close();
} else if (task.equals("resetUsers")) {
sessionbase.printTop(out, 21);
out.println(" ");
out.println("
Confirm
");
out.println("
Are you certain you want to clear all user holdings? ");
out.println("");
sessionbase.printBot(out, SessionId, 3);
out.close();
} else if (task.equals("resetQueues")) {
sessionbase.printTop(out, 21);
out.println(" ");
out.println("
Confirm
");
out.println("
Are you certain you want to clear all trade queues? ");
out.println("");
sessionbase.printBot(out, SessionId, 3);
out.close();
} else if (task.equals("deleteUsers")) {
sessionbase.printTop(out, 21);
out.println(" ");
out.println("
Confirm
");
out.println("
Are you certain you want to delete all users from " + SystemName + "? ");
out.println("");
sessionbase.printBot(out, SessionId, 3);
out.close();
} else if (task.equals("deleteCompanies")) {
sessionbase.printTop(out, 21);
out.println(" ");
out.println("
Confirm
");
out.println("
Are you certain you want to delete all companies from " + SystemName + "? ");
out.println("");
sessionbase.printBot(out, SessionId, 3);
out.close();
} else if (task.equals("StartOver")) {
sessionbase.printTop(out, 21);
out.println(" ");
out.println("
Confirm
");
out.println("
Are you certain you want to fully clear off " + SystemName + "? ");
out.println("");
sessionbase.printBot(out, SessionId, 3);
out.close();
} else if (task.equals("reset")) {
// This section gives access to various methods that will clear
// out portions of the Exchange. This can be useful when returning
// the system to an initial trading status.
sessionbase.printTop(out, 21);
out.println(" ");
out.println("" + SystemShortName + " Reset Options
");
out.println("Note: Be careful when using these options. They should only be used when resetting the system to an initial state.
");
out.println("
User Reset Reset the holdings and balance of all users in the system, this sets the users balance back to the default value.
");
out.println("
Queue Reset Resets all Exchange trading queues so that no trades are currently in progress.
");
out.println("
User Delete Deletes all standard users from the system.
");
out.println("
Company Delete Deletes all companies from the system.");
out.println("
Fresh Start Wipes everything. ");
sessionbase.printBot(out, SessionId, 3);
out.close();
} else if (task.equals("params")) {
// All parameters should already be stored except those for the
// market changes. Retrieve those.
double changeCap = 0.00;
double changeVal = 0.00;
double Commission = 0.00;
double userBalance = 0.00;
double companyBalance = 0.00;
query = "select ChangeCap, ChangeValue, Commission, userBalance, compBalance from Parameters";
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
changeCap = result.getDouble("ChangeCap");
changeVal = result.getDouble("ChangeValue");
Commission = result.getDouble("Commission");
userBalance = result.getDouble("userBalance");
companyBalance = result.getDouble("compBalance");
}
} catch (Exception e) {
e.printStackTrace();
}
sessionbase.printTop(out, 21);
out.println(" ");
out.println("" + SystemShortName + " Parameters ");
out.println("Here you can change parameters affecting how the Exchange works.
");
out.println("Warning: Several of these parameters are vital to the Exchange working properly. Please be careful when making changes. ");
out.println("");
out.println(" ");
out.println("If you are wanting to clean out some area of the system, go to the reset options.");
sessionbase.printBot(out, SessionId, 3);
out.close();
} else if (task.equals("confDelUsr")) {
String User = request.getParameter("User");
String AcId = (String)null;
Vector Syms = new Vector();
double Bal = 0.00;
AcId = sessionbase.getAccountId(User, 1);
deleteHoldings(AcId);
query = "drop table " + User;
sessionbase.doUpdate(query);
// Get user's balance, add this value to it, and add balance to
// the Administrative account
query = "select Balance from Portfolio where AccountId = " + AcId;
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
Bal = result.getDouble("Balance");
}
} catch (Exception e) {
e.printStackTrace();
}
sessionbase.modBalance("1", Bal, "+");
query = "delete from Portfolio where AccountId = " + AcId;
sessionbase.doUpdate(query);
query = "delete from Locks where AccountId = " + AcId;
sessionbase.doUpdate(query);
query = "delete from Account where AccountId = " + AcId;
sessionbase.doUpdate(query);
// User is gone, redirect back to Users main
response.sendRedirect(ServletURL + ".Admin?task=account");
} else if (task.equals("confDelCmp")) {
Symbol = request.getParameter("Symbol");
// Get company name
String Name = (String)null;
query = "select Name from Companies where Symbol = '" + Symbol + "'";
result = sessionbase.doQuery(query);
try {
while (result.next()) {
Name = result.getString("Name");
}
} catch (Exception e) {
e.printStackTrace();
}
deleteCompany(Symbol);
// Add Breaking News item
String Title = "Company Closing: " + Name + "(" + Symbol + ")";
String Author = "Exchange Administrator";
String Date = "NOW()";
String Type = "2";
String Story = "Announcing the closing of " + Name + "(" + Symbol + ") .
Market administrators today have liquidated all holdings of " + Symbol + " and have closed all trading of this company.";
addNews(Title, Author, Type, Story, SessionId);
// Redirect to Company main
response.sendRedirect(ServletURL + ".Admin?task=companies");
} else if (task.equals("showNews")) {
// Display the News page
displayNews(request, response, SessionId);
} else if (task.equals("showHeadlines")) {
displayHeadlines(request, response, SessionId);
} else if (task.equals("showArchives")) {
displayArchives(request, response, SessionId);
} else if (task.equals("showArticle")) {
displayArticle(request, response, SessionId);
} else if (task.equals("showAll")) {
// List Company quotes
displayAll(request, response, SessionId);
out.close();
} else if (task.equals("companies")) {
// Manage user accounts
sessionbase.printTop(out, 24);
out.println("
");
out.println("Please select an option: ");
out.println("
");
sessionbase.printBot(out, SessionId, 3);
out.close();
} else if (task.equals("lockCompany")) {
String companyName = request.getParameter("companySym");
if (companyName == null) {
companyName = "";
}
sessionbase.printTop(out, 24);
out.println(" ");
out.println("Lock Company");
out.println("");
sessionbase.printBot(out, SessionId, 3);
out.close();
} else if (task.equals("unlockCompany")) {
String companySym = request.getParameter("companySym");
if (companySym == null) {
companySym = "";
}
sessionbase.printTop(out, 24);
out.println(" ");
out.println("Unlock Company");
out.println("");
sessionbase.printBot(out, SessionId, 3);
out.close();
} else if (task.equals("modifyUser")) {
sessionbase.printTop(out, 22);
out.println(" ");
out.println("Modify User");
out.println("");
sessionbase.printBot(out, SessionId, 3);
out.close();
} else if (task.equals("modifyCompany")) {
sessionbase.printTop(out, 24);
out.println(" ");
out.println("Modify Company");
out.println("");
sessionbase.printBot(out, SessionId, 3);
out.close();
} else if (task.equals("modCompany")) {
Symbol = "";
String Name = (String)null;
String Last = (String)null;
String numShares = (String)null;
String Open = (String)null;
String Volume = (String)null;
String ftHi = (String)null;
String ftLo = (String)null;
String DayHi = (String)null;
String DayLo = (String)null;
String Previous = (String)null;
String Email = (String)null;
String Locked = (String)null;
String CmpAccountId = (String)null;
Symbol = request.getParameter("companySym");
double Bal = 0.00;
query = "select * from Companies where Symbol = '" + Symbol + "'";
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
Symbol = result.getString("Symbol");
Name = result.getString("Name");
Last = result.getString("Last");
numShares = result.getString("Tshares");
Open = result.getString("Open");
Volume = result.getString("Volume");
ftHi = result.getString("52WkHi");
ftLo = result.getString("52WkLo");
DayHi = result.getString("DayHi");
DayLo = result.getString("DayLo");
Previous = result.getString("Previous");
Locked = result.getString("Locked");
}
} catch (Exception e) {
e.printStackTrace();
}
query = "select Email, AccountId from Account where Username = '" + Symbol + "'";
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
Email = result.getString("Email");
CmpAccountId = result.getString("AccountId");
}
} catch (Exception e) {
e.printStackTrace();
}
// Get Company balance
query = "select Balance from Portfolio where AccountId = " + CmpAccountId;
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
Bal = result.getDouble("Balance");
}
} catch (Exception e) {
e.printStackTrace();
}
if (Symbol.length() < 1) {
sessionbase.printTop(out, 24);
out.println(" ");
out.println("
");
out.println("
");
out.println("Modify a Company- " + Symbol + "");
out.println("Error: Invalid Symbol Entered ");
out.println("Please go Back and enter a different symbol. ");
sessionbase.printBot(out, SessionId, 3);
out.close();
} else {
sessionbase.printTop(out, 24);
out.println(" ");
out.println("
");
out.println("
");
out.println("Modify a Company- " + Symbol + "");
out.println("
");
showHoldings(out, userName, SessionId);
sessionbase.printBot(out, SessionId, 3);
out.close();
} else if (task.equals("modUser")) {
String fName = (String)null;
String MI = (String)null;
String lName = (String)null;
String Email = (String)null;
String AccId = (String)null;
userName = "";
userName += request.getParameter("User");
query = "select Username, Fname, Lname, Mi, Email, AccountId from Account where Username = '" + userName + "'";
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
fName = result.getString("Fname");
MI = result.getString("Mi");
lName = result.getString("Lname");
Email = result.getString("Email");
AccId = result.getString("AccountId");
userName = result.getString("Username");
}
} catch (Exception e) {
e.printStackTrace();
}
// Get user's Balance
String Bal = "";
query = "select Balance from Portfolio where AccountId = " + AccId;
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
Bal = result.getString("Balance");
}
} catch (Exception e) {
e.printStackTrace();
}
// Make sure user is valid
if (userName == null) {
sessionbase.printTop(out, 22);
out.println(" ");
out.println("Modify User ");
out.println("Error: ");
out.println("Invalid User Specified.");
out.println("");
sessionbase.printBot(out, SessionId, 3);
out.close();
} else {
sessionbase.printTop(out, 22);
out.println(" ");
out.println("
");
out.println("");
out.println("
");
sessionbase.printBot(out, SessionId, 3);
out.close();
}
} else if (task.equals("deleteUser")) {
String delUser = request.getParameter("User");
if (delUser == null) {
delUser = "";
}
sessionbase.printTop(out, 22);
out.println(" ");
out.println("Delete a User Account ");
out.println("List Users ");
out.println("");
sessionbase.printBot(out, SessionId, 3);
out.close();
} else if (task.equals("deleteArticle")) {
String newsId = request.getParameter("Id");
String Title = "";
String Author = "";
query = "select Title, Author from News where Id = " + newsId;
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
Title += result.getString("Title");
Author += result.getString("Author");
}
} catch (Exception e) {
e.printStackTrace();
}
if (Title.length() < 1) {
sessionbase.printTop(out, 26);
out.println(" ");
out.println("Error! ");
out.println("The article entered does not exist. Please go back and try again.");
sessionbase.printBot(out, SessionId, 3);
out.close();
} else {
sessionbase.printTop(out, 26);
out.println(" ");
out.println("Delete a News Article ");
out.println("Are you sure you want to delete the article ");
out.println(Title + " by " + Author + "? ");
out.println("");
sessionbase.printBot(out, SessionId, 3);
out.close();
}
} else if (task.equals("confirmDeleteArticle")) {
String newsId = request.getParameter("Id");
// Delete News item
// First check the Type of article. If this is a Headline
// article, use the most recent News article for the Headline.
String Type = (String)null;
query = "select Type from News where Id = " + newsId;
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
Type = result.getString("Type");
}
} catch (Exception e) {
e.printStackTrace();
}
if (Type.equals("3")) {
query = "select max(Stamp) from News where Type = 1 or Type = 2";
result = sessionbase.doQuery(query, SessionId);
String Stamp = (String)null;
try {
while (result.next()) {
Stamp = result.getString("max(Stamp)");
}
} catch (Exception e) {
e.printStackTrace();
}
// The Timestamp is probably unique but just in case, do
// another query for a specific News Id
String headId = "";
query = "select Id from News where Stamp = " + Stamp;
result = sessionbase.doQuery(query, SessionId);
try {
// If there is more than one that match, we only want
// one of them, so no need for a while loop.
result.next();
headId += result.getString("Id");
} catch (Exception e) {
e.printStackTrace();
}
// Switch this article to Type 3
if (headId.length() > 0) {
query = "update News set Type = 3 where Id = " + headId;
sessionbase.doUpdate(query, SessionId);
} else {
// There is no remaining news! Add a default
addNews("No News", "root", "3", "No news is good news.", SessionId);
}
}
// Delete the article
query = "delete from News where Id = " + newsId;
sessionbase.doUpdate(query, SessionId);
response.sendRedirect(ServletURL + ".Admin?task=showNews");
} else if (task.equals("delCompany")) {
String delCmp = request.getParameter("Symbol");
if (delCmp == null) {
delCmp = "";
}
sessionbase.printTop(out, 24);
out.println(" ");
out.println("Delete a Company ");
out.println("List Companies ");
out.println("");
sessionbase.printBot(out, SessionId, 3);
out.close();
} else if (task.equals("createUser")) {
double startBal = 0;
query = "select userBalance from Parameters";
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
startBal = result.getDouble("userBalance");
}
} catch (Exception e) {
e.printStackTrace();
}
sessionbase.printTop(out, 22);
out.println(" ");
out.println("Create a New User");
out.println("
");
out.println("");
sessionbase.printBot(out, SessionId, 3);
out.close();
} else if (task.equals("viewCompanies")) {
String Name = (String)null;
Symbol = (String)null;
String Last = (String)null;
String Locked = (String)null;
query = "select Name, Symbol, Last, Locked from Companies where Symbol != 'SYS' order by Symbol";
result = sessionbase.doQuery(query, SessionId);
sessionbase.printTop(out, 24);
out.println(" ");
out.println("View Companies");
out.println("
");
out.println("");
out.println("
");
out.println("
");
out.println("");
out.println("
Symbol
Company Name
Task
");
try {
while (result.next()) {
Symbol = result.getString("Symbol");
Name = result.getString("Name");
Last = result.getString("Last");
Locked = result.getString("Locked");
if (Locked.equals("N")) {
out.println("
");
sessionbase.printBot(out, SessionId, 3);
out.close();
} else if (task.equals("viewUsers")) {
String fName = (String)null;
String MI = (String)null;
String lName = (String)null;
String Last = (String)null;
String Email = (String)null;
String Create = (String)null;
java.util.Date lastDate = new java.util.Date();
java.util.Date lastTime = new java.util.Date();
Locale locale = new Locale("en","US");
// Set the format for time and date
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG, locale);
DateFormat timeFormat = DateFormat.getTimeInstance(DateFormat.MEDIUM, locale);
query = "select Username, Fname, Lname, LastLogin from Account where Type != 'C' order by Username";
result = sessionbase.doQuery(query, SessionId);
sessionbase.printTop(out, 22);
out.println(" ");
out.println("
");
query = "select Username, Fname, Lname, LastLogin from Account where Type != 'U' order by Username";
result = sessionbase.doQuery(query, SessionId);
out.println("
");
out.println("Company Users: ");
out.println("
");
out.println("");
out.println("
");
out.println("
");
out.println("");
// out.println("
Username
First name
Last name
Last Login
");
try {
while (result.next()) {
userName = result.getString("Username");
fName = result.getString("Fname");
lName = result.getString("Lname");
lastDate = result.getDate("LastLogin");
lastTime = result.getTime("LastLogin");
Last = result.getString("LastLogin");
if (Last.equals("0000-00-00 00:00:00")) {
out.println("
");
sessionbase.printBot(out, SessionId, 3);
out.close();
} else if (task.equals("clearLog")) {
query = "delete from TransLog";
sessionbase.doQuery(query);
sessionbase.printTop(out, 23);
out.println("
");
out.println("The Transaction Log has been cleared. ");
sessionbase.printBot(out, SessionId, 3);
out.close();
} else if (task.equals("viewLog")) {
// This section outputs the contents of the TransLog table,
// which records all transactions that take place.
java.util.Date transDate = new java.util.Date();
java.util.Date transTime = new java.util.Date();
Locale locale = new Locale("en","US");
// Set the format for time and date
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG, locale);
DateFormat timeFormat = DateFormat.getTimeInstance(DateFormat.MEDIUM, locale);
String tempOwn = (String)null;
String tradeFrom = (String)null;
String tradeTo = (String)null;
int numShares = 0;
double Value = 0;
sessionbase.printTop(out, 23);
out.println("
");
sessionbase.printBot(out, SessionId, 3);
out.close();
} else if (task.equals("loffers")) {
// List pending offers with modify/freeze/delete option
displayAllTrans(request, response, SessionId);
out.close();
} else if(task.equals("confirmBuy")) {
// Initialize variables
String nums = request.getParameter("numShares");
String buyVal = request.getParameter("askPrice");
Symbol = request.getParameter("Symbol");
Timestamp now = null;
Double buyTemp = Double.valueOf(buyVal);
double buyAt = buyTemp.doubleValue();
Integer NumS = Integer.valueOf(nums);
int offerShares = NumS.intValue();
Offers currentOffer = new Offers(Symbol, "B", buyAt, offerShares, now, sessionbase.getAccountId(SessionId));
sessionbase.doTrans(currentOffer, SessionId, false, response);
response.sendRedirect(ServletURL + ".Admin?task=buysell");
} else if(task.equals("confirmSell")) {
// Initialize variables
String nums = request.getParameter("numShares");
String buyVal = request.getParameter("askPrice");
Symbol = request.getParameter("Symbol");
Timestamp now = null;
Double buyTemp = Double.valueOf(buyVal);
double buyAt = buyTemp.doubleValue();
Integer NumS = Integer.valueOf(nums);
int offerShares = NumS.intValue();
Offers currentOffer = new Offers(Symbol, "S", buyAt, offerShares, now, sessionbase.getAccountId(SessionId));
sessionbase.doTrans(currentOffer, SessionId, false, response);
response.sendRedirect(ServletURL + ".Admin?task=buysell");
} else if (task.equals("viewHold")) {
sessionbase.printTop(out, 23);
out.println(" ");
sessionbase.showHoldings(out, 1, SessionId);
sessionbase.printBot(out, SessionId, 3);
} else if (task.equals("buysell")) {
String NumS = (String)null;
String Price = (String)null;
Symbol = request.getParameter("Symbol");
Trans = request.getParameter("Trans");
NumS = request.getParameter("NumShares");
Price = request.getParameter("Price");
// Print top template for Buy and Sell
sessionbase.printTop(out, 23);
out.println(" ");
// Print link to view all current offers before Buy and Sell form
out.println("View pending offers on all stock.
");
// Output form for user to make selections on
out.println("");
// Print user holdings
sessionbase.showHoldings(out, 1, SessionId);
sessionbase.printBot(out, SessionId, 3);
out.close();
} else if (task.equals("lfrozen")) {
// List frozen offers with modify/unfreeze/delete option
displayAllFrozen(request, response, SessionId);
out.close();
} else if (task.equals("VOffers")) {
// List offers of a particular stock
displayTrans(request, response, SessionId);
out.close();
} else if (task.equals("quote")) {
// Show a stock quote
String Type = request.getParameter("Type");
displayQuote(request, response, SessionId, Type);
out.close();
} else if (task.equals("modf")) {
Symbol = request.getParameter("Symbol");
Owner = request.getParameter("Owner");
Trans = request.getParameter("Trans");
query = "select * from Frozen where Trans = '" + Trans + "' and AccountId = " + Owner + " and Symbol = '" + Symbol + "'";
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
Shares = result.getInt("NumShares");
Cost = result.getDouble("tradeAt");
}
} catch (Exception e) {
e.printStackTrace();
}
query = "select Username from Account where AccountId = " + Owner;
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
userName = result.getString("Username");
}
} catch (Exception e) {
e.printStackTrace();
}
sessionbase.printTop(out, 23);
out.println(" ");
out.println("Note: ");
out.println("This will modify a frozen offer. Please ");
out.println("be sure of the changes you make. These changes will effect");
out.println("the portfolio of the user owning this transaction.
");
out.println("");
sessionbase.printBot(out, SessionId, 3);
out.close();
} else if (task.equals("confirmFreeze")) {
Symbol = request.getParameter("Symbol");
Trans = request.getParameter("Trans");
Owner = request.getParameter("Owner");
// Get the userName
query = "select Username from Account where AccountId = " + Owner;
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
userName = result.getString("Username");
}
} catch (Exception e) {
e.printStackTrace();
}
// Lock required tables
sessionbase.doUpdate("lock tables " + userName + " write, qt" + Symbol.toLowerCase() + " write, Account write, Frozen write", SessionId);
// Get value and shares
query = "select tradeAt, NumShares from qt" + Symbol.toLowerCase() + " where AccountId = " + Owner + " and Trans = '" + Trans + "'";
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
Cost = result.getDouble("tradeAt");
Shares = result.getInt("NumShares");
}
} catch (Exception e) {
e.printStackTrace();
}
// Delete from the queue
query = "delete from qt" + Symbol.toLowerCase() + " where AccountId = " + Owner + " and Trans = '" + Trans + "'";
sessionbase.doUpdate(query, SessionId);
// Switch to Frozen in User Holdings
query = "update " + userName + " set Trans = 'F" + Trans + "' where Symbol = '" + Symbol + "' and Trans = '" + Trans + "'";
sessionbase.doUpdate(query, SessionId);
// Add to Frozen table
query = "insert into Frozen (AccountId, Symbol, Trans, NumShares, tradeAt) values (" + Owner + ", '" + Symbol + "', '" + Trans + "', " + Shares + ", " + Cost + ")";
sessionbase.doUpdate(query, SessionId);
// Send an email to the user about the change.
String Email = (String)null;
String from = (String)null;
String Subject = (String)null;
String Text = (String)null;
if (Trans.equals("B")) {
Trans = "Purchasing";
} else {
Trans = "Selling";
}
query = "select Email from Account where AccountId = " + Owner;
result = sessionbase.doQuery(query, SessionId);
try {
while(result.next()) {
Email = result.getString("Email");
}
} catch(Exception e) {
e.printStackTrace();
}
from = SystemEmail;
Subject = SystemShortName + " - Offer Frozen Notice";
Text = "This note is to inform you that one of your offers has been\nfrozen by a " + SystemName + " administrator\nfor suspicion of market manipulation practices.\n\nTransaction: " + Trans + " " + intFormat.format(Shares) + " shares of " + Symbol.toUpperCase() + " at " + curFormat.format(Cost) + ".\n\n";
sessionbase.sendEmail(Subject, Email, from, Text, SessionId);
sessionbase.doUpdate("unlock tables", SessionId);
response.sendRedirect(ServletURL + ".Admin?task=loffers");
out.close();
} else if (task.equals("unfreeze")) {
Symbol = request.getParameter("Symbol");
Trans = request.getParameter("Trans");
Owner = request.getParameter("Owner");
// Get the userName
query = "select Username from Account where AccountId = " + Owner;
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
userName = result.getString("Username");
}
} catch (Exception e) {
e.printStackTrace();
}
// Get value and shares
query = "select NumShares, tradeAt from Frozen where AccountId = " + Owner + " and Symbol = '" + Symbol + "' and Trans = '" + Trans + "'";
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
Cost = result.getDouble("tradeAt");
Shares = result.getInt("NumShares");
}
} catch (Exception e) {
e.printStackTrace();
}
// Okay, put the transaction through doTrans. First delete it from the
// users frozen holdings
query = "delete from " + userName + " where Symbol = '" + Symbol + "' and Trans = 'F" + Trans + "'";
sessionbase.doUpdate(query);
// Delete from Frozen table
query = "delete from Frozen where AccountId = " + Owner + " and Trans = '" + Trans + "' and Symbol = '" + Symbol + "'";
sessionbase.doUpdate(query);
Timestamp now = null;
Offers currentOffer = new Offers(Symbol, Trans, Cost, Shares, now, Owner);
sessionbase.doTrans(currentOffer, SessionId, true, response);
// Send an email to the user about the change.
String from = (String)null;
String Subject = (String)null;
String Text = (String)null;
if (Trans.equals("B")) {
Trans = "Purchasing";
} else {
Trans = "Selling";
}
Subject = SystemShortName + " - Offer UnFrozen Notice";
Text = "This note is to inform you that one of your frozen offers has been\nreleased by a " + SystemName + " administrator.\n\nTransaction: " + Trans + " " + intFormat.format(Shares) + " shares of " + Symbol.toUpperCase() + " at " + curFormat.format(Cost) + ".\n\n";
sessionbase.sendEmail(Subject, Text, Owner, SessionId);
response.sendRedirect(ServletURL + ".Admin?task=lfrozen");
out.close();
} else if (task.equals("deletef")) {
Symbol = request.getParameter("Symbol");
Trans = request.getParameter("Trans");
Owner = request.getParameter("Owner");
// Get the username
query = "select Username from Account where AccountId = " + Owner;
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
userName = result.getString("Username");
}
} catch (Exception e) {
e.printStackTrace();
}
// Get transaction info
query = "select tradeAt, NumShares from Frozen where AccountId = " + Owner + " and Trans = '" + Trans + "' and Symbol = '" + Symbol + "'";
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
Cost = result.getDouble("tradeAt");
Shares = result.getInt("NumShares");
}
} catch (Exception e) {
e.printStackTrace();
}
String Transe = (String)null;
if (Trans.equals("B")) {
Transe = "Purchase";
} else {
Transe = "Sell";
}
sessionbase.printTop(out, 23);
out.println(" ");
out.println("Are you sure you want to delete the following frozen transaction?
");
out.println("");
sessionbase.printBot(out, SessionId, 3);
out.close();
} else if (task.equals("confirmDelF")) {
Symbol = request.getParameter("Symbol");
Trans = request.getParameter("Trans");
Owner = request.getParameter("Owner");
// Get the userName
query = "select Username from Account where AccountId = " + Owner;
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
userName = result.getString("Username");
}
} catch (Exception e) {
e.printStackTrace();
}
// Lock required tables
sessionbase.doUpdate("lock tables " + userName + " write, Frozen write, Portfolio write, Account write", SessionId);
// Get value and shares
query = "select tradeAt, NumShares from Frozen where AccountId = " + Owner + " and Trans = '" + Trans + "'";
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
Cost = result.getDouble("tradeAt");
Shares = result.getInt("NumShares");
}
} catch (Exception e) {
e.printStackTrace();
}
// Delete from the queue
query = "delete from Frozen where AccountId = " + Owner + " and Trans = '" + Trans + "' and Symbol = '" + Symbol + "'";
sessionbase.doUpdate(query, SessionId);
// If it's a Buy offer, delete offer from users Holdings
// and add balance back into Portfolio
// If it's a Sell offer, place shares back into Holdings as
// fixed and delete the offer.
double Balance = 0.00;
if (Trans.equals("B")) {
// Retrieve users balance
query = "select Balance from Portfolio where AccountId = " + Owner;
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
Balance = result.getDouble("Balance");
}
} catch (Exception e) {
e.printStackTrace();
}
// Update balance
Balance += (Shares * Cost);
query = "update Portfolio set Balance = " + Balance + " where AccountId = " + Owner;
sessionbase.doUpdate(query, SessionId);
} else {
// Check to see if user already has Fixed shares of this
// type and just update that. Otherwise, insert the new
// amount.
int oShares = 0;
query = "select NumShares from " + userName + " where Symbol = '" + Symbol + "' and Trans = 'C'";
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
oShares = result.getInt("NumShares");
}
} catch (Exception e) {
e.printStackTrace();
}
if (oShares > 0) {
oShares += Shares;
query = "update " + userName + " set NumShares = " + oShares + " where Symbol = '" + Symbol + "' and Trans = 'C'";
sessionbase.doUpdate(query, SessionId);
} else {
query = "insert into " + userName + " (Symbol, NumShares, tradeAt, Trans, Dtime) values ('" + Symbol + "', " + Shares + ", " + Cost + ", 'C', NOW())";
sessionbase.doUpdate(query, SessionId);
}
}
// Delete offer
query = "delete from " + userName + " where Symbol = '" + Symbol + "' and Trans = 'F" + Trans + "'";
sessionbase.doUpdate(query, SessionId);
// Send an email to the user about the change.
String Email = (String)null;
String from = (String)null;
String Subject = (String)null;
String Text = (String)null;
if (Trans.equals("B")) {
Trans = "Purchasing";
} else {
Trans = "Selling";
}
query = "select Email from Account where AccountId = " + Owner;
result = sessionbase.doQuery(query, SessionId);
try {
while(result.next()) {
Email = result.getString("Email");
}
} catch(Exception e) {
e.printStackTrace();
}
// Get current balance
query = "select Balance from Portfolio where AccountId = " + Owner;
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
Balance = result.getDouble("Balance");
}
} catch (Exception e) {
e.printStackTrace();
}
from = SystemEmail;
Subject = SystemShortName + " - Frozen Offer Deleted Notice";
Text = "This note is to inform you that one of your frozen offers has been\ndeleted by a " + SystemName + " administrator.\n\nTransaction: " + Trans + " " + intFormat.format(Shares) + " shares of " + Symbol.toUpperCase() + " at " + curFormat.format(Cost) + ".\n\nBalance: " + curFormat.format(Balance) + "\n";
sessionbase.sendEmail(Subject, Email, from, Text, SessionId);
sessionbase.doUpdate("unlock tables", SessionId);
response.sendRedirect(ServletURL + ".Admin?task=loffers");
out.close();
} else if (task.equals("confirmDel")) {
Symbol = request.getParameter("Symbol");
Trans = request.getParameter("Trans");
Owner = request.getParameter("Owner");
// Get the userName
query = "select Username from Account where AccountId = " + Owner;
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
userName = result.getString("Username");
}
} catch (Exception e) {
e.printStackTrace();
}
// Lock required tables
sessionbase.doUpdate("lock tables " + userName + " write, qt" + Symbol.toLowerCase() + " write, Portfolio write, Account write", SessionId);
// Get value and shares
query = "select tradeAt, NumShares from qt" + Symbol.toLowerCase() + " where AccountId = " + Owner + " and Trans = '" + Trans + "'";
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
Cost = result.getDouble("tradeAt");
Shares = result.getInt("NumShares");
}
} catch (Exception e) {
e.printStackTrace();
}
// Delete from the queue
query = "delete from qt" + Symbol.toLowerCase() + " where AccountId = " + Owner + " and Trans = '" + Trans + "'";
sessionbase.doUpdate(query, SessionId);
// If it's a Buy offer, delete offer from users Holdings
// and add balance back into Portfolio
// If it's a Sell offer, place shares back into Holdings as
// fixed and delete the offer.
double Balance = 0.00;
if (Trans.equals("B")) {
// Retrieve users balance
query = "select Balance from Portfolio where AccountId = " + Owner;
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
Balance = result.getDouble("Balance");
}
} catch (Exception e) {
e.printStackTrace();
}
// Update balance
Balance += (Shares * Cost);
query = "update Portfolio set Balance = " + Balance + " where AccountId = " + Owner;
sessionbase.doUpdate(query, SessionId);
} else {
// Check to see if user already has Fixed shares of this
// type and just update that. Otherwise, insert the new
// amount.
int oShares = 0;
query = "select NumShares from " + userName + " where Symbol = '" + Symbol + "' and Trans = 'C'";
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
oShares = result.getInt("NumShares");
}
} catch (Exception e) {
e.printStackTrace();
}
if (oShares > 0) {
oShares += Shares;
query = "update " + userName + " set NumShares = " + oShares + " where Symbol = '" + Symbol + "' and Trans = 'C'";
sessionbase.doUpdate(query, SessionId);
} else {
query = "insert into " + userName + " (Symbol, NumShares, tradeAt, Trans, Dtime) values ('" + Symbol + "', " + Shares + ", " + Cost + ", 'C', NOW())";
sessionbase.doUpdate(query, SessionId);
}
}
// Delete offer
query = "delete from " + userName + " where Symbol = '" + Symbol + "' and Trans = '" + Trans + "'";
sessionbase.doUpdate(query, SessionId);
// Send an email to the user about the change.
String Email = (String)null;
String from = (String)null;
String Subject = (String)null;
String Text = (String)null;
if (Trans.equals("B")) {
Trans = "Purchasing";
} else {
Trans = "Selling";
}
query = "select Email from Account where AccountId = " + Owner;
result = sessionbase.doQuery(query, SessionId);
try {
while(result.next()) {
Email = result.getString("Email");
}
} catch(Exception e) {
e.printStackTrace();
}
// Get current balance
query = "select Balance from Portfolio where AccountId = " + Owner;
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
Balance = result.getDouble("Balance");
}
} catch (Exception e) {
e.printStackTrace();
}
from = SystemEmail;
Subject = SystemShortName + " - Offer Deleted Notice";
Text = "This note is to inform you that one of your offers has been\ndeleted by a " + SystemName + " administrator.\n\nTransaction: " + Trans + " " + intFormat.format(Shares) + " shares of " + Symbol.toUpperCase() + " at " + curFormat.format(Cost) + ".\n\nBalance: " + curFormat.format(Balance) + "\n";
sessionbase.sendEmail(Subject, Email, from, Text, SessionId);
sessionbase.doUpdate("unlock tables", SessionId);
response.sendRedirect(ServletURL + ".Admin?task=loffers");
out.close();
} else {
// Invalid task
sessionbase.printTop(out, 21);
out.println("Error:");
out.println("
Invalid Task Specified
");
sessionbase.printBot(out, SessionId, 3);
out.close();
}
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String AccountId = (String)null;
String task = request.getParameter("task");
String query = (String)null;
ResultSet result = null;
// Set up decimal formatting for digits. This will force at least 0.00.
DecimalFormat curFormat = new DecimalFormat("$##,###,###,###,###,##0.00");
DecimalFormat numFormat = new DecimalFormat("################0.00");
DecimalFormat intFormat = new DecimalFormat("################0");
// Initialize the output stream
response.setContentType("text/html");
PrintWriter out = response.getWriter();
HttpSession session = request.getSession();
String SessionId = session.getId();
// Retrieve AccountId
AccountId = sessionbase.getAccountId(SessionId);
if ( AccountId == null || !AccountId.equals("1") ) {
response.sendRedirect(ServletURL + ".nonMember?task=LI&Type=1");
} else {
if (task.equals("mod")) {
String Symbol = request.getParameter("Symbol");
String Trans = request.getParameter("Trans");
String askP = request.getParameter("Cost");
String numShares = request.getParameter("Shares");
String Owner = request.getParameter("Owner");
String userName = request.getParameter("Name");
int error = 0;
int lenCheck = 0;
char Checkit;
String numCheck = "";
Integer convInt;
Double contP;
double Cost = 0.00;
int Shares = 0;
int oShares = 0;
double oCost = 0.00;
double Balance = 0.00;
askP = askP.trim();
lenCheck = askP.length();
for (int i = 0 ; i < lenCheck ; i++) {
Checkit = askP.charAt(i);
if (Checkit != '$' && Checkit != ',') {
numCheck += Checkit;
}
}
askP = numCheck;
try {
convInt = new Integer(numShares);
Shares = convInt.intValue();
contP = new Double(askP);
Cost = contP.doubleValue();
} catch(NumberFormatException e) {
sessionbase.printTop(out, 23);
out.println(" ");
out.println("Error:");
out.println("
Invalid characters entered.");
out.println("Please go back and correct the problem.
");
sessionbase.printBot(out, SessionId, 3);
out.close();
error++;
}
if (error == 0) {
// First lock the tables needed
sessionbase.doUpdate("lock tables " + userName + " write, qt" + Symbol.toLowerCase() + " write, Portfolio write, Account write", SessionId);
// Check to make sure the transaction still exists. Don't
// want to try and modify what someone has already acted on
query = "select AccountId from qt" + Symbol.toLowerCase() + " where Trans = '" + Trans + "' and AccountId = " + Owner;
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
Owner = result.getString("AccountId");
}
} catch (Exception e) {
e.printStackTrace();
}
if (Owner == null) {
sessionbase.doUpdate("unlock tables", SessionId);
sessionbase.printTop(out, 23);
out.println(" ");
out.println("
Error
");
out.println("
The transaction you are attempting to modify");
out.println("is no longer in the queue. A possible reason for this is someone");
out.println("has already responded to the transaction.
");
sessionbase.printBot(out, SessionId, 3);
out.close();
} else {
// Modify the transaction. For a Buy transaction, the Queue,
// users Holdings and users Balance need to be modified.
// For a sell only the Queue and Holdings need be modified.
if (Trans.equals("B")) {
// Modify Buy offer
// Load the old transaction
query = "select * from qt" + Symbol.toLowerCase() + " where AccountId = " + Owner + " and Trans = 'B'";
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
oShares = result.getInt("NumShares");
oCost = result.getDouble("tradeAt");
}
} catch (Exception e) {
e.printStackTrace();
}
// Extract users balance
query = "select Balance from Portfolio where AccountId = " + Owner;
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
Balance = result.getDouble("Balance");
}
} catch (Exception e) {
e.printStackTrace();
}
// Add cost of original transaction back in
Balance = Balance + (oCost*oShares);
// Take out cost of modified transaction
Balance = Balance - (Cost * Shares);
// Make sure the user doesn't have a negative balance
if (Balance < 0) {
Balance = 0;
}
// Update Balance
query = "update Portfolio set Balance = " + numFormat.format(Balance) + " where AccountId = " + Owner;
sessionbase.doUpdate(query, SessionId);
query = "update qt" + Symbol.toLowerCase() + " set NumShares = " + Shares + ", tradeAt = " + Cost + " where AccountId = " + Owner + " and Trans = 'B'";
sessionbase.doUpdate(query, SessionId);
query = "update " + userName + " set NumShares = " + Shares + ", tradeAt = " + Cost + " where Symbol = '" + Symbol + "' and Trans = 'B'";
sessionbase.doUpdate(query, SessionId);
} else if (Trans.equals("S")) {
// Modify Sell offer
// Load the old transaction
query = "select * from qt" + Symbol.toLowerCase() + " where AccountId = " + Owner + " and Trans = 'S'";
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
oShares = result.getInt("NumShares");
oCost = result.getDouble("tradeAt");
}
} catch (Exception e) {
e.printStackTrace();
}
// Update the Queue table
query = "update qt" + Symbol.toLowerCase() + " set NumShares = " + Shares + ", tradeAt = " + Cost + " where AccountId = " + Owner + " and Trans = 'S'";
sessionbase.doUpdate(query, SessionId);
// Update the User table
query = "update " + userName + " set NumShares = " + Shares + ", tradeAt = " + Cost + " where Symbol = '" + Symbol + "' and Trans = 'S'";
sessionbase.doUpdate(query, SessionId);
// Check to see if any shares need to be added back in
if (Shares < oShares) {
int newShares = oShares - Shares;
// First see if there are any Completed shares
query = "select NumShares from " + userName + " where Symbol = '" + Symbol + "' and Trans = 'C'";
result = sessionbase.doQuery(query, SessionId);
int cShares = 0;
try {
while (result.next()) {
cShares = result.getInt("NumShares");
}
} catch (Exception e) {
e.printStackTrace();
}
if (cShares > 0) {
newShares += cShares;
query = "update " + userName + " set NumShares = " + newShares + " where Symbol = '" + Symbol + "' and Trans = 'C'";
sessionbase.doUpdate(query, SessionId);
} else {
query = "insert into " + userName + " (Symbol, NumShares, tradeAt, Trans, Dtime) values ('" + Symbol + "', " + newShares + ", " + Cost + ", 'C', NOW())";
sessionbase.doUpdate(query, SessionId);
}
}
// Check to see if any shares need to be removed
if (Shares > oShares) {
int newShares = Shares - oShares;
// First see if there are any Completed shares
query = "select NumShares from " + userName + " where Symbol = '" + Symbol + "' and Trans = 'C'";
result = sessionbase.doQuery(query, SessionId);
int cShares = 0;
try {
while (result.next()) {
cShares = result.getInt("NumShares");
}
} catch (Exception e) {
e.printStackTrace();
}
if (cShares > 0) {
if (cShares <= newShares) {
// User would be left with 0, just
// remove from their holdings
query = "delete from " + userName + " where Symbol = '" + Symbol + "' and Trans = 'C'";
sessionbase.doUpdate(query, SessionId);
} else {
int tShares = cShares - newShares;
query = "update " + userName + " set NumShares = " + tShares + " where Symbol = '" + Symbol + "' and Trans = 'C'";
sessionbase.doUpdate(query, SessionId);
}
}
}
}
// Send an email to the user about the change.
String Email = (String)null;
String from = (String)null;
String Subject = (String)null;
String Text = (String)null;
if (Trans.equals("B")) {
Trans = "Purchasing";
} else {
Trans = "Selling";
}
query = "select Email from Account where AccountId = " + Owner;
result = sessionbase.doQuery(query, SessionId);
try {
while(result.next()) {
Email = result.getString("Email");
}
} catch(Exception e) {
e.printStackTrace();
}
// Get current balance
query = "select Balance from Portfolio where AccountId = " + Owner;
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
Balance = result.getDouble("Balance");
}
} catch (Exception e) {
e.printStackTrace();
}
from = SystemEmail;
Subject = SystemShortName + " - Offer Modified Notice";
Text = "This note is to inform you that one of your offers has been\nmodified by a " + SystemName + " administrator.\n\nOriginal Transaction: " + Trans + " " + intFormat.format(oShares) + " shares of " + Symbol.toUpperCase() + " at " + curFormat.format(oCost) + ".\n\nNew Transaction: " + Trans + " " + intFormat.format(Shares) + " of " + Symbol.toUpperCase() + " at " + curFormat.format(Cost) + ".\n\nBalance: " + curFormat.format(Balance) + "\n";
sessionbase.sendEmail(Subject, Email, from, Text, SessionId);
sessionbase.doUpdate("unlock tables", SessionId);
response.sendRedirect(ServletURL + ".Admin?task=loffers");
}
}
} else if (task.equals("buy")) {
// Initialize variables
String Symbol = request.getParameter("symbol");
String nums = request.getParameter("numShares");
String askP = request.getParameter("askPrice");
Integer convInt;
int numShares = 0;
// Cost per share container
double shareCost = 0.00;
// Current balance container
double Balance = 0.00;
// Total cost of purchase container
double tCost = 0.00;
// End balance container
double endBalance = 0.00;
// Available shares container
int availShares = 0;
// Remaining shares available
int endShares = 0;
int error = 0;
Double contP;
double askPrice = 0.00;
String numCheck = "";
char Checkit;
int lenCheck = 0;
String Locked = "";
// Everything is type String, convert everything to its proper type.
askP = askP.trim();
lenCheck = askP.length();
// Filter out $ and , from the Cost
for (int i = 0 ; i < lenCheck ; i++) {
Checkit = askP.charAt(i);
if (Checkit != '$' && Checkit != ',') {
numCheck += Checkit;
}
}
askP = numCheck;
try {
convInt = new Integer(nums);
numShares = convInt.intValue();
contP = new Double(askP);
askPrice = contP.doubleValue();
} catch(NumberFormatException e) {
// User must have entered something else wrong
sessionbase.printTop(out, 23);
out.println(" ");
out.println("
Error:
");
out.println("
Invalid characters entered.");
out.println("Please go back and correct the problem.
");
sessionbase.printBot(out, SessionId, 3);
out.close();
error++;
}
// Make sure user isn't trying to sell 0 shares or < $0.01
if (numShares < 1 || askPrice < 0.01) {
sessionbase.printTop(out, 23);
out.println(" ");
out.println("
Error:
");
out.println("
0 and negative values not allowed for Shares or Amount.");
out.println("Go back and enter a different number.
");
sessionbase.printBot(out, SessionId, 3);
out.close();
error++;
}
// Make sure they entered a valid company and that it isn't locked
query = "Select Last, Locked from Companies where Symbol = '" + Symbol + "'";
result = sessionbase.doQuery(query, SessionId);
try {
while(result.next()) {
shareCost = result.getDouble("Last");
Locked = result.getString("Locked");
}
} catch(Exception e) {
e.printStackTrace();
}
if(shareCost == 0.00) {
sessionbase.printTop(out, 23);
out.println(" ");
out.println("
Error:
");
out.println("
You have entered an invalid symbol.");
out.println("Please go back and enter a different one.
");
out.println("The stock you are trying to buy is currently frozen, no buy or sell transactions can take place until this is lifted.");
sessionbase.printBot(out, SessionId, 3);
out.close();
error++;
}
// Get user balance and make sure they have enough money
query = "select Balance from Portfolio where AccountId = " + AccountId;
result = sessionbase.doQuery(query, SessionId);
try {
while(result.next()) {
Balance = result.getDouble("Balance");
}
} catch(Exception e) {
e.printStackTrace();
}
tCost = 20 + (askPrice * numShares);
endBalance = Balance - tCost;
if(endBalance < 0) {
sessionbase.printTop(out, 23);
out.println(" ");
out.println("
Error:
");
out.println("
You cannot afford the number of stocks requested. Keep in mind there is a $20 commission on each transaction.");
out.println("Please go back and enter a different one.
");
sessionbase.printBot(out, SessionId, 3);
out.close();
error++;
}
// Check to see if user already has an offer out for this symbol
query = "select Symbol from " + sessionbase.getUsername(SessionId) + " where Symbol = '" + Symbol + "' and Trans = 'B'";
result = sessionbase.doQuery(query, SessionId);
String Sym = (String)null;
try {
while(result.next()) {
Sym = result.getString("Symbol");
}
} catch(Exception e) {
e.printStackTrace();
}
if (Sym != null) {
sessionbase.printTop(out, 23);
out.println(" ");
out.println("
Existing Trade
");
out.println("
Only one \"buy\" or \"sell\" offer can be made at a time.");
out.println("You can have a \"buy\" and \"sell\" offer at the same time but only one of");
out.println("each. To change your transaction, go back");
out.println("and cancel the existing trade.
");
sessionbase.printBot(out, SessionId, 3);
out.close();
error++;
}
if (error == 0) {
// Everything must be ok. Prompt user to ensure they
// really want to do the trade.
sessionbase.printTop(out, 23);
out.println(" ");
out.println("
Invalid characters entered.");
out.println("Please go back and correct the problem.
");
sessionbase.printBot(out, SessionId, 3);
out.close();
error++;
}
// Make sure they entered a valid company
query = "select Last, Locked from Companies where Symbol = '" + Symbol + "'";
result = sessionbase.doQuery(query, SessionId);
try {
while(result.next()) {
shareCost = result.getDouble("Last");
Locked = result.getString("Locked");
}
} catch(Exception e) {
e.printStackTrace();
}
if (shareCost == 0.00) {
sessionbase.printTop(out, 23);
out.println(" ");
out.println("
Error:
");
out.println("
The Symbol you have entered doesn't exist.");
out.println("Go back and enter a valid symbol.
");
sessionbase.printBot(out, SessionId, 3);
error++;
out.close();
}
// Make sure user isn't trying to sell 0 shares or < $0.01
if (numShares < 1 || askPrice < 0.01) {
sessionbase.printTop(out, 23);
out.println(" ");
out.println("
Error:
");
out.println("
0 and negative values not allowed for Shares or Amount.");
out.println("Go back and enter a different number.
");
sessionbase.printBot(out, SessionId, 3);
out.close();
error++;
}
// Make sure user has the number of shares they try to sell
query = "Select NumShares from " + sessionbase.getUsername(SessionId) + " where Symbol = '" + Symbol + "' and Trans = 'C'";
result = sessionbase.doQuery(query, SessionId);
try {
while(result.next()) {
userShares = result.getInt("NumShares");
}
} catch(Exception e) {
e.printStackTrace();
}
if ((userShares - numShares) < 0 ) {
sessionbase.printTop(out, 23);
out.println(" ");
out.println("
Error:
");
out.println("
You don't have that many shares to sell.");
out.println("Go back and enter a different number.
");
out.println("The stock you are trying to buy is currently frozen, no buy or sell transactions can take place until this is lifted.");
sessionbase.printBot(out, SessionId, 3);
out.close();
error++;
}
// Check to see if user already has an offer out for this symbol
query = "select Symbol from " + sessionbase.getUsername(SessionId) + " where Symbol = '" + Symbol + "' and Trans = 'S'";
result = sessionbase.doQuery(query, SessionId);
String Sym = (String)null;
try {
while(result.next()) {
Sym = result.getString("Symbol");
}
} catch(Exception e) {
e.printStackTrace();
}
if (Sym != null) {
sessionbase.printTop(out, 23);
out.println(" ");
out.println("
Existing Trade
");
out.println("
Only one \"buy\" or \"sell\" offer can be made at a time.");
out.println("You can have a \"buy\" and \"sell\" offer at the same time but only one of");
out.println("each. To change your transaction, go back");
out.println("and cancel the existing trade.
");
sessionbase.printBot(out, SessionId, 3);
out.close();
error++;
}
if (error == 0) {
// Everything seems ok, confirm the sale
// Get user's balance
query = "select Balance from Portfolio where AccountId = " + AccountId;
result = sessionbase.doQuery(query, SessionId);
try {
while(result.next()) {
Balance = result.getDouble("Balance");
}
} catch(Exception e) {
e.printStackTrace();
}
Profit = (numShares * askPrice) - 20;
endBalance = Balance + Profit;
sessionbase.printTop(out, 23);
out.println(" ");
out.println("
Confirm
");
out.println("
Please confirm your sale bid: ");
out.println("" + numShares + " shares of " + Symbol.toUpperCase() + " at " + curFormat.format(askPrice) + " per share. ");
out.println("Sale Value: " + curFormat.format(Profit + 20) + " ");
out.println("Commission Cost: $20 ");
out.println("Ending Balance: " + curFormat.format(endBalance) + " ");
out.println("");
sessionbase.printBot(out, SessionId, 3);
out.close();
}
} else if (task.equals("createCompany")) {
String Name = request.getParameter("Name");
String Symbol = request.getParameter("Symbol");
String curBal = "0";
curBal += request.getParameter("cash");
String nums = "0";
nums += request.getParameter("numShares");
int numShares = 0;
String curVal = "0";
curVal += request.getParameter("curVal");
String Email = request.getParameter("Email");
String Pass = request.getParameter("Pass");
String Pass1 = request.getParameter("Pass1");
String Rand = "";
Rand += request.getParameter("Random");
Integer convInt;
Double contP;
double CmpBal = 0;
double Value = 0.00;
char Checkit;
String numCheck = "";
String Message = "";
int error = 0;
double startBal = 0;
query = "select compBalance from Parameters";
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
startBal = result.getDouble("compBalance");
}
} catch (Exception e) {
e.printStackTrace();
}
// Everything is type String, convert everything to its proper type.
curBal = curBal.trim();
int lenCheck = curBal.length();
// Filter out $ and , from the Value
for (int i = 0 ; i < lenCheck ; i++) {
Checkit = curBal.charAt(i);
if (Checkit != '$' && Checkit != ',') {
numCheck += Checkit;
}
}
try {
contP = new Double(curBal);
CmpBal = contP.doubleValue();
} catch(NumberFormatException e) {
// User must have entered something wrong
Message += "Invalid characters entered in Balance. ";
error++;
}
numCheck = "";
curVal = curVal.trim();
lenCheck = curVal.length();
// Filter out $ and , from the Value
for (int i = 0 ; i < lenCheck ; i++) {
Checkit = curVal.charAt(i);
if (Checkit != '$' && Checkit != ',') {
numCheck += Checkit;
}
}
try {
convInt = new Integer(nums);
numShares = convInt.intValue();
contP = new Double(curVal);
Value = contP.doubleValue();
} catch(NumberFormatException e) {
// User must have entered something wrong
Message += "Invalid characters entered in Shares or Value. ";
error++;
}
if (Name.length() < 1) {
Message += "Name field cannot be left empty. ";
error++;
}
if (Symbol.length() < 2) {
Message += "Symbol must be at least 2 characters long. ";
error++;
}
if (Symbol.equals("SYS")) {
Message += "SYS cannot be used since it is a symbol reserved for the Exchange. ";
error++;
}
if (numShares <= 0) {
Message += "Number of Available Shares must be a positive value. ";
error++;
}
if (Value < 0.01) {
Message += "Current Value must be at least $0.01. ";
error++;
}
if ((Pass.length() < 1 && Pass1.length() > 0) || (Pass.length() > 0 && Pass1.length() < 1)) {
Message += "You must fill in both password fields or choose Random Password. ";
error++;
}
if (Rand.equals("rand") && (Pass.length() > 0 || Pass1.length() > 0)) {
Message += "You cannot enter a password and choose Random Password, please pick one or the other. ";
error++;
}
if (!Rand.equals("rand") && Pass.length() < 1 && Pass1.length() < 1) {
Message += "You must either enter a password or choose Random Password. ";
error++;
}
if ((Pass.length() > 4 && Pass1.length() > 4 ) && !Pass.equals(Pass1)) {
Message += "The passwords you have entered do not match. ";
error++;
}
// Check for duplicate Symbol
query = "select Name from Companies where Symbol = '" + Symbol + "'";
result = sessionbase.doQuery(query, SessionId);
String checkSym = "";
try {
while (result.next()) {
checkSym = result.getString("Name");
}
} catch (Exception e) {
e.printStackTrace();
}
if (checkSym.length() > 0) {
Message += "Another Company (" + checkSym + ") is using the symbol you specified. Please enter another. ";
error++;
}
// Make sure the Email address entered doesn't belong to another
// company.
query = "select Email from Account where Email = '" + Email + "' and Type = 'C'";
result = sessionbase.doQuery(query, SessionId);
String checkComp = "";
try {
while (result.next()) {
checkComp = result.getString("Email");
}
} catch (Exception e) {
e.printStackTrace();
}
if (checkComp.length() > 1) {
Message += "The email address you entered is already being used for another company. ";
error++;
}
if (error > 0) {
sessionbase.printTop(out, 24);
out.println(" ");
out.println("Company Creation ");
out.println("Error: ");
out.println(Message);
out.println("
");
out.println("");
sessionbase.printBot(out, SessionId, 3);
out.close();
} else {
// Everything is okay, create the company
// Lock required tables
sessionbase.doUpdate("lock tables Companies write, Account write, Portfolio write", SessionId);
// Add company to the Companies table
query = "insert into Companies (Name, Symbol, Tshares, Open, Last, Volume, 52WkHi, 52WkLo, DayHi, DayLo, Previous, IPO) values ('" + Name + "', '" + Symbol + "', " + numShares + ", " + Value + ", " + Value + ", 0, " + Value + ", " + Value + ", " + Value + ", " + Value + ", " + Value + ", " + Value + ")";
sessionbase.doUpdate(query, SessionId);
// Create Company Table
query = "create table qt" + Symbol.toLowerCase() + " (AccountId int(12) not null, Trans enum('B','S') not null, NumShares int(10) not null, tradeAt double(5, 2) not null, Dtime timestamp not null, primary key(AccountId, Trans))";
sessionbase.doUpdate(query, SessionId);
// Create Company History table
query = "create table ht" + Symbol.toLowerCase() + " (Date date not null, Time time not null, Value double(5, 2) not null)";
sessionbase.doUpdate(query, SessionId);
// Create Company History table
query = "create table eht" + Symbol.toLowerCase() + " (Date date not null, Value double(5, 2) not null)";
sessionbase.doUpdate(query, SessionId);
// Create Company user
// Was a random password requested?
if (Rand.equals("rand")) {
Pass = sessionbase.genPass("", false);
}
// Get the next AccountId
query = "select max(AccountId) from Account";
result = sessionbase.doQuery(query, SessionId);
int AcId = 0;
try {
while (result.next()) {
AcId = result.getInt("max(AccountId)");
}
} catch (Exception e) {
e.printStackTrace();
}
AcId += 1;
// Insert into Account
query = "insert into Account (AccountId, Username, Password, Fname, Lname, Dcreate, Email, Type) values (" + AcId + ", '" + Symbol + "', Password('" + Pass + "'), 'Company', 'User', NOW(), '" + Email + "', 'C')";
sessionbase.doUpdate(query, SessionId);
// Create a Company User table
query = "create table " + Symbol + " (Symbol varchar(4) not null, NumShares int(10) not null, boughtAt double(5, 2) not null, tradeAt double(5, 2) not null, Trans enum('B','S','C','FB','FS') not null, Dtime datetime not null, primary key(Symbol, Trans))";
sessionbase.doUpdate(query, SessionId);
// Add Company User Portfolio
query = "insert into Portfolio (AccountId, Balance) values (" + AcId + ", " + CmpBal + ")";
sessionbase.doUpdate(query, SessionId);
// Unlock tables
sessionbase.doUpdate("unlock tables", SessionId);
// Add initial entries
query = "insert into eht" + Symbol.toLowerCase() + " (Date, Value) values (NOW(), " + Value + ")";
sessionbase.doUpdate(query, SessionId);
// Add Company Holdings
query = "insert into " + Symbol + " (Symbol, NumShares, tradeAt, boughtAt, Trans, Dtime) values ('" + Symbol + "', " + numShares + ", " + Value + ", " + Value + ", 'C', NOW())";
sessionbase.doUpdate(query, SessionId);
// Email Company User
String from = SystemEmail;
String Subject = "" + SystemShortName + " - Company Created!";
String Text = "An account has been created for the company " + Name + "(" + Symbol + " on the " + SystemName + ".\n\nPoint your browser to: " + SystemURL + " to log in.\n\nUsername: " + Symbol + "\nPassword: " + Pass + "\n";
sessionbase.sendEmail(Subject, Email, from, Text, SessionId);
sessionbase.printTop(out, 24);
out.println(" ");
out.println("Company Creation ");
out.println("The Company '" + Name + "'(" + Symbol + ") has been created. ");
out.println("Add a Company");
out.println("
");
out.println("");
sessionbase.printBot(out, SessionId, 3);
out.close();
}
} else if (task.equals("addNews")) {
String Author = "";
Author += request.getParameter("Name");
String Title = "";
Title += request.getParameter("Title");
String Type = "";
Type += request.getParameter("Type");
String Story = "";
Story += request.getParameter("Story");
String chkAuthor = "";
String cmpAuthor = "";
String Message = "";
int error = 0;
// Check for authentic user
query = "select Username from Account where Username = '" + Author + "'";
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
chkAuthor += result.getString("Username");
}
} catch (Exception e) {
e.printStackTrace();
}
cmpAuthor = Author.toLowerCase();
if (!cmpAuthor.equals(chkAuthor.toLowerCase())) {
Message += "The user you entered is not a registered user. ";
error++;
}
if (Title.length() < 1) {
Message += "You must provide a Title for the Article. ";
error++;
}
if (Story.length() < 1) {
Message += "You must provide text for the Article. ";
error++;
}
if (error > 0) {
sessionbase.printTop(out, 26);
out.println(" ");
out.println("Error: ");
out.println(Message);
out.println("");
sessionbase.printBot(out, SessionId, 3);
out.close();
} else {
addNews(Title, Author, Type, Story, SessionId);
response.sendRedirect(ServletURL + ".Admin?task=news");
}
} else if (task.equals("clearSys")) {
// This will clear all History tables. Make sure the user
// really wants to do this.
sessionbase.printTop(out, 21);
out.println(" ");
out.println("Confirm ");
out.println("Notice: This will clear the history tables of every company on the Exchange. Are you sure you want to do this?
");
out.println("");
sessionbase.printBot(out, SessionId, 3);
out.close();
} else if (task.equals("confirmParamsChange")) {
// Retrieve all the values
// Initialize each variable first so that we can do comparisons.
String Name = "";
String shortName = "";
String URL = "";
String SrvURL = "";
String Path = "";
String Email = "";
String Pass = "";
String verPass = "";
String changePer = "";
String changeCap = "";
String compBal = "";
String usrBal = "";
String Comms = "";
Name += request.getParameter("exchangeName");
shortName += request.getParameter("shortName");
URL += request.getParameter("httpurl");
SrvURL += request.getParameter("servleturl");
Path += request.getParameter("path");
Email += request.getParameter("Email");
Pass += request.getParameter("password");
verPass += request.getParameter("verpass");
changePer += request.getParameter("changepercent");
changeCap += request.getParameter("changecap");
Comms += request.getParameter("commission");
compBal += request.getParameter("companybalance");
usrBal += request.getParameter("userbalance");
int error = 0;
Double Conv;
double ChangePer = 0;
double ChangeCap = 0;
double Commission = 0.00;
double userBalance = 0.00;
double companyBalance = 0.00;
String numCheck = "";
char Checkit;
int lenCheck = 0;
String Message = "";
// Convert changePer and changeCap to double
try {
numCheck = "";
lenCheck = changePer.length();
for (int i = 0 ; i < lenCheck ; i++) {
Checkit = changePer.charAt(i);
if (Checkit != '$' && Checkit != ',') {
numCheck += Checkit;
}
}
Conv = new Double(numCheck);
ChangePer = Conv.doubleValue();
} catch(NumberFormatException e) {
Message += "Percentage Change Value contains invalid characters. ";
error++;
}
try {
numCheck = "";
lenCheck = Comms.length();
for (int i = 0 ; i < lenCheck ; i++) {
Checkit = Comms.charAt(i);
if (Checkit != '$' && Checkit != ',') {
numCheck += Checkit;
}
}
Conv = new Double(numCheck);
Commission = Conv.doubleValue();
} catch(NumberFormatException e) {
Message += "Commission Rate contains invalid characters. ";
error++;
}
try {
numCheck = "";
lenCheck = compBal.length();
for (int i = 0 ; i < lenCheck ; i++) {
Checkit = compBal.charAt(i);
if (Checkit != '$' && Checkit != ',') {
numCheck += Checkit;
}
}
Conv = new Double(numCheck);
companyBalance = Conv.doubleValue();
} catch(NumberFormatException e) {
Message += "Company Default balance contains invalid characters. ";
error++;
}
try {
numCheck = "";
lenCheck = usrBal.length();
for (int i = 0 ; i < lenCheck ; i++) {
Checkit = usrBal.charAt(i);
if (Checkit != '$' && Checkit != ',') {
numCheck += Checkit;
}
}
Conv = new Double(numCheck);
userBalance = Conv.doubleValue();
} catch(NumberFormatException e) {
Message += "User Default balance contains invalid characters. ";
error++;
}
try {
numCheck = "";
lenCheck = changeCap.length();
for (int i = 0 ; i < lenCheck ; i++) {
Checkit = changeCap.charAt(i);
if (Checkit != '$' && Checkit != ',') {
numCheck += Checkit;
}
}
Conv = new Double(numCheck);
ChangeCap = Conv.doubleValue();
} catch(NumberFormatException e) {
Message += "Market Capitalization Change contains invalid characters. ";
error++;
}
// Check everything
if (ChangePer >= 1 || ChangePer < 0) {
Message += "Change Percentage has to be a positive decimal value or 0. ";
error++;
}
if (ChangeCap < 0) {
Message += "Market Capitalization Change cannot be a negative value. ";
error++;
}
if (Commission < 0) {
Message += "Commission Rate cannot be a negative value. ";
error++;
}
if (userBalance < 0) {
Message += "User Default balance cannot be a negative value. ";
error++;
}
if (companyBalance < 0) {
Message += "Company Default balance cannot be a negative value. ";
error++;
}
if (Name.length() < 1) {
Message += "An Exchange Name must be provided. ";
error++;
}
if (shortName.length() < 1) {
Message += "An Exchange Short Name must be provided. ";
error++;
}
if (URL.length() < 1) {
Message += "A System URL must be provided. ";
error++;
}
if (SrvURL.length() < 1) {
Message += "A Servlet Server URL must be provided. ";
error++;
}
if (Path.length() < 1) {
Message += "A System Path must be provided. ";
error++;
}
if (Email.length() < 1) {
Message += "An Exchange Email Address must be provided. ";
error++;
}
if ((Pass.length() > 0 && verPass.length() < 1) || (verPass.length() > 0 && Pass.length() < 1)) {
Message += "A Password was entered but not verified. ";
error++;
}
if ((Pass.length() > 0 && verPass.length() > 0) && (Pass.length() < 5)) {
Message += "Password must be at least 5 characters long. ";
error++;
}
if ((Pass.length() > 0 && verPass.length() > 0) && (!Pass.equals(verPass))) {
Message += "The passwords entered don't match. ";
error++;
}
// Check URL and SrvURL from http://
// Convert a temporary to lowercase (user may enter httP, HTTP, or
// some such combination so just make sure it's lowercase)
String tmp = URL.toLowerCase();
if (!tmp.startsWith("http://")) {
URL = "http://" + URL;
}
tmp = SrvURL.toLowerCase();
if (!tmp.startsWith("http://")) {
SrvURL = "http://" + SrvURL;
}
// Check for a valid, unique email address
// Check for invalid email address. Must match *@*.* in some way.
// Simply checks character by character for @ and . then checks
// positioning.
if (Email.length() > 0) {
String lookFor = "@";
int found = 0;
for (int i = 0; i< Email.length(); i++) {
if(Email.charAt(i) == ' ') {
Message += "An invalid email address was entered. ";
error++;
}
if (Email.charAt(i) == lookFor.charAt(0)) {
found++;
if (i == 0) {
Message += "An invalid email address was entered. ";
error++;
} else if (i == (Email.length()-1)) {
Message += "An invalid email address was entered. ";
error++;
} else {
int j = i+1;
lookFor = ".";
for (int k = 0; k < (Email.length()-j); k++) {
if (Email.charAt(k+j) == lookFor.charAt(0)) {
found++;
if (k == 0) {
Message += "An invalid email address was entered. ";
error++;
} else if ( k == (Email.length()-j-1)) {
Message += "An invalid email address was entered. ";
error++;
}
}
}
}
}
}
if (found < 2) {
Message += "An invalid email address was entered. ";
error++;
}
}
if (error > 0) {
sessionbase.printTop(out, 21);
out.println(" ");
out.println("Error: ");
out.println(Message + " ");
out.println("" + SystemShortName + " Parameters ");
out.println("Here you can change parameters affecting how the Exchange ");
out.println("works.
");
out.println("Warning: Several of these parameters are vital to the ");
out.println("Exchange working properly. Please be careful when making changes. ");
out.println("");
sessionbase.printBot(out, SessionId, 3);
out.close();
} else {
// Make changes to Parameters
query = "update Parameters set URL = '" + URL + "', ServletURL = '" + SrvURL + "', Path = '" + Path + "', Email = '" + Email + "', Name = '" + Name + "', ShortName = '" + shortName + "', ChangeCap = " + ChangeCap + ", ChangeValue = " + ChangePer + ", Commission = " + Commission + ", userBalance = " + userBalance + ", compBalance = " + companyBalance;
sessionbase.doUpdate(query, SessionId);
// Update Admin email
query = "update Account set Email = '" + Email + "' where AccountId = 1";
sessionbase.doUpdate(query, SessionId);
// Change Admin password, if entered
if (Pass.length() > 0) {
query = "update Account set Password = Password('" + Pass + "') where AccountId = 1";
sessionbase.doUpdate(query, SessionId);
}
// Have the system reload changes
sessionbase.loadParams();
sessionbase.printTop(out, 21);
out.println(" ");
out.println(SystemName + " parameters successfully updated.");
sessionbase.printBot(out, SessionId, 3);
out.close();
}
} else if (task.equals("modusrRedir")) {
String Name = request.getParameter("Name");
response.sendRedirect(ServletURL + ".Admin?task=modUser&User=" + Name);
} else if (task.equals("modcomRedir")) {
String Symbol = request.getParameter("companySym");
response.sendRedirect(ServletURL + ".Admin?task=modCompany&companySym=" + Symbol);
} else if (task.equals("createUser")) {
String userName = "";
userName += request.getParameter("User");
String Bal = "";
Bal += request.getParameter("Balance");
String Fname = "";
Fname += request.getParameter("Fname");
String Lname = "";
Lname += request.getParameter("Lname");
String Mi = "";
Mi += request.getParameter("Mi");
String Email = "";
Email += request.getParameter("Email");
String Pass = "";
Pass += request.getParameter("Pass");
String Pass1 = "";
Pass1 += request.getParameter("Pass1");
String Randm = "";
Randm += request.getParameter("Random");
String Message = "";
int error = 0;
Double convD = null;
double Balance = 0;
int lenCheck = 0;
char Checkit;
String numCheck = "";
// Convert Balance to Double
try {
numCheck = "";
lenCheck = Bal.length();
for (int i = 0 ; i < lenCheck ; i++) {
Checkit = Bal.charAt(i);
if (Checkit != '$' && Checkit != ',') {
numCheck += Checkit;
}
}
convD = new Double(numCheck);
Balance = convD.doubleValue();
} catch(NumberFormatException e) {
Message += "Balance contains invalid characters. ";
error++;
}
double startBal = 0;
query = "select userBalance from Parameters";
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
startBal = result.getDouble("userBalance");
}
} catch (Exception e) {
e.printStackTrace();
}
if (Balance < sessionbase.getCommission(SessionId)) {
Message += "Balance should be at least $" + sessionbase.getCommission(SessionId) + " to cover the commission charge. ";
error++;
}
if (userName.length() < 1) {
Message += "Username field cannot be left empty. ";
error++;
}
if (Fname.length() < 1) {
Message += "First Name field cannot be left empty. ";
error++;
}
if (Mi.length() < 1 || Mi.equals("null")) {
Mi = "";
}
if (Lname.length() < 1) {
Message += "Last name field cannot be left empty. ";
error++;
}
if (Email.length() < 1) {
Message += "Email field cannot be left empty. ";
error++;
}
if (Randm.equals("rand") && (Pass.length() > 0 || Pass1.length() > 0)) {
Message += "You entered a password and selected Random Password. Please choose one or the other. ";
error++;
}
if (Pass.length() > 0 && Pass1.length() > 0) {
if (!Pass.equals(Pass1)) {
Message += "The passwords you entered do not match. ";
error++;
}
}
if (Pass.length() > 0 && Pass.length() < 5 && Pass.equals(Pass1) && !Randm.equals("rand")) {
Message += "Passwords must be at least 5 characters long. ";
error++;
}
if (!Randm.equals("rand") && (!(Pass.length() > 0) || !(Pass1.length() > 0))) {
Message += "You must either enter a password or select Random Password. ";
error++;
}
// Check for duplicate Username
query = "select Username from Account where Username = '" + userName + "'";
result = sessionbase.doQuery(query, SessionId);
String checkUser = "";
try {
while (result.next()) {
checkUser = result.getString("Username");
}
} catch (Exception e) {
e.printStackTrace();
}
if (checkUser.length() > 0) {
Message += "Another user is registered with the selected username. ";
error++;
}
// Check for duplicate email address
query = "select Username from Account where Email = '" + Email + "' and Type != 'C'";
result = sessionbase.doQuery(query, SessionId);
String checkEmail = "";
try {
while (result.next()) {
checkEmail = result.getString("Username");
}
} catch (Exception e) {
e.printStackTrace();
}
if (checkEmail.length() > 0) {
Message += "Another user is registered with the selected email address. ";
error++;
}
if (error > 0) {
sessionbase.printTop(out, 22);
out.println(" ");
out.println("Error: ");
out.println(Message);
out.println("
");
out.println("");
sessionbase.printBot(out, SessionId, 3);
out.close();
} else {
// Everything is okay, create the account
// Lock required tables
sessionbase.doUpdate("lock tables Account write, Portfolio write, Parameters write", SessionId);
// Was a random password requested?
if (Randm.equals("rand")) {
Pass = sessionbase.genPass("", false);
}
// Add user to the Account table
// Get the next AccountId
// Check to see what the current max AccountId is.
int NewAccountId = 0;
query = "select max(AccountId) from Account";
// Pass the query over to the BullyDB object. Return type is ResultSet.
result = sessionbase.doQuery(query, SessionId);
try {
while(result.next()) {
NewAccountId = result.getInt("max(AccountId)");
}
} catch(Exception e) {
e.printStackTrace();
}
// Increment AccountId for the new user
NewAccountId++;
// Add entry
query = "insert into Account (AccountId, Username, Password, Fname, Mi, Lname, Email, Dcreate) values (" + NewAccountId + ", '" + userName + "', Password('" + Pass + "'), '" + Fname + "', '" + Mi + "', '" + Lname + "', '" + Email + "', NOW())";
sessionbase.doUpdate(query, SessionId);
// Create Holdings table
query = "create table " + userName + " (Symbol varchar(4) not null, NumShares int(10) not null, boughtAt double(5, 2) not null, tradeAt double(5, 2) not null, Trans enum('B','S','C','FB','FS') not null, Dtime datetime not null, primary key(Symbol, Trans))";
sessionbase.doUpdate(query, SessionId);
// Add user's Portfolio
query = "insert into Portfolio (AccountId, Balance) values (" + NewAccountId + ", " + Balance + ")";
sessionbase.doUpdate(query, SessionId);
// Unlock tables
sessionbase.doUpdate("unlock tables", SessionId);
// Email creation message to user
String from = SystemEmail;
String Subject = SystemShortName + " - Account Creation Notice";
String Text = "An account has been created for you on the " + SystemName + ".\n\nPoint your browser to: " + SystemURL + " to log in.\n\nUsername: " + userName + "\nPassword: " + Pass + "\n";
sessionbase.sendEmail(Subject, Email, from, Text, SessionId);
// In the event that the admin is wanting to add a large
// block of users, have it display another User Creation
// page.
sessionbase.printTop(out, 22);
out.println(" ");
out.println("User Successfully Added");
out.println("
");
out.println("");
sessionbase.printBot(out, SessionId, 3);
out.close();
}
} else if (task.equals("modcmp")) {
String Symbol = request.getParameter("Symbol");
String Name = request.getParameter("Name");
String nums = request.getParameter("numShares");
String lst = request.getParameter("Last");
String opn = request.getParameter("Open");
String dayh = request.getParameter("DayHi");
String dayl = request.getParameter("DayLo");
String fth = request.getParameter("52WkHi");
String ftl = request.getParameter("52WkLo");
String vol = request.getParameter("Volume");
String Email = request.getParameter("Email");
String Locked = request.getParameter("Locked");
String prev = request.getParameter("Previous");
String Pass = request.getParameter("Pass");
String Pass1 = request.getParameter("Pass1");
String Bal = request.getParameter("Balance");
String Rand = "";
Rand += request.getParameter("Random");
Double convD;
Integer convI;
int numShares = 0;
double Balance = 0.00;
double Last = 0.00;
double Open = 0.00;
double DayHi = 0.00;
double DayLo = 0.00;
double ftHi = 0.00;
double ftLo = 0.00;
double Previous = 0.00;
int Volume = 0;
int lenCheck = 0;
char Checkit;
String numCheck = "";
String Message = "";
int error = 0;
try {
numCheck = "";
lenCheck = Bal.length();
for (int i = 0 ; i < lenCheck ; i++) {
Checkit = Bal.charAt(i);
if (Checkit != '$' && Checkit != ',') {
numCheck += Checkit;
}
}
convD = new Double(numCheck);
Balance = convD.doubleValue();
} catch(NumberFormatException e) {
Message += "Balance contains invalid characters. ";
error++;
}
try {
numCheck = "";
lenCheck = lst.length();
for (int i = 0 ; i < lenCheck ; i++) {
Checkit = lst.charAt(i);
if (Checkit != '$' && Checkit != ',') {
numCheck += Checkit;
}
}
convD = new Double(numCheck);
Last = convD.doubleValue();
} catch(NumberFormatException e) {
Message += "Current Value contains invalid characters. ";
error++;
}
try {
numCheck = "";
lenCheck = prev.length();
for (int i = 0 ; i < lenCheck ; i++) {
Checkit = prev.charAt(i);
if (Checkit != '$' && Checkit != ',') {
numCheck += Checkit;
}
}
convD = new Double(numCheck);
Previous = convD.doubleValue();
} catch(NumberFormatException e) {
Message += "Previous Value contains invalid characters. ";
error++;
}
try {
numCheck = "";
lenCheck = nums.length();
for (int i = 0 ; i < lenCheck ; i++) {
Checkit = nums.charAt(i);
if (Checkit != '$' && Checkit != ',') {
numCheck += Checkit;
}
}
convI = new Integer(numCheck);
numShares = convI.intValue();
} catch(NumberFormatException e) {
Message += "Total Shares contains invalid characters. ";
error++;
}
try {
numCheck = "";
lenCheck = opn.length();
for (int i = 0 ; i < lenCheck ; i++) {
Checkit = opn.charAt(i);
if (Checkit != '$' && Checkit != ',') {
numCheck += Checkit;
}
}
convD = new Double(numCheck);
Open = convD.doubleValue();
} catch(NumberFormatException e) {
Message += "Opening Value contains invalid characters. ";
error++;
}
try {
numCheck = "";
lenCheck = dayh.length();
for (int i = 0 ; i < lenCheck ; i++) {
Checkit = dayh.charAt(i);
if (Checkit != '$' && Checkit != ',') {
numCheck += Checkit;
}
}
convD = new Double(numCheck);
DayHi = convD.doubleValue();
} catch(NumberFormatException e) {
Message += "Day High contains invalid characters. ";
error++;
}
try {
numCheck = "";
lenCheck = dayl.length();
for (int i = 0 ; i < lenCheck ; i++) {
Checkit = dayl.charAt(i);
if (Checkit != '$' && Checkit != ',') {
numCheck += Checkit;
}
}
convD = new Double(numCheck);
DayLo = convD.doubleValue();
} catch(NumberFormatException e) {
Message += "Day Low contains invalid characters. ";
error++;
}
try {
numCheck = "";
lenCheck = fth.length();
for (int i = 0 ; i < lenCheck ; i++) {
Checkit = fth.charAt(i);
if (Checkit != '$' && Checkit != ',') {
numCheck += Checkit;
}
}
convD = new Double(numCheck);
ftHi = convD.doubleValue();
} catch(NumberFormatException e) {
Message += "52 Week High contains invalid characters. ";
error++;
}
try {
numCheck = "";
lenCheck = ftl.length();
for (int i = 0 ; i < lenCheck ; i++) {
Checkit = ftl.charAt(i);
if (Checkit != '$' && Checkit != ',') {
numCheck += Checkit;
}
}
convD = new Double(numCheck);
ftLo = convD.doubleValue();
} catch(NumberFormatException e) {
Message += "52 Week Low contains invalid characters. ";
error++;
}
try {
numCheck = "";
lenCheck = vol.length();
for (int i = 0 ; i < lenCheck ; i++) {
Checkit = vol.charAt(i);
if (Checkit != '$' && Checkit != ',') {
numCheck += Checkit;
}
}
convI = new Integer(numCheck);
Volume = convI.intValue();
} catch(NumberFormatException e) {
Message += "Volume contains invalid characters. ";
error++;
}
if (Symbol.length() < 1) {
Message += "A Symbol must be provided. ";
error++;
}
if (Name.length() < 1) {
Message += "A name must be provided. ";
error++;
}
if ((Pass.length() > 0 && Pass1.length() < 1) || (Pass.length() < 1 && Pass1.length() > 0)) {
Message += "When changing password, you must enter both the Password and the Verify Password. ";
error++;
}
if ((Pass.length() > 0 && Pass1.length() > 0) && !(Pass.equals(Pass1))) {
Message += "The passwords you entered do not match! ";
error++;
}
if (Rand.equals("rand") && (Pass.length() > 0 || Pass1.length() > 0)) {
Message += "You cannot enter a password and choose Random Password, please pick one or the other. ";
error++;
}
if (Balance < 0) {
Message += "The Balance field cannot be a negative value. ";
error++;
}
if (Volume < 0) {
Message += "The Volume field cannot be a negative value. ";
error++;
}
if (numShares < 0) {
Message += "Total Shares cannot be a negative value. ";
error++;
}
if (Last < 0.01 || Open < 0.01 || DayHi < 0.01 || DayLo < 0.01 || ftHi < 0.01 || ftLo < 0.01 || Previous < 0.01) {
Message += "Monetary Values must be at least 0.01. ";
error++;
}
if (Email.length() < 1) {
Message += "A Company Email Address must be provided. ";
error++;
}
// Make sure a duplicate email address was not entered
String checkCmp = "";
query = "select Username from Account where Email = '" + Email + "' and Type = 'C'";
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
checkCmp = result.getString("Username");
}
} catch (Exception e) {
e.printStackTrace();
}
if (checkCmp.length() > 0 && !checkCmp.equals(Symbol)) {
Message += "The email address you entered is in use by another company. ";
error++;
}
if (error > 0) {
sessionbase.printTop(out, 24);
out.println(" ");
out.println("Modify a Company");
out.println("Error: ");
out.println(Message);
out.println("
");
out.println("");
out.println("
");
sessionbase.printBot(out, SessionId, 3);
out.close();
} else {
// Everything seems to be fine, update the company info.
// Lock required tables
sessionbase.doUpdate("lock tables Companies write, Account write, Portfolio write, " + Symbol + " write", SessionId);
// Was a random password requested?
if (Rand.equals("rand")) {
Pass = sessionbase.genPass("", false);
}
// Was the password set to be changed?
if (Pass.length() > 0) {
// Update the password in Account
query = "update Account set Password = Password('" + Pass + "') where Username = '" + Symbol + "'";
sessionbase.doUpdate(query, SessionId);
// Email password to the user
String from = SystemEmail;
String Subject = SystemShortName + " - Password Change Notification";
String Text = "Please note that your password on the " + SystemName + " has been changed.\n\nCompany: " + Symbol + "\nPassword: " + Pass + "\n";
sessionbase.sendEmail(Subject, Email, from, Text, SessionId);
}
// Get original numShares to see if we'll need to add shares to
// the Company user.
int origShares = 0;
query = "select Tshares from Companies where Symbol = '" + Symbol + "'";
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
origShares = result.getInt("Tshares");
}
} catch (Exception e) {
e.printStackTrace();
}
if (origShares < numShares) {
// Okay, number of shares was increased. Add the extra to
// the Company User
// Check and see if user already has Fixed shares of this
// company. If so, just add to that.
int userShares = 0;
query = "select NumShares from " + Symbol + " where Symbol = '" + Symbol + "' and Trans = 'C'";
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
userShares = result.getInt("NumShares");
}
} catch (Exception e) {
e.printStackTrace();
}
if (userShares > 0) {
// Okay, user has some. Just add to that.
userShares += (numShares - origShares);
query = "update " + Symbol + " set NumShares = " + userShares + " where Symbol = '" + Symbol + "' and Trans = 'C'";
sessionbase.doUpdate(query, SessionId);
} else {
// Nope, no shares. Add an entry.
query = "insert into " + Symbol + " (Symbol, NumShares, tradeAt, Trans, Dtime) values ('" + Symbol + "', " + (numShares - origShares) + ", " + Last + ", 'C', NOW())";
sessionbase.doUpdate(query, SessionId);
}
}
// Update Company information
query = "update Companies set Name = '" + Name + "', Tshares = " + numShares + ", Open = " + Open + ", Last = " + Last + ", Volume = " + Volume + ", 52WkHi = " + ftHi + ", 52WkLo = " + ftLo + ", DayHi = " + DayHi + ", DayLo = " + DayLo + ", Previous = " + Previous + ", Locked = '" + Locked + "' where Symbol = '" + Symbol + "'";
sessionbase.doUpdate(query, SessionId);
// Update Email address in Account
query = "update Account set Email = '" + Email + "' where Username = '" + Symbol + "'";
sessionbase.doUpdate(query, SessionId);
// Update Portfolio
// Get Company AccountId
String CmpAccountId = (String)null;
query = "select AccountId from Account where Username = '" + Symbol + "'";
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
CmpAccountId = result.getString("AccountId");
}
} catch (Exception e) {
e.printStackTrace();
}
query = "update Portfolio set Balance = " + Balance + " where AccountId = " + CmpAccountId;
sessionbase.doUpdate(query, SessionId);
// Unlock tables
sessionbase.doUpdate("unlock tables", SessionId);
sessionbase.printTop(out, 24);
out.println(" ");
out.println("Modify Company ");
out.println("Company information for " + Name + "(" + Symbol + ") successfully updated.");
sessionbase.printBot(out, SessionId, 3);
out.close();
}
} else if (task.equals("modusr")) {
String userName = "";
userName += request.getParameter("User");
String fName = "";
fName += request.getParameter("fName");
String lName = "";
lName += request.getParameter("lName");
String MI = "";
MI += request.getParameter("MI");
String Email = "";
Email += request.getParameter("Email");
String Pass = "";
Pass += request.getParameter("Pass");
String Pass1 = "";
Pass1 += request.getParameter("Pass1");
String Bal = "";
Bal += request.getParameter("Balance");
String Randm = "";
Randm += request.getParameter("Random");
String Message = "";
int error = 0;
String numCheck = "";
int lenCheck = 0;
Double convD;
double Balance = 0.00;
char Checkit;
try {
numCheck = "";
lenCheck = Bal.length();
for (int i = 0 ; i < lenCheck ; i++) {
Checkit = Bal.charAt(i);
if (Checkit != '$' && Checkit != ',') {
numCheck += Checkit;
}
}
convD = new Double(numCheck);
Balance = convD.doubleValue();
} catch(NumberFormatException e) {
Message += "Balance contains invalid characters. ";
error++;
}
if (Balance < 0) {
Message += "Balance field cannot be a negative value. ";
error++;
}
if (fName.length() < 1) {
Message += "First Name field cannot be left empty. ";
error++;
}
if (lName.length() < 1) {
Message += "Last name field cannot be left empty. ";
error++;
}
if (Email.length() < 1) {
Message += "Email field cannot be left empty. ";
error++;
}
if ((Pass.length() < 1 && Pass1.length() > 0) || (Pass1.length() < 1 && Pass.length() > 0)) {
Message += "Password field cannot be left empty without selecting Random Password. ";
error++;
}
if (Pass.length() > 0 && Pass.length() < 5 && Pass.equals(Pass1) && !Randm.equals("rand")) {
Message += "The password you have entered is too short. ";
error++;
}
if (Randm.equals("rand") && (Pass.length() > 0 || Pass1.length() > 0)) {
Message += "You entered a password and selected Random Password. Please choose one or the other. ";
error++;
}
if (Pass.length() > 0) {
if (!Pass.equals(Pass1)) {
Message += "The passwords you entered do not match. ";
error++;
}
}
// Check for duplicate email address
query = "select AccountId from Account where Email = '" + Email + "'";
result = sessionbase.doQuery(query, SessionId);
String checkEmail = "";
try {
while (result.next()) {
checkEmail = result.getString("AccountId");
}
} catch (Exception e) {
e.printStackTrace();
}
AccountId = "";
query = "select AccountId from Account where Username = '" + userName + "'";
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
AccountId = result.getString("AccountId");
}
} catch (Exception e) {
e.printStackTrace();
}
if (!AccountId.equals(checkEmail) && checkEmail.length() > 0) {
Message += "Another user is registered with the selected email address. ";
error++;
}
if (error > 0) {
sessionbase.printTop(out, 22);
out.println(" ");
out.println("Error: ");
out.println(Message);
out.println("
");
out.println("");
out.println("
");
sessionbase.printBot(out, SessionId, 3);
out.close();
} else {
// Everything is okay, modify the account
// Lock required tables
sessionbase.doUpdate("lock tables Account write, Portfolio write", SessionId);
// Was a random password requested?
if (Randm.equals("rand")) {
Pass = sessionbase.genPass("", false);
}
// Modify information
// Don't change the password if nothing is set for it
if (Pass.length() < 1) {
query = "update Account set Fname = '" + fName + "', Lname = '" + lName + "', Mi = '" + MI + "', Email = '" + Email + "' where Username = '" + userName + "'";
} else {
query = "update Account set Fname = '" + fName + "', Lname = '" + lName + "', Mi = '" + MI + "', Email = '" + Email + "', Password = Password('" + Pass + "') where Username = '" + userName + "'";
// Email password to the user
String from = SystemEmail;
String Subject = SystemShortName + " - Password Change Notification";
String Text = "Please note that your password on the " + SystemName + " has been changed.\n\nUsername: " + userName + "\nPassword: " + Pass + "\n";
sessionbase.sendEmail(Subject, Email, from, Text, SessionId);
}
sessionbase.doUpdate(query, SessionId);
query = "update Portfolio set Balance = " + numFormat.format(Balance) + " where AccountId = " + AccountId;
sessionbase.doUpdate(query, SessionId);
// Unlock tables
sessionbase.doUpdate("unlock tables", SessionId);
sessionbase.printTop(out, 22);
out.println(" ");
out.println("Information for " + userName + " successfully updated.");
sessionbase.printBot(out, SessionId, 3);
out.close();
}
} else if (task.equals("confirmUserDel")) {
String userName = request.getParameter("User");
// Make sure it's a valid user and not a Company User
String userCheck = "";
String Fname = "";
String Lname = "";
query = "select Username, Fname, Lname from Account where Username = '" + userName + "'";
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
userCheck = result.getString("Username");
Fname = result.getString("Fname");
Lname = result.getString("Lname");
}
} catch (Exception e) {
e.printStackTrace();
}
String uChk = userName.toLowerCase();
if (!uChk.equals(userCheck.toLowerCase())) {
sessionbase.printTop(out, 23);
out.println(" ");
out.println("You have specified an invalid user.");
out.println("Please go Back and re-enter the user.");
sessionbase.printBot(out, SessionId, 3);
out.close();
} else {
if (Fname.equals("Company") && Lname.equals("User")) {
sessionbase.printTop(out, 23);
out.println(" ");
out.println("You are trying to delete a Company User. These users cannot be deleted - they are automatically removed when a Company is deleted. ");
out.println("Please go Back and re-enter the user.");
sessionbase.printBot(out, SessionId, 3);
out.close();
} else if (sessionbase.checkRoot(userCheck)) {
sessionbase.printTop(out, 23);
out.println(" ");
out.println("You are trying to delete the Administrative user. This user cannot be deleted. ");
out.println("Please go Back and re-enter the user.");
sessionbase.printBot(out, SessionId, 3);
out.close();
} else {
sessionbase.printTop(out, 23);
out.println(" ");
out.println("Confirm Account Delete ");
out.println("Delete the Account of " + userCheck + "? ");
out.println("Note: This will permanently delete the user account. ");
out.println("");
sessionbase.printBot(out, SessionId, 3);
out.close();
}
}
} else if (task.equals("confirmCompDel")) {
String Symbol = request.getParameter("Symbol");
// Make sure it's a valid company
String cmpCheck = "";
String cName = (String)null;
query = "select Symbol, Name from Companies where Symbol = '" + Symbol + "'";
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
cmpCheck = result.getString("Symbol");
cName = result.getString("Name");
}
} catch (Exception e) {
e.printStackTrace();
}
String uChk = Symbol.toLowerCase();
if (!uChk.equals(cmpCheck.toLowerCase())) {
sessionbase.printTop(out, 24);
out.println(" ");
out.println("You have specified an invalid company.");
out.println("Please go Back and re-enter the symbol.");
sessionbase.printBot(out, SessionId, 3);
out.close();
} else {
sessionbase.printTop(out, 24);
out.println(" ");
out.println("Confirm Company Deletion ");
out.println("Delete the Company " + cName + "(" + cmpCheck + ")? ");
out.println("Warning: This will permanently delete the Company. This step cannot be undone and will alter the holdings of everyone owning shares in this company.
");
out.println("Please make sure this is the right company to delete. ");
out.println("");
sessionbase.printBot(out, SessionId, 3);
out.close();
}
} else if (task.equals("confirmCompanyUnlock")) {
String companySym = request.getParameter("companySym");
// Make sure it's a valid company and that it's locked
String compCheck = "";
String lockStat = "";
query = "select Name, Locked from Companies where Symbol = '" + companySym + "'";
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
compCheck = result.getString("Name");
lockStat = result.getString("Locked");
}
} catch (Exception e) {
e.printStackTrace();
}
if (!(compCheck.length() > 0)) {
sessionbase.printTop(out, 24);
out.println(" ");
out.println("Company Unlock");
out.println("You have specified an invalid company.");
out.println("Please go Back and re-enter the company.");
sessionbase.printBot(out, SessionId, 3);
out.close();
} else if (lockStat.equals("N")) {
sessionbase.printTop(out, 24);
out.println(" ");
out.println("Company Unlock");
out.println("The company you are trying to unlock has not been locked.");
out.println("Please go Back and re-enter the company.");
sessionbase.printBot(out, SessionId, 3);
out.close();
} else {
sessionbase.printTop(out, 24);
out.println(" ");
out.println("Confirm Company Unlock");
out.println("Unlock the Company " + compCheck + "(" + companySym + ")? ");
out.println("");
sessionbase.printBot(out, SessionId, 3);
out.close();
} } else if (task.equals("confirmCompanyLock")) {
String companySym = request.getParameter("companySym");
// Make sure it's a valid company and that it's unlocked
String compCheck = "";
String lockStat = "";
query = "select Name, Locked from Companies where Symbol = '" + companySym + "'";
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
compCheck = result.getString("Name");
lockStat = result.getString("Locked");
}
} catch (Exception e) {
e.printStackTrace();
}
if (!(compCheck.length() > 0)) {
sessionbase.printTop(out, 24);
out.println(" ");
out.println("You have specified an invalid company.");
out.println("Please go Back and re-enter the company.");
sessionbase.printBot(out, SessionId, 3);
out.close();
} else if (lockStat.equals("Y")) {
sessionbase.printTop(out, 24);
out.println(" ");
out.println("The company you are trying to lock is already locked.");
out.println("Please go Back and re-enter the company.");
sessionbase.printBot(out, SessionId, 3);
out.close();
} else {
sessionbase.printTop(out, 24);
out.println(" ");
out.println("Confirm Company Lock");
out.println("Lock the Company " + compCheck + "(" + companySym + ")? ");
out.println("");
sessionbase.printBot(out, SessionId, 3);
out.close();
}
} else if (task.equals("confirmLock")) {
String userName = request.getParameter("User");
String reasonLock = request.getParameter("reason");
// Make sure it's a valid user
String userCheck = "";
query = "select AccountId from Account where Username = '" + userName + "'";
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
userCheck = result.getString("AccountId");
}
} catch (Exception e) {
e.printStackTrace();
}
if (!(userCheck.length() > 0)) {
sessionbase.printTop(out, 23);
out.println(" ");
out.println("You have specified an invalid user.");
out.println("Please go Back and re-enter the user.");
sessionbase.printBot(out, SessionId, 3);
out.close();
} else if (reasonLock.equals("")) {
sessionbase.printTop(out, 23);
out.println(" ");
out.println("You must enter a reason for locking the account.");
out.println("Please go Back and enter one.");
sessionbase.printBot(out, SessionId, 3);
out.close();
} else {
sessionbase.printTop(out, 23);
out.println(" ");
out.println("Confirm Account Lock");
out.println("Lock the Account of " + userCheck + " with the reason: ");
out.println(reasonLock + " ");
int lenS = reasonLock.length();
String tempS = "";
// Filter out things in the string that might throw off the url.
// Use a little pattern just for the Exchange.
for (int i = 0 ; i < lenS ; i++) {
if (reasonLock.charAt(i) != ' ' && reasonLock.charAt(i) != '\"' && reasonLock.charAt(i) !='\'' && reasonLock.charAt(i) != '?' && reasonLock.charAt(i) != '&') {
tempS = tempS + reasonLock.charAt(i);
} else if (reasonLock.charAt(i) == ' ') {
tempS += "^!1!^";
} else if (reasonLock.charAt(i) == '\"') {
tempS += "^!2!^";
} else if (reasonLock.charAt(i) == '\'') {
tempS += "^!3!^";
} else if (reasonLock.charAt(i) == '?') {
tempS += "^!4!^";
} else if (reasonLock.charAt(i) == '&') {
tempS += "^!5!^";
}
}
reasonLock = tempS;
out.println("");
sessionbase.printBot(out, SessionId, 3);
out.close();
}
} else if (task.equals("confirmUnlock")) {
String userName = request.getParameter("User");
// Make sure it's a valid user
String userCheck = "";
query = "select AccountId from Account where Username = '" + userName + "'";
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
userCheck = result.getString("AccountId");
}
} catch (Exception e) {
e.printStackTrace();
}
if (!(AccountId.length() > 0)) {
sessionbase.printTop(out, 23);
out.println(" ");
out.println("You have specified an invalid user.");
out.println("Please go Back and re-enter the user.");
sessionbase.printBot(out, SessionId, 3);
out.close();
} else {
sessionbase.printTop(out, 23);
out.println(" ");
out.println("Confirm Unlock ");
out.println("Are you sure you want to unlock the account of " + userCheck + "? ");
out.println("");
sessionbase.printBot(out, SessionId, 3);
out.close();
}
} else if (task.equals("modf")) {
String Symbol = request.getParameter("Symbol");
String Trans = request.getParameter("Trans");
String askP = request.getParameter("Cost");
String numShares = request.getParameter("Shares");
String Owner = request.getParameter("Owner");
String userName = request.getParameter("Name");
int error = 0;
int lenCheck = 0;
char Checkit;
String numCheck = "";
Integer convInt;
Double contP;
double Cost = 0.00;
int Shares = 0;
int oShares = 0;
double oCost = 0.00;
double Balance = 0.00;
askP = askP.trim();
lenCheck = askP.length();
for (int i = 0 ; i < lenCheck ; i++) {
Checkit = askP.charAt(i);
if (Checkit != '$' && Checkit != ',') {
numCheck += Checkit;
}
}
askP = numCheck;
try {
convInt = new Integer(numShares);
Shares = convInt.intValue();
contP = new Double(askP);
Cost = contP.doubleValue();
} catch(NumberFormatException e) {
sessionbase.printTop(out, 23);
out.println(" ");
out.println("Error:");
out.println("
Invalid characters entered.");
out.println("Please go back and correct the problem.
");
sessionbase.printBot(out, SessionId, 3);
out.close();
error++;
}
if (error == 0) {
// First lock the tables needed
sessionbase.doUpdate("lock tables " + userName + " write, Frozen write, Portfolio write, Account write", SessionId);
// Modify the transaction. For a Buy transaction, Frozen queue,
// users Holdings and users Balance need to be modified.
// For a sell only the Frozen queue and Holdings need be modified.
if (Trans.equals("B")) {
// Modify Buy offer
// Load the old transaction
query = "select * from Frozen where AccountId = " + Owner + " and Trans = 'B' and Symbol = '" + Symbol + "'";
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
oShares = result.getInt("NumShares");
oCost = result.getDouble("tradeAt");
}
} catch (Exception e) {
e.printStackTrace();
}
// Extract users balance
query = "select Balance from Portfolio where AccountId = " + Owner;
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
Balance = result.getDouble("Balance");
}
} catch (Exception e) {
e.printStackTrace();
}
// Add cost of original transaction back in
Balance = Balance + (oCost*oShares);
// Take out cost of modified transaction
Balance = Balance - (Cost * Shares);
// Make sure the user doesn't have a negative balance
if (Balance < 0) {
Balance = 0;
}
// Update Balance
query = "update Portfolio set Balance = " + numFormat.format(Balance) + " where AccountId = " + Owner;
sessionbase.doUpdate(query, SessionId);
query = "update Frozen set NumShares = " + Shares + ", tradeAt = " + Cost + " where AccountId = " + Owner + " and Trans = 'B' and Symbol = '" + Symbol + "'";
sessionbase.doUpdate(query, SessionId);
query = "update " + userName + " set NumShares = " + Shares + ", tradeAt = " + Cost + " where Symbol = '" + Symbol + "' and Trans = 'FB'";
sessionbase.doUpdate(query, SessionId);
} else if (Trans.equals("S")) {
// Modify Sell offer
// Load the old transaction
query = "select NumShares, tradeAt from Frozen where AccountId = " + Owner + " and Trans = 'S' and Symbol = '" + Symbol + "'";
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
oShares = result.getInt("NumShares");
oCost = result.getDouble("tradeAt");
}
} catch (Exception e) {
e.printStackTrace();
}
// Update the Queue table
query = "update Frozen set NumShares = " + Shares + ", tradeAt = " + Cost + " where AccountId = " + Owner + " and Trans = 'S' and Symbol = '" + Symbol + "'";
sessionbase.doUpdate(query, SessionId);
// Update the User table
query = "update " + userName + " set NumShares = " + Shares + ", tradeAt = " + Cost + " where Symbol = '" + Symbol + "' and Trans = 'FS'";
sessionbase.doUpdate(query, SessionId);
// Check to see if any shares need to be added back in
if (Shares < oShares) {
int newShares = oShares - Shares;
// First see if there are any Completed shares
query = "select NumShares from " + userName + " where Symbol = '" + Symbol + "' and Trans = 'C'";
result = sessionbase.doQuery(query, SessionId);
int cShares = 0;
try {
while (result.next()) {
cShares = result.getInt("NumShares");
}
} catch (Exception e) {
e.printStackTrace();
}
if (cShares > 0) {
newShares += cShares;
query = "update " + userName + " set NumShares = " + newShares + " where Symbol = '" + Symbol + "' and Trans = 'C'";
sessionbase.doUpdate(query, SessionId);
} else {
query = "insert into " + userName + " (Symbol, NumShares, tradeAt, Trans, Dtime) values ('" + Symbol + "', " + newShares + ", " + Cost + ", 'C', NOW())";
sessionbase.doUpdate(query, SessionId);
}
}
// Check to see if any shares need to be removed
if (Shares > oShares) {
int newShares = Shares - oShares;
// First see if there are any Completed shares
query = "select NumShares from " + userName + " where Symbol = '" + Symbol + "' and Trans = 'C'";
result = sessionbase.doQuery(query, SessionId);
int cShares = 0;
try {
while (result.next()) {
cShares = result.getInt("NumShares");
}
} catch (Exception e) {
e.printStackTrace();
}
if (cShares > 0) {
if (cShares <= newShares) {
// User would be left with 0, just
// remove from their holdings
query = "delete from " + userName + " where Symbol = '" + Symbol + "' and Trans = 'C'";
sessionbase.doUpdate(query, SessionId);
} else {
int tShares = cShares - newShares;
query = "update " + userName + " set NumShares = " + tShares + " where Symbol = '" + Symbol + "' and Trans = 'C'";
sessionbase.doUpdate(query, SessionId);
}
}
}
}
// Send an email to the user about the change.
String Email = (String)null;
String from = (String)null;
String Subject = (String)null;
String Text = (String)null;
if (Trans.equals("B")) {
Trans = "Purchasing";
} else {
Trans = "Selling";
}
query = "select Email from Account where AccountId = " + Owner;
result = sessionbase.doQuery(query, SessionId);
try {
while(result.next()) {
Email = result.getString("Email");
}
} catch(Exception e) {
e.printStackTrace();
}
// Get current balance
query = "select Balance from Portfolio where AccountId = " + Owner;
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
Balance = result.getDouble("Balance");
}
} catch (Exception e) {
e.printStackTrace();
}
from = SystemEmail;
Subject = SystemShortName + " - Frozen Offer Modified Notice";
Text = "This note is to inform you that one of your Frozen offers has been\nmodified by a " + SystemName + " administrator.\n\nOriginal Transaction: " + Trans + " " + intFormat.format(oShares) + " shares of " + Symbol.toUpperCase() + " at " + curFormat.format(oCost) + ".\n\nNew Transaction: " + Trans + " " + intFormat.format(Shares) + " of " + Symbol.toUpperCase() + " at " + curFormat.format(Cost) + ".\n\nBalance: " + curFormat.format(Balance) + "\n";
sessionbase.sendEmail(Subject, Email, from, Text, SessionId);
sessionbase.doUpdate("unlock tables", SessionId);
response.sendRedirect(ServletURL + ".Admin?task=lfrozen");
}
} else {
// Invalid task
sessionbase.printTop(out, 21);
out.println("Error: ");
out.println("
Invalid Task Specified
");
sessionbase.printBot(out, SessionId, 3);
out.close();
}
}
}
public void displayQuote(HttpServletRequest request, HttpServletResponse response, String SessionId, String Type)
throws IOException, ServletException {
String Symbol = request.getParameter("Symbol");
String query = (String)null;
String Name = (String)null;
int Tshares = 0;
double Ipo = 0.00;
double Open = 0.00;
double Last = 0.00;
int Volume = 0;
double Change = 0.00;
double WkHi = 0.00;
double WkLo = 0.00;
double DayHi = 0.00;
double DayLo = 0.00;
double Previous = 0.00;
String fLast = (String)null;
String fChange = (String)null;
String fLow = (String)null;
String fHigh = (String)null;
ResultSet result = null;
double Capitalization = 0.00;
ResultSet result2 = null;
double userValue = 0;
int userShares = 0;
String userName = (String)null;
boolean member = true;
String Locked = (String)null;
String Email = (String)null;
// Set up decimal formatting for digits. This will force at least 0.00.
DecimalFormat curFormat = new DecimalFormat("$##,###,###,###,###,##0.00");
DecimalFormat numFormat = new DecimalFormat("################0.00");
DecimalFormat intFormat = new DecimalFormat("################0");
// Initialize output stream
response.setContentType("text/html");
PrintWriter out=response.getWriter();
// Extract Company info from the database. A null ResultSet means the company
// must not be in the database.
query = "select * from Companies where Symbol = '" + Symbol + "'";
result = sessionbase.doQuery(query);
try {
while(result.next()) {
Name = result.getString("Name");
Tshares = result.getInt("Tshares");
Open = result.getDouble("Open");
Last = result.getDouble("Last");
Volume = result.getInt("Volume");
WkHi = result.getDouble("52WkHi");
WkLo = result.getDouble("52WkLo");
DayHi = result.getDouble("DayHi");
DayLo = result.getDouble("DayLo");
Previous = result.getDouble("Previous");
Locked = result.getString("Locked");
}
} catch(Exception e) {
e.printStackTrace();
}
// Get Company Email address
query = "select Email from Account where Username = '" + Symbol + "'";
result = sessionbase.doQuery(query);
try {
while (result.next()) {
Email = result.getString("Email");
}
} catch (Exception e) {
e.printStackTrace();
}
Change = Last - Previous;
// Calculating an accurate total Market Cap is a little
// trickier.
query = "select Username from Account where Username != 'root' order by Username";
result2 = sessionbase.doQuery(query);
try {
while (result2.next()) {
userName = result2.getString("Username");
query = "select * from " + userName + " where Symbol = '" + Symbol + "' and Trans != 'B' and Trans != 'FB'";
result = sessionbase.doQuery(query);
while (result.next()) {
userValue = result.getDouble("boughtAt");
userShares = result.getInt("NumShares");
Capitalization += (userValue * userShares);
}
}
} catch (Exception e) {
e.printStackTrace();
}
if (Name == null) {
response.sendRedirect(ServletURL + ".Quotes?Task=invalid&Link=3");
out.close();
} else {
sessionbase.printTop(out, 25);
out.println(" ");
out.println(" ");
out.println(" Today with " + Name + " (" + Symbol + ") ");
out.println("
");
// Display the Company graph
sessionbase.displayGraph(out, Type, Symbol, 2);
sessionbase.printBot(out, SessionId, 3);
out.close();
}
}
public void displayTrans(HttpServletRequest request, HttpServletResponse response, String SessionId)
throws IOException, ServletException {
String Trans = (String)null;
String TransN = (String)null;
int numShares = 0;
double tradeAt = 0.00;
int Owner = 0;
String userName = (String)null;
String AccountId = (String)null;
String query = (String)null;
String Symbol = request.getParameter("Symbol");
String Name = (String)null;
ResultSet result = null;
ResultSet un = null;
// Set up decimal formatting for digits. This will force at least 0.00.
DecimalFormat curFormat = new DecimalFormat("$##,###,###,###,###,##0.00");
DecimalFormat numFormat = new DecimalFormat("################0.00");
DecimalFormat intFormat = new DecimalFormat("################0");
// Retrieve AccountId
AccountId = sessionbase.getAccountId(SessionId);
if (AccountId == null) {
response.sendRedirect(ServletURL + ".nonMember?task=LI&Type=1");
} else {
// Initialize output stream
response.setContentType("text/html");
PrintWriter out=response.getWriter();
// Get Company name
query = "select Name from Companies where Symbol = '" + Symbol + "'";
result = sessionbase.doQuery(query);
try {
while(result.next()) {
Name = result.getString("Name");
}
} catch (Exception e) {
e.printStackTrace();
}
// Make sure the company has offers available
query = "select Trans from qt" + Symbol.toLowerCase();
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
Trans = result.getString("Trans");
}
} catch (Exception e) {
e.printStackTrace();
}
if (Trans == null) {
sessionbase.printTop(out, 23);
out.println(" ");
out.println("
No Data
");
out.println("
No offers are currently pending for this stock.
");
sessionbase.printBot(out, SessionId, 3);
out.close();
} else {
// Extract Company info from the database. A null ResultSet means
// the company must not be in the database.
query = "select * from qt" + Symbol.toLowerCase();
result = sessionbase.doQuery(query, SessionId);
sessionbase.printTop(out, 23);
out.println(" ");
out.println("
" + Name + " (" + Symbol.toUpperCase() + ")
");
out.println("
");
out.println("");
out.println("
");
out.println("
");
out.println("");
out.println("
Offer Type
Shares
Cost
Owner
Task
");
try {
while(result.next()) {
Trans = result.getString("Trans");
numShares = result.getInt("NumShares");
tradeAt = result.getDouble("tradeAt");
Owner = result.getInt("AccountId");
query = "select Username from Account where AccountId = " + Owner;
un = sessionbase.doQuery(query, SessionId);
while (un.next()) {
userName = un.getString("Username");
}
if (Trans.equals("S")) {
TransN = "Sell";
} else if (Trans.equals("B")) {
TransN = "Buy";
} else {
TransN = "Unknown";
}
out.println("
");
sessionbase.printBot(out, SessionId, 3);
out.close();
}
}
public void displayAll(HttpServletRequest request, HttpServletResponse response, String SessionId)
throws IOException, ServletException {
String query = (String)null;
String Name = (String)null;
int Tshares = 0;
double Ipo = 0.00;
double Open = 0.00;
double Last = 0.00;
int Volume = 0;
double Change = 0.00;
double WkHi = 0.00;
double WkLo = 0.00;
double DayHi = 0.00;
double DayLo = 0.00;
double Previous = 0.00;
String fLast = (String)null;
String fChange = (String)null;
String fLow = (String)null;
String fHigh = (String)null;
String Symbol = (String)null;
ResultSet result = null;
double Capitalization = 0.00;
Vector dumpPlace = new Vector();
int i = 0;
// Set up decimal formatting for digits. This will force at least 0.00.
DecimalFormat curFormat = new DecimalFormat("$##,###,###,###,###,##0.00");
DecimalFormat numFormat = new DecimalFormat("################0.00");
DecimalFormat intFormat = new DecimalFormat("################0");
// Initialize output stream
response.setContentType("text/html");
PrintWriter out=response.getWriter();
// Extract Company info from the database. A null ResultSet means the company
// must not be in the database.
query = "select * from Companies where Symbol != 'SYS' order by Symbol";
result = sessionbase.doQuery(query);
try {
while (result.next()) {
Symbol = result.getString("Symbol");
Last = result.getDouble("Last");
DayHi = result.getDouble("DayHi");
DayLo = result.getDouble("DayLo");
Volume = result.getInt("Volume");
dumpPlace.addElement(new Companies(Symbol, DayHi, DayLo, Last, Volume));
}
} catch (Exception e) {
e.printStackTrace();
}
sessionbase.printTop(out, 25);
out.println(" ");
out.println("
");
out.println("");
out.println("
");
out.println("
");
out.println("");
out.println("
Symbol
High
Low
Current
Volume
Offers
Action
");
Companies Company = new Companies();
int vSize = dumpPlace.size();
for (i = 0 ; i < vSize ; i++) {
Company = (Companies)dumpPlace.elementAt(i);
Symbol = Company.getSymbol();
DayHi = Company.getHigh();
DayLo = Company.getLow();
Last = Company.getCurrent();
Volume = Company.getVolume();
out.println("
");
sessionbase.printBot(out, SessionId, 3);
out.close();
}
public void displayAllTrans(HttpServletRequest request, HttpServletResponse response, String SessionId)
throws IOException, ServletException {
String Trans = (String)null;
int numShares = 0;
double tradeAt = 0.00;
String AccountId = (String)null;
String query = (String)null;
String Symbol = request.getParameter("Symbol");
String Name = (String)null;
String Owner = (String)null;
String userName = (String)null;
ResultSet Symbols = null;
ResultSet result = null;
Vector dumpPlace = new Vector();
Vector Buys = new Vector();
Vector Sells = new Vector();
int i = 0;
int j = 0;
Timestamp timeStamp = null;
int buy = 0;
int sell = 0;
int check = 1;
// Set up decimal formatting for digits. This will force at least 0.00.
DecimalFormat curFormat = new DecimalFormat("$##,###,###,###,###,##0.00");
DecimalFormat numFormat = new DecimalFormat("################0.00");
DecimalFormat intFormat = new DecimalFormat("################0");
// Initialize output stream
response.setContentType("text/html");
PrintWriter out=response.getWriter();
// Retrieve AccountId
AccountId = sessionbase.getAccountId(SessionId);
if (AccountId == null || !AccountId.equals("1") ) {
response.sendRedirect(ServletURL + ".nonMember?task=LI&Type=1");
} else {
// Output the header information
sessionbase.printTop(out, 23);
// Get symbols to use. These will be plugged in to the next search.
query = "select Symbol from Companies where Symbol != 'SYS' order by Symbol";
Symbols = sessionbase.doQuery(query);
try {
while (Symbols.next()) {
Symbol = Symbols.getString("Symbol");
// Extract all offers for the symbols
query = "select * from qt" + Symbol.toLowerCase() + " order by Trans, Dtime";
result = sessionbase.doQuery(query);
while (result.next()) {
Trans = result.getString("Trans");
numShares = result.getInt("NumShares");
Owner = result.getString("AccountId");
tradeAt = result.getDouble("tradeAt");
timeStamp = result.getTimestamp("Dtime");
dumpPlace.addElement(new Offers(Symbol, Trans, tradeAt, numShares, timeStamp, Owner));
}
}
} catch (Exception e) {
e.printStackTrace();
}
int vSize = dumpPlace.size();
if (vSize < 1) {
out.println(" ");
out.println("
");
// Loop to go through each of the stocks in the storage
// vector.
for(int i=0; i");
Holdings hold = (Holdings) dumpPlace.elementAt(i);
Trans = hold.getTrans();
if (Trans.equals("C")) {
Profit = hold.getLast() * hold.getnumShares();
Total += Profit;
out.println("
");
// Get Breaking News Links
query = "select Title, Date, Id from News where Type = 2";
result = sessionbase.doQuery(query, SessionId);
String breakingTitle = (String)null;
java.util.Date breakingDate = new java.util.Date();
java.util.Date breakingTime = new java.util.Date();
String breakingId = (String)null;
// Make sure there are any Breaking News articles
int bId = 0;
query = "select Id from News where Type = 2";
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
bId = result.getInt("Id");
}
} catch (Exception e) {
e.printStackTrace();
}
if (bId > 0) {
out.println("
");
// Get Breaking News Links
// Make sure there are any Breaking News articles
int bId = 0;
query = "select Id from News where Type = 2";
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
bId = result.getInt("Id");
}
} catch (Exception e) {
e.printStackTrace();
}
if (bId > 0) {
query = "select Title, Date, Id from News where Type = 2";
result = sessionbase.doQuery(query, SessionId);
String breakingTitle = (String)null;
java.util.Date breakingDate = new java.util.Date();
java.util.Date breakingTime = new java.util.Date();
String breakingId = (String)null;
out.println("
");
sessionbase.printBot(out, SessionId, 3);
out.close();
}
public void addNews (String Title, String Author, String Type, String Story, String SessionId)
throws IOException {
String query = (String)null;
ResultSet result = null;
int Id = 0;
int newsId = 0;
Timestamp newsTime = null;
Timestamp newsTimeCmp = null;
int count = 0;
// Lock News table
sessionbase.doUpdate("lock tables News write", SessionId);
// Get next NewsId
query = "select max(Id) from News";
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
newsId = result.getInt("max(Id)");
}
} catch (Exception e) {
e.printStackTrace();
}
newsId++;
if (Type.equals("2")) {
// If item is Breaking News, check the total number of Breaking
// News items currently in queue. Shouldn't be more than three,
// if this would cause more, change the oldest to General News
query = "select Id, Stamp from News where Type = 2";
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
count++;
newsTime = result.getTimestamp("Stamp");
if (count == 1) {
newsTimeCmp = newsTime;
Id = result.getInt("Id");
}
if (newsTime.before(newsTimeCmp)) {
Id = result.getInt("Id");
}
}
} catch (Exception e) {
e.printStackTrace();
}
if (count >= 3) {
// Move oldest article to General
query = "update News set Type = 1 where Id = " + Id;
sessionbase.doUpdate(query, SessionId);
}
}
// If article is the new Front Article, move the old one to
// General News.
if (Type.equals("3")) {
// Check article, make sure it isn't a default article. If
// so, just delete it
String chkTitle = "";
String chkStory = "";
query = "select Title, Story from News where Type = 3";
result = sessionbase.doQuery(query, SessionId);
try {
while (result.next()) {
chkTitle += result.getString("Title");
chkStory += result.getString("Story");
}
} catch (Exception e) {
e.printStackTrace();
}
if (chkTitle.equals("No News") && chkStory.equals("No news is good news.")) {
// This must be default article, just delete it
query = "delete from News where Type = 3";
sessionbase.doUpdate(query, SessionId);
} else {
query = "update News set Type = 1 where Type = 3";
sessionbase.doUpdate(query, SessionId);
}
}
// Modify any ' in the string
char Checkit;
String StorChek = "";
for (int i = 0 ; i < Story.length() ; i++) {
Checkit = Story.charAt(i);
if (Checkit == '\''){
StorChek += "\\'";
} else {
StorChek += Checkit;
}
}
Story = StorChek;
String TitlChek = "";
for (int i = 0 ; i < Title.length() ; i++) {
Checkit = Title.charAt(i);
if (Checkit == '\''){
TitlChek += "\'";
} else {
TitlChek += Checkit;
}
}
Title = TitlChek;
// Add news item
query = "insert into News (Title, Author, Date, Type, Story, Id, Stamp) values ('" + Title + "', '" + Author + "', NOW(), " + Type + ", '" + Story + "', " + newsId + ", NOW())";
sessionbase.doUpdate(query, SessionId);
// Unlock tables
sessionbase.doUpdate("unlock tables", SessionId);
}
public void displayQuickNews(PrintWriter out, String SessionId)
throws IOException, ServletException {
ResultSet result = null;
String query = (String)null;
Locale locale = new Locale("en","US");
DateFormat shortFormat = DateFormat.getDateInstance(DateFormat.SHORT, locale);
out.println("
");
out.println("
");
out.println("Market News
");
out.println("
");
out.println("
");
out.println("
");
String weekTitle = (String)null;
String weekId = (String)null;
java.util.Date weekDate = new java.util.Date();
java.util.Date weekDateO = new java.util.Date();
// We only want to get Headlines within 2 weeks of the current
// date, set up the Date qualifier.
query = "select Title, Id, Date from News order by Date desc";
result = sessionbase.doQuery(query, SessionId);
int reps = 0;
try {
while (result.next() && reps < 6) {
weekDateO = weekDate;
reps++;
weekTitle = result.getString("Title");
weekId = result.getString("Id");
weekDate = result.getDate("Date");
if (!weekDate.equals(weekDateO)) {
// Just a formatting thing, don't want to push down the links
// if it is printing the top one.
if (reps > 1) {
out.println(" ");
}
out.println("" + shortFormat.format(weekDate) + " ");
out.println("° " + weekTitle + " ");
} else {
out.println("° " + weekTitle + " ");
}
}
} catch (Exception e) {
e.printStackTrace();
}
out.println(" all headlines...");
out.println("
");
}
// This method will delete all the holdings of a user, restoring them to
// the originating company.
private void deleteHoldings(String AccountId)
throws IOException {
ResultSet result = null;
String query = (String)null;
String Symbol = (String)null;
int Shares = 0;
String Username = sessionbase.getUser(AccountId);
// Delete anything the user might have in Frozen
query = "delete from Frozen where AccountId = " + AccountId;
sessionbase.doUpdate(query);
// Find all pending offers and delete them from the queue tables
query = "select Symbol from " + Username + " where Trans = 'S' or Trans = 'B'";
result = sessionbase.doQuery(query);
try {
while (result.next()) {
Symbol = result.getString("Symbol");
query = "delete from qt" + Symbol.toLowerCase() + " where AccountId = " + AccountId;
sessionbase.doUpdate(query);
}
} catch (Exception e) {
e.printStackTrace();
}
// Delete all Buy offers the user has
query = "delete from " + Username + " where Trans = 'B' or Trans = 'FB'";
sessionbase.doUpdate(query);
// Loop through all other holdings, restoring them to the Company
query = "select Symbol, NumShares from " + Username;
result = sessionbase.doQuery(query);
try {
while (result.next()) {
Symbol = result.getString("Symbol");
Shares = result.getInt("NumShares");
sessionbase.modHoldings(Symbol, Symbol, Shares);
}
} catch (Exception e) {
e.printStackTrace();
}
// Delete what's left
query = "delete from " + Username;
sessionbase.doUpdate(query);
}
// This method deletes a Company user. Any User with holdings of this
// Company has their holdings converted to cash.
private void deleteCompany(String Symbol)
throws IOException {
String query = (String)null;
ResultSet result = null;
ResultSet result2 = null;
int Shares = 0;
double Value = 0.00;
double Earn = 0.00;
String User = (String)null;
// First get the last value and proper Symbol of the company
query = "select Last, Symbol from Companies where Symbol = '" + Symbol + "'";
result = sessionbase.doQuery(query);
try {
while (result.next()) {
Value = result.getDouble("Last");
Symbol = result.getString("Symbol");
}
} catch (Exception e) {
e.printStackTrace();
}
// Check all user accounts and convert their shares to cash.
query = "select Username from Account where Type != 'C'";
result = sessionbase.doQuery(query);
try {
while (result.next()) {
Earn = 0;
User = result.getString("Username");
query = "select NumShares from " + User + " where Symbol = '" + Symbol + "' and Trans = 'C' or Trans = 'S' or Trans = 'FS'";
result2 = sessionbase.doQuery(query);
try {
while (result2.next()) {
Shares = result2.getInt("NumShares");
Earn = Earn + (Shares * Value);
}
} catch (Exception e) {
e.printStackTrace();
}
// Add the money to the user
sessionbase.modBalance(sessionbase.getAccountId(User, 1), Earn, "+");
// Delete the company from the users holdings
query = "delete from " + User + " where Symbol = '" + Symbol + "'";
sessionbase.doUpdate(query);
}
} catch (Exception e) {
e.printStackTrace();
}
// Delete Company User
deleteHoldings(sessionbase.getAccountId(Symbol, 1));
// Delete any entries in Frozen
query = "delete from Frozen where Symbol = '" + Symbol + "'";
sessionbase.doUpdate(query);
// Delete the company tables
query = "drop table qt" + Symbol.toLowerCase();
sessionbase.doUpdate(query);
query = "drop table ht" + Symbol.toLowerCase();
sessionbase.doUpdate(query);
query = "drop table eht" + Symbol.toLowerCase();
sessionbase.doUpdate(query);
// Delete from Companies
query = "delete from Companies where Symbol = '" + Symbol + "'";
sessionbase.doUpdate(query);
query = "delete from Portfolio where AccountId = " + sessionbase.getAccountId(Symbol, 1);
sessionbase.doUpdate(query);
query = "delete from Account where Username = '" + Symbol + "'";
sessionbase.doUpdate(query);
query = "drop table " + Symbol;
sessionbase.doUpdate(query);
}
// This method resets a users balance back to the default value specified
// in Parameters
private void resetBalance(String AccountId, String Type)
throws IOException {
String query = (String)null;
ResultSet result = null;
double defaultBal = 0.00;
if (Type.equals("U")) {
query = "select userBalance from Parameters";
result = sessionbase.doQuery(query);
try {
while (result.next()) {
defaultBal = result.getDouble("userBalance");
}
} catch (Exception e) {
e.printStackTrace();
}
} else if (Type.equals("C")) {
query = "select compBalance from Parameters";
result = sessionbase.doQuery(query);
try {
while (result.next()) {
defaultBal = result.getDouble("compBalance");
}
} catch (Exception e) {
e.printStackTrace();
}
}
query = "update Portfolio set Balance = " + defaultBal + " where AccountId = " + AccountId;
sessionbase.doUpdate(query);
}
static public void reload() {
SystemURL = sessionbase.getURL();
ServletURL = sessionbase.getServletURL();
SystemEmail = sessionbase.getEmail();
SystemPath = sessionbase.getPath();
SystemName = sessionbase.getName();
SystemShortName = sessionbase.getShortName();
SystemFreeze = sessionbase.getFreezeStatus();
SystemLock = sessionbase.getLockStatus();
}
}