Advertisement
Guest User

Untitled

a guest
May 22nd, 2017
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.81 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Xml.Serialization;
  10. using System.IO;
  11. using nu.mrorange;
  12. using nu.mrorange.config;
  13. using nu.mrorange.search;
  14. using nu.mrorange.crs;
  15. using System.Drawing;
  16. using System.Threading;
  17. using nu.mrorange.util.log;
  18. using System.Diagnostics;
  19.  
  20. namespace MrOSearchApp2
  21. {
  22. public partial class Form1 : Form
  23. {
  24. TextWriter _writer = null;
  25. private string hostName = "", databaseName = "", userName = "sa", passWord = "homermad!";
  26. int partnerId = -1;
  27.  
  28. Thread mainSearchThread;
  29. public Form1()
  30. {
  31. InitializeComponent();
  32.  
  33. _writer = new TextBoxStreamWriter(textBox1);
  34. Console.SetOut(_writer);
  35. btnSearch.Enabled = false;
  36.  
  37. datePickerDeparture.Value = DateTime.Now.AddDays(7);
  38. datePickerArrival.Value = DateTime.Now.AddDays(14);
  39.  
  40. txtTo.Text = "STO";
  41. txtFrom.Text = "PAR";
  42.  
  43. Server[] servers = GetServers();
  44. int serverCount = 0;
  45. foreach (Server server in servers)
  46. {
  47. passWord = server.passWord;
  48. string[] databases;
  49. try
  50. {
  51. databases = Database.GetDatabases(server.IpAddress, server.user, server.passWord);
  52. }
  53. catch (Exception ex)
  54. {
  55. MessageBox.Show(this, ex.Message);
  56. return;
  57. }
  58. List<TreeNode> nodes = new List<TreeNode>();
  59. foreach (string database in databases)
  60. {
  61. if (
  62. !database.Contains("avail") &&
  63. !database.Contains("common") &&
  64. database != "information_schema" &&
  65. database != "test" &&
  66. database != "innodb_status" &&
  67. database != "mysql")
  68. nodes.Add(new TreeNode(database));
  69. }
  70. if (nodes.Count == 0)
  71. nodes.Add(new TreeNode("No databases!"));
  72. TreeNode node = new TreeNode(server.Name, nodes.ToArray());
  73. node.Name = server.IpAddress;
  74. TreeView1.Nodes.Add(node);
  75. serverCount++;
  76. }
  77. }
  78.  
  79. private void Form1_Load(object sender, EventArgs e)
  80. {
  81. _writer = new TextBoxStreamWriter(textBox1);
  82. Console.SetOut(_writer);
  83. }
  84.  
  85. /// <summary>
  86. /// Gets the servers.
  87. /// </summary>
  88. /// <returns>An array with servers[]</returns>
  89. private static Server[] GetServers()
  90. {
  91. XmlSerializer serializer = new XmlSerializer(typeof(List<Server>));
  92. StreamReader sr = new StreamReader("Servers.xml");
  93. List<Server> servers = (List<Server>)serializer.Deserialize(sr);
  94. return servers.ToArray();
  95. }
  96.  
  97. /// <summary>
  98. /// Handles the DoubleClick event of the TreeView1 control.
  99. /// </summary>
  100. /// <param name="sender">The source of the event.</param>
  101. /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
  102. private void TreeView1_DoubleClick(object sender, EventArgs e)
  103. {
  104. ComboBoxCustomer.Items.Clear();
  105. databaseName = TreeView1.SelectedNode.Text;
  106. hostName = TreeView1.SelectedNode.Parent.Name;
  107. FillPartnerDropdown();
  108. }
  109.  
  110. /// <summary>
  111. /// Fills the partner dropdown.
  112. /// </summary>
  113. private void FillPartnerDropdown()
  114. {
  115. ComboBoxCustomer.Items.Clear();
  116. string[] partners = Database.GetPartners(
  117. TreeView1.SelectedNode.Parent.Name,
  118. "sa",
  119. "homermad!",
  120. TreeView1.SelectedNode.Text);
  121.  
  122. ComboBoxCustomer.Items.Add("0 - BASESITE");
  123. if (partners != null)
  124. {
  125. foreach (string partner in partners)
  126. {
  127. ComboBoxCustomer.Items.Add(partner);
  128. }
  129. }
  130. ComboBoxCustomer.SelectedIndex = 0;
  131. }
  132.  
  133. /// <summary>
  134. /// Handles the Click event of the btnSearch control.
  135. /// </summary>
  136. /// <param name="sender">The source of the event.</param>
  137. /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
  138. private void btnSearch_Click(object sender, EventArgs e)
  139. {
  140. treeViewSearchInfo.Nodes.Clear();
  141. listViewItinerary.Items.Clear();
  142. listViewRowInfo.Items.Clear();
  143.  
  144. mainSearchThread = new Thread(new ThreadStart(DoSearch));
  145. mainSearchThread.Start();
  146. }
  147.  
  148. /// <summary>
  149. /// Does the search.
  150. /// </summary>
  151. private void DoSearch()
  152. {
  153. UpdateButton(false);
  154. UpdateProgressBar(true);
  155.  
  156. Thread progressBarThread = new Thread(new ThreadStart(RunProgressBar));
  157. progressBarThread.Start();
  158.  
  159. string connectionString = "Server=" + hostName + ";Database=" + databaseName + ";User ID=" + userName + "; Password=" + passWord + ";";
  160. string[] ageOfChildrenString = txtAgeOfChildren.Text.Split(',');
  161. decimal child;
  162. decimal[] ageChildren;
  163. if (ageOfChildrenString.All(number => Decimal.TryParse(number, out child)));
  164. {
  165. ageChildren = Array.ConvertAll<string, decimal>(ageOfChildrenString, Convert.ToDecimal);
  166. }
  167.  
  168. ConfigurationManager.DatabaseConnectionString = connectionString;
  169. PartnerConfig partnerConfig = new PartnerConfig(partnerId,true);
  170. AirSearchParameter airParam = new AirSearchParameter();
  171. airParam.FromCity = txtFrom.Text;
  172. airParam.ToCity = txtTo.Text;
  173. airParam.DepartureDate = DateTime.Parse(datePickerDeparture.Text);
  174. airParam.ReturnDate = DateTime.Parse(datePickerArrival.Text);
  175. airParam.Currency = txtCurrency.Text;
  176. airParam.Adults = Convert.ToInt32(NumericAdult.Value);
  177. airParam.NonAdultAges = ageChildren;
  178. airParam.OneWay = chkOneWay.Checked;
  179. airParam.PreferredCarriers = txtPrefCarrier.Text.Split(',');
  180. airParam.ExcludedCarriers = txtExclCarrier.Text.Split(',');
  181. airParam.RestrictToCarriers = txtRestrCarriers.Text.Split(',');
  182. airParam.PartnerConfig = partnerConfig;
  183.  
  184. try
  185. {
  186. List<Itineraries> itins = BookingEngine.GetAirFaresInterface(airParam);
  187. foreach (Itinerary itinerary in itins[0])
  188. {
  189. ListViewItem item = new ListViewItem(itinerary.Id.ToString()); //ItineraryID
  190. item.SubItems.Add(new ListViewItem.ListViewSubItem(item, itinerary.CRS.ToString()));
  191. item.SubItems.Add(new ListViewItem.ListViewSubItem(item, itinerary.MainCarrier));
  192. item.SubItems.Add(new ListViewItem.ListViewSubItem(item, itinerary.Pricing.BaseFarePerAdult.ToString()));
  193. item.SubItems.Add(new ListViewItem.ListViewSubItem(item, itinerary.Pricing.TaxPerAdult.ToString()));
  194. item.SubItems.Add(new ListViewItem.ListViewSubItem(item, (itinerary.Pricing.BaseFarePerAdult + itinerary.Pricing.TaxPerAdult).ToString()));
  195. item.SubItems.Add(new ListViewItem.ListViewSubItem(item, (itinerary.Pricing.BaseFarePerAdult + itinerary.Pricing.TaxPerAdult +
  196.  
  197. itinerary.Pricing.BaseFareAddonPerAdult + itinerary.Pricing.TaxAddonPerAdult).ToString()));
  198. UpdateListView(item);
  199. }
  200. int searchCount = 0;
  201. foreach (SearchInfo searchInfo in itins[0].searchInfo)
  202. {
  203. List<TreeNode> nodes = new List<TreeNode>();
  204. searchCount += 1;
  205. nodes.Add(new TreeNode("CRSName: " + searchInfo.CRStype.ToString()));
  206. nodes.Add(new TreeNode("Search Time: " + searchInfo.SearchTime));
  207. nodes.Add(new TreeNode("Itinerary Count: " + searchInfo.ItineraryCount));
  208. nodes.Add(new TreeNode("SearchInfo: " + searchInfo.MiscSearchInfo));
  209. nodes.Add(new TreeNode("Exception: " + searchInfo.ExceptionMessage));
  210.  
  211. TreeNode node = new TreeNode("Search " + searchCount, nodes.ToArray());
  212. UpdateTreeView(node);
  213. }
  214.  
  215. //Read blackbox
  216. foreach (nu.mrorange.util.log.BlackBoxEntry entry in airParam.PartnerConfig.BlackBox.GetCopyOfEntries())
  217. {
  218. SetBlackBox(entry);
  219. }
  220. }
  221.  
  222. catch (Exception ex)
  223. {
  224. MessageBox.Show(ex.Message);
  225. }
  226. finally
  227. {
  228. UpdateButton(true);
  229. UpdateProgressBar(false);
  230. progressBarThread.Abort();
  231. }
  232. }
  233.  
  234. /// <summary>
  235. /// Checks if BlackBoxEntry is null or not
  236. /// </summary>
  237. /// <param name="item">The item.</param>
  238. /// <returns></returns>
  239. private BlackBoxEntry BlackBoxEntry(BlackBoxEntry item)
  240. {
  241. if (item != null)
  242. {
  243. return item;
  244. }
  245. return null;
  246. }
  247.  
  248. /// <summary>
  249. /// Sets the black box.
  250. /// </summary>
  251. /// <param name="item">The item.</param>
  252. void SetBlackBox(BlackBoxEntry item)
  253. {
  254. if (InvokeRequired)
  255. {
  256. this.Invoke((MethodInvoker)delegate()
  257. {
  258. SetBlackBox(item);
  259. });
  260. return;
  261. }
  262. listBoxBlackboxOutput.Items.Add(item.ToString());
  263. }
  264.  
  265. /// <summary>
  266. /// Runs the progress bar.
  267. /// </summary>
  268. private void RunProgressBar()
  269. {
  270. while (true)
  271. {
  272. if (progressBar1.Value == progressBar1.Maximum)
  273. {
  274. SetValueProgressBar(0);
  275. }
  276. SetValueProgressBar(progressBar1.Value + 1);
  277. Thread.Sleep(200);
  278. }
  279. }
  280.  
  281. /// <summary>
  282. /// Updates the list view.
  283. /// </summary>
  284. /// <param name="item">The item.</param>
  285. void UpdateListView(ListViewItem item)
  286. {
  287. if (InvokeRequired)
  288. {
  289. this.Invoke((MethodInvoker)delegate() {
  290. UpdateListView(item);
  291. });
  292. return;
  293. }
  294. listViewItinerary.Items.Add(item).ToString();
  295. }
  296.  
  297. /// <summary>
  298. /// Updates the tree view.
  299. /// </summary>
  300. /// <param name="item">The item.</param>
  301. void UpdateTreeView(TreeNode item)
  302. {
  303. if (InvokeRequired)
  304. {
  305. this.Invoke((MethodInvoker)delegate()
  306. {
  307. UpdateTreeView(item);
  308. });
  309. return;
  310. }
  311. treeViewSearchInfo.Nodes.Add(item);
  312. }
  313.  
  314. /// <summary>
  315. /// Updates the button.
  316. /// </summary>
  317. /// <param name="enable">if set to <c>true</c> [enable].</param>
  318. void UpdateButton(bool enable)
  319. {
  320. if (InvokeRequired)
  321. {
  322. this.Invoke((MethodInvoker)delegate()
  323. {
  324. UpdateButton(enable);
  325. });
  326. return;
  327. }
  328. btnSearch.Enabled = enable;
  329. }
  330.  
  331. /// <summary>
  332. /// Updates the progress bar.
  333. /// </summary>
  334. /// <param name="enable">if set to <c>true</c> [enable].</param>
  335. void UpdateProgressBar(bool enable)
  336. {
  337. if (InvokeRequired)
  338. {
  339. this.Invoke((MethodInvoker)delegate()
  340. {
  341. UpdateProgressBar(enable);
  342. });
  343. return;
  344. }
  345. progressBar1.Visible = enable;
  346. }
  347.  
  348. /// <summary>
  349. /// Sets the value of the progress bar.
  350. /// </summary>
  351. /// <param name="value">The value.</param>
  352. void SetValueProgressBar(int value)
  353. {
  354. if (InvokeRequired)
  355. {
  356. this.Invoke((MethodInvoker)delegate()
  357. {
  358. SetValueProgressBar(value);
  359. });
  360. return;
  361. }
  362. progressBar1.Value = value;
  363. }
  364.  
  365. /// <summary>
  366. /// Handles the AfterSelect event of the TreeView1 control.
  367. /// </summary>
  368. /// <param name="sender">The source of the event.</param>
  369. /// <param name="e">The <see cref="System.Windows.Forms.TreeViewEventArgs"/> instance containing the event data.</param>
  370. private void TreeView1_AfterSelect(object sender, TreeViewEventArgs e)
  371. {
  372. btnSearch.Enabled = true;
  373. }
  374.  
  375. /// <summary>
  376. /// Handles the SelectedIndexChanged event of the ComboBoxCustomer control.
  377. /// </summary>
  378. /// <param name="sender">The source of the event.</param>
  379. /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
  380. private void ComboBoxCustomer_SelectedIndexChanged(object sender, EventArgs e)
  381. {
  382. partnerId = int.Parse(ComboBoxCustomer.Text.Substring(0, 3).Replace("-", "").Trim());
  383. }
  384.  
  385. /// <summary>
  386. /// Handles the SelectedIndexChanged event of the listViewItinerary control.
  387. /// </summary>
  388. /// <param name="sender">The source of the event.</param>
  389. /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
  390. private void listViewItinerary_SelectedIndexChanged(object sender, EventArgs e)
  391. {
  392. if (listViewItinerary.SelectedItems.Count > 0)
  393. toolStripMenuItem1.Enabled = true;
  394. else
  395. toolStripMenuItem1.Enabled = false;
  396.  
  397. listViewRowInfo.Items.Clear();
  398. PartnerConfig partnerConfig = new PartnerConfig(partnerId);
  399. {
  400. foreach (ListViewItem item in listViewItinerary.Items)
  401. {
  402. if (item.Selected == true)
  403. {
  404. int itineraryid = int.Parse(item.SubItems[0].Text);
  405.  
  406. Itinerary itinerary = nu.mrorange.BookingEngine.GetItinerary(itineraryid, partnerConfig);
  407. foreach (Segment seg in itinerary.Segments)
  408. {
  409. ListViewItem item2 = new ListViewItem(seg.Id.ToString()); //ItineraryID
  410.  
  411. item2.SubItems.Add(new ListViewItem.ListViewSubItem(item2, seg.DateTime1.ToString("hh:mm")));
  412. item2.SubItems.Add(new ListViewItem.ListViewSubItem(item2, seg.DateTime2.ToString("hh:mm")));
  413. item2.SubItems.Add(new ListViewItem.ListViewSubItem(item2, seg.City2.ToString()));
  414. item2.SubItems.Add(new ListViewItem.ListViewSubItem(item2, seg.City1.ToString()));
  415. item2.SubItems.Add(new ListViewItem.ListViewSubItem(item2, seg.Carrier + "-" + seg.FlightNumber.ToString()));
  416. listViewRowInfo.Items.Add(item2);
  417. }
  418. }
  419. }
  420. }
  421. }
  422.  
  423. /// <summary>
  424. /// ContextMenu called when right clicked on an item in listViewItinerary.
  425. /// </summary>
  426. /// <param name="sender">The sender.</param>
  427. /// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
  428. void OnEventListView_MouseDown(object sender, MouseEventArgs e)
  429. {
  430. if (e.Button == MouseButtons.Right)
  431. {
  432. listViewItinerary.ContextMenu.Show(listViewItinerary, new Point(e.X, e.Y));
  433. }
  434. }
  435.  
  436. /// <summary>
  437. /// Handles the Click event of the toolStripMenuItem1 control. Generates a pop p window to show.
  438. /// </summary>
  439. /// <param name="sender">The source of the event.</param>
  440. /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
  441. void toolStripMenuItem1_Click(object sender, EventArgs e)
  442. {
  443. PartnerConfig partnerConfig = new PartnerConfig(partnerId);
  444. int itineraryid = int.Parse(listViewItinerary.SelectedItems[0].SubItems[0].Text);
  445. Itinerary itinerary = nu.mrorange.BookingEngine.GetItinerary(itineraryid, partnerConfig);
  446. if (listViewItinerary.SelectedItems.Count > 0)
  447. {
  448. FormPopUp popUp = new FormPopUp(itinerary);
  449. popUp.ShowDialog(this);
  450. }
  451. }
  452. }
  453. }
  454. public class TextBoxStreamWriter : TextWriter
  455. {
  456. TextBox _output = null;
  457. public TextBoxStreamWriter(TextBox output)
  458. {
  459. _output = output;
  460. }
  461.  
  462. /// <summary>
  463. /// Writes a character to the text stream.
  464. /// </summary>
  465. /// <param name="value">The character to write to the text stream.</param>
  466. /// <exception cref="T:System.ObjectDisposedException">The <see cref="T:System.IO.TextWriter"/> is closed. </exception>
  467. /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
  468. public override void Write(char value)
  469. {
  470. MethodInvoker action = delegate { _output.AppendText(value.ToString()); };
  471. _output.BeginInvoke(action);
  472. }
  473.  
  474. /// <summary>
  475. /// When overridden in a derived class, returns the <see cref="T:System.Text.Encoding"/> in which the output is written.
  476. /// </summary>
  477. /// <value></value>
  478. /// <returns>The Encoding in which the output is written.</returns>
  479. public override Encoding Encoding
  480. {
  481. get { return System.Text.Encoding.UTF8; }
  482. }
  483. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement