PageRenderTime 26ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/GrinGlobal.DatabaseCopier/frmInstallData.cs

https://gitlab.com/GRIN-Global/GRIN-Global-server
C# | 212 lines | 158 code | 35 blank | 19 comment | 20 complexity | 4cd24e8cbc1425d99782bb12b502c05d MD5 | raw file
  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.Diagnostics;
  10. using GrinGlobal.InstallHelper;
  11. using Microsoft.Win32;
  12. using GrinGlobal.Core;
  13. namespace GrinGlobal.DatabaseCopier {
  14. public partial class frmInstallData : Form {
  15. public frmInstallData() {
  16. InitializeComponent();
  17. }
  18. private void frmInstallData_Load(object sender, EventArgs e) {
  19. DataFiles = new List<string>();
  20. treeView1.ExpandAll();
  21. treeView1.SelectedNode = treeView1.Nodes[0];
  22. }
  23. private void checkBox4_CheckedChanged(object sender, EventArgs e) {
  24. }
  25. private void checkBox5_CheckedChanged(object sender, EventArgs e) {
  26. }
  27. private void checkBox1_CheckedChanged(object sender, EventArgs e) {
  28. }
  29. private void checkBox3_CheckedChanged(object sender, EventArgs e) {
  30. }
  31. private void checkBox2_CheckedChanged(object sender, EventArgs e) {
  32. }
  33. private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) {
  34. string tag = e.Node.Tag == null ? "" : e.Node.Tag.ToString();
  35. switch (tag) {
  36. case "Taxonomy Data":
  37. lblDescription.Text = "Taxonomy data. Roughly 7 MB.";
  38. break;
  39. case "Accession Data":
  40. lblDescription.Text = "Example Accession and Inventory data. Requires Taxonomy data. Roughly 13 MB.";
  41. break;
  42. case "Order Data":
  43. lblDescription.Text = "Example Order data. Requires Accession / Inventory and Taxonomy data. Roughly 12 MB.";
  44. break;
  45. case "Observation Data":
  46. lblDescription.Text = "Example Observation and Evaluation data. Requires Accession / Inventory and Taxonomy data. Roughly 6 MB.";
  47. break;
  48. default:
  49. lblDescription.Text = "Select an item on the left for more information.";
  50. break;
  51. }
  52. }
  53. public List<string> DataFiles;
  54. private IEnumerable<TreeNode> getAllNodes(TreeNodeCollection col) {
  55. foreach (TreeNode node in col) {
  56. yield return node;
  57. foreach (TreeNode child in getAllNodes(node.Nodes)) {
  58. yield return child;
  59. }
  60. }
  61. }
  62. /// <summary>
  63. /// Downloads the files and fills the DataFiles property (which is also the return value of the method)
  64. /// </summary>
  65. public List<string> DownloadAllFiles() {
  66. var filesToDownload = determineFilesToDownload(true);
  67. // get location of GrinGlobal.Updater.exe
  68. var updaterPath = Utility.GetGrinGlobalApplicationInstallPath("Updater", @"<registry key not found>");
  69. return performDownloads(updaterPath, filesToDownload.ToArray());
  70. }
  71. /// <summary>
  72. /// Downloads files and adds the local path to each one to the DataFiles property. Throws exception if download fails for any reason
  73. /// </summary>
  74. /// <param name="updaterPath"></param>
  75. /// <param name="fileNames"></param>
  76. private List<string> performDownloads(string updaterPath, string[] fileNames) {
  77. string args = @"/conn last /download ""GRIN-Global Data Files"" """ + String.Join(@""" """, fileNames) + @"""";
  78. // MessageBox.Show(args);
  79. var output = Utility.Run((updaterPath + @"\GrinGlobal.Updater.exe").Replace(@"\\", @"\"), args, false, true);
  80. if (!output.StartsWith("FILES:")) {
  81. // failure
  82. throw new InvalidOperationException(getDisplayMember("performDownloads{download}", "Error downloading data files: {0}", output.Replace("ERROR:", "")));
  83. } else {
  84. // success
  85. var paths = output.Trim().Split('|');
  86. if (paths.Length < 2) {
  87. // no valid file paths given.
  88. if (paths.Length == 1) {
  89. throw new InvalidOperationException(getDisplayMember("performDownloads{paths}", "Error downloading data:\n{0}", paths[0]));
  90. } else {
  91. throw new InvalidOperationException(getDisplayMember("performDownlaods{unknown}", "Unknown error occurred when downloading data"));
  92. }
  93. } else {
  94. // fill list of datafiles with the local path to the files we downloaded.
  95. DataFiles = new List<string>();
  96. for (var i = 1; i < paths.Length; i++) {
  97. var newPath = paths[i].Trim();
  98. // MessageBox.Show("Filename: '" + newPath + "'");
  99. DataFiles.Add(newPath);
  100. }
  101. return DataFiles;
  102. }
  103. }
  104. }
  105. private void btnContinue_Click(object sender, EventArgs e) {
  106. var updaterPath = "<not filled yet>";
  107. try {
  108. this.Cursor = Cursors.WaitCursor;
  109. this.Enabled = false;
  110. // get location of GrinGlobal.Updater.exe
  111. updaterPath = Utility.GetGrinGlobalApplicationInstallPath("Updater", @"<registry key not found>");
  112. // remember files to download
  113. var filesToDownload = determineFilesToDownload(false);
  114. if (filesToDownload.Count == 0) {
  115. // MessageBox.Show("No files to download, continuing...");
  116. DialogResult = DialogResult.OK;
  117. Close();
  118. } else {
  119. performDownloads(updaterPath, filesToDownload.ToArray());
  120. DialogResult = DialogResult.OK;
  121. Close();
  122. }
  123. } catch (Exception ex) {
  124. MessageBox.Show(getDisplayMember("Continue{failed}", "{0}\nPath to Updater: {1}", ex.Message, updaterPath));
  125. } finally {
  126. this.Cursor = Cursors.Default;
  127. this.Enabled = true;
  128. }
  129. }
  130. private void recurseToggleChildren(TreeNode parent, bool chk) {
  131. foreach (TreeNode child in parent.Nodes) {
  132. child.Checked = chk;
  133. recurseToggleChildren(child, chk);
  134. }
  135. }
  136. bool _syncing;
  137. private void treeView1_AfterCheck(object sender, TreeViewEventArgs e) {
  138. if (!_syncing) {
  139. try {
  140. _syncing = true;
  141. if (!e.Node.Checked) {
  142. // auto-uncheck all children as their parent isn't checked
  143. recurseToggleChildren(e.Node, e.Node.Checked);
  144. } else {
  145. // auto-check all ancestors as they have a descendent checked
  146. var nd = e.Node;
  147. while (nd.Parent != null) {
  148. nd.Parent.Checked = nd.Checked;
  149. nd = nd.Parent;
  150. }
  151. }
  152. } finally {
  153. _syncing = false;
  154. }
  155. }
  156. }
  157. private void btnCancel_Click(object sender, EventArgs e) {
  158. DialogResult = DialogResult.Cancel;
  159. this.Close();
  160. }
  161. private void frmInstallData_FormClosing(object sender, FormClosingEventArgs e) {
  162. }
  163. private List<string> determineFilesToDownload(bool forceDownload) {
  164. var sel = new List<string>();
  165. foreach (TreeNode nd in getAllNodes(treeView1.Nodes)) {
  166. if (nd.Checked || forceDownload) {
  167. sel.Add((nd.Tag ?? "").ToString());
  168. }
  169. }
  170. return sel;
  171. }
  172. private static string getDisplayMember(string resourceName, string defaultValue, params string[] substitutes) {
  173. return ResourceHelper.GetDisplayMember(null, "DatabaseCopier", "frmInstallData", resourceName, null, defaultValue, substitutes);
  174. }
  175. }
  176. }