test
Форк от lirfrnk/test
1package servlets;
2
3import java.io.IOException;
4import java.io.PrintWriter;
5import java.util.List;
6
7import javax.servlet.RequestDispatcher;
8import javax.servlet.ServletException;
9import javax.servlet.http.HttpServlet;
10import javax.servlet.http.HttpServletRequest;
11import javax.servlet.http.HttpServletResponse;
12
13import com.bittercode.model.Book;
14import com.bittercode.model.UserRole;
15import com.bittercode.service.BookService;
16import com.bittercode.service.impl.BookServiceImpl;
17import com.bittercode.util.StoreUtil;
18
19public class StoreBookServlet extends HttpServlet {
20
21// book service for database operations and logics
22BookService bookService = new BookServiceImpl();
23
24public void service(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
25PrintWriter pw = res.getWriter();
26res.setContentType("text/html");
27
28// Check if the customer is logged in, or else return to login page
29if (!StoreUtil.isLoggedIn(UserRole.SELLER, req.getSession())) {
30RequestDispatcher rd = req.getRequestDispatcher("SellerLogin.html");
31rd.include(req, res);
32pw.println("<table class=\"tab\"><tr><td>Please Login First to Continue!!</td></tr></table>");
33return;
34}
35try {
36
37// Add/Remove Item from the cart if requested
38// store the comma separated bookIds of cart in the session
39// StoreUtil.updateCartItems(req);
40
41RequestDispatcher rd = req.getRequestDispatcher("SellerHome.html");
42rd.include(req, res);
43pw.println("<div class='container'>");
44// Set the active tab as cart
45StoreUtil.setActiveTab(pw, "storebooks");
46
47// Read the books from the database with the respective bookIds
48List<Book> books = bookService.getAllBooks();
49pw.println("<div id='topmid' style='background-color:grey'>Books Available In the Store</div>");
50pw.println("<table class=\"table table-hover\" style='background-color:white'>\r\n"
51+ " <thead>\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"
59+ " </tr>\r\n"
60+ " </thead>\r\n"
61+ " <tbody>\r\n");
62if (books == null || books.size() == 0) {
63pw.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"
65+ " </tr>\r\n");
66}
67for (Book book : books) {
68pw.println(getRowData(book));
69}
70
71pw.println(" </tbody>\r\n"
72+ "</table></div>");
73
74} catch (Exception e) {
75e.printStackTrace();
76}
77}
78
79public String getRowData(Book book) {
80return " <tr>\r\n"
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"
85+ " <td>"
86+ book.getQuantity()
87+ " </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>"
91+ " </form>"
92+ " </tr>\r\n";
93}
94
95}
96