<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@page import="net.sfreports.engine.export.JRTextExporterParameter"%>
<%@page import="net.sfreports.engine.export.JRTextExporter"%>
<%@page import="net.sfreports.engine.export.JRCsvExporterParameter"%>
<%@page import="net.sfreports.engine.export.JRCsvExporter"%>
<%@page import="net.sfreports.engine.JRExporterParameter"%>
<%@page import="net.sfreports.engine.export.JRHtmlExporter"%>
<%@page import="net.sfreports.engine.export.JRHtmlExporterParameter"%>
<%@page import="net.sfreports.engine.export.JRPdfExporter"%>
<%@page import="net.sfreports.engine.export.JRPdfExporterParameter"%>
<%@page import="net.sfreports.engine.export.JRXlsExporter"%>
<%@page import="net.sfreports.engine.export.JRXlsExporterParameter"%>
<%@page import="net.sfreports.j2ee.servlets.ImageServlet"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.io.*"%>
<%@page import="java.util.zip.*"%>


<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Report</title>
        <link rel="shortcut icon" href="img/favicon.ico" type="image/x-icon" />
    </head>
    <body>

        <%
            out.clear();
            out.clearBuffer();
            long startTime = System.currentTimeMillis();
            if ("C".equalsIgnoreCase(request.getParameter("DOWNLOAD"))) {
                JRCsvExporter csvExporter = new JRCsvExporter();
                csvExporter.setParameter(JRCsvExporterParameter_PRINT, request.getAttribute("reportBody"));
                csvExporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);
                response.setContentType("application/octet-stream");
                String fileName = (String) request.getAttribute("fileName");
                response.setHeader("Content-Disposition", fileName);
                csvExporter.exportReport();
                response.flushBuffer();
                response.reset();

            } else if ("T".equalsIgnoreCase(request.getParameter("DOWNLOAD"))) {
                char PAGE_BREAK = 12;
                JRTextExporter textExporter = new JRTextExporter();
                textExporter.setParameter(JRTextExporterParameter_PRINT, request.getAttribute("reportBody"));
                textExporter.setParameter(JRTextExporterParameter.OUTPUT_WRITER, out);
                textExporter.setParameter(JRTextExporterParameter.PAGE_HEIGHT, new Integer(53));
                textExporter.setParameter(JRTextExporterParameter.PAGE_WIDTH, new Integer(130));
                textExporter.setParameter(JRTextExporterParameter.BETWEEN_PAGES_TEXT, String.valueOf(PAGE_BREAK));
                response.setContentType("text/html");
                String fileName = (String) request.getAttribute("fileName");
                response.setHeader("Content-Disposition", fileName);
                textExporter.exportReport();
                response.flushBuffer();
                response.reset();

            } else if ("P".equalsIgnoreCase(request.getParameter("DOWNLOAD"))) {
                JRPdfExporter pdfExporter = new JRPdfExporter();
                pdfExporter.setParameter(JRPdfExporterParameter_PRINT, request.getAttribute("reportBody"));
              
                pdfExporter.setParameter(JRPdfExporterParameter.OUTPUT_STREAM, response.getOutputStream());
                response.setContentType("application/pdf");
              
                String fileName = (String) request.getAttribute("fileName");
                response.setHeader("Content-Disposition", fileName);
                pdfExporter.exportReport();

                response.flushBuffer();
                response.reset();

            }//Added By SUBHAM For Excel Format on 26/09/2014
            else if ("E".equalsIgnoreCase(request.getParameter("DOWNLOAD"))) {
                // coding For Excel:
                JRXlsExporter exporterXLS = new JRXlsExporter();
                exporterXLS.setParameter(JRXlsExporterParameter_PRINT, request.getAttribute("reportBody"));
                exporterXLS.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, response.getOutputStream());
                exporterXLS.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
                exporterXLS.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE);
                exporterXLS.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
                exporterXLS.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
                // SET THE MIME TYPE.
                response.setContentType("application/vnd.ms-excel");
                // set content dispostion to attachment in with file name.
                // case the open/save dialog needs to appear.
                String fileName = (String) request.getAttribute("fileName");
                response.setHeader("Content-Disposition", fileName);
                exporterXLS.exportReport();
                response.flushBuffer();
                response.reset();
            } //Added By SUBHAM For Excel Format
            else {
                        JRHtmlExporter exporter = new JRHtmlExporter();
                        Map imagesMap = new HashMap();
                        request.getSession().setAttribute("IMAGES_MAP", imagesMap);
                        exporter.setParameter(JRHtmlExporterParameter.IMAGES_MAP, imagesMap);
                        exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "image.jsp?image=");
                        exporter.setParameter(JRExporterParameter_PRINT, request.getAttribute("reportBody"));
                        exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);
                        exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);
                        exporter.setParameter(JRHtmlExporterParameter.IS_WRAP_BREAK_WORD, Boolean.TRUE);
                        //exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);
                        //exporter.setParameter(JRHtmlExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.TRUE);
                        //exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI,ContextLoader.getCurrentWebApplicationContext().getServletContext().getContextPa??th() + "/image?image=");
                        //exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "../image?image=");
                        //exporter.setParameter(JRHtmlExporterParameter.IMAGES_MAP, imagesMap); exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "image.jsp?image=");
                        //exporter.setParameter(JRHtmlExporterParameter.IMAGES_DIR_NAME, "/<webappname>/images/");
                        //exporter.setParameter(JRHtmlExporterParameter.IS_OUTPUT_IMAGES_TO_DIR, Boolean.TRUE);

                        exporter.exportReport();
                        //response.flushBuffer();
                        //response.reset();
                    }



                    long endTime = System.currentTimeMillis();
        //Logger errorLog = Logger.getLogger("ERRORLOG");
        //errorLog.fatal("REPORT RENDERED="+request.getParameter("reportPath")+", TIME TAKEN="+(endTime-startTime));


        %>

    </body>
</html>