<%@ page contentType="text/html"
	 import="olstore.entity.*"
	 import="olstore.session.*"
	 import="olstore.framework.EJBHomeFactory"
	 import="java.util.Date"
          %>

<%-- Get the Order item --%>
<%
    EJBHomeFactory factory = EJBHomeFactory.getInstance();
%>

<h2> Populating Orders</h2>
<p> Connecting to OrderLocalHome </p>
<%
    ItemLocalHome itemHome = (ItemLocalHome)factory.getLocalHome (EJBHomeFactory.ITEM);
    OrderLocalHome orderHome = (OrderLocalHome)factory.getLocalHome (EJBHomeFactory.ORDER);
    UserLocalHome userHome = (UserLocalHome)factory.getLocalHome (EJBHomeFactory.USER);
    ShoppingCartLocalHome shoppingCartHome =
    		(ShoppingCartLocalHome)factory.getLocalHome (EJBHomeFactory.SHOPPINGCART);

%>

<%
    ItemLocal item = null;
    UserLocal user = null;
    Integer amount1=new Integer (1);
    Integer amount2=new Integer (2);
    Integer amount3=new Integer (3);
    String DATE_FORMAT = "yyyy-MM-dd";
    Date date = new Date();
    java.text.SimpleDateFormat sdf =
          new java.text.SimpleDateFormat(DATE_FORMAT);
    String strdate = sdf.format(date);
    try {
	item=(ItemLocal)((itemHome.findByName ("Value Laptop")).iterator()).next();
		try {
			user=userHome.findByUsername ("mwringe");
			ShoppingCartLocal shoppingCart = shoppingCartHome.create(user);
			shoppingCart.updateQuantity(item.getItemId(), amount1);
			shoppingCart.createOrders();


		}
		catch (Exception e){
			out.println ("Cannot get User : " + e.getMessage());
		}



	item=(ItemLocal)((itemHome.findByName ("Gamer's laptop")).iterator()).next();
		try {
			user=userHome.findByUsername ("mwringe");
			ShoppingCartLocal shoppingCart = shoppingCartHome.create(user);
			shoppingCart.updateQuantity(item.getItemId(), amount1);
			shoppingCart.createOrders();

		}
		catch (Exception e){
			out.println ("Cannot get User : " + e.getMessage());
		}


	item=(ItemLocal)((itemHome.findByName ("Mobile Professional")).iterator()).next();
		try {
			user=userHome.findByUsername ("aahmed");
			ShoppingCartLocal shoppingCart = shoppingCartHome.create(user);
			shoppingCart.updateQuantity(item.getItemId(), amount1);
			shoppingCart.createOrders();

		}
		catch (Exception e){
			out.println ("Cannot get User : " + e.getMessage());
		}



	item=(ItemLocal)((itemHome.findByName ("Linux Compatible")).iterator()).next();
		try {
			user=userHome.findByUsername ("aahmed");
			ShoppingCartLocal shoppingCart = shoppingCartHome.create(user);
			shoppingCart.updateQuantity(item.getItemId(), amount1);
			shoppingCart.createOrders();

		}
		catch (Exception e){
			out.println ("Cannot get User : " + e.getMessage());
		}


	item=(ItemLocal)((itemHome.findByName ("Name Brand")).iterator()).next();
		try {
			user=userHome.findByUsername ("aahmed");
			ShoppingCartLocal shoppingCart = shoppingCartHome.create(user);
			shoppingCart.updateQuantity(item.getItemId(), amount1);
			shoppingCart.createOrders();
		}
		catch (Exception e){
			out.println ("Cannot get User : " + e.getMessage());
		}

	item=(ItemLocal)((itemHome.findByName ("Little Flower")).iterator()).next();
		try {
			user=userHome.findByUsername ("vivek");
			ShoppingCartLocal shoppingCart = shoppingCartHome.create(user);
			shoppingCart.updateQuantity(item.getItemId(), amount1);
			shoppingCart.createOrders();

		}
		catch (Exception e){
			out.println ("Cannot get User : " + e.getMessage());
		}


	item=(ItemLocal)((itemHome.findByName ("Palm Trees")).iterator()).next();
		try {
			user=userHome.findByUsername ("user2");
			ShoppingCartLocal shoppingCart = shoppingCartHome.create(user);
			shoppingCart.updateQuantity(item.getItemId(), amount1);
			shoppingCart.createOrders();

		}
		catch (Exception e){
			out.println ("Cannot get User : " + e.getMessage());
		}


	item=(ItemLocal)((itemHome.findByName ("Paris")).iterator()).next();
		try {
			user=userHome.findByUsername ("vivek");
			ShoppingCartLocal shoppingCart = shoppingCartHome.create(user);
			shoppingCart.updateQuantity(item.getItemId(), amount1);
			shoppingCart.createOrders();

		}
		catch (Exception e){
			out.println ("Cannot get User : " + e.getMessage());
		}


	item=(ItemLocal)((itemHome.findByName ("Catuber AS14")).iterator()).next();
		try {
			user=userHome.findByUsername ("user1");
			ShoppingCartLocal shoppingCart = shoppingCartHome.create(user);
			shoppingCart.updateQuantity(item.getItemId(), amount1);
			shoppingCart.createOrders();

		}
		catch (Exception e){
			out.println ("Cannot get User : " + e.getMessage());
		}


	item=(ItemLocal)((itemHome.findByName ("Catuber AS21")).iterator()).next();
		try {
			user=userHome.findByUsername ("vivek");
			ShoppingCartLocal shoppingCart = shoppingCartHome.create(user);
			shoppingCart.updateQuantity(item.getItemId(), amount1);
			shoppingCart.createOrders();

		}
		catch (Exception e){
			out.println ("Cannot get User : " + e.getMessage());
		}

	item=(ItemLocal)((itemHome.findByName ("Baskview BK19C")).iterator()).next();
		try {
			user=userHome.findByUsername ("user4");
			ShoppingCartLocal shoppingCart = shoppingCartHome.create(user);
			shoppingCart.updateQuantity(item.getItemId(), amount1);
			shoppingCart.createOrders();


		}
		catch (Exception e){
			out.println ("Cannot get User : " + e.getMessage());

		}

	item=(ItemLocal)((itemHome.findByName ("Neupton TD17")).iterator()).next();
		try {
			user=userHome.findByUsername ("user3");
			ShoppingCartLocal shoppingCart = shoppingCartHome.create(user);
			shoppingCart.updateQuantity(item.getItemId(), amount1);
			shoppingCart.createOrders();

		}
		catch (Exception e){
			out.println ("Cannot get User : " + e.getMessage());
		}

%>
	<p>  All orders placed!!!! </p>
<%
    }

    catch (Exception e){
	out.println("Cannot place orders : "+ e);
%>
	<p> Some orders failed to be created!!! </p>

<%
    }

%>
