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

import DataEntryBean.DepositMiscellaneousBean;
import LoginDb.DbHandler;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.StringTokenizer;
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 org.apache.commons.beanutils.BeanUtils;

/**
 *
 * @author 986137
 */
public class AccountModeOfOperationServlet 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);
        String depositAccSearch = "";
        ResultSet rs = null;
        int SeachFound = 0;
        String message = "";
        String resetBy=null;
        String action = request.getParameter("handle_id");
        String pacsId = (String) session.getAttribute("pacsId");
        String user = (String) session.getAttribute("user");
        //added by rajdiip
        StringBuilder query = null;
        PreparedStatement ps = null;
        int res = 0;
        String txnOrAccntNo = null;

        DepositMiscellaneousBean oDepositMiscellaneousBean = new DepositMiscellaneousBean();

        if (action.equalsIgnoreCase("Search")) {

           // depositAccSearch = request.getParameter("depositAccSearch");
            Connection connection = null;
            ResultSet resultset = null;
            Statement statement = null;
            connection = DbHandler.getDBConnection();

            try {
                statement = connection.createStatement();
            } catch (SQLException ex) {
               // Logger.getLogger(AccountModeOfOperationServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error Occurred during processing.");
            }
            try {
               // resultset = statement.executeQuery("select t.key_1,t.avail_bal as Account_BAlance,t.customer_no,k.first_name||' '||k.middle_name||' '||k.last_name as customer1_name,"
                        + " case when t.cif_no2 is null then 'NA' else t.cif_no2 end as cif_no2,(select kh.first_name||' '||kh.middle_name||' '||kh.last_name as customer2_name from kyc_hdr kh where kh.cif_no=t.cif_no2) as cif2_name,"
                        + " decode(t.CURR_STATUS,'C','Closed','D','Dormant','S','Stopped','M','Matured','Open/Active') as curr_status ,nvl(NOMINEE_CIF,'NA') as NOMINEE_CIF, "
                        + " nvl((select k.mop_name from acc_mop_map k where k.mop_code = t.mop), 'NA') MOP "
                        + " from dep_account t,kyc_hdr k,dep_product p where k.cif_no = t.customer_no and t.dep_prod_id = p.id and t.key_1 ='" + depositAccSearch + "' and t.PACS_ID = '" + pacsId + "'");



                while (resultset.next()) {
                    oDepositMiscellaneousBean.setDepositAccountNumber(resultset.getString("key_1"));
                    oDepositMiscellaneousBean.setCifNo(resultset.getString("customer_no"));
                    oDepositMiscellaneousBean.setAccBalance(resultset.getString("Account_BAlance"));
                    oDepositMiscellaneousBean.setAccStat(resultset.getString("curr_status"));
                    oDepositMiscellaneousBean.setNomCif(resultset.getString("NOMINEE_CIF"));

                    oDepositMiscellaneousBean.setCustomer1Name(resultset.getString("customer1_name"));
                    oDepositMiscellaneousBean.setCif2No(resultset.getString("cif_no2"));
                    oDepositMiscellaneousBean.setMode(resultset.getString("mop"));
                    if (resultset.getString("cif2_name") == null) {
                        oDepositMiscellaneousBean.setCustomer2Name("NA");
                    } else {
                        oDepositMiscellaneousBean.setCustomer2Name(resultset.getString("cif2_name"));
                    }

                    SeachFound = 1;

                    request.setAttribute("displayFlag", "Y");

                }

               // statement.close();
               // resultset.close();
               // connection.close();

            } catch (SQLException ex) {
               // Logger.getLogger(AccountModeOfOperationServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error Occurred during processing.");
            } finally {
                try {
                    if(statement !=null)
                        statement.close();
                    if(resultset !=null)
                        resultset.close();
                    if (connection != null)
                    connection.close();

            } catch (Exception e) {               
               System.out.println("Error Occurred during connection close.");
            }

            }

            if (SeachFound == 0) {
                message = "Deposit Account not exists in the system";
                request.setAttribute("message", message);
            }

            request.setAttribute("oDepositMiscellaneousBeanObj", oDepositMiscellaneousBean);
            request.getRequestDispatcher("/Deposit/AccountModeOfOperation.jsp").forward(request, response);

        } else if (action.equalsIgnoreCase("SubmitOperation")) {

            Connection con = null;
            CallableStatement proc = null;

              try {
                    con = DbHandler.getDBConnection();
                   // String acc = request.getParameter("depositAccountNumber");
                    //String opMode = request.getParameter("operation_type");
           
                    proc = con.prepareCall("{ call operations2.account_mod_operation(?,?,?,?,?) }");                                      

                    proc.setString(1, acc);
                    proc.setString(2, pacsId);
                    proc.setString(3, user);
                    proc.setString(4, opMode);
                    proc.registerOutParameter(5, java.sql.Types.VARCHAR);

                    proc.execute();

                    //message = proc.getString(5);

                    request.setAttribute("message", message);

                request.setAttribute("oDepositMiscellaneousBeanObj", oDepositMiscellaneousBean);
                request.getRequestDispatcher("/Deposit/AccountModeOfOperation.jsp").forward(request, response);
        }
                
             catch (SQLException ex) {
                // ex.printStackTrace();
               // System.out.println("Exception occured  in AccountModeOfOperationServlet for user Id :" + user);
               // message= "Error occured. Please try again";
                // Logger.getLogger(AccountModeOfOperationServlet.class.getName()).log(Level.SEVERE, null, ex);
                System.out.println("Error Occurred during processing.");
            } finally {
                try {
                    if(proc !=null)
                        proc.close();
                    if (con != null)
                    con.close();

            } catch (Exception e) {
               // e.printStackTrace();
               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>
}
