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

package Controller;

import Dao.BulkMarkDao;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
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;

/**
 *
 * @author 1004242
 */
public class BulkMarkServlet 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
     */
     @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 = request.getParameter("userName");
        String userId = (String) session.getAttribute("user");
        ServletOutputStream stream = null;
        BufferedInputStream buf = null;
        BulkMarkDao bDao = new BulkMarkDao();
        String action = request.getParameter("temp");

        try {
          if (action.equalsIgnoreCase("allAcc")) {
            //String accNo = request.getParameter("accNo");
            response.setContentType("application/json");
            PrintWriter out = response.getWriter();
            String jsonStr = bDao.bulkMarkAccounts(accNo,pacsId);
            out.print(jsonStr);
            out.flush();
          }
          else if (action.equalsIgnoreCase("allAcc_Exs")) {
            //String accNo = request.getParameter("exAccNo");
            response.setContentType("application/json");
            PrintWriter out = response.getWriter();
            String jsonStr = bDao.bulkMarkAccounts_Exs(accNo,pacsId);
            out.print(jsonStr);
            out.flush();
          }
          else if (action.equalsIgnoreCase("markBulk")) {
            response.setContentType("application/json");
            //String accNo = request.getParameter("accNo");
            String markType = "N";
            PrintWriter out = response.getWriter();
            String jsonStr = bDao.markBulkAccount(accNo,pacsId,userId,markType);
            out.print(jsonStr);
            out.flush();
          }
          else if (action.equalsIgnoreCase("markBulk_Exs")) {
            response.setContentType("application/json");
            //String accNo = request.getParameter("exAccNo");
            String markType = "E";
            PrintWriter out = response.getWriter();
            String jsonStr = bDao.markBulkAccount_Exs(accNo,pacsId,userId,markType);
            out.print(jsonStr);
            out.flush();
          }
          else if (action.equalsIgnoreCase("allAccAuth")) {
            response.setContentType("application/json");
            PrintWriter out = response.getWriter();
            String jsonStr = bDao.bulkMarkAccountsAuth(pacsId);
            out.print(jsonStr);
            out.flush();
          }
          else if (action.equalsIgnoreCase("allAccAuth_Exs")) {
            response.setContentType("application/json");
            PrintWriter out = response.getWriter();
            String jsonStr = bDao.bulkMarkAccountsAuth_Exs(pacsId);
            out.print(jsonStr);
            out.flush();
          }
           else if (action.equalsIgnoreCase("unmark")) {
            //String accNo = request.getParameter("accNo");
            response.setContentType("application/json");
            PrintWriter out = response.getWriter();
            String jsonStr = bDao.unmarkAccountFromAuth(accNo,pacsId,userId);
            out.print(jsonStr);
            out.flush();
          }
          else if (action.equalsIgnoreCase("unmark_Exs")) {
            //String accNo = request.getParameter("exAccNo");
            response.setContentType("application/json");
            PrintWriter out = response.getWriter();
            String jsonStr = bDao.unmarkAccountFromAuth(accNo,pacsId,userId);
            out.print(jsonStr);
            out.flush();
          }
           else if (action.equalsIgnoreCase("authBulk")) {
              //  String mark = request.getParameter("searchCust");
            
            String jsonStr =  bDao.authoriseBulk(pacsId,userId,mark);
            if(jsonStr.contains("success")){
                if (mark.equalsIgnoreCase("E")) {
                    File bulkFile = bDao.gerateBulkFlatFile2(pacsId,userId);
                    if (bulkFile != null) {
                       stream = response.getOutputStream();
                       response.setContentType("text/plain");
                       response.addHeader("Content-Disposition", "attachment; filename="
                               + bulkFile.getName());
                       response.setContentLength((int) bulkFile.length());
                       FileInputStream input = new FileInputStream(bulkFile);
                       buf = new BufferedInputStream(input);
                       int readBytes = 0;

                       while ((readBytes = buf.read()) != -1) {
                           stream.write(readBytes);
                       }

                   }
                 else {
                     request.setAttribute("message", jsonStr);
                     request.getRequestDispatcher("/Deposit/authBulkIncr.jsp").forward(request, response);
                   }
                }
                else {
                    File bulkFile = bDao.gerateBulkFlatFile1(pacsId,userId);
                    if (bulkFile != null) {
                       stream = response.getOutputStream();
                       response.setContentType("text/plain");
                       response.addHeader("Content-Disposition", "attachment; filename="
                               + bulkFile.getName());
                       response.setContentLength((int) bulkFile.length());
                       FileInputStream input = new FileInputStream(bulkFile);
                       buf = new BufferedInputStream(input);
                       int readBytes = 0;

                       while ((readBytes = buf.read()) != -1) {
                           stream.write(readBytes);
                       }

                   }
                 else {
                     request.setAttribute("message", jsonStr);
                     request.getRequestDispatcher("/Deposit/authBulkIncr.jsp").forward(request, response);
                   }
                }
                //File bulkFile = bDao.gerateBulkFlatFile(pacsId,userId);
             }
            else{
                     request.setAttribute("message", jsonStr);
                     request.getRequestDispatcher("/Deposit/authBulkIncr.jsp").forward(request, response);
             }
           
          }
//          else if (action.equalsIgnoreCase("allTran"))
//          {
//            response.setContentType("application/json");
//            PrintWriter out = response.getWriter();
//            String jsonStr = wDao.getAllTranDetails(userId ,pacsId);
//            out.print(jsonStr);
//            out.flush();
//          }

        } catch (Exception e) {
           // e.printStackTrace();
           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>
}