package juno2Beans.sellPositions;

import Juno2.PositionListCVO;
import Juno2.CmdDAO;
import Juno2.CmdDAOFactory;
import Juno2.*;
import java.io.*;
import java.util.HashMap;
import java.util.*;

/**
 * Bean implementation class for Enterprise Bean: CmdSellPosition
 */
public class CmdSellPositionBean implements javax.ejb.SessionBean {
	private javax.ejb.SessionContext mySessionCtx;
	/**
	 * getSessionContext
	 */
	public javax.ejb.SessionContext getSessionContext() {
		return mySessionCtx;
	}
	/**
	 * setSessionContext
	 */
	public void setSessionContext(javax.ejb.SessionContext ctx) {
		mySessionCtx = ctx;
	}
	/**
	 * ejbActivate
	 */
	public void ejbActivate() {
	}
	/**
	 * ejbCreate
	 */
	public void ejbCreate() throws javax.ejb.CreateException {
	}
	/**
	 * ejbPassivate
	 */
	public void ejbPassivate() {
	}
	/**
	 * ejbRemove
	 */
	public void ejbRemove() {
	}
	
	public PositionListCVO doSellPosition(String userId, String userPassword, String acctId, String symbol) throws java.lang.Exception
	{
		PositionListCVO plcvo = new PositionListCVO();
		
		CmdDAOFactory daoFactory = CmdDAOFactory.getInstance();
        CmdDAO cdao = daoFactory.getDAO();
        try{
        	
			plcvo.setLoginVO(cdao.getLogin(userId, userPassword));
			
        } catch (Exception e) {
        	System.out.println("PositionListCVO setLoginVO(" + userId + ", " + userPassword + ") threw exception: " + e.getMessage());
        }
		
		if(plcvo.getLoginState()) {
			if(!userId.equals(plcvo.getUserId()))
                System.out.println("Session (" + userId +
                	") and request (" + plcvo.getUserId() +
                    ") user out of sync!");

			//// do same test for password
            //// on error invalidate session throw CmsException
            //// and redirect to login
        }

		//// no real validation

		CmdMarketDAO mktdao = new CmdMarketDAO();
        EquityVO evo = mktdao.getEquityPrice(symbol);
        System.out.println("");
       		System.out.println("*** About to invoke sellPosition from BEAN ***");		
   		try {
       		System.out.println("****YASH check 1 ****");		
        	if(!cdao.sellPosition(userId, acctId, evo)){
        		       		System.out.println("****YASH check 2 ****");
                throw new Exception();
        	}
    	} catch (Exception e){
           System.out.println("Fatal error sellPosition()" + userId + " , " + acctId + ", " + symbol);
           System.out.println("sellPosition(): " + e.getMessage());
    	}
       		System.out.println("****YASH check 3 ****");
		///// Sell complete - now build pos list
			
		try {
			       		System.out.println("****YASH check 4 ****");
			plcvo.addPositionList(cdao.getPositionList(plcvo.getUserId(), acctId));
       		System.out.println("****YASH check 5 ****");
		} catch (Exception e) {
        	System.out.println("CmdSellPositionImpl addPositionList(" + plcvo.getUserId() + ", " + acctId + ") threw exception: " + e.getMessage());
		}
         //// now iterate cdao and grab symbols for pricing
        	System.out.println("****YASH check 5 ****");
        ArrayList symbolList = new ArrayList();
        HashMap pl = plcvo.getPositionList();
		Iterator poskeys = pl.keySet().iterator();
		while (poskeys.hasNext()) {
			String listSymbol = (String) poskeys.next();
			symbolList.add(listSymbol);
		}
			System.out.println("****YASH check 6 ****");
		plcvo.addEquityList(mktdao.getEquityPrice(symbolList));
	System.out.println("****YASH check 6 ****");
	return(plcvo);
	
 	}
}
