You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

346 lines
12 KiB

  1. using System;
  2. using System.Data;
  3. using System.Drawing;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Web;
  8. using System.Xml;
  9. using Swift.DAL.BL.Remit.OFACManagement;
  10. using Swift.DAL.BL.System.Utility;
  11. using Swift.DAL.SwiftDAL;
  12. using Swift.web.Library;
  13. using static Swift.web.Library.CustomConfigHelper;
  14. namespace Swift.web.Remit.OFACManagement
  15. {
  16. public partial class Import : System.Web.UI.Page
  17. {
  18. private OFACDao obj = new OFACDao();
  19. private const string ViewFunctionId = "20211000";
  20. private string root = GetStatic.GetAppRoot();
  21. private string directory;
  22. RemittanceLibrary rl = new RemittanceLibrary();
  23. public Import()
  24. {
  25. directory = root + "doc";
  26. }
  27. protected void Page_Load(object sender, EventArgs e)
  28. {
  29. Authenticate();
  30. LoadLogGrid();
  31. LoadSourceWiseData();
  32. if (!IsPostBack)
  33. {
  34. LoadLogGrid();
  35. }
  36. }
  37. private void Authenticate()
  38. {
  39. rl.CheckAuthentication(ViewFunctionId);
  40. }
  41. protected void btnImport_Click(object sender, EventArgs e)
  42. {
  43. var dbResult = new DbResult();
  44. try
  45. {
  46. /*
  47. GetSDNContent("http://www.treasury.gov/ofac/downloads/sdn.pip");
  48. GetALTContent("http://www.treasury.gov/ofac/downloads/alt.pip");
  49. GetADDContent("http://www.treasury.gov/ofac/downloads/add.pip");
  50. */
  51. GetSDNContent(GetStatic.GetOFACSDN());
  52. GetALTContent(GetStatic.GetOFACALT());
  53. GetADDContent(GetStatic.GetOFACADD());
  54. DivMessage.InnerHtml = "File Download Completed !";
  55. }
  56. catch (Exception ex)
  57. {
  58. GetStatic.AlertMessage(this, $"Technical Error: { GetStatic.LogError(ex).Id}");
  59. }
  60. }
  61. private void ManageMessage(DbResult dbResult)
  62. {
  63. GetStatic.SetMessage(dbResult);
  64. if (dbResult.ErrorCode == "0")
  65. {
  66. GetStatic.PrintMessage(this.Page);
  67. LoadLogGrid();
  68. }
  69. else
  70. {
  71. GetStatic.SetMessage(dbResult.Msg, "1");
  72. return;
  73. }
  74. }
  75. private void ReadUNSCRFile(string URL, string fileName)
  76. {
  77. System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
  78. System.Net.ServicePointManager.ServerCertificateValidationCallback =
  79. ((sender, certificate, chain, sslPolicyErrors) => true);
  80. XmlDocument xDoc = new XmlDocument();
  81. xDoc.Load(URL);
  82. if (!Directory.Exists(directory + "\\OFAC"))
  83. Directory.CreateDirectory(directory + "\\OFAC");
  84. string filPath = GetStatic.GetAppRoot() + "doc\\OFAC\\"+ fileName + ".xml";
  85. xDoc.Save(filPath);
  86. }
  87. private void GetSDNContent(string URL)
  88. {
  89. System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
  90. HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(URL);
  91. myRequest.Method = WebRequestMethods.Http.Get;
  92. WebResponse myResponse = myRequest.GetResponse();
  93. StreamReader sr = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8);
  94. string line;
  95. string fileToCreate = directory + "\\OFAC\\SDN.pip";
  96. if (!Directory.Exists(directory +"\\OFAC"))
  97. Directory.CreateDirectory(directory + "\\OFAC");
  98. using (var file = new StreamWriter(fileToCreate))
  99. {
  100. while ((line = sr.ReadLine()) != null)
  101. {
  102. file.WriteLine(line);
  103. }
  104. }
  105. //Eliminate Last Line
  106. var lines = File.ReadAllLines(fileToCreate);
  107. File.WriteAllLines(fileToCreate, lines.Take(lines.Length - 1).ToArray());
  108. sr.Close();
  109. myResponse.Close();
  110. }
  111. private void GetALTContent(string URL)
  112. {
  113. System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
  114. HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(URL);
  115. myRequest.Method = "GET";
  116. WebResponse myResponse = myRequest.GetResponse();
  117. StreamReader sr = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8);
  118. string line;
  119. string fileToCreate = directory + "\\OFAC\\ALT.pip";
  120. if (!Directory.Exists(directory +"\\OFAC"))
  121. Directory.CreateDirectory(directory + "\\OFAC");
  122. using (var file = new StreamWriter(fileToCreate))
  123. {
  124. while ((line = sr.ReadLine()) != null)
  125. {
  126. file.WriteLine(line);
  127. }
  128. }
  129. var lines = File.ReadAllLines(fileToCreate);
  130. File.WriteAllLines(fileToCreate, lines.Take(lines.Length - 1).ToArray());
  131. sr.Close();
  132. myResponse.Close();
  133. }
  134. private void GetADDContent(string URL)
  135. {
  136. System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
  137. HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(URL);
  138. myRequest.Method = "GET";
  139. WebResponse myResponse = myRequest.GetResponse();
  140. StreamReader sr = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8);
  141. string line;
  142. string fileToCreate = directory +"\\OFAC\\ADD.pip";
  143. if (!Directory.Exists(directory + "\\OFAC"))
  144. Directory.CreateDirectory(directory + "\\OFAC");
  145. using (var file = new StreamWriter(fileToCreate))
  146. {
  147. while ((line = sr.ReadLine()) != null)
  148. {
  149. file.WriteLine(line);
  150. }
  151. }
  152. var lines = File.ReadAllLines(fileToCreate);
  153. File.WriteAllLines(fileToCreate, lines.Take(lines.Length - 1).ToArray());
  154. sr.Close();
  155. myResponse.Close();
  156. }
  157. private void LoadLogGrid()
  158. {
  159. DataTable dt = obj.LoadLog(GetStatic.GetUser());
  160. rpt_grid.InnerHtml = Misc.DataTableToHtmlTable(ref dt);
  161. }
  162. private void LoadSourceWiseData()
  163. {
  164. DataTable dt = obj.LoadSourceWiseData(GetStatic.GetUser());
  165. SourceWiseData.InnerHtml = Misc.DataTableToHtmlTable(ref dt);
  166. }
  167. private void LoadAQLogGrid()
  168. {
  169. DataTable dt = obj.LoadLog(GetStatic.GetUser());
  170. rpt_grid.InnerHtml = Misc.DataTableToHtmlTable(ref dt);
  171. }
  172. protected void BtnImpAQList_Click(object sender, EventArgs e)
  173. {
  174. try
  175. {
  176. //ReadUNSCRFile("http://www.un.org/sc/committees/1267/AQList.xml");
  177. //DownloadUNSCRData();
  178. ReadUNSCRFile(GetStatic.GetOFACUNSCR(), "consolidated");
  179. DivMessage.InnerHtml = "File Download Completed !";
  180. }
  181. catch (Exception ex)
  182. {
  183. GetStatic.AlertMessage(this, $"Technical Error: { GetStatic.LogError(ex).Id}");
  184. }
  185. }
  186. private void DownloadUNSCRData()
  187. {
  188. var list = PartnerAgentConfig.GetParentConfig(SectionCategory.SDNListSection);
  189. var activeList = from s in list
  190. where s.Active.ToLower() == "yes"
  191. select s;
  192. foreach (var item in activeList)
  193. {
  194. ReadUNSCRFile(item.URL, item.Key);
  195. }
  196. }
  197. private string GetUNSCRData(string fileName)
  198. {
  199. //var aqFilePath = GetStatic.GetAppRoot() + "doc\\OFAC\\"+ fileName + ".xml";
  200. var aqFilePath = GetStatic.GetAppRoot() + fileName;
  201. FileWebRequest myRequest = (FileWebRequest)WebRequest.Create(aqFilePath);
  202. myRequest.Method = "GET";
  203. WebResponse myResponse = myRequest.GetResponse();
  204. StreamReader sr = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8);
  205. string xmlContent = sr.ReadToEnd().Trim();
  206. xmlContent = xmlContent.Replace("'", "");
  207. xmlContent = xmlContent.Replace("utf-8", "utf-16");
  208. xmlContent = xmlContent.Replace("UTF-8", "UTF-16");
  209. xmlContent = xmlContent.Replace("&amp", "and");
  210. return xmlContent;
  211. }
  212. protected void btnUpload_Click(object sender, EventArgs e)
  213. {
  214. try
  215. {
  216. //var list = PartnerAgentConfig.GetParentConfig(SectionCategory.SDNListSection);
  217. //var activeList = from s in list
  218. // where s.Active.ToLower() == "yes"
  219. // select s;
  220. //foreach (var item in activeList)
  221. //{
  222. // string xmlContent = GetUNSCRData(item.Key);
  223. // var dbResult = obj.UpdateAQList(GetStatic.GetUser(), xmlContent, item.Key + ".xml", item.Key);
  224. // ManageMessage(dbResult);
  225. // DivMessage.InnerHtml = dbResult.Msg;
  226. //}
  227. string xmlContent = GetUNSCRData("consolidated");
  228. var dbResult = obj.UpdateAQList(GetStatic.GetUser(), xmlContent, "consolidated.xml", "UNSC");
  229. ManageMessage(dbResult);
  230. DivMessage.InnerHtml = dbResult.Msg;
  231. LoadAQLogGrid();
  232. }
  233. catch (Exception ex)
  234. {
  235. GetStatic.AlertMessage(this, $"Technical Error: { GetStatic.LogError(ex).Id}");
  236. }
  237. }
  238. protected void btnLoadOfac_Click(object sender, EventArgs e)
  239. {
  240. try
  241. {
  242. var dbResult = new DbResult();
  243. var sdnFilePath = GetStatic.GetAppRoot() + "doc\\OFAC\\SDN.pip";
  244. var altFilePath = GetStatic.GetAppRoot() + "doc\\OFAC\\ALT.pip";
  245. var addFilePath = GetStatic.GetAppRoot() + "doc\\OFAC\\ADD.pip";
  246. dbResult = obj.Update(GetStatic.GetUser(), sdnFilePath, altFilePath, addFilePath);
  247. ManageMessage(dbResult);
  248. DivMessage.InnerHtml = dbResult.Msg;
  249. }
  250. catch (Exception ex)
  251. {
  252. GetStatic.AlertMessage(this, $"Technical Error: { GetStatic.LogError(ex).Id}");
  253. }
  254. }
  255. protected void Button1_Click(object sender, EventArgs e)
  256. {
  257. var dbResult = new DbResult();
  258. //import from inficare DB
  259. dbResult = obj.UpdateOther(GetStatic.GetUser());
  260. ManageMessage(dbResult);
  261. DivMessage.InnerHtml = dbResult.Msg;
  262. }
  263. protected void btnFileUpload_Click(object sender, EventArgs e)
  264. {
  265. try
  266. {
  267. if (fileUpload.FileContent.Length > 0)
  268. {
  269. if (fileUpload.FileName.ToLower().Contains(".xml"))
  270. {
  271. string fileName = fileUpload.FileName;
  272. string xmlContent = GetUNSCRData(fileName);
  273. var dbResult = obj.UpdateAQList(GetStatic.GetUser(), xmlContent, fileName, "UNSCR");
  274. ManageMessage(dbResult);
  275. DivMessage.InnerHtml = dbResult.Msg;
  276. LoadAQLogGrid();
  277. }
  278. else
  279. {
  280. GetStatic.AlertMessage(this, "Invalid file format uploaded");
  281. }
  282. }
  283. }
  284. catch (Exception ex)
  285. {
  286. GetStatic.AlertMessage(this, $"Technical Error: { GetStatic.LogError(ex).Id}");
  287. }
  288. }
  289. }
  290. }