/*
 * 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 DataEntryBean.AccountCreationBean;
import LoginDb.DbHandler;
import java.io.IOException;
import java.io.PrintWriter;
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.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 Tcs Helpdesk10
 */
public class AccountCreationServlet extends HttpServlet {

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

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

        HttpSession session = request.getSession(false);
        Connection con = null;
        CallableStatement proc = null;
        String message = "";
        String pacsId = (String) session.getAttribute("pacsId");
        String user = (String) session.getAttribute("user");

        //String ServletName = request.getParameter("handle_id");
        AccountCreationBean oAccountCreationBean = new AccountCreationBean();
        try {
           // BeanUtils.populate(oAccountCreationBean, request.getParameterMap());
        } catch (IllegalAccessException ex) {
            System.out.println("Error Occurred during processing.");
            //Logger.getLogger(AccountCreationServlet.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            System.out.println("Error Occurred during processing.");
            //Logger.getLogger(AccountCreationServlet.class.getName()).log(Level.SEVERE, null, ex);
        } finally
            { 
                System.out.println("Processing done."); 
            }

        try {
            con = DbHandler.getDBConnection();
            try {
                proc = con.prepareCall("{ call operations.account_opening_authorization(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) }");
            } catch (SQLException ex) {
               // Logger.getLogger(AccountCreationServlet.class.getName()).log(Level.SEVERE, null, ex);
            }

            proc.setString(1, pacsId);
            proc.setString(2, oAccountCreationBean.getProductCode());
            proc.setString(3, oAccountCreationBean.getInttCategory());
            proc.setString(4, oAccountCreationBean.getCifNumber());
            proc.setString(5, oAccountCreationBean.getCustomerType());
            proc.setString(6, oAccountCreationBean.getLimit());
            proc.setString(7, oAccountCreationBean.getLimitExpiryDate());
            proc.setString(8, oAccountCreationBean.getCollateralType());
            proc.setString(9, oAccountCreationBean.getLandinAcres());
            proc.setString(10, oAccountCreationBean.getDescription());
            proc.setString(11, oAccountCreationBean.getCurrentValuation());
            proc.setString(12, oAccountCreationBean.getSafeLendingMargin());
            proc.setString(13, user);
            proc.setString(14, oAccountCreationBean.getSegmentCode());
            proc.setString(15, oAccountCreationBean.getCbsSavingsAccount());
            proc.setString(15, oAccountCreationBean.getCbsSavingsAccount());
            proc.setString(16, oAccountCreationBean.getActivityCode());
            proc.setString(17, oAccountCreationBean.getLimit_kind());
            proc.setString(45, oAccountCreationBean.getIntRate());
            proc.setString(46, oAccountCreationBean.getPenIntRate());
            proc.setString(18, null);
            proc.setString(19, null);
            proc.setString(20, null);
            proc.setString(21, null);
            proc.setString(22, null);
            proc.setString(23, null);
            proc.setString(24, null);
            proc.setString(25, null);
            proc.setString(26, null);
            proc.registerOutParameter(27, java.sql.Types.VARCHAR);
            proc.registerOutParameter(28, java.sql.Types.VARCHAR);
            proc.setString(29, null);
            proc.setString(30, null);
            proc.setString(31, null);
            proc.setString(32, null);
            proc.setString(33, null);
            proc.setString(34, null);
            proc.setString(35, null);

/*Additional null parameters added By Bitan related to Changes for Multiple CIF in DEP Accounts*/
            proc.setString(36, null);
            proc.setString(37, null);
            proc.setString(38, null);
            proc.setString(39, null);
            proc.setString(40, null);
            proc.setString(41, null);
            proc.setString(42, null);
            proc.setString(43, null);
            proc.setString(44, null);


            proc.execute();

            

        } catch (SQLException ex) {
            System.out.println("Error Occurred during processing.");
            //Logger.getLogger(AccountCreationServlet.class.getName()).log(Level.SEVERE, null, ex);
            //System.out.println(ex.toString());
        } finally {
            try {
                proc.close();
            } catch (SQLException e) {
                System.out.println("Error occured during procedure close.");
            }
            try {
                con.close();
            } catch (SQLException ex) {
                System.out.println("Error occured during connection close.");
                //Logger.getLogger(AccountCreationServlet.class.getName()).log(Level.SEVERE, null, ex);
                //System.out.println(ex.toString());
            }

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

        }

    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
