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


import LoginDb.DbHandler;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
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 oracle.jdbc.OracleTypes;
import org.apache.commons.beanutils.BeanUtils;
import DataEntryBean.GliffDepositReportBean;

/**
 *
 * @author Tcs Helpdesk10
 */
public class GliffDepositReportServlet extends HttpServlet {

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

        HttpSession session = request.getSession(false);
        Connection connection = null;
        ResultSet rs = null;
        Statement statement = null;
        CallableStatement proc = null;
        int searchFound = 0;
        String message = "";
        String pacsId = (String) session.getAttribute("pacsId");

        String JSP_Name = request.getParameter("screenName");


        //For Pagination

        int page = 1;
        int recordsPerPage = 10;
        String butType = request.getParameter("but_type");
        if (request.getParameter("page") != null
                && !(request.getParameter("page").equals("")) && !(request.getParameter("page").equals("null"))) {
            //page = Integer.parseInt(request.getParameter("page"));
            if (null != butType && !(butType.equals(""))) {
                if (butType.equals("N")) {
                    page = page + 1;
                } else {
                    page = page - 1;
                }
            }

        }

        int startRow = 0;
        int endRow = 0;
        startRow = (page - 1) * recordsPerPage + 1;
        endRow = page * recordsPerPage;
        int recordCount = 0;
        int totalRecordCount = 0;
       // totalRecordCount = Integer.parseInt(request.getParameter("totalRecordCount"));

        //End of pagination


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

            GliffDepositReportBean oGliffDepositReportBean = new GliffDepositReportBean();
            GliffDepositReportBean oGliffDepositReportBeanSearch = new GliffDepositReportBean();
            ArrayList<GliffDepositReportBean> alGliffDepositReportBean = new ArrayList<GliffDepositReportBean>();
            try {
              //  BeanUtils.populate(oGliffDepositReportBean, request.getParameterMap());
            } catch (IllegalAccessException ex) {
               // Logger.getLogger(GliffDepositReportServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error Occurred during processing.");
            } catch (InvocationTargetException ex) {
               // Logger.getLogger(GliffDepositReportServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error Occurred during processing.");
            }
            try {
                connection = DbHandler.getDBConnection();
                try {
                    proc = connection.prepareCall("{ call Enquiry.Gliff_Report(?,?,?,?,?,?,?) }");
                } catch (SQLException ex) {
                   // Logger.getLogger(GlProductOperationServlet.class.getName()).log(Level.SEVERE, null, ex);
                   System.out.println("Error Occurred during processing.");
                }

                proc.setString(1, oGliffDepositReportBean.getFromDate());
                proc.setString(2, pacsId);
                proc.setInt(3, startRow);
                proc.setInt(4, endRow);
                proc.registerOutParameter(5, OracleTypes.CURSOR);
                proc.registerOutParameter(6, java.sql.Types.VARCHAR);
                proc.registerOutParameter(7, java.sql.Types.INTEGER);

                proc.executeUpdate();

               // rs = (ResultSet) proc.getObject(5);
                //recordCount = proc.getInt(7);

                while (rs.next()) {
                    searchFound = 1;
                    oGliffDepositReportBean = new GliffDepositReportBean();

                    oGliffDepositReportBean.setFromDate(rs.getString("JR_date"));
            oGliffDepositReportBean.setTXN_REF_NO(rs.getString("TXN_REF_NO"));
                        oGliffDepositReportBean.setTRAN_DATE(rs.getString("TRAN_DATE"));
                     oGliffDepositReportBean.setPACS_ID(rs.getString("PACS_ID"));
                      oGliffDepositReportBean.setPOST_DATE(rs.getString("POST_DATE"));
                       oGliffDepositReportBean.setGL_CLASS_CODE(rs.getString("GL_CLASS_CODE"));
                        oGliffDepositReportBean.setTXN_AMT(rs.getString("TXN_AMT"));
                     oGliffDepositReportBean.setTXN_IND(rs.getString("TXN_IND"));
                      oGliffDepositReportBean.setACCT_TYPE(rs.getString("ACCT_TYPE"));
                      oGliffDepositReportBean.setPROD_NAME(rs.getString("PROD_NAME"));
                     oGliffDepositReportBean.setPROD_CODE(rs.getString("PROD_CODE"));
                      oGliffDepositReportBean.setINTT_CAT(rs.getString("INTT_CAT"));
                      oGliffDepositReportBean.setKEY_1(rs.getString("KEY_1"));



                    alGliffDepositReportBean.add(oGliffDepositReportBean);
                    request.setAttribute("displayFlag", "Y");

                }

                //For pagination

                int noOfRecords = alGliffDepositReportBean.size();
                int noOfPages = (int) Math.ceil(noOfRecords * 1.0 / recordsPerPage);

                request.setAttribute("noOfPages", noOfPages);
                request.setAttribute("currentPage", page);
                request.setAttribute("totalRecordCount", totalRecordCount);
                request.setAttribute("recordCount", recordCount);
                request.setAttribute("noOfRecords", noOfRecords);

                //End Of Pagination



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


            } catch (SQLException ex) {
               // Logger.getLogger(UploadServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error Occurred during processing.");
            } finally {
                try {
                    proc.close();
                } catch (SQLException e) {
                    System.out.println("Error Occurred during proc close.");
                }
                try {
                    connection.close();
                } catch (SQLException ex) {
                   // Logger.getLogger(UploadServlet.class.getName()).log(Level.SEVERE, null, ex);
                   System.out.println("Error Occurred during connection close.");
                }
            }
            if (searchFound == 0) {
                message = "No data exists for selected item";
                request.setAttribute("message", message);
            }
            request.setAttribute("oGliffDepositReportBeanSearch", oGliffDepositReportBeanSearch);
            request.setAttribute("alGliffDepositReportBean", alGliffDepositReportBean);
            request.setAttribute("message", message);

            request.getRequestDispatcher("/Deposit/GliffDepositReport.jsp").forward(request, response);

        }



    }
}
