/// <summary> /// Updates the current workstation's cache time stamp. /// </summary> /// <param name="timestamp"></param> public static void UpdateCacheTimeStamp(DateTime timestamp) { string _path = Application.StartupPath + "\\Xml\\cachesettings.xml"; DataTable _cachesettings = SCMS.XmlToTable(_path); if (_cachesettings != null) { CompanyInfo _company = SCMS.CurrentCompany; ServerConnectionInfo _server = SCMS.ServerConnection; if (_company == null) { _company = new CompanyInfo(SCMS.LastSelectedCompany); } if (_server == null) { _server = new ServerConnectionInfo(SCMS.LastSelectedConnection); } if (_cachesettings.Rows.Count > 0) { DataRow[] _rows = _cachesettings.Select("[Company] LIKE '" + _company.Company.ToSqlValidString(true) + "' AND\n" + "[Server] LIKE '" + _server.Server.ToSqlValidString(true) + "' AND\n" + "[Database] LIKE '" + _server.Database.ToSqlValidString(true) + "'"); if (_rows.Length > 0) { _rows[0]["LastRestored"] = timestamp; } else { object[] _values = new object[_cachesettings.Columns.Count]; DataColumnCollection _cols = _cachesettings.Columns; _values[_cols["Company"].Ordinal] = _company.Company; _values[_cols["Server"].Ordinal] = _server.Server; _values[_cols["Database"].Ordinal] = _server.Database; _values[_cols["LastRestored"].Ordinal] = timestamp; _cachesettings.Rows.Add(_values); } } else { object[] _values = new object[_cachesettings.Columns.Count]; DataColumnCollection _cols = _cachesettings.Columns; _values[_cols["Company"].Ordinal] = _company.Company; _values[_cols["Server"].Ordinal] = _server.Server; _values[_cols["Database"].Ordinal] = _server.Database; _values[_cols["LastRestored"].Ordinal] = timestamp; _cachesettings.Rows.Add(_values); } _cachesettings.AcceptChanges(); try { _cachesettings.WriteXml(_path, XmlWriteMode.WriteSchema); } catch { } _cachesettings.Dispose(); _cachesettings = null; Materia.RefreshAndManageCurrentProcess(); } }
private void btnSignIn_Click(object sender, EventArgs e) { if (!btnSignIn.Enabled) { return; } Validator _validator = SCMS.Validators[this]; pnlNotification.Hide(); if (!Materia.Valid(_validator, cboServers, cboServers.SelectedIndex >= 0, "Select a database server.")) { return; } if (!Materia.Valid(_validator, cboCompanies, cboCompanies.SelectedIndex >= 0, "Select access company.")) { return; } SCMS.ServerConnection = null; SCMS.Connection = null; Materia.RefreshAndManageCurrentProcess(); if (String.IsNullOrEmpty(txtUsername.Text.RLTrim()) || String.IsNullOrEmpty(txtPassword.Text.RLTrim())) { pnlNotification.Text = "Invalid account credentials."; pnlNotification.Show(); pnlNotification.BringToFront(); return; } ServerConnectionInfo _info = new ServerConnectionInfo(cboServers.SelectedValue.ToString()); pctLoad.Show(); pctLoad.BringToFront(); btnSignIn.Enabled = false; cboServers.Enabled = false; cboCompanies.Enabled = false; IDbConnection _connection = Database.CreateConnection(_info.ToString()); Func <IDbConnection, bool> _connectdelegate = new Func <IDbConnection, bool>(Materia.CanConnect); IAsyncResult _connectresult = _connectdelegate.BeginInvoke(_connection, null, _connectdelegate); while (!_connectresult.IsCompleted && !_cancelled) { Thread.Sleep(1); Application.DoEvents(); } bool _connected = false; if (_cancelled) { if (!_connectresult.IsCompleted) { try { _connectresult = null; } catch { } finally { Materia.RefreshAndManageCurrentProcess(); } } } else { _connected = _connectdelegate.EndInvoke(_connectresult); if (!_connected) { MsgBoxEx.Alert("Failed to connect to the specified database server.", "Database Connection"); } } if (!_connected) { if (_connection != null) { if (_connection.State == ConnectionState.Open) { try { _connection.Close(); } catch { } } try { _connection.Dispose(); } catch { } _connection = null; Materia.RefreshAndManageCurrentProcess(); } cboServers.Enabled = true; cboCompanies.Enabled = true; btnSignIn.Enabled = true; pctLoad.Hide(); return; } SCMS.Connection = _connection; SCMS.ServerConnection = new ServerConnectionInfo(cboServers.SelectedValue.ToString()); SCMS.CurrentCompany = new CompanyInfo(cboCompanies.SelectedValue.ToString()); Func <IDbConnection, string, DateTime> _getvaluedelegate = new Func <IDbConnection, string, DateTime>(Que.GetValue <DateTime>); IAsyncResult _getvalueresult = _getvaluedelegate.BeginInvoke(_connection, "SELECT MAX(`LastRestored`) AS `Date` FROM `settings`", null, _getvaluedelegate); _getvalueresult.WaitToFinish(); DateTime _dbtimestamp = _getvaluedelegate.EndInvoke(_getvalueresult); DateTime _timestamp = Cache.LastRestoration; if (_dbtimestamp > _timestamp) { Cache.Clear(); Cache.UpdateCacheTimeStamp(_dbtimestamp); } Action <IDbConnection> _inituserdelegate = new Action <IDbConnection>(CreateInitialUser); IAsyncResult _inituserresult = _inituserdelegate.BeginInvoke(SCMS.Connection, null, _inituserdelegate); while (!_inituserresult.IsCompleted && !_cancelled) { Thread.Sleep(1); Application.DoEvents(); } if (_cancelled) { if (!_inituserresult.IsCompleted) { try { _inituserresult = null; } catch { } finally { Materia.RefreshAndManageCurrentProcess(); } } return; } else { Action <IDbConnection> _initcompaniesdelegate = new Action <IDbConnection>(CreateInitialCompanies); IAsyncResult _initcompaniesresult = _initcompaniesdelegate.BeginInvoke(SCMS.Connection, null, _initcompaniesdelegate); while (!_cancelled && !_initcompaniesresult.IsCompleted) { Thread.Sleep(1); Application.DoEvents(); } if (_cancelled) { if (_initcompaniesresult.IsCompleted) { try { _initcompaniesresult = null; } catch { } finally { Materia.RefreshAndManageCurrentProcess(); } } return; } else { SCMS.CurrentSystemUser = null; Materia.RefreshAndManageCurrentProcess(); SystemUserInfo _userinfo = new SystemUserInfo(txtUsername.Text, txtPassword.Text); IAsyncResult _loginasync = _userinfo.LogInAsync(); while (!_loginasync.IsCompleted && !_cancelled) { Thread.Sleep(1); Application.DoEvents(); } if (_cancelled) { if (!_loginasync.IsCompleted) { try { _loginasync = null; } catch { } finally { Materia.RefreshAndManageCurrentProcess(); } } } else { if (_userinfo.IsSignedIn) { SCMS.CurrentSystemUser = _userinfo; InitializerDialog _loader = new InitializerDialog(); _loader.Message = "Gathering application settings for " + SCMS.CurrentCompany.Company + "."; txtPassword.Text = ""; Hide(); _loader.Show(); IAsyncResult _gsresult = GlobalSettings.RefreshAsync(SCMS.CurrentCompany.Company); _gsresult.WaitToFinish(); SCMS.CleanUp(); _loader.Close(); _loader.Dispose(); _loader = null; Materia.RefreshAndManageCurrentProcess(); MainWindow _main = new MainWindow(); _main.Show(); } else { if (_userinfo.IsValidUser && !_userinfo.IsActive) { pnlNotification.Text = "Account needs activation."; pnlNotification.Show(); pnlNotification.BringToFront(); } else { if (_userinfo.IsValidUser && !_userinfo.AccessibleCompanies.Contains(SCMS.CurrentCompany.Company)) { pnlNotification.Text = "Account not allowed in selected company."; pnlNotification.Show(); pnlNotification.BringToFront(); } else { pnlNotification.Text = "Invalid account credentials."; pnlNotification.Show(); pnlNotification.BringToFront(); } } _userinfo = null; Materia.RefreshAndManageCurrentProcess(); } cboCompanies.Enabled = true; cboServers.Enabled = true; btnSignIn.Enabled = true; pctLoad.Hide(); } } } }