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

import Dao.AccAmendDao;
import DataEntryBean.AccountAmendBean;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
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;

/**
 *
 * @author 981898
 */
public class AccAmendServlet 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 {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet AccAmendServlet</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet AccAmendServlet at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
             */
        } finally {
            out.close();
        }
    }

    // <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);
        String pacsId = (String) session.getAttribute("pacsId");
        String userId = (String) session.getAttribute("user");
        AccAmendDao oAccAmendDao = new AccAmendDao();
        String action = request.getParameter("action");
        AccountAmendBean oAccountAmendBean = null;

        try {
            if (action.equalsIgnoreCase("search")) {
                
                response.setContentType("application/json");
                PrintWriter out = response.getWriter();
                String jsonStr = oAccAmendDao.searchAccounts(accNo, pacsId);
                out.print(jsonStr);
                out.flush();
            } else if (action.equalsIgnoreCase("accDtl")) {
               
                String accType = request.getParameter("accType");
                response.setContentType("application/json");
                PrintWriter out = response.getWriter();
                String jsonStr = oAccAmendDao.getAccDtl(accNo, pacsId, accType);
                out.print(jsonStr);
                out.flush();
            } else if (action.equalsIgnoreCase("newCifSearch")) {
                
                if(newCif.equalsIgnoreCase("NA")){
                    newCif = "";
                }
                response.setContentType("application/json");
                PrintWriter out = response.getWriter();
                String jsonStr = oAccAmendDao.getNewCif(newCif, pacsId);
                out.print(jsonStr);
                out.flush();
            } else if (action.equalsIgnoreCase("submit")) {
                
               
                oAccountAmendBean = new AccountAmendBean();
                try {
                  //  BeanUtils.populate(oAccountAmendBean, request.getParameterMap());
                } catch (IllegalAccessException ex) {
                    System.out.println("Error Occurred during processing.");
                   // Logger.getLogger(AccountCreationServlet.class.getName()).log(Level.SEVERE, null, ex);
                } catch (InvocationTargetException ex) {
                    System.out.println("Error Occurred during processing.");
                   // Logger.getLogger(AccountCreationServlet.class.getName()).log(Level.SEVERE, null, ex);
                }
                String retnMsg = oAccAmendDao.updateCif(userId, pacsId, accNo, oAccountAmendBean, remarks);
                request.setAttribute("message", retnMsg);
                request.getRequestDispatcher("/AccountAmendment.jsp").forward(request, response);
            }
        } catch (Exception e) {
            System.out.println("Error Occurred during processing.");
        } finally
        { 
            System.out.println("Processing done."); 
        } 
    }

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