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

import DataEntryBean.PaymentAcknowledgementBean;
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 986137
 */
public class PaymentAcknowledgementServlet 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 {
        Connection con = null;
        CallableStatement proc = null;
        String message = "";
        HttpSession session = request.getSession();
       // String ServletName = request.getParameter("handle_id");

        PaymentAcknowledgementBean oPaymentAcknowledgementBean = new PaymentAcknowledgementBean();



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

        try {
            con = DbHandler.getDBConnection();
            try {
                proc = con.prepareCall("{ call TRADING_OPERATION.trading_payment_ack(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) }");
            } catch (SQLException ex) {
               // Logger.getLogger(UserMaintenanceServlet.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, ServletName);
            if ("V".equalsIgnoreCase(ServletName)) {
                proc.setString(2, oPaymentAcknowledgementBean.getVnetAmt());
                proc.setString(3, oPaymentAcknowledgementBean.getOrderno());
                proc.setString(4, oPaymentAcknowledgementBean.getVpayMode());
            } else if ("S".equalsIgnoreCase(ServletName)) {
                proc.setString(2, oPaymentAcknowledgementBean.getCnetAmt());
                proc.setString(3, oPaymentAcknowledgementBean.getSalesrefno());
                proc.setString(4, oPaymentAcknowledgementBean.getCpayMode());
            }
            
            proc.setString(5, makerId);
            proc.setString(6, twothouIN);
            proc.setString(7, fivehundredIN);
            proc.setString(8, hundredIN);
            proc.setString(9, fiftyIN);
            proc.setString(10, twentyIN);
            proc.setString(11, tenIN);
            proc.setString(12, fiveIN);
            proc.setString(13, twoIN);
            proc.setString(14, oneIN);
            proc.setString(15, fiftyPaisaIN);
            proc.setString(16, onePaisaIN);
            proc.setString(17, twothouOUT);
            proc.setString(18, fivehundredOUT);
            proc.setString(19, hundredOUT);
            proc.setString(20, fiftyOUT);
            proc.setString(21, twentyOUT);
            proc.setString(22, tenOUT);
            proc.setString(23, fiveOUT);
            proc.setString(24, twoOUT);
            proc.setString(25, oneOUT);
            proc.setString(26, fiftyPaisaOUT);
            proc.setString(27, onePaisaOUT);
            if ("V".equalsIgnoreCase(ServletName)) {
                proc.setString(28, oPaymentAcknowledgementBean.getVsgst());
                proc.setString(29, oPaymentAcknowledgementBean.getVcgst());
                proc.setString(31, oPaymentAcknowledgementBean.getVtrfAcc());
                proc.setString(34, oPaymentAcknowledgementBean.getVtobepaid());
            } else if ("S".equalsIgnoreCase(ServletName)) {
                proc.setString(28, oPaymentAcknowledgementBean.getCsgst());
                proc.setString(29, oPaymentAcknowledgementBean.getCcgst());
                proc.setString(31, oPaymentAcknowledgementBean.getCtrfAcc());
                proc.setString(34, oPaymentAcknowledgementBean.getCtobepaid());
            }
            proc.setString(30, oPaymentAcknowledgementBean.getDisAmt());
            proc.setString(32, pacsId);
            proc.setString(33, oPaymentAcknowledgementBean.getProd_id());
            proc.registerOutParameter(35, java.sql.Types.VARCHAR);
            proc.setString(36, twohundredIN);
            proc.setString(37, twohundredOUT);

            proc.execute();

           // message = proc.getString(35);

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

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

        }


    }

    // <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);
    }

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