Guest User

controler

a guest
May 3rd, 2022
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.93 KB | None | 0 0
  1. package pt.agilefactor.trulydocs;
  2.  
  3. import java.io.IOException;
  4. import java.sql.SQLException;
  5. import java.time.LocalDateTime;
  6. import java.time.format.DateTimeFormatter;
  7. import java.util.List;
  8.  
  9. import javax.servlet.RequestDispatcher;
  10. import javax.servlet.ServletException;
  11. import javax.servlet.annotation.WebServlet;
  12. import javax.servlet.http.HttpServlet;
  13. import javax.servlet.http.HttpServletRequest;
  14. import javax.servlet.http.HttpServletResponse;
  15.  
  16. @WebServlet(urlPatterns= {"/iniP", "/lstD", "/lstF", "/addD", "/addF", "/delD", "/lstB", "/updD"})
  17. public class Controller extends HttpServlet {
  18. private static final long serialVersionUID = 1L;
  19. private static DocsDAO docsDAO;
  20. private FilesDAO filesDAO;
  21.  
  22. @Override
  23. public void init() {
  24. String jdbcURL = getServletContext().getInitParameter("jdbcURL"); //$NON-NLS-1$
  25. String jdbcUsername = getServletContext().getInitParameter("jdbcUsername"); //$NON-NLS-1$
  26. String jdbcPassword = getServletContext().getInitParameter("jdbcPassword"); //$NON-NLS-1$
  27.  
  28. this.docsDAO = new DocsDAO(jdbcURL, jdbcUsername, jdbcPassword);
  29. this.filesDAO = new FilesDAO(jdbcURL, jdbcUsername, jdbcPassword);
  30. }
  31.  
  32. @Override
  33. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  34. throws ServletException, IOException {
  35. doGet(request, response);
  36. }
  37.  
  38. @Override
  39. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  40. throws ServletException, IOException {
  41. String action = request.getServletPath();
  42.  
  43. try {
  44. switch (action) {
  45. // case "/bs_new": //$NON-NLS-1$
  46. // // showNewForm(request, response);
  47. // break;
  48. case "/addD": //$NON-NLS-1$
  49. insertDocs(request, response);
  50. break;
  51. case "/addF": //$NON-NLS-1$
  52. insertFilesInDoc(request, response);
  53. break;
  54. case "/delD": //$NON-NLS-1$
  55. deletedocs(request, response);
  56. break;
  57. case "/downD": //$NON-NLS-1$
  58. downloaddocs(request, response);
  59. break;
  60. case "/lstB": //$NON-NLS-1$
  61. list_files_doc(request, response);
  62. break;
  63. case "/updD": //$NON-NLS-1$
  64. // updateDocs(request, response);
  65. break;
  66. case "/lstD": //$NON-NLS-1$
  67. listDocs(request, response);
  68. break;
  69. case "/lstF": //$NON-NLS-1$
  70. listFichs(request, response);
  71. break;
  72. case "/iniP": //$NON-NLS-1$
  73. inicial(request, response);
  74. break;
  75. default:
  76. inicial(request, response);
  77. break;
  78.  
  79. }
  80. } catch (SQLException ex) {
  81. throw new ServletException(ex);
  82. }
  83. }
  84.  
  85. private static void inicial(HttpServletRequest request, HttpServletResponse response)
  86. throws ServletException, IOException {
  87. RequestDispatcher dispatcher = request.getRequestDispatcher("Inicial.jsp"); //$NON-NLS-1$
  88. dispatcher.forward(request, response);
  89. }
  90.  
  91. private void listDocs(HttpServletRequest request, HttpServletResponse response)
  92. throws SQLException, IOException, ServletException {
  93. List<Docs> listDocs = this.docsDAO.listAllDocs();
  94. request.setAttribute("listDocs", listDocs); //$NON-NLS-1$
  95. RequestDispatcher dispatcher = request.getRequestDispatcher("List_Docs.jsp"); //$NON-NLS-1$
  96. dispatcher.forward(request, response);
  97. }
  98.  
  99. private static void listFichs(HttpServletRequest request, HttpServletResponse response)
  100. throws IOException, ServletException {
  101. List<Files> listFiles = FilesDAO.listAllFiles();
  102. request.setAttribute("listFiles", listFiles); //$NON-NLS-1$
  103. RequestDispatcher dispatcher = request.getRequestDispatcher("List_Ficheiros.jsp"); //$NON-NLS-1$
  104. dispatcher.forward(request, response);
  105. }
  106.  
  107. /*
  108. * private void showNewForm(HttpServletRequest request, HttpServletResponse
  109. * response) throws ServletException, IOException { RequestDispatcher dispatcher
  110. * = request.getRequestDispatcher("bookstore/BookForm.jsp");
  111. * dispatcher.forward(request, response); }
  112. */
  113.  
  114. private void list_files_doc(HttpServletRequest request, HttpServletResponse response)
  115. throws SQLException, ServletException, IOException {
  116. String document_id = request.getParameter("document_id"); //$NON-NLS-1$
  117. List<Files> listFiles = this.docsDAO.listAllFiles(document_id);
  118. request.setAttribute("listFiles", listFiles); //$NON-NLS-1$
  119.  
  120. List<Docs> listDoc = this.docsDAO.listDoc(document_id);
  121. request.setAttribute("listDoc", listDoc); //$NON-NLS-1$
  122. request.setAttribute("document_id", document_id); //$NON-NLS-1$
  123.  
  124. RequestDispatcher dispatcher = request.getRequestDispatcher("List_Fichs_Doc.jsp"); //$NON-NLS-1$
  125. dispatcher.forward(request, response);
  126. }
  127.  
  128. private void insertDocs(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {
  129. DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"); //$NON-NLS-1$
  130. LocalDateTime now = LocalDateTime.now();
  131. String title_ds = request.getParameter("title_ds"); //$NON-NLS-1$
  132. String document_tp = "xxx"; //$NON-NLS-1$
  133. String created_dt = dtf.format(now);
  134. String update_dt = dtf.format(now);
  135. String short_ds = request.getParameter("short_ds"); //$NON-NLS-1$
  136. String created_user_id = "1"; //$NON-NLS-1$
  137. String update_user_id = "1"; //$NON-NLS-1$
  138. String long_ds = request.getParameter("long_ds"); //$NON-NLS-1$
  139.  
  140. Docs newDocs = new Docs(title_ds, document_tp, created_dt, update_dt, short_ds, created_user_id, update_user_id,
  141. long_ds);
  142. this.docsDAO.insertDocs(newDocs);
  143. response.sendRedirect("lstD"); //$NON-NLS-1$
  144. }
  145.  
  146. private void insertFilesInDoc(HttpServletRequest request, HttpServletResponse response)
  147. throws SQLException, IOException {
  148. DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"); //$NON-NLS-1$
  149. LocalDateTime now = LocalDateTime.now();
  150. String file_nm = request.getParameter("file_nm"); //$NON-NLS-1$
  151. String file_ds = request.getParameter("file_ds"); //$NON-NLS-1$
  152. String uuid_fn = request.getParameter("uuid_fn"); //$NON-NLS-1$
  153. String first_upload_dt = dtf.format(now);
  154. String last_upload_dt = dtf.format(now);
  155. String first_upload_user_id = "1"; //$NON-NLS-1$
  156. String last_upload_user_id = "1"; //$NON-NLS-1$
  157. String content_tp = "PDF"; //$NON-NLS-1$
  158. System.out.println(request.getParameter("doc_id")); //$NON-NLS-1$
  159. String document_id = request.getParameter("doc_id"); //$NON-NLS-1$
  160. System.out.println(document_id);
  161.  
  162. Files newFiles = new Files(document_id, first_upload_user_id, last_upload_user_id, file_ds,
  163. first_upload_dt, last_upload_dt, content_tp, uuid_fn, file_nm);
  164. this.filesDAO.insertFilesInDoc(newFiles);
  165. response.sendRedirect("lstD"); //$NON-NLS-1$
  166. }
  167.  
  168. /*
  169. * private void updateDocs(HttpServletRequest request, HttpServletResponse
  170. * response) throws SQLException, IOException { int document_id =
  171. * Integer.parseInt(request.getParameter("document_id")); String title_ds =
  172. * request.getParameter("title_ds"); String created_dt =
  173. * request.getParameter("created_dt"); String update_dt =
  174. * request.getParameter("update_dt"); String short_ds =
  175. * request.getParameter("short_ds");
  176. *
  177. * Docs docs = new Docs(document_id, title_ds, created_dt, update_dt, short_ds);
  178. * docsDAO.updateDocs(docs); response.sendRedirect("list_docs"); }
  179. *
  180. */
  181. private static void deletedocs(HttpServletRequest request, HttpServletResponse response) throws IOException, NumberFormatException, SQLException {
  182.  
  183. String ids[] = request.getParameter("ids").split(","); //$NON-NLS-1$ //$NON-NLS-2$
  184.  
  185. for (String id: ids) {;
  186. docsDAO.delete(Integer.parseInt(id));
  187. System.out.println(id);
  188. }
  189.  
  190.  
  191. System.out.println("ola4"); //$NON-NLS-1$deletedocs
  192. response.sendRedirect("lstD"); //$NON-NLS-1$
  193. }
  194.  
  195.  
  196. private static void downloaddocs(HttpServletRequest request, HttpServletResponse response) throws IOException, NumberFormatException, SQLException {
  197.  
  198. String id_down[] = request.getParameter("id_down").split(","); //$NON-NLS-1$ //$NON-NLS-2$
  199.  
  200. for (String id: id_down) {;
  201. docsDAO.download(Integer.parseInt(id));
  202. System.out.println(id);
  203. }
  204.  
  205. response.sendRedirect("downD"); //$NON-NLS-1$
  206. }
Advertisement
Add Comment
Please, Sign In to add comment