package StockTradeServices;
import Framework.Async;
import Framework.AsyncStarter;
import Framework.EventManager;
import Framework.ParameterHolder;
import Framework.ParameterHolder_float;
import Framework.ParameterHolder_integer;
import Framework.RemoteEvent;
import Framework.RuntimeProperties;
import Framework.ServiceObject;
import Framework.ServiceObjectContext;
import GenericDBMS.TransactionMgr;
import StockTradeBusinessClasses.Array_Of_Order;
import StockTradeBusinessClasses.Order;
import StockTradeServices.Sequencer;
import StockTradeServices.interfaces.*;
import StockTradeServices.interfaces.ITradeMgr;
import java.io.Serializable;
import java.lang.Cloneable;
import java.lang.RuntimeException;
import java.lang.String;
import java.rmi.RemoteException;
import java.util.Hashtable;
import java.util.Map;
import org.apache.log4j.Logger;
/**
* TradeMgr<p>
* <p>
* @author Generated from Forte
* @since 24-Aug-2010
*/
@RuntimeProperties(isDistributed=true, isAnchored=true, isShared=false, isTransactional=false)
@ServiceObject(name="TradeSO", visibility=ServiceObject.Visibility.ENVIRONMENT, dialogDuration=ServiceObject.DialogDuration.SESSION, failover=false, loadBalanced=false)
@SuppressWarnings("serial")
public class TradeMgr
implements Cloneable, Serializable, ITradeMgr
{
/**
* Do not call this method, used only to satisfy the interface
*/
public String registerInterest(String pHostName, RemoteEvent pAnchoredObject, String pEvent) throws RemoteException {
return EventManager.registerInterest(pHostName, pAnchoredObject, this, pEvent);
}
/**
* Do not call this method, used only to satisfy the interface
*/
public void postEvent(String pEventName, Hashtable<String, Object> pParameters) throws RemoteException {
EventManager.postEvent(this, pEventName, pParameters);
}
/**
* Do not call this method, used only to satisfy the interface
*/
public void deregisterInterest(String pHostName, RemoteEvent pAnchoredObject, String pEvent) throws RemoteException {
EventManager.deregisterInterest(pHostName, pAnchoredObject, this, pEvent);
}
// -------------
// Inner classes
// -------------
/**
* This class contains routines to be able to start threads for the methods on this class in a manner
* similar to that allowed by Forte. This includes:<p>
* <ul>
* <li>Arbitrary parameters</li>
* <li>Starting threads as daemon threads</li>
* <li>Naming the thread for easy debugging</li>
* </ul>
*/
public static class AsyncRunner extends AsyncStarter {
/**
* Create an async runner which does not fire completion events nor begin a transaction
*/
public AsyncRunner() {
super();
}
/**
* Create an async runner which can fire completion/exception events
*/
public AsyncRunner(boolean pFireEvents) {
super(pFireEvents);
}
/**
* makeTrade<p>
* <p>
* @param pBuyOrder Type: Order
* @param pSellOrder Type: Order
* @param pMTQuantity Type: int
* @param pMTPrice Type: float
* @return String
*/
@Async(returnEvent="MakeTrade_return", exceptionEvent="MakeTrade_exception")
public Thread makeTrade(final ITradeMgr pTradeMgr, final Order pBuyOrder, final Order pSellOrder, final ParameterHolder_integer pMTQuantity, final ParameterHolder_float pMTPrice) {
return startTask(pTradeMgr, "TradeMgr.MakeTrade",
new AsyncStarter.AsyncInvoker() {
@Override public Object run() {
return pTradeMgr.makeTrade(pBuyOrder, pSellOrder, pMTQuantity, pMTPrice);
}
@Override public String getCompletionEvent() {return cEVENT_MAKE_TRADE_RETURN;}
@Override public String getExceptionEvent() {return cEVENT_MAKE_TRADE_EXCEPTION;}
@Override public void setCompletionParameters(Map<String, Object> params) {
params.put( "pMTQuantity", pMTQuantity );
params.put( "pMTPrice", pMTPrice );
}
});
}
}
// ---------
// Constants
// ---------
// -----------------
// Event definitions
// -----------------
public static final String cEVENT_NEW_ORDER_ADDED = "NewOrderAdded";
public static final String cEVENT_ORDER_PROCESSED = "OrderProcessed";
public static final String cEVENT_MAKE_TRADE_RETURN = "MakeTrade_return";
public static final String cEVENT_MAKE_TRADE_EXCEPTION = "MakeTrade_exception";
// ----------
// Attributes
// ----------
private Array_Of_Order<Order> orderList;
private Sequencer orderSequencer;
// ------------
// Constructors
// ------------
public TradeMgr() {
// Explicitly call the superclass constructor to prevent the implicit call
super();
this.orderList = new Array_Of_Order<Order>();
//self.NextOrderID = 1;
this.orderSequencer = new Sequencer();
}
/**
* A dummy constructor used to instantiate a "blank" object. This is used for anchored objects, Spring beans and their
* superclasses, but should not be expected to create a valid TradeMgr.
*/
public TradeMgr(ServiceObjectContext pContext) {
}
// ----------------------
// Accessors and Mutators
// ----------------------
/**
* Getter for the attribute OrderList
*/
public Array_Of_Order<Order> getOrderList() {
return this.orderList;
}
// -------
// Methods
// -------
// Method GetOrderList() : Framework.Array of StockTradeBusinessClasses.Order skipped because it is replaced by accessor / mutator.
/**
* makeTrade<p>
* <p>
* @param pBuyOrder Type: Order
* @param pSellOrder Type: Order
* @param pMTQuantity Type: int
* @param pMTPrice Type: float
* @return String
*/
@Async(returnEvent="MakeTrade_return", exceptionEvent="MakeTrade_exception")
public String makeTrade(Order pBuyOrder, Order pSellOrder, ParameterHolder_integer pMTQuantity, ParameterHolder_float pMTPrice) {
// TODO [129,Info]: Copy of parameter pBuyOrder has been skipped because this method is on a service class for StockTradeServices.TradeSO
// TODO [129,Info]: Copy of parameter pSellOrder has been skipped because this method is on a service class for StockTradeServices.TradeSO
TransactionMgr.beginTransaction();
try {
CustomerSO_proxy.getInstance().incrementHolding(pBuyOrder.getCustomerName(), pBuyOrder.getStockName(), pBuyOrder.getQuantity(), pBuyOrder.getPrice());
CustomerSO_proxy.getInstance().decrementHolding(pSellOrder.getCustomerName(), pSellOrder.getStockName(), pSellOrder.getQuantity(), pSellOrder.getPrice());
TradeSO_proxy.getInstance().removeOrder(pBuyOrder.getOrderID());
TradeSO_proxy.getInstance().removeOrder(pSellOrder.getOrderID());
pMTPrice.setFloat(pBuyOrder.getPrice());
pMTQuantity.setInt(pBuyOrder.getQuantity());
// Testing for TradeNotMade_exception event
//
// forceBadTrade : GenericException = new;
// forceBadTrade.SetWithParams(severity = SP_ER_ERROR,
// message = 'Forcing TradeNotMade_exception event');
// task.errorMgr.Adderror(error=forceBadTrade);
// raise forceBadTrade;
}
catch (RuntimeException qq_err) {
TransactionMgr.abortWithinTransactionBlock();
Logger.getLogger("task.part.Database").error("Transaction aborted by exception: " + qq_err.getMessage(), qq_err );
throw qq_err;
}
finally {
TransactionMgr.endTransaction();
}
// TODO [1072,Info]: An event object reference has been altered for distributed events to work properly
Hashtable<String,Object> qq_Params = new Hashtable<String,Object>();
qq_Params.put( "pOrder", new ParameterHolder(pBuyOrder) );
EventManager.postEvent( TradeSO_proxy.getInstance(), TradeMgr.cEVENT_ORDER_PROCESSED, qq_Params );
// TODO [1072,Info]: An event object reference has been altered for distributed events to work properly
qq_Params = new Hashtable<String,Object>();
qq_Params.put( "pOrder", new ParameterHolder(pSellOrder) );
EventManager.postEvent( TradeSO_proxy.getInstance(), TradeMgr.cEVENT_ORDER_PROCESSED, qq_Params );
return pBuyOrder.getStockName();
}
/**
* removeOrder<p>
* <p>
* @param pOrderID Type: int
*/
public void removeOrder(int pOrderID) {
if (this.orderList != null) {
for (Order row : this.orderList) {
if (row.getOrderID() == pOrderID) {
// TODO [1037,Major]: If you are deleting a row inside a loop, use the Iterator in place of the array
this.orderList.remove(row);
return;
}
}
}
}
/**
* takeOrder<p>
* pOrderTaken.OrderID = NextOrderID;<br>
* self.NextOrderID = NextOrderID + 1;<br>
* <p>
* @param pOrderTaken Type: Order
*/
public void takeOrder(Order pOrderTaken) {
// TODO [129,Info]: Copy of parameter pOrderTaken has been skipped because this method is on a service class for StockTradeServices.TradeSO
pOrderTaken.setOrderID(this.orderSequencer.qq_getNextNumber());
this.orderList.add(pOrderTaken);
// TODO [1072,Info]: An event object reference has been altered for distributed events to work properly
Hashtable<String,Object> qq_Params = new Hashtable<String,Object>();
qq_Params.put( "pNewOrder", new ParameterHolder(pOrderTaken) );
EventManager.postEvent( TradeSO_proxy.getInstance(), TradeMgr.cEVENT_NEW_ORDER_ADDED, qq_Params );
}
} // end class TradeMgr
// c Pass 2 Conversion Time: 203 milliseconds