/*
 * 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 ExcelReportController;

import Controller.EnquiryServlet;
import DataEntryBean.DepositMiscellaneousBean;
import DataEntryBean.EnquiryBean;
import DataEntryBean.ExcelReportBean;
import DataEntryBean.GLTransactionEnquiryBean;
import DataEntryBean.GlAccountEnquiryBean;
import DataEntryBean.ReportBean;
import DataEntryBean.LoanRepaymentScheduleBean;
import DataEntryBean.TransactionEnquiryBean;
import DataEntryBean.NeftRtgsEnquiryBean;
import LoginDb.DbHandler;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.apache.commons.beanutils.BeanUtils;

/**
 *
 * @author Tcs Helpdesk10
 */
public class ExcelReportServlet 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
     */
    private static final String CONTENT_TYPE_EXCEL = "application/vnd.ms-excel";

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        HttpSession session = request.getSession(false);
        String pacsId = (String) session.getAttribute("pacsId");
        String subPacsFlag = (String) session.getAttribute("subPacsFlag");
        String JSP_Name = "";
        JSP_Name = null == (request.getParameter("screenName")) ? "" : (request.getParameter("screenName"));

        response.setContentType(CONTENT_TYPE_EXCEL);

       // String reportName = (String) request.getParameter("reportname");

        //To Populate Bean
        ExcelReportBean ExcelReportBeanObj = new ExcelReportBean();
        ExcelReportBeanObj = null;

        if (JSP_Name.equalsIgnoreCase("accountEnquiry")) {

            EnquiryBean oEnquiryBean = new EnquiryBean();
            oEnquiryBean = (EnquiryBean) session.getAttribute("oEnquiryBeanSearchObj");

            ExcelReportBeanObj = new ExcelReportBean();

            ExcelReportBeanObj.setAccountNo(oEnquiryBean.getAccountNo());
            ExcelReportBeanObj.setFromAmount(oEnquiryBean.getFromAmount());
            ExcelReportBeanObj.setToAmount(oEnquiryBean.getToAmount());
            ExcelReportBeanObj.setProductName(oEnquiryBean.getProductId());
            ExcelReportBeanObj.setToDate(oEnquiryBean.getToDate());
            ExcelReportBeanObj.setFromDate(oEnquiryBean.getFromDate());
            ExcelReportBeanObj.setFromDate(oEnquiryBean.getLinkedSbAccount());
            ExcelReportBeanObj.setSubpacs_id(oEnquiryBean.getSubpacs_id());

            // session.removeAttribute("oEnquiryBeanSearchObj");

        } else if (JSP_Name.equalsIgnoreCase("transactionEnquiry")) {

            TransactionEnquiryBean oTransactionEnquiryBean = new TransactionEnquiryBean();
            oTransactionEnquiryBean = (TransactionEnquiryBean) session.getAttribute("oTransactionEnquiryBeanSearchObj");

            ExcelReportBeanObj = new ExcelReportBean();

            ExcelReportBeanObj.setAccountNo(oTransactionEnquiryBean.getAccountNo());
            ExcelReportBeanObj.setFromAmount(oTransactionEnquiryBean.getFromAmount());
            ExcelReportBeanObj.setToAmount(oTransactionEnquiryBean.getToAmount());
            ExcelReportBeanObj.setProductName(oTransactionEnquiryBean.getProductId());
            ExcelReportBeanObj.setToDate(oTransactionEnquiryBean.getToDate());
            ExcelReportBeanObj.setFromDate(oTransactionEnquiryBean.getFromDate());
            ExcelReportBeanObj.setFromDate(oTransactionEnquiryBean.getLinkedSbAccount());
            ExcelReportBeanObj.setSubpacs_id(oTransactionEnquiryBean.getSubpacs_id());

            // session.removeAttribute("oTransactionEnquiryBeanSearchObj");

        } else if (JSP_Name.equalsIgnoreCase("glAccountEnquiry")) {

            GlAccountEnquiryBean oGlAccountEnquiryBean = new GlAccountEnquiryBean();
            oGlAccountEnquiryBean = (GlAccountEnquiryBean) session.getAttribute("oGlAccountEnquiryBeanSearchObj");

            ExcelReportBeanObj = new ExcelReportBean();

            ExcelReportBeanObj.setAccountNo(oGlAccountEnquiryBean.getAccountNo());
            ExcelReportBeanObj.setFromAmount(oGlAccountEnquiryBean.getFromAmount());
            ExcelReportBeanObj.setToAmount(oGlAccountEnquiryBean.getToAmount());
            ExcelReportBeanObj.setProductName(oGlAccountEnquiryBean.getProductId());
            ExcelReportBeanObj.setToDate(oGlAccountEnquiryBean.getToDate());
            ExcelReportBeanObj.setFromDate(oGlAccountEnquiryBean.getFromDate());
            ExcelReportBeanObj.setSubpacs_id(oGlAccountEnquiryBean.getSubpacs_id());

            // session.removeAttribute("oGlAccountEnquiryBeanSearchObj");

        } else if (JSP_Name.equalsIgnoreCase("glTransactionEnquiry")) {

            GLTransactionEnquiryBean oGLTransactionEnquiryBean = new GLTransactionEnquiryBean();
            oGLTransactionEnquiryBean = (GLTransactionEnquiryBean) session.getAttribute("oGlTransactionEnquiryBeanobj");

            ExcelReportBeanObj = new ExcelReportBean();

            ExcelReportBeanObj.setAccountNo(oGLTransactionEnquiryBean.getAccountNo());
            ExcelReportBeanObj.setFromAmount(oGLTransactionEnquiryBean.getFromAmount());
            ExcelReportBeanObj.setToAmount(oGLTransactionEnquiryBean.getToAmount());
            ExcelReportBeanObj.setProductName(oGLTransactionEnquiryBean.getProductId());
            ExcelReportBeanObj.setToDate(oGLTransactionEnquiryBean.getToDate());
            ExcelReportBeanObj.setFromDate(oGLTransactionEnquiryBean.getFromDate());
            ExcelReportBeanObj.setTranRefSearch(oGLTransactionEnquiryBean.getTranRefSearch());
            ExcelReportBeanObj.setSubpacs_id(oGLTransactionEnquiryBean.getSubpacs_id());

            // session.removeAttribute("oGlTransactionEnquiryBeanobj");

        } else if (JSP_Name.equalsIgnoreCase("DepositAccountEnquiry")) {

            EnquiryBean oEnquiryBean = new EnquiryBean();
            oEnquiryBean = (EnquiryBean) session.getAttribute("oEnquiryBeanSearchObj");

            ExcelReportBeanObj = new ExcelReportBean();

            ExcelReportBeanObj.setAccountNo(oEnquiryBean.getAccountNo());
            ExcelReportBeanObj.setFromAmount(oEnquiryBean.getFromAmount());
            ExcelReportBeanObj.setToAmount(oEnquiryBean.getToAmount());
            ExcelReportBeanObj.setProductName(oEnquiryBean.getProductId());
            ExcelReportBeanObj.setToDate(oEnquiryBean.getToDate());
            ExcelReportBeanObj.setFromDate(oEnquiryBean.getFromDate());
            ExcelReportBeanObj.setSrchByCIF(oEnquiryBean.getSrchByCIF());
            ExcelReportBeanObj.setSubpacs_id(oEnquiryBean.getSubpacs_id());
            ExcelReportBeanObj.setOldAccNo(oEnquiryBean.getOldAccNo());
            ExcelReportBeanObj.setOldCifNo(oEnquiryBean.getOldCifNo());
            ExcelReportBeanObj.setModOfAcc(oEnquiryBean.getModOfAcc());


            // session.removeAttribute("oEnquiryBeanSearchObj");

        } else if (JSP_Name.equalsIgnoreCase("DepositTransactionEnquiry")) {

            TransactionEnquiryBean oTransactionEnquiryBean = new TransactionEnquiryBean();
            oTransactionEnquiryBean = (TransactionEnquiryBean) session.getAttribute("oTransactionEnquiryBeanSearchObj");

            ExcelReportBeanObj = new ExcelReportBean();

            ExcelReportBeanObj.setAccountNo(oTransactionEnquiryBean.getAccountNo());
            ExcelReportBeanObj.setFromAmount(oTransactionEnquiryBean.getFromAmount());
            ExcelReportBeanObj.setToAmount(oTransactionEnquiryBean.getToAmount());
            ExcelReportBeanObj.setProductName(oTransactionEnquiryBean.getProductId());
            ExcelReportBeanObj.setToDate(oTransactionEnquiryBean.getToDate());
            ExcelReportBeanObj.setFromDate(oTransactionEnquiryBean.getFromDate());
            ExcelReportBeanObj.setTranRefSearch(oTransactionEnquiryBean.getTranRefSearch());
            ExcelReportBeanObj.setSubpacs_id(oTransactionEnquiryBean.getSubpacs_id());

        } else if (JSP_Name.equalsIgnoreCase("LoanRepayment")) {

            LoanRepaymentScheduleBean oLoanRepaymentScheduleBean = new LoanRepaymentScheduleBean();
            oLoanRepaymentScheduleBean = (LoanRepaymentScheduleBean) session.getAttribute("oLoanRepaymentScheduleBean");

            ExcelReportBeanObj = new ExcelReportBean();

            ExcelReportBeanObj.setAccountNo(oLoanRepaymentScheduleBean.getLoanAcc());
            ExcelReportBeanObj.setLoanRepYYYYMM(oLoanRepaymentScheduleBean.getLoanRepYYYYMM());



        } else if (JSP_Name.equalsIgnoreCase("LoanInquiry")) {

            EnquiryBean oEnquiryBean = new EnquiryBean();
            oEnquiryBean = (EnquiryBean) session.getAttribute("oEnquiryBeanSearchObj");

            ExcelReportBeanObj = new ExcelReportBean();

            ExcelReportBeanObj.setAccountNo(oEnquiryBean.getAccountNo());
            ExcelReportBeanObj.setCustomerNo(oEnquiryBean.getCustomerNo());
            ExcelReportBeanObj.setToDate(oEnquiryBean.getToDate());
            ExcelReportBeanObj.setFromDate(oEnquiryBean.getFromDate());
            ExcelReportBeanObj.setCifNumber(oEnquiryBean.getCifNumber());
            ExcelReportBeanObj.setSubpacs_id(oEnquiryBean.getSubpacs_id());
            ExcelReportBeanObj.setOldAccNo(oEnquiryBean.getOldAccNo());
            ExcelReportBeanObj.setOldCifNo(oEnquiryBean.getOldCifNo());
            ExcelReportBeanObj.setProductName(oEnquiryBean.getProductName());
            ExcelReportBeanObj.setProductID(oEnquiryBean.getProductId());


            // session.removeAttribute("oEnquiryBeanSearchObj");

        }
        else if (JSP_Name.equalsIgnoreCase("GliffDepositReport")) {

            ExcelReportBeanObj = new ExcelReportBean();

           // ExcelReportBeanObj.setFromDate(request.getParameter("fromDate"));


        }else if (JSP_Name.equalsIgnoreCase("LoanTransactionEnquiry")) {

            TransactionEnquiryBean oTransactionEnquiryBean = new TransactionEnquiryBean();
            oTransactionEnquiryBean = (TransactionEnquiryBean) session.getAttribute("oTransactionEnquiryBeanSearchObj");

            ExcelReportBeanObj = new ExcelReportBean();

            ExcelReportBeanObj.setAccountNo(oTransactionEnquiryBean.getAccountNo());
            ExcelReportBeanObj.setFromAmount(oTransactionEnquiryBean.getFromAmount());
            ExcelReportBeanObj.setToAmount(oTransactionEnquiryBean.getToAmount());
            ExcelReportBeanObj.setProductName(oTransactionEnquiryBean.getProductId());
            ExcelReportBeanObj.setToDate(oTransactionEnquiryBean.getToDate());
            ExcelReportBeanObj.setFromDate(oTransactionEnquiryBean.getFromDate());
            ExcelReportBeanObj.setTranRefSearch(oTransactionEnquiryBean.getTranRefSearch());
            ExcelReportBeanObj.setSubpacs_id(oTransactionEnquiryBean.getSubpacs_id());

        }else if (JSP_Name.equalsIgnoreCase("DepositMiscellaneousEnquiry")) {

            DepositMiscellaneousBean oDepositMiscellaneousBean = new DepositMiscellaneousBean();
            oDepositMiscellaneousBean = (DepositMiscellaneousBean) session.getAttribute("oEnquiryBeanSearchObj");

            ExcelReportBeanObj = new ExcelReportBean();

            ExcelReportBeanObj.setAccountNo(oDepositMiscellaneousBean.getAccountNo());
            ExcelReportBeanObj.setFromAmount(oDepositMiscellaneousBean.getFromAmount());
            ExcelReportBeanObj.setToAmount(oDepositMiscellaneousBean.getToAmount());
            ExcelReportBeanObj.setToDate(oDepositMiscellaneousBean.getToDate());
            ExcelReportBeanObj.setFromDate(oDepositMiscellaneousBean.getFromDate());
            ExcelReportBeanObj.setEnqTypeSearch(oDepositMiscellaneousBean.getEnqTypeSearch());
            ExcelReportBeanObj.setSubpacs_id(oDepositMiscellaneousBean.getSubpacs_id());

        } else if (JSP_Name.equalsIgnoreCase("NeftRtgsInitiation")) {

            NeftRtgsEnquiryBean oNeftRtgsEnquiryBean = new NeftRtgsEnquiryBean();
            oNeftRtgsEnquiryBean = (NeftRtgsEnquiryBean) session.getAttribute("alEnquiryBeanobj");

            ExcelReportBeanObj = new ExcelReportBean();

            ExcelReportBeanObj.setTxnNo(oNeftRtgsEnquiryBean.getTxnNo());
            ExcelReportBeanObj.setRemAccNo(oNeftRtgsEnquiryBean.getSrcAccNo());
            ExcelReportBeanObj.setBenAccNo(oNeftRtgsEnquiryBean.getDestAccNo2());
            ExcelReportBeanObj.setIfscNoVal2(oNeftRtgsEnquiryBean.getIfscNoVal2());
            ExcelReportBeanObj.setTxnFromDate(oNeftRtgsEnquiryBean.getFrom_date());
            ExcelReportBeanObj.setTxnToDate(oNeftRtgsEnquiryBean.getTo_date());
            //ExcelReportBeanObj.setSubpacs_id(oNeftRtgsEnquiryBean.getSubpacs_id());

        }

//To Populate Bean Ends Here
     //To generate Excel download log
        generateExcelDownloadLog(request);
        
        response.addHeader("content-disposition",
                "attachment; filename=" + reportName + ".xls");

        OutputStream out = response.getOutputStream();

        ExcelReport excelReport = new ExcelReport();
        WorkbookSettings ws = excelReport.getWorkBookSetting();
        String query = null;

        try {
            WritableWorkbook workbook = Workbook.createWorkbook(out, ws);
            WritableSheet s = workbook.createSheet(reportName, 0);
            ExcelReportInformation ex = new ExcelReportInformation(reportName);

            if (null == ExcelReportBeanObj) {
                query = ex.getReportQuery();

            } else {

                query = ex.getReportQuery(ExcelReportBeanObj, pacsId,subPacsFlag);
            }

            ArrayList aHeader = ex.getReportHeader();
            excelReport.writeDataSheet(s, aHeader, query);

            workbook.write();
            workbook.close();
            out.flush();
                        
        } catch (Exception e) {
           // e.printStackTrace();
           System.out.println("Error Occurred during processing.");
        
        } finally {
            System.out.println("Processing done.");
        }
    }
    
    private void generateExcelDownloadLog(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
                 
        String pacsId = (String) session.getAttribute("pacsId");
        String tellerID = (String) session.getAttribute("user");
      //  String reportName = (String) request.getParameter("reportname");
        Connection connection = null;
        Statement statement = null;
        
        ExcelReportBean ExcelReportBeanObj = new ExcelReportBean();

        try {
          //  BeanUtils.populate(ExcelReportBeanObj, request.getParameterMap());
        } catch (IllegalAccessException ex) {
          //  Logger.getLogger(ExcelReportServlet.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
           // Logger.getLogger(ExcelReportServlet.class.getName()).log(Level.SEVERE, null, ex);
        } 
        
        try {
            connection = DbHandler.getDBConnection();
            statement = connection.createStatement();
            ResultSet rs = statement.executeQuery(" insert into excel_report_log\n" +
"(pacs_id,\n" +
"teller_id,\n" +
"report_name,\n" +
"proc_dt,\n" +
"proc_time,\n" +
"accountno,\n" +
"productname,\n" +
"from_date,\n" +
"to_date,\n" +
"fromamount,\n" +
"toamount,\n" +
"linkedsbaccount,\n" +
"loanrepyyyymm,\n" +
"cifnumber,\n" +
"tranrefsearch,\n" +
"srchbycif,\n" +
"subpacs_id,\n" +
"subpacs_name,\n" +
"enqtypesearch,\n" +
"oldaccno,\n" +
"oldcifno,\n" +
"productid,\n" +
"txnno,\n" +
"remaccno,\n" +
"benaccno,\n" +
"ifscnoval2,\n" +
"txnfromdate,\n" +
"txntodate,\n" +
"modofacc)\n" +
"values\n" +
"('"+ pacsId +"',\n" +
"'"+ tellerID +"',\n" +
"'"+ reportName +"',\n" +
"(select system_date from system_date),\n" +
"to_date(to_char(SYSTIMESTAMP, 'DD-MON-RRHH24:MI:SS'),'DD-MON-RRHH24:MI:SS'),\n" +
"'"+ ExcelReportBeanObj.getAccountNo() +"',\n" +
"'"+ ExcelReportBeanObj.getProductName() +"',\n" +
"'"+ ExcelReportBeanObj.getFromDate() +"',\n" +
"'"+ ExcelReportBeanObj.getToDate() +"',\n" +
"'"+ ExcelReportBeanObj.getFromAmount() +"',\n" +
"'"+ ExcelReportBeanObj.getToAmount() +"',\n" +
"'"+ ExcelReportBeanObj.getLinkedSbAccount() +"',\n" +
"'"+ ExcelReportBeanObj.getLoanRepYYYYMM() +"',\n" +
"'"+ ExcelReportBeanObj.getCifNumber() +"',\n" +
"'"+ ExcelReportBeanObj.getTranRefSearch() +"',\n" +
"'"+ ExcelReportBeanObj.getSrchByCIF() +"',\n" +
"'"+ ExcelReportBeanObj.getSubpacs_id() +"',\n" +
"'"+ ExcelReportBeanObj.getSubpacs_name() +"',\n" +
"'"+ ExcelReportBeanObj.getEnqTypeSearch() +"',\n" +
"'"+ ExcelReportBeanObj.getOldAccNo() +"',\n" +
"'"+ ExcelReportBeanObj.getOldCifNo() +"',\n" +
"'"+ ExcelReportBeanObj.getProductID() +"',\n" +
"'"+ ExcelReportBeanObj.getTxnNo() +"',\n" +
"'"+ ExcelReportBeanObj.getRemAccNo() +"',\n" +
"'"+ ExcelReportBeanObj.getBenAccNo() +"',\n" +
"'"+ ExcelReportBeanObj.getIfscNoVal2() +"',\n" +
"'"+ ExcelReportBeanObj.getTxnFromDate() +"',\n" +
"'"+ ExcelReportBeanObj.getTxnToDate() +"',\n" +
"'"+ ExcelReportBeanObj.getModOfAcc() +"') ");
            

        } catch (SQLException ex) {
           System.out.println("Error Occurred during processing.");
        
        } finally {
                try {
                    if(statement !=null)
                        statement.close();
                    if (connection != null)
                    connection.close();

            } catch (Exception e) {               
               System.out.println("Error Occurred during connection 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>
}
