/////////////////////////////////////////////////////////////////////////////// // Public methods // /////////////////////////////////////////////////////////////////////////////// #region PUBLICMETHODS /// <summary> /// Serializes the experiment settings into the given file in a xml structure. /// </summary> /// <remarks>The xml file should have the .oga extension to achieve its affiliation to OGAMA</remarks> /// <param name="settings">The <see cref="ExperimentSettings"/> object to serialize.</param> /// <param name="filePath">Full file path to the .oga xml settings file.</param> /// <returns><strong>True</strong> if successful.</returns> public static bool Serialize(ExperimentSettings settings, string filePath) { try { using (TextWriter writer = new StreamWriter(filePath)) { // Create an instance of the XmlSerializer class; // specify the type of object to serialize XmlSerializer serializer = new XmlSerializer(typeof(ExperimentSettings)); // Serialize the ExperimentSettings, and close the TextWriter. serializer.Serialize(writer, settings); } } catch (Exception ex) { ExceptionMethods.HandleException(ex); return(false); } return(true); }
/// <summary> /// Load experiment settings from settings file given in parameters. /// </summary> /// <param name="filePath">A <see cref="string"/> with the path /// and filename to the settings file.</param> /// <param name="splash">The loading document splash screens background worker, /// needed for interrupting splash when SQL connection fails.</param> /// <returns><strong>True</strong>,if successful, otherwise /// <strong>false</strong>.</returns> public bool LoadSettingsFromFile(string filePath, BackgroundWorker splash) { try { this.experimentSettings = ExperimentSettings.Deserialize(filePath); // If deserialization succeeded update possible copied experiment folders // references. if (this.experimentSettings != null) { string filename = Path.GetFileNameWithoutExtension(filePath); string directory = Path.GetDirectoryName(filePath); // Setup document path and name this.experimentSettings.DocumentPath = directory; // Set path properties of slideshow elements this.experimentSettings.SlideShow.UpdateExperimentPathOfResources( this.experimentSettings.SlideResourcesPath); // Write version information if (!this.experimentSettings.UpdateVersion()) { return false; } this.experimentSettings.Name = filename; // Check if database file is correct located in the connection string // abort if user aborts. if (!this.experimentSettings.CheckDatabasePath()) { return false; } return true; } } catch (WarningException) { // This is the case when the user cancels the upgrading return false; } catch (Exception ex) { ExceptionMethods.HandleException(ex); } return false; }
/////////////////////////////////////////////////////////////////////////////// // Public methods // /////////////////////////////////////////////////////////////////////////////// #region PUBLICMETHODS /// <summary> /// Destruction. Clear member fields and close database connections. /// </summary> public void CleanUp() { //SqlConnection connectionString = new SqlConnection(Document.ActiveDocument.ExperimentSettings.ServerConnectionString); //ServerConnection connection = new ServerConnection(connectionString); //Server sqlServer = new Server(connection); try { //// If there are open connections set offline and online to kill all //// active connections, they are not used anymore. //int connections = sqlServer.GetActiveDBConnectionCount(Document.ActiveDocument.ExperimentSettings.Name); //if (connections > 0) //{ // string query = "ALTER DATABASE \"" + Document.ActiveDocument.ExperimentSettings.Name + // "\" SET OFFLINE WITH ROLLBACK IMMEDIATE;"; // Queries.ExecuteSQLCommand(query); // query = "ALTER DATABASE \"" + Document.ActiveDocument.ExperimentSettings.Name + // "\" SET ONLINE;"; // Queries.ExecuteSQLCommand(query); //} // Close database connection if (this.dataSet != null) { this.dataSet.Dispose(); } //// Detach database from user instance //if (sqlServer.Databases.Contains(Document.ActiveDocument.ExperimentSettings.Name)) //{ // sqlServer.DetachDatabase(Document.ActiveDocument.ExperimentSettings.Name, true); //} } catch (Exception ex) { ExceptionMethods.HandleException(ex); } activeDocument = null; this.isModified = false; this.experimentSettings = null; this.selectionState = null; }
/////////////////////////////////////////////////////////////////////////////// // Construction and Initializing methods // /////////////////////////////////////////////////////////////////////////////// #region CONSTRUCTION /// <summary> /// Initializes a new instance of the Document class. /// </summary> public Document() { activeDocument = this; this.isModified = false; this.experimentSettings = new ExperimentSettings(); this.selectionState = new SelectionsState(); }
/// <summary> /// The <see cref="Control.Click"/> event handler /// for the <see cref="ToolStripMenuItem"/> <see cref="mnuFileNewExperiment"/> /// Creates new experiment through creating new database and /// asks for stimulus path and screen size. /// </summary> /// <param name="sender">Source of the event</param> /// <param name="e">An empty <see cref="EventArgs"/></param> private void mnuFileNewExperiment_Click(object sender, EventArgs e) { if (Document.ActiveDocument == null) { // Show new experiment storage dialog. NewExperiment objfrmNewExperimentDlg = new NewExperiment(); if (objfrmNewExperimentDlg.ShowDialog() != DialogResult.OK) { return; } this.Cursor = Cursors.WaitCursor; string newExperimentFolder = Path.Combine( objfrmNewExperimentDlg.ParentFolder, objfrmNewExperimentDlg.ExperimentName); string newExperimentName = objfrmNewExperimentDlg.ExperimentName; string newExperimentFilename = Path.Combine(newExperimentFolder, newExperimentName + ".oga"); Document.ActiveDocument = new Document(); ExperimentSettings newSettings = new ExperimentSettings(); // Show new experiment properties dialog. ExperimentSettingsDialog popUpDlg = new ExperimentSettingsDialog(); if (popUpDlg.ShowDialog() == DialogResult.OK) { newSettings = popUpDlg.ExperimentSettings; newSettings.DocumentPath = newExperimentFolder; newSettings.Name = newExperimentName; newSettings.UpdateVersion(); } else { return; } // Show loading splash screen this.bgwLoad.RunWorkerAsync(); Application.DoEvents(); Directory.CreateDirectory(newExperimentFolder); Directory.CreateDirectory(newSettings.ThumbsPath); Directory.CreateDirectory(newSettings.DatabasePath); Directory.CreateDirectory(newSettings.SlideResourcesPath); Document.ActiveDocument.ExperimentSettings = newSettings; string sqliteSource = Application.StartupPath + "\\DataSet\\OgamaDatabaseTemplate.db"; if (File.Exists(sqliteSource)) { string sqliteDBDestination = newSettings.DatabaseSQLiteFile; bool overwrite = false; bool skip = false; if (File.Exists(sqliteDBDestination)) { // Hide loading splash screen this.bgwLoad.CancelAsync(); if (MessageBox.Show( "Overwrite existing Database File " + Environment.NewLine + sqliteDBDestination, Application.ProductName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { overwrite = true; } else { skip = true; } // Show loading splash screen again this.bgwLoad.RunWorkerAsync(); Application.DoEvents(); } try { if (!skip) { File.Copy(sqliteSource, sqliteDBDestination, overwrite); } } catch (ArgumentException ex) { ExceptionMethods.ProcessErrorMessage(ex.Message); } catch (IOException ex) { ExceptionMethods.ProcessErrorMessage(ex.Message); } catch (NotSupportedException ex) { ExceptionMethods.ProcessErrorMessage(ex.Message); } catch (UnauthorizedAccessException ex) { ExceptionMethods.ProcessErrorMessage(ex.Message); } catch (Exception ex) { ExceptionMethods.HandleException(ex); } if (!Document.ActiveDocument.SaveDocument(newExperimentFilename, this.bgwLoad)) { ExceptionMethods.ProcessErrorMessage("Couldn't create document"); this.ChangeMenuItems(false); if (Document.ActiveDocument != null) { this.CleanUpDocument(); } } else { if (!Document.ActiveDocument.LoadSQLData(this.bgwLoad)) { ExceptionMethods.ProcessErrorMessage("Couldn't create document, because database loading failed."); this.ChangeMenuItems(false); if (Document.ActiveDocument != null) { this.CleanUpDocument(); } } else { this.contextPanel.Init(); RecentFilesList.List.Add(newExperimentFilename); } // Hide loading splash screen this.bgwLoad.CancelAsync(); if (Document.ActiveDocument != null) { this.ShowTaskChooseDialog(); this.ChangeMenuItems(true); } } } else { // DataBaseSourceFile does not exist in the expected directory string message = "Could not find DataBaseTemplate File:" + Environment.NewLine; message += sqliteSource; message += Environment.NewLine + "Please make sure the File OgamaDatabaseTemplate.db (supplied with the application installation) is in the directory listed before and try again."; ExceptionMethods.ProcessErrorMessage(message); } // Hide loading splash screen this.bgwLoad.CancelAsync(); } else { // mDocument.ActiveDocument!=null string message = "Couldn't create new experiment, because there is one already open." + Environment.NewLine + "Please close it using File -> Close Experiment."; ExceptionMethods.ProcessErrorMessage(message); } this.Cursor = Cursors.Default; }
/////////////////////////////////////////////////////////////////////////////// // Public methods // /////////////////////////////////////////////////////////////////////////////// #region PUBLICMETHODS /// <summary> /// Serializes the experiment settings into the given file in a xml structure. /// </summary> /// <remarks>The xml file should have the .oga extension to achieve its affiliation to OGAMA</remarks> /// <param name="settings">The <see cref="ExperimentSettings"/> object to serialize.</param> /// <param name="filePath">Full file path to the .oga xml settings file.</param> /// <returns><strong>True</strong> if successful.</returns> public static bool Serialize(ExperimentSettings settings, string filePath) { try { using (TextWriter writer = new StreamWriter(filePath)) { // Create an instance of the XmlSerializer class; // specify the type of object to serialize XmlSerializer serializer = new XmlSerializer(typeof(ExperimentSettings)); // Serialize the ExperimentSettings, and close the TextWriter. serializer.Serialize(writer, settings); } } catch (Exception ex) { ExceptionMethods.HandleException(ex); return false; } return true; }