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

<%-- Get the home user --%>

<%
    EJBHomeFactory factory = EJBHomeFactory.getInstance();
%>

<p> Connecting to UserLocalHome </p>
<%
    UserLocalHome userHome = (UserLocalHome) factory.getLocalHome(EJBHomeFactory.USER);
    AddressLocalHome addressHome = (AddressLocalHome) factory.getLocalHome (EJBHomeFactory.ADDRESS);
%>

<p> Adding  new users to the database</p>

<!-- TO FIX: Need to figure out the proper way to handle transactions here.
If we fail to add a property or a picture, we need to fail on the whole add
user. The easiest way to do this is if the user add command is on method
then an failure inside the method will automatically rollback everything else
However, then the method signiture would become obscenly long.
Any way to do this with getting and using UserTransaction? -->


    <!-- Adding a new User -->

<%
    AddressLocal address=null;
    //Collection friends=null;
    //UserLocal user=null;

    try {
	address=null;
	address= addressHome.create("Canada","Toronto","F4R 1Y3", "88 Erskine Road", "Apartment 2303", "Ontario");
    }
    catch (Exception e) {
   	 out.println ("Cannot create Address: "+ e.getMessage() );
    }

    try {
    	UserLocal user= userHome.create("mwringe","customer","Wringe", "Matt", "mwringe@test.com", "555-482-2661","qwerty");

	user.setAddress (address);

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

%>

    <!-- Adding a new User -->

<%
    try {
	address=null;
	address= addressHome.create("Canada","Toronto","M4Y 6K7", "Yonge Street","", "Ontario");
    }
    catch (Exception e) {
   	 out.println ("Cannot create Address: " + e.getMessage() );
    }



    try {
    	UserLocal user= userHome.create("vivek","admin","Lakshmanan", "Vivek", "vivekl@test.com", "555-482-4361","vivek");
	user.setAddress (address);
   }
   catch (Exception e) {
   	out.println ("Cannot create User: " + e.getMessage() );
   }
%>

<%
    try {
	address=null;
	address= addressHome.create("Canada","Toronto","M4Y 4F5", "Eglington Ave", "Suite 300","Ontario");
    }
    catch (Exception e) {
   	 out.println ("Cannot create Address: "+ e.getMessage() );
    }

    try {
    	UserLocal user= userHome.create("aahmed","customer","Ahmed", "Aizaz", "aahmed@test.com", "555-485-2411","password");
	user.setAddress (address);
   }
   catch (Exception e) {
   	out.println ("Cannot create User: " + e.getMessage() );
   }
%>


<%


    try {
	address=null;
	address= addressHome.create("Canada","Toronto","K7R 1E7", "AppServer Street", "blah","Ontario");
    }
    catch (Exception e) {
   	 out.println ("Cannot create Address: "+ e.getMessage() );
    }

    try {
    	UserLocal user= userHome.create("jonas","admin","Server", "Jonas A.", "jonas@test.com", "555-432-1234","jonas");
	user.setAddress (address);

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

<%
    try {
	address=null;
	address= addressHome.create("Canada","Ottawa","K7R 1E7", " 1568 Main Rd", "Suite 2", "Ontario");
    }
    catch (Exception e) {
   	 out.println ("Cannot create Address: "+ e.getMessage() );
    }

    try {
    	UserLocal user= userHome.create("SamOak","customer","Oak", "Sam", "oaktree@test.com", "555-546-8354","Oaktree");
	user.setAddress (address);
   }
   catch (Exception e) {
   	out.println ("Cannot create User: " + e.getMessage() );
   }
%>

<%
    try {
	address=null;
	address= addressHome.create("Canada","Halifax","E5G 3R4", "80 King St", "","Nova Scotia");
    }
    catch (Exception e) {
   	 out.println ("Cannot create Address: "+ e.getMessage() );
    }

    try {
    	UserLocal user= userHome.create("bobbyv","customer","Vight", "Bob", "bob47@test.com", "555-345-2344","bob47");
	user.setAddress (address);
   }
   catch (Exception e) {
   	out.println ("Cannot create User: " + e.getMessage() );
   }
%>


<%
    try {
	address=null;
	address= addressHome.create("NA","NA","NA", "NA", "NA","NA");
    }
    catch (Exception e) {
   	 out.println ("Cannot create Address: "+ e.getMessage() );
    }

    try {
    	UserLocal user= userHome.create("admin","admin","NA", "Administrator", "admin@test.com", "555-124-3579","admin");
	user.setAddress (address);
   }
   catch (Exception e) {
   	out.println ("Cannot create User: " + e.getMessage() );
   }
%>

<%
    try {
	address=null;
	address= addressHome.create("Canada","Pitch","G6R 6R6", "33 Church St", "","Ontario");
    }
    catch (Exception e) {
   	 out.println ("Cannot create Address: "+ e.getMessage() );
    }

    try {
    	UserLocal user= userHome.create("lucy666","customer","Fur", "Lucy E.", "lucy666@test.com", "555-345-1134","meow666");
	user.setAddress (address);
   }
   catch (Exception e) {
   	out.println ("Cannot create User: " + e.getMessage() );
   }
%>

<%
    try {
	address=null;
	address= addressHome.create("Canada","Kingston","K4T 4G7", "45 Waterfront Street", "","Ontario");
    }
    catch (Exception e) {
   	 out.println ("Cannot create Address: "+ e.getMessage() );
    }

    try {
    	UserLocal user= userHome.create("troll","customer","Trolley", "Susan", "strolley@test.com", "555-243-1234","troll9");
	user.setAddress (address);
   }
   catch (Exception e) {
   	out.println ("Cannot create User: " + e.getMessage() );
   }
%>

<%
    try {
	address=null;
	address= addressHome.create("Canada","Calgary","K9Y 2D4", "21 Main Avenue", "Apartment 56","Alberta");
    }
    catch (Exception e) {
   	 out.println ("Cannot create Address: "+ e.getMessage() );
    }

    try {
    	UserLocal user= userHome.create("mfrost","customer","Frost", "Michelle", "michelle@test.com", "555-326-6486","elley");
	user.setAddress (address);
   }
   catch (Exception e) {
   	out.println ("Cannot create User: " + e.getMessage() );
   }
%>

<%
    try {
	address=null;
	address= addressHome.create("Canada","Whitehorse","E4C 3B6", "14 Polarbear Lane", "Apartment B","Yukon Territory");
    }
    catch (Exception e) {
   	 out.println ("Cannot create Address: "+ e.getMessage() );
    }

    try {
    	UserLocal user= userHome.create("icyMeg","customer","Ice", "Megan", "icy47@test.com", "555-748-3457","74yci");
	user.setAddress (address);
   }
   catch (Exception e) {
   	out.println ("Cannot create User: " + e.getMessage() );
   }
%>

<%
    try {
	address=null;
	address= addressHome.create("Canada","Quebec City","R5T 5Z8", "14 Francois Ave", "","Quebec");
    }
    catch (Exception e) {
   	 out.println ("Cannot create Address: "+ e.getMessage() );
    }

    try {
    	UserLocal user= userHome.create("Sammy","customer","Sampson", "Marg", "sampson@test.com", "555-438-237","MarG14");
	user.setAddress (address);
   }
   catch (Exception e) {
   	out.println ("Cannot create User: " + e.getMessage() );
   }
%>

<%
    try {
	address=null;
	address= addressHome.create("Canada","Toronto","A1A 1A1", "1 One Street", "","Ontario");
    }
    catch (Exception e) {
   	 out.println ("Cannot create Address: "+ e.getMessage() );
    }

    try {
    	UserLocal user= userHome.create("user1","customer","User1", "User1", "user1@test.com", "555-111-1111","user1");
	user.setAddress (address);
   }
   catch (Exception e) {
   	out.println ("Cannot create User: " + e.getMessage() );
   }
%>

<%
    try {
	address=null;
	address= addressHome.create("Canada","Toronto","B2B 2B2", "2 Two Street", "","Ontario");
    }
    catch (Exception e) {
   	 out.println ("Cannot create Address: "+ e.getMessage() );
    }

    try {
    	UserLocal user= userHome.create("user2","customer","User2", "User2", "user2@test.com", "555-222-2222","user2");
	user.setAddress (address);
   }
   catch (Exception e) {
   	out.println ("Cannot create User: " + e.getMessage() );
   }
%>

<%
    try {
	address=null;
	address= addressHome.create("Canada","Toronto","C3C 3C3", "3 Three Street", "","Ontario");
    }
    catch (Exception e) {
   	 out.println ("Cannot create Address: "+ e.getMessage() );
    }

    try {
    	UserLocal user= userHome.create("user3","customer","User3", "User3", "user3@test.com", "555-333-3333","user3");
	user.setAddress (address);
   }
   catch (Exception e) {
   	out.println ("Cannot create User: " + e.getMessage() );
   }
%>

<%
    try {
	address=null;
	address= addressHome.create("Canada","Toronto","D4D 4D4", "4 Four Street", "","Ontario");
    }
    catch (Exception e) {
   	 out.println ("Cannot create Address: "+ e.getMessage() );
    }

    try {
    	UserLocal user= userHome.create("user4","customer","User4", "User4", "user4@test.com", "555-444-4444","user4");
	user.setAddress (address);
   }
   catch (Exception e) {
   	out.println ("Cannot create User: " + e.getMessage() );
   }
%>


<p> Listing all the users in the database </p>
<%
    try {
	Collection allUsers = userHome.findAll();
        Iterator it = allUsers.iterator();
        while ( it.hasNext() ) {
           UserLocal user = (UserLocal) it.next();
	   request.setAttribute ( "userToDisplay", user ) ;
%> <jsp:include page="../views/UserDetailedView.jsp" /> <%
        }
    }  catch ( Exception e ) {
        out.println ( "Could not list all users: " + e.getMessage() );
    }

%>
