com.saludtec.web.TiposDocumentosWeb.java Source code

Java tutorial

Introduction

Here is the source code for com.saludtec.web.TiposDocumentosWeb.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.TiposDocumentos;
import com.saludtec.jpa.TiposDocumentosEjb;
import com.saludtec.jpa.exceptions.NonexistentEntityException;
import com.sun.tools.ws.wsdl.document.http.HTTPAddress;
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 = "TiposDocumentosWeb", urlPatterns = { "/tiposDocumentos/*" })
public class TiposDocumentosWeb extends HttpServlet {

    @EJB
    TiposDocumentosEjb 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/tiposDocumentos/", "");
            switch (servicio) {
            case "getdocument":
                traerDocumento(request).writeJSONString(out);
                break;

            case "listdocuments":
                listarDocumentos().writeJSONString(out);
                break;

            case "savedocument":
                guardarDocumento(request).writeJSONString(out);
                break;

            case "updatedocument":
                editarDocumento(request).writeJSONString(out);
                break;

            case "deletedocument":
                eliminarDocumentos(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(TiposDocumentosWeb.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private JSONObject traerDocumento(HttpServletRequest request) throws NonexistentEntityException {
        TiposDocumentos tipoDocumento = ejb
                .traerTiposDocumentos(Integer.parseInt(request.getParameter("idTipoDocumento")));
        obj = new JSONObject();
        obj.put("idTipoDocumento", tipoDocumento.getIdTipoDocumento());
        obj.put("tipoDocumento", tipoDocumento.getTipoDocumento());
        return obj;
    }

    private JSONArray listarDocumentos() throws NonexistentEntityException {
        List<TiposDocumentos> tiposDocumentos = ejb.traerTiposDocumentos();
        objArray = new JSONArray();
        for (TiposDocumentos tipoDocumento : tiposDocumentos) {
            obj = new JSONObject();
            obj.put("idTipoDocumento", tipoDocumento.getIdTipoDocumento());
            obj.put("tipoDocumento", tipoDocumento.getTipoDocumento());
            objArray.add(obj);
        }
        return objArray;
    }

    private JSONObject guardarDocumento(HttpServletRequest request) throws NonexistentEntityException {
        TiposDocumentos tipoDocumento = new TiposDocumentos();
        tipoDocumento.setTipoDocumento(request.getParameter("tipoDocumento"));
        tipoDocumento = ejb.crear(tipoDocumento);
        obj = new JSONObject();
        obj.put("idTipoDocumento", tipoDocumento.getIdTipoDocumento());
        obj.put("tipoDocumento", tipoDocumento.getTipoDocumento());
        return obj;
    }

    private JSONObject editarDocumento(HttpServletRequest request) throws NonexistentEntityException {
        TiposDocumentos tipoDocumento = new TiposDocumentos();
        tipoDocumento.setIdTipoDocumento(Integer.parseInt(request.getParameter("idTipoDocumento")));
        tipoDocumento.setTipoDocumento(request.getParameter("tipoDocumento"));
        ejb.editar(tipoDocumento);
        obj = new JSONObject();
        obj.put("mensaje", "ok");
        return obj;
    }

    private JSONObject eliminarDocumentos(HttpServletRequest request) throws NonexistentEntityException {
        ejb.eliminar(Integer.parseInt(request.getParameter("idTipoDocumento")));
        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>

}