PageRenderTime 50ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/source/library/Interlace/DatabaseManagement/WindowsDatabaseUpgrader.cs

https://bitbucket.org/VahidN/interlace
C# | 424 lines | 312 code | 53 blank | 59 comment | 13 complexity | 5c8e75d8ea3d52e9858c573f16142750 MD5 | raw file
  1. #region Using Directives and Copyright Notice
  2. // Copyright (c) 2006-2010, Bit Plantation (ABN 80 332 904 638)
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. // * Redistributions of source code must retain the above copyright
  8. // notice, this list of conditions and the following disclaimer.
  9. // * Redistributions in binary form must reproduce the above copyright
  10. // notice, this list of conditions and the following disclaimer in the
  11. // documentation and/or other materials provided with the distribution.
  12. // * Neither the name of the Computer Consultancy Pty Ltd nor the
  13. // names of its contributors may be used to endorse or promote products
  14. // derived from this software without specific prior written permission.
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. // ARE DISCLAIMED. IN NO EVENT SHALL COMPUTER CONSULTANCY PTY LTD BE LIABLE
  20. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  26. // DAMAGE.
  27. using System;
  28. using System.Drawing;
  29. using System.Collections;
  30. using System.ComponentModel;
  31. using System.Windows.Forms;
  32. using System.Data;
  33. #endregion
  34. namespace Interlace.DatabaseManagement
  35. {
  36. public class WindowsDatabaseUpgrader : System.Windows.Forms.Form
  37. {
  38. private System.Windows.Forms.PictureBox NormalIcon;
  39. private System.Windows.Forms.Label Prompt;
  40. private System.Windows.Forms.PictureBox WaitIcon;
  41. private System.Windows.Forms.PictureBox ErrorIcon;
  42. private System.Windows.Forms.ProgressBar ProgressBar;
  43. private System.Windows.Forms.Label ProgressLabel;
  44. private System.Windows.Forms.Button NextButton;
  45. private System.Windows.Forms.Button LeaveButton;
  46. private System.ComponentModel.Container components = null;
  47. public WindowsDatabaseUpgrader()
  48. {
  49. InitializeComponent();
  50. }
  51. DatabaseManager _manager = null;
  52. DatabaseManager.UpgradeDelegate _upgrade;
  53. string _fromVersion;
  54. string _toVersion;
  55. public static bool CheckDatabase(IDbConnection connection, IDatabaseImplementation implementation, IDatabaseSchemaFactory databaseSchemaFactory, string requiredProduct, string requiredVersion)
  56. {
  57. DatabaseManager manager = new DatabaseManager(connection, implementation, databaseSchemaFactory, requiredProduct);
  58. string fromVersion;
  59. // Get the current version of the database:
  60. if (manager.VersionTableExists())
  61. {
  62. try
  63. {
  64. DatabaseVersion version = manager.GetVersion();
  65. fromVersion = version.Version;
  66. }
  67. catch (DatabaseVersionTableInvalidException ex)
  68. {
  69. using (WindowsDatabaseUpgrader upgrader = new WindowsDatabaseUpgrader())
  70. {
  71. upgrader.ShowError("Database Version Problem", ex.Message);
  72. upgrader.ShowDialog();
  73. return false;
  74. }
  75. }
  76. }
  77. else
  78. {
  79. fromVersion = DatabaseManager.UninitialisedVersion;
  80. }
  81. // Exit unless an upgrade is needed:
  82. if (fromVersion == requiredVersion) return true;
  83. // Try to upgrade:
  84. if (!manager.CanUpgrade(fromVersion, requiredVersion))
  85. {
  86. using (WindowsDatabaseUpgrader upgrader = new WindowsDatabaseUpgrader())
  87. {
  88. upgrader.ShowError("No Upgrade Available", String.Format(
  89. "There is no upgrade available from version {0} to " +
  90. "version {1}. This database can not be used.",
  91. fromVersion, requiredVersion));
  92. upgrader.ShowDialog();
  93. return false;
  94. }
  95. }
  96. using (WindowsDatabaseUpgrader upgrader = new WindowsDatabaseUpgrader())
  97. {
  98. upgrader._manager = manager;
  99. upgrader._fromVersion = fromVersion;
  100. upgrader._toVersion = requiredVersion;
  101. DialogResult result = upgrader.ShowDialog();
  102. return result == DialogResult.OK;
  103. }
  104. }
  105. private void DatabaseUpgrader_Load(object sender, System.EventArgs e)
  106. {
  107. if (_manager != null)
  108. {
  109. if (_fromVersion == DatabaseManager.UninitialisedVersion)
  110. {
  111. ShowInitialisation();
  112. }
  113. else
  114. {
  115. ShowUpgrade();
  116. }
  117. }
  118. }
  119. private void NextButton_Click(object sender, System.EventArgs e)
  120. {
  121. _upgrade = new DatabaseManager.UpgradeDelegate(_manager.Upgrade);
  122. ShowUpgrading();
  123. _upgrade.BeginInvoke(_fromVersion, _toVersion,
  124. new DatabaseManager.UpgradeProgressDelegate(UpgradeProgressInvoke),
  125. new AsyncCallback(UpdateCompletedInvoke), null);
  126. }
  127. private void UpdateCompleted(IAsyncResult result)
  128. {
  129. try
  130. {
  131. _upgrade.EndInvoke(result);
  132. }
  133. catch (Exception)
  134. {
  135. ShowError("Upgrading Error",
  136. "An error occurred while upgrading the database.");
  137. return;
  138. }
  139. DialogResult = DialogResult.OK;
  140. }
  141. private void UpdateCompletedInvoke(IAsyncResult result)
  142. {
  143. System.Threading.Thread.Sleep(new TimeSpan(0, 0, 3));
  144. Invoke(new AsyncCallback(UpdateCompleted), new object[] {result});
  145. }
  146. protected void UpgradeProgress(int commandsRun, int totalCommands)
  147. {
  148. ProgressBar.Value = commandsRun;
  149. ProgressBar.Minimum = 0;
  150. ProgressBar.Maximum = totalCommands;
  151. if (commandsRun < totalCommands)
  152. {
  153. ProgressLabel.Text = String.Format("{0} of {1} commands executed.",
  154. commandsRun, totalCommands);
  155. }
  156. else
  157. {
  158. ProgressLabel.Text = "All commands executed.";
  159. }
  160. }
  161. protected void UpgradeProgressInvoke(int commandsRun, int totalCommands)
  162. {
  163. Invoke(new DatabaseManager.UpgradeProgressDelegate(
  164. UpgradeProgress), new object[] {commandsRun, totalCommands});
  165. }
  166. protected override void Dispose(bool disposing)
  167. {
  168. if (disposing)
  169. {
  170. if (components != null)
  171. {
  172. components.Dispose();
  173. }
  174. }
  175. base.Dispose(disposing);
  176. }
  177. public void ShowInitialisation()
  178. {
  179. Text = "Database Initialisation Required";
  180. NormalIcon.Visible = true;
  181. ErrorIcon.Visible = false;
  182. WaitIcon.Visible = false;
  183. Prompt.Text = "The database you have connected to has not been prepared for use " +
  184. "by this application. Click continue to prepare the database and start using it.";
  185. ProgressBar.Visible = false;
  186. ProgressLabel.Visible = false;
  187. NextButton.Visible = true;
  188. NextButton.Text = "&Continue";
  189. LeaveButton.Visible = true;
  190. LeaveButton.Text = "&Cancel";
  191. }
  192. public void ShowUpgrade()
  193. {
  194. Text = "Database Upgrade Required";
  195. NormalIcon.Visible = true;
  196. ErrorIcon.Visible = false;
  197. WaitIcon.Visible = false;
  198. Prompt.Text = "The database you have connected to must be upgraded to work with " +
  199. "this application. Click continue to upgrade the database and start using it.";
  200. ProgressBar.Visible = false;
  201. ProgressLabel.Visible = false;
  202. NextButton.Visible = true;
  203. NextButton.Text = "&Continue";
  204. LeaveButton.Visible = true;
  205. LeaveButton.Text = "&Cancel";
  206. }
  207. public void ShowWaiting()
  208. {
  209. Text = "Upgrading Database";
  210. NormalIcon.Visible = false;
  211. ErrorIcon.Visible = false;
  212. WaitIcon.Visible = true;
  213. Prompt.Text = "The database is in use by one or more other computers. Close this " +
  214. "application on all computers to continue.";
  215. ProgressBar.Visible = false;
  216. ProgressLabel.Visible = false;
  217. NextButton.Visible = false;
  218. LeaveButton.Visible = true;
  219. LeaveButton.Text = "&Cancel";
  220. }
  221. public void ShowUpgrading()
  222. {
  223. Text = "Upgrading Database";
  224. NormalIcon.Visible = true;
  225. ErrorIcon.Visible = false;
  226. WaitIcon.Visible = false;
  227. Prompt.Text = "The database is currently being updated.";
  228. ProgressBar.Visible = true;
  229. ProgressLabel.Visible = true;
  230. NextButton.Visible = false;
  231. LeaveButton.Visible = false;
  232. }
  233. public void ShowError(string title, string message)
  234. {
  235. Text = title;
  236. NormalIcon.Visible = false;
  237. ErrorIcon.Visible = true;
  238. WaitIcon.Visible = false;
  239. Prompt.Text = message;
  240. ProgressBar.Visible = false;
  241. ProgressLabel.Visible = false;
  242. NextButton.Visible = false;
  243. LeaveButton.Visible = true;
  244. LeaveButton.Text = "&Close";
  245. }
  246. #region Windows Form Designer generated code
  247. /// <summary>
  248. /// Required method for Designer support - do not modify
  249. /// the contents of this method with the code editor.
  250. /// </summary>
  251. private void InitializeComponent()
  252. {
  253. System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(WindowsDatabaseUpgrader));
  254. this.NormalIcon = new System.Windows.Forms.PictureBox();
  255. this.Prompt = new System.Windows.Forms.Label();
  256. this.WaitIcon = new System.Windows.Forms.PictureBox();
  257. this.ErrorIcon = new System.Windows.Forms.PictureBox();
  258. this.ProgressBar = new System.Windows.Forms.ProgressBar();
  259. this.ProgressLabel = new System.Windows.Forms.Label();
  260. this.NextButton = new System.Windows.Forms.Button();
  261. this.LeaveButton = new System.Windows.Forms.Button();
  262. ((System.ComponentModel.ISupportInitialize)(this.NormalIcon)).BeginInit();
  263. ((System.ComponentModel.ISupportInitialize)(this.WaitIcon)).BeginInit();
  264. ((System.ComponentModel.ISupportInitialize)(this.ErrorIcon)).BeginInit();
  265. this.SuspendLayout();
  266. //
  267. // NormalIcon
  268. //
  269. this.NormalIcon.Image = ((System.Drawing.Image)(resources.GetObject("NormalIcon.Image")));
  270. this.NormalIcon.Location = new System.Drawing.Point(8, 8);
  271. this.NormalIcon.Name = "NormalIcon";
  272. this.NormalIcon.Size = new System.Drawing.Size(32, 32);
  273. this.NormalIcon.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
  274. this.NormalIcon.TabIndex = 0;
  275. this.NormalIcon.TabStop = false;
  276. this.NormalIcon.Visible = false;
  277. //
  278. // Prompt
  279. //
  280. this.Prompt.Location = new System.Drawing.Point(48, 8);
  281. this.Prompt.Name = "Prompt";
  282. this.Prompt.Size = new System.Drawing.Size(384, 48);
  283. this.Prompt.TabIndex = 1;
  284. this.Prompt.Text = "(Prompt)";
  285. //
  286. // WaitIcon
  287. //
  288. this.WaitIcon.Image = ((System.Drawing.Image)(resources.GetObject("WaitIcon.Image")));
  289. this.WaitIcon.Location = new System.Drawing.Point(8, 8);
  290. this.WaitIcon.Name = "WaitIcon";
  291. this.WaitIcon.Size = new System.Drawing.Size(32, 32);
  292. this.WaitIcon.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
  293. this.WaitIcon.TabIndex = 2;
  294. this.WaitIcon.TabStop = false;
  295. this.WaitIcon.Visible = false;
  296. //
  297. // ErrorIcon
  298. //
  299. this.ErrorIcon.Image = ((System.Drawing.Image)(resources.GetObject("ErrorIcon.Image")));
  300. this.ErrorIcon.Location = new System.Drawing.Point(8, 8);
  301. this.ErrorIcon.Name = "ErrorIcon";
  302. this.ErrorIcon.Size = new System.Drawing.Size(32, 32);
  303. this.ErrorIcon.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
  304. this.ErrorIcon.TabIndex = 3;
  305. this.ErrorIcon.TabStop = false;
  306. this.ErrorIcon.Visible = false;
  307. //
  308. // ProgressBar
  309. //
  310. this.ProgressBar.Location = new System.Drawing.Point(48, 56);
  311. this.ProgressBar.Name = "ProgressBar";
  312. this.ProgressBar.Size = new System.Drawing.Size(320, 16);
  313. this.ProgressBar.TabIndex = 4;
  314. //
  315. // ProgressLabel
  316. //
  317. this.ProgressLabel.Location = new System.Drawing.Point(48, 80);
  318. this.ProgressLabel.Name = "ProgressLabel";
  319. this.ProgressLabel.Size = new System.Drawing.Size(320, 16);
  320. this.ProgressLabel.TabIndex = 5;
  321. this.ProgressLabel.Text = "(ProgressLabel)";
  322. //
  323. // NextButton
  324. //
  325. this.NextButton.Location = new System.Drawing.Point(256, 104);
  326. this.NextButton.Name = "NextButton";
  327. this.NextButton.Size = new System.Drawing.Size(80, 23);
  328. this.NextButton.TabIndex = 6;
  329. this.NextButton.Text = "(Next)";
  330. this.NextButton.Click += new System.EventHandler(this.NextButton_Click);
  331. //
  332. // LeaveButton
  333. //
  334. this.LeaveButton.Location = new System.Drawing.Point(344, 104);
  335. this.LeaveButton.Name = "LeaveButton";
  336. this.LeaveButton.Size = new System.Drawing.Size(80, 23);
  337. this.LeaveButton.TabIndex = 7;
  338. this.LeaveButton.Text = "(Leave)";
  339. this.LeaveButton.Click += new System.EventHandler(this.LeaveButton_Click);
  340. //
  341. // DatabaseUpgrader
  342. //
  343. this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  344. this.ClientSize = new System.Drawing.Size(432, 134);
  345. this.ControlBox = false;
  346. this.Controls.Add(this.LeaveButton);
  347. this.Controls.Add(this.NextButton);
  348. this.Controls.Add(this.ProgressLabel);
  349. this.Controls.Add(this.ProgressBar);
  350. this.Controls.Add(this.ErrorIcon);
  351. this.Controls.Add(this.WaitIcon);
  352. this.Controls.Add(this.Prompt);
  353. this.Controls.Add(this.NormalIcon);
  354. this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
  355. this.Name = "DatabaseUpgrader";
  356. this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
  357. this.Text = "Database Upgrade Required";
  358. this.Load += new System.EventHandler(this.DatabaseUpgrader_Load);
  359. ((System.ComponentModel.ISupportInitialize)(this.NormalIcon)).EndInit();
  360. ((System.ComponentModel.ISupportInitialize)(this.WaitIcon)).EndInit();
  361. ((System.ComponentModel.ISupportInitialize)(this.ErrorIcon)).EndInit();
  362. this.ResumeLayout(false);
  363. this.PerformLayout();
  364. }
  365. #endregion
  366. private void LeaveButton_Click(object sender, System.EventArgs e)
  367. {
  368. DialogResult = DialogResult.Cancel;
  369. }
  370. }
  371. }