package net.helipilot50.stocktrade.server;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import net.citrusleaf.CitrusleafClient.ClResult;
import net.citrusleaf.CitrusleafClient.ClResultCode;
import net.citrusleaf.CitrusleafClient.ScanCallback;
import net.helipilot50.stocktrade.client.TradeService;
import net.helipilot50.stocktrade.model.Order;
import com.google.gwt.dev.util.collect.HashMap;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
@SuppressWarnings("serial")
public class TradeServiceImpl extends RemoteServiceServlet implements
TradeService {
public TradeServiceImpl() {
super();
//createDummyTrades();
}
public TradeServiceImpl(Object delegate) {
super(delegate);
//createDummyTrades();
}
@Override
public List<Order> getOrderList() {
final List<Order> orders = new ArrayList<Order>();
Map<String, ClResultCode> rv = DBClient.getInstance().scanAllNodes(DBClient.NAME_SPACE, "Order", new ScanCallback() {
public void scanCallback(String namespace, String set, byte[] digest,
Map<String, Object> bins, int generation, int expirationDate,
Object userData) {
Order order = new Order();
order.set("orderID", bins.get("OrderID"));
order.set("stockName", bins.get("StockName"));
order.set("customerName", bins.get("CustomerName"));
order.setPrice(((Double)bins.get("Price")).floatValue());
order.setQuantity(((Double) bins.get("Quantity")).intValue());
order.set("type", bins.get("type"));
orders.add(order);
}
}, null);
return orders;
}
@Override
public String makeTrade(Order pBuyOrder, Order pSellOrder, int pMTQuantity,
double pMTPrice) {
CustomerServiceImpl customerSO = new CustomerServiceImpl(); //TODO this is a kludge
customerSO.incrementHolding(pBuyOrder.getCustomerName(), pBuyOrder.getStockName(), pBuyOrder.getQuantity(), pBuyOrder.getPrice());
customerSO.decrementHolding(pSellOrder.getCustomerName(), pSellOrder.getStockName(), pSellOrder.getQuantity(), pSellOrder.getPrice());
removeOrder(pBuyOrder.getOrderID());
removeOrder(pSellOrder.getOrderID());
// pMTPrice.setFloat(pBuyOrder.getPrice());
// pMTQuantity.setInt(pBuyOrder.getQuantity());
// TODO replace this code with a channel
// 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 );
// 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();
}
@Override
public void removeOrder(int pOrderID) {
DBClient.getInstance().delete(DBClient.NAME_SPACE, "Order", Integer.toString(pOrderID), null, null);
}
@Override
public void takeOrder(Order pOrderTaken) {
Map<String, Object> bins = new HashMap<String, Object>();
bins.put("OrderID", pOrderTaken.get("orderID"));
bins.put("StockName", pOrderTaken.get("stockName"));
bins.put("CustomerName", pOrderTaken.get("customerName"));
bins.put("Price", pOrderTaken.get("price"));
bins.put("Quantity", pOrderTaken.get("quantity"));
bins.put("type", pOrderTaken.get("type"));
DBClient.getInstance().set(DBClient.NAME_SPACE, "Order", Integer.toString(pOrderTaken.getOrderID()), bins, null, null);
}
@Override
public int nextOrderID() {
//Map<String, Object> bins = new HashMap<String, Object>();
//bins.put("nextID", 1);
//ClResult result = DBClient.getInstance().addAndGet(DBClient.NAME_SPACE, "IDtable", "Order", bins, null, null);
ClResultCode resultCode = DBClient.getInstance().add(DBClient.NAME_SPACE, "IDtable", "Order", "nextID", 1, null, null);
if (resultCode != ClResultCode.OK)
return -2;
ClResult result = DBClient.getInstance().get(DBClient.NAME_SPACE, "IDtable", "Order", "nextID", null);
if (result.resultCode == ClResultCode.OK) {
int nextID = (Integer) result.result;
return nextID;
} else {
return -1;
}
}
@Override
public String getDBHost() {
return DBClient.getHostString();
}
@Override
public void setDBHost(String hostString) {
DBClient.setHostString(hostString);
}
void createDummyTrades(){
Order order = new Order();
order.setOrderID(1);
order.setCustomerName("Bill");
order.setPrice(200);
order.setStockName("ORCL");
order.setType("Buy");
order.setQuantity(4500);
order.setPrice(25);
takeOrder(order);
order = new Order();
order.setOrderID(12);
order.setCustomerName("Pat");
order.setPrice(200);
order.setStockName("ORCL");
order.setType("Sell");
order.setQuantity(4500);
order.setPrice(25);
takeOrder(order);
}
}