<%--
 
 * Copyright 2002 Sun Microsystems, Inc. All Rights Reserved.
 * 
 * Redistribution and use in source and binary forms, with or
 * without modification, are permitted provided that the following
 * conditions are met:
 * 
 * - Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * 
 * - Redistribution in binary form must reproduce the above
 *   copyright notice, this list of conditions and the following
 *   disclaimer in the documentation and/or other materials
 *   provided with the distribution.
 * 
 * Neither the name of Sun Microsystems, Inc. or the names of
 * contributors may be used to endorse or promote products derived
 * from this software without specific prior written permission.
 * 
 * This software is provided "AS IS," without a warranty of any
 * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
 * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY
 * DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT OF OR
 * RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE OR
 * ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE
 * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT,
 * SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
 * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF
 * THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS
 * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 * 
 * You acknowledge that this software is not designed, licensed or
 * intended for use in the design, construction, operation or
 * maintenance of any nuclear facility.
  
--%>

<%@ taglib uri="/jstl-c" prefix="c" %>
<%@ taglib uri="/jstl-fmt" prefix="fmt" %>
<%@ taglib uri="/jstl-sql" prefix="sql" %>
<%@ page errorPage="errorpage.jsp" %>
<jsp:useBean id="cart" class="cart.ShoppingCart" scope="session"/>

<sql:setDataSource dataSource="jdbc/BookDB"/>

<c:if test="${!empty param.Add}">
  <c:set var="bid" value="${param.Add}"/>

  <sql:query var="books">
    select * from PUBLIC.books where id = ?
    <sql:param value="${bid}" />
  </sql:query>

  <c:forEach var="bookRow" begin="0" items="${books.rowsByIndex}"> 
    <jsp:useBean id="bid"  type="java.lang.String" />
    <jsp:useBean id="bookRow"  type="java.lang.Object[]" />
    <jsp:useBean id="addedBook" class="database.BookDetails" scope="page" />
      <jsp:setProperty name="addedBook" property="bookId" value="<%=bookRow[0]%>" />
      <jsp:setProperty name="addedBook" property="surname" value="<%=bookRow[1]%>" />
      <jsp:setProperty name="addedBook" property="firstName" value="<%=bookRow[2]%>" />
      <jsp:setProperty name="addedBook" property="title" value="<%=bookRow[3]%>" />
      <jsp:setProperty name="addedBook" property="price" value="<%=((Double)bookRow[4]).floatValue()%>" />
      <jsp:setProperty name="addedBook" property="year" value="<%=(Integer)bookRow[5]%>" />
      <jsp:setProperty name="addedBook" property="description" value="<%=bookRow[6]%>" />
      <jsp:setProperty name="addedBook" property="inventory" value="<%=(Integer)bookRow[7]%>" />
    </jsp:useBean>
    <% cart.add(bid, addedBook); %>
    <p><h3><font color="red" size="+2"> 
    <fmt:message key="CartAdded1"/> <em><c:out value="${bookRow[3]}"/></em> <fmt:message key="CartAdded2"/></font></h3>
  </c:forEach>
</c:if>

<c:if test="${sessionScope.cart.numberOfItems > 0}">     
  <c:url var="url" value="/showcart" >
    <c:param name="Clear" value="0" />
    <c:param name="Remove" value="0" />
  </c:url>
<p><strong><a href="<c:out value='${url}'/>"><fmt:message key="CartCheck"/></a>&nbsp;&nbsp;&nbsp;
    <c:url var="url" value="/cashier" />
    <a href="<c:out value='${url}'/>"><fmt:message key="Buy"/></a></p></strong>
</c:if>

<br>&nbsp;
<br>&nbsp;
<h3><fmt:message key="Choose"/></h3>
<center>
<table>

<sql:query var="books">
  select * from PUBLIC.books
</sql:query> 

<c:forEach var="book" begin="0" items="${books.rows}">
    <tr>
    <c:set var="bookId" value="${book.id}" />
    <td bgcolor="#ffffaa"> 
        <c:url var="url" value="/bookdetails" >
          <c:param name="bookId" value="${bookId}" />
        </c:url>
        <a href="<c:out value='${url}'/>"><strong><c:out value="${book.title}"/>&nbsp;</strong></a></td> 
    <td bgcolor="#ffffaa" rowspan=2> 
    <fmt:formatNumber value="${book.price}" type="currency"/> 
    &nbsp;</td> 

    <td bgcolor="#ffffaa" rowspan=2> 
    <c:url var="url" value="/catalog" >
      <c:param name="Add" value="${bookId}" />
    </c:url> 
    <p><strong><a href="<c:out value='${url}'/>">&nbsp;<fmt:message key="CartAdd"/>&nbsp;</a></td></tr> 

    <tr> 
    <td bgcolor="#ffffff"> 
    &nbsp;&nbsp;<fmt:message key="By"/> <em><c:out value="${book.firstname}"/>&nbsp;<c:out value="${book.surname}"/></em></td></tr>
</c:forEach>

</table>
</center>

