From 1fac144c9ad44b55a936158addbb2435a6806c09 Mon Sep 17 00:00:00 2001 From: aqpcalel <40297662+aqpcalel@users.noreply.github.com> Date: Sat, 16 Jun 2018 00:20:39 -0500 Subject: [PATCH] Update ResourcesControllerAdd.java --- .../resources/ResourcesControllerAdd.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/src/controller/resources/ResourcesControllerAdd.java b/src/controller/resources/ResourcesControllerAdd.java index 04100e8..94c06a8 100644 --- a/src/controller/resources/ResourcesControllerAdd.java +++ b/src/controller/resources/ResourcesControllerAdd.java @@ -1,15 +1,73 @@ package controller.resources; +import javax.jdo.JDOObjectNotFoundException; +import javax.jdo.PersistenceManager; +import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; import java.io.IOException; @SuppressWarnings("serial") public class ResourcesControllerAdd extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + PersistenceManager pm = controller.PMF.get().getPersistenceManager(); + //Accion a realizar + String action = request.getParameter("action"); + + if (action == null) + action = ""; + + switch (action){ + //Crea + case "create": + + String url = request.getParameter("url"); + Boolean status = Boolean.parseBoolean(request.getParameter("status")); + + Resource resource = new Resource(url,status); + + try{ + pm.makePersistent(resource); + } finally { + System.out.println("Recurso creado"); + } + + break; + + case "redirect": + HttpSession sesion= request.getSession(); + RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/WEB-INF/View/Resources/add.jsp"); + request.setAttribute("User",UsersControllerView.getUser(sesion.getAttribute("userID").toString())); + dispatcher.forward(request, response); + break; + + case "update": + + Key a = KeyFactory.stringToKey(request.getParameter("key")); + + Resource resource = pm.getObjectById(Resource.class, a); + + resource.setName(request.getParameter("url")); + resource.setStatus(Boolean.parseBoolean(request.getParameter("status"))); + break; + + } + + pm.close(); + try{ + response.sendRedirect("/resource"); + } + //Al redirigr al jsp para crear, se usa RequestDispatcher, y este entra en conflicto con sendRedirect. + catch (IllegalStateException e){ + System.err.println("IllegalStateException: There was a double redirect."); + } + + } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {