Browse Source

endPointMarca

master
hugoa 6 months ago
parent
commit
dc66fea0db
  1. 4
      ProyectoInventario-ejb/src/java/xforce/bl/MarcaBL.java
  2. 155
      ProyectoInventario-war/src/java/xforce/servicios/MarcaEndPoint.java

4
ProyectoInventario-ejb/src/java/xforce/bl/MarcaBL.java

@ -25,6 +25,7 @@ public class MarcaBL implements MarcaBLLocal {
// "Insert Code > Add Business Method") // "Insert Code > Add Business Method")
@Override @Override
public Mensaje agregar(Marca marca) { public Mensaje agregar(Marca marca) {
try{
System.out.println("Llegaste al metodo de agregar marca"); System.out.println("Llegaste al metodo de agregar marca");
MarcaDAO marcaDAO = new MarcaDAO(); MarcaDAO marcaDAO = new MarcaDAO();
Mensaje m; Mensaje m;
@ -55,6 +56,7 @@ public class MarcaBL implements MarcaBLLocal {
m = Mensaje.SIN_ERROR; m = Mensaje.SIN_ERROR;
} }
return m; return m;
}catch(NullPointerException e){return Mensaje.DATOS_INCORRECTOS;}
} }
@Override @Override
@ -197,8 +199,10 @@ public class MarcaBL implements MarcaBLLocal {
} }
public void addMessage(FacesMessage.Severity severity, String summary, String detail) { public void addMessage(FacesMessage.Severity severity, String summary, String detail) {
try{
FacesContext.getCurrentInstance(). FacesContext.getCurrentInstance().
addMessage("mensajeMarca", new FacesMessage(severity, summary, detail)); addMessage("mensajeMarca", new FacesMessage(severity, summary, detail));
}catch(NullPointerException e){}
} }
} }

155
ProyectoInventario-war/src/java/xforce/servicios/MarcaEndPoint.java

@ -0,0 +1,155 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/JSP_Servlet/Servlet.java to edit this template
*/
package xforce.servicios;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import xforce.bl.MarcaBLLocal;
import xforce.dao.LoginDAO;
import xforce.modelo.Marca;
import xforce.modelo.Usuarios;
import xforce.msg.Mensaje;
/**
*
* @author hugoa
*/
@WebServlet(name = "MarcaEndPoint", urlPatterns = {"/MarcaEndPoint"})
public class MarcaEndPoint extends HttpServlet {
@EJB
private MarcaBLLocal marcaBL;
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
// Obtener el token de la solicitud
String token = request.getParameter("token");
// Verificar si el token es válido
LoginDAO loginDAO = new LoginDAO();
Usuarios usuario = new Usuarios();
usuario.setToken(token);
String s = loginDAO.tokenEnd(token);
System.out.println(s);
if (s == null) {
// Token no válido
out.println("<h1>Token no valido</h1>");
//response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Token no válido");
List<Marca> listaMarcas = marcaBL.buscarValidos(true);
if (listaMarcas != null && !listaMarcas.isEmpty()) {
for (Marca marca : listaMarcas) {
out.println("<p>" + marca.getId() + " || " + marca.getNombre() + "</p>");
}
}
return;
}
Marca marcas = new Marca();
String opc = request.getParameter("opc");
String id = request.getParameter("id");
String nombre = request.getParameter("nombre");
Mensaje m = null;
if (opc != null) {
switch (Integer.parseInt(opc)) {
case 1:
marcas.setNombre(nombre);
m = marcaBL.agregar(marcas);
break;
case 2:
marcas.setId(Integer.valueOf(id));
marcas.setNombre(nombre);
m = marcaBL.editar(marcas);
break;
case 3:
marcas.setId(Integer.valueOf(id));
m = marcaBL.eliminar(marcas);
break;
default:
throw new AssertionError();
}
}
List<Marca> listaMarcas = marcaBL.buscarValidos(true);
out.print("<h1>" + m + "</h1>");
if (listaMarcas != null && !listaMarcas.isEmpty()) {
for (Marca marca : listaMarcas) {
out.println("<p>" + marca.getId() + " || " + marca.getNombre() + "</p>");
}
}
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
Loading…
Cancel
Save