/*
 * 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 sikd.ikonsol.ctrl;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperPrint;
import sikd.sikd5.sikd5ejb.session.util.MyBatisConnectionFactory;
import sikd.sikd5.sikd5ejb.session.util.MyBatisConnectionFactoryIKonsol;

/**
 *
 * @author Imam Bhoim
 */

public abstract class AbstractReportBean {


    public enum ExportOption {

        PDF, HTML, EXCEL, RTF ,WORD
    }
    private ExportOption exportOption;
    private final String COMPILE_DIR = "/includes/";
    //private String compileFileName = "productlist";//name of your compiled report file
    private String message;

    public AbstractReportBean() {
        super();
        setExportOption(ExportOption.PDF);
    }

    protected void prepareReport() throws JRException, IOException {
        ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();

       ServletContext context1 = (ServletContext) externalContext.getContext();
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
        HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();

        ReportConfigUtil.compileReport(context1, getCompileDir(), getCompileFileName());

        File reportFile = new File(ReportConfigUtil.getJasperFilePath(context1, getCompileDir(), getCompileFileName() + ".jasper"));

        ///////////////////
        Connection conn = null;
        try {
            conn = MyBatisConnectionFactory.getSqlSessionFactoryIkonsol().openSession().getConnection();
        } catch (Exception ex) {
           // ex.printStackTrace();
           System.out.println("Koneksi gagal");
        }

        /////////////////////

        JasperPrint jasperPrint = ReportConfigUtil.fillReport(reportFile, getReportParameters(), conn);

        if (getExportOption().equals(ExportOption.HTML)) {
            ReportConfigUtil.exportReportAsHtml(jasperPrint, response.getWriter());
        } else if (getExportOption().equals(ExportOption.EXCEL)) {
            ReportConfigUtil.exportReportAsExcel(jasperPrint, response.getWriter());

        }
        else {
               /*response.setrequest.getSession().setAttribute(BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
            response.sendRedirect(request.getContextPath() + "/laporan/" + getExportOption());


             */
System.out.println("JKL");
               ReportConfigUtil.exportReportAsPdf(jasperPrint,reportFile);
/*            JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());
            context.responseComplete();
  */
        }

        FacesContext.getCurrentInstance().responseComplete();
    }


    protected void prepareReport2() throws JRException, IOException {
        ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();

        ServletContext context1 = (ServletContext) externalContext.getContext();
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
        HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();

        ReportConfigUtil.compileReport(context1, getCompileDir(), getCompileFileName2());

        File reportFile = new File(ReportConfigUtil.getJasperFilePath(context1, getCompileDir(), getCompileFileName2() + ".jasper"));

        ///////////////////
        Connection conn = null;
        try {
            conn = MyBatisConnectionFactory.getSqlSessionFactoryIkonsol().openSession().getConnection();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        /////////////////////

        JasperPrint jasperPrint = ReportConfigUtil.fillReport(reportFile, getReportParameters2(), conn);

        if (getExportOption().equals(ExportOption.HTML)) {
            ReportConfigUtil.exportReportAsHtml(jasperPrint, response.getWriter());
        } else if (getExportOption().equals(ExportOption.EXCEL)) {
            ReportConfigUtil.exportReportAsExcel(jasperPrint, response.getWriter());
        } else {
            // request.getSession().setAttribute(BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
            // response.sendRedirect(request.getContextPath() + "/laporan/" + getExportOption());
            JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());
            context.responseComplete();
        }

        FacesContext.getCurrentInstance().responseComplete();
    }
    
     protected void prepareReport3() throws JRException, IOException {
        ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();

        ServletContext context1 = (ServletContext) externalContext.getContext();
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
        HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();

        ReportConfigUtil.compileReport(context1, getCompileDir(), getCompileFileName2());

        File reportFile = new File(ReportConfigUtil.getJasperFilePath(context1, getCompileDir(), getCompileFileName3() + ".jasper"));

        ///////////////////
        Connection conn = null;
        try {
            conn = MyBatisConnectionFactory.getSqlSessionFactoryIkonsol().openSession().getConnection();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        /////////////////////

        JasperPrint jasperPrint = ReportConfigUtil.fillReport(reportFile, getReportParameters3(), conn);

        if (getExportOption().equals(ExportOption.HTML)) {
            ReportConfigUtil.exportReportAsHtml(jasperPrint, response.getWriter());
        } else if (getExportOption().equals(ExportOption.EXCEL)) {
            ReportConfigUtil.exportReportAsExcel(jasperPrint, response.getWriter());
        } else {
            // request.getSession().setAttribute(BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
            // response.sendRedirect(request.getContextPath() + "/laporan/" + getExportOption());
            JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());
            context.responseComplete();
        }

        FacesContext.getCurrentInstance().responseComplete();
    }

    public ExportOption getExportOption() {
        return exportOption;
    }

    public void setExportOption(ExportOption exportOption) {
        this.exportOption = exportOption;
    }

    protected Map<String, Object> getReportParameters() {
        return new HashMap<String, Object>();
    }
    
    protected Map<String, Object> getReportParameters2() {
        return new HashMap<String, Object>();
    }
    
    protected Map<String, Object> getReportParameters3() {
        return new HashMap<String, Object>();
    }

    protected String getCompileDir() {
        return COMPILE_DIR;
    }

    protected abstract String getCompileFileName();


    protected abstract String getCompileFileName2();
     protected abstract String getCompileFileName3();

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
