/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Controller;

import DataEntryBean.DepositKYCCreationHdrBean;
import DataEntryBean.DepositKYCCreationDetailBean;
import LoginDb.DbHandler;
import java.io.IOException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.ResultSet;
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 java.sql.Statement;


/**
 *
 * @author 986137
 */
public class DepositKYCDocumentUploadServlet 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
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        HttpSession session = request.getSession(false);
        Connection con = null;
        Connection con1 = null;
        CallableStatement stmt = null;
        ResultSet rs = null;
        int SeachFound = 0;
        String message = "";
        String hdrId = "";
        String pacsId = (String) session.getAttribute("pacsId");
        String user = (String) session.getAttribute("user");
        String handle_id = request.getParameter("handle_id");
       // String cifNumber = request.getParameter("cifNumber");

        DepositKYCCreationHdrBean oDepositKYCCreationBean = new DepositKYCCreationHdrBean();
        DepositKYCCreationDetailBean oDepositKYCCreationDetailBean = new DepositKYCCreationDetailBean();
        ArrayList<DepositKYCCreationDetailBean> alDepositKYCCreationDetailBean = new ArrayList<DepositKYCCreationDetailBean>();

        if (handle_id.equalsIgnoreCase("SearchCIF")) {

            Connection connection = null;
            ResultSet resultset = null;
            Statement statement = null;
            
            
         try {   
             connection = DbHandler.getDBConnection();

            //Detail Part Populate
            try {
                statement = connection.createStatement();
            } catch (SQLException ex) {
              //  Logger.getLogger(DepositKYCDocumentUploadServlet.class.getName()).log(Level.SEVERE, null, ex);
            }

            try {
                String query = "select d.id,d.kyc_document_mst_id,m.document_dtl,d.id_no,d.cif_no, (h.first_name||nvl2(h.middle_name,' '||h.middle_name,'')||nvl2(h.last_name,' '||h.last_name,'')) name from kyc_dtl d,kyc_document_mst m,kyc_hdr h where d.kyc_document_mst_id = m.id and d.cif_no = h.cif_no and d.cif_no='" + cifNumber + "'";
               // rs = statement.executeQuery(query);

                while (rs.next()) {

                    oDepositKYCCreationDetailBean = new DepositKYCCreationDetailBean();
                    oDepositKYCCreationBean = new DepositKYCCreationHdrBean();

                    oDepositKYCCreationDetailBean.setIdType(rs.getString("document_dtl"));
                    oDepositKYCCreationDetailBean.setIdNumber(rs.getString("id_no"));
                    oDepositKYCCreationDetailBean.setDocument_mst_id(rs.getString("kyc_document_mst_id"));
                    oDepositKYCCreationDetailBean.setKYCCreation_dtl_id(rs.getString("id"));
                    oDepositKYCCreationBean.setCif(rs.getString("cif_no"));
                    oDepositKYCCreationBean.setCifName(rs.getString("name"));

                    alDepositKYCCreationDetailBean.add(oDepositKYCCreationDetailBean);

                    SeachFound = 1;
                    request.setAttribute("displayFlag", "Y");

                }
                statement.close();
                rs.close();
            } catch (SQLException ex) {
               // Logger.getLogger(DepositKYCDocumentUploadServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error occurred during processing.");
            }

            } finally {
            try {
                connection.close();
            } catch (SQLException e) {
               // System.out.println(e.toString());
               System.out.println("Error occurred during connection close.");
            }
        }
            if (SeachFound == 0) {
                message = "CIF details not exists in the system";
                request.setAttribute("message", message);
            }



            request.setAttribute("oDepositKYCCreationBeanObj", oDepositKYCCreationBean);
            request.setAttribute("alDepositKYCCreationDetailBean", alDepositKYCCreationDetailBean);

            request.setAttribute("message", message);
            request.getRequestDispatcher("/Deposit/DepositKYCDocumentUpload.jsp").forward(request, response);

        }
    }

    // <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>

}
