if (Log.doTrace())
Log.trace("TradeJpaAm:sell", userID, holdingID, orderProcessingMode);
AccountProfileDataBeanImpl profile = entityManager.find(AccountProfileDataBeanImpl.class, userID);
AccountDataBean account = profile.getAccount();
HoldingDataBeanImpl holding = entityManager.find(HoldingDataBeanImpl.class, holdingID);
if (holding == null) {
Log.error("TradeJpaAm:sell User " + userID
+ " attempted to sell holding " + holdingID
+ " which has already been sold");
OrderDataBean orderData = new OrderDataBeanImpl();
orderData.setOrderStatus("cancelled");
entityManager.persist(orderData);
entityManager.getTransaction().commit();
if (entityManager != null) {
entityManager.close();
entityManager = null;
}
return orderData;
}
QuoteDataBean quote = holding.getQuote();
double quantity = holding.getQuantity();
order = createOrder(account, quote, holding, "sell", quantity,
entityManager);
// UPDATE the holding purchase data to signify this holding is
// "inflight" to be sold
// -- could add a new holdingStatus attribute to holdingEJB
holding.setPurchaseDate(new java.sql.Timestamp(0));
// UPDATE - account should be credited during completeOrder
BigDecimal price = quote.getPrice();
BigDecimal orderFee = order.getOrderFee();
BigDecimal balance = account.getBalance();
total = (new BigDecimal(quantity).multiply(price)).subtract(orderFee);
account.setBalance(balance.add(total));
// commit the transaction before calling completeOrder
entityManager.getTransaction().commit();
if (orderProcessingMode == TradeConfig.SYNCH)