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

import DataEntryBean.DepositTransferBean;
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.SQLException;
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 org.apache.commons.beanutils.BeanUtils;
import javax.servlet.http.HttpSession;

/**
 *
 * @author 986517
 */
public class DepositTransferClosureServlet 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 {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet DepositTransferClosureServlet</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet DepositTransferClosureServlet at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
             */
        } finally {
            out.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 {
        Connection con = null;
        CallableStatement proc = null;
        String message = "";
        //String Account_type = "";
        String JSP = "";
        HttpSession session = request.getSession();
        JSP = request.getParameter("screenName") == null ? "" : (request.getParameter("screenName"));
        DepositTransferBean oDepositTransferBean = new DepositTransferBean();

        try {
            //BeanUtils.populate(oDepositTransferBean, request.getParameterMap());
        } catch (IllegalAccessException ex) {
           // Logger.getLogger(DepositGlTransactionServlet.class.getName()).log(Level.SEVERE, null, ex);
           System.out.println("Error occurred during processing.");
        } catch (InvocationTargetException ex) {
           // Logger.getLogger(DepositGlTransactionServlet.class.getName()).log(Level.SEVERE, null, ex);
           System.out.println("Error occurred during processing.");
        }
        if ((oDepositTransferBean.getCheckOption().equalsIgnoreCase("d2c")) || (oDepositTransferBean.getCheckOption().equalsIgnoreCase("t2g"))) {

            try {
                con = DbHandler.getDBConnection();
                try {

                    proc = con.prepareCall("{ call POST_transfer_close(?,?,?,?,?,?,?,?,?,?) }");

                } catch (SQLException ex) {
                   // Logger.getLogger(DepositGlTransactionServlet.class.getName()).log(Level.SEVERE, null, ex);
                   System.out.println("Error occurred during processing.");
                }
                String pacsId = session.getAttribute("pacsId").toString();
                String makerId = session.getAttribute("user").toString();

                proc.setString(1, pacsId);
                proc.setString(2, makerId);
                proc.setString(3, "DR");
                proc.setString(4, oDepositTransferBean.getAccNo_dd_from());
                if(oDepositTransferBean.getCheckOption().equalsIgnoreCase("d2c"))
                    proc.setString(5, oDepositTransferBean.getAccNo_dd_to());
                else if(oDepositTransferBean.getCheckOption().equalsIgnoreCase("t2g"))
                    proc.setString(5, oDepositTransferBean.getAccNo_gl_to());
                proc.setString(6, oDepositTransferBean.getTrAmount());
                proc.setString(7, oDepositTransferBean.getNarration());
                proc.setString(8, oDepositTransferBean.getCheckOption());
                proc.registerOutParameter(9, java.sql.Types.VARCHAR);
                proc.registerOutParameter(10, java.sql.Types.VARCHAR);

                proc.execute();

               // message = proc.getString(10);


                } catch (SQLException ex) {
               // Logger.getLogger(DepositGlTransactionServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error occurred during processing.");
            } finally {
                try {
                    proc.close();
                } catch (SQLException e) {
                    System.out.println("Error occurred during proc close.");
                }
                try {
                    con.close();
                } catch (SQLException ex) {
                   // Logger.getLogger(DepositGlTransactionServlet.class.getName()).log(Level.SEVERE, null, ex);
                   System.out.println("Error occurred during connection close.");
                }
            }
        } else if (oDepositTransferBean.getCheckOption().equalsIgnoreCase("c2c")) {


            try {
                con = DbHandler.getDBConnection();
                try {

                    proc = con.prepareCall("{ call POST_transfer_closecash(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) }");

                } catch (SQLException ex) {
                   // Logger.getLogger(DepositGlTransactionServlet.class.getName()).log(Level.SEVERE, null, ex);
                   System.out.println("Error occurred during processing.");
                }
                String pacsId = session.getAttribute("pacsId").toString();
                String makerId = session.getAttribute("user").toString();
              //  String twothouIN = (String) request.getParameter("twothouIN") == "" ? "0" : (request.getParameter("twothouIN"));
              //  String twothouOUT = request.getParameter("twothouOUT") == "" ? "0" : (request.getParameter("twothouOUT"));
               // String fivehundredIN = request.getParameter("fivehundredIN") == "" ? "0" : (request.getParameter("fivehundredIN"));
               // String fivehundredOUT = request.getParameter("fivehundredOUT") == "" ? "0" : (request.getParameter("fivehundredOUT"));
               // String hundredIN = request.getParameter("hundredIN") == "" ? "0" : (request.getParameter("hundredIN"));
               // String hundredOUT = request.getParameter("hundredOUT") == "" ? "0" : (request.getParameter("hundredOUT"));
               // String fiftyIN = request.getParameter("fiftyIN") == "" ? "0" : (request.getParameter("fiftyIN"));
               // String fiftyOUT = request.getParameter("fiftyOUT") == "" ? "0" : (request.getParameter("fiftyOUT"));
               // String twentyIN = request.getParameter("twentyIN") == "" ? "0" : (request.getParameter("twentyIN"));
              //  String twentyOUT = request.getParameter("twentyOUT") == "" ? "0" : (request.getParameter("twentyOUT"));
               // String tenIN = request.getParameter("tenIN") == "" ? "0" : (request.getParameter("tenIN"));
               // String tenOUT = request.getParameter("tenOUT") == "" ? "0" : (request.getParameter("tenOUT"));
               // String fiveIN = request.getParameter("fiveIN") == "" ? "0" : (request.getParameter("fiveIN"));
               // String fiveOUT = request.getParameter("fiveOUT") == "" ? "0" : (request.getParameter("fiveOUT"));
               // String twoIN = request.getParameter("twoIN") == "" ? "0" : (request.getParameter("twoIN"));
               // String twoOUT = request.getParameter("twoOUT") == "" ? "0" : (request.getParameter("twoOUT"));
               // String oneIN = request.getParameter("oneIN") == "" ? "0" : (request.getParameter("oneIN"));
               // String oneOUT = request.getParameter("oneOUT") == "" ? "0" : (request.getParameter("oneOUT"));
               // String fiftyPaisaIN = request.getParameter("fiftyPaisaIN") == "" ? "0" : (request.getParameter("fiftyPaisaIN"));
               // String fiftyPaisaOUT = request.getParameter("fiftyPaisaOUT") == "" ? "0" : (request.getParameter("fiftyPaisaOUT"));
               // String onePaisaIN = request.getParameter("onePaisaIN") == "" ? "0" : (request.getParameter("onePaisaIN"));
               // String onePaisaOUT = request.getParameter("onePaisaOUT") == "" ? "0" : (request.getParameter("onePaisaOUT"));
               // String twohundredIN = request.getParameter("twohundredIN") == "" ? "0" : (request.getParameter("twohundredIN"));
               // String twohundredOUT = request.getParameter("twohundredOUT") == "" ? "0" : (request.getParameter("twohundredOUT"));


                proc.setString(1, pacsId);
                proc.setString(2, makerId);
                proc.setString(3, "DR");
                proc.setString(4, oDepositTransferBean.getAccNo());
                proc.setString(6, oDepositTransferBean.getTrAmount());
                proc.setString(7, oDepositTransferBean.getNarration());
                proc.setString(5, "To_Cash");
                proc.setString(8, oDepositTransferBean.getCheckOption());
                proc.setString(9, twothouIN);
                proc.setString(10, fivehundredIN);
                proc.setString(11, hundredIN);
                proc.setString(12, fiftyIN);
                proc.setString(13, twentyIN);
                proc.setString(14, tenIN);
                proc.setString(15, fiveIN);
                proc.setString(16, twoIN);
                proc.setString(17, oneIN);
                proc.setString(18, fiftyPaisaIN);
                proc.setString(19, onePaisaIN);
                proc.setString(20, twothouOUT);
                proc.setString(21, fivehundredOUT);
                proc.setString(22, hundredOUT);
                proc.setString(23, fiftyOUT);
                proc.setString(24, twentyOUT);
                proc.setString(25, tenOUT);
                proc.setString(26, fiveOUT);
                proc.setString(27, twoOUT);
                proc.setString(28, oneOUT);
                proc.setString(29, fiftyPaisaOUT);
                proc.setString(30, onePaisaOUT);
                proc.registerOutParameter(31, java.sql.Types.VARCHAR);
                proc.registerOutParameter(32, java.sql.Types.VARCHAR);
                proc.setString(33, twohundredIN);
                proc.setString(34, twohundredOUT);

                proc.execute();

                //message = proc.getString(32);


            } catch (SQLException ex) {
               // Logger.getLogger(DepositGlTransactionServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error occurred during processing.");
            } finally {
                try {
                    proc.close();
                } catch (SQLException e) {
                    System.out.println("Error occurred during proc close.");
                }
                try {
                    con.close();
                } catch (SQLException ex) {
                   // Logger.getLogger(DepositGlTransactionServlet.class.getName()).log(Level.SEVERE, null, ex);
                   System.out.println("Error occurred during connection close.");
                }
            }
        }
        request.setAttribute("message", message);
        request.getRequestDispatcher("/Deposit/DepositTransferClosure.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>
}

