EJB Application with CRUD Operation

·         Relation Ship Configuration.
Additional ref :ALTER TABLE TEST1 ADD CONSTRAINT Ffgxff FOREIGN KEY (BB) REFERENCES TEST4 (ID);

1.       Many to many Mapping

@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.EAGER)
    @JoinTable(name = "book_author",joinColumns = {@JoinColumn(name = "book_id")},
            inverseJoinColumns = {@JoinColumn(name = "author_id")})
private Set<Author> authors;


2.       One to Many Mapping

@JoinColumn(name = "FID", referencedColumnName = "ID")
    @ManyToOne
    private Untitled fid;
 @OneToMany(mappedBy = "fid")
    private Collection<Test2> test2Collection;


·         Create Session bean for Entity Classes on ejb project.
·         Create Servlet in war project and right click on the class and go to insert code option and hit “call enterprise bean” and add necessary façade classes.

·         Add book

@WebServlet(name = "AddBook", urlPatterns = {"/AddBook"})
public class AddBook extends HttpServlet {
    @EJB
    private BookFacade bookFacade;
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        String isbn = request.getParameter("isbn");
        String title = request.getParameter("title");
        String price = request.getParameter("price");
        String year = request.getParameter("year");
        String language = request.getParameter("language");
        if (isbn != null && title != null) {
            Book book = new Book();
            book.setIsbn(isbn);
            book.setTitle(title);
            book.setPrice(Double.parseDouble(price));
            book.setYear(Integer.parseInt(year));
            book.setLanguage(language);
            bookFacade.create(book);
            response.sendRedirect("ListBooks");
        }
        String editmode = request.getParameter("editmode");
        PrintWriter out = response.getWriter();
        try {
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Add a Book</title>");
            out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"" + request.getContextPath() + "/default.css\" />");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1><a href=\"" + request.getContextPath() + "\">Home</a> &gt; Add a Book</h1>");
            out.println("<form>");
            out.println("<table>");
            out.println("<tr><td>ISBN:</td><td><input type='text' name='isbn'></td></tr>");
            out.println("<tr><td>Title:</td><td><input type='text' name='title'></td></tr>");
            out.println("<tr><td>Price:</td><td><input type='text' name='price'></td></tr>");
            out.println("<tr><td>Year:</td><td><input type='text' name='year'></td></tr>");
            out.println("<tr><td>Language:</td><td><input type='text' name='language'></td></tr>");
            out.println("</table>");
            out.println("<br><input type='submit' value='Add'><br/>");
            out.println("</form>");
            out.println("</body>");
            out.println("</html>");
        } finally {
            out.close();
        }
    }

·         Search book

Step 1

out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Search for a Book</title>");
            out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"" + request.getContextPath() + "/default.css\" />");
            out.println("<style>");
            out.println("table { border: none; }");
            out.println("</style>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1><a href=\"" + request.getContextPath() + "\">Home</a> &gt; Search for a Book</h1>");

            out.println("<p>Please enter the search criteria</p>");
            out.println("<form action='ListBooks' method='post'>");
            out.println("<table cellspacing=\"0\" cellpadding=\"0\" style=\"border: none;\">");
            out.println("<tr><td>ISBN (exact match):</td><td><input type='text' name='isbn'></td></tr>");
            out.println("<tr><td>Title:</td><td><input type='text' name='title'></td></tr>");
            out.println("<tr><td>Author:</td><td><input type='text' name='author'></td></tr>");
            out.println("</table>");
            out.println("<br><input type='submit' value='Search'><br>");
            out.println("</form>");

            out.println("</body>");
            out.println("</html>");

Step 2

@WebServlet(name = "ListBooks", urlPatterns = {"/ListBooks"})
public class ListBooks extends HttpServlet {
    @EJB
    private BookFacade bookFacade;
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        String isbn = request.getParameter("isbn");
        String title = request.getParameter("title");
        String author = request.getParameter("author");
        HashMap searchCriteria = null;
        if (isbn != null && !isbn.isEmpty()) {
            if (searchCriteria == null) {
                searchCriteria = new HashMap();
            }
            searchCriteria.put("isbn", isbn);
        }
        if (title != null && !title.isEmpty()) {
            if (searchCriteria == null) {
                searchCriteria = new HashMap();
            }
            searchCriteria.put("title", title);
        }
        if (author != null && !author.isEmpty()) {
            if (searchCriteria == null) {
                searchCriteria = new HashMap();
            }
            searchCriteria.put("author", author);
        }
        PrintWriter out = response.getWriter();//) {
        try {
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"" + request.getContextPath() + "/default.css\" />");
            out.println("</head>");
            out.println("<body>");
            if (searchCriteria == null) {
                out.println("<h1><a href=\"" + request.getContextPath() + "\">Home</a> &gt; List of Books</h1>");
            } else {
                out.println("<h1><a href=\"" + request.getContextPath() + "/SearchBooks\">Search</a> &gt; Search results</h1>");
            }
            out.println("<table border=\"1\" style=\"width:600px;\"");
            out.println("<tr>");
            out.println("   <th>ID</th>");
            out.println("   <th>ISBN</th>");
            out.println("   <th>Title</th>");
            out.println("   <th>Price</th>");
            out.println("   <th>Year</th>");
            out.println("   <th>Language</th>");
            out.println("   <th></th>");
            out.println("   <th></th>");
            out.println("</tr>");
            List<Book> books;
            if (searchCriteria == null) {
                books = bookFacade.findAll();
            } else {
                books = bookFacade.searchBooks(searchCriteria);
            }
            for (Book item : books) {
                out.println("<tr>");
                out.println("   <td>" + item.getId() + "</td>");
                out.println("   <td>" + item.getIsbn() + "</td>");
                out.println("   <td>" + item.getTitle() + "</td>");
                out.println("   <td>" + item.getPrice() + "</td>");
                out.println("   <td>" + item.getYear() + "</td>");
                out.println("   <td>" + item.getLanguage() + "</td>");
                out.println("   <td><a href=\"ViewBook?id=" + item.getId() + "\">Details</td>");
                out.println("   <td><a href=\"ViewBook?id=" + item.getId() + "&edit\">Edit</td>");
                out.println("</tr>");
            }
            out.println("</table><br>");
            out.println("<a href=\"AddBook\">Add a new Book</a><br>");
            out.println("<a href=\"SearchBooks\">Search Books</a>");
            out.println("</body>");
            out.println("</html>");
        } //  }
        finally {
            out.close();
        }
    }

·         Update book, Add author and Delete author

@WebServlet(name = "ViewBook", urlPatterns = {"/ViewBook"})
public class ViewBook extends HttpServlet {
    @EJB
    private AuthorFacade authorFacade;
    @EJB
    private BookFacade bookFacade;
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        String id = request.getParameter("id");
        String editmode = request.getParameter("edit");
        String action = request.getParameter("action");
        String authorid = request.getParameter("authorid");
        if (action != null && id != null) {
            if (action.equals("update")) {
                String editisbn = request.getParameter("editisbn");
                String edittitle = request.getParameter("edittitle");
                String editprice = request.getParameter("editprice");
                String edityear = request.getParameter("edityear");
                String editlang = request.getParameter("editlang");
                Book book = bookFacade.find(Long.parseLong(id));
                book.setIsbn(editisbn);
                book.setTitle(edittitle);
                double oldPrice = book.getPrice();
                try {
                    book.setPrice(Double.parseDouble(editprice));
                } catch (NumberFormatException ex) {
                    book.setPrice(oldPrice);
                }
                int oldYear = book.getYear();
                try {
                    book.setYear(Integer.parseInt(edityear));
                } catch (NumberFormatException ex) {
                    book.setYear(oldYear);
                }
                book.setLanguage(editlang);
                bookFacade.edit(book);
                response.sendRedirect("ListBooks");
            } else if (action.equals("deleteauthor")) {
                if (authorid != null) {
                    Book b = bookFacade.find(Long.parseLong(id));
                    Author a = authorFacade.find(Long.parseLong(authorid));
                    if (b != null && a != null) {
                        bookFacade.deleteAuthor(b, a);
                    }
                    response.sendRedirect("ViewBook?id=" + id + "&edit");
                }
            } else if (action.equals("addauthor")) {
                if (authorid != null) {
                    Book b = bookFacade.find(Long.parseLong(id));
                    Author a = authorFacade.find(Long.parseLong(authorid));
                    if (b != null && a != null) {
                        bookFacade.addAuthor(b, a);
                    }
                    response.sendRedirect("ViewBook?id=" + id + "&edit");
                }
            }
        }
        if (id != null) {
            Book book = bookFacade.find(Long.parseLong(id));
            PrintWriter out = response.getWriter(); //) {
            try {
                out.println("<!DOCTYPE html>");
                out.println("<html>");
                out.println("<head>");
                if (editmode == null) {
                    out.println("<title>Book Details</title>");
                } else {
                    out.println("<title>Edit Book Details</title>");
                }
                out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"" + request.getContextPath() + "/default.css\" />");
                out.println("</head>");
                out.println("<body>");
                if (editmode == null) {
                    out.println("<h1><a href=\"ListBooks\">List</a> &gt; Book Details</h1>");
                } else {
                    out.println("<h1><a href=\"ListBooks\">List</a> &gt; Edit Book Details</h1>");
                    out.println("<form>");
                }
                out.println("<table>");
                out.println("<tr>");
                out.println("<td>ID:</td>");
                out.println("<td>" + book.getId() + "</td>");
                out.println("</tr>");
                out.println("<tr>");
                out.println("<td>ISBN:</td>");
                if (editmode == null) {
                    out.println("<td>" + book.getIsbn() + "</td>");
                } else {
                    out.println("<td><input type='text' name='editisbn' value='" + book.getIsbn() + "'></input></td>");
                }
                out.println("</tr>");
                out.println("<tr>");
                out.println("<td>Title:</td>");
                if (editmode == null) {
                    out.println("<td>" + book.getTitle() + "</td>");
                } else {
                    out.println("<td><input type='text' name='edittitle' value='" + book.getTitle() + "'></input></td>");
                }
                out.println("</tr>");
                out.println("<tr>");
                out.println("<td>Price:</td>");
                if (editmode == null) {
                    out.println("<td>" + book.getPrice() + "</td>");
                } else {
                    out.println("<td><input type='text' name='editprice' value='" + book.getPrice() + "'></input></td>");
                }
                out.println("</tr>");
                out.println("<tr>");
                out.println("<td>Year:</td>");
                if (editmode == null) {
                    out.println("<td>" + book.getYear() + "</td>");
                } else {
                    out.println("<td><input type='text' name='edityear' value='" + book.getYear() + "'></input></td>");
                }
                out.println("</tr>");
                out.println("<tr>");
                out.println("<td>Language</td>");
                if (editmode == null) {
                    out.println("<td>" + book.getLanguage() + "</td>");
                } else {
                    out.println("<td><input type='text' name='editlang' value='" + book.getLanguage() + "'></input></td>");
                }
                out.println("</tr>");
                out.println("<tr>");
                out.println("<td>Author(s):</td>");
                out.println("<td>");
                if (editmode != null) {
                    Set<Author> authors = book.getAuthors();
                    for (Author auth : authors) {
                        out.println(auth.getName() + " <a href=\"ViewBook?action=deleteauthor&id=" + book.getId()
                                + "&authorid=" + auth.getId() + "\">Remove</a><br>");
                    }
                    // Make sure the authors already existing for the book doesn't show up in the combo box
                    Set<Author> dbAuthors = new HashSet<Author>(authorFacade.findAll());
                    dbAuthors.removeAll(authors);
                    out.println("<br><form>");
                    out.println("<input type='hidden' name='action' value='addauthor'></input>");
                    out.println("<select name=\"authorid\">");
                    for (Author auth : dbAuthors) {
                        out.println("   <option value='" + auth.getId() + "'>" + auth.getName() + "</option>");
                    }
                    out.println("</select>");
                    out.println("<input type='submit' value='Add' formmethod='post'></input>");
                    out.println("</form");
                } else {
                    Set<Author> authors = book.getAuthors();
                    for (Author auth : authors) {
                        out.println(auth.getName() + "<br>");
                    }
                }
                out.println("</td>");
                out.println("</tr>");
                out.println("</table>");
                if (editmode != null) {
                    out.println("<br><input type='submit' value='Save' formmethod='post'></input>");
                    out.println("<input type='hidden' name='action' value='update'></input>");
                    out.println("</form>");
                }
                out.println("</body>");
                out.println("</html>");
            } finally {
                out.close();
            }
        }

    }

Comments

Popular posts from this blog

Encrypt and Decrypt text using alphabet shifting method

Gridview to Datatable and Viewstate

You think Creating Circuler Graph is a difficult task ...........!!!