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

package Controller;

import Dao.CashWithdrawlDDSDao;
import DataEntryBean.MicroFileAccountDetBean;
import ServiceLayer.CashWithdrawlDDSService;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
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 TransactionDDSServlet extends HttpServlet {


   @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        HttpSession session = request.getSession(false);
        String pacsId = (String) session.getAttribute("pacsId");
        String tellerId = (String) session.getAttribute("user");
        String tellerName = (String) session.getAttribute("UserName");
        String tranType = request.getParameter("tranType");
      //  String from_dt =  request.getParameter("from_date");
      //  String to_dt =  request.getParameter("to_date");
        CashWithdrawlDDSDao ddsDao = new CashWithdrawlDDSDao();
        MicroFileAccountDetBean accountDet = new MicroFileAccountDetBean();
        ArrayList<MicroFileAccountDetBean> accountDetlist = new ArrayList<MicroFileAccountDetBean>();
        CashWithdrawlDDSService ddsService = new CashWithdrawlDDSService();
        String result = null;
        ServletOutputStream stream = null;
        BufferedInputStream buf = null;
        File pdfReceipt = null;
          if (request.getSession().getAttribute("errorMessage") != null) {
            request.getSession().removeAttribute("errorMessage");
        }
       try {

           accountDet.setTxnType(tranType);
           accountDet.setProdType(request.getParameter("accntType"));
           accountDet.setMessage("FAILURE");

           if (tranType.equalsIgnoreCase("all")) {
               ddsDao.getTotalTransactionForAgent(tellerId, pacsId, accountDet , from_dt , to_dt);

               if (accountDet.getMessage().equalsIgnoreCase("SUCCESS")) {
                   pdfReceipt = ddsService.createAllTransactionReceipt(accountDet, tellerId, tellerName, pacsId);

                   if (pdfReceipt != null) {
                       stream = response.getOutputStream();
                       response.setContentType("text/plain");
                       response.addHeader("Content-Disposition", "attachment; filename="
                               + "AllTransactionReciept.txt");
                       response.setContentLength((int) pdfReceipt.length());
                       FileInputStream input = new FileInputStream(pdfReceipt);
                       buf = new BufferedInputStream(input);
                       int readBytes = 0;

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

                   } else {
                       if (accountDet.getMessage().equalsIgnoreCase("SUCCESS")) {
                           request.setAttribute("message", "Error in generating receipt");
                           request.getRequestDispatcher("/DDS/TransactionReport.jsp").forward(request, response);
                       } else {
                           request.setAttribute("message", "Error in fetching report");
                           request.getRequestDispatcher("/DDS/TransactionReport.jsp").forward(request, response);
                       }
                   }
               } else {
                   request.setAttribute("message", "Error in fetching report");
                   request.getRequestDispatcher("/DDS/TransactionReport.jsp").forward(request, response);
               }
           } else if (tranType.equalsIgnoreCase("single")) {

              accountDetlist = ddsDao.getTransactionForAgent(tellerId, pacsId, accountDet,from_dt,to_dt);

               if (accountDetlist.size() !=0) {
                   pdfReceipt = ddsService.createTransactionReceiptDetailed(accountDetlist);

                   if (pdfReceipt != null) {
                       stream = response.getOutputStream();
                       response.setContentType("text/plain");
                       response.addHeader("Content-Disposition", "attachment; filename="
                               + "DetailedTransactionReciept.txt");
                       response.setContentLength((int) pdfReceipt.length());
                       FileInputStream input = new FileInputStream(pdfReceipt);
                       buf = new BufferedInputStream(input);
                       int readBytes = 0;

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

                   } else {
                       if (accountDet.getMessage().equalsIgnoreCase("SUCCESS")) {
                           request.setAttribute("message", "Error in generating receipt");
                           request.getRequestDispatcher("/DDS/TransactionReport.jsp").forward(request, response);
                       } else {
                           request.setAttribute("message", "Error in fetching report");
                           request.getRequestDispatcher("/DDS/TransactionReport.jsp").forward(request, response);
                       }
                   }
               } else {
                   request.setAttribute("message", "Error in fetching report");
                   request.getRequestDispatcher("/DDS/TransactionReport.jsp").forward(request, response);
               }
           }


       } catch (IOException ioe) {
          // ioe.printStackTrace();
          System.out.println("Error occurred during processing.");
           request.setAttribute("message", result + ".Error in generating receipt");
           request.getRequestDispatcher("/DDS/CashWidrawlDeposit.jsp").forward(request, response);
       } catch (Exception e) {
          // e.printStackTrace();
          System.out.println("Error occurred during processing.");
           request.setAttribute("message", "Request can not be processed.");
           request.getRequestDispatcher("/DDS/CashWidrawlDeposit.jsp").forward(request, response);
       } finally {
           if (stream != null) {
               stream.close();
           }
           if (buf != null) {
               buf.close();
           }
       }

    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */

    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
