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

import DataEntryBean.RDOperationBean;
import DataEntryBean.transactionOperationBean;
import DataEntryBean.TDOperationBean;
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 594267
 */
public class RDOperationServlet 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 RDOperationServlet</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet RDOperationServlet 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"));

        if (JSP.equalsIgnoreCase("RDOperation")) {

           // String transferAcc = request.getParameter("transferAcc") == null ? "" : (request.getParameter("transferAcc"));
           // String transferGLAcc = request.getParameter("transferGLAcc") == null ? "" : (request.getParameter("transferGLAcc"));
           // String transferMode = request.getParameter("payMode") == null ? "C" : (request.getParameter("payMode"));

            RDOperationBean oRDOperationBean = new RDOperationBean();

            try {
               // BeanUtils.populate(oRDOperationBean, request.getParameterMap());
            } catch (IllegalAccessException ex) {
               // Logger.getLogger(transactionOperationServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error occurred during processing.");
            } catch (InvocationTargetException ex) {
               // Logger.getLogger(transactionOperationServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error occurred during processing.");
            }
            try {
                con = DbHandler.getDBConnection();
                try {

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

                } catch (SQLException ex) {
                   // Logger.getLogger(transactionOperationServlet.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 = request.getParameter("twothouIN").isEmpty() ? "0" : (request.getParameter("twothouIN"));
                //String twothouOUT = request.getParameter("twothouOUT").isEmpty() ? "0" : (request.getParameter("twothouOUT"));
                //String fivehundredIN = request.getParameter("fivehundredIN").isEmpty() ? "0" : (request.getParameter("fivehundredIN"));
                //String fivehundredOUT = request.getParameter("fivehundredOUT").isEmpty() ? "0" : (request.getParameter("fivehundredOUT"));
                //String hundredIN = request.getParameter("hundredIN").isEmpty() ? "0" : (request.getParameter("hundredIN"));
                //String hundredOUT = request.getParameter("hundredOUT").isEmpty() ? "0" : (request.getParameter("hundredOUT"));
                //String fiftyIN = request.getParameter("fiftyIN").isEmpty() ? "0" : (request.getParameter("fiftyIN"));
                //String fiftyOUT = request.getParameter("fiftyOUT").isEmpty() ? "0" : (request.getParameter("fiftyOUT"));
                //String twentyIN = request.getParameter("twentyIN").isEmpty() ? "0" : (request.getParameter("twentyIN"));
                //String twentyOUT = request.getParameter("twentyOUT").isEmpty() ? "0" : (request.getParameter("twentyOUT"));
                //String tenIN = request.getParameter("tenIN").isEmpty() ? "0" : (request.getParameter("tenIN"));
                //String tenOUT = request.getParameter("tenOUT").isEmpty() ? "0" : (request.getParameter("tenOUT"));
                //String fiveIN = request.getParameter("fiveIN").isEmpty() ? "0" : (request.getParameter("fiveIN"));
                //String fiveOUT = request.getParameter("fiveOUT").isEmpty() ? "0" : (request.getParameter("fiveOUT"));
                //String twoIN = request.getParameter("twoIN").isEmpty() ? "0" : (request.getParameter("twoIN"));
                //String twoOUT = request.getParameter("twoOUT").isEmpty() ? "0" : (request.getParameter("twoOUT"));
                //String oneIN = request.getParameter("oneIN").isEmpty() ? "0" : (request.getParameter("oneIN"));
                //String oneOUT = request.getParameter("oneOUT").isEmpty() ? "0" : (request.getParameter("oneOUT"));
                //String fiftyPaisaIN = request.getParameter("fiftyPaisaIN").isEmpty() ? "0" : (request.getParameter("fiftyPaisaIN"));
                //String fiftyPaisaOUT = request.getParameter("fiftyPaisaOUT").isEmpty() ? "0" : (request.getParameter("fiftyPaisaOUT"));
                //String onePaisaIN = request.getParameter("onePaisaIN").isEmpty() ? "0" : (request.getParameter("onePaisaIN"));
                //String onePaisaOUT = request.getParameter("onePaisaOUT").isEmpty() ? "0" : (request.getParameter("onePaisaOUT"));
                //String twohundredIN = request.getParameter("twohundredIN").isEmpty() ? "0" : (request.getParameter("twohundredIN"));
                //String twohundredOUT = request.getParameter("twohundredOUT").isEmpty() ? "0" : (request.getParameter("twohundredOUT"));

                proc.setString(1, pacsId);
                proc.setString(2, oRDOperationBean.getTranType());
                proc.setString(3, oRDOperationBean.getAccNo());
                if (oRDOperationBean.getTranType().equals("D")) {
                    proc.setString(4, oRDOperationBean.getInstAmt());
                } else if (oRDOperationBean.getTranType().equals("W")) {
                    proc.setString(4, oRDOperationBean.getNetAmt());
                } else if (oRDOperationBean.getTranType().equals("P")) {
                    proc.setString(4, oRDOperationBean.getNetAmt());
                }
                proc.setString(5, oRDOperationBean.getNarration());
                proc.setString(6, makerId);
                proc.setString(7, transferMode);
                proc.setString(8, transferAcc);
                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.setString(31, oRDOperationBean.getIntAdjAmt());
                proc.registerOutParameter(32, java.sql.Types.VARCHAR);
                proc.setString(33, twohundredIN);
                proc.setString(34, twohundredOUT);
                proc.setString(35, transferGLAcc);
                
                proc.execute();

                //message = proc.getString(32);
                Account_type = "3";

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

        request.setAttribute("message", message);


        request.getRequestDispatcher("/Deposit/RDOperation.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>
}
