3
import java.io.IOException;
4
import java.io.PrintWriter;
7
import javax.servlet.RequestDispatcher;
8
import javax.servlet.ServletException;
9
import javax.servlet.http.HttpServlet;
10
import javax.servlet.http.HttpServletRequest;
11
import javax.servlet.http.HttpServletResponse;
13
import com.bittercode.model.Book;
14
import com.bittercode.model.UserRole;
15
import com.bittercode.service.BookService;
16
import com.bittercode.service.impl.BookServiceImpl;
17
import com.bittercode.util.StoreUtil;
19
public class StoreBookServlet extends HttpServlet {
21
// book service for database operations and logics
22
BookService bookService = new BookServiceImpl();
24
public void service(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
25
PrintWriter pw = res.getWriter();
26
res.setContentType("text/html");
28
// Check if the customer is logged in, or else return to login page
29
if (!StoreUtil.isLoggedIn(UserRole.SELLER, req.getSession())) {
30
RequestDispatcher rd = req.getRequestDispatcher("SellerLogin.html");
32
pw.println("<table class=\"tab\"><tr><td>Please Login First to Continue!!</td></tr></table>");
37
// Add/Remove Item from the cart if requested
38
// store the comma separated bookIds of cart in the session
39
// StoreUtil.updateCartItems(req);
41
RequestDispatcher rd = req.getRequestDispatcher("SellerHome.html");
43
pw.println("<div class='container'>");
44
// Set the active tab as cart
45
StoreUtil.setActiveTab(pw, "storebooks");
47
// Read the books from the database with the respective bookIds
48
List<Book> books = bookService.getAllBooks();
49
pw.println("<div id='topmid' style='background-color:grey'>Books Available In the Store</div>");
50
pw.println("<table class=\"table table-hover\" style='background-color:white'>\r\n"
52
+ " <tr style='background-color:black; color:white;'>\r\n"
53
+ " <th scope=\"col\">BookId</th>\r\n"
54
+ " <th scope=\"col\">Name</th>\r\n"
55
+ " <th scope=\"col\">Author</th>\r\n"
56
+ " <th scope=\"col\">Price</th>\r\n"
57
+ " <th scope=\"col\">Quantity</th>\r\n"
58
+ " <th scope=\"col\">Action</th>\r\n"
62
if (books == null || books.size() == 0) {
63
pw.println(" <tr style='background-color:green'>\r\n"
64
+ " <th scope=\"row\" colspan='6' style='color:yellow; text-align:center;'> No Books Available in the store </th>\r\n"
67
for (Book book : books) {
68
pw.println(getRowData(book));
71
pw.println(" </tbody>\r\n"
74
} catch (Exception e) {
79
public String getRowData(Book book) {
81
+ " <th scope=\"row\">" + book.getBarcode() + "</th>\r\n"
82
+ " <td>" + book.getName() + "</td>\r\n"
83
+ " <td>" + book.getAuthor() + "</td>\r\n"
84
+ " <td><span>₹</span> " + book.getPrice() + "</td>\r\n"
88
+ " <td><form method='post' action='updatebook'>"
89
+ " <input type='hidden' name='bookId' value='" + book.getBarcode() + "'/>"
90
+ " <button type='submit' class=\"btn btn-success\">Update</button>"