Exemplo n.º 1
0
        private void btnImport_Click(object sender, EventArgs e)
        {
            var dialog = new OpenFileDialog();

            dialog.Filter = "XML Files(*.xml)|*.xml";
            if (dialog.ShowDialog(this) != DialogResult.OK)
            {
                return;
            }

            var filePath = dialog.FileName;

            List <AutoQcConfig> readConfigs = new List <AutoQcConfig>();

            try
            {
                using (var stream = new FileStream(filePath, FileMode.Open))
                {
                    using (var reader = XmlReader.Create(stream))
                    {
                        while (reader.IsStartElement())
                        {
                            if (reader.Name == "autoqc_config")
                            {
                                AutoQcConfig config = AutoQcConfig.Deserialize(reader);
                                readConfigs.Add(config);
                            }
                            reader.Read();
                        }
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show(string.Format("Could not import configurations from file {0}", filePath),
                                "Import Configurations Error",
                                MessageBoxButtons.OK);
                return;
            }

            if (readConfigs.Count == 0)
            {
                MessageBox.Show(string.Format("Could not import configurations from file {0}", filePath),
                                "Import Configurations",
                                MessageBoxButtons.OK);
            }

            var validationErrors = new List <string>();
            var duplicateConfigs = new List <string>();
            var numAdded         = 0;

            foreach (AutoQcConfig config in readConfigs)
            {
                // Make sure that the configuration name is unique
                if (GetConfig(config.Name) != null)
                {
                    // If a configuration with the same name already exists, don't add it
                    duplicateConfigs.Add(config.Name);
                    continue;
                }

                try
                {
                    config.Validate();
                }
                catch (Exception ex)
                {
                    validationErrors.Add(string.Format("\"{0}\" Error: {1}", config.Name, ex.Message));
                    continue;
                }

                config.IsEnabled = false;
                AddConfiguration(config);
                numAdded++;
            }

            var message = new StringBuilder("Number of configurations imported: ");

            message.Append(numAdded).Append(Environment.NewLine);
            if (duplicateConfigs.Count > 0)
            {
                message.Append("The following configurations already exist and were not imported:")
                .Append(Environment.NewLine);
                foreach (var name in duplicateConfigs)
                {
                    message.Append("\"").Append(name).Append("\"").Append(Environment.NewLine);
                }
            }
            if (validationErrors.Count > 0)
            {
                message.Append("The following configurations could not be validated and were not imported:")
                .Append(Environment.NewLine);
                foreach (var error in validationErrors)
                {
                    message.Append(error).Append(Environment.NewLine);
                }
            }
            MessageBox.Show(message.ToString(), "Import Configurations", MessageBoxButtons.OK);
        }
Exemplo n.º 2
0
        private void btnImport_Click(object sender, EventArgs e)
        {
            var dialog = new OpenFileDialog();

            dialog.Filter = XML_FILES_FILTER;
            if (dialog.ShowDialog(this) != DialogResult.OK)
            {
                return;
            }

            var filePath = dialog.FileName;

            List <AutoQcConfig> readConfigs = new List <AutoQcConfig>();

            try
            {
                using (var stream = new FileStream(filePath, FileMode.Open))
                {
                    using (var reader = XmlReader.Create(stream))
                    {
                        while (reader.Read())
                        {
                            if (reader.IsStartElement() && reader.Name.Equals(@"autoqc_config"))
                            {
                                AutoQcConfig config = AutoQcConfig.Deserialize(reader);
                                readConfigs.Add(config);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ShowErrorWithExceptionDialog(Resources.MainForm_btnImport_Click_Import_Configurations_Error,
                                             string.Format(Resources.MainForm_btnImport_Click_Could_not_import_configurations_from_file__0_, filePath), ex);
                return;
            }

            if (readConfigs.Count == 0)
            {
                ShowWarningDialog(Resources.MainForm_btnImport_Click_Import_Configurations,
                                  string.Format(Resources.MainForm_btnImport_Click_No_configurations_were_found_in_file__0__,
                                                filePath));
                return;
            }

            var validationErrors = new List <string>();
            var duplicateConfigs = new List <string>();
            var numAdded         = 0;

            foreach (AutoQcConfig config in readConfigs)
            {
                // Make sure that the configuration name is unique
                if (GetConfig(config.Name) != null)
                {
                    // If a configuration with the same name already exists, don't add it
                    duplicateConfigs.Add(config.Name);
                    continue;
                }

                try
                {
                    config.Validate();
                }
                catch (Exception ex)
                {
                    validationErrors.Add(string.Format("\"{0}\" Error: {1}", config.Name, ex.Message));
                    continue;
                }

                config.IsEnabled = false;
                AddConfiguration(config);
                numAdded++;
            }

            var message = new StringBuilder(Resources.MainForm_btnImport_Click_Number_of_configurations_imported__);

            message.Append(numAdded).Append(Environment.NewLine);
            if (duplicateConfigs.Count > 0)
            {
                message.Append(Resources.MainForm_btnImport_Click_The_following_configurations_already_exist_and_were_not_imported_)
                .Append(Environment.NewLine);
                foreach (var name in duplicateConfigs)
                {
                    message.Append("\"").Append(name).Append("\"").Append(Environment.NewLine);
                }
            }
            if (validationErrors.Count > 0)
            {
                message.Append(Resources.MainForm_btnImport_Click_The_following_configurations_could_not_be_validated_and_were_not_imported_)
                .Append(Environment.NewLine);
                foreach (var error in validationErrors)
                {
                    message.Append(error).Append(Environment.NewLine);
                }
            }
            ShowInfoDialog(Resources.MainForm_btnImport_Click_Import_Configurations, message.ToString());
        }