

/*
 * 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.lang.reflect.InvocationTargetException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
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.tradingProductCreationBean;

/**
 *
 * @author 590685
 */
public class TradingProductOperationServlet 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 ServletName = request.getParameter("handle_id");
        tradingProductCreationBean tradingProductCreationBeanObj = null;
        tradingProductCreationBeanObj = new tradingProductCreationBean();
        System.out.println(tradingProductCreationBeanObj.getPricePerUnit());


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

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

                proc.setString(1, tradingProductCreationBeanObj.getProduct_id());
                proc.setString(2, tradingProductCreationBeanObj.getCommoditytype());
                proc.setString(3, tradingProductCreationBeanObj.getTypeDesc());
                proc.setString(4, tradingProductCreationBeanObj.getCommoditysubtype());
                proc.setString(5, tradingProductCreationBeanObj.getSubtypeDesc());
                proc.setString(6, tradingProductCreationBeanObj.getUnit());
                proc.setString(7, tradingProductCreationBeanObj.getStatus());
                proc.setString(8, ServletName);

                proc.registerOutParameter(9, java.sql.Types.VARCHAR);
                proc.setString(10, tradingProductCreationBeanObj.getLease());
                proc.setString(11, tradingProductCreationBeanObj.getPricePerUnit());
                proc.setString(12, tradingProductCreationBeanObj.getPriceFreq());
                proc.setString(13, tradingProductCreationBeanObj.getMinDays());
                proc.setString(14, tradingProductCreationBeanObj.getMaxDays());
                proc.setString(15, tradingProductCreationBeanObj.getLandArea());
                proc.setString(16, tradingProductCreationBeanObj.getCgst());
                proc.setString(17, tradingProductCreationBeanObj.getSgst());
                proc.setString(18, tradingProductCreationBeanObj.getPacsId());
                proc.setString(19, tradingProductCreationBeanObj.getPurchaseID());
                proc.setString(20, tradingProductCreationBeanObj.getSaleID());
                proc.setString(21, tradingProductCreationBeanObj.getCloseID());
                proc.setString(22, tradingProductCreationBeanObj.getTradeID());

                proc.execute();

              //  message = proc.getString(9);

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

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

            }

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

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

               // ResultSet rs = statement.executeQuery(" select ID, comm_type, TYPE_DESC, comm_subtype, SUBTYPE_DESC, UNIT, active_flag, nvl(lease_flag, 'N') lease_flag, price_per_unit, price_freq, min_trm, max_trm, land_area, cgst, sgst, pacs_id, purchase_gl_id, (select gp.gl_name from gl_product gp where gp.id = purchase_gl_id) purchase_gl_name, sale_gl_id, (select gp.gl_name from gl_product gp where gp.id = sale_gl_id) sale_gl_name, closing_stock_gl, (select gp.gl_name from gl_product gp where gp.id = closing_stock_gl) closing_stock_gl_name, stock_trade_gl, (select gp.gl_name from gl_product gp where gp.id = stock_trade_gl) stock_trade_gl_name   from trading_product_mst  where id = '" + tradingProductCreationBeanObj.getProduct_id() + "' and pacs_id = '" +pacsId+ "' ");

                while (rs.next()) {

                    tradingProductCreationBeanObj.setCommoditytype(rs.getString("comm_type"));
                    tradingProductCreationBeanObj.setTypeDesc(rs.getString("TYPE_DESC"));
                    tradingProductCreationBeanObj.setCommoditysubtype(rs.getString("comm_subtype"));
                    tradingProductCreationBeanObj.setSubtypeDesc(rs.getString("SUBTYPE_DESC"));
                    tradingProductCreationBeanObj.setUnit(rs.getString("UNIT"));
                    tradingProductCreationBeanObj.setStatus(rs.getString("active_flag"));
                    tradingProductCreationBeanObj.setProduct_id(rs.getString("ID"));
                    tradingProductCreationBeanObj.setLease(rs.getString("lease_flag"));
                    tradingProductCreationBeanObj.setPricePerUnit(rs.getString("price_per_unit"));
                    tradingProductCreationBeanObj.setPriceFreq(rs.getString("price_freq"));
                    tradingProductCreationBeanObj.setMinDays(rs.getString("min_trm"));
                    tradingProductCreationBeanObj.setMaxDays(rs.getString("max_trm"));
                    tradingProductCreationBeanObj.setLandArea(rs.getString("land_area"));
                    tradingProductCreationBeanObj.setCgst(rs.getString("cgst"));
                    tradingProductCreationBeanObj.setSgst(rs.getString("sgst"));
                    tradingProductCreationBeanObj.setPacsId(rs.getString("pacs_id"));
                    tradingProductCreationBeanObj.setPurchaseGL(rs.getString("purchase_gl_name"));
                    tradingProductCreationBeanObj.setPurchaseID(rs.getString("purchase_gl_id"));
                    tradingProductCreationBeanObj.setSaleGL(rs.getString("sale_gl_name"));
                    tradingProductCreationBeanObj.setSaleID(rs.getString("sale_gl_id"));
                    tradingProductCreationBeanObj.setCloseGL(rs.getString("closing_stock_gl_name"));
                    tradingProductCreationBeanObj.setCloseID(rs.getString("closing_stock_gl"));
                    tradingProductCreationBeanObj.setTradeGL(rs.getString("stock_trade_gl_name"));
                    tradingProductCreationBeanObj.setTradeID(rs.getString("stock_trade_gl"));

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

                }

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

            } catch (SQLException ex) {
               // Logger.getLogger(TradingProductOperationServlet.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 = "Trading Commodity type/Subtype Code not exists in the system";
                request.setAttribute("message", message);
            }
            request.setAttribute("ServletName","Amend");
            request.setAttribute("tradingProductCreationBeanObj", tradingProductCreationBeanObj);

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

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

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

                proc.setString(1, tradingProductCreationBeanObj.getProduct_id());
                proc.setString(2, tradingProductCreationBeanObj.getCommoditytypeAmend());
                proc.setString(3, tradingProductCreationBeanObj.getTypeDescAmend());
                proc.setString(4, tradingProductCreationBeanObj.getCommoditysubtypeAmend());
                proc.setString(5, tradingProductCreationBeanObj.getSubtypeDescAmend());
                proc.setString(6, tradingProductCreationBeanObj.getUnitAmend());
                proc.setString(7, tradingProductCreationBeanObj.getStatusAmend());
                proc.setString(8, ServletName);

                proc.registerOutParameter(9, java.sql.Types.VARCHAR);
                proc.setString(10, tradingProductCreationBeanObj.getLeaseAmend());
                proc.setString(11, tradingProductCreationBeanObj.getPricePerUnitAmend());
                proc.setString(12, tradingProductCreationBeanObj.getPriceFreqAmend());
                proc.setString(13, tradingProductCreationBeanObj.getMinDaysAmend());
                proc.setString(14, tradingProductCreationBeanObj.getMaxDaysAmend());
                proc.setString(15, tradingProductCreationBeanObj.getLandAreaAmend());
                proc.setString(16, tradingProductCreationBeanObj.getCgstAmend());
                proc.setString(17, tradingProductCreationBeanObj.getSgstAmend());
                proc.setString(18, tradingProductCreationBeanObj.getPacsId());
                proc.setString(19, tradingProductCreationBeanObj.getPurchaseIDAmend());
                proc.setString(20, tradingProductCreationBeanObj.getSaleIDAmend());
                proc.setString(21, tradingProductCreationBeanObj.getCloseIDAmend());
                proc.setString(22, tradingProductCreationBeanObj.getTradeIDAmend());

                proc.execute();

               // message = proc.getString(9);

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

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