package com.methods; import com.bean.DataBean; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.sql.CallableStatement; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.Iterator; import org.json.JSONArray; import org.json.JSONObject; public class RequestResponse { static Connection connection = null; static ResultSet rs = null; static Statement statement = null; public static ArrayList get_response(String jsonInputString, String date) throws IOException { // String urlFromDatabase = "https://IPKS.cedgeapiservices.in/IPKSTRAN"; String urlFromDatabase = ""; String bc = new JSONObject(jsonInputString).getString("bankcode"); if(bc.equals("0005") || bc.equals("0021")) { urlFromDatabase = "https://NABARDIPKS.cedgeapiservices.in/IPKSTRAN"; } else { urlFromDatabase = "https://UCBIPKS.cedgeapiservices.in/IPKSTRAN"; } System.out.println("Req MSG : " + jsonInputString); ArrayList data = new ArrayList<>(); URL url = null; try { url = new URL(urlFromDatabase); } catch (MalformedURLException e1) { e1.printStackTrace(); } HttpURLConnection con = null; try { con = (HttpURLConnection)url.openConnection(); con.setRequestMethod("POST"); con.setRequestProperty("Content-Type", "application/json; utf-8"); con.setRequestProperty("Accept", "application/json"); con.setDoOutput(true); } catch (IOException e2) { e2.printStackTrace(); } try (OutputStream os = con.getOutputStream()) { byte[] input = jsonInputString.getBytes("utf-8"); os.write(input, 0, input.length); } int responseCode = 0; try { responseCode = con.getResponseCode(); } catch (IOException e1) { e1.printStackTrace(); } System.out.println("Response Code : " + responseCode); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); StringBuffer response = new StringBuffer(); response.append("{\"result\":"); try { String inputLine; while ((inputLine = in.readLine()) != null) response.append(inputLine); } catch (IOException e1) { e1.printStackTrace(); } try { in.close(); } catch (IOException e1) { e1.printStackTrace(); } try { response.append("}"); if (response.toString().contains("No data found")) { System.out.println("No Transactions found"); } else { JSONObject json = new JSONObject(response.toString()); JSONArray dataArray = new JSONArray(json.get("result").toString()); for (int i = 0; i < dataArray.length(); i++) { String JSonData = dataArray.get(i).toString(); DataBean databean = new DataBean(); JSONObject dataD = new JSONObject(JSonData); databean.setAcctNo(dataD.get("acctno").toString()); databean.setBankcode(dataD.get("bankcode").toString()); databean.setTxnCode(dataD.get("trancode").toString()); databean.setTxnAmt(dataD.get("trnamt").toString()); databean.setNarration(dataD.get("narration").toString()); databean.setJrnlId(dataD.get("jrnl").toString()); databean.setTimestamp(date); if (Double.parseDouble(dataD.get("trnamt").toString()) > 0.0D) { databean.setTxnInd("CR"); } else if (Double.parseDouble(dataD.get("trnamt").toString()) < 0.0D) { databean.setTxnInd("DR"); } else { databean.setTxnInd("NA"); } data.add(databean); } setDatainDatabase(data, date); } } catch (Exception e3) { e3.printStackTrace(); } return data; } public static ArrayList getUrl() { Date date = new Date(); DateFormat sdf = new SimpleDateFormat("ddMMyyyy"); Calendar cal = Calendar.getInstance(); String dateToday = sdf.format(cal.getTime()); System.out.println("Date :" + dateToday); String url = null; String bankCode = null; ArrayList finalUrl = new ArrayList<>(); try { connection = Util.getDBConnection(); statement = connection.createStatement(); rs = statement.executeQuery("select * from rupay_kcc_api_map where status='Y' and API_TYPE='TXN'"); while (rs.next()) { url = rs.getString(7); bankCode = rs.getString(6); String finUrl = url.replaceAll("dt", dateToday); finUrl = finUrl.replaceAll("bcode", bankCode); finalUrl.add(finUrl); } } catch (Exception ex) { ex.printStackTrace(); } finally { try { if (connection != null) connection.close(); } catch (SQLException e1) { e1.printStackTrace(); } } return finalUrl; } public static void setDatainDatabase(ArrayList data, String date) { Connection connection = null; ResultSet rs = null; Statement st = null; String query = null; PreparedStatement statement = null; ArrayList combination = new ArrayList<>(); try { try { connection = Util.getDBConnection(); st = connection.createStatement(); rs = st.executeQuery("select l.bankcode,l.jrnl_id,l.txn_date from kcc_api_log l where l.txn_date='" + date + "'"); while (rs.next()) { String comb = rs.getString(1) + "|" + rs.getString(2) + "|" + rs.getString(3); combination.add(comb); } } catch (Exception ex) { System.out.println("No record found"); } Iterator iter = data.iterator(); Statement st2 = connection.createStatement(); while (iter.hasNext()) { DataBean d = iter.next(); if (!d.getTxnCode().equalsIgnoreCase("001057")) { if (combination.contains(d.getBankcode() + "|" + d.getJrnlId() + "|" + d.getTimestamp()) || d.getJrnlId() == null || d.getJrnlId().equalsIgnoreCase("")) { System.out.println("RECORD ALREADY PRESENT FOR " + d.getBankcode() + "|" + d.getJrnlId() + "|" + d.getTimestamp()); continue; } query = "insert into kcc_api_log (bankcode, cbsacctno, txncode, txnamt, txnind, narration, jrnl_id, txn_date, post_flag, success_flag, errorlog) values ('" + d.getBankcode() + "','" + d.getAcctNo() + "','" + d.getTxnCode() + "',abs(" + d.getTxnAmt() + "),'"; query = query + d.getTxnInd() + "','" + d.getNarration() + "','" + d.getJrnlId() + "','" + d.getTimestamp() + "','N','N'," + null + ")"; System.out.println("SQL Statement :" + query); st2.addBatch(query); System.out.println("Added to DB Insert Batch"); continue; } System.out.println("Txncode:" + d.getTxnCode()); } st2.executeBatch(); System.out.println("Insert to DB Completed"); System.out.println("Posting Txns in IPKS"); CallableStatement cs = connection.prepareCall("{call kcc_api_txn_post}"); cs.execute(); System.out.println("Posting Completed, Check log."); } catch (Exception ex) { ex.printStackTrace(); } finally { try { if (connection != null) connection.close(); } catch (SQLException e1) { e1.printStackTrace(); } } } }