Advertisement
stronk_8s

JAVA INTERNAL

Apr 3rd, 2025
433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.80 KB | Source Code | 0 0
  1. //Local.java
  2. @Local
  3. public interface MovieBeansLocal {
  4.  
  5.     void addmovie(String moviename, Collection<Integer> tids);
  6.  
  7.     Collection<Movie> getallmovies();
  8.  
  9.     Movie getMovieById(Integer id);
  10.  
  11.     void deletemoviebyid(Integer id);
  12.  
  13.     Collection<Theater> getalltheaters();
  14.  
  15.     Collection<Theater> getalltheatersbymovie(Integer id);
  16.  
  17.     Collection<Movie> getallmoviesbytheater(Integer id);
  18. }
  19.  
  20. //Beans.java
  21.  
  22. @Stateless
  23. public class MovieBeans implements MovieBeansLocal {
  24.  
  25.     @PersistenceContext(unitName = "my_persistence_unit")
  26.     EntityManager em;
  27.  
  28.     @Override
  29.     public void addmovie(String moviename, Collection<Integer> tids) {
  30.         Movie m = new Movie();
  31.         m.setName(moviename);
  32.         em.persist(m);
  33.         Collection<Theater> t = new ArrayList<>();
  34.         for (Integer q : tids) {
  35.             Theater o = em.find(Theater.class, q);
  36.  
  37.             if (o != null) {
  38.                 Collection<Movie> s = o.getMovieCollection();
  39.                 s.add(m);
  40.  
  41.                 t.add(o);
  42.                 m.setTheaterCollection(t);
  43.                 o.setMovieCollection(s);
  44.                 em.merge(o);
  45.             }
  46.         }
  47.     }
  48.  
  49.     @Override
  50.     public Collection<Movie> getallmovies() {
  51.         return em.createNamedQuery("Movie.findAll").getResultList();
  52.     }
  53.  
  54.     @Override
  55.     public void deletemoviebyid(Integer id) {
  56.         Movie m = em.find(Movie.class, id);
  57.         Collection<Theater> theater = m.getTheaterCollection();
  58.         for (Theater t : theater) {
  59.             if (t != null) {
  60.                 Collection<Movie> d = t.getMovieCollection();
  61.                 d.remove(m);
  62.                 t.setMovieCollection(d);
  63.             }
  64.         }
  65.         em.remove(m);
  66.     }
  67.  
  68.     @Override
  69.     public Collection<Theater> getalltheaters() {
  70.         return em.createNamedQuery("Theater.findAll").getResultList();
  71.     }
  72.  
  73.     @Override
  74.     public Collection<Theater> getalltheatersbymovie(Integer id) {
  75.         Movie m = em.find(Movie.class, id);
  76.         return m.getTheaterCollection();
  77.     }
  78.  
  79.     @Override
  80.     public Collection<Movie> getallmoviesbytheater(Integer id) {
  81.         Theater t = em.find(Theater.class, id);
  82.         return t.getMovieCollection();
  83.     }
  84.  
  85.     @Override
  86.     public Movie getMovieById(Integer id) {
  87.         return em.find(Movie.class, id);
  88.     }
  89. }
  90.  
  91.  
  92.  
  93. //List-Servlet.java
  94.  
  95. @WebServlet(name = "MovieServlet", urlPatterns = {"/MovieServlet"})
  96. public class MovieServlet extends HttpServlet {
  97.    
  98.     @EJB
  99.     MovieBeansLocal mbl;
  100.    
  101.     Collection<Movie> movies = new ArrayList<>();
  102.     Collection<Theater> theaters = new ArrayList<>();
  103.  
  104.  
  105.     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  106.             throws ServletException, IOException {
  107.         response.setContentType("text/html;charset=UTF-8");
  108.         try (PrintWriter out = response.getWriter()) {
  109.             /* TODO output your page here. You may use following sample code. */
  110.             out.println("<!DOCTYPE html>");
  111.             out.println("<html>");
  112.             out.println("<head>");
  113.             out.println("<title>Servlet MovieServlet</title>");
  114.             out.println("</head>");
  115.             out.println("<body>");
  116.             out.println("<h1>Servlet MovieServlet at " + request.getContextPath() + "</h1>");
  117.             out.println("<a href='AddServlet'>Add Servlet</a>");
  118.             out.println("<table border='1'><tr><td>Moviename</td><td>Theaters</td><td>City</td><td>State</td><td>location</td><td>TO Delete</td></tr>");
  119.             movies = mbl.getallmovies();
  120.             for (Movie k : movies) {
  121.                 out.println("<tr>");
  122.                 out.println("<td>" + k.getName() + "</td>");
  123.                 theaters = mbl.getalltheatersbymovie(k.getId());
  124.                 Theater temp = new Theater();
  125.                 out.println("<td><ui>");
  126.                 for (Theater t : theaters) {
  127.                     out.println("<li>" + t.getName() + "</li>");
  128.                     temp = t;
  129.                 }
  130.                 out.println("</ui></td>");
  131.                 out.println("<td>" + temp.getState() + "</td>");
  132.                 out.println("<td>" + temp.getLocation() + "</td>");
  133.                 out.println("<td>" + temp.getCity() + "</td>");
  134.                 out.println("<td><a href='EditMovieServlet?id=" + k.getId() + "'>Edit Movie</a></td>");
  135.                 out.println("<td><a href='MovieServlet?delid=" + k.getId() + "'>Delete Movie</a></td>");
  136.                 out.println("</tr>");
  137.             }
  138.             out.println("</table>");
  139.             out.println("</body>");
  140.             out.println("</html>");
  141.         }
  142.     }
  143.  
  144.  
  145.     @Override
  146.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  147.             throws ServletException, IOException {
  148.         String del_id = request.getParameter("delid");
  149.         if (del_id != null) {
  150.             mbl.deletemoviebyid(Integer.parseInt(del_id));
  151.             response.sendRedirect("MovieServlet");
  152.         } else {
  153.             processRequest(request, response);
  154.         }
  155.     }
  156.  
  157.     @Override
  158.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  159.             throws ServletException, IOException {
  160.         processRequest(request, response);
  161.     }
  162.  
  163.     @Override
  164.     public String getServletInfo() {
  165.         return "Short description";
  166.     }
  167.  
  168. }
  169.  
  170.  
  171. //Add-Servlet.java
  172.  
  173. @WebServlet(name = "AddServlet", urlPatterns = {"/AddServlet"})
  174. public class AddServlet extends HttpServlet {
  175.  
  176.     @EJB
  177.     MovieBeansLocal mbl;
  178.  
  179.     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  180.             throws ServletException, IOException {
  181.         response.setContentType("text/html;charset=UTF-8");
  182.         try (PrintWriter out = response.getWriter()) {
  183.             /* TODO output your page here. You may use following sample code. */
  184.             out.println("<!DOCTYPE html>");
  185.             out.println("<html>");
  186.             out.println("<head>");
  187.             out.println("<title>Servlet AddServlet</title>");
  188.             out.println("</head>");
  189.             out.println("<body>");
  190.             out.println("<h1>Servlet AddServlet at " + request.getContextPath() + "</h1>");
  191.             out.println("<form action=\"AddServlet\" method=\"POST\">\n"
  192.                     + "<input type=\"text\" name=\"name\" value=\"\" />\n"
  193.                     + "<input type=\"checkbox\" name=\"theater\" value=\"1\" checked=\"checked\" />Rupam\n"
  194.                     + "<input type=\"checkbox\" name=\"theater\" value=\"2\" />Rajhans\n"
  195.                     + "<input type=\"checkbox\" name=\"theater\" value=\"3\" />PVR\n"
  196.                     + "<input type=\"checkbox\" name=\"theater\" value=\"4\" />INOR\n"
  197.                     + "<input type=\"submit\" value=\"add\" />"
  198.                     + "</form>\n");
  199.             out.println("</body>");
  200.             out.println("</html>");
  201.         }
  202.     }
  203.     @Override
  204.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  205.             throws ServletException, IOException {
  206.         processRequest(request, response);
  207.     }
  208.  
  209.     @Override
  210.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  211.             throws ServletException, IOException {
  212.         String moviename = request.getParameter("name");
  213.         if (moviename != null) {
  214.             String[] s = request.getParameterValues("theater");
  215.             Collection<Integer> c = new ArrayList<>();
  216.             for (String p : s) {
  217.                 Integer a = Integer.parseInt(p);
  218.                 c.add(a);
  219.             }
  220.  
  221.             mbl.addmovie(moviename, c);
  222.         }
  223.         response.sendRedirect("MovieServlet");
  224.     }
  225.     @Override
  226.     public String getServletInfo() {
  227.         return "Short description";
  228.     }
  229. }
  230.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement