* @see TradeServices#sell(String, Integer)
*/
public OrderDataBean sell(String userID, Integer holdingID, int orderProcessingMode) throws Exception {
Connection conn = null;
OrderDataBean orderData = null;
/*
* total = (quantity * purchasePrice) + orderFee
*/
BigDecimal total;
try {
if (Log.doTrace())
Log.trace("TradeJdbc:sell - inSession(" + this.inSession + ")", userID, holdingID);
conn = getConn();
AccountDataBean accountData = getAccountData(conn, userID);
HoldingDataBean holdingData = getHoldingData(conn, holdingID.intValue());
QuoteDataBean quoteData = null;
if (holdingData != null)
quoteData = getQuoteData(conn, holdingData.getQuoteID());
if ((accountData == null) || (holdingData == null) || (quoteData == null)) {
String error =
"TradeJdbc:sell -- error selling stock -- unable to find: \n\taccount=" + accountData
+ "\n\tholding=" + holdingData + "\n\tquote=" + quoteData + "\nfor user: " + userID
+ " and holdingID: " + holdingID;
Log.error(error);
rollBack(conn, new Exception(error));
return orderData;
}
double quantity = holdingData.getQuantity();
orderData = createOrder(conn, accountData, quoteData, holdingData, "sell", quantity);
// Set the holdingSymbol purchaseDate to selling to signify the sell
// is "inflight"
updateHoldingStatus(conn, holdingData.getHoldingID(), holdingData.getQuoteID());
// UPDATE -- account should be credited during completeOrder
BigDecimal price = quoteData.getPrice();
BigDecimal orderFee = orderData.getOrderFee();
total = (new BigDecimal(quantity).multiply(price)).subtract(orderFee);
creditAccountBalance(conn, accountData, total);
completeOrder(conn, orderData.getOrderID());
orderData = getOrderData(conn, orderData.getOrderID().intValue());
commit(conn);
} catch (Exception e) {
Log.error("TradeJdbc:sell error", e);
rollBack(conn, e);
} finally {
releaseConn(conn);
}
if (!(orderData.getOrderStatus().equalsIgnoreCase("cancelled")))
//after the purchase or sell of a stock, update the stocks volume
// and price
updateQuotePriceVolume(orderData.getSymbol(), TradeConfig.getRandomPriceChangeFactor(), orderData.getQuantity());
return orderData;
}