/*
 * 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.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 Shub
 */
public class TermDepositOperationServlet 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. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet transactionOperationServlet</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet transactionOperationServlet 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 {
        //processRequest(request, response);

        Connection con = null;
        CallableStatement proc = null;
        String message = "";
        String JSP = "";
        HttpSession session = request.getSession();
        JSP = request.getParameter("screenName") == null ? "" : (request.getParameter("screenName"));

        transactionOperationBean otransactionOperationBean = new transactionOperationBean();

        try {
           // BeanUtils.populate(otransactionOperationBean, 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.");
        }

        //For Loan

        if (JSP.equalsIgnoreCase("TDOperation")) {
          //  String transferAcc = request.getParameter("transferAcc") == null ? "" : (request.getParameter("transferAcc"));
           // String transferGLAcc = request.getParameter("transferGLAcc") == null ? "" : (request.getParameter("transferGLAcc"));
           // String transferMode = request.getParameter("payMode") == null ? "" : (request.getParameter("payMode"));

            TDOperationBean oTDOperationBean = new TDOperationBean();

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

            if(oTDOperationBean.getTranType().equalsIgnoreCase("R")){
                try {
                con = DbHandler.getDBConnection();
                try {

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

                } catch (SQLException ex) {
                   // Logger.getLogger(TermDepositOperationServlet.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, oTDOperationBean.getTranType());
                proc.setString(1, oTDOperationBean.getAccNo());
                proc.setString(2, oTDOperationBean.getTrmYr());
                proc.setString(3, oTDOperationBean.getTrmMon());
                proc.setString(4, oTDOperationBean.getTrmDay());
                proc.registerOutParameter(5, java.sql.Types.VARCHAR);

                proc.execute();

               // message = proc.getString(5);
                } catch (SQLException ex) {
               // Logger.getLogger(TermDepositOperationServlet.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.commit();
                    con.close();
                } catch (SQLException ex) {
                   // Logger.getLogger(TermDepositOperationServlet.class.getName()).log(Level.SEVERE, null, ex);
                   System.out.println("Error occurred during connection close.");
                }

            }

            } else {

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

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

                } catch (SQLException ex) {
                   // Logger.getLogger(TermDepositOperationServlet.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"));

                //added for interest portion
               // String twothouIN_intt = (String) request.getParameter("twothouIN_intt") == "" ? "0" : (request.getParameter("twothouIN_intt"));
                //String twothouOUT_intt = request.getParameter("twothouOUT_intt") == "" ? "0" : (request.getParameter("twothouOUT_intt"));
                //String fivehundredIN_intt = request.getParameter("fivehundredIN_intt") == "" ? "0" : (request.getParameter("fivehundredIN_intt"));
                //String fivehundredOUT_intt = request.getParameter("fivehundredOUT_intt") == "" ? "0" : (request.getParameter("fivehundredOUT_intt"));
                //String hundredIN_intt = request.getParameter("hundredIN_intt") == "" ? "0" : (request.getParameter("hundredIN_intt"));
                //String hundredOUT_intt = request.getParameter("hundredOUT_intt") == "" ? "0" : (request.getParameter("hundredOUT_intt"));
                //String fiftyIN_intt = request.getParameter("fiftyIN_intt") == "" ? "0" : (request.getParameter("fiftyIN_intt"));
                //String fiftyOUT_intt = request.getParameter("fiftyOUT_intt") == "" ? "0" : (request.getParameter("fiftyOUT_intt"));
                //String twentyIN_intt = request.getParameter("twentyIN_intt") == "" ? "0" : (request.getParameter("twentyIN_intt"));
                //String twentyOUT_intt = request.getParameter("twentyOUT_intt") == "" ? "0" : (request.getParameter("twentyOUT_intt"));
                //String tenIN_intt = request.getParameter("tenIN_intt") == "" ? "0" : (request.getParameter("tenIN_intt"));
                //String tenOUT_intt = request.getParameter("tenOUT_intt") == "" ? "0" : (request.getParameter("tenOUT_intt"));
                //String fiveIN_intt = request.getParameter("fiveIN_intt") == "" ? "0" : (request.getParameter("fiveIN_intt"));
                //String fiveOUT_intt = request.getParameter("fiveOUT_intt") == "" ? "0" : (request.getParameter("fiveOUT_intt"));
                //String twoIN_intt = request.getParameter("twoIN_intt") == "" ? "0" : (request.getParameter("twoIN_intt"));
                //String twoOUT_intt = request.getParameter("twoOUT_intt") == "" ? "0" : (request.getParameter("twoOUT_intt"));
                //String oneIN_intt = request.getParameter("oneIN_intt") == "" ? "0" : (request.getParameter("oneIN_intt"));
                //String oneOUT_intt = request.getParameter("oneOUT_intt") == "" ? "0" : (request.getParameter("oneOUT_intt"));
                //String fiftyPaisaIN_intt = request.getParameter("fiftyPaisaIN_intt") == "" ? "0" : (request.getParameter("fiftyPaisaIN_intt"));
                //String fiftyPaisaOUT_intt = request.getParameter("fiftyPaisaOUT_intt") == "" ? "0" : (request.getParameter("fiftyPaisaOUT_intt"));
                //String onePaisaIN_intt = request.getParameter("onePaisaIN_intt") == "" ? "0" : (request.getParameter("onePaisaIN_intt"));
                //String onePaisaOUT_intt = request.getParameter("onePaisaOUT_intt") == "" ? "0" : (request.getParameter("onePaisaOUT_intt"));                
                //String twohundredIN_intt = request.getParameter("twohundredIN_intt") == "" ? "0" : (request.getParameter("twohundredIN_intt"));
                //String twohundredOUT_intt = request.getParameter("twohundredOUT_intt") == "" ? "0" : (request.getParameter("twohundredOUT_intt"));


                proc.setString(1, pacsId);
                proc.setString(2, oTDOperationBean.getTranType());
                proc.setString(3, oTDOperationBean.getAccNo());
                if(oTDOperationBean.getTranType().equalsIgnoreCase("W") || oTDOperationBean.getTranType().equalsIgnoreCase("P"))
                    proc.setString(4, oTDOperationBean.getNetAmt());
                else if(oTDOperationBean.getTranType().equalsIgnoreCase("D"))
                    proc.setString(4, oTDOperationBean.getTrAmount());
                proc.setString(5, oTDOperationBean.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, twothouIN_intt);
                proc.setString(32, fivehundredIN_intt);
                proc.setString(33, hundredIN_intt);
                proc.setString(34, fiftyIN_intt);
                proc.setString(35, twentyIN_intt);
                proc.setString(36, tenIN_intt);
                proc.setString(37, fiveIN_intt);
                proc.setString(38, twoIN_intt);
                proc.setString(39, oneIN_intt);
                proc.setString(40, fiftyPaisaIN_intt);
                proc.setString(41, onePaisaIN_intt);
                proc.setString(42, twothouOUT_intt);
                proc.setString(43, fivehundredOUT_intt);
                proc.setString(44, hundredOUT_intt);
                proc.setString(45, fiftyOUT_intt);
                proc.setString(46, twentyOUT_intt);
                proc.setString(47, tenOUT_intt);
                proc.setString(48, fiveOUT_intt);
                proc.setString(49, twoOUT_intt);
                proc.setString(50, oneOUT_intt);
                proc.setString(51, fiftyPaisaOUT_intt);
                proc.setString(52, onePaisaOUT_intt);
                proc.setString(53, oTDOperationBean.getIntAdjAmt());
                proc.registerOutParameter(54, java.sql.Types.VARCHAR);
                proc.setString(55, twohundredIN);
                proc.setString(56, twohundredOUT);
                proc.setString(57, twohundredIN_intt);
                proc.setString(58, twohundredOUT_intt);
                proc.setString(59, transferGLAcc);

                proc.execute();

               // message = proc.getString(54);


            } catch (SQLException ex) {
               // Logger.getLogger(TermDepositOperationServlet.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.commit();
                    con.close();
                } catch (SQLException ex) {
                   // Logger.getLogger(TermDepositOperationServlet.class.getName()).log(Level.SEVERE, null, ex);
                   System.out.println("Error occurred during connection close.");
                }

            }
            }
        }

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