test
Форк от lirfrnk/test
1package servlets;
2
3import java.io.IOException;
4import java.io.PrintWriter;
5import java.util.UUID;
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.constant.BookStoreConstants;
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 AddBookServlet 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
35String bName = req.getParameter(BooksDBConstants.COLUMN_NAME);
36RequestDispatcher rd = req.getRequestDispatcher("SellerHome.html");
37rd.include(req, res);
38StoreUtil.setActiveTab(pw, "addbook");
39pw.println("<div class='container my-2'>");
40if(bName == null || bName.isBlank()) {
41//render the add book form;
42showAddBookForm(pw);
43return;
44} //else process the add book
45
46
47try {
48String uniqueID = UUID.randomUUID().toString();
49String bCode = uniqueID;
50String bAuthor = req.getParameter(BooksDBConstants.COLUMN_AUTHOR);
51double bPrice = Integer.parseInt(req.getParameter(BooksDBConstants.COLUMN_PRICE));
52int bQty = Integer.parseInt(req.getParameter(BooksDBConstants.COLUMN_QUANTITY));
53
54Book book = new Book(bCode, bName, bAuthor, bPrice, bQty);
55String message = bookService.addBook(book);
56if ("SUCCESS".equalsIgnoreCase(message)) {
57pw.println(
58"<table class=\"tab\"><tr><td>Book Detail Updated Successfully!<br/>Add More Books</td></tr></table>");
59} else {
60pw.println("<table class=\"tab\"><tr><td>Failed to Add Books! Fill up CareFully</td></tr></table>");
61//rd.include(req, res);
62}
63} catch (Exception e) {
64e.printStackTrace();
65pw.println("<table class=\"tab\"><tr><td>Failed to Add Books! Fill up CareFully</td></tr></table>");
66}
67}
68
69private static void showAddBookForm(PrintWriter pw) {
70String form = "<table class=\"tab my-5\" style=\"width:40%;\">\r\n"
71+ " <tr>\r\n"
72+ " <td>\r\n"
73+ " <form action=\"addbook\" method=\"post\">\r\n"
74+ " <!-- <label for=\"bookCode\">Book Code : </label><input type=\"text\" name=\"barcode\" id=\"bookCode\" placeholder=\"Enter Book Code\" required><br/> -->\r\n"
75+ " <label for=\"bookName\">Book Name : </label> <input type=\"text\" name=\"name\" id=\"bookName\" placeholder=\"Enter Book's name\" required><br/>\r\n"
76+ " <label for=\"bookAuthor\">Book Author : </label><input type=\"text\" name=\"author\" id=\"bookAuthor\" placeholder=\"Enter Author's Name\" required><br/>\r\n"
77+ " <label for=\"bookPrice\">Book Price : </label><input type=\"number\" name=\"price\" placeholder=\"Enter the Price\" required><br/>\r\n"
78+ " <label for=\"bookQuantity\">Book Qnty : </label><input type=\"number\" name=\"quantity\" id=\"bookQuantity\" placeholder=\"Enter the quantity\" required><br/>\r\n"
79+ " <input class=\"btn btn-success my-2\" type=\"submit\" value=\" Add Book \">\r\n"
80+ " </form>\r\n"
81+ " </td>\r\n"
82+ " </tr> \r\n"
83+ " <!-- <tr>\r\n"
84+ " <td><a href=\"index.html\">Go Back To Home Page</a></td>\r\n"
85+ " </tr> -->\r\n"
86+ " </table>";
87pw.println(form);
88}
89}
90