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

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;
import DataEntryBean.GodownBean;




/**
 *
 * @author 590685
 */
public class GodownServlet 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);
        Connection con = null;
        CallableStatement proc = null;
        String message = "";
        String pacsId = (String) session.getAttribute("pacsId");
        String user = (String) session.getAttribute("user");

       // String ServletName = request.getParameter("handle_id");


        GodownBean GodownBeanObj = new GodownBean();


        if ("Create".equalsIgnoreCase(ServletName)) {

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

            try {
                con = DbHandler.getDBConnection();
                try {
                    proc = con.prepareCall("{ call parameter.Upsert_godown(?,?,?,?,?,?,?,?,?) }");
                } catch (SQLException ex) {
                   // Logger.getLogger(GodownServlet.class.getName()).log(Level.SEVERE, null, ex);
                   System.out.println("Error Occurred during processing.");
                }

               proc.setString(1, GodownBeanObj.getGodown_id());
                proc.setString(2, GodownBeanObj.getName());
                proc.setString(3, GodownBeanObj.getAddress());
                 proc.setString(4, GodownBeanObj.getPhone());
                proc.setString(5, GodownBeanObj.getContactperson());
                proc.setString(6, GodownBeanObj.getCapacity());
                proc.setString(7, pacsId);
                proc.setString(8, ServletName);

                proc.registerOutParameter(9, java.sql.Types.VARCHAR);

                proc.execute();

              //  message = proc.getString(9);

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

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

            }

        } else if ("Search".equalsIgnoreCase(ServletName)) {

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

            Connection connection = null;
            ResultSet resultset = null;
            Statement statement = null;
            connection = DbHandler.getDBConnection();
            int SeachFound = 0;

            try {
                statement = connection.createStatement();
            } catch (SQLException ex) {
               // Logger.getLogger(GodownServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error Occurred during processing.");
            }
            try {

               // ResultSet rs = statement.executeQuery("select ID,GODOWN_NAME, GODOWN_ADDRESS, GODOWN_SPOC, PHN_NO, CAPACITY,AVAILABLE_CAP from TRADING_GODOWN_MST where ID= '" + GodownBeanObj.getGodown_id() + "' ");

                while (rs.next()) {

                    GodownBeanObj.setName(rs.getString("GODOWN_NAME"));
                    GodownBeanObj.setAddress(rs.getString("GODOWN_ADDRESS"));
                     GodownBeanObj.setPhone(rs.getString("PHN_NO"));
                    GodownBeanObj.setContactperson(rs.getString("GODOWN_SPOC"));
                    GodownBeanObj.setCapacity(rs.getString("CAPACITY"));
                    GodownBeanObj.setAvcapacity(rs.getString("AVAILABLE_CAP"));
                    GodownBeanObj.setGodown_id(rs.getString("ID"));




                    SeachFound = 1;
                    request.setAttribute("displayFlag", "Y");

                }

               // statement.close();
               // connection.close();

            } catch (SQLException ex) {
               // Logger.getLogger(GodownServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error Occurred during processing.");
            } finally {
                try {
                    if(statement !=null)
                        statement.close();
                    if (connection != null)
                    connection.close();

            } catch (Exception e) {               
               System.out.println("Error Occurred during connection close.");
            }

            }

            if (SeachFound == 0) {
                message = "Godown does not exist in the system";
                request.setAttribute("message", message);
            }
            request.setAttribute("GodownBeanObj", GodownBeanObj);

            request.getRequestDispatcher("/Trading_JSP/Godown.jsp").forward(request, response);

        } else if ("Update".equalsIgnoreCase(ServletName)) {

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

            try {
                con = DbHandler.getDBConnection();
                try {
                    proc = con.prepareCall("{ call parameter.Upsert_godown(?,?,?,?,?,?,?,?,?) }");
                } catch (SQLException ex) {
                   // Logger.getLogger(GodownServlet.class.getName()).log(Level.SEVERE, null, ex);
                   System.out.println("Error Occurred during processing.");
                }

                proc.setString(1, GodownBeanObj.getGodown_id());
                proc.setString(2, GodownBeanObj.getNameAmend());
                proc.setString(3, GodownBeanObj.getAddressAmend());
                proc.setString(4, GodownBeanObj.getPhoneAmend());
                proc.setString(5, GodownBeanObj.getContactpersonAmend());
                proc.setString(6, GodownBeanObj.getCapacityAmend());
                proc.setString(7, GodownBeanObj.getAvcapacityAmend());
                proc.setString(8, ServletName);

                proc.registerOutParameter(9, java.sql.Types.VARCHAR);

                proc.execute();

              //  message = proc.getString(9);

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

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