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

import DataEntryBean.memberEnrollmentHeaderBean;
import DataEntryBean.memberEnrollmentDetailsBean;
import java.util.StringTokenizer;
//import LoginDb.DbHandler;
import LoginDb.DbHandler;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.beanutils.BeanUtils;
import java.util.ArrayList;

/**
 *
 * @author 1249241
 */
public class MemberEnrollmentServlet 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 {
        //processRequest(request, response);
        HttpSession session = request.getSession(false);
        String pacsId = (String) session.getAttribute("pacsId");
        String user = (String) session.getAttribute("user");
        Connection con = null;
        CallableStatement proc = null, proc1 = null;
        String message = "";
        int counter;
        String headerId = "";

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

        memberEnrollmentHeaderBean omemberEnrollmentHeaderBean = new memberEnrollmentHeaderBean();
        memberEnrollmentDetailsBean omemberEnrollmentDetailsBean = new memberEnrollmentDetailsBean();
        omemberEnrollmentDetailsBean = null;
        ArrayList<memberEnrollmentDetailsBean> alMemberEnrollmentDetailsBean = new ArrayList<memberEnrollmentDetailsBean>();

        //for amend
        ArrayList<memberEnrollmentDetailsBean> almemberEnrollmentDetailsBeanNew = new ArrayList<memberEnrollmentDetailsBean>();
        ArrayList<memberEnrollmentDetailsBean> almemberEnrollmentDetailsBeanDeleted = new ArrayList<memberEnrollmentDetailsBean>();
        ArrayList<memberEnrollmentDetailsBean> almemberEnrollmentDetailsBeanUpdated = new ArrayList<memberEnrollmentDetailsBean>();
        ArrayList<memberEnrollmentDetailsBean> almemberEnrollmentDetailsBeanDetailNew = new ArrayList<memberEnrollmentDetailsBean>();

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

            try {
              //  BeanUtils.populate(omemberEnrollmentHeaderBean, request.getParameterMap());

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

            try {
                con = DbHandler.getDBConnection();
                try {
                    proc = con.prepareCall("{ call PDS_OPERATIONS.Upsert_CARD_HOLDER_DTLS(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) }");//change as required

                } catch (SQLException ex) {
                   // Logger.getLogger(MemberEnrollmentServlet.class.getName()).log(Level.SEVERE, null, ex);
                   System.out.println("Error occurred during processing.");
                }
                proc.setString(1, omemberEnrollmentHeaderBean.getHolderId());
                proc.setString(2, omemberEnrollmentHeaderBean.getCardNo());
                proc.setString(3, omemberEnrollmentHeaderBean.getCustName());
                proc.setString(4, omemberEnrollmentHeaderBean.getSDWOf());
                proc.setString(5, omemberEnrollmentHeaderBean.getHouseNo());
                proc.setString(6, omemberEnrollmentHeaderBean.getStreet());
                proc.setString(7, omemberEnrollmentHeaderBean.getGramPanchayat());
                proc.setString(8, omemberEnrollmentHeaderBean.getRevenueBlock());
                proc.setString(9, omemberEnrollmentHeaderBean.getSubdivision());
                proc.setString(10, omemberEnrollmentHeaderBean.getDistrict());
                proc.setString(11, omemberEnrollmentHeaderBean.getState());
                proc.setString(12, omemberEnrollmentHeaderBean.getIssuedOn());
                proc.setString(13, omemberEnrollmentHeaderBean.getValidUpto());
                proc.setString(14, omemberEnrollmentHeaderBean.getDateOfBirth());
                proc.setString(15, omemberEnrollmentHeaderBean.getCardTypeId());
                proc.setString(16, pacsId);
                proc.setString(17, ServletName);
                proc.registerOutParameter(18, java.sql.Types.VARCHAR);
                proc.registerOutParameter(19, java.sql.Types.VARCHAR);

                proc.execute();
               // message = proc.getString(18);
               // headerId = proc.getString(19);

                

            } catch (SQLException ex) {
                try {
                   // con.rollback();
                } catch (Exception ex1) {
                   // Logger.getLogger(MemberEnrollmentServlet.class.getName()).log(Level.SEVERE, null, ex1);
                   System.out.println("Error occurred during processing.");
                }

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

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

            for (int i = 1; i <= counter; i++) {
                try {

                    //String familyId = request.getParameter("familyId" + i);
                   // String cardHolderNameFamily = request.getParameter("cardHolderNameFamily" + i);
                    //String cardNoFamily = request.getParameter("cardNoFamily" + i);
                    //String DOBFamily = request.getParameter("DOBFamily" + i);


                    if (cardHolderNameFamily != null) {
                        omemberEnrollmentDetailsBean = new memberEnrollmentDetailsBean();
                        omemberEnrollmentDetailsBean.setFamilyId(familyId);
                        omemberEnrollmentDetailsBean.setCardHolderNameFamily(cardHolderNameFamily);
                        omemberEnrollmentDetailsBean.setCardNoFamily(cardNoFamily);
                        omemberEnrollmentDetailsBean.setDOBFamily(DOBFamily);
                        alMemberEnrollmentDetailsBean.add(omemberEnrollmentDetailsBean);
                    }
                } catch (Exception e) {
                    System.out.println("Error occurred during processing.");
                }
            }
            if ((alMemberEnrollmentDetailsBean.size() > 0) && (!message.equalsIgnoreCase("")) && (!headerId.equalsIgnoreCase(""))) {

                try {
                    con = DbHandler.getDBConnection();
                    try {
                        proc = con.prepareCall("{ call pds_operations.Upsert_pds_family_dtls(?,?,?,?,?,?) }");

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


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

                        omemberEnrollmentDetailsBean = alMemberEnrollmentDetailsBean.get(j);


                        proc.setString(1, omemberEnrollmentDetailsBean.getFamilyId());
                        proc.setString(2, headerId);
                        proc.setString(3, omemberEnrollmentDetailsBean.getCardHolderNameFamily());
                        proc.setString(4, omemberEnrollmentDetailsBean.getDOBFamily());
                        proc.setString(5, omemberEnrollmentDetailsBean.getCardNoFamily());
                        proc.setString(6, ServletName);

                        //proc.registerOutParameter(7, java.sql.Types.VARCHAR);

                        proc.addBatch();


                    }

                    proc.executeBatch();
                    message = message + headerId;

                } catch (SQLException ex) {
                    try {
                       // con.rollback();
                    } catch (Exception ex1) {
                       // Logger.getLogger(MemberEnrollmentServlet.class.getName()).log(Level.SEVERE, null, ex1);
                       System.out.println("Error occurred during processing.");
                    }
                   // Logger.getLogger(MemberEnrollmentServlet.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(MemberEnrollmentServlet.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("/Pds_JSP/memberEnrollment.jsp").forward(request, response);

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

            try {
              //  BeanUtils.populate(omemberEnrollmentHeaderBean, request.getParameterMap());

            } catch (IllegalAccessException ex) {
               // Logger.getLogger(MemberEnrollmentServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error occurred during processing.");
            } catch (InvocationTargetException ex) {
               // Logger.getLogger(MemberEnrollmentServlet.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(MemberEnrollmentServlet.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error occurred during processing.");
            }
            try {

               // ResultSet rs = statement.executeQuery("select p.ID,p.CARD_NO,p.NAME,p.GUARDIAN_NAME,p.HOUSE_NO,p.STREET_ROAD,p.PANCHAYAT,p.BLOCK,p.SUB_DIV,p.DISTRICT,p.STATE,to_char(p.ISSUED_ON,'dd/mm/yyyy') ISSUED_ON,to_char(p.VALID_UPTO,'dd/mm/yyyy') VALID_UPTO,to_char(p.DOB,'dd/mm/yyyy') DOB,c.CARD_TYPE,c.card_desc,c.card_type_id,p.PACS_ID from PDS_CARD_HOLDERS p, PDS_CARD_TYPE_MST c where p.CARD_NO= '" + omemberEnrollmentHeaderBean.getCardNoAmendSearch() + "' and c.card_type_id=p.card_type_id");

                while (rs.next()) {


                    headerId = rs.getString("ID");
                    omemberEnrollmentHeaderBean.setHolderId(headerId);
                    omemberEnrollmentHeaderBean.setCardNo(rs.getString("CARD_NO"));
                    omemberEnrollmentHeaderBean.setCustName(rs.getString("NAME"));
                    omemberEnrollmentHeaderBean.setSDWOf(rs.getString("GUARDIAN_NAME"));
                    omemberEnrollmentHeaderBean.setDateOfBirth(rs.getString("DOB"));
                    omemberEnrollmentHeaderBean.setHouseNo(rs.getString("HOUSE_NO"));
                    omemberEnrollmentHeaderBean.setStreet(rs.getString("STREET_ROAD"));
                    omemberEnrollmentHeaderBean.setGramPanchayat(rs.getString("PANCHAYAT"));
                    omemberEnrollmentHeaderBean.setRevenueBlock(rs.getString("BLOCK"));
                    omemberEnrollmentHeaderBean.setSubdivision(rs.getString("SUB_DIV"));
                    omemberEnrollmentHeaderBean.setDistrict(rs.getString("DISTRICT"));
                    omemberEnrollmentHeaderBean.setState(rs.getString("STATE"));
                    omemberEnrollmentHeaderBean.setIssuedOn(rs.getString("ISSUED_ON"));
                    omemberEnrollmentHeaderBean.setValidUpto(rs.getString("VALID_UPTO"));
                    String card = rs.getString("CARD_TYPE") + ":" + rs.getString("CARD_DESC");
                    omemberEnrollmentHeaderBean.setCardCategory(card);
                    omemberEnrollmentHeaderBean.setCardTypeId(rs.getString("CARD_TYPE_ID"));
                    SeachFound = 1;
                    request.setAttribute("displayFlag", "Y");

                }

                statement.close();



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

            if (SeachFound == 0) {
                message = "Member does not exist in the system";
                request.setAttribute("message", message);
            }


            //For Details Part Populate

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

               // ResultSet rsDetails = statement.executeQuery("select ID,MEMBER_NAME,CARD_NO,to_char(DOB,'dd/mm/yyyy') DOB from PDS_FAMILY_DTLS where HOLDER_ID= '" + headerId + "'");


                while (rsDetails.next()) {

                    omemberEnrollmentDetailsBean = new memberEnrollmentDetailsBean();
                    omemberEnrollmentDetailsBean.setDetailIdAmend(rsDetails.getString("ID"));
                    omemberEnrollmentDetailsBean.setCardNoFamily(rsDetails.getString("CARD_NO"));
                    omemberEnrollmentDetailsBean.setCardHolderNameFamily(rsDetails.getString("MEMBER_NAME"));
                    omemberEnrollmentDetailsBean.setDOBFamily(rsDetails.getString("DOB"));

                    alMemberEnrollmentDetailsBean.add(omemberEnrollmentDetailsBean);


                }

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

            } catch (SQLException ex) {
               // Logger.getLogger(VendorMasterServlet.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("omemberEnrollmentHeaderBean", omemberEnrollmentHeaderBean);
            request.setAttribute("alMemberEnrollmentDetailsBeanApend", alMemberEnrollmentDetailsBean);
            request.getRequestDispatcher("/Pds_JSP/memberEnrollment.jsp").forward(request, response);

        } else if ("Update".equalsIgnoreCase(ServletName)) {
            String familyIdAmend = null;
            String cardNoFamilyAmend = null;
            String cardHolderNameFamilyAmend = null;
            String DOBFamilyAmend = null;
            String familyIdAmend3 = null;

            String rowStatus = null;
            String detailID = null;
            String deletedRows = null;

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

            try {
                con = DbHandler.getDBConnection();
                try {


                    proc = con.prepareCall("{ call PDS_OPERATIONS.Upsert_CARD_HOLDER_DTLS(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) }");//change as required
                } catch (SQLException ex) {
                   // Logger.getLogger(MemberEnrollmentServlet.class.getName()).log(Level.SEVERE, null, ex);
                   System.out.println("Error occurred during processing.");
                }


                proc.setString(1, omemberEnrollmentHeaderBean.getHolderIdAmend());
                proc.setString(2, omemberEnrollmentHeaderBean.getCardNoAmend());
                proc.setString(3, omemberEnrollmentHeaderBean.getCustNameAmend());
                proc.setString(4, omemberEnrollmentHeaderBean.getSDWOfAmend());
                proc.setString(5, omemberEnrollmentHeaderBean.getHouseNoAmend());
                proc.setString(6, omemberEnrollmentHeaderBean.getStreetAmend());
                proc.setString(7, omemberEnrollmentHeaderBean.getGramPanchayatAmend());
                proc.setString(8, omemberEnrollmentHeaderBean.getRevenueBlockAmend());
                proc.setString(9, omemberEnrollmentHeaderBean.getSubdivisionAmend());
                proc.setString(10, omemberEnrollmentHeaderBean.getDistrictAmend());
                proc.setString(11, omemberEnrollmentHeaderBean.getStateAmend());
                proc.setString(12, omemberEnrollmentHeaderBean.getIssuedOnAmend());
                proc.setString(13, omemberEnrollmentHeaderBean.getValidUptoAmend());
                proc.setString(14, omemberEnrollmentHeaderBean.getDateOfBirthAmend());
                proc.setString(15, omemberEnrollmentHeaderBean.getCardTypeIdAmend());
                proc.setString(16, pacsId);

                proc.setString(17, ServletName);

                proc.registerOutParameter(18, java.sql.Types.VARCHAR);
                proc.registerOutParameter(19, java.sql.Types.VARCHAR);

                proc.execute();
               // message = proc.getString(18);
                headerId = proc.getString(19);




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


//New And UPdate After Populating Family Details

            counter = Integer.parseInt(request.getParameter("rowCounterAmend"));

            for (int i = 1; i <= counter; i++) {
                try {

                   // familyIdAmend = request.getParameter("familyIdAmend" + i);
                    //cardNoFamilyAmend = request.getParameter("cardNoFamilyAmend" + i);
                   // cardHolderNameFamilyAmend = request.getParameter("cardHolderNameFamilyAmend" + i);
                   // DOBFamilyAmend = request.getParameter("DOBFamilyAmend" + i);
                    rowStatus = request.getParameter("rowStatus" + i);
                    detailID = request.getParameter("detailIDAmend" + i);



                    if (rowStatus != null) {
                        omemberEnrollmentDetailsBean = new memberEnrollmentDetailsBean();
                        omemberEnrollmentDetailsBean.setFamilyIdAmend(familyIdAmend);
                        omemberEnrollmentDetailsBean.setCardNoFamilyAmend(cardNoFamilyAmend);
                        omemberEnrollmentDetailsBean.setCardHolderNameFamilyAmend(cardHolderNameFamilyAmend);
                        omemberEnrollmentDetailsBean.setDOBFamilyAmend(DOBFamilyAmend);

                        if (rowStatus.equals("N")) {
                            almemberEnrollmentDetailsBeanDetailNew.add(omemberEnrollmentDetailsBean);

                        } else {
                            almemberEnrollmentDetailsBeanUpdated.add(omemberEnrollmentDetailsBean);
                        }


                    }
                } catch (Exception e) {
                    System.out.println("Error occurred during processing.");
                }
            }









//For New Insertion if there is no family earlier

            counter = Integer.parseInt(request.getParameter("rowCounterAmend3"));

            for (int i = 1; i <= counter; i++) {
                try {

                   // familyIdAmend = request.getParameter("familyIdAmend3" + i);
                  //  cardNoFamilyAmend = request.getParameter("cardNoFamilyAmend3" + i);
                   // cardHolderNameFamilyAmend = request.getParameter("cardHolderNameFamilyAmend3" + i);
                   // DOBFamilyAmend = request.getParameter("DOBFamilyAmend3" + i);
                    rowStatus = request.getParameter("rowStatus2" + i);



                    if (rowStatus != null) {
                        omemberEnrollmentDetailsBean = new memberEnrollmentDetailsBean();
                        omemberEnrollmentDetailsBean.setFamilyIdAmend3(familyIdAmend);
                        omemberEnrollmentDetailsBean.setCardNoFamilyAmend3(cardNoFamilyAmend);
                        omemberEnrollmentDetailsBean.setCardHolderNameFamilyAmend3(cardHolderNameFamilyAmend);
                        omemberEnrollmentDetailsBean.setDOBFamilyAmend3(DOBFamilyAmend);

                        if (rowStatus.equals("N")) {
                            almemberEnrollmentDetailsBeanNew.add(omemberEnrollmentDetailsBean);
                        }


                    }
                } catch (Exception e) {
                    System.out.println("Error occurred during processing.");
                }
            }



            //For Deletion

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

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

                while (tokenizer.hasMoreTokens()) {

                    omemberEnrollmentDetailsBean = new memberEnrollmentDetailsBean();
                    omemberEnrollmentDetailsBean.setDetailIdAmend(tokenizer.nextToken());

                    almemberEnrollmentDetailsBeanDeleted.add(omemberEnrollmentDetailsBean);

                }
            }




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

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


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

                        omemberEnrollmentDetailsBean = new memberEnrollmentDetailsBean();

                        omemberEnrollmentDetailsBean = almemberEnrollmentDetailsBeanUpdated.get(j);


                        proc.setString(1, omemberEnrollmentDetailsBean.getFamilyIdAmend());
                        proc.setString(2, headerId);
                        proc.setString(3, omemberEnrollmentDetailsBean.getCardHolderNameFamilyAmend());
                        proc.setString(4, omemberEnrollmentDetailsBean.getDOBFamilyAmend());
                        proc.setString(5, omemberEnrollmentDetailsBean.getCardNoFamilyAmend());
                        proc.setString(6, ServletName);

                        proc.addBatch();


                    }

                    proc.executeBatch();

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

            //for deletion

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

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


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

                        omemberEnrollmentDetailsBean = new memberEnrollmentDetailsBean();

                        omemberEnrollmentDetailsBean = almemberEnrollmentDetailsBeanDeleted.get(j);



                        proc.setString(1, omemberEnrollmentDetailsBean.getDetailIdAmend());
                        proc.setString(2, null);
                        proc.setString(3, null);
                        proc.setString(4, null);
                        proc.setString(5, null);

                        proc.setString(6, "Delete");
                        //  proc.registerOutParameter(7, java.sql.Types.VARCHAR);


                        proc.addBatch();


                    }

                    proc.executeBatch();

                } catch (SQLException ex) {
                    try {
                       // con.rollback();
                    } catch (Exception ex1) {
                       // Logger.getLogger(MemberEnrollmentServlet.class.getName()).log(Level.SEVERE, null, ex1);
                       System.out.println("Error occurred during processing.");
                    }

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


            //For New Insertion If no family exists earlier


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

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


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

                        omemberEnrollmentDetailsBean = new memberEnrollmentDetailsBean();
                        omemberEnrollmentDetailsBean = almemberEnrollmentDetailsBeanNew.get(j);

                        proc.setString(1, familyIdAmend3);
                        proc.setString(2, headerId);
                        proc.setString(3, omemberEnrollmentDetailsBean.getCardHolderNameFamilyAmend3());
                        proc.setString(4, omemberEnrollmentDetailsBean.getDOBFamilyAmend3());
                        proc.setString(5, omemberEnrollmentDetailsBean.getCardNoFamilyAmend3());

                        proc.setString(6, "Create");

                        proc.addBatch();


                    }

                    proc.executeBatch();

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


//For New Insertion if Family Details Exist earlier

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

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


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

                        omemberEnrollmentDetailsBean = new memberEnrollmentDetailsBean();
                        omemberEnrollmentDetailsBean = almemberEnrollmentDetailsBeanDetailNew.get(j);

                        proc.setString(1, familyIdAmend);
                        proc.setString(2, headerId);
                        proc.setString(3, omemberEnrollmentDetailsBean.getCardHolderNameFamilyAmend());
                        proc.setString(4, omemberEnrollmentDetailsBean.getDOBFamilyAmend());
                        proc.setString(5, omemberEnrollmentDetailsBean.getCardNoFamilyAmend());

                        proc.setString(6, "Create");

                        proc.addBatch();


                    }

                    proc.executeBatch();

                } catch (SQLException ex) {
                    try {
                       // con.rollback();
                    } catch (Exception ex1) {
                       // Logger.getLogger(MemberEnrollmentServlet.class.getName()).log(Level.SEVERE, null, ex1);
                       System.out.println("Error occurred during processing.");
                    }
                   // Logger.getLogger(MemberEnrollmentServlet.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(MemberEnrollmentServlet.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("/Pds_JSP/memberEnrollment.jsp").forward(request, response);
        }

    }

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