// .NET StockTrader Sample WCF Application for Benchmarking, Performance Analysis and Design Considerations for Service-Oriented Applications
using System;
using System.Globalization;
using System.Web;
using System.Text;
using Trade.StockTraderWebApplicationModelClasses;
using Trade.StockTraderWebApplicationSettings;
using Trade.StockTraderWebApplicationServiceClient;
using Trade.Utility;
namespace Trade.Web
{
///
/// Calls into Business Services to place an order (buy/sell). Note that this page is called from the
/// StockTrade.aspx page after user enters number of shares. This is slightly different than Trade 6.1 flow,
/// but necessary since we allow selling portions of a holding vs. just entire holding ala Trade 6.1. However,
/// for benchmarking this page can be called directly without visiting StockTrade.aspx when comparing perf to Trade 6.1,
/// for equivalence.
///
public partial class Order : System.Web.UI.Page
{
public bool badQuantity = false;
protected void Page_Load(object sender, EventArgs e)
{
Date.Text = DateTime.Now.ToString("f");
BSLClient businessServicesClient = new BSLClient();
OrderDataUI order = null;
string userid = HttpContext.Current.User.Identity.Name;
string action = Input.InputText(Request["action"], 10);
double quantity = 0;
string symbol = null;
int holdingID = -1;
if (action.Equals(StockTraderUtility.ORDER_TYPE_BUY))
{
if (!Double.TryParse(Request["quantity"], out quantity))
badQuantity = true;
symbol = Input.InputText(Request["symbol"], StockTraderUtility.SYMBOLSTRING_MAXLENGTH);
if (quantity > 0)
order = businessServicesClient.buy(userid, symbol, quantity);
else
badQuantity = true;
}
else if (action.Equals(StockTraderUtility.ORDER_TYPE_SELL))
{
bool hasQuantityParam=false;
holdingID = Convert.ToInt32(Request["holdingid"]);
//WebSphere Trade 6.1 does not provide functionality for trading partial holdings.
//.NET StockTrader does, but will default to Trade 6.1 behavior and sell and entire holding if no
//quantity parameter is detected on the query string. Here we check if a quantity parameter actually
//exists on the query string. This is a bit of extra overhead, but done so we can add functionality
// (selling part of a holding) yet not sacrifice interop with WebSphere Trade 6.1
int i = Request.Params.Count;
for (int index = 0; index < i; index++)
{
string param = Request.Params.GetKey(index);
if (param.Contains("quantity"))
{
hasQuantityParam = true;
break;
}
}
if (hasQuantityParam)
{
quantity = Convert.ToDouble(Request["quantity"]);
if (quantity <= 0)
badQuantity = true;
}
else
quantity = 0; //Value of 0 indicates to sell entire holding.
if (!badQuantity)
order = businessServicesClient.sell(userid, holdingID, quantity);
}
else
//Goodbye! Only valid ops are buy and sell. This is a harsh
//penalty for trying to be tricky.
Response.Redirect(Settings.PAGE_LOGOUT);
if (!badQuantity)
{
if (order != null)
{
Cache.Remove(Settings.CACHE_KEY_CLOSED_ORDERSALERT + userid);
string orderIdStr = order.orderID.ToString();
OrderID.Text = orderIdStr;
OrderStatus.Text = order.orderStatus;
OpenDate.Text = order.openDate.ToString();
CompletionDate.Text = order.completionDate.ToString();
OrderFee.Text = string.Format("{0:C}", order.orderFee);
OrderType.Text = order.orderType;
string orderLink = order.quoteLink;
Symbol.Text = orderLink;
string orderQty = string.Format("{0:0,0}", order.quantity);
Quantity.Text = orderQty;
StringBuilder strBuilder = new StringBuilder("Order ");
strBuilder.Append(orderIdStr);
strBuilder.Append(" to ");
strBuilder.Append(order.orderType);
strBuilder.Append(" ");
strBuilder.Append(orderQty);
strBuilder.Append(" shares of ");
strBuilder.Append(orderLink);
strBuilder.Append(" has been submitted for processing.
");
strBuilder.Append("Order Details:");
ConfirmMessage.Text = (strBuilder.ToString());
}
else
ConfirmMessage.Text = StockTraderUtility.EXCEPTION_MESSAGE_BAD_ORDER_RETURN;
}
else ConfirmMessage.Text = StockTraderUtility.EXCEPTION_MESSAGE_BAD_ORDER_PARMS;
return;
}
}
}