test
Форк от lirfrnk/test
1package servlets;
2
3import java.io.IOException;
4import java.io.PrintWriter;
5
6import javax.servlet.RequestDispatcher;
7import javax.servlet.ServletException;
8import javax.servlet.http.HttpServlet;
9import javax.servlet.http.HttpServletRequest;
10import javax.servlet.http.HttpServletResponse;
11
12import com.bittercode.constant.BookStoreConstants;
13import com.bittercode.constant.ResponseCode;
14import com.bittercode.constant.db.BooksDBConstants;
15import com.bittercode.model.Book;
16import com.bittercode.model.UserRole;
17import com.bittercode.service.BookService;
18import com.bittercode.service.impl.BookServiceImpl;
19import com.bittercode.util.StoreUtil;
20
21public class UpdateBookServlet extends HttpServlet {
22BookService bookService = new BookServiceImpl();
23
24public void service(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
25PrintWriter pw = res.getWriter();
26res.setContentType(BookStoreConstants.CONTENT_TYPE_TEXT_HTML);
27
28if (!StoreUtil.isLoggedIn(UserRole.SELLER, req.getSession())) {
29RequestDispatcher rd = req.getRequestDispatcher("SellerLogin.html");
30rd.include(req, res);
31pw.println("<table class=\"tab\"><tr><td>Please Login First to Continue!!</td></tr></table>");
32return;
33}
34
35RequestDispatcher rd = req.getRequestDispatcher("SellerHome.html");
36rd.include(req, res);
37StoreUtil.setActiveTab(pw, "storebooks");
38pw.println("<div class='container my-2'>");
39
40try {
41if (req.getParameter("updateFormSubmitted") != null) {
42String bName = req.getParameter(BooksDBConstants.COLUMN_NAME);
43String bCode = req.getParameter(BooksDBConstants.COLUMN_BARCODE);
44String bAuthor = req.getParameter(BooksDBConstants.COLUMN_AUTHOR);
45double bPrice = Double.parseDouble(req.getParameter(BooksDBConstants.COLUMN_PRICE));
46int bQty = Integer.parseInt(req.getParameter(BooksDBConstants.COLUMN_QUANTITY));
47
48Book book = new Book(bCode, bName, bAuthor, bPrice, bQty);
49String message = bookService.updateBook(book);
50if (ResponseCode.SUCCESS.name().equalsIgnoreCase(message)) {
51pw.println(
52"<table class=\"tab\"><tr><td>Book Detail Updated Successfully!</td></tr></table>");
53} else {
54pw.println("<table class=\"tab\"><tr><td>Failed to Update Book!!</td></tr></table>");
55// rd.include(req, res);
56}
57
58return;
59}
60
61String bookId = req.getParameter("bookId");
62
63if (bookId != null) {
64Book book = bookService.getBookById(bookId);
65showUpdateBookForm(pw, book);
66}
67
68} catch (Exception e) {
69e.printStackTrace();
70pw.println("<table class=\"tab\"><tr><td>Failed to Load Book data!!</td></tr></table>");
71}
72}
73
74private static void showUpdateBookForm(PrintWriter pw, Book book) {
75String form = "<table class=\"tab my-5\" style=\"width:40%;\">\r\n"
76+ " <tr>\r\n"
77+ " <td>\r\n"
78+ " <form action=\"updatebook\" method=\"post\">\r\n"
79+ " <label for=\"bookCode\">Book Code : </label><input type=\"text\" name=\"barcode\" id=\"bookCode\" placeholder=\"Enter Book Code\" value='"
80+ book.getBarcode() + "' readonly><br/>"
81+ " <label for=\"bookName\">Book Name : </label> <input type=\"text\" name=\"name\" id=\"bookName\" placeholder=\"Enter Book's name\" value='"
82+ book.getName() + "' required><br/>\r\n"
83+ " <label for=\"bookAuthor\">Book Author : </label><input type=\"text\" name=\"author\" id=\"bookAuthor\" placeholder=\"Enter Author's Name\" value='"
84+ book.getAuthor() + "' required><br/>\r\n"
85+ " <label for=\"bookPrice\">Book Price : </label><input type=\"number\" name=\"price\" placeholder=\"Enter the Price\" value='"
86+ book.getPrice() + "' required><br/>\r\n"
87+ " <label for=\"bookQuantity\">Book Qnty : </label><input type=\"number\" name=\"quantity\" id=\"bookQuantity\" placeholder=\"Enter the quantity\" value='"
88+ book.getQuantity() + "' required><br/>\r\n"
89+ " <input class=\"btn btn-success my-2\" type=\"submit\" name='updateFormSubmitted' value=\" Update Book \">\r\n"
90+ " </form>\r\n"
91+ " </td>\r\n"
92+ " </tr> \r\n"
93+ " </table>";
94pw.println(form);
95}
96}
97