package Juno2;

import java.io.*;
import java.util.HashMap;
import java.util.*;
import javax.servlet.http.*;

public class CmdPositionListImpl {

	public PositionListCVO doPositionList(String userId, String userPassword, String acctId)
	{
		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
        }

		try {
			plcvo.addPositionList(cdao.getPositionList(plcvo.getUserId(), acctId));
		} catch (Exception e) {
        	System.out.println("PositionListCVO addPositionList(" + plcvo.getUserId() + ", " + acctId + ") threw exception: " + e.getMessage());
		}
         //// now iterate cdao and grab symbols for pricing
        ArrayList symbolList = new ArrayList();
        HashMap pl = plcvo.getPositionList();
		Iterator poskeys = pl.keySet().iterator();
		while (poskeys.hasNext()) {
			String symbol = (String) poskeys.next();
			symbolList.add(symbol);
		}

		CmdMarketDAO mktdao = new CmdMarketDAO();
		
		plcvo.addEquityList(mktdao.getEquityPrice(symbolList));

		return(plcvo);
 	}
}




