Package org.apache.stonehenge.stocktrader.services

Source Code of org.apache.stonehenge.stocktrader.services.TraderServiceManager

/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.stonehenge.stocktrader.services;

import java.math.BigDecimal;
import java.util.Calendar;

import java.util.List;               
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.stonehenge.stocktrader.CustomAccountBean;
import org.apache.stonehenge.stocktrader.CustomAccountProfileBean;
import org.apache.stonehenge.stocktrader.CustomHoldingBean;
import org.apache.stonehenge.stocktrader.CustomMarketSummaryBean;
import org.apache.stonehenge.stocktrader.CustomOrderBean;
import org.apache.stonehenge.stocktrader.CustomQuoteBean;
import org.apache.stonehenge.stocktrader.dal.CustomerDAO;
import org.apache.stonehenge.stocktrader.dal.DAOException;
import org.apache.stonehenge.stocktrader.dal.DAOFactory;
import org.apache.stonehenge.stocktrader.dal.MarketSummaryDAO;
import org.apache.stonehenge.stocktrader.dal.OrderDAO;
import org.apache.stonehenge.stocktrader.util.StockTraderUtility;

public class TraderServiceManager {

  private static final Log logger = LogFactory.getLog(TraderServiceManager.class);

  private DAOFactory factory = null;

  public TraderServiceManager() {
    factory = DAOFactory.getFacotry();
  }

  public CustomAccountBean login(String userId, String password) throws DAOException {
    CustomerDAO customerDAO = factory.getCustomerDAO();
    return customerDAO.login(userId, password);
  }

  public void logout(String userId) throws DAOException {
    CustomerDAO customerDAO = factory.getCustomerDAO();
    customerDAO.logoutUser(userId);
  }

  public CustomAccountBean getAccountData(String userId) throws DAOException {
    CustomerDAO customerDAO = factory.getCustomerDAO();
    return customerDAO.getCustomerByUserId(userId);
  }

  public CustomAccountProfileBean getAccountProfileData(String userId) throws DAOException {
    CustomerDAO customerDAO = factory.getCustomerDAO();
    return customerDAO.getAccountProfileData(userId);
  }

  public List<CustomOrderBean> getOrders(String userId) throws DAOException {
    CustomerDAO customerDAO = factory.getCustomerDAO();
    return customerDAO.getOrders(userId, false, StockTraderUtility.MAX_QUERY_TOP_ORDERS, StockTraderUtility.MAX_QUERY_ORDERS);
  }

  public List<CustomOrderBean> getTopOrders(String userId) throws DAOException {
    CustomerDAO customerDAO = factory.getCustomerDAO();
    return customerDAO.getOrders(userId, true, StockTraderUtility.MAX_QUERY_TOP_ORDERS, StockTraderUtility.MAX_QUERY_ORDERS);
  }

  public List<CustomOrderBean> getClosedOrders(String userId) throws DAOException {
    CustomerDAO customerDAO = factory.getCustomerDAO();
    return customerDAO.getClosedOrders(userId);
  }

  public CustomAccountBean register(String userId, String password,
      String fullName, String address, String email, String creditcard,
      BigDecimal openBalance) throws DAOException {
    throw new UnsupportedOperationException();
  }

  public CustomAccountBean addNewRegisteredUser(String userId,
      String password, String fullName, String address, String email,
      String creditcard, BigDecimal openBalance) throws DAOException {
    CustomerDAO customerDAO = factory.getCustomerDAO();
    CustomAccountProfileBean customerProfile = new CustomAccountProfileBean(
        userId, password, fullName, address, email, creditcard);
    customerDAO.insertAccountProfile(customerProfile);
    CustomAccountBean customer = new CustomAccountBean(0, userId, Calendar
        .getInstance(), openBalance, 0, openBalance, Calendar
        .getInstance(), 0);
    return customer;
  }

  public CustomAccountProfileBean updateAccountProfile(
      CustomAccountProfileBean customAccountProfile) throws DAOException {
    CustomerDAO customerDAO = factory.getCustomerDAO();
    return customerDAO.update(customAccountProfile);
  }

  public CustomMarketSummaryBean getMarketSummary() throws DAOException {
    MarketSummaryDAO marketSummaryDAO = factory.getMarketSummaryDAO();
    return marketSummaryDAO.getCustomMarketSummary();
  }

  public CustomQuoteBean getQuote(String symbol) throws DAOException {
    MarketSummaryDAO marketSummaryDAO = factory.getMarketSummaryDAO();
    return marketSummaryDAO.getQuote(symbol);
  }

  public CustomOrderBean buy(String userID, String symbol, double quantity)
      throws DAOException {
    return placeOrder(StockTraderUtility.ORDER_TYPE_BUY, userID, 0, symbol,
        quantity);
  }

  public CustomOrderBean sell(String userID, int holdingID,
      int orderProcessingMode) throws DAOException {
    return placeOrder(StockTraderUtility.ORDER_TYPE_SELL, userID,
        holdingID, null, 0);
  }

  public CustomOrderBean sellEnhanced(String userID, int holdingID,
      double quantity) throws DAOException {
    return placeOrder(StockTraderUtility.ORDER_TYPE_SELL_ENHANCED, userID,
        holdingID, null, quantity);
  }

  public CustomOrderBean placeOrder(String orderType, String userID,
      int holdingID, String symbol, double quantity) throws DAOException {
    OrderDAO orderDAO = factory.getOrderDAO();
    CustomOrderBean order = null;
    CustomHoldingBean holding = new CustomHoldingBean();
    try {

      orderDAO.beginTransaction();
      order = createOrder(orderType, userID, holdingID, symbol, quantity,
          holding);

      TradeOrderServiceClient asynClient = TradeOrderServiceClient.getInstance();
      asynClient.SubmitOrderTrasactedQueue(order);
      orderDAO.commitTransaction();
      return order;
    } catch (Exception e) {
      try {
        orderDAO.rollbackTransaction();
      } catch (DAOException e2) {
        throw e2;
      }
      throw new RuntimeException(e);
    }
  }

  private CustomOrderBean createOrder(String orderType, String userID,
      int holdingID, String symbol, double quantity,
      CustomHoldingBean holding) throws DAOException {
    CustomOrderBean order = null;
    OrderDAO orderDAO = factory.getOrderDAO();

    if (StockTraderUtility.ORDER_TYPE_SELL.equals(orderType)) {
      // CHECKME holding is the argument
      holding = orderDAO.getHolding(holdingID);
      if (holding == null) {
        throw new DAOException("No holding entry found for HoldingID<"
            + holdingID + ">");
      }
      order = orderDAO.createOrder(userID, holding.getQuoteID(),
          StockTraderUtility.ORDER_TYPE_SELL, holding.getQuantity(),
          holdingID);

    } else if (StockTraderUtility.ORDER_TYPE_SELL_ENHANCED
        .equals(orderType)) {
      holding = orderDAO.getHolding(holdingID);
      if (holding == null) {
        throw new DAOException("No holding entry found for HoldingID<"
            + holdingID + ">");
      }
      if (quantity > holding.getQuantity()) {
        order = orderDAO.createOrder(userID, holding.getQuoteID(),
            StockTraderUtility.ORDER_TYPE_SELL, holding
                .getQuantity(), holdingID);
      } else {
        order = orderDAO
            .createOrder(userID, holding.getQuoteID(),
                StockTraderUtility.ORDER_TYPE_SELL, quantity,
                holdingID);
      }
    } else if (StockTraderUtility.ORDER_TYPE_BUY.equals(orderType)) {
      order = orderDAO.createOrder(userID, symbol,
          StockTraderUtility.ORDER_TYPE_BUY, quantity, -1);
    } else {
      throw new IllegalArgumentException("Invalid orderType<" + orderType
          + ">");
    }
    return order;
  }

  public CustomHoldingBean getHolding(String userID, int holdingID)
      throws DAOException {
    CustomerDAO customerDAO = factory.getCustomerDAO();
    return customerDAO.getHolding(userID, holdingID);
  }

  public List<CustomHoldingBean> getHoldings(String userID)
      throws DAOException {
    CustomerDAO customerDAO = factory.getCustomerDAO();
    return customerDAO.getHoldings(userID);
  }

}
TOP

Related Classes of org.apache.stonehenge.stocktrader.services.TraderServiceManager

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.