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

import LoginDb.DbHandler;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.sql.BatchUpdateException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.PreparedStatement;
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 org.apache.commons.beanutils.BeanUtils;
import java.sql.Statement;
import java.util.Arrays;
import java.util.StringTokenizer;
import javax.servlet.ServletContext;
import DataEntryBean.AccountAmendBean;

/**
 *
 * @author 981898
 */
public class AccountServletSTB 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;
        CallableStatement stmt = null;
        ResultSet rs = null;
        String bankId = (String) session.getAttribute("pacsId");
        String user = (String) session.getAttribute("user");
        //String pacsID_PACS = request.getParameter("pacsId_PACS");
        //String accN = request.getParameter("accNo");
        //String newLimit = request.getParameter("newLimit");
        //String newExpDt = request.getParameter("newExpDate");
       // AccountAmendBean oAccountAmendBean = new AccountAmendBean();
        String message = "";

//        try {
//            BeanUtils.populate(oAccountAmendBean, request.getParameterMap());
//        } catch (IllegalAccessException ex) {
//            Logger.getLogger(DepositKYCCreationServlet.class.getName()).log(Level.SEVERE, null, ex);
//        } catch (InvocationTargetException ex) {
//            Logger.getLogger(DepositKYCCreationServlet.class.getName()).log(Level.SEVERE, null, ex);
//        }

        try {
            con = DbHandler.getDBConnection();
            try {
                stmt = con.prepareCall("{ call  Operations1.BANK_ACCT_LIMIT_SET(?,?,?,?,?,?,?) }");
            } catch (SQLException ex) {
               // Logger.getLogger(DepositKYCCreationServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error occurred during processing.");
            }
            stmt.setString(1, pacsID_PACS);
            stmt.setString(2, accN);
            stmt.setString(3, user);
            stmt.setString(4, newLimit);
            stmt.setString(5, newExpDt);
            stmt.registerOutParameter(6, java.sql.Types.VARCHAR);
            stmt.setString(7, bankId);

            stmt.execute();

           // message = stmt.getString(6);

        } catch (SQLException ex) {
            try {
               // con.rollback();
                if(message.isEmpty())
                    message="Error in account amendment";
            } catch (Exception ex1) {
               // Logger.getLogger(DepositKYCCreationServlet.class.getName()).log(Level.SEVERE, null, ex1);
               System.out.println("Error occurred during processing.");
            }
           // Logger.getLogger(DepositKYCCreationServlet.class.getName()).log(Level.SEVERE, null, ex);
           // System.out.println(ex.toString());
           System.out.println("Error occurred during processing.");
        } finally {
            try {
                stmt.close();
            } catch (SQLException e) {
                System.out.println("Error occurred during statement close.");
            }
            try {
                con.close();
            } catch (SQLException ex) {
               // Logger.getLogger(DepositKYCCreationServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error occurred during connection close.");
            }

        }
        request.setAttribute("message", message);
        request.getRequestDispatcher("/accountAmendSTB.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>
}
