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

package Controller;

import Controller.UploadServlet;
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.Statement;
import java.util.ArrayList;
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.Asset_EnquiryBean;
import LoginDb.DbHandler;
import java.sql.SQLException;
import oracle.jdbc.OracleTypes;


/**
 *
 * @author 1319104
 */
public class Asset_EnquiryServlet extends HttpServlet {
       protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession(false);
        Connection connection = null;
        ResultSet rs = null;
        Statement statement = null;
        CallableStatement proc = null;
        int searchFound = 0;
        String message = "";
        String pacsId = (String) session.getAttribute("pacsId");

            Asset_EnquiryBean oAsset_EnquiryBean = new Asset_EnquiryBean();

            ArrayList<Asset_EnquiryBean> alAsset_EnquiryBean = new ArrayList<Asset_EnquiryBean>();
            try {
                //BeanUtils.populate(oAsset_EnquiryBean, request.getParameterMap());
            } catch (IllegalAccessException ex) {
               // Logger.getLogger(Asset_EnquiryServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error occurred during processing.");
            } catch (InvocationTargetException ex) {
               // Logger.getLogger(Asset_EnquiryServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error occurred during processing.");
            }
            try {
                connection = DbHandler.getDBConnection();
                try {
                    proc = connection.prepareCall("{  call ASSET_OPERATION.asset_Enquiry(?,?,?,?) }");
                } catch (SQLException ex) {
                   // Logger.getLogger(Asset_EnquiryServlet.class.getName()).log(Level.SEVERE, null, ex);
                   System.out.println("Error occurred during processing.");
                }

                proc.setString(1, oAsset_EnquiryBean.getAsset_mst_id());
                proc.setString(2, pacsId);

                proc.registerOutParameter(3, OracleTypes.CURSOR);
                proc.registerOutParameter(4, java.sql.Types.VARCHAR);

                proc.executeUpdate();

               

                while (rs.next()) {
                    searchFound = 1;
                    oAsset_EnquiryBean = new Asset_EnquiryBean();
                    oAsset_EnquiryBean.setDetail_id(rs.getString("ID"));
                    oAsset_EnquiryBean.setAsset_id(rs.getString("ASSET_ID"));
                    oAsset_EnquiryBean.setDescription(rs.getString("DESCRIPTION"));
                    oAsset_EnquiryBean.setInt_value(rs.getString("INITIAL_VALUE"));
                    oAsset_EnquiryBean.setPres_val(rs.getString("PRESENT_VALUE"));
                    oAsset_EnquiryBean.setMode_of_aqr(rs.getString("ACQUIREMENT"));
                    oAsset_EnquiryBean.setEntry_date(rs.getString("last_eval_date"));
                    oAsset_EnquiryBean.setStatus(rs.getString("ACTIVE_FLAG"));
                    oAsset_EnquiryBean.setPurchase_date(rs.getString("entry_date"));
                    oAsset_EnquiryBean.setGlcode(rs.getString("asset_gl_class"));
                    oAsset_EnquiryBean.setDepRate(rs.getString("depre_rate"));

                    alAsset_EnquiryBean.add(oAsset_EnquiryBean);

                    request.setAttribute("displayFlag", "Y");

                }

                


            } catch (SQLException ex) {
               // Logger.getLogger(UploadServlet.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 procedure close.");
                }
                try {
                    connection.close();
                } catch (SQLException ex) {
                   // Logger.getLogger(UploadServlet.class.getName()).log(Level.SEVERE, null, ex);
                   System.out.println("Error occurred during connection close.");
                }
            }
            if (searchFound == 0) {
                message = "Asset ID does not exist. Please enter correct Asset ID.";
                request.setAttribute("message", message);
            }

            request.setAttribute("oAsset_EnquiryBean", oAsset_EnquiryBean);
            request.setAttribute("alAsset_EnquiryBean", alAsset_EnquiryBean);
            request.setAttribute("message", message);

            request.getRequestDispatcher("/Asset_JSP/Asset_Enquiry.jsp").forward(request, response);
    }

    /**
     * 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
     */


    // <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>

}
