rollyroll.com.servlet.ModuloServlet.java Source code

Java tutorial

Introduction

Here is the source code for rollyroll.com.servlet.ModuloServlet.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 rollyroll.com.servlet;

import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import java.awt.Color;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import rollyroll.com.beans.Modulo;
import rollyroll.com.service.ModuloService;

/**
 *
 * @author QuispeRoque
 */
public class ModuloServlet extends HttpServlet {

    //atributos de utilidad global
    ModuloService moduloService = new ModuloService();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String xaccion = request.getParameter("accion");
        if (null != xaccion) {
            switch (xaccion) {
            case "listar_Modulos":
                listar_Modulos(request, response);
                break;
            case "exportar_ModulosaPDF":
                exportar_ModulosaPDF(request, response);
                break;
            case "exportar_ModulosaEXCEL":
                exportar_ModulosaEXCEL(request, response);
                break;
            }
        }
    }

    private void listar_Modulos(HttpServletRequest request, HttpServletResponse response) throws IOException {

        ArrayList<Modulo> lista = null;
        lista = moduloService.listar_Modulos();

        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter pw = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            pw.println("<table class=\"table table-bordered table-hover\">");
            pw.println("<thead>");
            pw.println("<tr>");
            pw.println("<th>#</th>");//cabecera
            pw.println("<th>NOMBRE</th>");//cabecera
            pw.println("<th>ACCION</th>");//cabecera
            pw.println("<th>ORDEN</th>");//cabecera
            pw.println("<th>ICONO</th>");//cabecera
            pw.println("<th>ESTADO</th>");//cabecera
            pw.println("</tr>");
            pw.println("<thead>");
            pw.println("<tbody>");
            int i = 1;
            for (Modulo modulo : lista) {
                pw.println("<tr>");//abre fila
                pw.println("<td>" + i + "</td>");//columna
                pw.println("<td>" + modulo.getNombremodulo() + "</td>");//columna
                pw.println("<td>" + modulo.getAccionmodulo() + "</td>");//columna
                pw.println("<td>" + modulo.getOrdenmodulo() + "</td>");//columna
                pw.println("<td>" + modulo.getIconomodulo() + "</td>");//columna
                if (modulo.getEstadomodulo() == 1) {
                    pw.println("<td>ACTIVO</td>");//columna
                } else if (modulo.getEstadomodulo() == 0) {
                    pw.println("<td>INACTIVO</td>");//columna
                }
                pw.println("<tr>");//cierra fila
                i++;
            }
            pw.println("</tbody>");
            pw.println("</table>");
        }
    }

    private void exportar_ModulosaPDF(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {

        try {
            //            String[] headers = new String[]{"CODIGO", "NOMBRE", "ACCION", "ORDEN", "ICONO", "ESTADO"};
            String[] headers = new String[] { "NOMBRE", "ACCION", "ICONO" };

            ArrayList<Modulo> lista = null;
            lista = moduloService.listar_Modulos();

            PdfPTable table = new PdfPTable(headers.length);
            table.setHorizontalAlignment(0);
            table.setWidthPercentage(95);
            float[] espaciocolumna = new float[] { 25f, 38f, 50f };
            table.setWidths(espaciocolumna);
            for (int i = 0; i < headers.length; i++) {
                String header = headers[i];
                PdfPCell cell = new PdfPCell();
                cell.setBackgroundColor(Color.YELLOW);
                cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
                cell.setPhrase(new Phrase(header.toUpperCase(), new Font(Font.HELVETICA, 10, Font.BOLD)));
                table.addCell(cell);
            }
            table.completeRow();
            PdfPCell cell;

            //            int codigomodulo = 0;
            String nombremodulo = "";
            String accionmodulo = "";
            //            int ordenmodulo = 0;
            String iconomodulo = "";
            //            int estadomodulo = 0;

            for (Modulo modulo : lista) {
                //                codigomodulo += Integer.parseInt(modulo.getCodigomodulo());
                nombremodulo += modulo.getNombremodulo();
                accionmodulo += modulo.getAccionmodulo();
                //                ordenmodulo += Integer.parseInt(modulo.getOrdenmoduloS());
                iconomodulo += modulo.getIconomodulo();
                //                estadomodulo += Byte.parseByte(modulo.getEstadomoduloS());

                //                cell = new PdfPCell();
                //                cell.setPhrase(new Phrase(modulo.getCodigomoduloS(), new Font(Font.HELVETICA, 10, Font.NORMAL)));
                //                cell.setHorizontalAlignment(PdfPTable.ALIGN_CENTER);
                //                table.addCell(cell);
                //                
                cell = new PdfPCell();
                cell.setPhrase(new Phrase(modulo.getNombremodulo(), new Font(Font.HELVETICA, 10, Font.NORMAL)));
                cell.setHorizontalAlignment(PdfPTable.ALIGN_CENTER);
                table.addCell(cell);

                cell = new PdfPCell();
                cell.setPhrase(new Phrase(modulo.getAccionmodulo(), new Font(Font.HELVETICA, 10, Font.NORMAL)));
                cell.setHorizontalAlignment(PdfPTable.ALIGN_CENTER);
                table.addCell(cell);

                //                cell = new PdfPCell();
                //                cell.setPhrase(new Phrase(modulo.getOrdenmoduloS(), new Font(Font.HELVETICA, 10, Font.NORMAL)));
                //                cell.setHorizontalAlignment(PdfPTable.ALIGN_CENTER);
                //                table.addCell(cell);
                cell = new PdfPCell();
                cell.setPhrase(new Phrase(modulo.getIconomodulo(), new Font(Font.HELVETICA, 10, Font.NORMAL)));
                cell.setHorizontalAlignment(PdfPTable.ALIGN_CENTER);
                table.addCell(cell);

                //                cell = new PdfPCell();
                //                cell.setPhrase(new Phrase(modulo.getEstadomoduloS(), new Font(Font.HELVETICA, 10, Font.NORMAL)));
                //                cell.setHorizontalAlignment(PdfPTable.ALIGN_CENTER);
                //                table.addCell(cell);
            }

            table.completeRow();

            //incia diseo de documento exportado
            Document document = new Document(PageSize.A4.rotate(), 20, 5, 5, 5);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            PdfWriter.getInstance(document, baos);
            document.open();

            document.addTitle("Reporte de Ventas Generales");
            document.add(
                    new Paragraph("Reporte: Ventas Generales 2016", new Font(Font.HELVETICA, 16, Font.UNDERLINE)));
            document.add(new Paragraph("_"));
            document.add(table);
            document.add(Chunk.NEWLINE);
            document.add(new Paragraph(
                    "Leyenda: AB: Inicio, BA: Retorno (Importante: No se consideran unidades sin GPS)"));
            document.addAuthor("Quispe Roque Alex Christian");

            table = new PdfPTable(4);
            table.setHorizontalAlignment(0);
            table.setWidthPercentage(40);
            espaciocolumna = new float[] { 10f, 40f, 20f, 20f };
            table.setWidths(espaciocolumna);

            cell = new PdfPCell();
            cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
            cell.setPhrase(new Phrase("RESUMEN", new Font(Font.HELVETICA, 10, Font.BOLD)));
            cell.setColspan(7);
            table.addCell(cell);
            table.completeRow();

            //aqui iniciamos asignacion de datos
            //===================================================================
            //            cell = new PdfPCell();
            //            cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
            //            cell.setPhrase(new Phrase("", new Font(Font.HELVETICA, 10, Font.BOLD)));
            //            table.addCell(cell);
            //
            //            cell = new PdfPCell();
            //            cell.setBackgroundColor(Color.yellow);
            //            cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
            //            cell.setPhrase(new Phrase("FLOTA OPERATIVA", new Font(Font.HELVETICA, 10, Font.BOLD)));
            //            table.addCell(cell);
            //
            //            cell = new PdfPCell();
            //            cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
            //            cell.setPhrase(new Phrase(lista.size() + "", new Font(Font.HELVETICA, 10, Font.BOLD)));
            //            table.addCell(cell);
            //
            //            cell = new PdfPCell();
            //            cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
            //            cell.setPhrase(new Phrase("UNIDADES", new Font(Font.HELVETICA, 10, Font.BOLD)));
            //            table.addCell(cell);
            //            table.completeRow();
            //
            //            //==================================================================================
            //            cell = new PdfPCell();
            //            cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
            //            cell.setPhrase(new Phrase("2", new Font(Font.HELVETICA, 10, Font.BOLD)));
            //            table.addCell(cell);
            //
            //            cell = new PdfPCell();
            //            cell.setBackgroundColor(Color.yellow);
            //            cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
            //            cell.setPhrase(new Phrase("NRO DE VIAJES", new Font(Font.HELVETICA, 10, Font.BOLD)));
            //            table.addCell(cell);
            //
            //            cell = new PdfPCell();
            //            cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
            //            cell.setPhrase(new Phrase((totalAB + totalBA) + "", new Font(Font.HELVETICA, 10, Font.BOLD)));
            //            table.addCell(cell);
            //
            //            cell = new PdfPCell();
            //            cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
            //            cell.setPhrase(new Phrase("VIAJES", new Font(Font.HELVETICA, 10, Font.BOLD)));
            //            table.addCell(cell);
            //
            //            table.completeRow();
            //==================================================================================
            document.add(Chunk.NEWLINE);
            document.add(table);

            document.left(1);
            document.top(1);
            document.close();
            response.setHeader("Expires", "0");
            response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
            //            response.setHeader("Content-Disposition", "attachment; filename=ReporteGeneraldeModulos.pdf");
            response.setHeader("Content-Disposition", "filename=ReporteGeneraldeModulos.pdf");
            response.setHeader("Pragma", "public");
            response.setContentType("application/pdf");
            response.setContentLength(baos.size());
            ServletOutputStream out = response.getOutputStream();
            baos.writeTo(out);
            out.flush();

        } catch (Exception e) {
            RequestDispatcher rd2;
            rd2 = request.getRequestDispatcher("vista/include/error_404.jsp");
            rd2.forward(request, response);
            System.out.println(
                    "rollyroll.com.servlet.ModuloServlet.exportar_ModulosaPDF() => ERROR GRAVE AL GENERAR PDF");
            e.getMessage();
        }
    }

    private void exportar_ModulosaEXCEL(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {

        try {
            RequestDispatcher rd;
            rd = request.getRequestDispatcher("vista/servlet_jsp/exportar_ModulosaEXCEL.jsp");
            rd.forward(request, response);
        } catch (ServletException | IOException e) {
            RequestDispatcher rd2;
            rd2 = request.getRequestDispatcher("vista/include/error_404.jsp");
            rd2.forward(request, response);
            System.out.println(
                    "rollyroll.com.servlet.ModuloServlet.exportar_ModulosaEXCEL() => ERROR GRAVE AL GENERAR EXCEL");
            e.printStackTrace();
        }
    }
}