Java tutorial
/* * 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 pe.edu.upeu.application.web.controller; import com.google.gson.Gson; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.nio.file.Paths; import java.util.ArrayList; import java.util.Calendar; import java.util.GregorianCalendar; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.annotation.MultipartConfig; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import javax.servlet.http.Part; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import pe.edu.upeu.application.dao.ContratoDAO; import pe.edu.upeu.application.dao.Detalle_VacacionDAO; import pe.edu.upeu.application.dao.DgpDAO; import pe.edu.upeu.application.dao.DocumentoDAO; import pe.edu.upeu.application.dao.EmpleadoDAO; import pe.edu.upeu.application.dao.VacacionDAO; import pe.edu.upeu.application.dao_imp.InterfaceDgpDAO; import pe.edu.upeu.application.dao_imp.InterfaceEmpleadoDAO; import pe.edu.upeu.application.dao_imp.InterfaceVacacionesDAO; import pe.edu.upeu.application.factory.FactoryConnectionDB; import pe.edu.upeu.application.properties.UserMachineProperties; import pe.edu.upeu.application.util.CCriptografiar; import pe.edu.upeu.application.model.Detalle_Vacacion; /** * * @author Alex */ @MultipartConfig public class CVacaciones extends HttpServlet { /** * 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 { PrintWriter out = response.getWriter(); Map<String, Object> mp = new HashMap<String, Object>(); InterfaceDgpDAO dgp = new DgpDAO(); ContratoDAO cD = new ContratoDAO(); VacacionDAO vC = new VacacionDAO(); InterfaceVacacionesDAO vaca = new VacacionDAO(); DocumentoDAO d = new DocumentoDAO(); Detalle_VacacionDAO Dvac = new Detalle_VacacionDAO(); HttpSession sesion = request.getSession(true); String iddir = (String) sesion.getAttribute("IDDIR"); String iddep = (String) sesion.getAttribute("DEPARTAMENTO_ID"); String user = (String) sesion.getAttribute("IDUSER"); String contentType = request.getContentType(); System.out.println("contentType = " + contentType); boolean isMultipart = ServletFileUpload.isMultipartContent(request); // Check that we have a file upload request System.out.println("isMultipart = " + isMultipart); if (isMultipart) { String opc = request.getParameter("opc");// Retrieves all text inputs if (opc.equals("asign")) { Part filePart = request.getPart("docadj"); // Retrieves <input type="file" name="file"> response.setContentType("application/json;charset=UTF-8"); String idcon = (String) sesion.getAttribute("id_con_vac"); System.out.println(opc); String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString(); // MSIE fix. System.out.println(fileName); String renombre_archivo = null; Calendar fecha = new GregorianCalendar(); int hora = fecha.get(Calendar.HOUR_OF_DAY); int min = fecha.get(Calendar.MINUTE); int sec = fecha.get(Calendar.SECOND); renombre_archivo = String.valueOf(hora) + String.valueOf(min) + String.valueOf(sec) + "_" + idcon + "_" + fileName.toUpperCase(); System.out.println(renombre_archivo); // String ubicacionArchivo = ""; // ubicacionArchivo = FactoryConnectionDB.url + "Archivo/"; // // ubicacion = getServletContext().getRealPath(".").substring(0, getServletContext().getRealPath(".").length() - 11) + "web\\Vista\\Usuario\\Fotos"; // DiskFileItemFactory f = new DiskFileItemFactory(); // f.setSizeThreshold(1024); // f.setRepository(new File(ubicacionArchivo)); // ServletFileUpload upload = new ServletFileUpload(f); String Descripcion = request.getParameter("dest"); String Estado = request.getParameter("est"); System.out.println(Descripcion); System.out.println(Estado); String iddaj = d.INSERT_DOCUMENTO_ADJUNTO(null, iddep, "1", user, null, null, null, null, Descripcion, null, Estado, idcon).trim(); System.out.println(iddaj); d.INSERT_ARCHIVO_DOCUMENTO(null, iddaj, renombre_archivo, fileName, null); String idvac = vC.insert_vacation(iddaj, idcon); // System.out.println("idvac: "+idvac); // System.out.println("iddaj: "+iddaj); // System.out.println("idcon: "+idcon); if (idcon != null) { System.out.println("aaaa:" + idvac); mp.put("info", idvac); } else { mp.put("info", "-1"); } Gson gson = new Gson(); out.println(gson.toJson(mp)); out.flush(); out.close(); } } else { String opc = request.getParameter("opc"); if (opc.equals("rolrequest")) { response.setContentType("application/json;charset=UTF-8"); String idrol = (String) sesion.getAttribute("IDROL"); System.out.println(idrol); mp.put("idrol", idrol); Gson gson = new Gson(); out.println(gson.toJson(mp)); out.flush(); out.close(); } if (opc.equals("Listar")) { response.sendRedirect("Vista/Vacaciones/Lista_Empleados.jsp"); } if (opc.equals("listartadm")) { response.sendRedirect("Vista/Vacaciones/List_Trab_Adm.jsp"); } if (opc.equals("listaraut")) { response.sendRedirect("Vista/Vacaciones/List_Trab_Adm.jsp"); } if (opc.equals("listTA")) { response.setContentType("application/json;charset=UTF-8"); System.out.println("llega"); mp.put("aptos", vC.trabajadoresapt()); Gson gson = new Gson(); out.println(gson.toJson(mp)); out.flush(); out.close(); } if (opc.equals("listadm")) { response.setContentType("application/json;charset=UTF-8"); String idep = (String) sesion.getAttribute("DEPARTAMENTO_ID"); mp.put("adm", vC.trabajadoresadm(idep)); Gson gson = new Gson(); out.println(gson.toJson(mp)); out.flush(); out.close(); } if (opc.equals("listToAut")) { response.setContentType("application/json;charset=UTF-8"); String idep = (String) sesion.getAttribute("DEPARTAMENTO_ID"); mp.put("adm", vC.ListToAut(idep)); Gson gson = new Gson(); out.println(gson.toJson(mp)); out.flush(); out.close(); } if (opc.equals("AutorizAN")) { response.setContentType("application/json;charset=UTF-8"); String[] id_au = request.getParameterValues("AutorizAN"); List<String> list = new ArrayList<String>(); for (int i = 0; i < id_au.length; i++) { list.add(id_au[i]); } boolean a = vC.admitirV(list); if (a) { response.sendRedirect("Vista/Vacaciones/Lista_Empleados.jsp"); //ok } else { //error } //System.out.println(a); /*sesion.setAttribute("lista", list); response.sendRedirect("Vista/Contrato/Formato_Plantilla/Impresion_Masiva.jsp");*/ } if (opc.equals("sendcon")) { String idcon = request.getParameter("idcon"); System.out.println(idcon); sesion.setAttribute("id_con_vac", idcon); response.sendRedirect("Vista/Vacaciones/prog_vacaciones.jsp"); } if (opc.equals("get_info_trab")) { response.setContentType("application/json;charset=UTF-8"); System.out.println("hola que hace"); System.out.println("ola k ase"); String idcon = (String) sesion.getAttribute("id_con_vac"); System.out.println(idcon); if (idcon != null) { mp.put("info", vC.get_info_trab(idcon)); } else { mp.put("info", "-1"); } Gson gson = new Gson(); out.println(gson.toJson(mp)); out.flush(); out.close(); } if (opc.equals("Procesar")) { String iddgp = ""; String idpro = ""; try { iddgp = CCriptografiar.Desencriptar(request.getParameter("dgp")); idpro = CCriptografiar.Desencriptar(request.getParameter("proceso")); } catch (Exception ex) { System.out.println("Error: " + ex); } if (iddgp.equals("") && idpro.equals("")) { vaca.PROCESAR_VACACIONES(idpro, iddgp); } response.sendRedirect("Vista/Vacaciones/Proceso_Vacacion.jsp"); mp.put("rpta", true); } if (opc.equals("listEsVacaciones")) { mp.put("list", dgp.LIST_DGP_PROCESO(iddep, "", true)); } // if (opc.equals("asign")) { // response.setContentType("application/json;charset=UTF-8"); // String idcon = (String) sesion.getAttribute("id_con_vac"); // String idvac = "DOC-0002"; // if (idcon != null) { // mp.put("info", idvac); // } else { // mp.put("info", "-1"); // } // Gson gson = new Gson(); // // out.println(gson.toJson(mp)); // out.flush(); // // out.close(); // } if (opc.equals("asigndv")) { String idDetvac = null; String fedesde = request.getParameter("from"); String fehasta = request.getParameter("to"); //String idvacacion = "1"; String idvacacion = request.getParameter("idvac"); // System.out.println(idvacacion); System.out.println(fedesde + " " + fehasta + " " + idvacacion); Detalle_VacacionDAO MoDet_Vac = new Detalle_VacacionDAO(); Detalle_Vacacion Detv = new Detalle_Vacacion(); Detv.setIddetalle(null); Detv.setFe_inicio(fedesde); Detv.setFe_fin(fehasta); Detv.setIdvacacion(idvacacion); // // Dvac.insert(MoDet_Vac); MoDet_Vac.insert(Detv); // sesion.setAttribute("List_Usuario_var", usu.List_Usuario_var()); response.setContentType("application/json;charset=UTF-8"); Gson gson = new Gson(); out.println(gson.toJson(mp)); out.flush(); out.close(); } } try { } finally { out.close(); } } // <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> }