362 lines
14 KiB
C#
362 lines
14 KiB
C#
///
|
|
/// Copyright (c) 2013-2019 Sensus Metering Systems
|
|
///
|
|
using System;
|
|
using System.Windows.Forms;
|
|
using System.Drawing;
|
|
using System.IO;
|
|
using System.Diagnostics;
|
|
using log4net;
|
|
using TBF.Resources;
|
|
|
|
namespace TBF.Forms
|
|
{
|
|
/// <summary>
|
|
/// Dialog to process test bench name and database settings (BenchSettings)
|
|
/// </summary>
|
|
public partial class DatabaseSettingsDlg : Form
|
|
{
|
|
static readonly ILog log = LogManager.GetLogger(typeof(DatabaseSettingsDlg));
|
|
|
|
// Reference to bench DB settins (not a local copy)
|
|
Config.DatabaseSettings bench;
|
|
|
|
/// <summary>
|
|
/// Constructor
|
|
/// </summary>
|
|
/// <param name="bench">Test bench database settings</param>
|
|
public DatabaseSettingsDlg(Config.DatabaseSettings bench)
|
|
{
|
|
this.bench = bench;
|
|
|
|
InitializeComponent();
|
|
}
|
|
|
|
void Localize()
|
|
{
|
|
Text = Strings.Test_Bench_Databases_Specification;
|
|
label1.Text = Strings.Test_bench_name;
|
|
realBenchCheckBox.Text = Strings.Test_bench_is_controlled_by_this_computer;
|
|
dbTypeLabel.Text = Strings.Database_type;
|
|
connectionStringLabel.Text = Strings.Connection_string;
|
|
|
|
configLabel.Text = Strings.Configuration;
|
|
resultsLabel.Text = Strings.Results;
|
|
usersLabel.Text = Strings.Users;
|
|
|
|
testConnConfigDbBtn.Text = Strings.Test_connection;
|
|
testConnResultsDbBtn.Text = Strings.Test_connection;
|
|
testConnUsersDbBtn.Text = Strings.Test_connection;
|
|
|
|
createConfigDbBtn.Text = Strings.Create_DB;
|
|
createResultsDbBtn.Text = Strings.Create_DB;
|
|
|
|
okButton.Text = Strings.OkBtnText;
|
|
cancelButton.Text = Strings.CancelBtnText;
|
|
}
|
|
|
|
private void BenchSettingsDlg_Load(object sender, EventArgs e)
|
|
{
|
|
Localize();
|
|
|
|
benchNameTextBox.Text = bench.BenchName;
|
|
realBenchCheckBox.Checked = bench.IsRealBench;
|
|
|
|
/// Initialize 'Database type' combo-boxes
|
|
for (int i = 0; i < (int)Users.Entities.DBType.Count; i++)
|
|
{
|
|
Users.Entities.DBType dbType = (Users.Entities.DBType)i;
|
|
configDbTypeComboBox.Items.Add(dbType);
|
|
resultsDbTypeComboBox.Items.Add(dbType);
|
|
usersDbTypeComboBox.Items.Add(dbType);
|
|
}
|
|
configDbTypeComboBox.SelectedIndex = (int)bench.ProceduresDBSettings.DbType;
|
|
resultsDbTypeComboBox.SelectedIndex = (int)bench.WaterMetersDBSettings.DbType;
|
|
usersDbTypeComboBox.SelectedIndex = (int)bench.UsersDBSettings.DbType;
|
|
|
|
/// Initialize connection strings
|
|
configDbConnectionStringTextBox.Text = bench.ProceduresDBSettings.ConnectionString;
|
|
resultsDbConnectionStringTextBox.Text = bench.WaterMetersDBSettings.ConnectionString;
|
|
usersDbConnectionStringTextBox.Text = bench.UsersDBSettings.ConnectionString;
|
|
}
|
|
|
|
bool UpdateBenchFromUIControls()
|
|
{
|
|
/// TODO: The following code doesnot work as expected - error message shown!
|
|
//for (int i = 0; i < Program.LocalSettings.BenchesCount; i++)
|
|
//{
|
|
// if (bench != Program.LocalSettings.TestBenches[i] &&
|
|
// benchNameTextBox.Text == Program.LocalSettings.TestBenches[i].BenchName)
|
|
// {
|
|
// MessageBox.Show(Strings.Bench_name_conflict_Use_another_name_pls, Strings.Error, MessageBoxButtons.OK);
|
|
// return false;
|
|
// }
|
|
//}
|
|
|
|
bench.BenchName = benchNameTextBox.Text;
|
|
bench.IsRealBench = realBenchCheckBox.Checked;
|
|
|
|
bench.ProceduresDBSettings.ConnectionString = configDbConnectionStringTextBox.Text;
|
|
bench.WaterMetersDBSettings.ConnectionString = resultsDbConnectionStringTextBox.Text;
|
|
bench.UsersDBSettings.ConnectionString = usersDbConnectionStringTextBox.Text;
|
|
|
|
bench.ProceduresDBSettings.DbType = (Users.Entities.DBType)configDbTypeComboBox.SelectedIndex;
|
|
bench.WaterMetersDBSettings.DbType = (Users.Entities.DBType)resultsDbTypeComboBox.SelectedIndex;
|
|
bench.UsersDBSettings.DbType = (Users.Entities.DBType)usersDbTypeComboBox.SelectedIndex;
|
|
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update bench DB setting, return 'OK'
|
|
/// </summary>
|
|
/// <param name="sender">Not used</param>
|
|
/// <param name="e">Not used</param>
|
|
private void okButton_Click(object sender, EventArgs e)
|
|
{
|
|
if (UpdateBenchFromUIControls())
|
|
{
|
|
DialogResult = DialogResult.OK;
|
|
Close();
|
|
return;
|
|
}
|
|
|
|
DialogResult = DialogResult.None;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Throw away changes, return 'Cancel'
|
|
/// </summary>
|
|
/// <param name="sender">Not used</param>
|
|
/// <param name="e">Not used</param>
|
|
private void cancelButton_Click(object sender, EventArgs e)
|
|
{
|
|
DialogResult = DialogResult.Cancel;
|
|
Close();
|
|
}
|
|
|
|
|
|
private void testConnConfigDbBtn_Click(object sender, EventArgs e)
|
|
{
|
|
}
|
|
|
|
private void testConnResultsDbBtn_Click(object sender, EventArgs e)
|
|
{
|
|
}
|
|
|
|
private void testConnUsersDbBtn_Click(object sender, EventArgs e)
|
|
{
|
|
|
|
}
|
|
|
|
|
|
private void createConfigDbBtn_Click(object sender, EventArgs e)
|
|
{
|
|
/// Update database connection strings
|
|
if (!UpdateBenchFromUIControls()) return;
|
|
|
|
if (DialogResult.OK == MessageBox.Show(Strings.DB_will_be_overwritten + Environment.NewLine +
|
|
Strings.Do_you_want_to_proceed, Strings.Warning,
|
|
MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation))
|
|
{
|
|
try
|
|
{
|
|
Cursor.Current = Cursors.WaitCursor;
|
|
Config.FluentCommon.CreateEmptyConfigDB(bench.ProceduresDBSettings.DbType, bench.ProceduresDBSettings.ConnectionString);
|
|
MessageBox.Show(Strings.DB_was_successfully_created, Strings.Notification);
|
|
Cursor.Current = Cursors.Default;
|
|
DialogResult = DialogResult.None;
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Cursor.Current = Cursors.Default;
|
|
MessageBox.Show(Strings.Error_while_creating_DB_Reason + exception.Message, Strings.Error);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void createResultsDbBtn_Click(object sender, EventArgs e)
|
|
{
|
|
/// Update database connection strings
|
|
if (!UpdateBenchFromUIControls()) return;
|
|
|
|
if (DialogResult.OK == MessageBox.Show(Strings.DB_will_be_overwritten + Environment.NewLine +
|
|
Strings.Do_you_want_to_proceed, Strings.Warning,
|
|
MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation))
|
|
{
|
|
try
|
|
{
|
|
Cursor.Current = Cursors.WaitCursor;
|
|
Results.DB.DbType = bench.WaterMetersDBSettings.DbType;
|
|
Results.DB.ConnectionString = bench.WaterMetersDBSettings.ConnectionString;
|
|
Results.DB.CreateEmptyDB();
|
|
MessageBox.Show(Strings.DB_was_successfully_created, Strings.Notification);
|
|
Cursor.Current = Cursors.Default;
|
|
DialogResult = DialogResult.None;
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Cursor.Current = Cursors.Default;
|
|
string msg = Strings.Error_while_creating_DB_Reason + Environment.NewLine + exception.Message;
|
|
if (exception.InnerException != null)
|
|
{
|
|
msg += Environment.NewLine + exception.InnerException.Message;
|
|
}
|
|
MessageBox.Show(msg, Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
}
|
|
|
|
//private void createUsersDbBtn_Click(object sender, EventArgs e)
|
|
//{
|
|
// /// Update database connection strings
|
|
// if (!UpdateBenchFromUIControls()) return;
|
|
|
|
// if (DialogResult.OK == MessageBox.Show(Strings.DB_will_be_overwritten + Environment.NewLine +
|
|
// Strings.Do_you_want_to_proceed, Strings.Warning,
|
|
// MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation))
|
|
// {
|
|
// try
|
|
// {
|
|
// Cursor.Current = Cursors.WaitCursor;
|
|
// Users.DB.DbType = bench.UsersDBSettings.DbType;
|
|
// Users.DB.ConnectionString = bench.UsersDBSettings.ConnectionString;
|
|
// Users.DB.CreateEmptyDB();
|
|
// MessageBox.Show(Strings.DB_was_successfully_created, Strings.Notification);
|
|
// Cursor.Current = Cursors.Default;
|
|
// DialogResult = DialogResult.None;
|
|
// }
|
|
// catch (Exception exception)
|
|
// {
|
|
// Cursor.Current = Cursors.Default;
|
|
// string msg = Strings.Error_while_creating_DB_Reason + Environment.NewLine + exception.Message;
|
|
// if (exception.InnerException != null)
|
|
// {
|
|
// msg += Environment.NewLine + exception.InnerException.Message;
|
|
// }
|
|
// MessageBox.Show(msg, Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
// }
|
|
// }
|
|
//}
|
|
|
|
|
|
/// <summary>
|
|
/// Configuration database import (UI)
|
|
/// </summary>
|
|
private void importConfigDbBtn_Click(object sender, EventArgs e)
|
|
{
|
|
/// Update database connection strings
|
|
if (!UpdateBenchFromUIControls()) return;
|
|
|
|
if (MessageBox.Show(Strings.Do_you_want_to_restore_configuration_qm,
|
|
string.Empty,
|
|
MessageBoxButtons.YesNo,
|
|
MessageBoxIcon.Question) == DialogResult.Yes)
|
|
{
|
|
OpenFileDialog dlg = new OpenFileDialog();
|
|
dlg.InitialDirectory = string.Format(@"C:\TBF\DbBackups\");
|
|
if (dlg.ShowDialog() != DialogResult.OK) return;
|
|
|
|
DBImportMessageStart("Importing database with configuration");
|
|
ImportDatabase(Config.Data.CurrentBench.ProceduresDBSettings.ConnectionString, dlg.FileName);
|
|
DBImportMessageEnd();
|
|
}
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// Results database import (UI)
|
|
/// </summary>
|
|
private void importResultsDbBtn_Click(object sender, EventArgs e)
|
|
{
|
|
/// Update database connection strings
|
|
if (!UpdateBenchFromUIControls()) return;
|
|
|
|
if (MessageBox.Show(Strings.Do_you_want_to_restore_results_qm,
|
|
string.Empty,
|
|
MessageBoxButtons.YesNo,
|
|
MessageBoxIcon.Question) == DialogResult.Yes)
|
|
{
|
|
OpenFileDialog dlg = new OpenFileDialog();
|
|
dlg.InitialDirectory = string.Format(@"C:\TBF\DbBackups\");
|
|
if (dlg.ShowDialog() != DialogResult.OK) return;
|
|
|
|
DBImportMessageStart("Importing database with results");
|
|
ImportDatabase(Config.Data.CurrentBench.WaterMetersDBSettings.ConnectionString, dlg.FileName);
|
|
DBImportMessageEnd();
|
|
}
|
|
}
|
|
|
|
TBF.Forms.ModelessActivityForm form;
|
|
System.Threading.Thread formThread;
|
|
///
|
|
void DBImportMessageStart(string message)
|
|
{
|
|
form = new TBF.Forms.ModelessActivityForm()
|
|
{
|
|
Message = message,
|
|
FontFamily = "Arial",
|
|
FontSize = 24,
|
|
FontStyle = FontStyle.Regular,
|
|
BackgroundColor = Color.RoyalBlue,
|
|
};
|
|
formThread = new System.Threading.Thread(() => form.ShowDialog());
|
|
formThread.Start();
|
|
}
|
|
///
|
|
void DBImportMessageEnd()
|
|
{
|
|
if (form != null && formThread != null)
|
|
{
|
|
form.CloseForm(null, new EventArgs());
|
|
formThread.Join();
|
|
}
|
|
form = null;
|
|
formThread = null;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Database import (functionality)
|
|
/// Invokes:
|
|
/// mysql -u[userName] --default-character-set=utf8 --database [databaseName]
|
|
/// SOURCE {SQL file name]
|
|
/// </summary>
|
|
/// <param name="connectionString">DB connection string</param>
|
|
/// <param name="outputFileName">Output file name</param>
|
|
void ImportDatabase(string connectionString, string inputFileName)
|
|
{
|
|
string databaseName = Config.Utils.GetDBName(connectionString);
|
|
string userName = Config.Utils.GetDBUser(connectionString);
|
|
string password = Config.Utils.GetDBPassword(connectionString); /// TODO: Use password
|
|
|
|
try
|
|
{
|
|
Process proc = new Process();
|
|
proc.StartInfo.FileName = @"C:\xampp\mysql\bin\mysql.exe";
|
|
proc.StartInfo.Arguments = string.Format("--user={0} --default-character-set=utf8 --database={2}", userName, password, databaseName);
|
|
proc.StartInfo.UseShellExecute = false; /// Must be false to redirect standard input or standard output
|
|
proc.StartInfo.RedirectStandardOutput = false;
|
|
proc.StartInfo.RedirectStandardInput = true;
|
|
proc.StartInfo.CreateNoWindow = false;
|
|
//proc.StartInfo.Verb = "runas"; /// To run mysql.exe as administrator
|
|
Debug.WriteLine("Ideme na to.");
|
|
proc.Start();
|
|
|
|
string command = string.Format("SOURCE {0}{1}", inputFileName, Environment.NewLine);
|
|
proc.StandardInput.Write(command);
|
|
Debug.Write(command);
|
|
|
|
proc.StandardInput.Write(string.Format("exit{0}", Environment.NewLine));
|
|
Debug.WriteLine("exit");
|
|
|
|
proc.WaitForExit();
|
|
log.ErrorFormat("Database {0} imported from file {1}", databaseName, inputFileName);
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
log.FatalFormat("Failed to import database {0} from file {1}: {2}", databaseName, inputFileName, exc.Message);
|
|
}
|
|
}
|
|
}
|
|
} |