<%-- 
/**
  * Copyright (c) 2004 Red Hat, Inc. All rights reserved.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or any later version.
  *
  * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
  * USA
  *
  * Component of: Red Hat Application Server
  * 
  * Initial Developers: Aizaz Ahmed
  *                     Vivek Lakshmanan
  *                     Andrew Overholt
  *                     Matthew Wringe
  *
  */
--%>
<%@ page contentType="text/html"
         import="olstore.entity.*"
         import="olstore.util.WebUtils"
         import="olstore.entity.UserLocalHome"
         import="olstore.entity.UserLocal"
         import="java.util.Collection"
         import="java.util.Iterator"
         import="olstore.framework.EJBHomeFactory" %>


    <% request.setAttribute ( "page_title", "RHAPS Online Store - Demo" ); %>
    <jsp:include page="../nav/Header.jsp" />

    <!-- Content begins here -->
    <div id="form" >

<%-- Get the home Type --%>
<%
    String FRIENDHEADER = "Items your Friends picked";
    String SPECIALHEADER = "Items on special offer";

    EJBHomeFactory factory = EJBHomeFactory.getInstance();
    ItemLocalHome home = (ItemLocalHome)factory.getLocalHome(EJBHomeFactory.ITEM);
    UserLocalHome userHome = (UserLocalHome)factory.getLocalHome (EJBHomeFactory.USER);
    
    String username = request.getRemoteUser();
    UserLocal user = userHome.findByUsername( username );

    String typeStr = WebUtils.getValue ( "type", request );

    Collection friendSet = null;
    Collection specialSet = null;
    Collection otherSet = null;

    // If no type is specified, get the main page
    if ( WebUtils.isEmpty ( typeStr ) ) {
        friendSet = home.findByMainPageAndFriendsOf ( username );
        specialSet = home.findByMainPageAndSpecialOffer ();
        otherSet = home.findByMainPage ();        

    } else {
        friendSet = home.findByTypeAndFriendsOf ( typeStr, username );
        specialSet = home.findByTypeAndSpecialOffer ( typeStr );
        otherSet = home.findByType ( typeStr );
    }


    if ( friendSet.isEmpty() ) {
        // grab the first four special offers and place them where the
        // friends would have been
        int count = 1 ;
        Iterator it = specialSet.iterator();
        while ( it.hasNext() && count <=4 ) {
            friendSet.add ( it.next() );
            it.remove();
            count++;
        }
        FRIENDHEADER = "Special Offers";
        SPECIALHEADER = "More Specials";
    }


    if ( ! friendSet.isEmpty() ) {
%>
        <p> <div class="formHeader"> <%=FRIENDHEADER%> </div>
<%
        request.setAttribute ( "collection", friendSet );
        request.setAttribute ( "template", "../views/ItemDetailedView.jsp" );
%>
        <jsp:include page="../views/ListCollection.jsp" />
<%
    }

    if ( ! specialSet.isEmpty() ) {
%>
        <p> <div class="formHeader"> <%=SPECIALHEADER%></div>
<%
        request.setAttribute ( "collection", specialSet );
        request.setAttribute ( "template", "../views/ItemThumbView.jsp" );
%>
        <jsp:include page="../views/ListCollection.jsp" />
<%
    }

    if ( ! otherSet.isEmpty() ) {
%>

        <p> <div class="formHeader"> All Available Items </div>
<%
        request.setAttribute ( "collection", otherSet );
        request.setAttribute ( "template", "../views/ItemListView.jsp" );
%>
        <jsp:include page="../views/ListCollection.jsp" />
<%
    }
%>

<div class="spacer" ></div>
<br style="clear:both" />
</div>

    <jsp:include page="../nav/Footer.jsp" />
