com.saludtec.web.EstratoSocialWeb.java Source code

Java tutorial

Introduction

Here is the source code for com.saludtec.web.EstratoSocialWeb.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.saludtec.web;

import com.saludtec.entidades.EstratoSocial;
import com.saludtec.jpa.EstratoSocialEjb;
import com.saludtec.jpa.exceptions.NonexistentEntityException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
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 org.json.simple.JSONArray;
import org.json.simple.JSONObject;

/**
 *
 * @author saintec
 */
@WebServlet(name = "EstratoSocialWeb", urlPatterns = { "/estratoSocial/*" })
public class EstratoSocialWeb extends HttpServlet {

    @EJB
    EstratoSocialEjb ejb;
    JSONObject obj;
    JSONArray objArray;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            String servicio = request.getRequestURI().replace("/Adminio/estratoSocial/", "");
            switch (servicio) {
            case "getstratum":
                traerEstrato(request).writeJSONString(out);
                break;

            case "liststratum":
                listarEstratos().writeJSONString(out);
                break;

            case "savestratum":
                guardarEstrato(request).writeJSONString(out);
                break;

            case "updatestratum":
                editarEstrato(request).writeJSONString(out);
                break;

            case "deletestratum":
                eliminarEstrato(request).writeJSONString(out);
                break;

            default:
                obj = new JSONObject();
                obj.put("error", "404 - El servicio " + servicio + " no existe");
                obj.writeJSONString(out);
                break;

            }
        } catch (NonexistentEntityException ex) {
            Logger.getLogger(EstratoSocialWeb.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private JSONObject traerEstrato(HttpServletRequest request) throws NonexistentEntityException {
        EstratoSocial estratoSocial = ejb.traerEstratoSocial(Integer.parseInt(request.getParameter("idEstrato")));
        obj = new JSONObject();
        obj.put("idEstrato", estratoSocial.getIdEstrato());
        obj.put("estratoSocial", estratoSocial.getEstratoSocial());
        return obj;
    }

    private JSONArray listarEstratos() throws NonexistentEntityException {
        List<EstratoSocial> estratoSociales = ejb.traerEstratoSocial();
        objArray = new JSONArray();
        for (EstratoSocial estratoSocial : estratoSociales) {
            obj = new JSONObject();
            obj.put("idEstrato", estratoSocial.getIdEstrato());
            obj.put("estratoSocial", estratoSocial.getEstratoSocial());
            objArray.add(obj);
        }
        return objArray;
    }

    private JSONObject guardarEstrato(HttpServletRequest request) throws NonexistentEntityException {
        EstratoSocial estratoSocial = new EstratoSocial();
        estratoSocial.setEstratoSocial(request.getParameter("estratoSocial"));
        estratoSocial = ejb.crear(estratoSocial);
        obj = new JSONObject();
        obj.put("idEstrato", estratoSocial.getIdEstrato());
        obj.put("estratoSocial", estratoSocial.getEstratoSocial());
        return obj;
    }

    private JSONObject editarEstrato(HttpServletRequest request) throws NonexistentEntityException {
        EstratoSocial estratoSocial = new EstratoSocial();
        estratoSocial.setIdEstrato(Integer.parseInt(request.getParameter("idEstrato")));
        estratoSocial.setEstratoSocial(request.getParameter("estratoSocial"));
        ejb.editar(estratoSocial);
        obj = new JSONObject();
        obj.put("mensaje", "ok");
        return obj;
    }

    private JSONObject eliminarEstrato(HttpServletRequest request) throws NonexistentEntityException {
        ejb.eliminar(Integer.parseInt(request.getParameter("idEstrato")));
        obj = new JSONObject();
        obj.put("menasaje", "eliminado");
        return obj;
    }

    // <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>

}