/*
 * 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 java.util.ArrayList;
import java.util.StringTokenizer;
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.StandingInstructionBean;
import javax.servlet.http.HttpSession;

/**
 *
 * @author 986137
 */
public class StandingInstructionServlet 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 {
        
    } 

    // <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 {
        HttpSession session = request.getSession(false);
        Connection con = null;
        CallableStatement proc = null;
        String message = "";

        //String ServletName = request.getParameter("handle_id");
        String pacsId = (String) session.getAttribute("pacsId");
        String user = (String) session.getAttribute("user");

        StandingInstructionBean oStandingInstructionBean = new StandingInstructionBean();

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

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

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

                proc.setString(1, pacsId);
                proc.setString(2, oStandingInstructionBean.getFromAcc());
                proc.setString(3, oStandingInstructionBean.getToAcc());
                proc.setString(4, oStandingInstructionBean.getFromDate());
                proc.setString(5, oStandingInstructionBean.getToDate());
                proc.setString(6, oStandingInstructionBean.getSiAmount());
                proc.setString(7, oStandingInstructionBean.getFreqOption());
                proc.setString(8, oStandingInstructionBean.getChaseDay());
                proc.setString(9, user);
                proc.setString(10, ServletName);
                proc.setString(11, oStandingInstructionBean.getOpsType());

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

                proc.execute();

               // message = proc.getString(12);

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

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

            }

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

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

              //  ResultSet rs = statement.executeQuery("select t.from_acct,t.to_acct,t.amount,t.freq,to_char(t.start_date,'DD/MM/YYYY') as start_date,to_char(t.end_date,'DD/MM/YYYY') as end_date,t.chase_days,t.operation_type from si_details t"
                        + " where t.from_acct = '" + oStandingInstructionBean.getFromAccSearch() + "' and t.to_acct = '" + oStandingInstructionBean.getToAccSearch() + "' and t.operation_type =  '" + oStandingInstructionBean.getOpsTypeSearch() + "'");

                while (rs.next()) {

                    oStandingInstructionBean.setFromAcc(rs.getString("from_acct"));
                    oStandingInstructionBean.setToAcc(rs.getString("to_acct"));
                    oStandingInstructionBean.setSiAmount(rs.getString("amount"));
                    oStandingInstructionBean.setFreqOption(rs.getString("freq"));
                    oStandingInstructionBean.setFromDate(rs.getString("start_date"));
                    oStandingInstructionBean.setToDate(rs.getString("end_date"));
                    oStandingInstructionBean.setChaseDay(rs.getString("chase_days"));
                    oStandingInstructionBean.setOpsType(rs.getString("operation_type"));
                    

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

            } catch (SQLException ex) {
               // Logger.getLogger(StandingInstructionServlet.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 Result Found! Please try again.";
                request.setAttribute("message", message);
            }

            request.setAttribute("oStandingInstructionBeanObj", oStandingInstructionBean);
            request.getRequestDispatcher("/StandingInstructionCreation.jsp").forward(request, response);

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

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

            try {
                con = DbHandler.getDBConnection();
                try {
                    proc = con.prepareCall("{ call operations.POST_SI_Details(?,?,?,?,?,?,?,?,?,?,?,?) }");
                } catch (SQLException ex) {
                   // Logger.getLogger(StandingInstructionServlet.class.getName()).log(Level.SEVERE, null, ex);
                   System.out.println("Error occurred during processing.");
                }
                proc.setString(1, pacsId);
                proc.setString(2, oStandingInstructionBean.getFromAccAmend());
                proc.setString(3, oStandingInstructionBean.getToAccAmend());
                proc.setString(4, oStandingInstructionBean.getFromDateAmend());
                proc.setString(5, oStandingInstructionBean.getToDateAmend());
                proc.setString(6, oStandingInstructionBean.getSiAmountAmend());
                proc.setString(7, oStandingInstructionBean.getFreqOptionAmend());
                proc.setString(8, oStandingInstructionBean.getChaseDayAmend());
                proc.setString(9, user);
                proc.setString(10, ServletName);
                proc.setString(11, oStandingInstructionBean.getOpsTypeAmend());

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

                proc.execute();

              //  message = proc.getString(12);

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

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

            }

        }
//            else if ("Search".equalsIgnoreCase(ServletName)) {
//
//            try {
//                BeanUtils.populate(oStandingInstructionBean, request.getParameterMap());
//            } catch (IllegalAccessException ex) {
//                Logger.getLogger(StandingInstructionServlet.class.getName()).log(Level.SEVERE, null, ex);
//            } catch (InvocationTargetException ex) {
//                Logger.getLogger(StandingInstructionServlet.class.getName()).log(Level.SEVERE, null, ex);
//            }
//
//            Connection connection = null;
//            ResultSet resultset = null;
//            Statement statement = null;
//            connection = DbHandler.getDBConnection();
//            int SeachFound = 0;
//
//            try {
//                statement = connection.createStatement();
//            } catch (SQLException ex) {
//                Logger.getLogger(StandingInstructionServlet.class.getName()).log(Level.SEVERE, null, ex);
//            }
//            try {
//
//                ResultSet rs = statement.executeQuery("select distinct s.id, s.from_acct, s.to_acct, s.amount, decode(s.operation_type,'s2s','Dep to Dep', 's2r','Dep to RD', 's2l','Dep to Loan') op_typ,"
//                        + " s.start_date, s.end_date, s.active_flag from si_details s  where s.pacs_id='" + pacsId + "' and s.active_flag='Y' and s.from_acct = '" + oStandingInstructionBean.getFromAccSearch() + "' ");
//
//                while (rs.next()) {
//
//                    oStandingInstructionBean.setID(rs.getString("id"));
//                    oStandingInstructionBean.setFromAcc(rs.getString("from_acct"));
//                    oStandingInstructionBean.setToAcc(rs.getString("to_acct"));
//                    oStandingInstructionBean.setSiAmount(rs.getString("amount"));
//                    oStandingInstructionBean.setOpsType(rs.getString("op_typ"));
//                    oStandingInstructionBean.setFromDate(rs.getString("start_date"));
//                    oStandingInstructionBean.setToDate(rs.getString("end_date"));
//                    //oStandingInstructionBean.setFlag(rs.getString("active_flag"));
//
//                    SeachFound = 1;
//                   // request.setAttribute("displayFlag", "Y");
//                }
//                statement.close();
//                connection.close();
//
//            } catch (SQLException ex) {
//                Logger.getLogger(StandingInstructionServlet.class.getName()).log(Level.SEVERE, null, ex);
//            }
//
//            if (SeachFound == 0) {
//                message = "No Result Found! Please try again.";
//                request.setAttribute("message", message);
//            }
//
//            request.setAttribute("oStandingInstructionBeanObj", oStandingInstructionBean);
//            request.getRequestDispatcher("/StandingInstructionCreation.jsp").forward(request, response);
//
//        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
