}
private OrderDataBean completeOrder(Connection conn, Integer orderID) throws Exception {
OrderDataBean orderData = null;
if (Log.doTrace())
Log.trace("TradeJdbc:completeOrderInternal - inSession(" + this.inSession + ")", orderID);
PreparedStatement stmt = getStatement(conn, getOrderSQL);
stmt.setInt(1, orderID.intValue());
ResultSet rs = stmt.executeQuery();
if (!rs.next()) {
Log.error("TradeJdbc:completeOrder -- unable to find order: " + orderID);
stmt.close();
return orderData;
}
orderData = getOrderDataFromResultSet(rs);
String orderType = orderData.getOrderType();
String orderStatus = orderData.getOrderStatus();
// if (order.isCompleted())
if ((orderStatus.compareToIgnoreCase("completed") == 0)
|| (orderStatus.compareToIgnoreCase("alertcompleted") == 0)
|| (orderStatus.compareToIgnoreCase("cancelled") == 0))
throw new Exception("TradeJdbc:completeOrder -- attempt to complete Order that is already completed");
int accountID = rs.getInt("account_accountID");
String quoteID = rs.getString("quote_symbol");
int holdingID = rs.getInt("holding_holdingID");
BigDecimal price = orderData.getPrice();
double quantity = orderData.getQuantity();
BigDecimal orderFee = orderData.getOrderFee();
// get the data for the account and quote
// the holding will be created for a buy or extracted for a sell
/*
* Use the AccountID and Quote Symbol from the Order AccountDataBean accountData = getAccountData(accountID,
* conn); QuoteDataBean quoteData = getQuoteData(conn, quoteID);
*/
String userID = getAccountProfileData(conn, new Integer(accountID)).getUserID();
HoldingDataBean holdingData = null;
if (Log.doTrace())
Log.trace("TradeJdbc:completeOrder--> Completing Order " + orderData.getOrderID() + "\n\t Order info: "
+ orderData + "\n\t Account info: " + accountID + "\n\t Quote info: " + quoteID);
// if (order.isBuy())
if (orderType.compareToIgnoreCase("buy") == 0) {
/*
* Complete a Buy operation - create a new Holding for the Account - deduct the Order cost from the Account
* balance
*/
holdingData = createHolding(conn, accountID, quoteID, quantity, price);
updateOrderHolding(conn, orderID.intValue(), holdingData.getHoldingID().intValue());
}
// if (order.isSell()) {
if (orderType.compareToIgnoreCase("sell") == 0) {
/*
* Complete a Sell operation - remove the Holding from the Account - deposit the Order proceeds to the
* Account balance
*/
holdingData = getHoldingData(conn, holdingID);
if (holdingData == null)
Log.debug("TradeJdbc:completeOrder:sell -- user: " + userID + " already sold holding: " + holdingID);
else
removeHolding(conn, holdingID, orderID.intValue());
}
updateOrderStatus(conn, orderData.getOrderID(), "closed");
if (Log.doTrace())
Log.trace("TradeJdbc:completeOrder--> Completed Order " + orderData.getOrderID() + "\n\t Order info: "
+ orderData + "\n\t Account info: " + accountID + "\n\t Quote info: " + quoteID + "\n\t Holding info: "
+ holdingData);
stmt.close();