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

package Controller;

import Dao.CashWithdrawlDDSDao;
import Dao.MicroFileProcessingDao;
import DataEntryBean.MicroFileAccountDetBean;
import ServiceLayer.CashWithdrawlDDSService;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
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 CashWidrawlDDSServlet 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 accNo = request.getParameter("accNoServer");
        //Double amount = Double.parseDouble(request.getParameter("amnt"));
        //String tranType = request.getParameter("tranType");
        CashWithdrawlDDSDao ddsDao = new CashWithdrawlDDSDao();
        MicroFileAccountDetBean accountDet = new 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.setAccountCode(accNo);
            accountDet.setAvailBal(Double.parseDouble(request.getParameter("balanceServer")));
            accountDet.setCrTotal(amount);
            accountDet.setTxnType(tranType);
            accountDet.setProdType(request.getParameter("accntType"));
            accountDet.setMessage("FAILURE");


            result = ddsDao.CashDepositOperation(tellerId, pacsId, accNo, amount, tranType, accountDet);

            if (accountDet.getMessage().equalsIgnoreCase("SUCCESS")) {
                ddsDao.getreciptDetailsFromDB(accountDet);
                pdfReceipt = ddsService.createReceipt(accountDet, tellerId, tellerName);

            }
           
            if (pdfReceipt != null) {
                stream = response.getOutputStream();
               // request.getSession().setAttribute("errorMessage", result);
                //request.setAttribute("message", result);
                response.setContentType("text/plain");
                response.addHeader("Content-Disposition", "attachment; filename="
                        + "TransactionReciept.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);
                }


                 // request.setAttribute("message", result);
                  //request.getRequestDispatcher("/DDS/CashWidrawlDeposit.jsp").forward(request, response);
            }else
            {
                if(accountDet.getMessage().equalsIgnoreCase("SUCCESS")){
                  request.setAttribute("message", result + ".Error in generating receipt");
                  request.getRequestDispatcher("/DDS/CashWidrawlDeposit.jsp").forward(request, response);
                }
                else
                {
                    request.setAttribute("message", result);
                  request.getRequestDispatcher("/DDS/CashWidrawlDeposit.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>

}
