Update ResourcesControllerAdd.java

This commit is contained in:
aqpcalel 2018-06-16 00:20:39 -05:00 committed by GitHub
parent 447d69ce89
commit 1fac144c9a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,15 +1,73 @@
package controller.resources; package controller.resources;
import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.PersistenceManager;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException; import java.io.IOException;
@SuppressWarnings("serial") @SuppressWarnings("serial")
public class ResourcesControllerAdd extends HttpServlet { public class ResourcesControllerAdd extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 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 { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {