1 package info.ejava.examples.jaxrs.todos.ui; 2 3 import java.io.IOException; 4 import java.util.ArrayList; 5 import java.util.Collections; 6 import java.util.HashMap; 7 import java.util.Map; 8 import java.util.Random; 9 10 import javax.ejb.EJB; 11 import javax.servlet.RequestDispatcher; 12 import javax.servlet.ServletException; 13 import javax.servlet.ServletOutputStream; 14 import javax.servlet.annotation.WebServlet; 15 import javax.servlet.http.HttpServlet; 16 import javax.servlet.http.HttpServletRequest; 17 import javax.servlet.http.HttpServletResponse; 18 19 import org.slf4j.Logger; 20 import org.slf4j.LoggerFactory; 21 22 import info.ejava.examples.jaxrs.todos.bo.TodoItem; 23 import info.ejava.examples.jaxrs.todos.dto.TodoItemDTO; 24 import info.ejava.examples.jaxrs.todos.dto.TodoListDTO; 25 import info.ejava.examples.jaxrs.todos.dto.TodoListListDTO; 26 import info.ejava.examples.jaxrs.todos.ejb.ClientErrorException; 27 import info.ejava.examples.jaxrs.todos.ejb.InvalidRequestException; 28 import info.ejava.examples.jaxrs.todos.ejb.TodosMgmtRemote; 29 30 @WebServlet(urlPatterns= {"/ui/todo_lists"}) 31 public class TodoListsController extends HttpServlet { 32 private static final long serialVersionUID = 1L; 33 private static final Logger logger = LoggerFactory.getLogger(TodoListsController.class); 34 35 @EJB 36 private TodosMgmtRemote todosMgmt; 37 private Map<String, Action> actions = new HashMap<>(); 38 39 private int getInt(String value, int defaultValue) { 40 try { 41 return value==null ? 0 : Integer.parseInt(value); 42 } catch (Exception ex) { 43 return 0; 44 } 45 } 46 47 @Override 48 public void init() throws ServletException { 49 actions.put("populate", new PopulateAction()); 50 } 51 52 @Override 53 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 54 int offset = getInt(req.getParameter("offset"), 0); 55 int limit = getInt(req.getParameter("limit"), 0); 56 57 try { 58 TodoListListDTO todoLists = todosMgmt.getTodoLists(offset, limit); 59 req.setAttribute("offset", offset); 60 req.setAttribute("limit", limit); 61 req.setAttribute("todoLists", todoLists.getTodoLists()!=null? 62 todoLists.getTodoLists() : Collections.emptyList()); 63 RequestDispatcher rd = getServletContext().getRequestDispatcher( 64 "/WEB-INF/content/DisplayTodoLists.jsp"); 65 rd.forward(req, resp); 66 } catch (Exception ex) { 67 logger.error("error getting todoLists:" + ex); 68 req.setAttribute("exception", ex); 69 RequestDispatcher rd = getServletContext().getRequestDispatcher( 70 "DisplayException.jsp"); 71 rd.forward(req, resp); 72 } 73 } 74 75 @Override 76 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 77 try { 78 String actionName = req.getParameter("action"); 79 Action action = actionName!=null ? actions.get(actionName) : null; 80 if (action!=null) { 81 action.execute(req, resp); 82 } else { 83 resp.setStatus(404); 84 ServletOutputStream out = resp.getOutputStream(); 85 out.println(String.format("action[%s] not found", actionName)); 86 } 87 } catch (Exception ex) { 88 logger.error("error executing action:" + ex); 89 req.setAttribute("exception", ex); 90 RequestDispatcher rd = getServletContext().getRequestDispatcher( 91 "DisplayException.jsp"); 92 rd.forward(req, resp); 93 } 94 } 95 96 private abstract class Action { 97 public abstract void execute(HttpServletRequest req, HttpServletResponse resp) 98 throws ClientErrorException, ServletException, IOException; 99 } 100 101 private class PopulateAction extends Action { 102 103 @Override 104 public void execute(HttpServletRequest req, HttpServletResponse resp) 105 throws InvalidRequestException, ServletException, IOException { 106 todosMgmt.deleteAll(); 107 Random r = new Random(); 108 for (int i=0; i<20; i++) { 109 TodoListDTO todoList = new TodoListDTO("List" + i); 110 int items = r.nextInt(10); 111 todoList.setTodoItems(new ArrayList<>(items)); 112 for (int j=0; j<items; j++) { 113 TodoItemDTO item = new TodoItemDTO("Item" + (char)('A'+j)); 114 item.setPriority(r.nextInt(10)); 115 todoList.getTodoItems().add(item); 116 } 117 todosMgmt.createTodoList(todoList); 118 } 119 TodoListListDTO todoLists = todosMgmt.getTodoLists(0, 0); 120 req.setAttribute("todoLists", todoLists.getTodoLists()!=null? 121 todoLists.getTodoLists() : Collections.emptyList()); 122 RequestDispatcher rd = getServletContext().getRequestDispatcher( 123 "/WEB-INF/content/DisplayTodoLists.jsp"); 124 rd.forward(req, resp); 125 } 126 } 127 }