2015-04-15 18:32:56 +00:00
|
|
|
///
|
2017-02-23 15:01:31 +00:00
|
|
|
/// Copyright (c) 2013-2017 Sensus Metering Systems
|
2015-04-15 18:32:56 +00:00
|
|
|
///
|
2014-07-28 07:54:35 +00:00
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using log4net;
|
2015-12-04 16:17:20 +00:00
|
|
|
using Config.Entities;
|
2017-03-19 09:37:06 +00:00
|
|
|
using Users;
|
2014-07-28 07:54:35 +00:00
|
|
|
using TBF.Resources;
|
|
|
|
|
using TBF.Forms;
|
|
|
|
|
|
|
|
|
|
namespace TBF
|
|
|
|
|
{
|
|
|
|
|
public class Program
|
|
|
|
|
{
|
2017-04-06 21:49:51 +00:00
|
|
|
public const string HomeDir = "C:\\Tbf\\"; /// Contains subdirectories Results, Logs, Images, ...
|
|
|
|
|
public const string ImagesDir = "C:\\Tbf\\Images\\";
|
2017-07-27 16:02:19 +00:00
|
|
|
public const string TempImagesDir = "C:\\Tbf\\Images\\Temp\\";
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
|
|
|
/// log4net
|
2016-01-26 12:39:51 +00:00
|
|
|
static ILog log;
|
2014-07-28 07:54:35 +00:00
|
|
|
const string log4netConfigFName = "log4netConfig.xml";
|
|
|
|
|
|
2016-02-18 00:08:14 +00:00
|
|
|
/// Program information
|
|
|
|
|
public static string Version; /// version string
|
|
|
|
|
public static DateTime BuildDateTime; /// date and time of program build
|
2015-11-12 23:03:33 +00:00
|
|
|
|
2014-07-28 07:54:35 +00:00
|
|
|
/// Local settings
|
2015-02-28 20:36:33 +00:00
|
|
|
public static LocalSettings LocalSettings;
|
2014-07-28 07:54:35 +00:00
|
|
|
public const string LocalSettingsFileName = "config.xml";
|
2015-02-28 20:36:33 +00:00
|
|
|
public const string LocalSettingsBackupName = "config.backup.xml";
|
2014-07-28 07:54:35 +00:00
|
|
|
|
2015-04-28 14:17:36 +00:00
|
|
|
public static System.Globalization.CultureInfo AltCulture;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
2015-09-26 18:59:04 +00:00
|
|
|
/// <summary>
|
2014-07-28 07:54:35 +00:00
|
|
|
/// Selected procedure data
|
|
|
|
|
/// - before the main program window is open, objects referenced by this variable
|
|
|
|
|
/// are loaded from the database.
|
|
|
|
|
/// - user interface displays data stored in objects referenced by this variable.
|
|
|
|
|
/// When user modifies procedure settings and confirms changes (choses to save them),
|
|
|
|
|
/// data referenced by this variable are updated and written to the database.
|
|
|
|
|
/// - when users starts a test procedure, state machine creates its own copy
|
|
|
|
|
/// of procedure data and uses them for the test. Changes of 'SelectedProcedure'
|
|
|
|
|
/// done during the test do not have any influence on the currently running test.
|
|
|
|
|
/// </summary>
|
2015-12-04 16:17:20 +00:00
|
|
|
public static Config.Entities.Procedure SelectedProcedure = null;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Main window object.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static MainWnd MainWnd;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The main entry point for the application.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[STAThread]
|
|
|
|
|
static void Main()
|
|
|
|
|
{
|
2015-02-28 20:36:33 +00:00
|
|
|
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler);
|
|
|
|
|
|
2015-11-12 23:03:33 +00:00
|
|
|
/// Program version string
|
2016-02-18 00:08:14 +00:00
|
|
|
System.Reflection.Assembly thisAssembly = System.Reflection.Assembly.GetExecutingAssembly();
|
|
|
|
|
Version ver = thisAssembly.GetName().Version;
|
2015-12-20 12:26:33 +00:00
|
|
|
Version = string.Format("{0}.{1}.{2}", ver.Major, ver.Minor, ver.Build);
|
2016-02-18 00:08:14 +00:00
|
|
|
BuildDateTime = new System.IO.FileInfo(thisAssembly.Location).LastWriteTime;
|
2015-11-12 23:03:33 +00:00
|
|
|
|
2014-10-02 18:56:24 +00:00
|
|
|
/// Local program configuration directory including the trailing backslash
|
2014-07-28 07:54:35 +00:00
|
|
|
string locAppDataDir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) +
|
|
|
|
|
Path.DirectorySeparatorChar + "TestBenchFramework" + Path.DirectorySeparatorChar;
|
|
|
|
|
|
|
|
|
|
/// Check whether local application data directory exists.
|
|
|
|
|
/// If not, create it and copy the initial configuration to this directory.
|
|
|
|
|
/// This is done only once after a clean program installation on the first program start-up
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (!Directory.Exists(locAppDataDir))
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(locAppDataDir);
|
|
|
|
|
Environment.CurrentDirectory = locAppDataDir;
|
|
|
|
|
|
|
|
|
|
string sampleConfig = Application.StartupPath + Path.DirectorySeparatorChar + "SampleConfig" + Path.DirectorySeparatorChar;
|
|
|
|
|
if (File.Exists(sampleConfig + LocalSettingsFileName)) { File.Copy(sampleConfig + LocalSettingsFileName, LocalSettingsFileName); }
|
|
|
|
|
if (File.Exists(sampleConfig + log4netConfigFName)) { File.Copy(sampleConfig + log4netConfigFName, log4netConfigFName); }
|
2015-12-04 16:17:20 +00:00
|
|
|
if (File.Exists(sampleConfig + Config.Data.SQLiteDbFName)) { File.Copy(sampleConfig + Config.Data.SQLiteDbFName, Config.Data.SQLiteDbFName); }
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
|
|
|
File.SetAttributes(LocalSettingsFileName, FileAttributes.Normal);
|
|
|
|
|
File.SetAttributes(log4netConfigFName, FileAttributes.Normal);
|
2015-12-04 16:17:20 +00:00
|
|
|
File.SetAttributes(Config.Data.SQLiteDbFName, FileAttributes.Normal);
|
2014-07-28 07:54:35 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Environment.CurrentDirectory = locAppDataDir;
|
|
|
|
|
}
|
2017-04-06 21:49:51 +00:00
|
|
|
|
|
|
|
|
if (!Directory.Exists(ImagesDir)) Directory.CreateDirectory(ImagesDir);
|
2017-07-27 16:02:19 +00:00
|
|
|
if (!Directory.Exists(TempImagesDir)) Directory.CreateDirectory(TempImagesDir);
|
|
|
|
|
|
|
|
|
|
IEnumerable<string> files = Directory.EnumerateFiles(TempImagesDir); /// TODO: Implement recursive directory delete
|
2017-04-06 21:49:51 +00:00
|
|
|
foreach (var file in files) File.Delete(file);
|
2014-07-28 07:54:35 +00:00
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
log.Error("A problem when creating the local application data directory and preparing the initial configuration", e);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-29 14:15:45 +00:00
|
|
|
/// Configure and start logging
|
2014-07-28 07:54:35 +00:00
|
|
|
log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(locAppDataDir + log4netConfigFName));
|
2016-01-26 12:39:51 +00:00
|
|
|
log = LogManager.GetLogger(typeof(Program));
|
2016-01-14 20:49:20 +00:00
|
|
|
log.Fatal("--------------------------------------------------------------------------------");
|
2016-01-26 12:39:51 +00:00
|
|
|
log.Fatal(string.Format("TBF ver.{0}", Version));
|
2014-07-28 07:54:35 +00:00
|
|
|
|
2015-02-28 20:36:33 +00:00
|
|
|
///
|
2014-07-28 07:54:35 +00:00
|
|
|
/// Load the local settings
|
2015-02-28 20:36:33 +00:00
|
|
|
///
|
|
|
|
|
Program.LocalSettings = LocalSettings.Load(Program.LocalSettingsFileName);
|
|
|
|
|
if (Program.LocalSettings == null || Program.LocalSettings.TestBenches == null)
|
|
|
|
|
{
|
|
|
|
|
/// Loading local seetings from regular config file failed. Use the backup
|
|
|
|
|
Program.LocalSettings = LocalSettings.Load(Program.LocalSettingsBackupName);
|
|
|
|
|
if (Program.LocalSettings == null || Program.LocalSettings.TestBenches == null)
|
|
|
|
|
{
|
2016-04-07 11:52:57 +00:00
|
|
|
log.FatalFormat("Local settings: Could not load file {0}, nor {1}.", Program.LocalSettingsFileName, Program.LocalSettingsBackupName);
|
|
|
|
|
log.Fatal("Application terminated.");
|
2015-02-28 20:36:33 +00:00
|
|
|
return; /// Fatal error
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
LocalSettings.Save(); /// Save the settings to overwrite the wrong file
|
2016-04-07 11:52:57 +00:00
|
|
|
log.FatalFormat("Local settings: Could not load file {0}, successfully loaded {1}", Program.LocalSettingsFileName, Program.LocalSettingsBackupName);
|
|
|
|
|
log.FatalFormat("BatchNr = {0}", LocalSettings.BatchNr);
|
2015-02-28 20:36:33 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/// Loading local seetings from the regular config file was successful. Update the backup
|
|
|
|
|
File.Copy(Program.LocalSettingsFileName, Program.LocalSettingsBackupName, true);
|
2016-04-07 11:52:57 +00:00
|
|
|
log.FatalFormat("Local settings: Successfully loaded from {0}", Program.LocalSettingsFileName);
|
|
|
|
|
log.FatalFormat("BatchNr = {0}", LocalSettings.BatchNr);
|
2015-02-28 20:36:33 +00:00
|
|
|
}
|
2014-07-28 07:54:35 +00:00
|
|
|
|
2016-04-07 11:52:57 +00:00
|
|
|
|
2015-03-27 06:27:36 +00:00
|
|
|
try
|
|
|
|
|
{
|
2015-04-13 09:04:18 +00:00
|
|
|
string culture = LocalSettings.Language.Replace('_', '-');
|
|
|
|
|
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(culture);
|
2015-03-27 06:27:36 +00:00
|
|
|
}
|
2015-04-10 10:59:26 +00:00
|
|
|
catch (Exception e)
|
2015-03-27 06:27:36 +00:00
|
|
|
{
|
2015-04-10 10:59:26 +00:00
|
|
|
string msg = e.Message;
|
2015-03-27 06:27:36 +00:00
|
|
|
MessageBox.Show("Selected language not supported.\nUsing English.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
|
2015-04-13 09:04:18 +00:00
|
|
|
System.Threading.Thread.CurrentThread.CurrentUICulture =
|
|
|
|
|
new System.Globalization.CultureInfo("en");
|
2015-03-27 06:27:36 +00:00
|
|
|
}
|
|
|
|
|
|
2015-04-28 14:17:36 +00:00
|
|
|
//AltCulture = null;
|
|
|
|
|
AltCulture = new System.Globalization.CultureInfo("SK");
|
2014-07-28 07:54:35 +00:00
|
|
|
|
2016-04-07 11:52:57 +00:00
|
|
|
|
2014-07-28 07:54:35 +00:00
|
|
|
/// This belongs to Application.Run(new MainWnd()) ...
|
|
|
|
|
/// and should be executed before opening 'loginDlg'
|
|
|
|
|
Application.EnableVisualStyles();
|
|
|
|
|
Application.SetCompatibleTextRenderingDefault(false);
|
|
|
|
|
|
2016-04-07 11:52:57 +00:00
|
|
|
|
2014-07-28 07:54:35 +00:00
|
|
|
/// Ensure that there is at least one bench in the configuration
|
2015-02-28 20:36:33 +00:00
|
|
|
while (Program.LocalSettings.BenchesCount == 0)
|
2014-07-28 07:54:35 +00:00
|
|
|
{
|
|
|
|
|
log.Warn("No test benches in the local configuration -> display an appropriate dialog.");
|
|
|
|
|
|
|
|
|
|
/// Ask what to do, ask for the password, open 'DatabaseSetingsDlg' and continue on OK
|
2015-02-19 14:50:31 +00:00
|
|
|
if ((new Forms.NoBenchOrDatabaseDlg { Message = Strings.NoBenchMsg }.ShowDialog() != DialogResult.OK) ||
|
2017-03-15 11:56:53 +00:00
|
|
|
(new Users.Forms.LoginDlg(true).ShowDialog() == DialogResult.Cancel) ||
|
2015-02-19 14:50:31 +00:00
|
|
|
(new BenchesDlg().ShowDialog() == DialogResult.Cancel))
|
2014-07-28 07:54:35 +00:00
|
|
|
{
|
|
|
|
|
return; /// Exit program
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.Warn("Loading the local settings again.");
|
2015-02-28 20:36:33 +00:00
|
|
|
Program.LocalSettings = LocalSettings.Load(Program.LocalSettingsFileName);
|
|
|
|
|
if (Program.LocalSettings == null) return; /// Fatal error
|
2014-07-28 07:54:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// User login
|
|
|
|
|
bool retryLogin = true; /// true = stay in a login loop
|
2016-04-07 11:52:57 +00:00
|
|
|
do
|
|
|
|
|
{
|
2015-02-19 14:50:31 +00:00
|
|
|
LoginDlgWithBenchSelection loginDlgBench;
|
2014-07-28 07:54:35 +00:00
|
|
|
if (LocalSettings.LastBenchName != null)
|
|
|
|
|
{
|
2015-02-19 14:50:31 +00:00
|
|
|
loginDlgBench = new LoginDlgWithBenchSelection(LocalSettings.LastBenchName);
|
2014-07-28 07:54:35 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2015-02-19 14:50:31 +00:00
|
|
|
loginDlgBench = new LoginDlgWithBenchSelection();
|
2014-07-28 07:54:35 +00:00
|
|
|
}
|
2016-12-21 08:27:25 +00:00
|
|
|
loginDlgBench.Method = LocalSettings.LoginMethod;
|
|
|
|
|
DialogResult dr = loginDlgBench.ShowDialog();
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
|
|
|
if (dr == DialogResult.Cancel)
|
|
|
|
|
{
|
|
|
|
|
return; /// Login canceled --> Exit application
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < LocalSettings.BenchesCount; i++)
|
|
|
|
|
{
|
2015-02-19 14:50:31 +00:00
|
|
|
if (LocalSettings.TestBenches[i].BenchName == loginDlgBench.BenchName)
|
2016-04-07 11:52:57 +00:00
|
|
|
{
|
|
|
|
|
log.FatalFormat("Test bench name: {0}", loginDlgBench.BenchName);
|
|
|
|
|
|
2017-03-19 09:37:06 +00:00
|
|
|
GlobalData.RemoteUsersDB = LocalSettings.TestBenches[i].UsersDBSettings;
|
|
|
|
|
GlobalData.LocalUsersDB = LocalSettings.TestBenches[i].ProceduresDBSettings;
|
|
|
|
|
|
2017-09-26 05:52:48 +00:00
|
|
|
Users.Entities.User loadedUser = null;
|
2014-07-28 07:54:35 +00:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
bool authorized = false;
|
2017-03-19 09:37:06 +00:00
|
|
|
Users.Entities.AuthorizedAs authorizedAs = Users.Entities.AuthorizedAs.PowerUser;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
2017-03-19 09:37:06 +00:00
|
|
|
if (Users.Entities.User.IsPowerUser(loginDlgBench.Alias, loginDlgBench.Password))
|
2015-02-19 14:50:31 +00:00
|
|
|
{
|
2017-03-19 09:37:06 +00:00
|
|
|
loadedUser = new Users.Entities.User(loginDlgBench.Alias, 6, true);
|
2016-12-21 08:22:30 +00:00
|
|
|
authorized = loadedUser.Authorize(loginDlgBench.Alias, loginDlgBench.Password);
|
2017-03-19 09:37:06 +00:00
|
|
|
if (authorized)
|
|
|
|
|
GlobalData.AuthorizedAs = Users.Entities.AuthorizedAs.PowerUser;
|
2015-02-19 14:50:31 +00:00
|
|
|
}
|
|
|
|
|
|
2016-12-20 09:16:08 +00:00
|
|
|
///
|
2017-03-19 09:37:06 +00:00
|
|
|
/// Authorisation using databases
|
2016-12-20 09:16:08 +00:00
|
|
|
///
|
2016-12-20 13:22:56 +00:00
|
|
|
if (!authorized)
|
|
|
|
|
{
|
2017-03-19 09:37:06 +00:00
|
|
|
DBSettings[] dbs = new DBSettings[] { GlobalData.RemoteUsersDB, GlobalData.LocalUsersDB };
|
|
|
|
|
|
|
|
|
|
authorizedAs = Users.Entities.AuthorizedAs.RemoteUser; /// Try remote DB first
|
2016-12-20 13:22:56 +00:00
|
|
|
|
2017-03-19 09:37:06 +00:00
|
|
|
foreach (var db in dbs)
|
2016-12-20 13:22:56 +00:00
|
|
|
{
|
2017-03-19 09:37:06 +00:00
|
|
|
/// Load and authorise user from the UsersDB database
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
switch (loginDlgBench.Method)
|
|
|
|
|
{
|
|
|
|
|
default:
|
|
|
|
|
case Users.Entities.LoginMethod.UserName:
|
|
|
|
|
loadedUser = Users.Entities.User.LoadUserByName(loginDlgBench.Alias, db);
|
|
|
|
|
if (loadedUser != null) authorized = loadedUser.Authorize(loginDlgBench.Alias, loginDlgBench.Password);
|
|
|
|
|
break;
|
|
|
|
|
case Users.Entities.LoginMethod.FullName:
|
|
|
|
|
loadedUser = Users.Entities.User.LoadUserByFullName(loginDlgBench.Alias, db);
|
|
|
|
|
if (loadedUser != null) authorized = loadedUser.AuthorizeFullName(loginDlgBench.Alias, loginDlgBench.Password);
|
|
|
|
|
break;
|
|
|
|
|
case Users.Entities.LoginMethod.Number:
|
|
|
|
|
int number;
|
|
|
|
|
if (!int.TryParse(loginDlgBench.Alias, out number)) break;
|
|
|
|
|
loadedUser = Users.Entities.User.LoadUserByNumber(number, db);
|
|
|
|
|
if (loadedUser != null) authorized = loadedUser.AuthorizeNumber(number, loginDlgBench.Password);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
authorized = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (authorized) break;
|
|
|
|
|
|
|
|
|
|
authorizedAs = Users.Entities.AuthorizedAs.LocalUser; /// Try local DB afterwards
|
2016-12-20 13:22:56 +00:00
|
|
|
}
|
|
|
|
|
}
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
|
|
|
if (authorized)
|
|
|
|
|
{
|
2017-09-26 05:52:48 +00:00
|
|
|
loadedUser.SetLegalizator(loginDlgBench.Legalizator);
|
|
|
|
|
Users.GlobalData.AuthorizedAs = authorizedAs;
|
2017-03-19 09:37:06 +00:00
|
|
|
|
|
|
|
|
LocalSettings.LoginMethod = loginDlgBench.Method; /// Save the login method actually used
|
2016-12-21 08:27:25 +00:00
|
|
|
|
2016-08-18 09:38:14 +00:00
|
|
|
if (LocalSettings.TestBenches[i].IsRealBench)
|
|
|
|
|
{
|
|
|
|
|
/// This is a real bench ==> check if another instance is running
|
|
|
|
|
string processName = System.IO.Path.GetFileNameWithoutExtension(thisAssembly.Location);
|
|
|
|
|
if (System.Diagnostics.Process.GetProcessesByName(processName).Length > 1)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(Strings.Another_instance_is_running, Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.InfoFormat("Password accepted, bench '{0}' parameters loaded.",
|
2015-12-04 16:17:20 +00:00
|
|
|
LocalSettings.TestBenches[i].BenchName);
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
|
|
|
/// Copy the selected bench settings to CurrentBench.
|
|
|
|
|
/// Clone() guarantees that current bench settings wont be modified
|
|
|
|
|
/// when user modifies the database settings in DatabaseSettingsDlg.
|
2015-12-04 16:17:20 +00:00
|
|
|
Config.Data.CurrentBench = (Config.DatabaseSettings)LocalSettings.TestBenches[i].Clone();
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
|
|
|
/// Save the selection to local settings
|
2015-12-04 16:17:20 +00:00
|
|
|
LocalSettings.LastBenchName = Config.Data.CurrentBench.BenchName;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
2015-12-31 23:00:03 +00:00
|
|
|
Results.DB.DbType = Config.Data.CurrentBench.WaterMetersDBSettings.DbType;
|
|
|
|
|
Results.DB.ConnectionString = Config.Data.CurrentBench.WaterMetersDBSettings.ConnectionString;
|
2016-01-01 23:54:21 +00:00
|
|
|
Results.DB.LoadSharedData();
|
2017-10-31 08:45:29 +00:00
|
|
|
int maxBatchNr = Results.DB.GetMaxSavedBatchNr();
|
2017-06-27 09:29:09 +00:00
|
|
|
if (!Config.Data.CurrentBench.IsRealBench || Program.LocalSettings.BatchNr <= maxBatchNr)
|
2017-10-31 08:45:29 +00:00
|
|
|
{
|
|
|
|
|
log.FatalFormat("Max. BatchNr in DB = {0}, LocalSettings.BatchNr = {1}", maxBatchNr, Program.LocalSettings.BatchNr);
|
|
|
|
|
Program.LocalSettings.BatchNr = maxBatchNr + 1;
|
|
|
|
|
Program.LocalSettings.Save();
|
|
|
|
|
log.FatalFormat("LocalSettings.BatchNr updated to {0}", Program.LocalSettings.BatchNr);
|
|
|
|
|
}
|
2015-12-31 23:00:03 +00:00
|
|
|
|
2016-07-08 14:10:04 +00:00
|
|
|
/// This is to trigger an exception in case of a power user and no Config database
|
2017-03-16 20:53:12 +00:00
|
|
|
IList<Procedure> listOfProcedures = Config.FluentCommon.CreateSession(Users.Entities.DBKind.Config)
|
2016-12-02 11:52:30 +00:00
|
|
|
.QueryOver<Procedure>()
|
|
|
|
|
.Where(x => (x.ProcedureState == ProcedureState.Active))
|
|
|
|
|
.And(x => (x.Name == LocalSettings.LastProcedureName))
|
|
|
|
|
.List();
|
2016-07-08 14:10:04 +00:00
|
|
|
|
2014-07-28 07:54:35 +00:00
|
|
|
retryLogin = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (retryLogin)
|
|
|
|
|
{
|
|
|
|
|
/// Close Fluent NHibernate
|
|
|
|
|
MessageBox.Show(Strings.Invalid_user_password_or_bench, Strings.Error, MessageBoxButtons.OK);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-16 11:06:48 +00:00
|
|
|
catch (Exception exc)
|
2014-07-28 07:54:35 +00:00
|
|
|
{
|
2017-05-16 11:06:48 +00:00
|
|
|
/// Database connect failed, create a log
|
|
|
|
|
log.FatalFormat("Connection to database failed: {0}", exc.Message);
|
|
|
|
|
if (exc.InnerException != null && !string.IsNullOrEmpty(exc.InnerException.Message))
|
|
|
|
|
{
|
|
|
|
|
log.FatalFormat("Inner exception: {0}", exc.InnerException.Message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Ask what to do
|
2014-07-28 07:54:35 +00:00
|
|
|
switch ((new Forms.NoBenchOrDatabaseDlg { Message = Strings.NoDatabaseMsg, ShowRetry = true }).ShowDialog())
|
|
|
|
|
{
|
2017-10-31 08:45:29 +00:00
|
|
|
case DialogResult.Abort:
|
|
|
|
|
return; /// Exit program
|
|
|
|
|
|
|
|
|
|
case DialogResult.Retry:
|
|
|
|
|
break; /// Retry connection to the database, stay inside the loop
|
|
|
|
|
|
|
|
|
|
case DialogResult.Yes:
|
|
|
|
|
new UpgradeSelectionDlg().ShowDialog();
|
|
|
|
|
break; /// Stay inside the loop
|
|
|
|
|
|
2014-07-28 07:54:35 +00:00
|
|
|
default:
|
|
|
|
|
/// Open 'DatabaseSetingsDlg' and retry DB connect on OK
|
2017-03-15 11:56:53 +00:00
|
|
|
if (new Users.Forms.LoginDlg(true).ShowDialog() == DialogResult.Cancel ||
|
2014-07-28 07:54:35 +00:00
|
|
|
new BenchesDlg().ShowDialog() == DialogResult.Cancel)
|
|
|
|
|
{
|
2017-10-31 08:45:29 +00:00
|
|
|
return; /// Exit program
|
2014-07-28 07:54:35 +00:00
|
|
|
}
|
2015-02-28 20:36:33 +00:00
|
|
|
Program.LocalSettings = LocalSettings.Load(Program.LocalSettingsFileName);
|
2017-10-31 08:45:29 +00:00
|
|
|
if (Program.LocalSettings == null)
|
|
|
|
|
return; /// Fatal error -> Exit program
|
|
|
|
|
else
|
|
|
|
|
break; /// Stay inside the loop
|
2014-07-28 07:54:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} // if benchname is OK
|
|
|
|
|
} // for all benches in local settings loop
|
|
|
|
|
} // not Canceled
|
|
|
|
|
} //do
|
|
|
|
|
while (retryLogin);
|
|
|
|
|
|
|
|
|
|
///
|
|
|
|
|
/// Load the last procedure or create a new, default one
|
|
|
|
|
///
|
|
|
|
|
if (LocalSettings.LastProcedureName != null)
|
|
|
|
|
{
|
2017-03-16 20:53:12 +00:00
|
|
|
IList<Procedure> listOfProcedures = Config.FluentCommon.CreateSession(Users.Entities.DBKind.Config)
|
2016-12-02 11:52:30 +00:00
|
|
|
.QueryOver<Procedure>()
|
|
|
|
|
.Where(x => (x.ProcedureState == ProcedureState.Active))
|
|
|
|
|
.And(x => (x.Name == LocalSettings.LastProcedureName))
|
|
|
|
|
.List();
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
|
|
|
if (listOfProcedures.Count == 1)
|
|
|
|
|
{
|
|
|
|
|
log.InfoFormat("Pre-selecting the procedure {0}", LocalSettings.LastProcedureName);
|
|
|
|
|
SelectedProcedure = listOfProcedures[0];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-28 20:36:33 +00:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
/// Open the main application window
|
|
|
|
|
log.Info("Creating the main window");
|
|
|
|
|
MainWnd = new MainWnd();
|
|
|
|
|
log.Info("Opening the main window");
|
|
|
|
|
Application.Run(MainWnd);
|
|
|
|
|
log.Info("The main window was closed");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
LogException(log, "Exception in Application.Run(MainWnd)", e);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
/// Save local settings
|
|
|
|
|
LocalSettings.Save();
|
|
|
|
|
}
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
|
|
|
/// Close Fluent NHibernate
|
|
|
|
|
/// Todo
|
|
|
|
|
|
|
|
|
|
log.Info("Exiting application.");
|
|
|
|
|
}
|
2015-02-28 20:36:33 +00:00
|
|
|
|
|
|
|
|
static void MyHandler(object sender, UnhandledExceptionEventArgs args)
|
|
|
|
|
{
|
|
|
|
|
LogException(log, "Unhandled exception", (Exception)args.ExceptionObject);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void LogException(ILog log, string description, Exception e)
|
|
|
|
|
{
|
|
|
|
|
log.FatalFormat("---------------( {0} )---------------", description);
|
|
|
|
|
log.FatalFormat("Message : {0}", e.Message);
|
|
|
|
|
if (e.InnerException != null)
|
|
|
|
|
{
|
|
|
|
|
log.FatalFormat("InnerMessage : {0}", e.InnerException.Message);
|
|
|
|
|
}
|
|
|
|
|
log.FatalFormat("StackTrace : {0}{1}", Environment.NewLine, e.StackTrace);
|
|
|
|
|
log.Fatal("--------------------------------------");
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-07-28 07:54:35 +00:00
|
|
|
}
|