using Swift.API.MapAPIData; using Swift.DAL.APIDataMappingDao; using Swift.DAL.SwiftDAL; using Swift.web.Library; using System; using System.Collections.Generic; using System.Data; using System.Data.OleDb; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Windows.Forms; using System.Xml.Serialization; using static Swift.API.Common.MapAPIData.APIBankModel; namespace Swift.web.Remit.APIDataMapping.BankDataMapping { public partial class ManageBankData : System.Web.UI.Page { protected DownloadAPIData _map = new DownloadAPIData(); protected APIMapping _dao = new APIMapping(); protected FuzzyMatcher _match = new FuzzyMatcher(); private readonly RemittanceLibrary _sl = new RemittanceLibrary(); private const string ViewFunctionId = "20201800"; protected void Page_Load(object sender, EventArgs e) { Authenticate(); if (!IsPostBack) { ShowMasterData(); } } private void Authenticate() { _sl.CheckAuthentication(ViewFunctionId); } protected void btnDownloadBank_Click(object sender, EventArgs e) { //string partner = GetPartner(); //if (!string.IsNullOrEmpty(partner)) //{ // BankRequest request = new BankRequest() // { // CountryCode = GetCountry(), // ProviderId = partner, // SessionId = GetStatic.GetSessionId() // }; // var response = _map.ThirdPartyApiGetDataOnlyNew(request, "api/v1/TP/bankList", "Data", "post"); // if (response.ResponseCode == "0") // { // var sessionId = GetStatic.GetSessionId(); // var xml = ObjectToXML(response.Data); // var res = _dao.SyncBank(GetStatic.GetUser(), xml, GetCountry(), request.ProviderId, "NPR", sessionId); // if (res.ErrorCode == "0") // { // LoadDataFromTemp(sessionId); // } // else // { // GetStatic.AlertMessage(this, res.Msg); // } // } //} } private List GetMatchingData(DataRow item, DataTable dt) { int pcnt = 0; string stringToSearch = item["BANK_NAME"].ToString().Replace(".", ""); string pattern = ""; bool res = false; string md1 = "", md2 = "", md3 = "", ret1 = "", ret2 = ""; List list = new List(); //var masterData = item["BANK_NAME"].ToString().Replace(".", "").Split(' '); //md1 = SoundexA.Soundex(masterData[0]); //int matchCount = 0; //if(masterData.Length > 1) // md2 = SoundexA.Soundex(masterData[1]); //if (masterData.Length > 2) // md3 = SoundexA.Soundex(masterData[2]); foreach (DataRow dr in dt.Rows) { //var matchData = dr["BANK_NAME"].ToString().Replace(".", "").Split(' '); //foreach (var items in matchData) //{ // string soundex = SoundexA.Soundex(items); // if (soundex == md1 || soundex == md2 || soundex == md3) // { // matchCount++; // } //} int lengthh = stringToSearch.Length; pattern = dr["BANK_NAME"].ToString().Replace(".", ""); res = FuzzyMatcher.FuzzyMatch(stringToSearch, pattern, out pcnt); if (res == true && pcnt >= 0) { list.Add(dr["ROW_ID"].ToString()); list.Add(dr["BANK_NAME"].ToString()); break; } //matchCount = 0; } return list; } public string ObjectToXML(object input) { try { var stringwriter = new StringWriter(); var serializer = new XmlSerializer(input.GetType()); serializer.Serialize(stringwriter, input); return stringwriter.ToString(); } catch (Exception ex) { if (ex.InnerException != null) ex = ex.InnerException; return "Could not convert: " + ex.Message; } } protected void ShowMasterData(string isAfterMap = "N") { detailsLabel.Text = "Mapping for: " + GetPartnerName() + " >> " + GetStatic.ReadQueryString("countryName", "") + " >> " + GetStatic.ReadQueryString("paymentTypeName", ""); payoutPartner.Text = "Bank List For: " + GetPartnerName(); if (!string.IsNullOrEmpty(GetCountry()) && !string.IsNullOrEmpty(GetPaymentMode())) { DataTable dt = null; if (isAfterMap == "N") { dt = GetStatic.ReadSessionAsTable("MasterDataList"); //if (dt == null) //{ dt = _dao.GetMasterDataList(GetStatic.GetUser(), GetCountry(), GetPaymentMode(), GetPartner(), GetNoOfRows()); //} } else { dt = _dao.GetMasterDataList(GetStatic.GetUser(), GetCountry(), GetPaymentMode(), GetPartner(), GetNoOfRows()); } if (dt != null || dt.Rows.Count > 0) { GetStatic.WriteSessionAsDataTable("MasterDataList", dt); PopulateMasterData(dt); } } } private void PopulateMasterData(DataTable dt) { //if (dt.Rows.Count == 0) //{ // btnDownloadBank.Enabled = true; // btnLoadFromTemp.Enabled = false; // btnSaveMapping.Enabled = false; // btnShowMappedData.Disabled = true; //} StringBuilder sb = new StringBuilder(); foreach (DataRow item in dt.Rows) { sb.AppendLine(""); sb.AppendLine("" + item["BANK_NAME"].ToString() + ""); sb.AppendLine("" + item["JME_BANK_CODE"].ToString() + ""); sb.AppendLine(""); } masterTableBody.InnerHtml = sb.ToString(); } protected string GetCountry() { return GetStatic.ReadQueryString("country", ""); } protected string GetPartner() { return GetStatic.ReadQueryString("partner", ""); } protected string GetPartnerName() { return GetStatic.ReadQueryString("partnerName", ""); } protected string GetCountryName() { return GetStatic.ReadQueryString("countryName", ""); } protected string GetPaymentTypeName() { return GetStatic.ReadQueryString("paymentTypeName", ""); } protected string GetPaymentMode() { return GetStatic.ReadQueryString("paymentType", "") == "" ? "0" : GetStatic.ReadQueryString("paymentType", ""); } protected string GetNoOfRows() { return ddlNoOfBanks.SelectedValue; } protected void btnLoadFromTemp_Click(object sender, EventArgs e) { LoadDataFromTemp(""); } protected void LoadDataFromTemp(string sessionId) { DataTable dt = _dao.GetMasterDownlodList(GetStatic.GetUser(), GetCountry(), GetPaymentMode(), GetPartner(), sessionId); if (dt.Rows.Count > 0 || dt != null) { StringBuilder sb = new StringBuilder(); DataTable _masterData = _dao.GetMasterDataList(GetStatic.GetUser(), GetCountry(), GetPaymentMode(), GetPartner(), GetNoOfRows()); foreach (DataRow item in _masterData.Rows) { var res = GetMatchingData(item, dt); sb.AppendLine(""); if (res.Count > 0) { sb.AppendLine("" + GetStatic.MakeAutoCompleteControl(item["MASTER_BANK_ID"].ToString(), "'category' : 'remit-mapBankData'", res[0], res[1] + " | " + res[0]) + ""); } else { sb.AppendLine("" + GetStatic.MakeAutoCompleteControl(item["MASTER_BANK_ID"].ToString(), "'category' : 'remit-mapBankData'") + ""); } sb.AppendLine("" + item["JME_BANK_CODE"].ToString() + ""); sb.AppendLine(""); } tableBody.InnerHtml = sb.ToString(); } } protected void btnSaveMapping_Click(object sender, EventArgs e) { DataTable dt = GetStatic.ReadSessionAsTable("MasterDataList"); if (dt.Rows.Count == 0 || dt == null) { return; } StringBuilder sb = new StringBuilder(""); foreach (DataRow item in dt.Rows) { sb.AppendLine(""); } sb.Append(""); string xml = sb.ToString(); _dao.SaveMappingData(GetStatic.GetUser(), xml); ShowMasterData("Y"); LoadDataFromTemp(""); } protected void ddlNoOfBanks_SelectedIndexChanged(object sender, EventArgs e) { ShowMasterData("Y"); } protected void btnUploadBank_Click(object sender, EventArgs e) { string filePath = string.Empty; string fileExt = string.Empty; string partner = GetPartner(); var sessionId = GetStatic.GetSessionId(); if (!string.IsNullOrEmpty(partner)) { filePath = uploadedFile.Text; //get the path of the file fileExt = Path.GetExtension(filePath); //get the file extension if (fileExt.CompareTo(".xls") == 0 || fileExt.CompareTo(".xlsx") == 0) { try { DataSet dtExcel = new DataSet(); dtExcel = ReadExcel(filePath, fileExt); //read excel file var xml = ObjectToXML(dtExcel.Tables[0]); //Compare Jme BankName and partner bankName DbResult matchedResult = CompareBankName(xml); if (matchedResult.ErrorCode.ToString() != "1") { var res = _dao.SyncBankNew(GetStatic.GetUser(), xml, GetCountryName(), partner, "NPR", sessionId, GetPaymentMode()); if (res.ErrorCode == "0") { ShowMasterData(); LoadDataFromTemp(sessionId); } else { GetStatic.AlertMessage(this, res.Msg); } } else { GetStatic.AlertMessage(this.Page, matchedResult.Msg.ToString()); } } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } } else { MessageBox.Show("Please choose .xls or .xlsx file only.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error); //custom messageBox to show error } } } protected void chooseFile_Click(object sender, EventArgs e) { Thread t = new Thread((ThreadStart)(() => { OpenFileDialog saveFileDialog1 = new OpenFileDialog(); saveFileDialog1.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm"; saveFileDialog1.FilterIndex = 2; saveFileDialog1.RestoreDirectory = true; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { uploadedFile.Text = saveFileDialog1.FileName; } })); // Run your code from a thread that joins the STA Thread t.SetApartmentState(ApartmentState.STA); t.Start(); t.Join(); } public DataSet ReadExcel(string fileName, string fileExt) { string conn = string.Empty; DataSet dtexcel = new DataSet(); if (fileExt.CompareTo(".xls") == 0) conn = @"provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + ";Extended Properties='Excel 8.0;HRD=Yes;IMEX=1';"; //for below excel 2007 else conn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties=Excel 12.0;"; //for above excel 2007 using (OleDbConnection con = new OleDbConnection(conn)) { try { OleDbDataAdapter oleAdpt = new OleDbDataAdapter("select * from [Sheet1$]", con); //here we read data from sheet1 oleAdpt.Fill(dtexcel); //fill excel data into dataTable } catch (Exception ex) { } } return dtexcel; } public DbResult CompareBankName(string xml) { DbResult result = _dao.CompareBankName(GetStatic.GetUser(), xml); return result; } public class MyObj { public string Name { get; set; } public string ID { get; set; } } protected void btnLoadFromBankList_Click(object sender, EventArgs e) { DbResult res = _dao.LoadFromBankList(GetStatic.GetUser(), GetCountry(), GetPaymentMode(), GetPartner(), ""); if (res.ErrorCode == "0") { LoadDataFromTemp(""); } else { GetStatic.AlertMessage(this.Page, res.Msg); } } } }