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

package Controller;

import java.io.InputStream;
import java.io.Reader;
import java.math.BigDecimal;
import java.net.URL;
import java.sql.Array;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Date;
import java.sql.NClob;
import java.sql.ParameterMetaData;
import java.sql.Ref;
import java.sql.ResultSetMetaData;
import java.sql.RowId;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.ArrayList;
import DataEntryBean.NeftRtgsEnquiryBean;
import LoginDb.DbHandler;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Calendar;
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 oracle.jdbc.OracleTypes;
import DataEntryBean.AccountCreationBean;
/**
 *
 * @author 981898
 */
public class NeftRtgsInitiationServlet extends HttpServlet {


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

        HttpSession session = request.getSession(false);
        Statement statement = null;
        Connection con = null;
        CallableStatement proc = null;
        String message = "";
        String trn_no="";
        String trn_no_2="";
        Integer err_count=0;
        ResultSet rs=null;
        Integer flag=0;


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

        NeftRtgsEnquiryBean NeftRtgsEnquiryBeanObj=new NeftRtgsEnquiryBean();

        if(handle.equalsIgnoreCase("Transfer")){
        //String ServletName = request.getParameter("handle_id");
        NeftRtgsEnquiryBean oNeftRtgsEnquiryBean = new NeftRtgsEnquiryBean();
        try {
           // BeanUtils.populate(oNeftRtgsEnquiryBean, request.getParameterMap());
        } catch (IllegalAccessException ex) {

           // Logger.getLogger(NeftRtgsInitiationServlet.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("Error occurred during processing.");
        } catch (InvocationTargetException ex) {

           // Logger.getLogger(NeftRtgsInitiationServlet.class.getName()).log(Level.SEVERE, null, ex);
           System.out.println("Error occurred during processing.");
        }

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

                   // proc.setString(1, request.getParameter("accNo"));
                    //proc.setString(2,request.getParameter("destAccNoVal"));
                   // proc.setString(3, request.getParameter("ifscNoVal"));
                   // proc.setString(4, request.getParameter("transAmtVal"));
                    proc.setString(5,user);
                    proc.setString(6, pacsId);
                   // proc.setString(7, request.getParameter("benNameVal"));
                    //proc.setString(8, request.getParameter("benAddressVal"));
                   // proc.setString(9, request.getParameter("comAmtVal"));
                    proc.registerOutParameter(10, java.sql.Types.VARCHAR);
                    proc.registerOutParameter(11, java.sql.Types.VARCHAR);
                    proc.registerOutParameter(12, java.sql.Types.INTEGER);
                    proc.registerOutParameter(13,java.sql.Types.VARCHAR);
                    //proc.setString(14,request.getParameter("bankOption"));
                    proc.execute();

                   // trn_no=proc.getString(10);
                    trn_no_2=proc.getString(11);
                    err_count=proc.getInt(12);
                   // message = proc.getString(13);

                    if((trn_no!=null)&&(trn_no!=""))
                    {
                        request.setAttribute("displayFlag", "N");
                        message="Transaction is posted for authorization with Reference ID : " + trn_no ;
                    }
                    else {
                        //message="Transaction failed.Please check the available balance and try again.";
                        request.setAttribute("message", message);
                        request.getRequestDispatcher("/NeftRtgsInitiation.jsp").forward(request, response);
                    }

        } catch (SQLException ex) {

            message="Transaction failed.Kindly provide proper input.";
           // Logger.getLogger(NeftRtgsInitiationServlet.class.getName()).log(Level.SEVERE, null, ex);
           // System.out.println(ex.toString());
           System.out.println("Error occurred during processing.");

        } finally {
            try {
                proc.close();
            } catch (SQLException e) {
               // System.out.println(e.toString());
               System.out.println("Error occurred during proc close.");
            }
            try {
                con.close();
            } catch (SQLException ex) {
//                message="Transaction failed.";
//                Logger.getLogger(NeftRtgsInitiationServlet.class.getName()).log(Level.SEVERE, null, ex);
//                System.out.println(ex.toString());
                System.out.println("Error occurred during connection close.");
            }

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

        if(handle.equalsIgnoreCase("Details")){
        NeftRtgsEnquiryBean oNeftRtgsEnquiryBean = new NeftRtgsEnquiryBean();
        NeftRtgsEnquiryBean EnqBean= new NeftRtgsEnquiryBean();
        ArrayList<NeftRtgsEnquiryBean> alNeftRtgsEnquiryBean = new ArrayList<NeftRtgsEnquiryBean>();
        try {
           // BeanUtils.populate(oNeftRtgsEnquiryBean, request.getParameterMap());
        } catch (IllegalAccessException ex) {
           // Logger.getLogger(NeftRtgsInitiationServlet.class.getName()).log(Level.SEVERE, null, ex);
           System.out.println("Error occurred during processing.");
        } catch (InvocationTargetException ex) {
           // Logger.getLogger(NeftRtgsInitiationServlet.class.getName()).log(Level.SEVERE, null, ex);
           System.out.println("Error occurred during processing.");
        }

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


           // proc.setString(1, request.getParameter("srcAccNo"));
           // proc.setString(2, request.getParameter("destAccNo2"));
           // proc.setString(3, request.getParameter("ifscNoVal2"));
           // proc.setString(4, request.getParameter("refNo"));
           // proc.setString(5, request.getParameter("txnFromDate"));
           // proc.setString(6, request.getParameter("txnDate"));
            proc.setString(7, pacsId);
            proc.registerOutParameter(8, OracleTypes.CURSOR);
            proc.registerOutParameter(9, java.sql.Types.INTEGER);
            proc.registerOutParameter(10,java.sql.Types.VARCHAR);

            proc.execute();

           // message = proc.getString(10);
            //rs = (ResultSet) proc.getObject(8);

            while(rs.next())
            {
                oNeftRtgsEnquiryBean = new NeftRtgsEnquiryBean();
                oNeftRtgsEnquiryBean.setTxnNo(rs.getString("TXN_NO"));
                oNeftRtgsEnquiryBean.setSrcAccNo(rs.getString("SRC_AC_NO"));
                oNeftRtgsEnquiryBean.setRem_ac_no(rs.getString("REM_AC_NO"));
                oNeftRtgsEnquiryBean.setDestAccNo2(rs.getString("DEST_AC_NO"));
                oNeftRtgsEnquiryBean.setBankName(rs.getString("BANK_NAME"));
                oNeftRtgsEnquiryBean.setBranchName(rs.getString("BRANCH_NAME"));
                oNeftRtgsEnquiryBean.setIfscNoVal2(rs.getString("IFSC_CODE"));
                oNeftRtgsEnquiryBean.setTransAmt(rs.getString("TXN_AMT"));
                oNeftRtgsEnquiryBean.setStatus(rs.getString("STATUS"));
                oNeftRtgsEnquiryBean.setTxnDate(rs.getString("TXN_DATE"));
                oNeftRtgsEnquiryBean.setBen_name(rs.getString("beneficiary_name"));
                oNeftRtgsEnquiryBean.setBen_add(rs.getString("beneficiary_address"));
                oNeftRtgsEnquiryBean.setCom_amt(rs.getString("COMM_AMOUNT"));
                oNeftRtgsEnquiryBean.setCom_txn_no(rs.getString("COMM_TXN_NO"));
                oNeftRtgsEnquiryBean.setQueueNo(rs.getString("queue_no"));
                oNeftRtgsEnquiryBean.setQueueNo2(rs.getString("queue_no2"));
                //oNeftRtgsEnquiryBean.setBen_add(trn_no);
                alNeftRtgsEnquiryBean.add(oNeftRtgsEnquiryBean);
                request.setAttribute("displayFlag", "Y");
                flag=1;
            }

            //String srcacno=request.getParameter("srcAccNo");
           // String destacno=request.getParameter("destAccNo2");
           // String ifsc_val=request.getParameter("ifscNoVal2");
           // String ref_no=request.getParameter("refNo");
           // String tranfrmdt=request.getParameter("txnFromDate");
           // String trantodt=request.getParameter("txnDate");

            EnqBean.setSrcAccNo(srcacno);
            EnqBean.setDestAccNo2(destacno);
            EnqBean.setIfscNoVal2(ifsc_val);
            EnqBean.setTxnNo(ref_no);
            EnqBean.setFrom_date(tranfrmdt);
            EnqBean.setTo_date(trantodt);

        } catch (SQLException ex) {
           // Logger.getLogger(NeftRtgsInitiationServlet.class.getName()).log(Level.SEVERE, null, ex);
           // System.out.println(ex.toString());
           System.out.println("Error occurred during processing.");
        } finally {
            try {
                proc.close();
            } catch (SQLException e) {
                //System.out.println(e.toString());
                System.out.println("Error occurred during proc close");
            }
            try {
                con.close();
            } catch (SQLException ex) {
               // Logger.getLogger(NeftRtgsInitiationServlet.class.getName()).log(Level.SEVERE, null, ex);
               // System.out.println(ex.toString());
               System.out.println("Error occurred during connection close.");
            }

            if(flag==0)
            {
                message="No Record to display.";
            }

            request.setAttribute("EnqBean", EnqBean);
            request.setAttribute("oNeftRtgsEnquiryBean", oNeftRtgsEnquiryBean);
            request.setAttribute("alNeftRtgsEnquiryBean", alNeftRtgsEnquiryBean);
            request.setAttribute("message", message);
            request.getRequestDispatcher("/NeftRtgsInitiation.jsp").forward(request, response);

        }

        }

    }
}
