/// <summary> /// Check certain conditions and drops the access database every time PRPO reports are imported. This is /// important becauase the database will continue to grow in size. /// </summary> public static bool DropCreateDb() { bool result = false; // delete the database because after so many imports, the database will continue to get larger and take up space. try { DatabaseConnection.Close(); Logger.Log(AppDirectoryUtils.LogFiles.DbConnectionEvents, "Import Process Started. Database connection dropped."); File.Delete(Configuration.DbPath); AccessUtils.CreateAccessDB(); result = true; } catch (Exception ex) { // Something happened while deleting the file MessageBox.Show(ex.Message, "Database Delete Error", MessageBoxButtons.OK, MessageBoxIcon.Error); Application.Exit(); } return(result); }
/// <summary> /// Triggered when the user drops any file into the region that allows a drop. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void pnl_DragDropArea_DragDrop(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) { string[] filePaths = ((string[])e.Data.GetData(DataFormats.FileDrop)); try { DragDropUtils.ProcessFiles(filePaths); } catch (DragDropExceptions.DragDropFileOverloadException ex) { // An attempt of more than two files were dropped on the form. errorList.Clear(); errorList.Add(ex.Message); using (IOUtils.ErrorMessageBox errmsgbox = new IOUtils.ErrorMessageBox()) { if (errmsgbox.ShowDialog() == DialogResult.OK) { errmsgbox.Close(); } } } catch (DragDropExceptions.DragDropInvalidExtensionException ex) { // Files were dropped that had an invalid file extention errorList.Clear(); errorList.Add(ex.Message); using (IOUtils.ErrorMessageBox errmsgbox = new IOUtils.ErrorMessageBox()) { if (errmsgbox.ShowDialog() == DialogResult.OK) { errmsgbox.Close(); } } } catch (DragDropExceptions.DragDropInvalidExcelFileException ex) { // Files were dropped that were not PRPO files errorList.Clear(); errorList.Add(ex.Message); using (IOUtils.ErrorMessageBox errmsgbox = new IOUtils.ErrorMessageBox()) { if (errmsgbox.ShowDialog() == DialogResult.OK) { errmsgbox.Close(); } } } catch (Exception ex) { errorList.Clear(); errorList.Add(ex.Message); using (IOUtils.ErrorMessageBox errmsgbox = new IOUtils.ErrorMessageBox()) { if (errmsgbox.ShowDialog() == DialogResult.OK) { errmsgbox.Close(); Application.Exit(); } } } Importer.NumberOfImports = filePaths.Length; Importer.ImportComplete = false; Importer.CompletedImports = 0; Importer.ImportProgress += ImportProgress; Importer.importStarted = false; if (ExcelInfo.USUpdated || ExcelInfo.MXUpdated) { overallData = new KPA_KPI_Overall.Overall(); if (AccessUtils.US_PRPO_TableExists || AccessUtils.MX_PRPO_TableExists) { PRPO_DB_Utils.DropCreateDb(); } else { AccessUtils.CreateAccessDB(); } btn_DatabaseConnectionStatus.Invoke((MethodInvoker) delegate { btn_DatabaseConnectionStatus.Image = Properties.Resources.databaseConn_Disconnected_Icon; }); pnl_activePage.Controls.Clear(); pnl_loadingScreen.Visible = true; pnl_loadingScreen.BringToFront(); lbl_loadingStatus.Text = "Importing Data..."; ImportTimer.Start(); } } }
/// <summary> /// This function will check the presence of the database used to read data from. /// </summary> private void CheckDatabase() { try { lbl_CheckStatus.Invoke((MethodInvoker) delegate { lbl_CheckStatus.Text = "Checking for a valid database..."; }); Thread.Sleep(2000); AccessUtils.CheckDatabase(); lbl_CheckStatus.Invoke((MethodInvoker) delegate { lbl_CheckStatus.Text = "Valid database found. Establishing a connection..."; }); conn = new OleDbConnection(AccessUtils.AI.connectionString()); Thread.Sleep(5000); CurrentStatus = CheckStatus.Complete; } catch (TablesDoNotExistException) { // The database exists but the tables did not. We want to keep the current database but we do not want to attempt to connect. CurrentStatus = CheckStatus.Complete; } catch (PRPODatabaseNotFoundException) { // The database did not exist so create a new one before the application starts. lbl_CheckStatus.Invoke((MethodInvoker) delegate { lbl_CheckStatus.Text = "Creating new database..."; }); try { AccessUtils.CreateAccessDB(); } catch (DatabaseCreationFailureException ex) { errorList.Add(ex.Message); using (IOUtils.ErrorMessageBox errmsgbx = new IOUtils.ErrorMessageBox() { Errors = errorList }) { if (errmsgbx.ShowDialog() == DialogResult.OK) { errmsgbx.Close(); Application.Exit(); } } } Thread.Sleep(2000); CurrentStatus = CheckStatus.Complete; } catch (TableNameMismatchException) { // The names of the table are not correct so lets delete the database and create a new one and // wait for the user to drop new files to load. lbl_CheckStatus.Invoke((MethodInvoker) delegate { lbl_CheckStatus.Text = "Database error! Creating new database..."; }); try { File.Delete(Configuration.DbPath); AccessUtils.CreateAccessDB(); Thread.Sleep(2000); CurrentStatus = CheckStatus.Complete; } catch (DatabaseCreationFailureException ex) { errorList.Add(ex.Message); using (IOUtils.ErrorMessageBox errmsgbx = new IOUtils.ErrorMessageBox() { Errors = errorList }) { if (errmsgbx.ShowDialog() == DialogResult.OK) { errmsgbx.Close(); Application.Exit(); } } } catch (Exception ex) { // Something happened while deleting the file errorList.Add(ex.Message); errorList.Add("There was an error while attempting to delete the MS Access Database"); using (IOUtils.ErrorMessageBox errmsgbx = new IOUtils.ErrorMessageBox() { Errors = errorList }) { if (errmsgbx.ShowDialog() == DialogResult.OK) { errmsgbx.Close(); Application.Exit(); } } } } catch (Exception ex) { // Some other type of run-time error occured. errorList.Add(ex.Message); using (IOUtils.ErrorMessageBox errmsgbx = new IOUtils.ErrorMessageBox() { Errors = errorList }) { if (errmsgbx.ShowDialog() == DialogResult.OK) { errmsgbx.Close(); Application.Exit(); } } } }
/// <summary> /// This function will check the files of the application. If they are not there, create them. /// </summary> private void CheckApplicationFiles() { lbl_CheckStatus.Invoke((MethodInvoker) delegate { lbl_CheckStatus.Text = "Checking Application Files..."; }); try { foreach (var file in Enum.GetValues(typeof(IOUtils.AppDirectoryUtils.ResourceFiles))) { if (!File.Exists(Path.Combine(Configuration.AppDir, IOUtils.AppDirectoryUtils.resourceFiles[(int)file]))) { lbl_CheckStatus.Invoke((MethodInvoker) delegate { lbl_CheckStatus.Text = "Creating File - " + IOUtils.AppDirectoryUtils.resourceFiles[(int)file]; }); try { AccessUtils.CreateAccessDB(); } catch (Exception ex) { throw ex; // throw the exception thrown by AccesUtils } Thread.Sleep(2000); } } foreach (var file in Enum.GetValues(typeof(IOUtils.AppDirectoryUtils.ReportFiles))) { if (!File.Exists(Path.Combine(Configuration.AppDir, IOUtils.AppDirectoryUtils.reportFiles[(int)file]))) { lbl_CheckStatus.Invoke((MethodInvoker) delegate { lbl_CheckStatus.Text = "Creating File - " + IOUtils.AppDirectoryUtils.reportFiles[(int)file]; }); File.Create(IOUtils.AppDirectoryUtils.reportFiles[(int)file]); Thread.Sleep(2000); } } foreach (var file in Enum.GetValues(typeof(IOUtils.AppDirectoryUtils.LogFiles))) { if (!File.Exists(Path.Combine(Configuration.AppDir, IOUtils.AppDirectoryUtils.logFiles[(int)file]))) { lbl_CheckStatus.Invoke((MethodInvoker) delegate { lbl_CheckStatus.Text = "Creating File - " + IOUtils.AppDirectoryUtils.logFiles[(int)file]; }); File.Create(Path.Combine(Configuration.AppDir, IOUtils.AppDirectoryUtils.logFiles[(int)file])); Thread.Sleep(2000); } } } catch (DatabaseCreationFailureException ex) { errorList.Add(ex.Message); using (IOUtils.ErrorMessageBox errmsgbx = new IOUtils.ErrorMessageBox() { Errors = errorList }) { if (errmsgbx.ShowDialog() == DialogResult.OK) { errmsgbx.Close(); Application.Exit(); } } } catch (Exception ex) { errorList.Add(ex.Message); using (IOUtils.ErrorMessageBox errmsgbx = new IOUtils.ErrorMessageBox() { Errors = errorList }) { if (errmsgbx.ShowDialog() == DialogResult.OK) { errmsgbx.Close(); } } } Thread.Sleep(2000); CurrentStatus = CheckStatus.Complete; }