| TradeManagementHelper.java |
/*
* @author : Pushkala
* @version : 1.0
*
* Development Environment : Oracle9i JDeveloper
*
* Name of the File : TradeManagementHelper.java
*
* Creation / Modification History
* Pushkala 26-Apr-2002 Created
*
*/
package oracle.otnsamples.ibfbs.trademanagement.helper;
// Required EJB Classes
import javax.ejb.CreateException;
import java.rmi.RemoteException;
// Utility classes
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Collection;
import java.util.Date;
// JNDI
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.sql.SQLException; // SQL
import javax.servlet.http.HttpServletRequest; // Required Servlet Classes
// Required TradeManagement classes
import oracle.otnsamples.ibfbs.trademanagement.ejb.TradeManagementSessionRemote;
import oracle.otnsamples.ibfbs.trademanagement.ejb.TradeManagementSessionHomeRemote;
import oracle.otnsamples.ibfbs.trademanagement.exception.TradeManagementEventException;
import oracle.otnsamples.ibfbs.trademanagement.helper.DateFormatter;
// Required UserManagement classes
import oracle.otnsamples.ibfbs.usermanagement.ejb.ContactInfo;
import oracle.otnsamples.ibfbs.usermanagement.ejb.AccountInfo;
import oracle.otnsamples.ibfbs.usermanagement.helper.UserManagementHelper;
import oracle.otnsamples.ibfbs.usermanagement.exception.ApplicationError;
// Required Admin classes
import oracle.otnsamples.ibfbs.admin.helper.StockSymbolHelper;
import oracle.otnsamples.ibfbs.admin.exception.AdminHelperException;
// Required Utility classes
import oracle.otnsamples.ibfbs.utils.ConnectionParams;
/**
* This class is registered in the control.xml file of the application.
* This forms the interface between the controller servlet and the model
* where the business logic is embedded consisting of the
* TradeManagementSessionFacadeBean which further accesses the Entity Beans.
* This creates an instance of the TradeManagementSessionFacadeBean in the
* constructor which can then be used in all the methods.
*
* @version 1.0
* @since 1.0
*/
public class TradeManagementHelper {
/** Reference to Remote interface */
private TradeManagementSessionRemote rmr = null;
/** Reference to Home interface */
private TradeManagementSessionHomeRemote rmrh = null;
/** Instance of StockSymbolHelper */
private StockSymbolHelper ssh = null;
/** Instance of UserManagementHelper */
private UserManagementHelper umh = null;
private Hashtable connHash = null;
/**
* Constructor of the class. Here the initialize method is called.
*/
public TradeManagementHelper() {
initialize();
}
/**
* Initialization method used to initialize the Home and Remote
* Interfaces of TradeManagementSessionFacadeBean. Here the instance
* of TradeManagementSessionFacadeBean is created and stored in the
* global variable.
* @since 1.0
*/
private void initialize() {
if (rmr == null || rmrh == null) {
try {
InitialContext ic = new InitialContext();
// Look up the TradeManagementSessionFacadeBean
rmrh = (TradeManagementSessionHomeRemote)
ic.lookup("java:comp/env/ejb/TradeManagementSessionHomeRemote");
rmr = rmrh.create();
// Instantiate StockSymbolHelper
ssh = new StockSymbolHelper();
// Instantiate UserManagementHelper
umh = new UserManagementHelper();
} catch(RemoteException ne) {
ne.printStackTrace();
} catch(CreateException ce) {
ce.printStackTrace();
} catch(NamingException ce) {
ce.printStackTrace();
} catch(Exception ex) {
ex.printStackTrace();
}
}
if(connHash == null) {
connHash = new Hashtable();
connHash.put("EXCHQCONNFACTORY", ConnectionParams.exchangeQueueConnFactory);
connHash.put("EXCHQUEUE", ConnectionParams.exchangeQueue);
}
}
/**
* This method calls the method in TradeManagementSessionFacadeBean
* to get the Portfolio details corresponding to the Account Number
* and then returns the obtained Collection.
*
* @param accountNo Account Number
* @param recordNumber Record Number
* @param linesPerPage Lines Per Page
* @return The Collection of Portfolio Information
* @exception TradeManagementEventException
* @since 1.0
*/
private Collection getPortfolioInfo(Integer accountNo,
String recordNumber,
int linesPerPage)
throws TradeManagementEventException {
try {
// Access the method of the Session Bean and return the Collection
return rmr.getPortfolio(accountNo, recordNumber, linesPerPage);
} catch(RemoteException re) {
throw new TradeManagementEventException(
"Remote Exception while getting Portfolio info : " + re.toString());
}
}
/**
* This method calls the method in the TradeManagementSessionFacadeBean
* to get the Portfolio Valuation details corresponding to the
* Account Number and returns the new Collection.
*
* @param accountNo Account Number
* @param recordNumber Record Number
* @param linesPerPage Lines Per Page
* @return The Collection of Portfolio Valuation Information
* @exception TradeManagementEventException
* @since 1.0
*/
private Collection getPortfolioValuation(Integer accountNo,
String recordNumber,
int linesPerPage)
throws TradeManagementEventException {
try {
// Access the method of the Session Bean and return the Collection
return rmr.getPortfolioValuation(accountNo, recordNumber, linesPerPage);
} catch(RemoteException re) {
throw new TradeManagementEventException(
"Remote Exception while getting Portfolio Valuation info : "
+ re.toString());
}
}
/**
* This method calls the method in the TradeManagementSessionFacadeBean to
* get the Trade details corresponding to the input Account Number and then
* returns the obtained Collection.
*
* @param accountNo Account Number
* @param recordNumber Record Number
* @param linesPerPage Lines Per Page
* @return The Collection of Trade Details Information
* @exception TradeManagementEventException
* @since 1.0
*/
private Collection getTradeDetailsInfo(Integer accountNo,
String recordNumber,
int linesPerPage)
throws TradeManagementEventException {
try {
// Access the method of the Session Bean and return the Collection
return rmr.getTradeDetails(accountNo, recordNumber, linesPerPage);
} catch(RemoteException re) {
throw new TradeManagementEventException(
"Remote Exception while getting Trade Details info : " + re.toString());
}
}
/**
* This method is registered in control.xml and will be called in the
* controller servlet using Reflection to get the values required to be
* displayed in the JSPs invoked by the client. This method calls the
* methods in the same class to get Portfolio details along with Valuation
* details, Account Information. This then creates a Hashmap of the
* retrieved values and returns it to the calling JSP where it is displayed.
*
* @param req HttpServletRequest
* @return The Hashmap containing the Portfolio Valuation Information
* and User Account Information
* @exception TradeManagementEventException
* @since 1.0
*/
public HashMap populatePortfolioValuation(HttpServletRequest req)
throws TradeManagementEventException {
HashMap hm = new HashMap();
// The User Account Number is retrieved from the session attribute
Integer acctNo = (Integer)req.getSession().getAttribute("LOGIN.RESPONSE");
// Access the method of the Session Bean and set the values in the Hashmap
AccountInfo acInfo = (AccountInfo)getAccountInfo(acctNo);
String recordNum = req.getParameter("RECORDNUM");
if (recordNum == null) recordNum = "1";
hm.put("ACCOUNTINFO", acInfo);
hm.put("PORTFOLIOVALUATION",
getPortfolioValuation(acctNo, recordNum,
acInfo.getLinesPerPage().intValue()));
return hm;
}
/**
* This method is registered in control.xml and will be called in the
* controller servlet using Reflection to get the values required to be
* displayed in the JSPs invoked by the client. This method calls the
* methods in the same class to get Portfolio details, Account Information.
* This then creates a Hashmap of the retrieved values and returns it to
* the calling JSP where it is displayed.
*
* @param req HttpServletRequest
* @return The Hashmap containing the Portfolio Information
* and User Account Information
* @exception TradeManagementEventException
* @since 1.0
*/
public HashMap populatePortfolio(HttpServletRequest req)
throws TradeManagementEventException {
HashMap hm = new HashMap();
// The User Account Number is retrieved from the session attribute
Integer acctNo = (Integer)req.getSession().getAttribute("LOGIN.RESPONSE");
// Access the method of the Session Bean and set the values in the Hashmap
AccountInfo acInfo = (AccountInfo)getAccountInfo(acctNo);
String recordNum = req.getParameter("RECORDNUM");
if (recordNum == null) recordNum = "1";
hm.put("ACCOUNTINFO", acInfo);
hm.put("PORTFOLIOINFO",
getPortfolioInfo(acctNo, recordNum,
acInfo.getLinesPerPage().intValue()));
return hm;
}
/**
* This method returns the Account Information of the logged in user.
*
* @param accountNo User Account Number
* @return The Account Information as a Value Object AccountInfo
* @exception TradeManagementEventException
* @since 1.0
*/
private AccountInfo getAccountInfo(Integer accountNo)
throws TradeManagementEventException {
AccountInfo aInfo = null;
try {
// Get the Account Information of the User, Lines Per Page is required!
aInfo = umh.getAccountInfo(accountNo);
} catch(Exception ex) {
throw new TradeManagementEventException(
"Exception while getting Account information: " + ex.toString());
}
return aInfo;
}
/**
* This method is registered in control.xml and will be called in the
* controller servlet using Reflection to get the values required to be
* displayed in the JSP invoked by the client. This method calls the
* methods in the same class to get the Trade Details and Account Information.
* This then creates a Hashmap of the retrieved values and returns it to the
* calling JSP where it is displayed.
*
* @param req HttpServletRequest
* @return The Hashmap containing the Portfolio Information,
* Portfolio Valuation Information and User Account Information
* @exception TradeManagementEventException
* @since 1.0
*/
public HashMap populateTradeDetails(HttpServletRequest req)
throws TradeManagementEventException {
HashMap hm = new HashMap();
// The User Account Number is retrieved from the session attribute
Integer acctNo = (Integer)req.getSession().getAttribute("LOGIN.RESPONSE");
// Access the method of the Session Bean and set the values in the Hashmap
AccountInfo acInfo = (AccountInfo)getAccountInfo(acctNo);
String recordNum = req.getParameter("RECORDNUM");
if (recordNum == null) recordNum = "1";
hm.put("ACCOUNTINFO", acInfo);
hm.put("TRADEDETAILSINFO",
getTradeDetailsInfo(acctNo, recordNum,
acInfo.getLinesPerPage().intValue()));
return hm;
}
/**
* This method is called when the user buys stock using the application.
* The User Account Number is obtained from the session of the user's request.
* The Symbol and Quantity traded are obtained from the request parameters.
* StockSymbolHelper is used to check if the symbol is traded here.
* UserManagementHelper is used to get the Contact Information of the
* logged in user. This method calls the method in
* TradeManagementSessionFacadeBean to carry out the buy transaction.
*
* @param req HttpServletRequest
* @exception ApplicationError When an application specific error
* message needs to be thrown and handled
* @exception TradeManagementEventException
* @since 1.0
*/
public void buyStock(HttpServletRequest req)
throws ApplicationError, TradeManagementEventException {
String retMessage = "FAILURE";
// Get user account number from the session of user's request
Integer accountNo = (Integer)req.getSession().getAttribute("LOGIN.RESPONSE");
// Get the quantity from the request parameters
String qty = req.getParameter("QUANTITY").trim();
if ((qty == null) || (qty == "")) {
throw new ApplicationError("Quantity should be entered");
}
Integer quantity = null;
try {
quantity = new Integer(qty.trim());
} catch (NumberFormatException nfe) {
throw new ApplicationError("Quantity should be a number");
}
if (quantity.intValue() <= 0) {
throw new ApplicationError("Quantity should be greater than zero");
}
// Get the symbol from the request parameters
String symbol = req.getParameter("SYMBOL").trim().toUpperCase();
if ((symbol == null) || (symbol == "")) {
throw new ApplicationError("Symbol should be entered");
}
boolean isTraded = false;
try {
// Get the isTraded flag from the StockSymbolHelper
isTraded = ssh.isTradedHere(symbol);
} catch(AdminHelperException ex) {
throw new TradeManagementEventException(
"Exception while getting isTraded flag: " + ex.toString());
}
ContactInfo cInfo = null;
try {
// Get the Contact Information of the User, email id is required!
cInfo = umh.getContactInfo(accountNo);
} catch(Exception ex) {
throw new TradeManagementEventException(
"Exception while getting User's Contact Info : " + ex.toString());
}
Date sysDate = new Date(); // Current Date
try {
// Call the method in TradeManagementSessionFacadeBean
// to carry out the buy transaction
retMessage = rmr.buyStock(accountNo, quantity, symbol,
isTraded, cInfo.getEmail(), connHash,
DateFormatter.getStringFromDate(sysDate,
"yyyy.MM.dd hh:mm:ss a zzz"));
} catch(SQLException re) {
throw new TradeManagementEventException(
"SQL Exception while calling buy stock : " + re.toString());
} catch(RemoteException re) {
throw new TradeManagementEventException(
"Remote Exception while calling buy stock : " + re.toString());
}
if (retMessage.equals("SUCCESS")) {
StringBuffer sb = new StringBuffer();
sb.append("Transaction completed. ");
sb.append(quantity);
sb.append(" stocks of '");
sb.append(symbol);
sb.append("' added to your Portfolio ");
req.setAttribute("InfoMessage", sb.toString());
} else {
throw new ApplicationError(retMessage);
}
}
/**
* This method is used by the AdminHelper to transfer stocks from a corporate
* to an individual. StockSymbolHelper is used to check if the symbol is
* traded here. UserManagementHelper is used to get the Contact Information
* of the logged in user. This method calls the method in
* TradeManagementSessionFacadeBean to carry out the transfer of stocks to
* the input Account Number.
*
* @param toAccountNumber Account Number to which Stocks are transfered
* @param symbol Stock Symbol
* @param qty Quantity
* @param price Price
* @return An integer indicating success or failure,
* 0 indicates success and -1 indicates failure
* @exception TradeManagementEventException
* @since 1.0
*/
public Integer corporateTransfer(Integer toAccountNumber, String symbol,
Integer qty, Float price)
throws ApplicationError, TradeManagementEventException {
Integer nextTradeId = new Integer("0");
boolean isTraded = false;
try {
// Get the isTraded flag from the StockSymbolHelper
isTraded = ssh.isTradedHere(symbol);
} catch(AdminHelperException ex) {
throw new TradeManagementEventException(
"Exception while getting isTraded flag: " + ex.toString());
}
ContactInfo cInfo = null;
try {
// Get the Contact Information of the User, email id is required!
cInfo = umh.getContactInfo(toAccountNumber);
} catch(Exception ex) {
throw new TradeManagementEventException(
"Exception while getting User's Contact Info : " + ex.toString());
}
Date sysDate = new Date(); // Current Date
try {
// Call the method in TradeManagementSessionFacadeBean
// to carry out the corporate transfer
nextTradeId = rmr.corporateTransfer(toAccountNumber, symbol, qty, price,
isTraded, cInfo.getEmail(), connHash,
DateFormatter.getStringFromDate(
sysDate, "yyyy.MM.dd hh:mm:ss a zzz"));
} catch(RemoteException re) {
throw new TradeManagementEventException(
"Remote Exception while calling corporate transfer : " + re.toString());
}
return nextTradeId;
}
/**
* This method is used when the user sells stock using the application.
* The User Account Number is obtained from the session of the user's request.
* The Sell Quantity, Line Numbers, Symbols, Trade Id are obtained from the
* request parameters. StockSymbolHelper is used to check if the symbol is
* traded here. UserManagementHelper is used to get the Contact Information
* of the logged in user. This method calls the method in
* TradeManagementSessionFacadeBean to carry out the sell transaction of stocks.
*
* @param req HttpServletRequest
* @return The return message is a string which contains details
* about the failure or success of the sell transactions.
* @exception TradeManagementEventException
* @since 1.0
*/
public String sellStock(HttpServletRequest req)
throws ApplicationError, TradeManagementEventException {
Integer accountNo = (Integer)req.getSession().getAttribute("LOGIN.RESPONSE");
String[] qtyValues = req.getParameterValues("SELLQTY");
String[] lineNos = req.getParameterValues("LINENO");
String[] symbols = req.getParameterValues("SYMBOL");
String[] tradeId = req.getParameterValues("TRADEID");
Date sysDate = new Date();
String retMessage = "FAILURE";
String isTradedFlag[] = new String[lineNos.length];
try {
for(int i = 0;i < lineNos.length;i++) {
// Get the isTraded flag for the symbol from StockSymbolHelper
boolean isTraded = ssh.isTradedHere(symbols[i]);
// Set the isTradedFlag array to 'Y' or 'N'
// depending on whether isTraded is 'true' or 'false'
if (isTraded) {
isTradedFlag[i] = new String("Y");
} else {
isTradedFlag[i] = new String("N");
}
}
} catch(AdminHelperException ex) {
throw new TradeManagementEventException(
"Exception while getting isTraded flag: " + ex.toString());
}
ContactInfo cInfo = null;
try {
// Get the Contact Information of the User, email id is required!
cInfo = umh.getContactInfo(accountNo);
} catch(Exception ex) {
throw new TradeManagementEventException(
"Exception while getting User's Contact Info : " + ex.toString());
}
try {
// Call the method in TradeManagementSessionFacadeBean
// to carry out the sell transactions
retMessage =
rmr.sellStock(accountNo, qtyValues, lineNos, symbols,
tradeId, isTradedFlag, cInfo.getEmail(), connHash,
DateFormatter.getStringFromDate(sysDate,
"yyyy.MM.dd hh:mm:ss a zzz"));
} catch(SQLException re) {
throw new TradeManagementEventException(
"SQL Exception while calling sell stock : " + re.toString());
} catch(RemoteException re) {
throw new TradeManagementEventException(
"Remote Exception while calling sell stock : " + re.toString());
}
return retMessage;
}
}