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

import DataEntryBean.MemberDetailsBean;
import DataEntryBean.MemberDetailsBean;
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 MemberDetailsServlet 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);
        Integer flag=0;

      //  String ServletName = request.getParameter("handle_id");
        String pacsId = (String) session.getAttribute("pacsId");

        MemberDetailsBean oMemberDetailsBean = new MemberDetailsBean();
        MemberDetailsBean oMemberDetailsBeanObj = new MemberDetailsBean();
       ArrayList<MemberDetailsBean> alMemberDetailsBean = new ArrayList<MemberDetailsBean>();



        //For Amend Div

        ArrayList<MemberDetailsBean> alMemberDetailsBeanNew = new ArrayList<MemberDetailsBean>();
        ArrayList<MemberDetailsBean> alMemberDetailsBeanDeleted = new ArrayList<MemberDetailsBean>();
        ArrayList<MemberDetailsBean> alMemberDetailsBeanUpdated = new ArrayList<MemberDetailsBean>();

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

            try {
              //  BeanUtils.populate(oMemberDetailsBean, request.getParameterMap());
            } catch (IllegalAccessException ex) {
               // Logger.getLogger(MemberDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error occurred during processing.");
            } catch (InvocationTargetException ex) {
               // Logger.getLogger(MemberDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error occurred during processing.");
            }

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

                proc.setString(1, oMemberDetailsBean.getMale());
                proc.setString(2, oMemberDetailsBean.getFemale());
                proc.setString(3, oMemberDetailsBean.getOther());
                proc.setString(4, oMemberDetailsBean.getTotal());
                proc.setString(5, oMemberDetailsBean.getMuslim());
                proc.setString(6, oMemberDetailsBean.getChristian());
                proc.setString(7, oMemberDetailsBean.getBuddhist());
                proc.setString(8, oMemberDetailsBean.getSikh());
                proc.setString(9, oMemberDetailsBean.getParsi());
                proc.setString(10, oMemberDetailsBean.getJain());
                proc.setString(11, oMemberDetailsBean.getOthers());
                proc.setString(12, oMemberDetailsBean.getShgcode());
                proc.setString(13, ServletName);
                proc.registerOutParameter(14, java.sql.Types.VARCHAR);
                proc.setString(15, pacsId);

                proc.execute();

              //  message = proc.getString(14);

                if(message.equalsIgnoreCase("Member Details already present for SHG"))
                {
                    flag=1;
                }



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

            }
            if(flag==0)
            {
            int counter = Integer.parseInt(request.getParameter("rowCounter"));

            for (int i = 1; i <= counter; i++) {
                try {
                   // String shgcode = request.getParameter("shgcode");
                   // String membername = request.getParameter("membername" + i);
                   // String gender = request.getParameter("gender" + i);
                   // String dob = request.getParameter("dob" + i);
                   // String guardianname = request.getParameter("guardianname" + i);
                   // String address = request.getParameter("address" + i);
                    //String religion = request.getParameter("religion" + i);
                    //String caste=request.getParameter("caste"+i);
                   // String qualification=request.getParameter("qualification"+i);
                   // String cif=request.getParameter("cif"+i);


                    if (membername != null) {
                        oMemberDetailsBean = new MemberDetailsBean();
                        oMemberDetailsBean.setShgcode(shgcode);
                        oMemberDetailsBean.setMembername(membername);
                        oMemberDetailsBean.setGender(gender);
                        oMemberDetailsBean.setDob(dob);
                        oMemberDetailsBean.setGuardianname(guardianname);
                        oMemberDetailsBean.setAddress(address);
                        oMemberDetailsBean.setReligion(religion);
                        oMemberDetailsBean.setAddress(address);
                        oMemberDetailsBean.setReligion(religion);
                        oMemberDetailsBean.setCaste(caste);
                        oMemberDetailsBean.setQualification(qualification);
                        oMemberDetailsBean.setCif(cif);
                        alMemberDetailsBean.add(oMemberDetailsBean);
                    }
                } catch (Exception e) {
                    System.out.println("Error occurred during processing.");
                }
            }
            if ((alMemberDetailsBean.size() > 0) && (!message.equalsIgnoreCase(""))) {

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


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

                        oMemberDetailsBean = alMemberDetailsBean.get(j);


                        proc.setString(1, oMemberDetailsBean.getMemberID());
                        proc.setString(2, oMemberDetailsBean.getShgcode());
                        proc.setString(3, oMemberDetailsBean.getMemberName());
                        proc.setString(4, oMemberDetailsBean.getGender());
                        proc.setString(5, oMemberDetailsBean.getDob());
                        proc.setString(6, oMemberDetailsBean.getGuardianName());
                        proc.setString(7, oMemberDetailsBean.getAddress());
                        proc.setString(8, oMemberDetailsBean.getReligion());
                        proc.setString(9, ServletName);
                        proc.setString(10,oMemberDetailsBean.getCaste());
                        proc.setString(11,oMemberDetailsBean.getQualification());
                        proc.setString(12, pacsId);
                        proc.setString(13,oMemberDetailsBean.getCif());

                        proc.addBatch();


                    }

                    proc.executeBatch();


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

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

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

            try {
               // BeanUtils.populate(oMemberDetailsBean, request.getParameterMap());
            } catch (IllegalAccessException ex) {
               // Logger.getLogger(MemberDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error occurred during processing.");
            } catch (InvocationTargetException ex) {
               // Logger.getLogger(MemberDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error occurred during processing.");
            }

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

           /* try {
                statement = connection.createStatement();
            } catch (SQLException ex) {
                Logger.getLogger(MemberDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
            }*/
              try {
              oMemberDetailsBeanObj = new MemberDetailsBean();

           // oMemberDetailsBeanObj.setShgcode(request.getParameter("shgSearch").toString() );
            String shg = request.getParameter("shgSearch").toString();

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

           // statement.close();

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

            if (SeachFound == 0) {
            message = "Vendor not exists in the system";
            request.setAttribute("message", message);
            }*/

            //For Details Part Populate

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

//                ResultSet rs = statement.executeQuery("select MEMBER_ID,MEMBER_NAME, GENDER, to_char(DOB,'DD/MM/RRRR') as DOB , GUARDIAN_NAME,ADDRESS,RELIGION,CASTE,QUALIFICATION,to_char(ENTRY_DATE,'DD-MON-RRRR') ENTRY_DATE,to_char(AMEND_DATE,'DD-MON-RRRR') AMEND_DATE from MEMBER_INFORMATION mi where SHG_CODE= '" + oMemberDetailsBean.getShgSearch() + "'  and mi.shg_id = (select shg_id from basic_info bi where bi.shg_code = '" + oMemberDetailsBean.getShgSearch() + "' and bi.pacs_id = '" +pacsId+ "') ");
               // ResultSet rs = statement.executeQuery(" select MEMBER_ID, cif_no, (select trim(first_name || ' ' || middle_name || ' ' || last_name) from kyc_hdr kd where kd.cif_no = mi.cif_no) MEMBER_NAME, (case (select kd.gender from kyc_hdr kd where kd.cif_no = mi.cif_no) when 'M' then 'Male' when 'F' then 'Female' else 'Other' end) GENDER, (select kd.birth_date from kyc_hdr kd where kd.cif_no = mi.cif_no) as DOB, (select trim(kd.guardian_name) from kyc_hdr kd where kd.cif_no = mi.cif_no) GUARDIAN_NAME, (select trim(kd.address_1 || ' ' || kd.address_2 || ' ' || kd.address_3) from kyc_hdr kd where kd.cif_no = mi.cif_no) ADDRESS, (case (select kd.religion from kyc_hdr kd where kd.cif_no = mi.cif_no) when '1' then 'Hindu' when '2' then 'Muslim' when '3' then 'Christian' when '4' then 'Sikh' when '5' then 'Jain' when '6' then 'Buddhist' when '7' then 'Parsi' else 'Others' end) RELIGION, (select kd.caste from kyc_hdr kd where kd.cif_no = mi.cif_no) CASTE, QUALIFICATION, to_char(ENTRY_DATE, 'DD-MON-RRRR') ENTRY_DATE, to_char(AMEND_DATE, 'DD-MON-RRRR') AMEND_DATE from MEMBER_INFORMATION mi where SHG_CODE = '" + oMemberDetailsBean.getShgSearch() + "' and mi.shg_id = (select shg_id from basic_info bi where bi.shg_code = '" + oMemberDetailsBean.getShgSearch() + "' and bi.pacs_id = '" +pacsId+ "') ");    

                while (rs.next()) {
                  oMemberDetailsBean = new MemberDetailsBean();
                    oMemberDetailsBean.setMemberID(rs.getString("MEMBER_ID"));
                    oMemberDetailsBean.setMembername(rs.getString("MEMBER_NAME"));
                    oMemberDetailsBean.setGender(rs.getString("GENDER"));
                    oMemberDetailsBean.setDob(rs.getString("DOB"));
                    oMemberDetailsBean.setGuardianname(rs.getString("GUARDIAN_NAME"));
                    oMemberDetailsBean.setAddress(rs.getString("ADDRESS"));
                    oMemberDetailsBean.setReligion(rs.getString("RELIGION"));
                    oMemberDetailsBean.setCaste(rs.getString("CASTE"));
                    oMemberDetailsBean.setQualification(rs.getString("Qualification"));
                    oMemberDetailsBean.setCif(rs.getString("cif_no"));
                    oMemberDetailsBean.setEntry_date(rs.getString("ENTRY_DATE"));
                    oMemberDetailsBean.setAmend_date(rs.getString("AMEND_DATE"));

                    alMemberDetailsBean.add(oMemberDetailsBean);

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

            } catch (SQLException ex) {
               // Logger.getLogger(MemberDetailsServlet.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("MemberDetailsBeanObj", oMemberDetailsBeanObj);
            request.setAttribute("alMemberDetailsBean", alMemberDetailsBean);
            request.getRequestDispatcher("/Shg_JSP/MemberDetails.jsp").forward(request, response);

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

            String rowStatus = null;
            String memberID = null;
            String deletedRows = null;

            try {
             //   BeanUtils.populate(oMemberDetailsBean, request.getParameterMap());
            } catch (IllegalAccessException ex) {
               // Logger.getLogger(MemberDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error occurred during processing.");
            } catch (InvocationTargetException ex) {
               // Logger.getLogger(MemberDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error occurred during processing.");
            }

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

                proc.setString(1, oMemberDetailsBean.getMale());
                proc.setString(2, oMemberDetailsBean.getFemale());
                proc.setString(3, oMemberDetailsBean.getOther());
                proc.setString(4, oMemberDetailsBean.getTotal());
                proc.setString(5, oMemberDetailsBean.getMuslim());
                proc.setString(6, oMemberDetailsBean.getChristian());
                proc.setString(7, oMemberDetailsBean.getBuddhist());
                proc.setString(8, oMemberDetailsBean.getSikh());
                proc.setString(9, oMemberDetailsBean.getParsi());
                proc.setString(10, oMemberDetailsBean.getJain());
                proc.setString(11, oMemberDetailsBean.getOthers());
                proc.setString(12, oMemberDetailsBean.getShgcode());
                proc.setString(13, ServletName);
                proc.registerOutParameter(14, java.sql.Types.VARCHAR);
                proc.setString(15, pacsId);

                proc.execute();

                //message = proc.getString(14);

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

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

            for (int i = 1; i <= counter; i++) {
                try {
                   // String shgcode = request.getParameter("shgcode");
                   // memberID = request.getParameter("memberIDAmend" + i);
                   // String membername = request.getParameter("membernameAmend" + i);
                   // String gender = request.getParameter("genderAmend" + i);
                   // String dob = request.getParameter("dobAmend" + i);
                   // String guardianname = request.getParameter("guardiannameAmend" + i);
                    //String address = request.getParameter("addressAmend" + i);
                    //String religion = request.getParameter("religionAmend" + i);
                    //String caste    = request.getParameter("casteAmend" + i);
                   // String qualification=request.getParameter("qualificationAmend"+i);
                    //String cif = request.getParameter("cifAmend"+i);
                    rowStatus = request.getParameter("rowStatus" + i);

                    if ((memberID != null) && (rowStatus != null)) {
                        oMemberDetailsBean = new MemberDetailsBean();
                        oMemberDetailsBean.setShgcode(shgcode);
                        oMemberDetailsBean.setMemberID(memberID);
                        oMemberDetailsBean.setMembername(membername);
                        oMemberDetailsBean.setGender(gender);
                        oMemberDetailsBean.setDob(dob);
                        oMemberDetailsBean.setGuardianname(guardianname);
                        oMemberDetailsBean.setAddress(address);
                        oMemberDetailsBean.setReligion(religion);
                        oMemberDetailsBean.setCaste(caste);
                        oMemberDetailsBean.setQualification(qualification);
                        oMemberDetailsBean.setCif(cif);
                        if (rowStatus.equals("N")) {
                            alMemberDetailsBeanNew.add(oMemberDetailsBean);
                        } else {
                            alMemberDetailsBeanUpdated.add(oMemberDetailsBean);
                        }
                    }
                } catch (Exception e) {
                    System.out.println("Error occurred during processing.");
                }
            }
            //For Deletion

           // deletedRows = request.getParameter("deletedRows");

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

                while (tokenizer.hasMoreTokens()) {

                    oMemberDetailsBean = new MemberDetailsBean();
                    oMemberDetailsBean.setMemberID(tokenizer.nextToken());
                   // oMemberDetailsBean.setShgcode(request.getParameter("shgcode"));

                    alMemberDetailsBeanDeleted.add(oMemberDetailsBean);

                }
            }
            if ((alMemberDetailsBeanUpdated.size() > 0) && (!message.equalsIgnoreCase(""))) {

                try {
                    con = DbHandler.getDBConnection();
                    try {
                        proc = con.prepareCall("{ call SHG_OPERATIONS.Upsert_member_information (?,?,?,?,?,?,?,?,?,?,?,?,?) }");
                    } catch (SQLException ex) {
                       // Logger.getLogger(MemberDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
                       System.out.println("Error occurred during processing.");
                    }
                    for (int j = 0; j < alMemberDetailsBeanUpdated.size(); j++) {

                        oMemberDetailsBean = new MemberDetailsBean();

                        oMemberDetailsBean = alMemberDetailsBeanUpdated.get(j);

                        proc.setString(2, oMemberDetailsBean.getShgcode());
                        proc.setString(1, oMemberDetailsBean.getMemberID());
                        proc.setString(3, oMemberDetailsBean.getMemberName());
                        proc.setString(4, oMemberDetailsBean.getGender());
                        proc.setString(5, oMemberDetailsBean.getDob());
                        proc.setString(6, oMemberDetailsBean.getGuardianName());
                        proc.setString(7, oMemberDetailsBean.getAddress());
                        proc.setString(8, oMemberDetailsBean.getReligion());
                        proc.setString(9, ServletName);
                        proc.setString(10, oMemberDetailsBean.getCaste());
                        proc.setString(11, oMemberDetailsBean.getQualification());
                        proc.setString(12, pacsId);
                        proc.setString(13, oMemberDetailsBean.getCif());
                        proc.addBatch();

                    }
                    proc.executeBatch();

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

            if ((alMemberDetailsBeanDeleted.size() > 0) && (!message.equalsIgnoreCase(""))) {

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

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

                       // oMemberDetailsBean = new MemberDetailsBean();
                        oMemberDetailsBean = alMemberDetailsBeanDeleted.get(j);

                        proc.setString(2, oMemberDetailsBean.getShgcode());
                        proc.setString(1, oMemberDetailsBean.getMemberID());
                        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, "Delete");
                        proc.setString(10, null);
                        proc.setString(11, null);
                        proc.setString(12, pacsId);
                        proc.setString(13, null);
                        proc.addBatch();

                    }
                    proc.executeBatch();
                   
                } catch (SQLException ex) {
                   // Logger.getLogger(MemberDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
                   // System.out.println(ex.toString());
                   System.out.println("Error occurred during processing.");
                } finally {
                    try {
                        proc.close();
                    } catch (SQLException e) {
                       // System.out.println(e.toString());
                       System.out.println("Error occurred during proc close.");
                    }
                    try {
                        con.close();
                    } catch (SQLException ex) {
                       // Logger.getLogger(MemberDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
                       // System.out.println(ex.toString());
                       System.out.println("Error occurred during connection close.");
                    }
                }
            }
            //For New Insertion
            if ((alMemberDetailsBeanNew.size() > 0) && (!message.equalsIgnoreCase(""))) {

                try {
                    con = DbHandler.getDBConnection();
                    try {
                        proc = con.prepareCall("{ call SHG_OPERATIONS.Upsert_member_information(?,?,?,?,?,?,?,?,?,?,?,?,?) }");
                    } catch (SQLException ex) {
                       // Logger.getLogger(MemberDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
                       System.out.println("Error occurred during processing.");
                    }
                    for (int j = 0; j < alMemberDetailsBeanNew.size(); j++) {

                        oMemberDetailsBean = new MemberDetailsBean();
                        oMemberDetailsBean = alMemberDetailsBeanNew.get(j);

                        proc.setString(2, oMemberDetailsBean.getShgcode());
                        proc.setString(1, oMemberDetailsBean.getMemberID());
                        proc.setString(3, oMemberDetailsBean.getMemberName());
                        proc.setString(4, oMemberDetailsBean.getGender());
                        proc.setString(5, oMemberDetailsBean.getDob());
                        proc.setString(6, oMemberDetailsBean.getGuardianName());
                        proc.setString(7, oMemberDetailsBean.getAddress());
                        proc.setString(8, oMemberDetailsBean.getReligion());
                        proc.setString(9, "Create");
                        proc.setString(10, oMemberDetailsBean.getCaste());
                        proc.setString(11, oMemberDetailsBean.getQualification());
                        proc.setString(12, pacsId);
                        proc.setString(13, oMemberDetailsBean.getCif());
                        proc.addBatch();

                    }
                    proc.executeBatch();

                } catch (SQLException ex) {
                   // Logger.getLogger(MemberDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
                   // System.out.println(ex.toString());
                   System.out.println("Error occurred during processing.");
                } finally {
                    try {
                        proc.close();
                    } catch (SQLException e) {
                       // System.out.println(e.toString());
                       System.out.println("Error occurred during proc close.");
                    }
                    try {
                        con.close();
                    } catch (SQLException ex) {
                       // Logger.getLogger(MemberDetailsServlet.class.getName()).log(Level.SEVERE, null, ex);
                       // System.out.println(ex.toString());
                       System.out.println("Error occurred during connection close.");
                    }
                }
            }
            request.setAttribute("message", message);
            request.getRequestDispatcher("/Shg_JSP/MemberDetails.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>
}
