/*
 * 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.pdsProductCreationBean;

/**
 *
 * @author 590685
 */
public class PdsProductOperationServlet 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 = "";

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

        pdsProductCreationBean pdsProductCreationBeanObj=new pdsProductCreationBean();


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

            try {
               // BeanUtils.populate(pdsProductCreationBeanObj, request.getParameterMap());
            } catch (IllegalAccessException ex) {
               // Logger.getLogger(PdsProductOperationServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error occurred during processing.");
            } catch (InvocationTargetException ex) {
               // Logger.getLogger(PdsProductOperationServlet.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_pds_product(?,?,?,?,?,?,?,?,?) }");
                } catch (SQLException ex) {
                   // Logger.getLogger(PdsProductOperationServlet.class.getName()).log(Level.SEVERE, null, ex);
                   System.out.println("Error occurred during processing.");
                }
proc.setString(1, pdsProductCreationBeanObj.getProduct_id());
 proc.setString(2, pdsProductCreationBeanObj.getProductType());
  proc.setString(3, pdsProductCreationBeanObj.getProductDescription());
  proc.setString(4, pdsProductCreationBeanObj.getProductSubType());
                proc.setString(5, pdsProductCreationBeanObj.getProductSubTypeDescription());
                proc.setString(6, pdsProductCreationBeanObj.getUnit());
                proc.setString(7, pdsProductCreationBeanObj.getStatus());






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

                proc.execute();

               // message = proc.getString(9);

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

                request.setAttribute("message", message);
                request.getRequestDispatcher("/Pds_JSP/pdsProductCreation.jsp").forward(request, response);

            }

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

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

                //ResultSet rs = statement.executeQuery("select ID,PRODUCT_NAME,PRODUCT_CODE,UNIT,STATUS,PROFIT_FACTOR from pds_product_mst where product_code= '" + pdsProductCreationBeanObj.getInventorySearch() + "'");
                //ResultSet rs = statement.executeQuery("select ID,ITEM_TYPE,TYPE_DESC,UNIT,STATUS,ITEM_SUBTYPE,SUBTYPE_DESC from pds_product_mst where ID= '" + pdsProductCreationBeanObj.getProduct_id_search() + "'");
                while (rs.next()) {

                    pdsProductCreationBeanObj.setProduct_id(rs.getString("ID"));
                    //pdsProductCreationBeanObj.setProductName(rs.getString("PRODUCT_NAME"));
                    pdsProductCreationBeanObj.setProductType(rs.getString("ITEM_TYPE"));
                    pdsProductCreationBeanObj.setProductDescription(rs.getString("TYPE_DESC"));
                    pdsProductCreationBeanObj.setProductSubType(rs.getString("ITEM_SUBTYPE"));
                    pdsProductCreationBeanObj.setProductSubTypeDescription(rs.getString("SUBTYPE_DESC"));
                    //pdsProductCreationBeanObj.setProductCode(rs.getString("PRODUCT_CODE"));
                    pdsProductCreationBeanObj.setUnit(rs.getString("UNIT"));
                    pdsProductCreationBeanObj.setStatus(rs.getString("STATUS"));
                    //pdsProductCreationBeanObj.setProfitFactor(rs.getString("PROFIT_FACTOR"));

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

                }

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

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

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

            request.getRequestDispatcher("/Pds_JSP/pdsProductCreation.jsp").forward(request, response);

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

            try {
               // BeanUtils.populate(pdsProductCreationBeanObj, request.getParameterMap());
            } catch (IllegalAccessException ex) {
               // Logger.getLogger(PdsProductOperationServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error occurred during processing.");
            } catch (InvocationTargetException ex) {
               // Logger.getLogger(PdsProductOperationServlet.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_pds_product(?,?,?,?,?,?,?,?,?) }");
                } catch (SQLException ex) {
                   // Logger.getLogger(PdsProductOperationServlet.class.getName()).log(Level.SEVERE, null, ex);
                   System.out.println("Error occurred during processing.");
                }
 proc.setString(1, pdsProductCreationBeanObj.getProduct_id());
 proc.setString(2, pdsProductCreationBeanObj.getProductTypeAmend());
                proc.setString(3, pdsProductCreationBeanObj.getProductDescriptionAmend());
                proc.setString(4, pdsProductCreationBeanObj.getProductSubTypeAmend());
                proc.setString(5, pdsProductCreationBeanObj.getProductSubTypeDescriptionAmend());
                proc.setString(6, pdsProductCreationBeanObj.getUnitAmend());
               proc.setString(7, pdsProductCreationBeanObj.getStatusAmend());
               proc.setString(8, ServletName);

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

                proc.execute();

               // message = proc.getString(9);

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

                request.setAttribute("message", message);
                request.getRequestDispatcher("/Pds_JSP/pdsProductCreation.jsp").forward(request, response);

            }

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

            //String productCodeActivation = request.getParameter("productCodeActivation");
           // String productDescriptionActivation = request.getParameter("productDescriptionActivation");
            //String productSubTypeDescriptionActivation = request.getParameter("productSubTypeDescriptionActivation");
            //String vFlag = request.getParameter("productStatusFlag");

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

                proc.setString(1, productDescriptionActivation);
                proc.setString(2, productSubTypeDescriptionActivation);
                proc.setString(3, vFlag);
                proc.registerOutParameter(4, java.sql.Types.VARCHAR);

                proc.execute();

                //message = proc.getString(4);

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

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