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

import LoginDb.DbHandler;
import java.io.*;
import java.util.*;
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;
import DataEntryBean.LeaseCommodityBean;

/**
 *
 * @author 594267
 */
public class LeaseCommodityServlet 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(false);
        String ServletName = request.getParameter("handle_id");
        String pacsId = (String) session.getAttribute("pacsId");
        LeaseCommodityBean LeaseCommodityBeanObj = null;
        LeaseCommodityBeanObj = new LeaseCommodityBean();
        System.out.println(request.getParameter("prod_id"));

        try {
          //  BeanUtils.populate(LeaseCommodityBeanObj, request.getParameterMap());
        } catch (IllegalAccessException ex) {
           // Logger.getLogger(TradingProductOperationServlet.class.getName()).log(Level.SEVERE, null, ex);
           System.out.println("Error occurred during processing.");           
        } catch (InvocationTargetException ex) {
           // Logger.getLogger(TradingProductOperationServlet.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.lease_rent_commodity(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) }");
            } catch (SQLException ex) {
               // Logger.getLogger(TradingProductOperationServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error occurred during processing.");
            }

            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"));

           // LeaseCommodityBeanObj.setProd_Id(request.getParameter("prod_id"));
           // LeaseCommodityBeanObj.setTdLeaseID(request.getParameter("tdLeaseID"));
            proc.setString(1, LeaseCommodityBeanObj.getProd_Id());
            proc.setString(2, LeaseCommodityBeanObj.getCustID());
            proc.setString(3, LeaseCommodityBeanObj.getNoOfUnits());
            proc.setString(4, LeaseCommodityBeanObj.getPriceFreq());
            proc.setString(5, LeaseCommodityBeanObj.getTrmLen());
            proc.setString(6, LeaseCommodityBeanObj.getTotPayDue());
            proc.setString(7, LeaseCommodityBeanObj.getSecAmt());
            proc.setString(8, pacsId);
            proc.setString(9, LeaseCommodityBeanObj.getTranType());
            proc.setString(10, LeaseCommodityBeanObj.getTdLeaseID());
            proc.setString(11, null);
            proc.setString(12, makerId);
            proc.setString(13, twothouIN);
            proc.setString(14, fivehundredIN);
            proc.setString(15, hundredIN);
            proc.setString(16, fiftyIN);
            proc.setString(17, twentyIN);
            proc.setString(18, tenIN);
            proc.setString(19, fiveIN);
            proc.setString(20, twoIN);
            proc.setString(21, oneIN);
            proc.setString(22, fiftyPaisaIN);
            proc.setString(23, onePaisaIN);
            proc.setString(24, twothouOUT);
            proc.setString(25, fivehundredOUT);
            proc.setString(26, hundredOUT);
            proc.setString(27, fiftyOUT);
            proc.setString(28, twentyOUT);
            proc.setString(29, tenOUT);
            proc.setString(30, fiveOUT);
            proc.setString(31, twoOUT);
            proc.setString(32, oneOUT);
            proc.setString(33, fiftyPaisaOUT);
            proc.setString(34, onePaisaOUT);
            proc.setString(35, pacsId);
            proc.setString(36, LeaseCommodityBeanObj.getRentAmt());
            proc.registerOutParameter(37, java.sql.Types.VARCHAR);
            proc.setString(38, twohundredIN);
            proc.setString(39, twohundredOUT);

            proc.execute();

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

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