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

import DataEntryBean.TransactioDetailsBean;
import LoginDb.DbHandler;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import oracle.jdbc.OracleTypes;
import org.codehaus.jackson.map.ObjectMapper;

/**
 *
 * @author 1004242
 */
public class BulkMarkDao {

      public String bulkMarkAccounts(String accNo, String pacsId) {
        Connection con = null;
        CallableStatement proc = null;
        Statement qry = null;
        String res = null;
        ResultSet rs = null;
        List<TransactioDetailsBean> tdBeanList = new ArrayList<TransactioDetailsBean>();
        TransactioDetailsBean tdBean = null;
        ObjectMapper objmap = new ObjectMapper();
        String jsonString = null;

        try {
            con = DbHandler.getDBConnection();
            qry = con.createStatement();
            rs = qry.executeQuery(" select * from ( select (select p.prod_name from dep_product p where p.id=d.dep_prod_id) as prod,d.key_1,"
                                  +" d.customer_no,(select r.first_name||' '||r.middle_name||' '||r.last_name from kyc_hdr r where r.cif_no=d.customer_no) as cust_name,"
                                  +" nvl((select r.guardian_name from kyc_hdr r where r.cif_no=d.customer_no),'NA') as guardian_name,(select r.bank_cif from kyc_hdr r where r.cif_no=d.customer_no) as bank_cif "
                                  +" from dep_account d where d.key_1 not in (select k.key_1 from bulk_acct_mark k where k.pacs_id='"+pacsId+"' ) and d.dep_prod_id in (select p.id from dep_product p where p.prod_code in ('1101','1102' )"
                                  +" and substr(p.int_cat,0,1)='1') and trim(d.link_accno) is null and  d.pacs_id='"+pacsId+"' and d.key_1 = '"+accNo+"' and d.curr_status='O') where bank_cif is null");

         while (rs.next()) {

                tdBean = new TransactioDetailsBean();
                tdBean.setAuthorisedQ(rs.getString("prod")==null?"NA":rs.getString("prod"));
                tdBean.setRejectedQ(rs.getString("key_1")==null?"NA":rs.getString("key_1"));
                tdBean.setPendingQ(rs.getString("cust_name")==null?"NA":rs.getString("cust_name"));
                tdBean.setCashIn(rs.getString("guardian_name")==null?"NA":rs.getString("guardian_name"));                
                tdBeanList.add(tdBean);
            }
           jsonString = objmap.writeValueAsString(tdBeanList);
        } catch (Exception e) {
           // e.printStackTrace();
           // System.out.println("Exception occured  in bulkMarkAccounts for pacs Id :" + pacsId);
           System.out.println("Error occurred during processing.");

        } finally {
            try {
                if (con != null) {
                   // con.commit();
                    con.close();
                }
            } catch (Exception e) {
               // e.printStackTrace();
               System.out.println("Error occurred during connection close.");
            }
        }

        return jsonString;
    }
      public String bulkMarkAccounts_Exs(String accNo, String pacsId) {
        Connection con = null;
        CallableStatement proc = null;
        Statement qry = null;
        String res = null;
        ResultSet rs = null;
        List<TransactioDetailsBean> tdBeanList = new ArrayList<TransactioDetailsBean>();
        TransactioDetailsBean tdBean = null;
        ObjectMapper objmap = new ObjectMapper();
        String jsonString = null;

        try {
            con = DbHandler.getDBConnection();
            qry = con.createStatement();
            rs = qry.executeQuery(" select * from ( select (select p.prod_name from dep_product p where p.id=d.dep_prod_id) as prod,d.key_1,"
                                  +" d.customer_no,(select r.first_name||' '||r.middle_name||' '||r.last_name from kyc_hdr r where r.cif_no=d.customer_no) as cust_name,"
                                  +" nvl((select r.guardian_name from kyc_hdr r where r.cif_no=d.customer_no),'NA') as guardian_name,(select r.bank_cif from kyc_hdr r where r.cif_no=d.customer_no) as bank_cif "
                                  +" from dep_account d where d.key_1 not in (select k.key_1 from bulk_acct_mark k where k.pacs_id='"+pacsId+"' ) and d.dep_prod_id in (select p.id from dep_product p where p.prod_code in ('1101','1102' )"
                                  +" and substr(p.int_cat,0,1)='1') and trim(d.link_accno) is null and  d.pacs_id='"+pacsId+"' and d.key_1 = '"+accNo+"' and d.curr_status='O') where bank_cif is not null");

         while (rs.next()) {

                tdBean = new TransactioDetailsBean();
                tdBean.setAuthorisedQ(rs.getString("prod")==null?"NA":rs.getString("prod"));
                tdBean.setRejectedQ(rs.getString("key_1")==null?"NA":rs.getString("key_1"));
                tdBean.setPendingQ(rs.getString("cust_name")==null?"NA":rs.getString("cust_name"));
                tdBean.setCashIn(rs.getString("guardian_name")==null?"NA":rs.getString("guardian_name"));
                tdBean.setBankCif(rs.getString("bank_cif")==null?"NA":rs.getString("bank_cif"));
                tdBeanList.add(tdBean);
            }
           jsonString = objmap.writeValueAsString(tdBeanList);
        } catch (Exception e) {
           // e.printStackTrace();
           // System.out.println("Exception occured  in bulkMarkAccounts for pacs Id :" + pacsId);
           System.out.println("Error occurred during processing.");

        } finally {
            try {
                if (con != null) {
                   // con.commit();
                    con.close();
                }
            } catch (Exception e) {
               // e.printStackTrace();
               System.out.println("Error occurred during connection close.");
            }
        }

        return jsonString;
    }

       public String markBulkAccount(String accno , String pacsId , String tellerId, String markType) {
        Connection con = null;
        CallableStatement proc = null;
        Statement qry = null;
        String res = null;
        ResultSet rs = null;
        String txnType;
        int count = 0;

        try {
            con = DbHandler.getDBConnection();


            proc = con.prepareCall("{ call operations1.BULK_ACCT_MARKING(?,?,?,?,?) }");

            proc.setString(1, pacsId);
            proc.setString(2, accno);
            proc.setString(3, tellerId);
            proc.registerOutParameter(4, OracleTypes.VARCHAR);
            proc.setString(5, markType);

            proc.executeUpdate();

           // res = proc.getString(4);//error message from dds procedure

        } catch (Exception e) {
           // e.printStackTrace();
           // System.out.println("Exception occured  in markBulkAccount for pacs Id :" + pacsId);
           res = "Exception occured  in markBulkAccount for pacs Id :" + pacsId;
           System.out.println("Error occurred during processing.");
        } finally {
            try {
                if (con != null) {
                   // con.commit();
                    con.close();
                }
            } catch (Exception e) {
               // e.printStackTrace();
               System.out.println("Error occurred during connection close.");
            }
        }

        return "{ \"result\": \" "+res+"\" }" ;
    }

       public String markBulkAccount_Exs(String accno , String pacsId , String tellerId, String markType) {
        Connection con = null;
        CallableStatement proc = null;
        Statement qry = null;
        String res = null;
        ResultSet rs = null;
        String txnType;
        int count = 0;

        try {
            con = DbHandler.getDBConnection();


            proc = con.prepareCall("{ call operations1.BULK_ACCT_MARKING(?,?,?,?,?) }");

            proc.setString(1, pacsId);
            proc.setString(2, accno);
            proc.setString(3, tellerId);
            proc.registerOutParameter(4, OracleTypes.VARCHAR);
            proc.setString(5, markType);

            proc.executeUpdate();

          //  res = proc.getString(4);//error message from dds procedure

        } catch (Exception e) {
           // e.printStackTrace();
           // System.out.println("Exception occured  in markBulkAccount for pacs Id :" + pacsId);
           res = "Exception occured  in markBulkAccount for pacs Id :" + pacsId;
           System.out.println("Error occurred during processing.");
        } finally {
            try {
                if (con != null) {
                   // con.commit();
                    con.close();
                }
            } catch (Exception e) {
               // e.printStackTrace();
               System.out.println("Error occurred during connection close.");
            }
        }

        return "{ \"result\": \" "+res+"\" }" ;
    }

        public String unmarkAccountFromAuth(String accNo, String pacsId, String tellerId) {
        Connection con = null;
        CallableStatement proc = null;
        Statement qry = null;
        String res = null;
        int rs = 0;
        List<TransactioDetailsBean> tdBeanList = new ArrayList<TransactioDetailsBean>();
        TransactioDetailsBean tdBean = null;
        ObjectMapper objmap = new ObjectMapper();
        String jsonString = null;

        try {
            con = DbHandler.getDBConnection();
            qry = con.createStatement();
            rs = qry.executeUpdate("insert into bulk_acct_mark_hist (pacs_id, key_1, mark_teller_id, tran_dt, tran_tym, status, remarks, send_teller_id, send_dt, send_tym) select pacs_id, key_1, mark_teller_id, tran_dt, tran_tym, status, 'UNMARKED', " +
                    " "+tellerId+ ", (select system_date from system_date), to_char(SYSTIMESTAMP, 'DD-MON-RRHH12:MI:SS') from bulk_acct_mark where KEY_1= '"+accNo+"' and PACS_ID = '"+pacsId+"'");
            rs = qry.executeUpdate("update bulk_acct_mark set status = 'RBA' where KEY_1= '"+accNo+"' and PACS_ID = '"+pacsId+"'");

          if(rs != 0)
          {
                 res="Account has been unmarked successfully";
          }else
          {
                res="Account unmarking unsuccessfull";
          }
        
        } catch (Exception e) {
           // e.printStackTrace();
           // System.out.println("Exception occured  in bulkMarkAccountsAuth for pacs Id :" + pacsId);
           res = "Exception occured  in bulkMarkAccountsAuth for pacs Id :" + pacsId;
           System.out.println("Error occurred during processing.");
        } finally {
            try {
                if (con != null) {
                   // con.commit();
                    con.close();
                }
            } catch (Exception e) {
               // e.printStackTrace();
               System.out.println("Error occurred during connection close.");
            }
        }

        return "{ \"result\": \" "+res+"\" }" ;

        
    }

        public String  bulkMarkAccountsAuth( String pacsId ) {
        Connection con = null;
        CallableStatement proc = null;
        Statement qry = null;
        String res = null;
        ResultSet rs = null;
        List<TransactioDetailsBean> tdBeanList = new ArrayList<TransactioDetailsBean>();
        TransactioDetailsBean tdBean = null;
        ObjectMapper objmap = new ObjectMapper();
        String jsonString = null;

        try {
            con = DbHandler.getDBConnection();
            qry = con.createStatement();
            rs = qry.executeQuery(" select (select p.prod_name from dep_product p where p.id=d.dep_prod_id) as prod,d.key_1,"
                                  +" d.customer_no,(select r.first_name||' '||r.middle_name||' '||r.last_name from kyc_hdr r where r.cif_no=d.customer_no) as cust_name,"
                                  +" nvl((select r.guardian_name from kyc_hdr r where r.cif_no=d.customer_no),'NA') as guardian_name,"
                                  +" (select (case k.id_type when '06' then 'VOTER CARD' when '47' then 'AADHAR CARD' when '08' then 'RATION CARD' when '09' then 'EMPLOYER ID CARD' when '04' then 'PASSPORT' "
                                  +" when '10' then 'DRIVING LICENCE' when '41' then 'INTRODUCED BY MEMBER' when '07' then 'PAN CARD' when '38' then 'BOARD RESOLUTION' end) from bulk_acct_mark k where k.key_1 = d.key_1 and k.pacs_id = '"+pacsId+"' "
                                  +" and k.status = 'A' and k.mark_type = 'N') id_type, (select k.id_number from bulk_acct_mark k where k.key_1 = d.key_1 and k.pacs_id = '"+pacsId+"' and k.status = 'A' and k.mark_type = 'N') id_number ,"

                                  +" (select (case k.id_type_two when '06' then 'VOTER CARD' when '47' then 'AADHAR CARD' when '08' then 'RATION CARD' when '09' then 'EMPLOYER ID CARD' when '04' then 'PASSPORT' "
                                  +" when '10' then 'DRIVING LICENCE' when '41' then 'INTRODUCED BY MEMBER' when '07' then 'PAN CARD' when '38' then 'BOARD RESOLUTION' end) from bulk_acct_mark k where k.key_1 = d.key_1 and k.pacs_id = '"+pacsId+"' "
                                  +" and k.status = 'A' and k.mark_type = 'N') id_type_two, (select k.id_number_two from bulk_acct_mark k where k.key_1 = d.key_1 and k.pacs_id = '"+pacsId+"' and k.status = 'A' and k.mark_type = 'N') id_number_two "

                                  +" from dep_account d where d.dep_prod_id in (select p.id from dep_product p where p.prod_code in ('1101','1102' )"
                                  +" and substr(p.int_cat,0,1)='1') and trim(d.link_accno) is null and  d.pacs_id='"+pacsId+"' and d.key_1 in ( select key_1 from bulk_acct_mark where pacs_id= '"+pacsId+"' and status = 'A' and mark_type= 'N') ");

         while (rs.next()) {

                tdBean = new TransactioDetailsBean();
                tdBean.setAuthorisedQ(rs.getString("prod")==null?"NA":rs.getString("prod"));
                tdBean.setRejectedQ(rs.getString("key_1")==null?"NA":rs.getString("key_1"));
                tdBean.setPendingQ(rs.getString("cust_name")==null?"NA":rs.getString("cust_name"));
                tdBean.setCashIn(rs.getString("guardian_name")==null?"NA":rs.getString("guardian_name"));
                tdBean.setCashOut(rs.getString("id_type")==null?"NA":rs.getString("id_type"));
                tdBean.setGuardianName(rs.getString("id_number")==null?"NA":rs.getString("id_number"));
                tdBean.setIdTypeTwo(rs.getString("id_type_two")==null?"NA":rs.getString("id_type_two"));
                tdBean.setIdNumberTwo(rs.getString("id_number_two")==null?"NA":rs.getString("id_number_two"));
                tdBeanList.add(tdBean);
            }
           jsonString = objmap.writeValueAsString(tdBeanList);
        } catch (Exception e) {
           // e.printStackTrace();
           // System.out.println("Exception occured  in bulkMarkAccounts for pacs Id :" + pacsId);
           System.out.println("Error occurred during processing.");

        } finally {
            try {
                if (con != null) {
                   // con.commit();
                    con.close();
                }
            } catch (Exception e) {
               // e.printStackTrace();
               System.out.println("Error occurred during connection close.");
            }
        }

        return jsonString;
    }

    public String  bulkMarkAccountsAuth_Exs( String pacsId ) {
        Connection con = null;
        CallableStatement proc = null;
        Statement qry = null;
        String res = null;
        ResultSet rs = null;
        List<TransactioDetailsBean> tdBeanList = new ArrayList<TransactioDetailsBean>();
        TransactioDetailsBean tdBean = null;
        ObjectMapper objmap = new ObjectMapper();
        String jsonString = null;

        try {
            con = DbHandler.getDBConnection();
            qry = con.createStatement();
            rs = qry.executeQuery(" select (select p.prod_name from dep_product p where p.id=d.dep_prod_id) as prod,d.key_1,"
                                  +" d.customer_no,(select r.first_name||' '||r.middle_name||' '||r.last_name from kyc_hdr r where r.cif_no=d.customer_no) as cust_name,"
                                  +" nvl((select r.guardian_name from kyc_hdr r where r.cif_no=d.customer_no),'NA') as guardian_name,"
                                  +" (select (case k.id_type when '06' then 'VOTER CARD' when '47' then 'AADHAR CARD' when '08' then 'RATION CARD' when '09' then 'EMPLOYER ID CARD' when '04' then 'PASSPORT' "
                                  +" when '10' then 'DRIVING LICENCE' when '41' then 'INTRODUCED BY MEMBER' when '07' then 'PAN CARD' when '38' then 'BOARD RESOLUTION' end) from bulk_acct_mark k where k.key_1 = d.key_1 and k.pacs_id = '"+pacsId+"' "
                                  +" and k.status = 'A' and k.mark_type = 'E') id_type, (select k.id_number from bulk_acct_mark k where k.key_1 = d.key_1 and k.pacs_id = '"+pacsId+"' and k.status = 'A' and k.mark_type = 'E') id_number, "
                                  +" (select r.bank_cif from kyc_hdr r where r.cif_no=d.customer_no) as bank_cif "
                                  +" from dep_account d where d.dep_prod_id in (select p.id from dep_product p where p.prod_code in ('1101','1102' )"
                                  +" and substr(p.int_cat,0,1)='1') and trim(d.link_accno) is null and  d.pacs_id='"+pacsId+"' and d.key_1 in ( select key_1 from bulk_acct_mark where pacs_id= '"+pacsId+"' and status = 'A' and mark_type= 'E') ");

         while (rs.next()) {

                tdBean = new TransactioDetailsBean();
                tdBean.setAuthorisedQ(rs.getString("prod")==null?"NA":rs.getString("prod"));
                tdBean.setRejectedQ(rs.getString("key_1")==null?"NA":rs.getString("key_1"));
                tdBean.setPendingQ(rs.getString("cust_name")==null?"NA":rs.getString("cust_name"));
                tdBean.setCashIn(rs.getString("guardian_name")==null?"NA":rs.getString("guardian_name"));
                tdBean.setCashOut(rs.getString("id_type")==null?"NA":rs.getString("id_type"));
                tdBean.setGuardianName(rs.getString("id_number")==null?"NA":rs.getString("id_number"));
                tdBean.setBankCif(rs.getString("bank_cif")==null?"NA":rs.getString("bank_cif"));
                tdBeanList.add(tdBean);
            }
           jsonString = objmap.writeValueAsString(tdBeanList);
        } catch (Exception e) {
           // e.printStackTrace();
           // System.out.println("Exception occured  in bulkMarkAccounts for pacs Id :" + pacsId);
           System.out.println("Error occurred during processing.");
           
        } finally {
            try {
                if (con != null) {
                   // con.commit();
                    con.close();
                }
            } catch (Exception e) {
               // e.printStackTrace();
               System.out.println("Error occurred during connection close.");
            }
        }

        return jsonString;
    }

     public String authoriseBulk(String pacsId , String tellerId, String mark) {
        Connection con = null;
        CallableStatement proc = null;
        Statement qry = null;
        String res = null;
        ResultSet rs = null;
        String txnType;
        int count = 0;

        try {
            con = DbHandler.getDBConnection();


            proc = con.prepareCall("{ call generate_bulk_acct_auto(?,?,?,?) }");


            proc.setString(1, pacsId);
            proc.setString(2, tellerId);
            proc.setString(4, mark);

            proc.registerOutParameter(3, OracleTypes.VARCHAR);

            proc.executeUpdate();

           // res = proc.getString(3);//error message from dds procedure

        } catch (Exception e) {
           // e.printStackTrace();
           // System.out.println("Exception occured  in authoriseBulk for pacs Id :" + pacsId);
           System.out.println("Error occurred during processing.");
            res = "Something went wrong. Please try agian.";
        } finally {
            try {
                if (con != null) {
                   // con.commit();
                    con.close();
                }
            } catch (Exception e) {
               // e.printStackTrace();
               System.out.println("Error occurred during connection close.");
            }
        }

        return res ;
    }

      public File  gerateBulkFlatFile1( String pacsId , String tellerId ) {
        Connection con = null;
        CallableStatement proc = null;
        Statement qry = null;
        String res = null;
        ResultSet rs = null;
        File bulkFile = null;
        boolean bool = false;

        BufferedWriter writer = null;

          try {
              con = DbHandler.getDBConnection();
              qry = con.createStatement();
              rs = qry.executeQuery("select "
                      + " rpad(nvl(trim(h.record_type),' '),2,' ')||' '||                    "
                      + " rpad(nvl(trim(h.branch_number),' '),5,' ')||' '||                  "
                      + " rpad(nvl(trim(h.account_type),' '),4,' ')||' '||                   "
                      + " rpad(nvl(trim(h.interest_category),' '),4,' ')||' '||	             "
                      + " rpad(nvl(trim(h.segment_code),' '),4,' ')||	' '||                "
                      + " rpad(nvl(trim(h.term_length),' '),4,' ')||' '||	                 "
                      + " rpad(nvl(trim(h.term_basis),' '),1,' ')||' '||                     "
                      + " rpad(nvl(trim(h.term_amount),' '),10,' ')||' '||	                 "
                      + " rpad(nvl(trim(h.account_open_date),' '),8,' ')||' '||	             "
                      + " rpad(nvl(trim(h.term_frequency),' '),4,' ')||' '||	             "
                      + " rpad(nvl(trim(h.Term_Days),' '),4,' ')||' '||	                     "
                      + " rpad(nvl(trim(h.term_months),' '),4,' ')||' '||	                 "
                      + " rpad(nvl(trim(h.term_years),' '),4,' ')||' '||	                 "
                      + " rpad(nvl(trim(h.installment_due_date),' '),2,' ')||' '||	         "
                      + " rpad(nvl(trim(h.installment_frequency),' '),1,' ')||' '||	         "
                      + " rpad(nvl(trim(h.language_code),' '),2,' ')||' '||	                 "
                      + " rpad(nvl(trim(h.teller_number),' '),7,' ')||' '||	                 "
                      + " rpad(nvl(trim(h.stop_required),' '),1,' ')||' '||	                 "
                      + " rpad(nvl(trim(h.data_a),' '),1,' ')||' '||	                     "
                      + " rpad(nvl(trim(h.interest_payment_method_trap),' '),1,' ')||' '||	 "
                      + " rpad(nvl(trim(h.backdate_account_opening_flag),' '),1,' ')||' '||	 "
                      + " rpad(nvl(trim(h.existing_cif),' '),16,' ')||' '||	                 "
                      + " rpad(nvl(trim(h.transfer_account_number),' '),16,' ')||' '||       "
                      + " lpad(nvl(trim(h.description_1),' '),50,' ')||' '||	             "
                      + " rpad(nvl(trim(h.sal_or_other),' '),1,' ')||' '||	                 "
                      + " rpad(nvl(trim(h.employer_name),' '),60,' ')||' '||	             "
                      + " rpad(nvl(trim(h.beneficiary_name),' '),118,' ')	                 "
                      + " from hh_format_auto h where h.pacs_id= "+pacsId+"                     ");

         while (rs.next()) {

            bulkFile = new File("BulkFlatFile" + System.currentTimeMillis() + ".txt");
            writer = new BufferedWriter(new FileWriter(bulkFile));

            String header = rs.getString(1);
            writer.write(header);

          
            }

              qry = null;
              rs = null;
              if (bulkFile != null) {
                  qry = con.createStatement();
                  rs = qry.executeQuery("select																	"
                          + "rpad(nvl(trim(t.record_type),' '),2,' ')||                            "
                          + "rpad(nvl(trim(t.student_number),' '),15,' ')||                        "
                          + "rpad(nvl(trim(t.first_name),' '),40,' ')||                            "
                          + "rpad(nvl(trim(t.middle_name),' '),40,' ')||                           "
                          + "rpad(nvl(trim(t.last_name),' '),40,' ')||                             "
                          + "rpad(nvl(trim(t.add_1),' '),40,' ')||                                 "
                          + "rpad(nvl(trim(t.add_2),' '),40,' ')||                                 "
                          + "rpad(nvl(trim(t.add_3),' '),24,' ')||                                 "
                          + "rpad(nvl(trim(t.city_code),' '),3,' ')||                              "
                          + "rpad(nvl(trim(t.state_code),' '),3,' ')||                             "
                          + "rpad(nvl(trim(t.po_number),' '),8,' ')||                              "
                          + "rpad(nvl(trim(t.phn_number),' '),12,' ')||                            "
                          + "rpad(nvl(trim(t.id_type),' '),2,' ')||                                "
                          + "rpad(nvl(trim(t.id_number),' '),14,' ')||                             "
                          + "rpad(nvl(trim(t.birth_date),' '),8,' ')||                             "
                          + "rpad(nvl(trim(t.gender),' '),1,' ')||                                 "
                          + "rpad(nvl(trim(t.mother_name),' '),25,' ')||                           "
                          + "rpad(nvl(trim(t.residential_status),' '),1,' ')||                     "
                          + "rpad(nvl(trim(t.nationality_code),' '),3,' ')||                       "
                          + "rpad(nvl(trim(t.customer_type),' '),8,' ')||                          "
                          + "rpad(nvl(trim(t.title),' '),2,' ')                                    "
                          + "from BULK_ACC_TRICKLEFEED_FILE_auto t where t.pacs_id="+pacsId+" and     "
                          + "t.status='R' and t.mark_type='N' and t.teller_id="+tellerId+" ");
//                          + "(select system_date from system_date)                                ");

         while (rs.next()) {

            writer.write("\r\n");
             //writer.newLine();
            String line1 = rs.getString(1);
            //line1 = line1.replaceAll("\\r|\\n|\\r|\\n", "");
            //String line = rs.getString(1).substring(0, rs.getString(1).length()-2);
            //line1 = line1.trim();
            writer.write(line1);

            }
            }

                  if(writer != null)
                  {
                       writer.flush();
                       writer.close();
                  }
              //updating sttaus of the generated bulks to S
              updateBulkAuthStatusToS(pacsId);

        } catch (Exception e) {
           // e.printStackTrace();
           // System.out.println("Exception occured  in bulkMarkAccounts for pacs Id :" + pacsId);
           System.out.println("Error occurred during processing.");
            bulkFile = null;

        } finally {
            try {
                if (con != null) {
                   // con.commit();
                    con.close();
                }
            } catch (Exception e) {
               // e.printStackTrace();
               System.out.println("Error occurred during connection close.");
            }
        }

        return bulkFile;
    }

    public File  gerateBulkFlatFile2( String pacsId , String tellerId ) {
        Connection con = null;
        CallableStatement proc = null;
        Statement qry = null;
        String res = null;
        ResultSet rs = null;
        File bulkFile = null;
        boolean bool = false;

        BufferedWriter writer = null;

          try {
              con = DbHandler.getDBConnection();
                  qry = con.createStatement();
                  rs = qry.executeQuery("select "
                      + " rpad(nvl(trim(h.record_type),' '),2,' ')||' '||                    "
                      + " rpad(nvl(trim(h.branch_number),' '),5,' ')||' '||                  "
                      + " rpad(nvl(trim(h.account_type),' '),4,' ')||' '||                   "
                      + " rpad(nvl(trim(h.interest_category),' '),4,' ')||' '||	             "
                      + " rpad(nvl(trim(h.segment_code),' '),4,' ')||	' '||                "
                      + " rpad(nvl(trim(h.term_length),' '),4,' ')||' '||	                 "
                      + " rpad(nvl(trim(h.term_basis),' '),1,' ')||' '||                     "
                      + " rpad(nvl(trim(h.term_amount),' '),10,' ')||' '||	                 "
                      + " rpad(nvl(trim(h.account_open_date),' '),8,' ')||' '||	             "
                      + " rpad(nvl(trim(h.term_frequency),' '),4,' ')||' '||	             "
                      + " rpad(nvl(trim(h.Term_Days),' '),4,' ')||' '||	                     "
                      + " rpad(nvl(trim(h.term_months),' '),4,' ')||' '||	                 "
                      + " rpad(nvl(trim(h.term_years),' '),4,' ')||' '||	                 "
                      + " rpad(nvl(trim(h.installment_due_date),' '),2,' ')||' '||	         "
                      + " rpad(nvl(trim(h.installment_frequency),' '),1,' ')||' '||	         "
                      + " rpad(nvl(trim(h.language_code),' '),2,' ')||' '||	                 "
                      + " rpad(nvl(trim(h.teller_number),' '),7,' ')||' '||	                 "
                      + " rpad(nvl(trim(h.stop_required),' '),1,' ')||' '||	                 "
                      + " rpad(nvl(trim(h.data_a),' '),1,' ')||' '||	                     "
                      + " rpad(nvl(trim(h.interest_payment_method_trap),' '),1,' ')||' '||	 "
                      + " rpad(nvl(trim(h.backdate_account_opening_flag),' '),1,' ')||' '||	 "
                      + " lpad(nvl(substr(t.bank_cif, 1, length(t.bank_cif) - 1), ' '),16,'0') || ' ' || "
                      //+ " rpad(nvl(trim(h.existing_cif),' '),16,' ')||' '||	                 "
                      + " rpad(nvl(trim(h.transfer_account_number),' '),16,' ')||' '||       "
                      + " rpad(nvl(trim('BULK ACCOUNT'), ' '), 50, ' ') || ' ' || "
                      //+ " lpad(nvl(trim(h.description_1),' '),50,' ')||' '||	             "
                      + " rpad(nvl(trim(h.sal_or_other),' '),1,' ')||' '||	                 "
                      + " rpad(nvl(trim(h.employer_name),' '),60,' ')||' '||	             "
                      + " rpad(nvl(trim(h.beneficiary_name),' '),118,' ')	                 "
                      + " from hh_format_auto h, BULK_ACC_TRICKLEFEED_FILE_auto t where h.pacs_id = t.pacs_id and "
                      + " h.pacs_id= "+pacsId+" and t.pacs_id="+pacsId+" and t.status='R' and t.mark_type='E' and t.teller_id="+tellerId+" ");

                  bulkFile = new File("BulkFlatFile" + System.currentTimeMillis() + ".txt");
                  writer = new BufferedWriter(new FileWriter(bulkFile));
         while (rs.next()) {
            
             //writer.newLine();
            String line1 = rs.getString(1);
            //line1 = line1.replaceAll("\\r|\\n|\\r|\\n", "");
            //String line = rs.getString(1).substring(0, rs.getString(1).length()-2);
            //line1 = line1.trim();
            writer.write(line1);
            writer.write("\r\n");
            writer.write("DD000000000000000                                                                                                                                                                                                                                5320000000000000000000000000              00000000                                        ");            
            writer.write("\r\n");
         }

                  if(writer != null)
                  {
                       writer.flush();
                       writer.close();
                  }
              //updating sttaus of the generated bulks to S
              updateBulkAuthStatusToS_Exs(pacsId);

        } catch (Exception e) {
           // e.printStackTrace();
           // System.out.println("Exception occured  in bulkMarkAccounts for pacs Id :" + pacsId);
           System.out.println("Error occurred during processing.");
            bulkFile = null;

        } finally {
            try {
                if (con != null) {
                   // con.commit();
                    con.close();
                }
            } catch (Exception e) {
               // e.printStackTrace();
               System.out.println("Error occurred during connection close.");
            }
        }

        return bulkFile;
    }


     public void updateBulkAuthStatusToS(String pacsId) {
        Connection con = null;
        Statement qry = null;
        ResultSet rs = null;
        int count = 0;

        try {
            con = DbHandler.getDBConnection();
            qry = con.createStatement();
            count = qry.executeUpdate(" update bulk_acct_mark k set k.status='S' where k.pacs_id= "+pacsId +" and k.status='R' and k.mark_type='N' "
                                     +" and k.key_1 in ( select trim(o.add_1) from BULK_ACC_TRICKLEFEED_FILE_auto o where o.status='R' and o.mark_type='N' )");
            if (count > 0) {
                count=0;
                count = qry.executeUpdate(" update BULK_ACC_TRICKLEFEED_FILE_auto o set o.status='S' where o.pacs_id="+pacsId+" and o.status='R' and o.mark_type='N' ");
            }

            if (count != 0) {
                System.out.println("Bulk auth status updated to S :" + pacsId);
            } else {
                System.out.println("Bulk auth status updatation failed :" + pacsId);
            }

        } catch (Exception e) {
           // e.printStackTrace();
           // System.out.println("Exception occured  in updateBulkAuthStatusToS for pacs Id :" + pacsId);
           System.out.println("Error occurred during processing.");

        } finally {
            try {
                if (con != null) {
                   // con.commit();
                    con.close();
                }
            } catch (Exception e) {
               // e.printStackTrace();
               System.out.println("Error occurred during connection close.");
            }
        }
    }

     public void updateBulkAuthStatusToS_Exs(String pacsId) {
        Connection con = null;
        Statement qry = null;
        ResultSet rs = null;
        int count = 0;

        try {
            con = DbHandler.getDBConnection();
            qry = con.createStatement();
            count = qry.executeUpdate(" update bulk_acct_mark k set k.status='S' where k.pacs_id= "+pacsId +" and k.status='R' and k.mark_type='E' "
                                     +" and k.key_1 in ( select trim(o.add_1) from BULK_ACC_TRICKLEFEED_FILE_auto o where o.status='R' and o.mark_type='E' )");
            if (count > 0) {
                count=0;
                count = qry.executeUpdate(" update BULK_ACC_TRICKLEFEED_FILE_auto o set o.status='S' where o.pacs_id="+pacsId+" and o.status='R' and o.mark_type='E' ");
            }

            if (count != 0) {
                System.out.println("Bulk auth status updated to S :" + pacsId);
            } else {
                System.out.println("Bulk auth status updatation failed :" + pacsId);
            }

        } catch (Exception e) {
           // e.printStackTrace();
           // System.out.println("Exception occured  in updateBulkAuthStatusToS_Exs for pacs Id :" + pacsId);
           System.out.println("Error occurred during processing.");

        } finally {
            try {
                if (con != null) {
                   // con.commit();
                    con.close();
                }
            } catch (Exception e) {
               // e.printStackTrace();
               System.out.println("Error occurred during connection close.");
            }
        }
    }
}

