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

import DataEntryBean.AccountCreationBean;
import DataEntryBean.DepositAccountCreationBean;
import DataEntryBean.LoanAccountCreationBean;
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.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
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 oracle.jdbc.OracleTypes;
import org.apache.commons.beanutils.BeanUtils;

/**
 *
 * @author 590685
 */
public class ReadOnlyServlet 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 index = request.getParameter("detailId");
       // String AuthActivity = request.getParameter("checkOption");
       // String refID = request.getParameter("refno" + request.getParameter("detailId"));
        //String AuthActivity = request.getAttribute("AuthActivity").toString();
        //String refID = request.getAttribute("refID").toString();
        System.out.println("AuthActivity is "+AuthActivity+" and refID is "+refID);
        String filter = "";

        DepositAccountCreationBean oDepositAccountCreationBean = new DepositAccountCreationBean();
        AccountCreationBean oAccountCreationBean = new AccountCreationBean();
        LoanAccountCreationBean oLoanAccountCreationBean = new LoanAccountCreationBean();

        try {
            connection = DbHandler.getDBConnection();
            try {
                proc = connection.prepareCall("{ call worklist.view_details(?,?,?,?,?) }");
            } catch (SQLException ex) {
                // Logger.getLogger(GlProductOperationServlet.class.getName()).log(Level.SEVERE, null, ex);
                System.out.println("Error occurred during processing.");
            }
            proc.setString(1, refID);
            proc.setString(2, pacsId);
            proc.setString(3, AuthActivity);
            proc.registerOutParameter(4, OracleTypes.CURSOR);
            proc.registerOutParameter(5, java.sql.Types.VARCHAR);

            proc.executeUpdate();

           // rs = (ResultSet) proc.getObject(4);
            filter = proc.getObject(5).toString();

            if (filter.equalsIgnoreCase("DEPOSIT")) {

                while (rs.next()) {
                    searchFound = 1;
                    oDepositAccountCreationBean = new DepositAccountCreationBean();
                    oDepositAccountCreationBean.setCifNumber(rs.getString(1));
                    oDepositAccountCreationBean.setProductName(rs.getString(2));
                    oDepositAccountCreationBean.setInttDescription(rs.getString(3));
                    oDepositAccountCreationBean.setAccOpenDate(rs.getString(4));
                    oDepositAccountCreationBean.setODlimit(rs.getString(5));
                    oDepositAccountCreationBean.setActivityCode(rs.getString(6));
                    oDepositAccountCreationBean.setCustomerType(rs.getString(7));
                    oDepositAccountCreationBean.setInttrepmethod(rs.getString(8));
                    oDepositAccountCreationBean.setIntttransacc(rs.getString(9));
                    oDepositAccountCreationBean.setSegmentCode(rs.getString(10));
                    oDepositAccountCreationBean.setId(rs.getString(11));
                    oDepositAccountCreationBean.setTermValue(rs.getString(12));
                    oDepositAccountCreationBean.setTermDays(rs.getString(13));
                    oDepositAccountCreationBean.setTermMonth(rs.getString(14));
                    oDepositAccountCreationBean.setTermYears(rs.getString(15));
                    oDepositAccountCreationBean.setInstAmt(rs.getString(16));
                    oDepositAccountCreationBean.setCifName(rs.getString(18));
                    //Changed SetCifNumber2 to setJt_cifNumber1 Bitan
                    oDepositAccountCreationBean.setJt_cifNumber1(rs.getString(19));
                    oDepositAccountCreationBean.setJt_cifName1(rs.getString(20));
                    //End
                    oDepositAccountCreationBean.setNominee(rs.getString(21));
                    oDepositAccountCreationBean.setNomineeName(rs.getString(22));
                    //Changes Added Bitan For 10 Secondary CIF
                    oDepositAccountCreationBean.setJt_cifNumber2(rs.getString(23));
                    oDepositAccountCreationBean.setJt_cifName2(rs.getString(24));
                    oDepositAccountCreationBean.setJt_cifNumber3(rs.getString(25));
                    oDepositAccountCreationBean.setJt_cifName3(rs.getString(26));
                    oDepositAccountCreationBean.setJt_cifNumber4(rs.getString(27));
                    oDepositAccountCreationBean.setJt_cifName4(rs.getString(28));
                    oDepositAccountCreationBean.setJt_cifNumber5(rs.getString(29));
                    oDepositAccountCreationBean.setJt_cifName5(rs.getString(30));
                    oDepositAccountCreationBean.setJt_cifNumber6(rs.getString(31));
                    oDepositAccountCreationBean.setJt_cifName6(rs.getString(32));
                    oDepositAccountCreationBean.setJt_cifNumber7(rs.getString(33));
                    oDepositAccountCreationBean.setJt_cifName7(rs.getString(34));
                    oDepositAccountCreationBean.setJt_cifNumber8(rs.getString(35));
                    oDepositAccountCreationBean.setJt_cifName8(rs.getString(36));
                    oDepositAccountCreationBean.setJt_cifNumber8(rs.getString(37));
                    oDepositAccountCreationBean.setJt_cifName8(rs.getString(38));
                    oDepositAccountCreationBean.setJt_cifNumber10(rs.getString(39));
                    oDepositAccountCreationBean.setJt_cifName10(rs.getString(40));
                    //END
                    request.setAttribute("displayFlag", "Y");

                }
            }

            if (filter.equalsIgnoreCase("KCC")) {

                while (rs.next()) {
                    searchFound = 1;
                    oAccountCreationBean = new AccountCreationBean();
                    oAccountCreationBean.setCifNumber(rs.getString(1));
                    oAccountCreationBean.setProductName(rs.getString(2));
                    oAccountCreationBean.setInttDescription(rs.getString(3));
                    oAccountCreationBean.setActivityCode(rs.getString(4));
                    oAccountCreationBean.setCustomerType(rs.getString(5));
                    oAccountCreationBean.setSegmentCode(rs.getString(6));
                    oAccountCreationBean.setId(rs.getString(7));
                    oAccountCreationBean.setCbsSavingsAccount(rs.getString(8));
                    oAccountCreationBean.setLimit(rs.getString(9));
                    oAccountCreationBean.setLimit_kind(rs.getString(10));
                    oAccountCreationBean.setLimitExpiryDate(rs.getString(11));
                    oAccountCreationBean.setCollateralType(rs.getString(12));
                    oAccountCreationBean.setLandinAcres(rs.getString(13));
                    oAccountCreationBean.setDescription(rs.getString(14));
                    oAccountCreationBean.setCurrentValuation(rs.getString(15));
                    oAccountCreationBean.setSafeLendingMargin(rs.getString(16));
                    oAccountCreationBean.setIntRate(rs.getString(17));
                    oAccountCreationBean.setPenIntRate(rs.getString(18));

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

            }

            if (filter.equalsIgnoreCase("LOAN")) {

                while (rs.next()) {
                    searchFound = 1;
                    oLoanAccountCreationBean = new LoanAccountCreationBean();
                    oLoanAccountCreationBean.setProductCode(rs.getString(1));
                    oLoanAccountCreationBean.setInttCategory(rs.getString(2));
                    oLoanAccountCreationBean.setActivityCode(rs.getString(3));
                    oLoanAccountCreationBean.setSegmentCode(rs.getString(4));
                    oLoanAccountCreationBean.setId(rs.getString(5));
                    oLoanAccountCreationBean.setCifNumber(rs.getString(6));
                    oLoanAccountCreationBean.setCustomerType(rs.getString(7));
                    oLoanAccountCreationBean.setLoanappAmt(rs.getString(8));
                    oLoanAccountCreationBean.setLoanTerm(rs.getString(9));
                    oLoanAccountCreationBean.setSchemeCode(rs.getString(10));
                    oLoanAccountCreationBean.setPurposeCode(rs.getString(11));
                    oLoanAccountCreationBean.setCollateralType(rs.getString(12));
                    oLoanAccountCreationBean.setDescription(rs.getString(13));
                    oLoanAccountCreationBean.setCurrentValuation(rs.getString(14));
                    oLoanAccountCreationBean.setSafeLendingMargin(rs.getString(15));
                    oLoanAccountCreationBean.setEligAmt(rs.getString(16));
                    oLoanAccountCreationBean.setGurantorName(rs.getString(17));
                    oLoanAccountCreationBean.setGuarantorAddress(rs.getString(18));
                    oLoanAccountCreationBean.setGuarantorDOB(rs.getString(19));
                    oLoanAccountCreationBean.setGuarantorJob(rs.getString(20));
                    oLoanAccountCreationBean.setGurantorIncome(rs.getString(21));
                    oLoanAccountCreationBean.setGuarRelation(rs.getString(22));
                    oLoanAccountCreationBean.setGuarIdType(rs.getString(23));
                    oLoanAccountCreationBean.setGuarIdNo(rs.getString(24));
                    oLoanAccountCreationBean.setCustName(rs.getString(25));
                    oLoanAccountCreationBean.setIntRate(rs.getString(26));
                    oLoanAccountCreationBean.setPenIntRate(rs.getString(27));

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

                }
            }
            if (searchFound == 0) {
                message = "No data exists for selected item";
                request.setAttribute("message", message);
            }

            request.setAttribute("message", message);

            if (filter.equalsIgnoreCase("DEPOSIT")) {
                request.setAttribute("oDepositAccountCreationBean", oDepositAccountCreationBean);
                request.getRequestDispatcher("/Deposit/DepositAccountCreationReadonly.jsp").forward(request, response);
            } else if (filter.equalsIgnoreCase("KCC")) {
                request.setAttribute("oAccountCreationBean", oAccountCreationBean);
                request.getRequestDispatcher("/accountCreationReadOnly.jsp").forward(request, response);
            } else if (filter.equalsIgnoreCase("LOAN")) {
                request.setAttribute("oLoanAccountCreationBean", oLoanAccountCreationBean);
                request.getRequestDispatcher("/Loan/LoanAccountCreationReadOnly.jsp").forward(request, response);
            }

        } catch (SQLException ex) {
            // Logger.getLogger(UploadServlet.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(UploadServlet.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("message", message);
//
//        if (filter.equalsIgnoreCase("DEPOSIT")) {
//            request.setAttribute("oDepositAccountCreationBean", oDepositAccountCreationBean);
//            request.getRequestDispatcher("/Deposit/DepositAccountCreationReadonly.jsp").forward(request, response);
//        } else if (filter.equalsIgnoreCase("KCC")) {
//            request.setAttribute("oAccountCreationBean", oAccountCreationBean);
//            request.getRequestDispatcher("/accountCreationReadOnly.jsp").forward(request, response);
//        }else if (filter.equalsIgnoreCase("LOAN")) {
//            request.setAttribute("oLoanAccountCreationBean", oLoanAccountCreationBean);
//            request.getRequestDispatcher("/Loan/LoanAccountCreationReadOnly.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>
}
