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

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import DataEntryBean.RationingLedgerHeaderBean;
import DataEntryBean.RationingLedgerDetailsBean;
import LoginDb.DbHandler;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpSession;
import oracle.jdbc.OracleTypes;

/**
 *
 * @author 1320035
 */
public class RationingLedgerServlet 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 {
        HttpSession session = request.getSession(false);
        //String handle_id = request.getParameter("handle_id");
        Connection con = null;
        CallableStatement proc = null;
        String message = "";
        String pacsId = (String) session.getAttribute("pacsId");
        String user = (String) session.getAttribute("user");
        RationingLedgerHeaderBean orationingLedgerHeaderBean = null;
        RationingLedgerDetailsBean orationingLedgerDetailsBean = null;
        ResultSet rs = null;
        ArrayList<RationingLedgerDetailsBean> alRationingLedgerDetailsBeanApend = new ArrayList<RationingLedgerDetailsBean>();


        String productDetails = "";
        int searchFound = 0;
        //String customerId = request.getParameter("customerId");
        try {
            con = DbHandler.getDBConnection();
            proc = con.prepareCall("{ call pds_operations.pds_ration_ledger_view(?,?,?,?) }");

            proc.setString(1, customerId);
            proc.setString(2, pacsId);
            proc.registerOutParameter(3, OracleTypes.CURSOR);
            proc.registerOutParameter(4, java.sql.Types.VARCHAR);
            proc.execute();
            message = proc.getString(4);
            //rs = (ResultSet) proc.getObject(3);

            while (rs.next()) {
                searchFound = 1;
                orationingLedgerDetailsBean = new RationingLedgerDetailsBean();
                orationingLedgerDetailsBean.setProductTypeDetails(rs.getString(1));
                orationingLedgerDetailsBean.setProductSubTypeDetails(rs.getString(2));
                orationingLedgerDetailsBean.setEligibilityBeginDate(rs.getString("ELIG_FROM_DATE"));
                orationingLedgerDetailsBean.setEligibilityEndDate(rs.getString("ELIG_TO_DATE"));
                orationingLedgerDetailsBean.setExpiryDate(rs.getString("exp_DATE"));
                orationingLedgerDetailsBean.setBaseQuantity(rs.getString("BASE_QTY"));
                orationingLedgerDetailsBean.setAvailedQuantity(rs.getString("QTY_AVAILED"));
                orationingLedgerDetailsBean.setQuantityAvailable(rs.getString("QTY_REMAINING"));



                alRationingLedgerDetailsBeanApend.add(orationingLedgerDetailsBean);
                request.setAttribute("displayFlag", "Y");
            }

            orationingLedgerHeaderBean=new RationingLedgerHeaderBean();
           // orationingLedgerHeaderBean.setCardHolderName(request.getParameter("cardHolderName"));
            //orationingLedgerHeaderBean.setCardType(request.getParameter("cardType"));
            //orationingLedgerHeaderBean.setCardNo(request.getParameter("cardNo"));
            


            if (searchFound == 0) {

                message = "No Details Exist for the selected criteria";
                request.setAttribute("message", message);

            }

        } catch (SQLException ex) {
           // Logger.getLogger(RationingLedgerServlet.class.getName()).log(Level.SEVERE, null, ex);
           // System.out.println(ex.toString());
           System.out.println("Error occurred during processing.");
        } finally {

            try {
                proc.close();
                con.close();
            } catch (SQLException e) {
               // System.out.println(e.toString());
               System.out.println("Error occurred during connection close.");
            }
        }

        request.setAttribute("oRationingLedgerHeaderBean", orationingLedgerHeaderBean);
         request.setAttribute("alRationingLedgerDetailsBeanApend", alRationingLedgerDetailsBeanApend);
        request.getRequestDispatcher("/Pds_JSP/pdsRationingLedger.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>
}
