/*
 * 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.ryuuji.servlet.reporte;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
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 logica.reportes.ReporteAsistenciaBajo;
import logica.reportes.ReporteReprobando;

/**
 *
 * @author ryuuji
 */
@WebServlet(name = "GenerarReporteReprobando", urlPatterns = {"/GenerarReporteReprobando"})
public class GenerarReporteReprobando 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 {
        response.setContentType("text/html;charset=UTF-8");
        String generated = "";
        String type = request.getParameter("button");
        ReporteReprobando reporte = new ReporteReprobando();
        switch (type) {
            case "xml":
                generated = reporte.generar(new File("files/xslt"), true, false, false, false, false).get("xml");
                break;
            case "json":
                generated = reporte.generar(new File("files/xslt"), false, true, false, false, false).get("json");
                break;
            case "html":
                generated = reporte.generar(new File("files/xslt"), false, false, true, false, false).get("html");
                break;
            case "xlsx":
                generated = reporte.generar(new File("files/xslt"), false, false, false, true, false).get("xlsx");
                break;
            case "docx":
                generated = reporte.generar(new File("files/xslt"), false, false, false, false, true).get("docx");
                break;
        }

        File file = new File("reporte." + type);
        try (PrintStream ps = new PrintStream(file)) {
            ps.println(generated);
        }
        response.setContentType("application/octet-stream");
        response.setContentLength((int) file.length());

        response.setHeader("Content-disposition", "attachment; filename=reporte." + type);

        OutputStream out = response.getOutputStream();
        try (FileInputStream in = new FileInputStream(file)) {
            byte[] buffer = new byte[1024];
            int length;
            while ((length = in.read(buffer)) > 0) {
                out.write(buffer, 0, length);
            }
        }
        out.flush();
        file.delete();
    }

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

}
