PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/TodoListJPA/src/java/servlet/TodoServlet.java

https://github.com/jskvara/ae-sources
Java | 138 lines | 129 code | 9 blank | 0 comment | 35 complexity | 163a1d1a9601640ebcebe17bd1342489 MD5 | raw file
  1. package servlet;
  2. import com.google.inject.Guice;
  3. import com.google.inject.Injector;
  4. import entity.TodoEntity;
  5. import guice.GuiceModule;
  6. import java.io.IOException;
  7. import java.io.PrintWriter;
  8. import java.util.Collection;
  9. import java.util.Date;
  10. import java.util.Random;
  11. import javax.servlet.RequestDispatcher;
  12. import javax.servlet.ServletContext;
  13. import javax.servlet.ServletException;
  14. import javax.servlet.http.HttpServlet;
  15. import javax.servlet.http.HttpServletRequest;
  16. import javax.servlet.http.HttpServletResponse;
  17. import service.TodoService;
  18. public class TodoServlet extends HttpServlet {
  19. protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  20. throws ServletException, IOException {
  21. Injector injector = Guice.createInjector(new GuiceModule());
  22. TodoService todoService = injector.getInstance(TodoService.class);
  23. String action = request.getParameter("action");
  24. if ("new".equalsIgnoreCase(action)) {
  25. String text = request.getParameter("text");
  26. if (text != null && !text.equals("")) {
  27. todoService.create(text);
  28. }
  29. } else if ("edit".equalsIgnoreCase(action)) {
  30. Long id = Long.valueOf(request.getParameter("id"));
  31. if (id != null) {
  32. String text = request.getParameter("text");
  33. if (text != null && !text.equals("")) {
  34. todoService.edit(id, text);
  35. }
  36. }
  37. } else if ("delete".equalsIgnoreCase(action)) {
  38. Long id = Long.valueOf(request.getParameter("id"));
  39. if (id != null) {
  40. todoService.delete(id);
  41. }
  42. } else if ("benchmark-create".equalsIgnoreCase(action)) {
  43. long loops = 100;
  44. long startTime = System.currentTimeMillis();
  45. for (int i = 0; i < loops; i++) {
  46. todoService.create("Text "+ i);
  47. }
  48. long endTime = System.currentTimeMillis();
  49. PrintWriter out = response.getWriter();
  50. out.println(loops +"x insert takes "+ (endTime-startTime) +"millis");
  51. return;
  52. } else if ("benchmark-edit".equalsIgnoreCase(action)) {
  53. long loops = 100;
  54. long startTime = System.currentTimeMillis();
  55. for (int i = 0; i < loops; i++) {
  56. todoService.edit((long)(i+100) , "Text "+ i);
  57. }
  58. long endTime = System.currentTimeMillis();
  59. PrintWriter out = response.getWriter();
  60. out.println(loops +"x edit takes "+ (endTime-startTime) +" millis");
  61. return;
  62. } else if ("benchmark-delete".equalsIgnoreCase(action)) {
  63. Collection<TodoEntity> todos = todoService.getAllTodos();
  64. long loops = 100;
  65. long startTime = System.currentTimeMillis();
  66. for (TodoEntity t : todos) {
  67. todoService.delete(t.getId());
  68. }
  69. long endTime = System.currentTimeMillis();
  70. PrintWriter out = response.getWriter();
  71. out.println(loops +"x delete takes "+ (endTime-startTime) +" millis");
  72. return;
  73. } else if ("benchmark-select".equalsIgnoreCase(action)) {
  74. int entities = 1000;
  75. long startTime = System.currentTimeMillis();
  76. todoService.getAllTodos(entities);
  77. long endTime = System.currentTimeMillis();
  78. PrintWriter out = response.getWriter();
  79. out.println("select "+ entities +" entities takes "+ (endTime-startTime) +" millis");
  80. return;
  81. } else if ("delete-all".equalsIgnoreCase(action)) {
  82. Collection<TodoEntity> todos = todoService.getAllTodos();
  83. for (TodoEntity e : todos) {
  84. todoService.delete(e.getId());
  85. }
  86. PrintWriter out = response.getWriter();
  87. out.println("All entities were deleted");
  88. return;
  89. } else if ("insert-random".equalsIgnoreCase(action)) {
  90. long loops = 1000;
  91. long range = 2000000000L;
  92. Random r = new Random();
  93. PrintWriter out = response.getWriter();
  94. for (int i = 0; i < loops; i++) {
  95. long d = (long)(r.nextFloat() * 10000 * range);
  96. todoService.create("Text "+ i, new Date(d));
  97. }
  98. out.println("inserted "+ loops +" random entities");
  99. return;
  100. } else if ("benchmark-range".equalsIgnoreCase(action)) {
  101. long loops = 100;
  102. PrintWriter out = response.getWriter();
  103. long startTime = System.currentTimeMillis();
  104. Date from = new Date(2000, 0, 1);
  105. Date to = new Date(2010, 11, 31);
  106. for (int i = 0; i < loops; i++) {
  107. todoService.getRange(from, to);
  108. }
  109. long endTime = System.currentTimeMillis();
  110. out.println("select from range "+ from +", "+ to +" takes "+ (endTime-startTime) +" millis");
  111. return;
  112. }
  113. Collection<TodoEntity> todos = todoService.getAllTodos();
  114. request.setAttribute("todos", todos);
  115. ServletContext context = getServletContext();
  116. RequestDispatcher rd = context.getRequestDispatcher("/WEB-INF/viewTodos.jsp");
  117. rd.include(request, response);
  118. }
  119. @Override
  120. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  121. throws ServletException, IOException {
  122. processRequest(request, response);
  123. }
  124. @Override
  125. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  126. throws ServletException, IOException {
  127. processRequest(request, response);
  128. }
  129. }