package net.helipilot50.stocktrade.server;
import java.io.IOException;
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.aerospike.sqlimport.NoCaseFileStream;
import net.helipilot50.aerospike.sqlimport.ToolSQLLexer;
import net.helipilot50.aerospike.sqlimport.ToolSQLParser;
import net.helipilot50.aerospike.sqlimport.ToolSQLParser.sql_file_return;
import net.helipilot50.stocktrade.client.CustomerService;
import net.helipilot50.stocktrade.model.Customer;
import net.helipilot50.stocktrade.model.Holding;
import net.helipilot50.stocktrade.model.Stock;
import net.helipilot50.stocktrade.shared.NoSuchCustomerException;
import org.antlr.runtime.CharStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RecognitionException;
import org.antlr.runtime.TokenStream;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
@SuppressWarnings("serial")
public class CustomerServiceImpl extends RemoteServiceServlet implements
CustomerService {
@Override
public void decrementHolding(String pCustomerName, String pStockName,
int pQuantity, float pPrice) {
DBClient.getInstance().add(DBClient.NAME_SPACE, pCustomerName + "Holding", pStockName, "Quantity", -pQuantity, null, null);
}
@Override
public Customer getCustomer(String pCustomerName) throws NoSuchCustomerException {
ClResult result = DBClient.getInstance().getAll(DBClient.NAME_SPACE, "Customer", pCustomerName, null);
if (result.resultCode != ClResultCode.OK){
throw new NoSuchCustomerException("cannot find customer " + pCustomerName);
}
Customer customer = new Customer();
customer.setCustomerName((String) result.results.get("CustomerName"));
customer.setAddress((String) result.results.get("Address"));
customer.setCashBalance(Float.parseFloat((String) result.results.get("CashBalance")));
customer.setPhoneNumber((String) result.results.get("PhoneNumber"));
final List<Holding> holdings = new ArrayList<Holding>();
DBClient.getInstance().scanAllNodes(DBClient.NAME_SPACE, pCustomerName + "Holding", new ScanCallback() {
@Override
public void scanCallback(String namespace, String set, byte[] digest,
Map<String, Object> bins, int generation, int expirationDate,
Object userData) {
Holding holding = new Holding();
holding.set("customerName", bins.get("CustomerName"));
holding.set("stockName", bins.get("StockName"));
holding.set("quantity", bins.get("Quantity"));
holding.set("price", bins.get("Price"));
holdings.add(holding);
}
}, null);
customer.setHoldingList(holdings);
return customer;
}
@Override
public Holding getHolding(Customer pCustomer, String pGetHoldStockName) {
// ClResult result = DBClient.getInstance().getAll(DBClient.NAME_SPACE, "Holding", pCustomer, null);
// if (result.resultCode == ClResultCode.KEY_NOT_FOUND_ERROR)
// System.out.println("couldnt find " + pCustomer +"'s holdings" );
return null;
}
@Override
public List<Stock> getStocks() {
final List<Stock> stocks = new ArrayList<Stock>();
Map<String, ClResultCode> rvs = DBClient.getInstance().scanAllNodes(DBClient.NAME_SPACE, "Stock", new ScanCallback() {
public void scanCallback(String namespace, String set, byte[] digest,
Map<String, Object> bins, int generation, int expirationDate,
Object userData) {
Stock stock = new Stock();
stock.setStockName((String) bins.get("StockName"));
stock.setCompanyName((String) bins.get("Company"));
stocks.add(stock);
}
}, null);
return stocks;
}
@Override
public void incrementHolding(String pCustomerName, String pStockName,
int pQuantity, float pPrice) {
DBClient.getInstance().add(DBClient.NAME_SPACE, pCustomerName + "Holding", pStockName, "Quantity", pQuantity, null, null);
}
@Override
public void reloadData() {
try {
CharStream stream = new NoCaseFileStream("sqldata/Oracledb.sql");
ToolSQLLexer lexer = new ToolSQLLexer(stream);
TokenStream tokenStream = new CommonTokenStream(lexer);
ToolSQLParser parser = new ToolSQLParser(tokenStream);
parser.connectCitrusleaf(DBClient.getInstance());
sql_file_return result = parser.sql_file();
System.out.printf("SQL syntax errors: %d", parser.getNumberOfSyntaxErrors());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RecognitionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}