/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
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 LoginDb.DbHandler;
import DataEntryBean.MyIntimationBean;
import java.sql.SQLException;
import oracle.jdbc.OracleTypes;

/**
 *
 * @author Tcs Helpdesk10
 */
public class MyIntimationServlet_Trading 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 {
        HttpSession session = request.getSession(false);
        Connection connection = null;
        ResultSet rs = null;
        Statement statement = null;
        CallableStatement proc = null;
        int searchFound = 0;
        String message = "";

        String pacsId = (String) session.getAttribute("pacsId");
        String tellerId = session.getAttribute("user").toString();
        String viewType = session.getAttribute("moduleName").toString();

        MyIntimationBean MyIntimationBeanObj = new MyIntimationBean();
        ArrayList<MyIntimationBean> alMyIntimationBean = new ArrayList<MyIntimationBean>();


        try {
           // BeanUtils.populate(MyIntimationBeanObj, request.getParameterMap());
        } catch (IllegalAccessException ex) {
           // Logger.getLogger(MyIntimationServlet.class.getName()).log(Level.SEVERE, null, ex);
           System.out.println("Error occurred during processing.");
        } catch (InvocationTargetException ex) {
           // Logger.getLogger(MyIntimationServlet.class.getName()).log(Level.SEVERE, null, ex);
           System.out.println("Error occurred during processing.");
        }
        try {
            connection = DbHandler.getDBConnection();
            try {
                proc = connection.prepareCall("{ call worklist.my_intimation(?,?,?,?,?,?,?,?,?) }");
            } catch (SQLException ex) {
               // Logger.getLogger(MyIntimationServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error occurred during processing.");
            }

            proc.setString(1, tellerId);
            proc.setString(2, MyIntimationBeanObj.getRefno());
            proc.setString(3, pacsId);
            proc.setString(4, MyIntimationBeanObj.getTransactiontype());
            proc.setString(5, MyIntimationBeanObj.getFromDate());
            proc.setString(6, MyIntimationBeanObj.getToDate());

            proc.registerOutParameter(7, OracleTypes.CURSOR);
            proc.registerOutParameter(8, java.sql.Types.VARCHAR);
            proc.setString(8, viewType);
            proc.setString(9, MyIntimationBeanObj.getCheckOption());
            proc.executeUpdate();

           // rs = (ResultSet) proc.getObject(7);

            if (MyIntimationBeanObj.getCheckOption().equalsIgnoreCase("TTXN")) {

                while (rs.next()) {
                    searchFound = 1;
                    MyIntimationBeanObj = new MyIntimationBean();
                    MyIntimationBeanObj.setAccount_no(rs.getString("acct_no"));
                    MyIntimationBeanObj.setAccount_no2(rs.getString("acct_no2"));
                    MyIntimationBeanObj.setAccount_bal(rs.getString("cum_curr_val"));
                    MyIntimationBeanObj.setRefnoSearch(rs.getString("cbs_ref_no"));
                    MyIntimationBeanObj.setTran_date(rs.getString("tran_date"));
                    MyIntimationBeanObj.setTransactiontypeSearch(rs.getString("txn_type"));
                    MyIntimationBeanObj.setAuth_id(rs.getString("checker_id"));
                    MyIntimationBeanObj.setRemarks(rs.getString("remarks"));
                    MyIntimationBeanObj.setAmount(rs.getString("txn_amt"));
                    MyIntimationBeanObj.setStatus(rs.getString("status"));
                    MyIntimationBeanObj.setSale_purchase_ref(rs.getString("SALE_PURCHASE_REF_ID"));
                    MyIntimationBeanObj.setTrade_product(rs.getString("trad_prod_name"));

                    alMyIntimationBean.add(MyIntimationBeanObj);

                    request.setAttribute("displayFlag", "Y");

                }
            }

            //MyIntimationBeanObj.setRefno(request.getParameter("refno"));
            //MyIntimationBeanObj.setTransactiontype(request.getParameter("transactiontype"));
            //MyIntimationBeanObj.setFromDate(request.getParameter("fromDate"));
            //MyIntimationBeanObj.setToDate(request.getParameter("toDate"));
            //MyIntimationBeanObj.setCheckOption(request.getParameter("checkOption"));
            

        } catch (SQLException ex) {
           // Logger.getLogger(MyIntimationServlet.class.getName()).log(Level.SEVERE, null, ex);
           System.out.println("Error occurred during processing.");
        } finally {
            try {
                proc.close();
            } catch (SQLException e) {
                System.out.println("Error occurred during proc close.");
            }
            try {
                connection.close();
            } catch (SQLException ex) {
               // Logger.getLogger(MyIntimationServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error occurred during connection close.");
            }
        }
        if (searchFound == 0) {
            message = "No data exists for selected item.";
            request.setAttribute("message", message);
        }

        request.setAttribute("oMyIntimationBeanSearchObj", MyIntimationBeanObj);
        request.setAttribute("alMyIntimationBean", alMyIntimationBean);
        request.setAttribute("message", message);

        request.getRequestDispatcher("/Trading_JSP/MyIntimation_Trading.jsp").forward(request, response);

    }

// <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 {
        processRequest(request, response);
    }

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