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

import DataEntryBean.AccountDetailsBean;
import DataEntryBean.AccountDetailsBean;
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.ArrayList;
import java.util.StringTokenizer;
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;

/**
 *
 * @author 986137
 */
public class AccountDetailsServlet 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 {
    }

    // <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 {

        Connection con = null;
        CallableStatement proc = null;
        String message = "";
        String ScreenStatus = "";
        //String hdrID = "";
        HttpSession session = request.getSession(false);

        
        String pacsId = (String) session.getAttribute("pacsId");
        String user = (String) session.getAttribute("user");
     

        AccountDetailsBean oAccountDetailsBean = new AccountDetailsBean();
        AccountDetailsBean oAccountDetailsBeanObj = new AccountDetailsBean();
        ArrayList<AccountDetailsBean> alAccountDetailsBean = new ArrayList<AccountDetailsBean>();



        //For Amend Div

        ArrayList<AccountDetailsBean> alAccountDetailsBeanNew = new ArrayList<AccountDetailsBean>();
        ArrayList<AccountDetailsBean> alAccountDetailsBeanDeleted = new ArrayList<AccountDetailsBean>();
        ArrayList<AccountDetailsBean> alAccountDetailsBeanUpdated = new ArrayList<AccountDetailsBean>();

        if (ServletName.equalsIgnoreCase("CreateLoan")) {


            try {
                //BeanUtils.populate(oAccountDetailsBean, request.getParameterMap());
            } catch (IllegalAccessException ex) {
               // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error Occurred during processing.");
            } catch (InvocationTargetException ex) {
               // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error Occurred during processing.");
            } finally { 
                    System.out.println("Processing done."); 
                }


            int counter = Integer.parseInt(request.getParameter("rowCounter"));

            for (int i = 1; i <= counter; i++) {
                try {
                   // String shgcode = request.getParameter("shgcode");
                    //String shgname = request.getParameter("shgname");
                    //String source = request.getParameter("source" + i);
                    //String bankname = request.getParameter("bankname" + i);
                    //String loanamount = request.getParameter("loanamount" + i);
                    //String accountnumber = request.getParameter("accountnumber" + i);
                    //String accountopendate = request.getParameter("accountopendate" + i);
                    //String lastdisbursementdate = request.getParameter("lastdisbursementdate" + i);
                    //String principaloutstanding = request.getParameter("principaloutstanding" + i);
                    //String principalpaid = request.getParameter("principalpaid" + i);
                    //String interestoutstanding = request.getParameter("interestoutstanding" + i);
                    //String interestpaid = request.getParameter("interestpaid" + i);

                    if (accountnumber != null) {
                        oAccountDetailsBean = new AccountDetailsBean();
                        oAccountDetailsBean.setShgcode(shgcode);
                        oAccountDetailsBean.setShgname(shgname);
                        oAccountDetailsBean.setSource(source);
                        oAccountDetailsBean.setBankname(bankname);
                        oAccountDetailsBean.setLoanamount(loanamount);
                        oAccountDetailsBean.setAccountnumber(accountnumber);
                        oAccountDetailsBean.setAccountopendate(accountopendate);
                        oAccountDetailsBean.setLastdisbursementdate(lastdisbursementdate);
                        oAccountDetailsBean.setPrincipaloutstanding(principaloutstanding);
                        oAccountDetailsBean.setPrincipalpaid(principalpaid);
                        oAccountDetailsBean.setInterestoutstanding(interestoutstanding);
                        oAccountDetailsBean.setInterestpaid(interestpaid);
                        alAccountDetailsBean.add(oAccountDetailsBean);
                    }
                } catch (Exception e) {
                    System.out.println("Error Occurred during processing.");
                } finally { 
                    System.out.println("Processing done."); 
                }
            }
            if (alAccountDetailsBean.size() > 0) {

                try {
                    con = DbHandler.getDBConnection();
                    try {
                        proc = con.prepareCall("{ call SHG_OPERATIONS.Upsert_account_details_loan(?,?,?,?,?,?,?,?,?,?,?,?,?,?) }");
                    } catch (SQLException ex) {
                       // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
                       System.out.println("Error Occurred during processing.");
                    } finally { 
                        System.out.println("Processing done."); 
                      }

                    for (int j = 0; j < alAccountDetailsBean.size(); j++) {

                        oAccountDetailsBean = alAccountDetailsBean.get(j);


                        proc.setString(1, oAccountDetailsBean.getAcc_id());
                        proc.setString(2, oAccountDetailsBean.getShgcode());
                        proc.setString(3, oAccountDetailsBean.getShgname());
                        proc.setString(4, oAccountDetailsBean.getSource());
                        proc.setString(5, oAccountDetailsBean.getBankname());
                        proc.setString(6, oAccountDetailsBean.getAccountnumber());
                        proc.setString(7, oAccountDetailsBean.getAccountopendate());
                        proc.setString(8, oAccountDetailsBean.getLastdisbursementdate());
                        proc.setString(9, oAccountDetailsBean.getLoanamount());
                        proc.setString(10, oAccountDetailsBean.getPrincipaloutstanding());
                        proc.setString(11, oAccountDetailsBean.getPrincipalpaid());
                        proc.setString(12, oAccountDetailsBean.getInterestoutstanding());
                        proc.setString(13, oAccountDetailsBean.getInterestpaid());
                        proc.setString(14, ServletName);
                        proc.addBatch();


                    }

                    proc.executeBatch();
                    message = "Account Details created Successfully";

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

            request.setAttribute("message", message);
            request.getRequestDispatcher("/Shg_JSP/AccountDetails.jsp").forward(request, response);

        } else if (ServletName.equalsIgnoreCase("CreateDeposit")) {


            try {
               // BeanUtils.populate(oAccountDetailsBean, request.getParameterMap());
            } catch (IllegalAccessException ex) {
                //Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
                System.out.println("Error Occurred during processing.");
            } catch (InvocationTargetException ex) {
               // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
                System.out.println("Error Occurred during processing.");
            } finally { 
                    System.out.println("Processing done."); 
                }



            int counter = Integer.parseInt(request.getParameter("rowCounter"));

            for (int i = 1; i <= counter; i++) {
                try {
                   // String shgcode = request.getParameter("shgcode");
                    //String shgname = request.getParameter("shgname");
                    //String source = request.getParameter("source1" + i);
                    //String bankname = request.getParameter("bankname1" + i);
                    //String accountnumber = request.getParameter("accountnumber1" + i);
                    //String accountopendate = request.getParameter("accountopendate1" + i);
                    //String outstandingbalance = request.getParameter("outstandingbalance" + i);
                    //String interestaccrued = request.getParameter("interestaccrued" + i);
                    //String lasttransactiondate = request.getParameter("lasttransactiondate" + i);

                    if (accountnumber != null) {
                        oAccountDetailsBean = new AccountDetailsBean();
                        oAccountDetailsBean.setShgcode(shgcode);
                        oAccountDetailsBean.setShgname(shgname);
                        oAccountDetailsBean.setSource1(source);
                        oAccountDetailsBean.setBankname1(bankname);
                        oAccountDetailsBean.setAccountnumber1(accountnumber);
                        oAccountDetailsBean.setAccountopendate1(accountopendate);
                        oAccountDetailsBean.setOutstandingbalance(outstandingbalance);
                        oAccountDetailsBean.setInterestaccrued(interestaccrued);
                        oAccountDetailsBean.setLasttransactiondate(lasttransactiondate);
                        alAccountDetailsBean.add(oAccountDetailsBean);
                    }
                } catch (Exception e) {
                    System.out.println("Error Occurred during processing.");
                } finally { 
                    System.out.println("Processing done."); 
                }
                
            }
            if (alAccountDetailsBean.size() > 0) {

                try {
                    con = DbHandler.getDBConnection();
                    try {
                        proc = con.prepareCall("{ call SHG_OPERATIONS.Upsert_account_details_deposit(?,?,?,?,?,?,?,?,?,?,?)}");
                    } catch (SQLException ex) {
                        System.out.println("Error Occurred during processing.");
                       // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } finally { 
                        System.out.println("Processing done."); 
                    }


                    for (int j = 0; j < alAccountDetailsBean.size(); j++) {

                        oAccountDetailsBean = alAccountDetailsBean.get(j);


                        proc.setString(1, oAccountDetailsBean.getAcc_id());
                        proc.setString(2, oAccountDetailsBean.getShgcode());
                        proc.setString(3, oAccountDetailsBean.getShgname());
                        proc.setString(4, oAccountDetailsBean.getSource1());
                        proc.setString(5, oAccountDetailsBean.getBankname1());
                        proc.setString(6, oAccountDetailsBean.getAccountnumber1());
                        proc.setString(7, oAccountDetailsBean.getAccountopendate1());
                        proc.setString(8, oAccountDetailsBean.getOutstandingbalance());
                        proc.setString(9, oAccountDetailsBean.getInterestaccrued());
                        proc.setString(10, oAccountDetailsBean.getLasttransactiondate());
                        proc.setString(11, ServletName);
                        proc.addBatch();


                    }

                    proc.executeBatch();
                    message = "Account Details created Successfully";

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

            request.setAttribute("message", message);
            request.getRequestDispatcher("/Shg_JSP/AccountDetails.jsp").forward(request, response);

        } else if ("SearchLoan".equalsIgnoreCase(ServletName)) {

            try {
                //BeanUtils.populate(oAccountDetailsBean, request.getParameterMap());
            } catch (IllegalAccessException ex) {
               // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error Occurred during processing.");
            } catch (InvocationTargetException ex) {
               // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error Occurred during processing.");
            } finally { 
                    System.out.println("Processing done."); 
                }

            Connection connection = null;
            ResultSet resultset = null;
            Statement statement = null;
            connection = DbHandler.getDBConnection();
            int SeachFound = 0;

            try {
                oAccountDetailsBeanObj = new AccountDetailsBean();

                SeachFound = 1;
                request.setAttribute("displayFlag", "Y");
                request.setAttribute("checkoption3", "loan1");
                request.setAttribute("checkoption", "amend");

                //oAccountDetailsBeanObj.setShgcode(request.getParameter("shgSearch").toString());
                //oAccountDetailsBeanObj.setShgname(request.getParameter("shgName").toString());

            } catch (Exception ex) {
               // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error Occurred during processing.");
            } finally { 
                    System.out.println("Processing done."); 
                }

            //For Details Part Populate

            try {
                statement = connection.createStatement();
            } catch (SQLException ex) {
               // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error Occurred during processing.");
            } finally { 
                    System.out.println("Processing done."); 
                }
            try {

                //ResultSet rs = statement.executeQuery("select ID,SOURCE, BANK_NAME, to_char(ACCOUNT_OPEN_DATE,'DD/MM/RRRR')as ACCOUNT_OPEN_DATE ,to_char(LAST_DISBURSEMENT_DATE,'DD/MM/RRRR') as LAST_DISBURSEMENT_DATE,ACCOUNT_NO,PRINCIPAL_OUTSTANDING,LOAN_AMOUNT,PRINCIPAL_PAID,INTEREST_OUTSTANDING,INTEREST_PAID  from ACCOUNT_DETAILS_LOAN where SHG_CODE= '" + oAccountDetailsBean.getShgSearch() + "' ");



                while (rs.next()) {
                    oAccountDetailsBean = new AccountDetailsBean();
                    oAccountDetailsBean.setAcc_id(rs.getString("ID"));
                    oAccountDetailsBean.setSource(rs.getString("SOURCE"));
                    oAccountDetailsBean.setBankname(rs.getString("BANK_NAME"));
                    oAccountDetailsBean.setAccountopendate(rs.getString("ACCOUNT_OPEN_DATE"));
                    oAccountDetailsBean.setLastdisbursementdate(rs.getString("LAST_DISBURSEMENT_DATE"));
                    oAccountDetailsBean.setAccountnumber(rs.getString("ACCOUNT_NO"));
                    oAccountDetailsBean.setPrincipaloutstanding(rs.getString("PRINCIPAL_OUTSTANDING"));
                    oAccountDetailsBean.setLoanamount(rs.getString("LOAN_AMOUNT"));
                    oAccountDetailsBean.setPrincipalpaid(rs.getString("PRINCIPAL_PAID"));
                    oAccountDetailsBean.setInterestpaid(rs.getString("INTEREST_PAID"));
                    oAccountDetailsBean.setInterestoutstanding(rs.getString("INTEREST_OUTSTANDING"));

                    alAccountDetailsBean.add(oAccountDetailsBean);


                }

               // statement.close();
               // connection.close();

            } catch (SQLException ex) {
               // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
                System.out.println("Error Occurred during processing.");
            } finally {
                try {
                    if(statement !=null)
                        statement.close();
                    if (connection != null)
                    connection.close();

            } catch (Exception e) {               
               System.out.println("Error Occurred during connection close.");
            }

            }



            request.setAttribute("AccountDetailsBeanObj", oAccountDetailsBeanObj);
            request.setAttribute("alAccountDetailsBean", alAccountDetailsBean);
            request.getRequestDispatcher("/Shg_JSP/AccountDetails.jsp").forward(request, response);

        }else if ("SearchLoan2".equalsIgnoreCase(ServletName)) {

            try {
               // BeanUtils.populate(oAccountDetailsBean, request.getParameterMap());
            } catch (IllegalAccessException ex) {
               // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
                System.out.println("Error Occurred during processing.");
            } catch (InvocationTargetException ex) {
               // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
                System.out.println("Error Occurred during processing.");
            } finally { 
                    System.out.println("Processing done."); 
                }

            Connection connection = null;
            //ResultSet rs = null;
            Statement statement = null;
            connection = DbHandler.getDBConnection();
            int SeachFound = 0;
            //String shg_code=request.getParameter("shgSearch2");

            try {
                oAccountDetailsBeanObj = new AccountDetailsBean();



                SeachFound = 1;
                request.setAttribute("displayFlag", "Y");
                request.setAttribute("checkoption5", "loan3");

                request.setAttribute("checkoption", "transaction");
                request.setAttribute("viewshg2", shgName);



                //oAccountDetailsBeanObj.setShgcode(request.getParameter("shgSearch").toString());
                //oAccountDetailsBeanObj.setShgname(request.getParameter("shgName").toString());
                //oAccountDetailsBeanObj.setShgcode(request.getParameter("shgSearch2").toString());
                //oAccountDetailsBeanObj.setShgname(request.getParameter("viewshg2").toString());
                //oAccountDetailsBeanObj.setShgname(request.getParameter("shgName").toString());

            } catch (Exception ex) {
               // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
                System.out.println("Error Occurred during processing.");
            } finally { 
                    System.out.println("Processing done."); 
                }
            //For Details Part Populate

            try {
                statement = connection.createStatement();
            } catch (SQLException ex) {
               // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error Occurred during processing.");
            } finally { 
                    System.out.println("Processing done."); 
                }
            try {

                //ResultSet rs = statement.executeQuery("select s.txn_ref,s.acc_no,s.txn_amt,s.txn_type,m.member_name,trunc(s.txn_date) as txn_date from shg_txn s,basic_info b,member_information m where s.flag='T' and s.cif=b.cif and b.shg_id=m.shg_id and s.member_code=m.member_id and s.module_type='Loan' and m.shg_code= '" + shg_code + "' ");

                while (rs.next()) {
                    oAccountDetailsBean = new AccountDetailsBean();
                    oAccountDetailsBean.setTxn_ref_no(rs.getString("TXN_REF"));
                    oAccountDetailsBean.setAccountnumber(rs.getString("ACC_NO"));
                    oAccountDetailsBean.setTxn_amt(rs.getString("TXN_AMT"));
                    oAccountDetailsBean.setTxn_type(rs.getString("TXN_TYPE"));
                    oAccountDetailsBean.setMember_name(rs.getString("MEMBER_NAME"));
                    oAccountDetailsBean.setLasttransactiondate(rs.getString("TXN_DATE").substring(0, 10));
                    //oAccountDetailsBeanObj.setShgname(request.getParameter("viewshg2"));

                    alAccountDetailsBean.add(oAccountDetailsBean);

                }

               // statement.close();
               // connection.close();

            } catch (SQLException ex) {
               // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error Occurred during connection close.");
            } finally {
                try {
                    if(statement !=null)
                        statement.close();
                    if (connection != null)
                    connection.close();

            } catch (Exception e) {               
               System.out.println("Error Occurred during connection close.");
            }

            }

            request.setAttribute("AccountDetailsBeanObj", oAccountDetailsBeanObj);
            request.setAttribute("alAccountDetailsBean", alAccountDetailsBean);
            request.getRequestDispatcher("/Shg_JSP/AccountDetails.jsp").forward(request, response);

        } else if ("viewLoan".equalsIgnoreCase(ServletName)) {

            try {
               // BeanUtils.populate(oAccountDetailsBean, request.getParameterMap());
            } catch (IllegalAccessException ex) {
               // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error Occurred during processing.");
            } catch (InvocationTargetException ex) {
                // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
                System.out.println("Error Occurred during processing.");
            } finally { 
                    System.out.println("Processing done."); 
                }

            Connection connection = null;
            ResultSet resultset = null;
            Statement statement = null;
            connection = DbHandler.getDBConnection();
            int SeachFound = 0;


            try {
                oAccountDetailsBeanObj = new AccountDetailsBean();

                SeachFound = 1;
                request.setAttribute("displayFlag", "Y");

                request.setAttribute("checkoption4", "loan2");

                request.setAttribute("checkoption", "view");

                //oAccountDetailsBeanObj.setShgcode(request.getParameter("shgSearch1").toString());
                //oAccountDetailsBeanObj.setShgname(request.getParameter("viewshg").toString());

            } catch (Exception ex) {
               // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error Occurred during processing.");
            } finally { 
                    System.out.println("Processing done."); 
                }



            //For Details Part Populate

            try {
                statement = connection.createStatement();
            } catch (SQLException ex) {
               // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error Occurred during processing.");
            } finally { 
                    System.out.println("Processing done."); 
                }
            try {

               // ResultSet rs = statement.executeQuery("select t.key_1 as account_no,to_char(t.acct_open_dt) as account_open_date,'' as bank_name, to_char(max(d.disb_dt)) as last_disbursement_date,to_char(t.sanction_amt) as loan_amount,to_char(t.prin_outst) as principal_outstanding,to_char(t.intt_outst) as interest_outstanding,to_char (t.prin_paid) as principal_paid,to_char(t.intt_paid) as interest_paid,'Internal' as source from loan_account t,basic_info b, kyc_hdr g, loan_disb_details d where t.customer_no=b.cif and b.name_of_shg=(g.first_name||nvl2(g.middle_name,' '||g.middle_name,'')||nvl2(g.last_name,' '||g.last_name,'')) and b.shg_code= " + request.getParameter("shgSearch1").toString() + "  group by t.key_1,t.acct_open_dt,t.sanction_amt,t.prin_outst,t.intt_outst,t.prin_paid,t.intt_paid union select to_char(m.account_no),to_char(m.account_open_date),m.bank_name,to_char(m.last_disbursement_date),to_char(m.loan_amount),to_char(m.principal_outstanding),to_char(m.interest_outstanding),to_char(m.principal_paid),to_char(m.interest_paid),m.source from account_details_loan m, kyc_hdr g where m.source = 'External'and m.shg_name=(g.first_name||nvl2(g.middle_name,' '||g.middle_name,'')||nvl2(g.last_name,' '||g.last_name,'')) and m.shg_code= '" + request.getParameter("shgSearch1").toString() + "' ");

                while (rs.next()) {
                    oAccountDetailsBean = new AccountDetailsBean();
                    // oAccountDetailsBean.setAcc_id(rs.getString("ID"));
                    oAccountDetailsBean.setSource(rs.getString("source"));
                    oAccountDetailsBean.setBankname(rs.getString("bank_name"));
                    oAccountDetailsBean.setAccountopendate(rs.getString("account_open_date"));
                    oAccountDetailsBean.setLastdisbursementdate(rs.getString("last_disbursement_date"));
                    oAccountDetailsBean.setAccountnumber(rs.getString("account_no"));
                    oAccountDetailsBean.setPrincipaloutstanding(rs.getString("principal_outstanding"));
                    oAccountDetailsBean.setLoanamount(rs.getString("loan_amount"));
                    oAccountDetailsBean.setPrincipalpaid(rs.getString("principal_paid"));
                    oAccountDetailsBean.setInterestpaid(rs.getString("interest_paid"));
                    oAccountDetailsBean.setInterestoutstanding(rs.getString("interest_outstanding"));

                    alAccountDetailsBean.add(oAccountDetailsBean);
                }

               // statement.close();
               // connection.close();

            } catch (SQLException ex) {
               // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error Occurred during connection close.");
            } finally {
                try {
                    if(statement !=null)
                        statement.close();
                    if (connection != null)
                    connection.close();

            } catch (Exception e) {               
               System.out.println("Error Occurred during connection close.");
            }

            }

            request.setAttribute("AccountDetailsBeanObj", oAccountDetailsBeanObj);
            request.setAttribute("alAccountDetailsBean", alAccountDetailsBean);
            request.getRequestDispatcher("/Shg_JSP/AccountDetails.jsp").forward(request, response);

        } else if ("SearchDeposit".equalsIgnoreCase(ServletName)) {

            try {
              //  BeanUtils.populate(oAccountDetailsBean, request.getParameterMap());
            } catch (IllegalAccessException ex) {
               // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error Occurred during processing.");
            } catch (InvocationTargetException ex) {
                // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
                System.out.println("Error Occurred during processing.");
            } finally { 
                    System.out.println("Processing done."); 
                }

            Connection connection = null;
            ResultSet resultset = null;
            Statement statement = null;
            connection = DbHandler.getDBConnection();
            int SeachFound = 0;


            try {
                oAccountDetailsBeanObj = new AccountDetailsBean();

                SeachFound = 1;
                request.setAttribute("displayFlag", "Y");
                request.setAttribute("checkoption3", "deposit1");

                request.setAttribute("checkoption", "amend");

                //oAccountDetailsBeanObj.setShgcode(request.getParameter("shgSearch").toString());
                //oAccountDetailsBeanObj.setShgname(request.getParameter("shgName").toString());

            } catch (Exception ex) {
               // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error Occurred during processing.");
            } finally { 
                    System.out.println("Processing done."); 
                }

            //For Details Part Populate

            try {
                statement = connection.createStatement();
            } catch (SQLException ex) {
               // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error Occurred during processing.");
            } finally { 
                    System.out.println("Processing done."); 
                }
            try {

                //ResultSet rs = statement.executeQuery("select ID,SOURCE, BANK_NAME, to_char(ACCOUNT_OPEN_DATE,'DD/MM/RRRR') as ACCOUNT_OPEN_DATE ,to_char(LAST_TRANSACTION_DATE,'DD/MM/RRRR') as LAST_TRANSACTION_DATE,ACCOUNT_NUMBER,OUTSTANDING_BALANCE,INTEREST_ACCRUED  from ACCOUNT_DETAILS_DEPOSIT where SHG_CODE= '" + oAccountDetailsBean.getShgSearch() + "' ");

                while (rs.next()) {
                    oAccountDetailsBean = new AccountDetailsBean();
                    oAccountDetailsBean.setAcc_id(rs.getString("ID"));
                    oAccountDetailsBean.setSource1(rs.getString("SOURCE"));
                    oAccountDetailsBean.setBankname1(rs.getString("BANK_NAME"));
                    oAccountDetailsBean.setAccountopendate1(rs.getString("ACCOUNT_OPEN_DATE"));
                    oAccountDetailsBean.setLasttransactiondate(rs.getString("LAST_TRANSACTION_DATE"));
                    oAccountDetailsBean.setAccountnumber1(rs.getString("ACCOUNT_NUMBER"));
                    oAccountDetailsBean.setInterestaccrued(rs.getString("INTEREST_ACCRUED"));
                    oAccountDetailsBean.setOutstandingbalance(rs.getString("OUTSTANDING_BALANCE"));

                    alAccountDetailsBean.add(oAccountDetailsBean);

                }

               // statement.close();
               // connection.close();

            } catch (SQLException ex) {
               // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error Occurred during connection close.");
            } finally {
                try {
                    if(statement !=null)
                        statement.close();
                    if (connection != null)
                    connection.close();

            } catch (Exception e) {               
               System.out.println("Error Occurred during connection close.");
            }

            }

            request.setAttribute("AccountDetailsBeanObj", oAccountDetailsBeanObj);
            request.setAttribute("alAccountDetailsBean", alAccountDetailsBean);
            request.getRequestDispatcher("/Shg_JSP/AccountDetails.jsp").forward(request, response);

        }

        else if ("SearchDeposit2".equalsIgnoreCase(ServletName)) {

            try {
               // BeanUtils.populate(oAccountDetailsBean, request.getParameterMap());
            } catch (IllegalAccessException ex) {
               // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error Occurred during processing.");
            } catch (InvocationTargetException ex) {
               // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error Occurred during processing.");
            } finally { 
                    System.out.println("Processing done."); 
                }

            Connection connection = null;
            ResultSet resultset = null;
            Statement statement = null;
            connection = DbHandler.getDBConnection();
            int SeachFound = 0;
            

            try {
                oAccountDetailsBeanObj = new AccountDetailsBean();

                SeachFound = 1;
                request.setAttribute("displayFlag", "Y");
                request.setAttribute("checkoption5", "deposit3");

                request.setAttribute("checkoption", "transaction");
                request.setAttribute("viewshg2", shgName);

                //oAccountDetailsBeanObj.setShgcode(request.getParameter("shgSearch2").toString());
                //oAccountDetailsBeanObj.setShgname(request.getParameter("shgName").toString());
                //oAccountDetailsBeanObj.setShgname(request.getParameter("viewshg2").toString());

            } catch (Exception ex) {
               // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error Occurred during processing.");
            } finally { 
                    System.out.println("Processing done."); 
                }

            //For Details Part Populate

            try {
                statement = connection.createStatement();
            } catch (SQLException ex) {
               // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error Occurred during processing.");
            } finally { 
                    System.out.println("Processing done."); 
                }
            try {

                //ResultSet rs = statement.executeQuery("select s.txn_ref,s.acc_no,b.name_of_shg,s.txn_amt,s.txn_type,m.member_name,trunc(s.txn_date) as txn_date from shg_txn s,basic_info b,member_information m where s.flag='T' and s.cif=b.cif and b.shg_id=m.shg_id and s.member_code=m.member_id and s.module_type='Deposit' and m.shg_code= '" + shg_code + "' ");

                while (rs.next()) {
                    oAccountDetailsBean = new AccountDetailsBean();
                    oAccountDetailsBean.setTxn_ref_no(rs.getString("TXN_REF"));
                    oAccountDetailsBean.setAccountnumber(rs.getString("ACC_NO"));
                    oAccountDetailsBean.setTxn_amt(rs.getString("TXN_AMT"));
                    oAccountDetailsBean.setTxn_type(rs.getString("TXN_TYPE"));
                    oAccountDetailsBean.setMember_name(rs.getString("MEMBER_NAME"));
                    oAccountDetailsBean.setLasttransactiondate(rs.getString("TXN_DATE").substring(0, 10));
                    //oAccountDetailsBeanObj.setShgname(rs.getString("NAME_OF_SHG"));

                    alAccountDetailsBean.add(oAccountDetailsBean);

                }

               // statement.close();
               // connection.close();

            } catch (SQLException ex) {
               // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error Occurred during connection close.");
            } finally {
                try {
                    if(statement !=null)
                        statement.close();
                    if (connection != null)
                    connection.close();

            } catch (Exception e) {               
               System.out.println("Error Occurred during connection close.");
            }

            }

            request.setAttribute("AccountDetailsBeanObj", oAccountDetailsBeanObj);
            request.setAttribute("alAccountDetailsBean", alAccountDetailsBean);
            request.getRequestDispatcher("/Shg_JSP/AccountDetails.jsp").forward(request, response);

        } else if ("SearchDepositMember".equalsIgnoreCase(ServletName)) {

            try {
               // BeanUtils.populate(oAccountDetailsBean, request.getParameterMap());
            } catch (IllegalAccessException ex) {
               // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error Occurred during processing.");
            } catch (InvocationTargetException ex) {
                // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
                System.out.println("Error Occurred during processing.");
            } finally { 
                    System.out.println("Processing done."); 
                }

            Connection connection = null;
            ResultSet resultset = null;
            Statement statement = null;
            connection = DbHandler.getDBConnection();
            int SeachFound = 0;

            
            
            

            if(mem_id=="")
            {
                mem_id="%";
            }

            try {
                oAccountDetailsBeanObj = new AccountDetailsBean();

                SeachFound = 1;
                request.setAttribute("displayFlag", "Y");
                request.setAttribute("s_code", s_code);
                request.setAttribute("mem_name", m_name);
                request.setAttribute("tType", t_Type);
                request.setAttribute("viewshg3", v_shg);
                request.setAttribute("member_id", mem_id);
                request.setAttribute("checkoption", "memberDetails");

                //oAccountDetailsBeanObj.setShgcode(request.getParameter("shgSearch").toString());
                //oAccountDetailsBeanObj.setShgname(request.getParameter("shgName").toString());

            } catch (Exception ex) {
               // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error Occurred during processing.");
            } finally { 
                    System.out.println("Processing done."); 
                }

            //For Details Part Populate

            try {
                statement = connection.createStatement();
            } catch (SQLException ex) {
               // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error Occurred during processing.");
            } finally { 
                    System.out.println("Processing done."); 
                }
            try {

                //ResultSet rs = statement.executeQuery("select s.member_code,(select m.member_name from member_information m where m.member_id=s.member_code)member_name,(select sum(st.txn_amt) from shg_txn st where st.txn_type = 'DR' and st.member_code = s.member_code and st.flag='T') withdrawl,(select sum(st.txn_amt) from shg_txn st where st.txn_type = 'CR' and st.member_code = s.member_code and st.flag='T') Deposit,(select distinct(d.avail_bal) from dep_account d, shg_txn st where d.key_1 = st.acc_no and st.member_code = s.member_code) total_bal from shg_txn s where s.member_code like '%" + mem_id + "%' and s.shg_code =(select b.shg_code from basic_info b where b.shg_id = '" +v_shg+ "') and s.module_type = 'Deposit' and s.flag='T' group by s.member_code");

                while (rs.next()) {
                    oAccountDetailsBean = new AccountDetailsBean();
                    oAccountDetailsBean. setMember_code(rs.getString("MEMBER_CODE"));
                    oAccountDetailsBean. setMember_name(rs.getString("MEMBER_NAME"));
                    oAccountDetailsBean. setTotal_bal(rs.getString("TOTAL_BAL"));
                    oAccountDetailsBean.setWithdrawl(rs.getString("WITHDRAWL"));
                    oAccountDetailsBean.setDeposit(rs.getString("DEPOSIT"));

                    alAccountDetailsBean.add(oAccountDetailsBean);
                }

               // statement.close();
               // connection.close();

            } catch (SQLException ex) {
               // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error Occurred during processing.");
            } finally {
                try {
                    if(statement !=null)
                        statement.close();
                    if (connection != null)
                    connection.close();

            } catch (Exception e) {               
               System.out.println("Error Occurred during connection close.");
            }

            }

            request.setAttribute("AccountDetailsBeanObj", oAccountDetailsBeanObj);
            request.setAttribute("alAccountDetailsBean", alAccountDetailsBean);
            request.getRequestDispatcher("/Shg_JSP/AccountDetails.jsp").forward(request, response);

        }

        else if ("viewDeposit".equalsIgnoreCase(ServletName)) {

            try {
                //BeanUtils.populate(oAccountDetailsBean, request.getParameterMap());
            } catch (IllegalAccessException ex) {
               // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error Occurred during processing.");
            } catch (InvocationTargetException ex) {
               // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error Occurred during processing.");
            } finally { 
                    System.out.println("Processing done."); 
                }

            Connection connection = null;
            ResultSet resultset = null;
            Statement statement = null;
            connection = DbHandler.getDBConnection();
            int SeachFound = 0;


            try {
                oAccountDetailsBeanObj = new AccountDetailsBean();

                SeachFound = 1;
                request.setAttribute("displayFlag", "Y");

                request.setAttribute("checkoption4", "deposit2");
                request.setAttribute("checkoption", "view");

                //oAccountDetailsBeanObj.setShgcode(request.getParameter("shgSearch1").toString());
                //oAccountDetailsBeanObj.setShgname(request.getParameter("viewshg").toString());

            } catch (Exception ex) {
               // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error Occurred during processing.");
            } finally { 
                    System.out.println("Processing done."); 
                }

            //For Details Part Populate

            try {
                statement = connection.createStatement();
            } catch (SQLException ex) {
               // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error Occurred during processing.");
            } finally { 
                    System.out.println("Processing done."); 
                }
            try {

                //ResultSet rs = statement.executeQuery("select t.key_1 as account_number,TO_CHAR(t.acct_open_dt) as account_open_date,t.avail_bal as outstanding_balance,t.intt_outst as interest_accrued,'Internal' as source, to_char(max(f.tran_date)) as last_transaction_date,'' as bank_name from dep_account t,basic_info b, kyc_hdr g,dep_txn f where t.customer_no=b.cif and b.name_of_shg=(g.first_name||nvl2(g.middle_name,' '||g.middle_name,'')||nvl2(g.last_name,' '|| g.last_name,'')) and t.key_1=f.acct_no and b.shg_code= " + request.getParameter("shgSearch1").toString() + " group by t.key_1,TO_CHAR(t.acct_open_dt),t.avail_bal,t.intt_outst  union select TO_CHAR(m.account_number),TO_CHAR(m.account_open_date),m.outstanding_balance,m.interest_accrued,m.source,to_char(m.last_transaction_date),m.bank_name from account_details_deposit m, kyc_hdr g where m.source = 'External'and m.shg_name=(g.first_name||nvl2(g.middle_name,' '|| g.middle_name,'')||nvl2(g.last_name,' '||g.last_name,''))and  m.shg_code= '" + request.getParameter("shgSearch1").toString() + "' ");

                while (rs.next()) {
                    oAccountDetailsBean = new AccountDetailsBean();
                    //oAccountDetailsBean.setAcc_id(rs.getString("ID"));
                    oAccountDetailsBean.setSource1(rs.getString("source"));
                    oAccountDetailsBean.setBankname1(rs.getString("bank_name"));
                    oAccountDetailsBean.setAccountopendate1(rs.getString("account_open_date"));
                    oAccountDetailsBean.setLasttransactiondate(rs.getString("last_transaction_date"));
                    oAccountDetailsBean.setAccountnumber1(rs.getString("account_number"));
                    oAccountDetailsBean.setInterestaccrued(rs.getString("interest_accrued"));
                    oAccountDetailsBean.setOutstandingbalance(rs.getString("outstanding_balance"));

                    alAccountDetailsBean.add(oAccountDetailsBean);

                }

               // statement.close();
               // connection.close();

            } catch (SQLException ex) {
               // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error Occurred during processing.");
            } finally {
                try {
                    if(statement !=null)
                        statement.close();
                    if (connection != null)
                    connection.close();

            } catch (Exception e) {               
               System.out.println("Error Occurred during connection close.");
            }

            }

            request.setAttribute("AccountDetailsBeanObj", oAccountDetailsBeanObj);
            request.setAttribute("alAccountDetailsBean", alAccountDetailsBean);
            request.getRequestDispatcher("/Shg_JSP/AccountDetails.jsp").forward(request, response);

        } else if (ServletName.equalsIgnoreCase("UpdateLoan")) {

            String rowStatus = null;
            String acc_id = null;
            String deletedRows = null;
            int f = 0;

            try {
                BeanUtils.populate(oAccountDetailsBean, request.getParameterMap());
            } catch (IllegalAccessException ex) {
                f = 1;
               // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error Occurred during processing.");
            } catch (InvocationTargetException ex) {
                f = 1;
               // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error Occurred during processing.");
            } finally { 
                    System.out.println("Processing done."); 
                }


            int counter = Integer.parseInt(request.getParameter("rowCounter"));

            for (int i = 1; i <= counter; i++) {
                try {
                    String shgcode = request.getParameter("shgcodeAmend");
                    String shgname = request.getParameter("shgnameAmend");
                    acc_id = request.getParameter("acc_idAmend" + i);
                    



                    if ((accountnumber != null) && (rowStatus != null)) {
                        oAccountDetailsBean = new AccountDetailsBean();
                        oAccountDetailsBean.setAcc_id(acc_id);
                        oAccountDetailsBean.setShgcode(shgcode);
                        oAccountDetailsBean.setShgname(shgname);
                        oAccountDetailsBean.setSource(source);
                        oAccountDetailsBean.setBankname(bankname);
                        oAccountDetailsBean.setLoanamount(loanamount);
                        oAccountDetailsBean.setAccountnumber(accountnumber);
                        oAccountDetailsBean.setAccountopendate(accountopendate);
                        oAccountDetailsBean.setLastdisbursementdate(lastdisbursementdate);
                        oAccountDetailsBean.setPrincipaloutstanding(principaloutstanding);
                        oAccountDetailsBean.setPrincipalpaid(principalpaid);
                        oAccountDetailsBean.setInterestoutstanding(interestoutstanding);
                        oAccountDetailsBean.setInterestpaid(interestpaid);

                        if (rowStatus.equals("N")) {
                            alAccountDetailsBeanNew.add(oAccountDetailsBean);
                        } else {
                            alAccountDetailsBeanUpdated.add(oAccountDetailsBean);
                        }
                    }
                } catch (Exception e) {
                    f = 1;
                } finally { 
                    System.out.println("Processing done."); 
                }
            }


            //For Deletion

            deletedRows = request.getParameter("deletedRows");

            if ((!deletedRows.equalsIgnoreCase(null)) || (!deletedRows.equalsIgnoreCase(""))) {
                StringTokenizer tokenizer = new StringTokenizer(deletedRows, ",");

                while (tokenizer.hasMoreTokens()) {

                    oAccountDetailsBean = new AccountDetailsBean();
                    oAccountDetailsBean.setAcc_id(tokenizer.nextToken());
                    oAccountDetailsBean.setShgcode(request.getParameter("shgcodeAmend"));

                    alAccountDetailsBeanDeleted.add(oAccountDetailsBean);

               // }
            }

            if (alAccountDetailsBeanUpdated.size() > 0) {

                try {
                    con = DbHandler.getDBConnection();
                    try {
                        proc = con.prepareCall("{ call SHG_OPERATIONS.Upsert_account_details_loan(?,?,?,?,?,?,?,?,?,?,?,?,?,?) }");
                    //} catch (SQLException ex) {
                        f = 1;
                       // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
                       System.out.println("Error Occurred during processing.");
                    } finally { 
                        System.out.println("Processing done."); 
                      }


                    for (int j = 0; j < alAccountDetailsBeanUpdated.size(); j++) {

                        oAccountDetailsBean = new AccountDetailsBean();

                        oAccountDetailsBean = alAccountDetailsBeanUpdated.get(j);


                        proc.setString(1, oAccountDetailsBean.getAcc_id());
                        proc.setString(2, oAccountDetailsBean.getShgcode());
                        proc.setString(3, oAccountDetailsBean.getShgname());
                        proc.setString(4, oAccountDetailsBean.getSource());
                        proc.setString(5, oAccountDetailsBean.getBankname());
                        proc.setString(6, oAccountDetailsBean.getAccountnumber());
                        proc.setString(7, oAccountDetailsBean.getAccountopendate());
                        proc.setString(8, oAccountDetailsBean.getLastdisbursementdate());
                        proc.setString(9, oAccountDetailsBean.getLoanamount());
                        proc.setString(10, oAccountDetailsBean.getPrincipaloutstanding());
                        proc.setString(11, oAccountDetailsBean.getPrincipalpaid());
                        proc.setString(12, oAccountDetailsBean.getInterestoutstanding());
                        proc.setString(13, oAccountDetailsBean.getInterestpaid());
                        proc.setString(14, ServletName);
                        proc.addBatch();


                    }

                    proc.executeBatch();

                } catch (SQLException ex) {
                    f = 1;
                   // Logger.getLogger(AccountDetailsServlet.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) {
                        f = 1;
                        System.out.println("Error Occurred during connection close.");
                    }
                    try {
                        con.close();
                    } catch (SQLException ex) {
                        f = 1;
                       // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
                       // System.out.println(ex.toString());
                       System.out.println("Error Occurred during connection close.");
                    }
                }
            }

            //for deletion

            if (alAccountDetailsBeanDeleted.size() > 0) {

                try {
                    con = DbHandler.getDBConnection();
                    try {
                        proc = con.prepareCall("{ call SHG_OPERATIONS.Upsert_account_details_loan(?,?,?,?,?,?,?,?,?,?,?,?,?,?) }");
                    } catch (SQLException ex) {
                        f = 1;
                       // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
                       System.out.println("Error Occurred during processing.");
                    } finally { 
                        System.out.println("Processing done."); 
                    }

                    for (int j = 0; j < alAccountDetailsBeanDeleted.size(); j++) {

                        // oAccountDetailsBean = new AccountDetailsBean();

                        oAccountDetailsBean = alAccountDetailsBeanDeleted.get(j);

                        proc.setString(2, oAccountDetailsBean.getShgcode());
                        proc.setString(1, oAccountDetailsBean.getAcc_id());
                        proc.setString(3, null);
                        proc.setString(4, null);
                        proc.setString(5, null);
                        proc.setString(6, null);
                        proc.setString(7, null);
                        proc.setString(8, null);
                        proc.setString(9, null);
                        proc.setString(10, null);
                        proc.setString(11, null);
                        proc.setString(12, null);
                        proc.setString(13, null);
                        proc.setString(14, "DeleteLoan");
                        proc.addBatch();

                    }
                    proc.executeBatch();

                } catch (SQLException ex) {
                    f = 1;
                   // Logger.getLogger(AccountDetailsServlet.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) {
                        f = 1;
                       // System.out.println(e.toString());
                       System.out.println("Error Occurred during connection close.");
                    }
                    try {
                        con.close();
                    } catch (SQLException ex) {
                        f = 1;
                       // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
                       // System.out.println(ex.toString());
                       System.out.println("Error Occurred during connection close.");
                    }
                }
            }


            //For New Insertion


            if (alAccountDetailsBeanNew.size() > 0) {

                try {
                    con = DbHandler.getDBConnection();
                    try {
                        proc = con.prepareCall("{ call SHG_OPERATIONS.Upsert_account_details_loan(?,?,?,?,?,?,?,?,?,?,?,?,?,?) }");
                    } catch (SQLException ex) {
                        f = 1;
                       // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
                       System.out.println("Error Occurred during processing.");
                    } finally { 
                        System.out.println("Processing done."); 
                    }


                    for (int j = 0; j < alAccountDetailsBeanNew.size(); j++) {

                        oAccountDetailsBean = new AccountDetailsBean();
                        oAccountDetailsBean = alAccountDetailsBeanNew.get(j);



                        proc.setString(1, oAccountDetailsBean.getAcc_id());
                        proc.setString(2, oAccountDetailsBean.getShgcode());
                        proc.setString(3, oAccountDetailsBean.getShgname());
                        proc.setString(4, oAccountDetailsBean.getSource());
                        proc.setString(5, oAccountDetailsBean.getBankname());
                        proc.setString(6, oAccountDetailsBean.getAccountnumber());
                        proc.setString(7, oAccountDetailsBean.getAccountopendate());
                        proc.setString(8, oAccountDetailsBean.getLastdisbursementdate());
                        proc.setString(9, oAccountDetailsBean.getLoanamount());
                        proc.setString(10, oAccountDetailsBean.getPrincipaloutstanding());
                        proc.setString(11, oAccountDetailsBean.getPrincipalpaid());
                        proc.setString(12, oAccountDetailsBean.getInterestoutstanding());
                        proc.setString(13, oAccountDetailsBean.getInterestpaid());
                        proc.setString(14, "CreateLoan");

                        proc.addBatch();


                    }

                    proc.executeBatch();

                } catch (SQLException ex) {
                    f = 1;
                   // Logger.getLogger(AccountDetailsServlet.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) {
                        f = 1;
                       // System.out.println(e.toString());
                       System.out.println("Error Occurred during connection close.");
                    }
                    try {
                        con.close();
                    } catch (SQLException ex) {
                        f = 1;
                       // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
                       // System.out.println(ex.toString());
                       System.out.println("Error Occurred during connection close.");
                    }
                }
            }
            if (f != 0) {
                message = "Error occured during Account Details amendment";
            } else {
                message = "Account Details amended Successfully";
            }

            request.setAttribute("message", message);
            request.getRequestDispatcher("/Shg_JSP/AccountDetails.jsp").forward(request, response);

        } else if (ServletName.equalsIgnoreCase("UpdateDeposit")) {



            String rowStatus = null;
            String acc_id = null;
            String deletedRows = null;
            int f = 0;

            try {
                BeanUtils.populate(oAccountDetailsBean, request.getParameterMap());
            } catch (IllegalAccessException ex) {
                f = 1;
               // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error Occurred during processing.");
            } catch (InvocationTargetException ex) {
                f = 1;
               // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error Occurred during processing.");
            } finally { 
                    System.out.println("Processing done."); 
                }


            int counter = Integer.parseInt(request.getParameter("rowCounter"));

            for (int i = 1; i <= counter; i++) {
                try {
                    String shgcode = request.getParameter("shgcodeAmend");
                    String shgname = request.getParameter("shgnameAmend");
                    acc_id = request.getParameter("acc_idAmend" + i);
                    String source = request.getParameter("source1Amend" + i);
                    String bankname = request.getParameter("bankname1Amend" + i);
                    String accountnumber = request.getParameter("accountnumber1Amend" + i);
                    String accountopendate = request.getParameter("accountopendate1Amend" + i);
                    String outstandingbalance = request.getParameter("outstandingbalanceAmend" + i);
                    String interestaccrued = request.getParameter("interestaccruedAmend" + i);
                    String lasttransactiondate = request.getParameter("lasttransactiondateAmend" + i);
                    rowStatus = request.getParameter("rowStatus" + i);



                    //if ((accountnumber != null) && (rowStatus != null)) {
                        //oAccountDetailsBean = new AccountDetailsBean();
                        //oAccountDetailsBean.setAcc_id(acc_id);
                        //oAccountDetailsBean.setShgcode(shgcode);
                        //oAccountDetailsBean.setShgname(shgname);
                        //oAccountDetailsBean.setSource1(source);
                        //oAccountDetailsBean.setBankname1(bankname);
                        //oAccountDetailsBean.setAccountnumber1(accountnumber);
                        //oAccountDetailsBean.setAccountopendate1(accountopendate);
                        oAccountDetailsBean.setOutstandingbalance(outstandingbalance);
                        oAccountDetailsBean.setInterestaccrued(interestaccrued);
                        oAccountDetailsBean.setLasttransactiondate(lasttransactiondate);

                        if (rowStatus.equals("N")) {
                            alAccountDetailsBeanNew.add(oAccountDetailsBean);
                        } else {
                            alAccountDetailsBeanUpdated.add(oAccountDetailsBean);
                        }
                    }
                } catch (Exception e) {
                    f = 1;
                } finally { 
                    System.out.println("Processing done."); 
                }
            }








            //For Deletion

            deletedRows = request.getParameter("deletedRows");

            if ((!deletedRows.equalsIgnoreCase(null)) || (!deletedRows.equalsIgnoreCase(""))) {
                StringTokenizer tokenizer = new StringTokenizer(deletedRows, ",");

                while (tokenizer.hasMoreTokens()) {

                    oAccountDetailsBean = new AccountDetailsBean();
                    oAccountDetailsBean.setAcc_id(tokenizer.nextToken());
                    oAccountDetailsBean.setShgcode(request.getParameter("shgcodeAmend"));

                    alAccountDetailsBeanDeleted.add(oAccountDetailsBean);

                //}
            }




            if (alAccountDetailsBeanUpdated.size() > 0) {

                try {
                    //con = DbHandler.getDBConnection();
                    try {
                        proc = con.prepareCall("{ call SHG_OPERATIONS.Upsert_account_details_deposit(?,?,?,?,?,?,?,?,?,?,?)}");
                    } catch (SQLException ex) {
                        f = 1;
                       // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
                       System.out.println("Error Occurred during processing.");
                    } finally { 
                    System.out.println("Processing done."); 
                }


                    for (int j = 0; j < alAccountDetailsBeanUpdated.size(); j++) {

                        oAccountDetailsBean = new AccountDetailsBean();

                        oAccountDetailsBean = alAccountDetailsBeanUpdated.get(j);



                        proc.setString(1, oAccountDetailsBean.getAcc_id());
                        proc.setString(2, oAccountDetailsBean.getShgcode());
                        proc.setString(3, oAccountDetailsBean.getShgname());
                        proc.setString(4, oAccountDetailsBean.getSource1());
                        proc.setString(5, oAccountDetailsBean.getBankname1());
                        proc.setString(6, oAccountDetailsBean.getAccountnumber1());
                        proc.setString(7, oAccountDetailsBean.getAccountopendate1());
                        proc.setString(8, oAccountDetailsBean.getOutstandingbalance());
                        proc.setString(9, oAccountDetailsBean.getInterestaccrued());
                        proc.setString(10, oAccountDetailsBean.getLasttransactiondate());
                        proc.setString(11, ServletName);
                        proc.addBatch();


                    }

                    proc.executeBatch();

                } catch (SQLException ex) {
                    f = 1;
                   // Logger.getLogger(AccountDetailsServlet.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) {
                        f = 1;
                       // System.out.println(e.toString());
                       System.out.println("Error Occurred during connection close.");
                    }
                    try {
                        con.close();
                    } catch (SQLException ex) {
                        f = 1;
                       // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
                       // System.out.println(ex.toString());
                       System.out.println("Error Occurred during connection close.");
                    }
                }
            }

            //for deletion

            if (alAccountDetailsBeanDeleted.size() > 0) {

                try {
                    con = DbHandler.getDBConnection();
                    try {
                        proc = con.prepareCall("{ call SHG_OPERATIONS.Upsert_account_details_deposit(?,?,?,?,?,?,?,?,?,?,?)}");
                    } catch (SQLException ex) {
                        f = 1;
                       // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
                       System.out.println("Error Occurred during connection close.");
                    } finally {
                        System.out.println("Processing done."); 
                    }


                    for (int j = 0; j < alAccountDetailsBeanDeleted.size(); j++) {

                        // oAccountDetailsBean = new AccountDetailsBean();

                        oAccountDetailsBean = alAccountDetailsBeanDeleted.get(j);


                        proc.setString(2, oAccountDetailsBean.getShgcode());
                        proc.setString(1, oAccountDetailsBean.getAcc_id());
                        proc.setString(3, null);
                        proc.setString(4, null);
                        proc.setString(5, null);
                        
                        proc.setString(7, null);
                        proc.setString(8, null);
                        proc.setString(9, null);
                        proc.setString(10, null);
                        proc.setString(11, "DeleteDeposit");
                        proc.addBatch();


                    }

                    proc.executeBatch();

                } catch (SQLException ex) {
                    f = 1;
                   // Logger.getLogger(AccountDetailsServlet.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) {
                        f = 1;
                       // System.out.println(e.toString());
                       System.out.println("Error Occurred during connection close.");
                    }
                    try {
                        con.close();
                    } catch (SQLException ex) {
                        f = 1;
                       // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
                       // System.out.println(ex.toString());
                       System.out.println("Error Occurred during connection close.");
                    }
                }
            }


            //For New Insertion


            if (alAccountDetailsBeanNew.size() > 0) {

                try {
                    con = DbHandler.getDBConnection();
                    try {
                        proc = con.prepareCall("{ call SHG_OPERATIONS.Upsert_account_details_deposit(?,?,?,?,?,?,?,?,?,?,?)}");
                    } catch (SQLException ex) {
                        f = 1;
                       // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
                       System.out.println("Error Occurred during processing.");
                    } finally { 
                        System.out.println("Processing done."); 
                    }


                    for (int j = 0; j < alAccountDetailsBeanNew.size(); j++) {

                        oAccountDetailsBean = alAccountDetailsBeanNew.get(j);


                        proc.setString(1, oAccountDetailsBean.getAcc_id());
                        proc.setString(2, oAccountDetailsBean.getShgcode());
                        proc.setString(3, oAccountDetailsBean.getShgname());
                        proc.setString(4, oAccountDetailsBean.getSource1());
                        proc.setString(5, oAccountDetailsBean.getBankname1());
                        proc.setString(6, oAccountDetailsBean.getAccountnumber1());
                        proc.setString(7, oAccountDetailsBean.getAccountopendate1());
                        proc.setString(8, oAccountDetailsBean.getOutstandingbalance());
                        proc.setString(9, oAccountDetailsBean.getInterestaccrued());
                        proc.setString(10, oAccountDetailsBean.getLasttransactiondate());

                        proc.setString(11, "CreateDeposit");

                        proc.addBatch();


                    }

                    proc.executeBatch();

                } catch (SQLException ex) {
                    f = 1;
                   // Logger.getLogger(AccountDetailsServlet.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) {
                        f = 1;
                       // System.out.println(e.toString());
                       System.out.println("Error Occurred during connection close.");
                    }
                    try {
                        con.close();
                    } catch (SQLException ex) {
                        f = 1;
                       // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
                       // System.out.println(ex.toString());
                       System.out.println("Error Occurred during connection close.");
                    }
                }
            }

            if (f != 0) {
                message = "Error occured during Account Details amendment";
            } else {
                message = "Account Details amended Successfully";
            }
            request.setAttribute("message", message);
            request.getRequestDispatcher("/Shg_JSP/AccountDetails.jsp").forward(request, response);

        }

        else if(ServletName.equalsIgnoreCase("SearchDepositMember"))
        {
            try {
                BeanUtils.populate(oAccountDetailsBean, request.getParameterMap());
            } catch (IllegalAccessException ex) {
               // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error Occurred during processing.");
            } catch (InvocationTargetException ex) {
               // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error Occurred during processing.");
            } finally { 
                    System.out.println("Processing done."); 
                }

            Connection connection = null;
            ResultSet resultset = null;
            
            connection = DbHandler.getDBConnection();
            int SeachFound = 0;





            try {
                oAccountDetailsBeanObj = new AccountDetailsBean();



                SeachFound = 1;
                request.setAttribute("displayFlag", "Y");

                request.setAttribute("checkoption4", "deposit2");
                request.setAttribute("checkoption", "view");


                oAccountDetailsBeanObj.setShgcode(request.getParameter("shgSearch1").toString());
                oAccountDetailsBeanObj.setShgname(request.getParameter("viewshg").toString());




            } catch (Exception ex) {
               // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error Occurred during processing.");
            } finally { 
                    System.out.println("Processing done."); 
                }



            //For Details Part Populate

            try {
                statement = connection.createStatement();
            } catch (SQLException ex) {
               // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error Occurred during processing.");
            } finally { 
                    System.out.println("Processing done."); 
                }
            try {

                //ResultSet rs = statement.executeQuery("select * from shg_txn s where s.module_type='Deposit' and s.member_code= '" + request.getParameter("member_id").toString() + "' ");


                while (rs.next()) {
                    oAccountDetailsBean = new AccountDetailsBean();
                    //oAccountDetailsBean.setAcc_id(rs.getString("ID"));
                    oAccountDetailsBean.setSource1(rs.getString("source"));
                    oAccountDetailsBean.setBankname1(rs.getString("bank_name"));
                    oAccountDetailsBean.setAccountopendate1(rs.getString("account_open_date"));
                    oAccountDetailsBean.setLasttransactiondate(rs.getString("last_transaction_date"));
                    oAccountDetailsBean.setAccountnumber1(rs.getString("account_number"));
                    oAccountDetailsBean.setInterestaccrued(rs.getString("interest_accrued"));
                    oAccountDetailsBean.setOutstandingbalance(rs.getString("outstanding_balance"));

                    alAccountDetailsBean.add(oAccountDetailsBean);


                }

               // statement.close();
               // connection.close();

            } catch (SQLException ex) {
               // Logger.getLogger(AccountDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error Occurred during processing.");
            } finally {
                try {
                    if(statement !=null)
                        statement.close();
                    if (connection != null)
                    connection.close();

            } catch (Exception e) {
               // e.printStackTrace();
               System.out.println("Error Occurred during connection close.");
            }

            }




            request.setAttribute("AccountDetailsBeanObj", oAccountDetailsBeanObj);
            request.setAttribute("alAccountDetailsBean", alAccountDetailsBean);
            request.getRequestDispatcher("/Shg_JSP/AccountDetails.jsp").forward(request, response);
        }



    }

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