/// <summary>
        /// Start processing the user selected Omicron Test Files.
        /// </summary>
        private void StartProcessingFiles()
        {
            int regulatorGeneration = !MyCommons.GenerateRegulators ? 1 : MaximumRegulatorNumber;

            int profileGeneration = !MyCommons.GenerateRegulators ? 2 : 5;

            // Reset File counter.
            MyCommons.CurrentFileNumber = 0;

            // Update FileProcessBar;
            ViewModel.FileProgressBarMax = MyCommons.TotalFileNumber = FileNames.Count * regulatorGeneration * (profileGeneration - 1);

            // Refresh Process bars.
            ViewModel.UpdateCommand.Execute(null);

            // get Change Active Profile value.
            ViewModel.ChangeActiveProfile = MyCommons.ChangeActiveProfileValue;

            try
            {
                // make regulator files.
                for (regulator = 1; regulator <= regulatorGeneration; regulator++)
                {
                    // since original files are always profile 1 need to update this only when regulator changes.
                    ViewModel.FindWhatTextBoxText    = new Regulator().GetValues(regulator, 0, Column.OriginalSettingValue) ?? string.Empty; // would have every profiles
                    ViewModel.ReplaceWithTextBoxText = new Regulator().GetValues(regulator, 0, Column.ReplacementValue) ?? string.Empty;     // would have every profiles

                    // since original files are always profile 1. Change 1 -> whatever is the file profile is.
                    MyCommons.FindProfile = new Regulator().GetValues(regulator, 1, Column.OriginalTestValue) ?? string.Empty;
                    // MyCommons.FindProfile = new Regulator().GetValues(MyCommons.Regulators, regulator, 1, Column.OriginalTestValue);

                    Parallel.ForEach(FileNames, MyCommons.ParallelingOptions, (currentFile) =>
                    {
                        // for (int profile = 1; profile <= 4; profile++)
                        Parallel.For(1, profileGeneration, MyCommons.ParallelingOptions, (profile) =>
                        {
                            // would have Profile x only
                            MyCommons.ReplaceProfile = new Regulator().GetValues(regulator, profile, Column.ReplacementValue) ?? string.Empty;
                            //MyCommons.ReplaceProfile = new Regulator().GetValues(MyCommons.Regulators, regulator, profile, Column.ReplacementValue);

                            activeProfile = profile;

                            SetSearchItems();

                            // Increment current file number;
                            MyCommons.CurrentFileNumber++;
                            MyCommons.CurrentModuleNumber = 0;
                            ViewModel.UpdateCommand.Execute(null);

                            CurrentFileName    = currentFile;
                            MyCommons.FileName = Path.GetFileName(CurrentFileName);

                            try
                            {
                                // opens the document
                                Task.Factory.StartNew(() =>
                                {
                                    try
                                    {
                                        // Polling CancellationToken's status.
                                        // If cancellation requested throw error and exit loop.
                                        if (MyCommons.CancellationToken.IsCancellationRequested == true)
                                        {
                                            MyCommons.CancellationToken.ThrowIfCancellationRequested();
                                        }

                                        // Update DetailsTextBoxText.
                                        ViewModel.DetailsTextBoxText =
                                            MyCommons.LogProcess.Append(
                                                string.Format(
                                                    CultureInfo.InvariantCulture,
                                                    MyResources.Strings_CurrentFileName,
                                                    Environment.NewLine,
                                                    Repeat.StringDuplicate(Settings.Default.RepeatChar, Settings.Default.RepeatNumber),
                                                    Path.GetFileName(CurrentFileName),
                                                    string.Format(CultureInfo.InvariantCulture,
                                                                  MyResources.Strings_TestStart,
                                                                  DateTime.Now)))
                                            .ToString();

                                        // Open Omicron Document.
                                        // this.OmicronDocument = OpenDocument ( this.CurrentFileName, "" );
                                        OmicronDocument = OpenDocument(CurrentFileName);
                                    }
                                    catch (ArgumentException ae)
                                    {
                                        // Save to the fileOutputFolder and print to Debug window if the project build is in Debug.
                                        ErrorHandler.Log(ae, CurrentFileName);
                                        return;
                                    }
                                    catch (AggregateException ae)
                                    {
                                        foreach (Exception ex in ae.InnerExceptions)
                                        {
                                            // Save to the fileOutputFolder and print to Debug window if the project build is in Debug.
                                            ErrorHandler.Log(ex, CurrentFileName);
                                        }
                                        return;
                                    }
                                }
                                                      , MyCommons.CancellationToken)
                                .ContinueWith(scanThread =>
                                {
                                    // scans the document
                                    try
                                    {
                                        // Polling CancellationToken's status.
                                        // If cancellation requested throw error and exit loop.
                                        if (MyCommons.CancellationToken.IsCancellationRequested == true)
                                        {
                                            MyCommons.CancellationToken.ThrowIfCancellationRequested();
                                        }

                                        // Scan the Test Document for the Test Modules.
                                        Scan();
                                    }
                                    catch (AggregateException ae)
                                    {
                                        foreach (Exception ex in ae.InnerExceptions)
                                        {
                                            // Save to the fileOutputFolder and print to Debug window if the project build is in Debug.
                                            ErrorHandler.Log(ex, CurrentFileName);
                                        }
                                        return;
                                    }
                                }
                                              , MyCommons.CancellationToken)
                                .Wait();

                                // Save the new file with a different name.
                                Task.Factory.StartNew(() =>
                                {
                                    try
                                    {
                                        // Polling CancellationToken's status.
                                        // If cancellation requested throw error and exit loop.
                                        if (MyCommons.CancellationToken.IsCancellationRequested == true)
                                        {
                                            MyCommons.CancellationToken.ThrowIfCancellationRequested();
                                        }

                                        // Save the new file.
                                        SaveOmicronFiles(OmicronDocument.FullName, true);
                                    }
                                    catch (AggregateException ae)
                                    {
                                        foreach (Exception ex in ae.InnerExceptions)
                                        {
                                            // Save to the fileOutputFolder and print to Debug window if the project build is in Debug.
                                            ErrorHandler.Log(ex, CurrentFileName);
                                        }
                                        return;
                                    }
                                }
                                                      , MyCommons.CancellationToken)
                                .ContinueWith(closeThread =>
                                {
                                    // close the document
                                    try
                                    {
                                        // Polling CancellationToken's status.
                                        // If cancellation requested throw error and exit loop.
                                        if (MyCommons.CancellationToken.IsCancellationRequested == true)
                                        {
                                            MyCommons.CancellationToken.ThrowIfCancellationRequested();
                                        }

                                        // Close Omicron Control Center without saving any changes to the original file.
                                        OmicronDocument.Close(false);
                                    }
                                    catch (COMException ae)
                                    {
                                        ErrorHandler.Log(ae, CurrentFileName);
                                        return;
                                    }
                                    catch (AggregateException ae)
                                    {
                                        foreach (Exception ex in ae.InnerExceptions)
                                        {
                                            // Save to the fileOutputFolder and print to Debug window if the project build is in Debug.
                                            ErrorHandler.Log(ex, CurrentFileName);
                                        }
                                        return;
                                    }
                                }
                                              , MyCommons.CancellationToken)
                                .ContinueWith(threadCloseApp =>
                                {
                                    // quit the application
                                    try
                                    {
                                        // Polling CancellationToken's status.
                                        // If cancellation requested throw error and exit loop.
                                        if (MyCommons.CancellationToken.IsCancellationRequested == true)
                                        {
                                            MyCommons.CancellationToken.ThrowIfCancellationRequested();
                                        }

                                        // Close Omicron Control Center Application
                                        OmicronApplication.Quit();
                                    }
                                    catch (COMException ae)
                                    {
                                        ErrorHandler.Log(ae, CurrentFileName);
                                    }
                                    catch (ObjectDisposedException ae)
                                    {
                                        ErrorHandler.Log(ae, CurrentFileName);
                                    }
                                    catch (AggregateException ae)
                                    {
                                        foreach (Exception ex in ae.InnerExceptions)
                                        {
                                            // Save to the fileOutputFolder and print to Debug window if the project build is in Debug.
                                            ErrorHandler.Log(ex, CurrentFileName);
                                        }
                                        return;
                                    }
                                }
                                              , MyCommons.CancellationToken)
                                .Wait();

                                // terminate omicron processes.
                                Task.Factory.StartNew(() =>
                                {
                                    try
                                    {
                                        // Garbage Collection.
                                        OmicronApplication = null;
                                        OmicronDocument    = null;

                                        // Polling CancellationToken's status.
                                        // If cancellation requested throw error and exit loop.
                                        if (MyCommons.CancellationToken.IsCancellationRequested == true)
                                        {
                                            MyCommons.CancellationToken.ThrowIfCancellationRequested();
                                        }

                                        // Terminate Omicron Processes.
                                        KillOmicronProcesses();
                                    }
                                    catch (COMException ae)
                                    {
                                        ErrorHandler.Log(ae, CurrentFileName);
                                    }
                                    catch (ObjectDisposedException ae)
                                    {
                                        ErrorHandler.Log(ae, CurrentFileName);
                                    }
                                    catch (AggregateException ae)
                                    {
                                        foreach (Exception ex in ae.InnerExceptions)
                                        {
                                            // Save to the fileOutputFolder and print to Debug window if the project build is in Debug.
                                            ErrorHandler.Log(ex, CurrentFileName);
                                        }
                                        return;
                                    }
                                }
                                                      , MyCommons.CancellationToken)
                                .Wait();

                                ViewModel.FileSideCoverText = MyResources.Strings_FormEndTest;

                                // Refresh Process bars.
                                ViewModel.UpdateCommand.Execute(null);
                            }
                            catch (COMException ae)
                            {
                                ErrorHandler.Log(ae, CurrentFileName);
                            }
                            catch (ObjectDisposedException ae)
                            {
                                ErrorHandler.Log(ae, CurrentFileName);
                            }
                            catch (AggregateException ae)
                            {
                                foreach (Exception ex in ae.InnerExceptions)
                                {
                                    // Save to the fileOutputFolder and print to Debug window if the project build is in Debug.
                                    ErrorHandler.Log(ex, CurrentFileName);
                                }
                                return;
                            }
                        });
                    });
                }
            }
            catch (OperationCanceledException oe)
            {
                // Save to the fileOutputFolder and print to Debug window if the project build is in Debug.
                ErrorHandler.Log(oe, CurrentFileName);
                return;
            }
            catch (COMException ce)
            {
                // Save to the fileOutputFolder and print to Debug window if the project build is in Debug.
                ErrorHandler.Log(ce, CurrentFileName);
                return;
            }
        }
Exemple #2
0
        private OMICRON.OCCenter.IAutoDoc OpenDocuments()
        {
            try
            {
                this.OmicronApplication = new OMICRON.OCCenter.Application();
                OMICRON.OCCenter.IAutoConst occConstants = this.OmicronApplication.Constants;  // occApp.Constants;

                try
                {
                    Debug.WriteLine(string.Format("{1}\tOpening file {0} ---", this.FilePath, DateTime.Now));
                    Debug.WriteLine(string.Format("OPENDOCUMENT thread: {0}", Thread.CurrentThread.GetHashCode()));

                    // Open the document.
                    this.OmicronDocument = this.OmicronApplication.Documents.Open(this.FilePath);

                    // Store Omicron.Application.Protection level to save each new file with same Protection Level
                    // as the original file.
                    this.ProtectionLevel = this.OmicronDocument.Protection;

                    //// TODO: Add logic to perform if ClearAll needed.
                    //// Clear All Test Modules.

                    //Debug.WriteLine("--- ClearAll command is processing ---");
                    //this.OmicronDocument.ClearAll();

                    // Strip down any type of file protection if a file is protected with the user specified password.
                    // If the password does not unlock the file report it and move to the next file in the list.
                    if (!(this.OmicronDocument.Protection == occConstants.cProtectionNoProtection))
                    {
                        // Setting new password to nothing will unlock the file for editing.
                        // For more info: http://briannoyesblog.azurewebsites.net/2015/03/04/wpf-using-passwordbox-in-mvvm/
                        ISecurePasswordToString secureToString = new SecurePasswordToString();
                        string insecurePassword = secureToString.ConvertToInsecureString(this.ViewModel.Password);

                        bool protectionChanged = this.OmicronDocument.ChangeProtection(occConstants.cProtectionNoProtection, insecurePassword, string.Empty);

                        // Clear insecurePassword.
                        insecurePassword = string.Empty;

                        // Verify protection removed.
                        if (!protectionChanged)
                        {
                            MessageBox.Show(MyResources.Strings_ErrorProtectionMessage,
                                            MyResources.Strings_ErrorProtectionCaption,
                                            MessageBoxButton.OK,
                                            MessageBoxImage.Information);

                            // Update DetailsTextBoxText.
                            MyCommons.MyViewModel.DetailsTextBoxText =
                                MyCommons.LogProcess.Append(
                                    string.Format(
                                        CultureInfo.InvariantCulture,
                                        MyResources.Strings_ErrorProtectionLevel,
                                        Environment.NewLine,
                                        this.CurrentFileName,
                                        Repeat.StringDuplicate(Settings.Default.RepeatChar, Settings.Default.RepeatNumber)))
                                .ToString();

                            // Bad password or unknown problem the user interaction necessary.
                            // try to fail gracefully.
                            // TODO: Change default behavior to move to the next file in the line.
                            // Currently stops processing of the files.
                            throw new ArgumentException("password",
                                                        string.Format(CultureInfo.InvariantCulture,
                                                                      MyResources.Strings_ErrorProtectionLevel,
                                                                      Environment.NewLine,
                                                                      this.CurrentFileName,
                                                                      Repeat.StringDuplicate(Settings.Default.RepeatChar, Settings.Default.RepeatNumber)));
                        }
                    }
                }
                catch (ArgumentException ae)
                {
                    // Save to the fileOutputFolder and print to Debug window if the project build is in Debug.
                    ErrorHandler.Log(ae, this.CurrentFileName);

                    // Trying to shut down processing of the file.
                    // cannot change protection of a file.
                    MyCommons.TokenSource.Cancel();

                    return(null);
                }

                // Returns the Omicron Control Center file.
                return(this.OmicronDocument);
            }
            catch (ArgumentException ae)
            {
                // Save to the fileOutputFolder and print to Debug window if the project build is in Debug.
                ErrorHandler.Log(ae, this.CurrentFileName);
                return(null);
            }
            catch (NullReferenceException nre)
            {
                // Save to the fileOutputFolder and print to Debug window if the project build is in Debug.
                ErrorHandler.Log(nre, this.CurrentFileName);
                return(null);
            }
            catch (COMException ce)
            {
                // Save to the fileOutputFolder and print to Debug window if the project build is in Debug.
                ErrorHandler.Log(ce, this.CurrentFileName);
                return(null);
            }
        }
        private void SaveOmicronFile()
        {
            try
            {
                // Polling CancellationToken's status.
                // If cancellation requested throw error and exit loop.
                if (MyCommons.CancellationToken.IsCancellationRequested == true)
                {
                    MyCommons.CancellationToken.ThrowIfCancellationRequested();
                }

                // Setting new password to original password so the user can still have same level protection in the modified files.
                // For more info: http://briannoyesblog.azurewebsites.net/2015/03/04/wpf-using-passwordbox-in-mvvm/
                ISecurePasswordToString secureToString = new SecurePasswordToString();
                string insecurePassword = secureToString.ConvertToInsecureString(ViewModel.Password);

                bool protectionChanged = OmicronDocument.ChangeProtection(ProtectionLevel, string.Empty, insecurePassword);

                // Clear insecurePassword.
                insecurePassword = string.Empty;

                if (!protectionChanged)
                {
                    // Bad password or unknown problem the user interaction necessary.
                    // try to fail gracefully.
                    throw new ArgumentException("protectionChanged");
                }
                else
                {
                    // Depending file exists and saveAs value,
                    // Save the files.
                    if (File.Exists(OldFileName))
                    {
                        if (SaveAs)
                        {
                            OmicronDocument.SaveAs(OldFileName);
                        }
                        else
                        {
                            OmicronDocument.Save();
                        }
                    }
                    else
                    {
                        OmicronDocument.SaveAs(OldFileName);
                    }

                    // Update DetailsTextBoxText.
                    MyCommons.MyViewModel.DetailsTextBoxText =
                        MyCommons.LogProcess.Append(
                            (string.Format(
                                 CultureInfo.InvariantCulture,
                                 MyResources.Strings_TestEnd,
                                 DateTime.Now,
                                 Environment.NewLine,
                                 Repeat.StringDuplicate(Settings.Default.RepeatChar, Settings.Default.RepeatNumber),
                                 MyCommons.FileName,
                                 OldFileName)
                            ))
                        .ToString();
                }
            }
            catch (System.Runtime.InteropServices.COMException ae)
            {
                ErrorHandler.Log(ae, OldFileName);
                return;
            }
            catch (ArgumentException ae)
            {
                // Save to the fileOutputFolder and print to Debug window if the project build is in Debug.
                ErrorHandler.Log(ae, OldFileName);
                return;
            }
            catch (NullReferenceException ne)
            {
                // Save to the fileOutputFolder and print to Debug window if the project build is in Debug.
                ErrorHandler.Log(ne, OldFileName);
                return;
            }
            catch (AggregateException ae)
            {
                foreach (Exception ex in ae.InnerExceptions)
                {
                    // Save to the fileOutputFolder and print to Debug window if the project build is in Debug.
                    ErrorHandler.Log(ex, OldFileName);
                }
                return;
            }
        }