package Controller;

import DataEntryBean.DepositProductCreationBean;
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 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 org.apache.commons.beanutils.BeanUtils;

/**
 *
 * @author Tcs Helpdesk10
 */
public class DepositProductCreationServlet 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");

        DepositProductCreationBean oDepositProductCreationBean = new DepositProductCreationBean();

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

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

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

                proc.setString(1, oDepositProductCreationBean.getProductcode());
                proc.setString(2, oDepositProductCreationBean.getInttcategory());
                proc.setString(3, oDepositProductCreationBean.getProductname());
                proc.setString(4, oDepositProductCreationBean.getProductdescription());
                proc.setString(5, oDepositProductCreationBean.getIntcatdescription());
                proc.setString(6, oDepositProductCreationBean.getSegmentCode());
                proc.setString(7, "0");
                proc.setString(8, oDepositProductCreationBean.getStatus());
                proc.setString(9, oDepositProductCreationBean.getIntrate());
                proc.setString(10, oDepositProductCreationBean.getInttfrequency());
                proc.setString(11, oDepositProductCreationBean.getInttmethod());
                proc.setString(12, oDepositProductCreationBean.getMinbal());
                proc.setString(13, oDepositProductCreationBean.getMaxbal());
                proc.setString(14, oDepositProductCreationBean.getOdindicator());
                proc.setString(15, "0");
                proc.setString(16, oDepositProductCreationBean.getPenalinttfrequency());
                proc.setString(17, oDepositProductCreationBean.getPenalinttmethod());
                proc.setString(18, "0");
                proc.setString(19, "N");
                proc.setString(20, oDepositProductCreationBean.getDebitComp1());
                proc.setString(21, oDepositProductCreationBean.getDebitComp2());
                proc.setString(22, oDepositProductCreationBean.getCreditComp1());
                proc.setString(23, oDepositProductCreationBean.getCreditComp2());
                proc.setString(24, oDepositProductCreationBean.getMinwdl());
                proc.setString(25, oDepositProductCreationBean.getMaxwdl());
                proc.setString(26, oDepositProductCreationBean.getInttcapfrequency());
                proc.setString(27, oDepositProductCreationBean.getAllowdr());
                proc.setString(28, oDepositProductCreationBean.getAllowcr());
                proc.setString(29, oDepositProductCreationBean.getDormancypr());
                proc.setString(30, ServletName);
                proc.setString(31, oDepositProductCreationBean.getMaxterm());
                proc.setString(32, oDepositProductCreationBean.getMinterm());
                proc.setString(33, oDepositProductCreationBean.getDormancytrigger());
                proc.setString(34, oDepositProductCreationBean.getOverduerate());
                proc.setString(35, oDepositProductCreationBean.getRdFlag());
                proc.setString(36, oDepositProductCreationBean.getPreClosure());
                proc.setString(37, oDepositProductCreationBean.getInttpayoutfrequency());
                proc.setString(38, oDepositProductCreationBean.getGlCodeInttPAYABLE());
                proc.setString(39, oDepositProductCreationBean.getGlCodeInttPAID());
                proc.setString(40, "0");
                proc.setString(41, oDepositProductCreationBean.getHeadAccType());

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

                proc.execute();

               // message = proc.getString(42);

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

                request.setAttribute("message", message);
                request.getRequestDispatcher("/Deposit/ProductCreation.jsp").forward(request, response);

            }

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

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

               // ResultSet rs = statement.executeQuery("select dp.id, prod_code,int_cat,prod_name,prod_desc,"
                        + "intt_cat_desc,segment_code,gl_prod_id as Gl_Description,status,"
                        + "intt_rate,intt_freq,int_method,min_bal,max_bal,DEBIT_COMP1,DEBIT_COMP2,CREDIT_COMP1,CREDIT_COMP2,"
                        + "to_char(effec_dt,'dd/mm/yyyy') as eff_date, od_indicator,"
                        + "od_pen_int_rate,od_pen_intt_freq,od_pen_int_method,min_WDL,max_WDL,INT_CAP_FREQ,DR_TXN_FLAG,CR_TXN_FLAG,DORMANCY,MIN_TERM,MAX_TERM,DORM_TRIG,RD_FLAG,OVRDUE_RATE,PRE_CLOSURE_CHARGE,INTT_PAYOUT_FREQ,GL_ID_INTT_PAYABLE as payable,GL_ID_INTT_PAID as paid,CROP_INT_RATE,head_acc_type from dep_product dp"
                        + " where to_number(substr(prod_code,1,1)) < 5 and  int_cat= '" + oDepositProductCreationBean.getIntcatSearch() + "' AND prod_code='" + oDepositProductCreationBean.getProductcodeSearch() + "'");

                while (rs.next()) {

                    oDepositProductCreationBean.setDep_product_id(rs.getString("id"));
                    oDepositProductCreationBean.setProductcode(rs.getString("prod_code"));
                    oDepositProductCreationBean.setInttcategory(rs.getString("int_cat"));
                    oDepositProductCreationBean.setProductname(rs.getString("prod_name"));
                    oDepositProductCreationBean.setProductdescription(rs.getString("prod_desc"));
                    oDepositProductCreationBean.setIntcatdescription(rs.getString("intt_cat_desc"));
                    oDepositProductCreationBean.setSegmentCode(rs.getString("segment_code"));
                    oDepositProductCreationBean.setGlCode(rs.getString("Gl_Description"));
                    oDepositProductCreationBean.setStatus(rs.getString("status"));
                    oDepositProductCreationBean.setIntrate(rs.getString("intt_rate"));
                    oDepositProductCreationBean.setInttfrequency(rs.getString("intt_freq"));
                    oDepositProductCreationBean.setInttmethod(rs.getString("int_method"));
                    oDepositProductCreationBean.setMinbal(rs.getString("min_bal"));
                    oDepositProductCreationBean.setMaxbal(rs.getString("max_bal"));
                    oDepositProductCreationBean.setEffectDate(rs.getString("eff_date"));
                    oDepositProductCreationBean.setOdindicator(rs.getString("od_indicator"));
                    oDepositProductCreationBean.setPenalinttrate(rs.getString("od_pen_int_rate"));
                    oDepositProductCreationBean.setPenalinttfrequency(rs.getString("od_pen_intt_freq"));
                    oDepositProductCreationBean.setPenalinttmethod(rs.getString("od_pen_int_method"));
                    oDepositProductCreationBean.setMinwdl(rs.getString("min_wdl"));
                    oDepositProductCreationBean.setMaxwdl(rs.getString("max_wdl"));
                    oDepositProductCreationBean.setInttcapfrequency(rs.getString("INT_CAP_FREQ"));
                    oDepositProductCreationBean.setAllowdr(rs.getString("DR_TXN_FLAG"));
                    oDepositProductCreationBean.setAllowcr(rs.getString("CR_TXN_FLAG"));
                    oDepositProductCreationBean.setDormancypr(rs.getString("DORMANCY"));
                    oDepositProductCreationBean.setDebitComp1(rs.getString("DEBIT_COMP1"));
                    oDepositProductCreationBean.setDebitComp2(rs.getString("DEBIT_COMP2"));
                    oDepositProductCreationBean.setCreditComp1(rs.getString("CREDIT_COMP1"));
                    oDepositProductCreationBean.setCreditComp2(rs.getString("CREDIT_COMP2"));
                    oDepositProductCreationBean.setMinterm(rs.getString("MIN_TERM"));
                    oDepositProductCreationBean.setMaxterm(rs.getString("MAX_TERM"));
                    oDepositProductCreationBean.setDormancytrigger(rs.getString("DORM_TRIG"));
                    oDepositProductCreationBean.setRdFlag(rs.getString("RD_FLAG"));
                    oDepositProductCreationBean.setOverduerate(rs.getString("OVRDUE_RATE"));
                    oDepositProductCreationBean.setPreClosure(rs.getString("PRE_CLOSURE_CHARGE"));
                    oDepositProductCreationBean.setInttpayoutfrequency(rs.getString("INTT_PAYOUT_FREQ"));
                    oDepositProductCreationBean.setGlCodeInttPAYABLE(rs.getString("payable"));
                    oDepositProductCreationBean.setGlCodeInttPAID(rs.getString("paid"));
                    oDepositProductCreationBean.setCropIns(rs.getString("CROP_INT_RATE"));
                    oDepositProductCreationBean.setHeadAccType(rs.getString("head_acc_type"));


                    SeachFound = 1;
                    request.setAttribute("displayFlag", "Y");
                }
               // statement.close();
               // connection.close();

            } catch (SQLException ex) {
               // Logger.getLogger(DepositProductCreationServlet.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 = "No Product Code or Interest Category exists in system for entered Product Code = " + oDepositProductCreationBean.getProductcodeSearch() + " and Interest Category = " + oDepositProductCreationBean.getIntcatSearch() + " . Please enter correct details.";
                request.setAttribute("message", message);
            }

            request.setAttribute("oDepositProductCreationBeanObj", oDepositProductCreationBean);
            request.getRequestDispatcher("/Deposit/ProductCreation.jsp").forward(request, response);

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

            try {
              //  BeanUtils.populate(oDepositProductCreationBean, request.getParameterMap());
            } catch (IllegalAccessException ex) {
               // Logger.getLogger(DepositProductCreationServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error occurred during processing.");
            } catch (InvocationTargetException ex) {
               // Logger.getLogger(DepositProductCreationServlet.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_dep_product(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) }");
                } catch (SQLException ex) {
                   // Logger.getLogger(DepositProductCreationServlet.class.getName()).log(Level.SEVERE, null, ex);
                   System.out.println("Error occurred during processing.");
                }
                proc.setString(1, oDepositProductCreationBean.getProductcodeAmend());
                proc.setString(2, oDepositProductCreationBean.getInttcategoryAmend());
                proc.setString(3, oDepositProductCreationBean.getProductnameAmend());
                proc.setString(4, oDepositProductCreationBean.getProductdescriptionAmend());
                proc.setString(5, oDepositProductCreationBean.getIntcatdescriptionAmend());
                proc.setString(6, oDepositProductCreationBean.getSegmentCodeAmend());
                proc.setString(7, oDepositProductCreationBean.getGlCodeAmend());
                proc.setString(8, oDepositProductCreationBean.getStatusAmend());
                proc.setString(9, oDepositProductCreationBean.getIntrateAmend());
                proc.setString(10, oDepositProductCreationBean.getInttfrequencyAmend());
                proc.setString(11, oDepositProductCreationBean.getInttmethodAmend());
                proc.setString(12, oDepositProductCreationBean.getMinbalAmend());
                proc.setString(13, oDepositProductCreationBean.getMaxbalAmend());
                proc.setString(14, oDepositProductCreationBean.getOdindicatorAmend());
                proc.setString(15, oDepositProductCreationBean.getPenalinttrateAmend());
                proc.setString(16, oDepositProductCreationBean.getPenalinttfrequencyAmend());
                proc.setString(17, oDepositProductCreationBean.getPenalinttmethodAmend());
                proc.setString(18, null);
                proc.setString(19, null);
                proc.setString(20, oDepositProductCreationBean.getDebitComp1Amend());
                proc.setString(21, oDepositProductCreationBean.getDebitComp2Amend());
                proc.setString(22, oDepositProductCreationBean.getCreditComp1Amend());
                proc.setString(23, oDepositProductCreationBean.getCreditComp2Amend());
                proc.setString(24, oDepositProductCreationBean.getMinwdlAmend());
                proc.setString(25, oDepositProductCreationBean.getMaxwdlAmend());
                proc.setString(26, oDepositProductCreationBean.getInttcapfrequencyAmend());
                proc.setString(27, oDepositProductCreationBean.getAllowdrAmend());
                proc.setString(28, oDepositProductCreationBean.getAllowcrAmend());
                proc.setString(29, oDepositProductCreationBean.getDormancyprAmend());

                proc.setString(30, ServletName);
                proc.setString(31, oDepositProductCreationBean.getMaxtermAmend());
                proc.setString(32, oDepositProductCreationBean.getMintermAmend());
                proc.setString(33, oDepositProductCreationBean.getDormancytriggerAmend());
                proc.setString(34, oDepositProductCreationBean.getOverduerateAmend());
                proc.setString(35, oDepositProductCreationBean.getRdFlagAmend());
                proc.setString(36, oDepositProductCreationBean.getPreClosureAmend());
                proc.setString(37, oDepositProductCreationBean.getInttpayoutfrequencyAmend());
                proc.setString(38, oDepositProductCreationBean.getGlCodeInttPAYABLE_Amend());
                proc.setString(39, oDepositProductCreationBean.getGlCodeInttPAID_Amend());
                proc.setString(40, oDepositProductCreationBean.getCropInsAmend());
                proc.setString(41, oDepositProductCreationBean.getHeadAccTypeAmend());

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

                proc.execute();

               // message = proc.getString(42);

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

                request.setAttribute("message", message);
                request.getRequestDispatcher("/Deposit/ProductCreation.jsp").forward(request, response);

            }

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

           // String inttCategoryActivation = request.getParameter("inttCategoryActivation");
           // String productCodeActivation = request.getParameter("productCodeActivation");
           // String vFlag = request.getParameter("PacsproductstatusFlag");

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

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

                proc.execute();

              //  message = proc.getString(4);

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

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