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

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

/**
 *
 * @author 594267
 */
public class SwapCashDenoServlet 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 {

        CashDenoBean oCashDenoBean = null;
        Connection conn = null;
        CallableStatement proc = null;
        HttpSession session = request.getSession();
        ResultSet rs = null;
        String pacsid = (String) session.getAttribute("pacsId");
        String user = (String) session.getAttribute("user");
       // String opType = (String) request.getParameter("operationType");
        String handle_id = (String) request.getParameter("handle_id");
        handle_id = handle_id == null ? "" : handle_id;
        Statement statement = null;
        String message = "";
        try {
            conn = DbHandler.getDBConnection();
            if (handle_id.isEmpty()) {
                oCashDenoBean = new CashDenoBean();
                try {
                    statement = conn.createStatement();
                  //  rs = statement.executeQuery("select nvl(cr.deno_2000,0),nvl(cr.deno_500,0),nvl(cr.deno_100,0),nvl(cr.deno_50,0),nvl(cr.deno_20,0),nvl(cr.deno_10,0),nvl(cr.deno_5,0),nvl(cr.deno_2,0),nvl(cr.deno_1,0),nvl(cr.deno_50p,0),nvl(cr.deno_1p,0),nvl(cr.deno_200,0) from cash_reserve cr where cr.pacs_id='" + pacsid + "'");

                    while (rs.next()) {
                        oCashDenoBean.setDeno_2000(rs.getString(1));
                        oCashDenoBean.setDeno_500(rs.getString(2));
                        oCashDenoBean.setDeno_100(rs.getString(3));
                        oCashDenoBean.setDeno_50(rs.getString(4));
                        oCashDenoBean.setDeno_20(rs.getString(5));
                        oCashDenoBean.setDeno_10(rs.getString(6));
                        oCashDenoBean.setDeno_5(rs.getString(7));
                        oCashDenoBean.setDeno_2(rs.getString(8));
                        oCashDenoBean.setDeno_1(rs.getString(9));
                        oCashDenoBean.setDeno_50p(rs.getString(10));
                        oCashDenoBean.setDeno_1p(rs.getString(11));
                        oCashDenoBean.setDeno_200(rs.getString(12));
                    }
                } catch (Exception e) {
                   // Logger.getLogger(SwapCashDenoServlet.class.getName()).log(Level.SEVERE, null, e);
                   System.out.println("Error occurred during processing.");
                }
                
                request.setAttribute("oCashDenoBean", oCashDenoBean);
                request.getRequestDispatcher("/swapCashDeno.jsp").forward(request, response);

            } else if(!(handle_id.isEmpty()) &&handle_id.equalsIgnoreCase("update")){
                oCashDenoBean = new CashDenoBean();
                try {
                  //  BeanUtils.populate(oCashDenoBean, request.getParameterMap());
                } catch (IllegalAccessException ex) {
                   // Logger.getLogger(EnquiryServlet.class.getName()).log(Level.SEVERE, null, ex);
                   System.out.println("Error occurred during processing.");
                } catch (InvocationTargetException ex) {
                   // Logger.getLogger(EnquiryServlet.class.getName()).log(Level.SEVERE, null, ex);
                   System.out.println("Error occurred during processing.");
                }
                try {
                    proc = conn.prepareCall("{ call cash_denominations.swap_cash_reserve(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) }");
                } catch (Exception e) {
                   // Logger.getLogger(SwapCashDenoServlet.class.getName()).log(Level.SEVERE, null, e);
                   System.out.println("Error occurred during processing.");
                }
                try {
                    proc.setString(1, user);
                    proc.setString(2, oCashDenoBean.getDeno_2000());
                    proc.setString(3, oCashDenoBean.getDeno_500());
                    proc.setString(4, oCashDenoBean.getDeno_100());
                    proc.setString(5, oCashDenoBean.getDeno_50());
                    proc.setString(6, oCashDenoBean.getDeno_20());
                    proc.setString(7, oCashDenoBean.getDeno_10());
                    proc.setString(8, oCashDenoBean.getDeno_5());
                    proc.setString(9, oCashDenoBean.getDeno_2());
                    proc.setString(10, oCashDenoBean.getDeno_1());
                    proc.setString(11, oCashDenoBean.getDeno_50p());
                    proc.setString(12, oCashDenoBean.getDeno_1p());
                    proc.setString(13, pacsid);
                    proc.setString(14, opType);
                    proc.registerOutParameter(15, java.sql.Types.VARCHAR);
                    proc.setString(16, oCashDenoBean.getDeno_200());

                    proc.execute();

                  //  message = proc.getString(15);

                    statement = conn.createStatement();
                   // rs = statement.executeQuery("select nvl(cr.deno_2000,0),nvl(cr.deno_500,0),nvl(cr.deno_100,0),nvl(cr.deno_50,0),nvl(cr.deno_20,0),nvl(cr.deno_10,0),nvl(cr.deno_5,0),nvl(cr.deno_2,0),nvl(cr.deno_1,0),nvl(cr.deno_50p,0),nvl(cr.deno_1p,0),nvl(cr.deno_200,0) from cash_reserve cr where cr.pacs_id='" + pacsid + "'");

                    while (rs.next()) {
                        oCashDenoBean.setDeno_2000(rs.getString(1));
                        oCashDenoBean.setDeno_500(rs.getString(2));
                        oCashDenoBean.setDeno_100(rs.getString(3));
                        oCashDenoBean.setDeno_50(rs.getString(4));
                        oCashDenoBean.setDeno_20(rs.getString(5));
                        oCashDenoBean.setDeno_10(rs.getString(6));
                        oCashDenoBean.setDeno_5(rs.getString(7));
                        oCashDenoBean.setDeno_2(rs.getString(8));
                        oCashDenoBean.setDeno_1(rs.getString(9));
                        oCashDenoBean.setDeno_50p(rs.getString(10));
                        oCashDenoBean.setDeno_1p(rs.getString(11));
                        oCashDenoBean.setDeno_200(rs.getString(12));
                    }
                } catch (SQLException ex) {
                   // Logger.getLogger(SwapCashDenoServlet.class.getName()).log(Level.SEVERE, null, ex);
                   System.out.println("Error occurred during processing.");
                } finally {
                    try {
                        proc.close();
                    } catch (SQLException e) {
                       // Logger.getLogger(SwapCashDenoServlet.class.getName()).log(Level.SEVERE, null, e);
                       System.out.println("Error occurred during proc close.");
                    }
                }
                request.setAttribute("message", message);                
                request.setAttribute("oCashDenoBean", oCashDenoBean);
                request.getRequestDispatcher("/swapCashDeno.jsp").forward(request, response);
            }
        } catch (Exception e) {
           // Logger.getLogger(SwapCashDenoServlet.class.getName()).log(Level.SEVERE, null, e);
           System.out.println("Error occurred during processing.");
        } finally {
            try {
                conn.close();
            } catch (SQLException ex) {
               // Logger.getLogger(TradingProductOperationServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error occurred during connection 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);
    }

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