/*
 * 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.DepositAccountCreationBean;
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 DepositAccountCreationServlet 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");
        Connection connection = null;
        ResultSet rs = null;
        Statement statement = null;
        String sysDt = "";
       // String ODlimit = request.getParameter("ODlimit").isEmpty() ? "0" : (request.getParameter("ODlimit"));

        //String ServletName = request.getParameter("handle_id");
        DepositAccountCreationBean oDepositAccountCreationBean = new DepositAccountCreationBean();
        try {
           // BeanUtils.populate(oDepositAccountCreationBean, request.getParameterMap());
        } catch (IllegalAccessException ex) {
           // Logger.getLogger(AccountCreationServlet.class.getName()).log(Level.SEVERE, null, ex);
           System.out.println("Error occurred during processing.");
        } catch (InvocationTargetException ex) {
           // Logger.getLogger(AccountCreationServlet.class.getName()).log(Level.SEVERE, null, ex);
           System.out.println("Error occurred during processing.");
        }
        try {
            connection = DbHandler.getDBConnection();
            statement = connection.createStatement();
            rs = statement.executeQuery("select to_char(SYSTEM_DATE,'dd/mm/yyyy') from system_date");
            while (rs.next()) {
                sysDt = rs.getString(1);
            }
            statement.close();
            connection.close();
        } catch (Exception e) {
            System.out.println("Error occurred during processing.");
        }
        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);
               System.out.println("Error occurred during processing.");
            }

            proc.setString(1, pacsId);
            proc.setString(2, oDepositAccountCreationBean.getProductCode());
            proc.setString(3, oDepositAccountCreationBean.getInttCategory());
            proc.setString(4, oDepositAccountCreationBean.getCifNumber());
            proc.setString(5, oDepositAccountCreationBean.getCustomerType());
            proc.setString(6, null);
            proc.setString(7, null);
            proc.setString(8, null);
            proc.setString(9, null);
            proc.setString(10, null);
            proc.setString(11, null);
            proc.setString(12, null);
            proc.setString(13, user);
            proc.setString(14, oDepositAccountCreationBean.getSegmentCode());
            proc.setString(15, null);

            proc.setString(16, oDepositAccountCreationBean.getActivityCode());
            proc.setString(17, null);
            proc.setString(18, sysDt);
            proc.setString(19, ODlimit);
            proc.setString(20, oDepositAccountCreationBean.getInttrepmethod());
            proc.setString(21, oDepositAccountCreationBean.getIntttransacc());
            proc.setString(22, oDepositAccountCreationBean.getTermValue());
            proc.setString(23, oDepositAccountCreationBean.getTermYears());
            proc.setString(24, oDepositAccountCreationBean.getTermMonth());
            proc.setString(25, oDepositAccountCreationBean.getTermDays());
            proc.setString(26, oDepositAccountCreationBean.getInstAmt());
            proc.registerOutParameter(27, java.sql.Types.VARCHAR);
            proc.registerOutParameter(28, java.sql.Types.VARCHAR);
            proc.setInt(29, 0);
            proc.setInt(30, 0);
            proc.setInt(31, 0);
            proc.setInt(32, 0);
            proc.setString(33, oDepositAccountCreationBean.getMemberno());
            //proc.setString(34, oDepositAccountCreationBean.getCifNumber2());
            //Change By Bitan for Multiple CIFS
            proc.setString(34, oDepositAccountCreationBean.getJt_cifNumber1());
            proc.setString(35, oDepositAccountCreationBean.getNominee());
            proc.setString(36, oDepositAccountCreationBean.getJt_cifNumber2());
            proc.setString(37, oDepositAccountCreationBean.getJt_cifNumber3());
            proc.setString(38, oDepositAccountCreationBean.getJt_cifNumber4());
            proc.setString(39, oDepositAccountCreationBean.getJt_cifNumber5());
            proc.setString(40, oDepositAccountCreationBean.getJt_cifNumber6());
            proc.setString(41, oDepositAccountCreationBean.getJt_cifNumber7());
            proc.setString(42, oDepositAccountCreationBean.getJt_cifNumber8());
            proc.setString(43, oDepositAccountCreationBean.getJt_cifNumber9());
            proc.setString(44, oDepositAccountCreationBean.getJt_cifNumber10());
            proc.setString(45, oDepositAccountCreationBean.getIntRate());
            proc.setString(46, oDepositAccountCreationBean.getPenIntRate());

            proc.execute();

           // message = proc.getString(28);

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

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