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

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

<h2> Populating Friends</h2>
<%
    UserLocalHome userHome = (UserLocalHome)factory.getLocalHome (EJBHomeFactory.USER);
    FriendLocalHome friendHome = (FriendLocalHome)factory.getLocalHome (EJBHomeFactory.FRIENDS);
%>

<p> Adding Friends to Users</p>



    <!-- Adding a group of Friends  -->
<%

    UserLocal user1=null;
    UserLocal user2=null;
    UserLocal user3=null;
    UserLocal user4=null;
    UserLocal user5=null;
    UserLocal user6=null;
    UserLocal user7=null;




    Collection friends=null;

    try {
    	user1=userHome.findByUsername ("mwringe");
        user2=userHome.findByUsername ("vivek");
        user3=userHome.findByUsername ("aahmed");
	user4=userHome.findByUsername ("lucy666");
	user5=userHome.findByUsername ("bobbyv");
	user5=userHome.findByUsername ("SamOak");
	user6=userHome.findByUsername ("mfrost");
	user7=userHome.findByUsername ("icyMeg");
    }
    catch (Exception e){
	out.println("Cannot get Users : "+ e.getMessage());
    }

    try{
        friends=user1.getFriends();
        friends.add (friendHome.create (user2) );
        friends.add (friendHome.create (user3) );
	friends.add (friendHome.create (user7) );
	friends.add (friendHome.create (user6) );
	user1.setFriends (friends);
    }  catch (Exception e){
        out.println ("Cannot get create Friends : " + e.getMessage());
    }

    try{
        friends=user2.getFriends();
        friends.add (friendHome.create (user1) );
        friends.add (friendHome.create (user3) );
        user2.setFriends (friends);
    }  catch (Exception e){
        out.println ("Cannot get create Friends : " + e.getMessage());
    }

    try{
        friends=user3.getFriends();
        friends.add (friendHome.create (user1) );
	friends.add (friendHome.create (user2) );
	friends.add (friendHome.create (user5) );
	friends.add (friendHome.create (user6) );
        user3.setFriends (friends);
    }  catch (Exception e){
        out.println ("Cannot get create Friends : " + e.getMessage());
    }

   try{
        friends=user4.getFriends();
        friends.add (friendHome.create (user7) );
	friends.add (friendHome.create (user3) );
	friends.add (friendHome.create (user5) );
	friends.add (friendHome.create (user6) );
        user4.setFriends (friends);
    }  catch (Exception e){
        out.println ("Cannot get create Friends : " + e.getMessage());
    }

    try{
        friends=user5.getFriends();
        friends.add (friendHome.create (user7) );
	friends.add (friendHome.create (user6) );
        user5.setFriends (friends);
    }  catch (Exception e){
        out.println ("Cannot get create Friends : " + e.getMessage());
    }

    try{
        friends=user6.getFriends();
	friends.add (friendHome.create (user4) );
	friends.add (friendHome.create (user7) );
	friends.add (friendHome.create (user3) );
	friends.add (friendHome.create (user5) );
	friends.add (friendHome.create (user1) );
        user6.setFriends (friends);
    }  catch (Exception e){
        out.println ("Cannot get create Friends : " + e.getMessage());
    }

    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/FriendSetView.jsp" /> <%
        }
    }  catch ( Exception e ) {
        out.println ( "Could not list all users FriendSet: " + e.getMessage() );
    }

%>








